10b57cec5SDimitry Andric //===-- AArch64ConditionalCompares.cpp --- CCMP formation for AArch64 -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the AArch64ConditionalCompares pass which reduces
100b57cec5SDimitry Andric // branching and code size by using the conditional compare instructions CCMP,
110b57cec5SDimitry Andric // CCMN, and FCMP.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // The CFG transformations for forming conditional compares are very similar to
140b57cec5SDimitry Andric // if-conversion, and this pass should run immediately before the early
150b57cec5SDimitry Andric // if-conversion pass.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "AArch64.h"
200b57cec5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
210b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineTraceMetrics.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
34480093f4SDimitry Andric #include "llvm/InitializePasses.h"
350b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
360b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-ccmp"
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric // Absolute maximum number of instructions allowed per speculated block.
440b57cec5SDimitry Andric // This bypasses all other heuristics, so it should be set fairly high.
450b57cec5SDimitry Andric static cl::opt<unsigned> BlockInstrLimit(
460b57cec5SDimitry Andric     "aarch64-ccmp-limit", cl::init(30), cl::Hidden,
470b57cec5SDimitry Andric     cl::desc("Maximum number of instructions per speculated block."));
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric // Stress testing mode - disable heuristics.
500b57cec5SDimitry Andric static cl::opt<bool> Stress("aarch64-stress-ccmp", cl::Hidden,
510b57cec5SDimitry Andric                             cl::desc("Turn all knobs to 11"));
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric STATISTIC(NumConsidered, "Number of ccmps considered");
540b57cec5SDimitry Andric STATISTIC(NumPhiRejs, "Number of ccmps rejected (PHI)");
550b57cec5SDimitry Andric STATISTIC(NumPhysRejs, "Number of ccmps rejected (Physregs)");
560b57cec5SDimitry Andric STATISTIC(NumPhi2Rejs, "Number of ccmps rejected (PHI2)");
570b57cec5SDimitry Andric STATISTIC(NumHeadBranchRejs, "Number of ccmps rejected (Head branch)");
580b57cec5SDimitry Andric STATISTIC(NumCmpBranchRejs, "Number of ccmps rejected (CmpBB branch)");
590b57cec5SDimitry Andric STATISTIC(NumCmpTermRejs, "Number of ccmps rejected (CmpBB is cbz...)");
600b57cec5SDimitry Andric STATISTIC(NumImmRangeRejs, "Number of ccmps rejected (Imm out of range)");
610b57cec5SDimitry Andric STATISTIC(NumLiveDstRejs, "Number of ccmps rejected (Cmp dest live)");
620b57cec5SDimitry Andric STATISTIC(NumMultNZCVUses, "Number of ccmps rejected (NZCV used)");
630b57cec5SDimitry Andric STATISTIC(NumUnknNZCVDefs, "Number of ccmps rejected (NZCV def unknown)");
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric STATISTIC(NumSpeculateRejs, "Number of ccmps rejected (Can't speculate)");
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric STATISTIC(NumConverted, "Number of ccmp instructions created");
680b57cec5SDimitry Andric STATISTIC(NumCompBranches, "Number of cbz/cbnz branches converted");
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
710b57cec5SDimitry Andric //                                 SSACCmpConv
720b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
730b57cec5SDimitry Andric //
740b57cec5SDimitry Andric // The SSACCmpConv class performs ccmp-conversion on SSA form machine code
750b57cec5SDimitry Andric // after determining if it is possible. The class contains no heuristics;
760b57cec5SDimitry Andric // external code should be used to determine when ccmp-conversion is a good
770b57cec5SDimitry Andric // idea.
780b57cec5SDimitry Andric //
790b57cec5SDimitry Andric // CCmp-formation works on a CFG representing chained conditions, typically
800b57cec5SDimitry Andric // from C's short-circuit || and && operators:
810b57cec5SDimitry Andric //
820b57cec5SDimitry Andric //   From:         Head            To:         Head
830b57cec5SDimitry Andric //                 / |                         CmpBB
840b57cec5SDimitry Andric //                /  |                         / |
850b57cec5SDimitry Andric //               |  CmpBB                     /  |
860b57cec5SDimitry Andric //               |  / |                    Tail  |
870b57cec5SDimitry Andric //               | /  |                      |   |
880b57cec5SDimitry Andric //              Tail  |                      |   |
890b57cec5SDimitry Andric //                |   |                      |   |
900b57cec5SDimitry Andric //               ... ...                    ... ...
910b57cec5SDimitry Andric //
920b57cec5SDimitry Andric // The Head block is terminated by a br.cond instruction, and the CmpBB block
930b57cec5SDimitry Andric // contains compare + br.cond. Tail must be a successor of both.
940b57cec5SDimitry Andric //
950b57cec5SDimitry Andric // The cmp-conversion turns the compare instruction in CmpBB into a conditional
960b57cec5SDimitry Andric // compare, and merges CmpBB into Head, speculatively executing its
970b57cec5SDimitry Andric // instructions. The AArch64 conditional compare instructions have an immediate
980b57cec5SDimitry Andric // operand that specifies the NZCV flag values when the condition is false and
990b57cec5SDimitry Andric // the compare isn't executed. This makes it possible to chain compares with
1000b57cec5SDimitry Andric // different condition codes.
1010b57cec5SDimitry Andric //
1020b57cec5SDimitry Andric // Example:
1030b57cec5SDimitry Andric //
1040b57cec5SDimitry Andric //    if (a == 5 || b == 17)
1050b57cec5SDimitry Andric //      foo();
1060b57cec5SDimitry Andric //
1070b57cec5SDimitry Andric //    Head:
1080b57cec5SDimitry Andric //       cmp  w0, #5
1090b57cec5SDimitry Andric //       b.eq Tail
1100b57cec5SDimitry Andric //    CmpBB:
1110b57cec5SDimitry Andric //       cmp  w1, #17
1120b57cec5SDimitry Andric //       b.eq Tail
1130b57cec5SDimitry Andric //    ...
1140b57cec5SDimitry Andric //    Tail:
1150b57cec5SDimitry Andric //      bl _foo
1160b57cec5SDimitry Andric //
1170b57cec5SDimitry Andric //  Becomes:
1180b57cec5SDimitry Andric //
1190b57cec5SDimitry Andric //    Head:
1200b57cec5SDimitry Andric //       cmp  w0, #5
1210b57cec5SDimitry Andric //       ccmp w1, #17, 4, ne  ; 4 = nZcv
1220b57cec5SDimitry Andric //       b.eq Tail
1230b57cec5SDimitry Andric //    ...
1240b57cec5SDimitry Andric //    Tail:
1250b57cec5SDimitry Andric //      bl _foo
1260b57cec5SDimitry Andric //
1270b57cec5SDimitry Andric // The ccmp condition code is the one that would cause the Head terminator to
1280b57cec5SDimitry Andric // branch to CmpBB.
1290b57cec5SDimitry Andric //
1300b57cec5SDimitry Andric // FIXME: It should also be possible to speculate a block on the critical edge
1310b57cec5SDimitry Andric // between Head and Tail, just like if-converting a diamond.
1320b57cec5SDimitry Andric //
1330b57cec5SDimitry Andric // FIXME: Handle PHIs in Tail by turning them into selects (if-conversion).
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric namespace {
1360b57cec5SDimitry Andric class SSACCmpConv {
1370b57cec5SDimitry Andric   MachineFunction *MF;
1380b57cec5SDimitry Andric   const TargetInstrInfo *TII;
1390b57cec5SDimitry Andric   const TargetRegisterInfo *TRI;
1400b57cec5SDimitry Andric   MachineRegisterInfo *MRI;
1410b57cec5SDimitry Andric   const MachineBranchProbabilityInfo *MBPI;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric public:
1440b57cec5SDimitry Andric   /// The first block containing a conditional branch, dominating everything
1450b57cec5SDimitry Andric   /// else.
1460b57cec5SDimitry Andric   MachineBasicBlock *Head;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   /// The block containing cmp+br.cond with a successor shared with Head.
1490b57cec5SDimitry Andric   MachineBasicBlock *CmpBB;
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   /// The common successor for Head and CmpBB.
1520b57cec5SDimitry Andric   MachineBasicBlock *Tail;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   /// The compare instruction in CmpBB that can be converted to a ccmp.
1550b57cec5SDimitry Andric   MachineInstr *CmpMI;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric private:
1585ffd83dbSDimitry Andric   /// The branch condition in Head as determined by analyzeBranch.
1590b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> HeadCond;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   /// The condition code that makes Head branch to CmpBB.
1620b57cec5SDimitry Andric   AArch64CC::CondCode HeadCmpBBCC;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   /// The branch condition in CmpBB.
1650b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> CmpBBCond;
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   /// The condition code that makes CmpBB branch to Tail.
1680b57cec5SDimitry Andric   AArch64CC::CondCode CmpBBTailCC;
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   /// Check if the Tail PHIs are trivially convertible.
1710b57cec5SDimitry Andric   bool trivialTailPHIs();
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   /// Remove CmpBB from the Tail PHIs.
1740b57cec5SDimitry Andric   void updateTailPHIs();
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   /// Check if an operand defining DstReg is dead.
1770b57cec5SDimitry Andric   bool isDeadDef(unsigned DstReg);
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   /// Find the compare instruction in MBB that controls the conditional branch.
1800b57cec5SDimitry Andric   /// Return NULL if a convertible instruction can't be found.
1810b57cec5SDimitry Andric   MachineInstr *findConvertibleCompare(MachineBasicBlock *MBB);
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   /// Return true if all non-terminator instructions in MBB can be safely
1840b57cec5SDimitry Andric   /// speculated.
1850b57cec5SDimitry Andric   bool canSpeculateInstrs(MachineBasicBlock *MBB, const MachineInstr *CmpMI);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric public:
1880b57cec5SDimitry Andric   /// runOnMachineFunction - Initialize per-function data structures.
runOnMachineFunction(MachineFunction & MF,const MachineBranchProbabilityInfo * MBPI)1890b57cec5SDimitry Andric   void runOnMachineFunction(MachineFunction &MF,
1900b57cec5SDimitry Andric                             const MachineBranchProbabilityInfo *MBPI) {
1910b57cec5SDimitry Andric     this->MF = &MF;
1920b57cec5SDimitry Andric     this->MBPI = MBPI;
1930b57cec5SDimitry Andric     TII = MF.getSubtarget().getInstrInfo();
1940b57cec5SDimitry Andric     TRI = MF.getSubtarget().getRegisterInfo();
1950b57cec5SDimitry Andric     MRI = &MF.getRegInfo();
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   /// If the sub-CFG headed by MBB can be cmp-converted, initialize the
1990b57cec5SDimitry Andric   /// internal state, and return true.
2000b57cec5SDimitry Andric   bool canConvert(MachineBasicBlock *MBB);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   /// Cmo-convert the last block passed to canConvertCmp(), assuming
2030b57cec5SDimitry Andric   /// it is possible. Add any erased blocks to RemovedBlocks.
2040b57cec5SDimitry Andric   void convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks);
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   /// Return the expected code size delta if the conversion into a
2070b57cec5SDimitry Andric   /// conditional compare is performed.
2080b57cec5SDimitry Andric   int expectedCodeSizeDelta() const;
2090b57cec5SDimitry Andric };
2100b57cec5SDimitry Andric } // end anonymous namespace
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric // Check that all PHIs in Tail are selecting the same value from Head and CmpBB.
2130b57cec5SDimitry Andric // This means that no if-conversion is required when merging CmpBB into Head.
trivialTailPHIs()2140b57cec5SDimitry Andric bool SSACCmpConv::trivialTailPHIs() {
2150b57cec5SDimitry Andric   for (auto &I : *Tail) {
2160b57cec5SDimitry Andric     if (!I.isPHI())
2170b57cec5SDimitry Andric       break;
2180b57cec5SDimitry Andric     unsigned HeadReg = 0, CmpBBReg = 0;
2190b57cec5SDimitry Andric     // PHI operands come in (VReg, MBB) pairs.
2200b57cec5SDimitry Andric     for (unsigned oi = 1, oe = I.getNumOperands(); oi != oe; oi += 2) {
2210b57cec5SDimitry Andric       MachineBasicBlock *MBB = I.getOperand(oi + 1).getMBB();
2228bcb0991SDimitry Andric       Register Reg = I.getOperand(oi).getReg();
2230b57cec5SDimitry Andric       if (MBB == Head) {
2240b57cec5SDimitry Andric         assert((!HeadReg || HeadReg == Reg) && "Inconsistent PHI operands");
2250b57cec5SDimitry Andric         HeadReg = Reg;
2260b57cec5SDimitry Andric       }
2270b57cec5SDimitry Andric       if (MBB == CmpBB) {
2280b57cec5SDimitry Andric         assert((!CmpBBReg || CmpBBReg == Reg) && "Inconsistent PHI operands");
2290b57cec5SDimitry Andric         CmpBBReg = Reg;
2300b57cec5SDimitry Andric       }
2310b57cec5SDimitry Andric     }
2320b57cec5SDimitry Andric     if (HeadReg != CmpBBReg)
2330b57cec5SDimitry Andric       return false;
2340b57cec5SDimitry Andric   }
2350b57cec5SDimitry Andric   return true;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric // Assuming that trivialTailPHIs() is true, update the Tail PHIs by simply
2390b57cec5SDimitry Andric // removing the CmpBB operands. The Head operands will be identical.
updateTailPHIs()2400b57cec5SDimitry Andric void SSACCmpConv::updateTailPHIs() {
2410b57cec5SDimitry Andric   for (auto &I : *Tail) {
2420b57cec5SDimitry Andric     if (!I.isPHI())
2430b57cec5SDimitry Andric       break;
2440b57cec5SDimitry Andric     // I is a PHI. It can have multiple entries for CmpBB.
2450b57cec5SDimitry Andric     for (unsigned oi = I.getNumOperands(); oi > 2; oi -= 2) {
2460b57cec5SDimitry Andric       // PHI operands are (Reg, MBB) at (oi-2, oi-1).
2470b57cec5SDimitry Andric       if (I.getOperand(oi - 1).getMBB() == CmpBB) {
24881ad6265SDimitry Andric         I.removeOperand(oi - 1);
24981ad6265SDimitry Andric         I.removeOperand(oi - 2);
2500b57cec5SDimitry Andric       }
2510b57cec5SDimitry Andric     }
2520b57cec5SDimitry Andric   }
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric // This pass runs before the AArch64DeadRegisterDefinitions pass, so compares
2560b57cec5SDimitry Andric // are still writing virtual registers without any uses.
isDeadDef(unsigned DstReg)2570b57cec5SDimitry Andric bool SSACCmpConv::isDeadDef(unsigned DstReg) {
2580b57cec5SDimitry Andric   // Writes to the zero register are dead.
2590b57cec5SDimitry Andric   if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
2600b57cec5SDimitry Andric     return true;
2618bcb0991SDimitry Andric   if (!Register::isVirtualRegister(DstReg))
2620b57cec5SDimitry Andric     return false;
2630b57cec5SDimitry Andric   // A virtual register def without any uses will be marked dead later, and
2640b57cec5SDimitry Andric   // eventually replaced by the zero register.
2650b57cec5SDimitry Andric   return MRI->use_nodbg_empty(DstReg);
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2685ffd83dbSDimitry Andric // Parse a condition code returned by analyzeBranch, and compute the CondCode
2690b57cec5SDimitry Andric // corresponding to TBB.
2700b57cec5SDimitry Andric // Return
parseCond(ArrayRef<MachineOperand> Cond,AArch64CC::CondCode & CC)2710b57cec5SDimitry Andric static bool parseCond(ArrayRef<MachineOperand> Cond, AArch64CC::CondCode &CC) {
2720b57cec5SDimitry Andric   // A normal br.cond simply has the condition code.
2730b57cec5SDimitry Andric   if (Cond[0].getImm() != -1) {
2740b57cec5SDimitry Andric     assert(Cond.size() == 1 && "Unknown Cond array format");
2750b57cec5SDimitry Andric     CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
2760b57cec5SDimitry Andric     return true;
2770b57cec5SDimitry Andric   }
2780b57cec5SDimitry Andric   // For tbz and cbz instruction, the opcode is next.
2790b57cec5SDimitry Andric   switch (Cond[1].getImm()) {
2800b57cec5SDimitry Andric   default:
2810b57cec5SDimitry Andric     // This includes tbz / tbnz branches which can't be converted to
2820b57cec5SDimitry Andric     // ccmp + br.cond.
2830b57cec5SDimitry Andric     return false;
2840b57cec5SDimitry Andric   case AArch64::CBZW:
2850b57cec5SDimitry Andric   case AArch64::CBZX:
2860b57cec5SDimitry Andric     assert(Cond.size() == 3 && "Unknown Cond array format");
2870b57cec5SDimitry Andric     CC = AArch64CC::EQ;
2880b57cec5SDimitry Andric     return true;
2890b57cec5SDimitry Andric   case AArch64::CBNZW:
2900b57cec5SDimitry Andric   case AArch64::CBNZX:
2910b57cec5SDimitry Andric     assert(Cond.size() == 3 && "Unknown Cond array format");
2920b57cec5SDimitry Andric     CC = AArch64CC::NE;
2930b57cec5SDimitry Andric     return true;
2940b57cec5SDimitry Andric   }
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
findConvertibleCompare(MachineBasicBlock * MBB)2970b57cec5SDimitry Andric MachineInstr *SSACCmpConv::findConvertibleCompare(MachineBasicBlock *MBB) {
2980b57cec5SDimitry Andric   MachineBasicBlock::iterator I = MBB->getFirstTerminator();
2990b57cec5SDimitry Andric   if (I == MBB->end())
3000b57cec5SDimitry Andric     return nullptr;
3010b57cec5SDimitry Andric   // The terminator must be controlled by the flags.
3020b57cec5SDimitry Andric   if (!I->readsRegister(AArch64::NZCV)) {
3030b57cec5SDimitry Andric     switch (I->getOpcode()) {
3040b57cec5SDimitry Andric     case AArch64::CBZW:
3050b57cec5SDimitry Andric     case AArch64::CBZX:
3060b57cec5SDimitry Andric     case AArch64::CBNZW:
3070b57cec5SDimitry Andric     case AArch64::CBNZX:
3080b57cec5SDimitry Andric       // These can be converted into a ccmp against #0.
3090b57cec5SDimitry Andric       return &*I;
3100b57cec5SDimitry Andric     }
3110b57cec5SDimitry Andric     ++NumCmpTermRejs;
3120b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Flags not used by terminator: " << *I);
3130b57cec5SDimitry Andric     return nullptr;
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // Now find the instruction controlling the terminator.
3170b57cec5SDimitry Andric   for (MachineBasicBlock::iterator B = MBB->begin(); I != B;) {
3185ffd83dbSDimitry Andric     I = prev_nodbg(I, MBB->begin());
3190b57cec5SDimitry Andric     assert(!I->isTerminator() && "Spurious terminator");
3200b57cec5SDimitry Andric     switch (I->getOpcode()) {
3210b57cec5SDimitry Andric     // cmp is an alias for subs with a dead destination register.
3220b57cec5SDimitry Andric     case AArch64::SUBSWri:
3230b57cec5SDimitry Andric     case AArch64::SUBSXri:
3240b57cec5SDimitry Andric     // cmn is an alias for adds with a dead destination register.
3250b57cec5SDimitry Andric     case AArch64::ADDSWri:
3260b57cec5SDimitry Andric     case AArch64::ADDSXri:
3270b57cec5SDimitry Andric       // Check that the immediate operand is within range, ccmp wants a uimm5.
3280b57cec5SDimitry Andric       // Rd = SUBSri Rn, imm, shift
3290b57cec5SDimitry Andric       if (I->getOperand(3).getImm() || !isUInt<5>(I->getOperand(2).getImm())) {
3300b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Immediate out of range for ccmp: " << *I);
3310b57cec5SDimitry Andric         ++NumImmRangeRejs;
3320b57cec5SDimitry Andric         return nullptr;
3330b57cec5SDimitry Andric       }
334bdd1243dSDimitry Andric       [[fallthrough]];
3350b57cec5SDimitry Andric     case AArch64::SUBSWrr:
3360b57cec5SDimitry Andric     case AArch64::SUBSXrr:
3370b57cec5SDimitry Andric     case AArch64::ADDSWrr:
3380b57cec5SDimitry Andric     case AArch64::ADDSXrr:
3390b57cec5SDimitry Andric       if (isDeadDef(I->getOperand(0).getReg()))
3400b57cec5SDimitry Andric         return &*I;
3410b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't convert compare with live destination: "
3420b57cec5SDimitry Andric                         << *I);
3430b57cec5SDimitry Andric       ++NumLiveDstRejs;
3440b57cec5SDimitry Andric       return nullptr;
3450b57cec5SDimitry Andric     case AArch64::FCMPSrr:
3460b57cec5SDimitry Andric     case AArch64::FCMPDrr:
3470b57cec5SDimitry Andric     case AArch64::FCMPESrr:
3480b57cec5SDimitry Andric     case AArch64::FCMPEDrr:
3490b57cec5SDimitry Andric       return &*I;
3500b57cec5SDimitry Andric     }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric     // Check for flag reads and clobbers.
353480093f4SDimitry Andric     PhysRegInfo PRI = AnalyzePhysRegInBundle(*I, AArch64::NZCV, TRI);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     if (PRI.Read) {
3560b57cec5SDimitry Andric       // The ccmp doesn't produce exactly the same flags as the original
3570b57cec5SDimitry Andric       // compare, so reject the transform if there are uses of the flags
3580b57cec5SDimitry Andric       // besides the terminators.
3590b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't create ccmp with multiple uses: " << *I);
3600b57cec5SDimitry Andric       ++NumMultNZCVUses;
3610b57cec5SDimitry Andric       return nullptr;
3620b57cec5SDimitry Andric     }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric     if (PRI.Defined || PRI.Clobbered) {
3650b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Not convertible compare: " << *I);
3660b57cec5SDimitry Andric       ++NumUnknNZCVDefs;
3670b57cec5SDimitry Andric       return nullptr;
3680b57cec5SDimitry Andric     }
3690b57cec5SDimitry Andric   }
3700b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Flags not defined in " << printMBBReference(*MBB)
3710b57cec5SDimitry Andric                     << '\n');
3720b57cec5SDimitry Andric   return nullptr;
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric /// Determine if all the instructions in MBB can safely
3760b57cec5SDimitry Andric /// be speculated. The terminators are not considered.
3770b57cec5SDimitry Andric ///
3780b57cec5SDimitry Andric /// Only CmpMI is allowed to clobber the flags.
3790b57cec5SDimitry Andric ///
canSpeculateInstrs(MachineBasicBlock * MBB,const MachineInstr * CmpMI)3800b57cec5SDimitry Andric bool SSACCmpConv::canSpeculateInstrs(MachineBasicBlock *MBB,
3810b57cec5SDimitry Andric                                      const MachineInstr *CmpMI) {
3820b57cec5SDimitry Andric   // Reject any live-in physregs. It's probably NZCV/EFLAGS, and very hard to
3830b57cec5SDimitry Andric   // get right.
3840b57cec5SDimitry Andric   if (!MBB->livein_empty()) {
3850b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has live-ins.\n");
3860b57cec5SDimitry Andric     return false;
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   unsigned InstrCount = 0;
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   // Check all instructions, except the terminators. It is assumed that
3920b57cec5SDimitry Andric   // terminators never have side effects or define any used register values.
3930b57cec5SDimitry Andric   for (auto &I : make_range(MBB->begin(), MBB->getFirstTerminator())) {
3940b57cec5SDimitry Andric     if (I.isDebugInstr())
3950b57cec5SDimitry Andric       continue;
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric     if (++InstrCount > BlockInstrLimit && !Stress) {
3980b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " has more than "
3990b57cec5SDimitry Andric                         << BlockInstrLimit << " instructions.\n");
4000b57cec5SDimitry Andric       return false;
4010b57cec5SDimitry Andric     }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric     // There shouldn't normally be any phis in a single-predecessor block.
4040b57cec5SDimitry Andric     if (I.isPHI()) {
4050b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't hoist: " << I);
4060b57cec5SDimitry Andric       return false;
4070b57cec5SDimitry Andric     }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric     // Don't speculate loads. Note that it may be possible and desirable to
4100b57cec5SDimitry Andric     // speculate GOT or constant pool loads that are guaranteed not to trap,
4110b57cec5SDimitry Andric     // but we don't support that for now.
4120b57cec5SDimitry Andric     if (I.mayLoad()) {
4130b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Won't speculate load: " << I);
4140b57cec5SDimitry Andric       return false;
4150b57cec5SDimitry Andric     }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric     // We never speculate stores, so an AA pointer isn't necessary.
4180b57cec5SDimitry Andric     bool DontMoveAcrossStore = true;
4190b57cec5SDimitry Andric     if (!I.isSafeToMove(nullptr, DontMoveAcrossStore)) {
4200b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Can't speculate: " << I);
4210b57cec5SDimitry Andric       return false;
4220b57cec5SDimitry Andric     }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric     // Only CmpMI is allowed to clobber the flags.
4250b57cec5SDimitry Andric     if (&I != CmpMI && I.modifiesRegister(AArch64::NZCV, TRI)) {
4260b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Clobbers flags: " << I);
4270b57cec5SDimitry Andric       return false;
4280b57cec5SDimitry Andric     }
4290b57cec5SDimitry Andric   }
4300b57cec5SDimitry Andric   return true;
4310b57cec5SDimitry Andric }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric /// Analyze the sub-cfg rooted in MBB, and return true if it is a potential
4340b57cec5SDimitry Andric /// candidate for cmp-conversion. Fill out the internal state.
4350b57cec5SDimitry Andric ///
canConvert(MachineBasicBlock * MBB)4360b57cec5SDimitry Andric bool SSACCmpConv::canConvert(MachineBasicBlock *MBB) {
4370b57cec5SDimitry Andric   Head = MBB;
4380b57cec5SDimitry Andric   Tail = CmpBB = nullptr;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   if (Head->succ_size() != 2)
4410b57cec5SDimitry Andric     return false;
4420b57cec5SDimitry Andric   MachineBasicBlock *Succ0 = Head->succ_begin()[0];
4430b57cec5SDimitry Andric   MachineBasicBlock *Succ1 = Head->succ_begin()[1];
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   // CmpBB can only have a single predecessor. Tail is allowed many.
4460b57cec5SDimitry Andric   if (Succ0->pred_size() != 1)
4470b57cec5SDimitry Andric     std::swap(Succ0, Succ1);
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // Succ0 is our candidate for CmpBB.
4500b57cec5SDimitry Andric   if (Succ0->pred_size() != 1 || Succ0->succ_size() != 2)
4510b57cec5SDimitry Andric     return false;
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   CmpBB = Succ0;
4540b57cec5SDimitry Andric   Tail = Succ1;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   if (!CmpBB->isSuccessor(Tail))
4570b57cec5SDimitry Andric     return false;
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   // The CFG topology checks out.
4600b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nTriangle: " << printMBBReference(*Head) << " -> "
4610b57cec5SDimitry Andric                     << printMBBReference(*CmpBB) << " -> "
4620b57cec5SDimitry Andric                     << printMBBReference(*Tail) << '\n');
4630b57cec5SDimitry Andric   ++NumConsidered;
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   // Tail is allowed to have many predecessors, but we can't handle PHIs yet.
4660b57cec5SDimitry Andric   //
4670b57cec5SDimitry Andric   // FIXME: Real PHIs could be if-converted as long as the CmpBB values are
4680b57cec5SDimitry Andric   // defined before The CmpBB cmp clobbers the flags. Alternatively, it should
4690b57cec5SDimitry Andric   // always be safe to sink the ccmp down to immediately before the CmpBB
4700b57cec5SDimitry Andric   // terminators.
4710b57cec5SDimitry Andric   if (!trivialTailPHIs()) {
4720b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't handle phis in Tail.\n");
4730b57cec5SDimitry Andric     ++NumPhiRejs;
4740b57cec5SDimitry Andric     return false;
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   if (!Tail->livein_empty()) {
4780b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't handle live-in physregs in Tail.\n");
4790b57cec5SDimitry Andric     ++NumPhysRejs;
4800b57cec5SDimitry Andric     return false;
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   // CmpBB should never have PHIs since Head is its only predecessor.
4840b57cec5SDimitry Andric   // FIXME: Clean them up if it happens.
4850b57cec5SDimitry Andric   if (!CmpBB->empty() && CmpBB->front().isPHI()) {
4860b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't handle phis in CmpBB.\n");
4870b57cec5SDimitry Andric     ++NumPhi2Rejs;
4880b57cec5SDimitry Andric     return false;
4890b57cec5SDimitry Andric   }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   if (!CmpBB->livein_empty()) {
4920b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't handle live-in physregs in CmpBB.\n");
4930b57cec5SDimitry Andric     ++NumPhysRejs;
4940b57cec5SDimitry Andric     return false;
4950b57cec5SDimitry Andric   }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // The branch we're looking to eliminate must be analyzable.
4980b57cec5SDimitry Andric   HeadCond.clear();
4990b57cec5SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
5000b57cec5SDimitry Andric   if (TII->analyzeBranch(*Head, TBB, FBB, HeadCond)) {
5010b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Head branch not analyzable.\n");
5020b57cec5SDimitry Andric     ++NumHeadBranchRejs;
5030b57cec5SDimitry Andric     return false;
5040b57cec5SDimitry Andric   }
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   // This is weird, probably some sort of degenerate CFG, or an edge to a
5070b57cec5SDimitry Andric   // landing pad.
5080b57cec5SDimitry Andric   if (!TBB || HeadCond.empty()) {
5090b57cec5SDimitry Andric     LLVM_DEBUG(
5105ffd83dbSDimitry Andric         dbgs() << "analyzeBranch didn't find conditional branch in Head.\n");
5110b57cec5SDimitry Andric     ++NumHeadBranchRejs;
5120b57cec5SDimitry Andric     return false;
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   if (!parseCond(HeadCond, HeadCmpBBCC)) {
5160b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Unsupported branch type on Head\n");
5170b57cec5SDimitry Andric     ++NumHeadBranchRejs;
5180b57cec5SDimitry Andric     return false;
5190b57cec5SDimitry Andric   }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   // Make sure the branch direction is right.
5220b57cec5SDimitry Andric   if (TBB != CmpBB) {
5230b57cec5SDimitry Andric     assert(TBB == Tail && "Unexpected TBB");
5240b57cec5SDimitry Andric     HeadCmpBBCC = AArch64CC::getInvertedCondCode(HeadCmpBBCC);
5250b57cec5SDimitry Andric   }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   CmpBBCond.clear();
5280b57cec5SDimitry Andric   TBB = FBB = nullptr;
5290b57cec5SDimitry Andric   if (TII->analyzeBranch(*CmpBB, TBB, FBB, CmpBBCond)) {
5300b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "CmpBB branch not analyzable.\n");
5310b57cec5SDimitry Andric     ++NumCmpBranchRejs;
5320b57cec5SDimitry Andric     return false;
5330b57cec5SDimitry Andric   }
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric   if (!TBB || CmpBBCond.empty()) {
5360b57cec5SDimitry Andric     LLVM_DEBUG(
5375ffd83dbSDimitry Andric         dbgs() << "analyzeBranch didn't find conditional branch in CmpBB.\n");
5380b57cec5SDimitry Andric     ++NumCmpBranchRejs;
5390b57cec5SDimitry Andric     return false;
5400b57cec5SDimitry Andric   }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   if (!parseCond(CmpBBCond, CmpBBTailCC)) {
5430b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Unsupported branch type on CmpBB\n");
5440b57cec5SDimitry Andric     ++NumCmpBranchRejs;
5450b57cec5SDimitry Andric     return false;
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   if (TBB != Tail)
5490b57cec5SDimitry Andric     CmpBBTailCC = AArch64CC::getInvertedCondCode(CmpBBTailCC);
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Head->CmpBB on "
5520b57cec5SDimitry Andric                     << AArch64CC::getCondCodeName(HeadCmpBBCC)
5530b57cec5SDimitry Andric                     << ", CmpBB->Tail on "
5540b57cec5SDimitry Andric                     << AArch64CC::getCondCodeName(CmpBBTailCC) << '\n');
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric   CmpMI = findConvertibleCompare(CmpBB);
5570b57cec5SDimitry Andric   if (!CmpMI)
5580b57cec5SDimitry Andric     return false;
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   if (!canSpeculateInstrs(CmpBB, CmpMI)) {
5610b57cec5SDimitry Andric     ++NumSpeculateRejs;
5620b57cec5SDimitry Andric     return false;
5630b57cec5SDimitry Andric   }
5640b57cec5SDimitry Andric   return true;
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric 
convert(SmallVectorImpl<MachineBasicBlock * > & RemovedBlocks)5670b57cec5SDimitry Andric void SSACCmpConv::convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks) {
5680b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Merging " << printMBBReference(*CmpBB) << " into "
5690b57cec5SDimitry Andric                     << printMBBReference(*Head) << ":\n"
5700b57cec5SDimitry Andric                     << *CmpBB);
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   // All CmpBB instructions are moved into Head, and CmpBB is deleted.
5730b57cec5SDimitry Andric   // Update the CFG first.
5740b57cec5SDimitry Andric   updateTailPHIs();
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   // Save successor probabilties before removing CmpBB and Tail from their
5770b57cec5SDimitry Andric   // parents.
5780b57cec5SDimitry Andric   BranchProbability Head2CmpBB = MBPI->getEdgeProbability(Head, CmpBB);
5790b57cec5SDimitry Andric   BranchProbability CmpBB2Tail = MBPI->getEdgeProbability(CmpBB, Tail);
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   Head->removeSuccessor(CmpBB);
5820b57cec5SDimitry Andric   CmpBB->removeSuccessor(Tail);
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   // If Head and CmpBB had successor probabilties, udpate the probabilities to
5850b57cec5SDimitry Andric   // reflect the ccmp-conversion.
5860b57cec5SDimitry Andric   if (Head->hasSuccessorProbabilities() && CmpBB->hasSuccessorProbabilities()) {
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric     // Head is allowed two successors. We've removed CmpBB, so the remaining
5890b57cec5SDimitry Andric     // successor is Tail. We need to increase the successor probability for
5900b57cec5SDimitry Andric     // Tail to account for the CmpBB path we removed.
5910b57cec5SDimitry Andric     //
5920b57cec5SDimitry Andric     // Pr(Tail|Head) += Pr(CmpBB|Head) * Pr(Tail|CmpBB).
5930b57cec5SDimitry Andric     assert(*Head->succ_begin() == Tail && "Head successor is not Tail");
5940b57cec5SDimitry Andric     BranchProbability Head2Tail = MBPI->getEdgeProbability(Head, Tail);
5950b57cec5SDimitry Andric     Head->setSuccProbability(Head->succ_begin(),
5960b57cec5SDimitry Andric                              Head2Tail + Head2CmpBB * CmpBB2Tail);
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric     // We will transfer successors of CmpBB to Head in a moment without
5990b57cec5SDimitry Andric     // normalizing the successor probabilities. Set the successor probabilites
6000b57cec5SDimitry Andric     // before doing so.
6010b57cec5SDimitry Andric     //
6020b57cec5SDimitry Andric     // Pr(I|Head) = Pr(CmpBB|Head) * Pr(I|CmpBB).
6030b57cec5SDimitry Andric     for (auto I = CmpBB->succ_begin(), E = CmpBB->succ_end(); I != E; ++I) {
6040b57cec5SDimitry Andric       BranchProbability CmpBB2I = MBPI->getEdgeProbability(CmpBB, *I);
6050b57cec5SDimitry Andric       CmpBB->setSuccProbability(I, Head2CmpBB * CmpBB2I);
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   Head->transferSuccessorsAndUpdatePHIs(CmpBB);
6100b57cec5SDimitry Andric   DebugLoc TermDL = Head->getFirstTerminator()->getDebugLoc();
6110b57cec5SDimitry Andric   TII->removeBranch(*Head);
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   // If the Head terminator was one of the cbz / tbz branches with built-in
6140b57cec5SDimitry Andric   // compare, we need to insert an explicit compare instruction in its place.
6150b57cec5SDimitry Andric   if (HeadCond[0].getImm() == -1) {
6160b57cec5SDimitry Andric     ++NumCompBranches;
6170b57cec5SDimitry Andric     unsigned Opc = 0;
6180b57cec5SDimitry Andric     switch (HeadCond[1].getImm()) {
6190b57cec5SDimitry Andric     case AArch64::CBZW:
6200b57cec5SDimitry Andric     case AArch64::CBNZW:
6210b57cec5SDimitry Andric       Opc = AArch64::SUBSWri;
6220b57cec5SDimitry Andric       break;
6230b57cec5SDimitry Andric     case AArch64::CBZX:
6240b57cec5SDimitry Andric     case AArch64::CBNZX:
6250b57cec5SDimitry Andric       Opc = AArch64::SUBSXri;
6260b57cec5SDimitry Andric       break;
6270b57cec5SDimitry Andric     default:
6280b57cec5SDimitry Andric       llvm_unreachable("Cannot convert Head branch");
6290b57cec5SDimitry Andric     }
6300b57cec5SDimitry Andric     const MCInstrDesc &MCID = TII->get(Opc);
6310b57cec5SDimitry Andric     // Create a dummy virtual register for the SUBS def.
6328bcb0991SDimitry Andric     Register DestReg =
6330b57cec5SDimitry Andric         MRI->createVirtualRegister(TII->getRegClass(MCID, 0, TRI, *MF));
6340b57cec5SDimitry Andric     // Insert a SUBS Rn, #0 instruction instead of the cbz / cbnz.
6350b57cec5SDimitry Andric     BuildMI(*Head, Head->end(), TermDL, MCID)
6360b57cec5SDimitry Andric         .addReg(DestReg, RegState::Define | RegState::Dead)
6370b57cec5SDimitry Andric         .add(HeadCond[2])
6380b57cec5SDimitry Andric         .addImm(0)
6390b57cec5SDimitry Andric         .addImm(0);
6400b57cec5SDimitry Andric     // SUBS uses the GPR*sp register classes.
6410b57cec5SDimitry Andric     MRI->constrainRegClass(HeadCond[2].getReg(),
6420b57cec5SDimitry Andric                            TII->getRegClass(MCID, 1, TRI, *MF));
6430b57cec5SDimitry Andric   }
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   Head->splice(Head->end(), CmpBB, CmpBB->begin(), CmpBB->end());
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   // Now replace CmpMI with a ccmp instruction that also considers the incoming
6480b57cec5SDimitry Andric   // flags.
6490b57cec5SDimitry Andric   unsigned Opc = 0;
6500b57cec5SDimitry Andric   unsigned FirstOp = 1;   // First CmpMI operand to copy.
6510b57cec5SDimitry Andric   bool isZBranch = false; // CmpMI is a cbz/cbnz instruction.
6520b57cec5SDimitry Andric   switch (CmpMI->getOpcode()) {
6530b57cec5SDimitry Andric   default:
6540b57cec5SDimitry Andric     llvm_unreachable("Unknown compare opcode");
6550b57cec5SDimitry Andric   case AArch64::SUBSWri:    Opc = AArch64::CCMPWi; break;
6560b57cec5SDimitry Andric   case AArch64::SUBSWrr:    Opc = AArch64::CCMPWr; break;
6570b57cec5SDimitry Andric   case AArch64::SUBSXri:    Opc = AArch64::CCMPXi; break;
6580b57cec5SDimitry Andric   case AArch64::SUBSXrr:    Opc = AArch64::CCMPXr; break;
6590b57cec5SDimitry Andric   case AArch64::ADDSWri:    Opc = AArch64::CCMNWi; break;
6600b57cec5SDimitry Andric   case AArch64::ADDSWrr:    Opc = AArch64::CCMNWr; break;
6610b57cec5SDimitry Andric   case AArch64::ADDSXri:    Opc = AArch64::CCMNXi; break;
6620b57cec5SDimitry Andric   case AArch64::ADDSXrr:    Opc = AArch64::CCMNXr; break;
6630b57cec5SDimitry Andric   case AArch64::FCMPSrr:    Opc = AArch64::FCCMPSrr; FirstOp = 0; break;
6640b57cec5SDimitry Andric   case AArch64::FCMPDrr:    Opc = AArch64::FCCMPDrr; FirstOp = 0; break;
6650b57cec5SDimitry Andric   case AArch64::FCMPESrr:   Opc = AArch64::FCCMPESrr; FirstOp = 0; break;
6660b57cec5SDimitry Andric   case AArch64::FCMPEDrr:   Opc = AArch64::FCCMPEDrr; FirstOp = 0; break;
6670b57cec5SDimitry Andric   case AArch64::CBZW:
6680b57cec5SDimitry Andric   case AArch64::CBNZW:
6690b57cec5SDimitry Andric     Opc = AArch64::CCMPWi;
6700b57cec5SDimitry Andric     FirstOp = 0;
6710b57cec5SDimitry Andric     isZBranch = true;
6720b57cec5SDimitry Andric     break;
6730b57cec5SDimitry Andric   case AArch64::CBZX:
6740b57cec5SDimitry Andric   case AArch64::CBNZX:
6750b57cec5SDimitry Andric     Opc = AArch64::CCMPXi;
6760b57cec5SDimitry Andric     FirstOp = 0;
6770b57cec5SDimitry Andric     isZBranch = true;
6780b57cec5SDimitry Andric     break;
6790b57cec5SDimitry Andric   }
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric   // The ccmp instruction should set the flags according to the comparison when
6820b57cec5SDimitry Andric   // Head would have branched to CmpBB.
6830b57cec5SDimitry Andric   // The NZCV immediate operand should provide flags for the case where Head
6840b57cec5SDimitry Andric   // would have branched to Tail. These flags should cause the new Head
6850b57cec5SDimitry Andric   // terminator to branch to tail.
6860b57cec5SDimitry Andric   unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(CmpBBTailCC);
6870b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII->get(Opc);
6880b57cec5SDimitry Andric   MRI->constrainRegClass(CmpMI->getOperand(FirstOp).getReg(),
6890b57cec5SDimitry Andric                          TII->getRegClass(MCID, 0, TRI, *MF));
6900b57cec5SDimitry Andric   if (CmpMI->getOperand(FirstOp + 1).isReg())
6910b57cec5SDimitry Andric     MRI->constrainRegClass(CmpMI->getOperand(FirstOp + 1).getReg(),
6920b57cec5SDimitry Andric                            TII->getRegClass(MCID, 1, TRI, *MF));
6930b57cec5SDimitry Andric   MachineInstrBuilder MIB = BuildMI(*Head, CmpMI, CmpMI->getDebugLoc(), MCID)
6940b57cec5SDimitry Andric                                 .add(CmpMI->getOperand(FirstOp)); // Register Rn
6950b57cec5SDimitry Andric   if (isZBranch)
6960b57cec5SDimitry Andric     MIB.addImm(0); // cbz/cbnz Rn -> ccmp Rn, #0
6970b57cec5SDimitry Andric   else
6980b57cec5SDimitry Andric     MIB.add(CmpMI->getOperand(FirstOp + 1)); // Register Rm / Immediate
6990b57cec5SDimitry Andric   MIB.addImm(NZCV).addImm(HeadCmpBBCC);
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   // If CmpMI was a terminator, we need a new conditional branch to replace it.
7020b57cec5SDimitry Andric   // This now becomes a Head terminator.
7030b57cec5SDimitry Andric   if (isZBranch) {
7040b57cec5SDimitry Andric     bool isNZ = CmpMI->getOpcode() == AArch64::CBNZW ||
7050b57cec5SDimitry Andric                 CmpMI->getOpcode() == AArch64::CBNZX;
7060b57cec5SDimitry Andric     BuildMI(*Head, CmpMI, CmpMI->getDebugLoc(), TII->get(AArch64::Bcc))
7070b57cec5SDimitry Andric         .addImm(isNZ ? AArch64CC::NE : AArch64CC::EQ)
7080b57cec5SDimitry Andric         .add(CmpMI->getOperand(1)); // Branch target.
7090b57cec5SDimitry Andric   }
7100b57cec5SDimitry Andric   CmpMI->eraseFromParent();
7115ffd83dbSDimitry Andric   Head->updateTerminator(CmpBB->getNextNode());
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric   RemovedBlocks.push_back(CmpBB);
7140b57cec5SDimitry Andric   CmpBB->eraseFromParent();
7150b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Result:\n" << *Head);
7160b57cec5SDimitry Andric   ++NumConverted;
7170b57cec5SDimitry Andric }
7180b57cec5SDimitry Andric 
expectedCodeSizeDelta() const7190b57cec5SDimitry Andric int SSACCmpConv::expectedCodeSizeDelta() const {
7200b57cec5SDimitry Andric   int delta = 0;
7210b57cec5SDimitry Andric   // If the Head terminator was one of the cbz / tbz branches with built-in
7220b57cec5SDimitry Andric   // compare, we need to insert an explicit compare instruction in its place
7230b57cec5SDimitry Andric   // plus a branch instruction.
7240b57cec5SDimitry Andric   if (HeadCond[0].getImm() == -1) {
7250b57cec5SDimitry Andric     switch (HeadCond[1].getImm()) {
7260b57cec5SDimitry Andric     case AArch64::CBZW:
7270b57cec5SDimitry Andric     case AArch64::CBNZW:
7280b57cec5SDimitry Andric     case AArch64::CBZX:
7290b57cec5SDimitry Andric     case AArch64::CBNZX:
7300b57cec5SDimitry Andric       // Therefore delta += 1
7310b57cec5SDimitry Andric       delta = 1;
7320b57cec5SDimitry Andric       break;
7330b57cec5SDimitry Andric     default:
7340b57cec5SDimitry Andric       llvm_unreachable("Cannot convert Head branch");
7350b57cec5SDimitry Andric     }
7360b57cec5SDimitry Andric   }
7370b57cec5SDimitry Andric   // If the Cmp terminator was one of the cbz / tbz branches with
7380b57cec5SDimitry Andric   // built-in compare, it will be turned into a compare instruction
7390b57cec5SDimitry Andric   // into Head, but we do not save any instruction.
7400b57cec5SDimitry Andric   // Otherwise, we save the branch instruction.
7410b57cec5SDimitry Andric   switch (CmpMI->getOpcode()) {
7420b57cec5SDimitry Andric   default:
7430b57cec5SDimitry Andric     --delta;
7440b57cec5SDimitry Andric     break;
7450b57cec5SDimitry Andric   case AArch64::CBZW:
7460b57cec5SDimitry Andric   case AArch64::CBNZW:
7470b57cec5SDimitry Andric   case AArch64::CBZX:
7480b57cec5SDimitry Andric   case AArch64::CBNZX:
7490b57cec5SDimitry Andric     break;
7500b57cec5SDimitry Andric   }
7510b57cec5SDimitry Andric   return delta;
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7550b57cec5SDimitry Andric //                       AArch64ConditionalCompares Pass
7560b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric namespace {
7590b57cec5SDimitry Andric class AArch64ConditionalCompares : public MachineFunctionPass {
7600b57cec5SDimitry Andric   const MachineBranchProbabilityInfo *MBPI;
7610b57cec5SDimitry Andric   const TargetInstrInfo *TII;
7620b57cec5SDimitry Andric   const TargetRegisterInfo *TRI;
7630b57cec5SDimitry Andric   MCSchedModel SchedModel;
7640b57cec5SDimitry Andric   // Does the proceeded function has Oz attribute.
7650b57cec5SDimitry Andric   bool MinSize;
7660b57cec5SDimitry Andric   MachineRegisterInfo *MRI;
7670b57cec5SDimitry Andric   MachineDominatorTree *DomTree;
7680b57cec5SDimitry Andric   MachineLoopInfo *Loops;
7690b57cec5SDimitry Andric   MachineTraceMetrics *Traces;
7700b57cec5SDimitry Andric   MachineTraceMetrics::Ensemble *MinInstr;
7710b57cec5SDimitry Andric   SSACCmpConv CmpConv;
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric public:
7740b57cec5SDimitry Andric   static char ID;
AArch64ConditionalCompares()7750b57cec5SDimitry Andric   AArch64ConditionalCompares() : MachineFunctionPass(ID) {
7760b57cec5SDimitry Andric     initializeAArch64ConditionalComparesPass(*PassRegistry::getPassRegistry());
7770b57cec5SDimitry Andric   }
7780b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
7790b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
getPassName() const7800b57cec5SDimitry Andric   StringRef getPassName() const override {
7810b57cec5SDimitry Andric     return "AArch64 Conditional Compares";
7820b57cec5SDimitry Andric   }
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric private:
7850b57cec5SDimitry Andric   bool tryConvert(MachineBasicBlock *);
7860b57cec5SDimitry Andric   void updateDomTree(ArrayRef<MachineBasicBlock *> Removed);
7870b57cec5SDimitry Andric   void updateLoops(ArrayRef<MachineBasicBlock *> Removed);
7880b57cec5SDimitry Andric   void invalidateTraces();
7890b57cec5SDimitry Andric   bool shouldConvert();
7900b57cec5SDimitry Andric };
7910b57cec5SDimitry Andric } // end anonymous namespace
7920b57cec5SDimitry Andric 
7930b57cec5SDimitry Andric char AArch64ConditionalCompares::ID = 0;
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(AArch64ConditionalCompares, "aarch64-ccmp",
7960b57cec5SDimitry Andric                       "AArch64 CCMP Pass", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)7970b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
7980b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
7990b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
8000b57cec5SDimitry Andric INITIALIZE_PASS_END(AArch64ConditionalCompares, "aarch64-ccmp",
8010b57cec5SDimitry Andric                     "AArch64 CCMP Pass", false, false)
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric FunctionPass *llvm::createAArch64ConditionalCompares() {
8040b57cec5SDimitry Andric   return new AArch64ConditionalCompares();
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const8070b57cec5SDimitry Andric void AArch64ConditionalCompares::getAnalysisUsage(AnalysisUsage &AU) const {
8080b57cec5SDimitry Andric   AU.addRequired<MachineBranchProbabilityInfo>();
8090b57cec5SDimitry Andric   AU.addRequired<MachineDominatorTree>();
8100b57cec5SDimitry Andric   AU.addPreserved<MachineDominatorTree>();
8110b57cec5SDimitry Andric   AU.addRequired<MachineLoopInfo>();
8120b57cec5SDimitry Andric   AU.addPreserved<MachineLoopInfo>();
8130b57cec5SDimitry Andric   AU.addRequired<MachineTraceMetrics>();
8140b57cec5SDimitry Andric   AU.addPreserved<MachineTraceMetrics>();
8150b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
8160b57cec5SDimitry Andric }
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric /// Update the dominator tree after if-conversion erased some blocks.
updateDomTree(ArrayRef<MachineBasicBlock * > Removed)8190b57cec5SDimitry Andric void AArch64ConditionalCompares::updateDomTree(
8200b57cec5SDimitry Andric     ArrayRef<MachineBasicBlock *> Removed) {
8210b57cec5SDimitry Andric   // convert() removes CmpBB which was previously dominated by Head.
8220b57cec5SDimitry Andric   // CmpBB children should be transferred to Head.
8230b57cec5SDimitry Andric   MachineDomTreeNode *HeadNode = DomTree->getNode(CmpConv.Head);
8240b57cec5SDimitry Andric   for (MachineBasicBlock *RemovedMBB : Removed) {
8250b57cec5SDimitry Andric     MachineDomTreeNode *Node = DomTree->getNode(RemovedMBB);
8260b57cec5SDimitry Andric     assert(Node != HeadNode && "Cannot erase the head node");
8270b57cec5SDimitry Andric     assert(Node->getIDom() == HeadNode && "CmpBB should be dominated by Head");
8280b57cec5SDimitry Andric     while (Node->getNumChildren())
8295ffd83dbSDimitry Andric       DomTree->changeImmediateDominator(Node->back(), HeadNode);
8300b57cec5SDimitry Andric     DomTree->eraseNode(RemovedMBB);
8310b57cec5SDimitry Andric   }
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric /// Update LoopInfo after if-conversion.
8350b57cec5SDimitry Andric void
updateLoops(ArrayRef<MachineBasicBlock * > Removed)8360b57cec5SDimitry Andric AArch64ConditionalCompares::updateLoops(ArrayRef<MachineBasicBlock *> Removed) {
8370b57cec5SDimitry Andric   if (!Loops)
8380b57cec5SDimitry Andric     return;
8390b57cec5SDimitry Andric   for (MachineBasicBlock *RemovedMBB : Removed)
8400b57cec5SDimitry Andric     Loops->removeBlock(RemovedMBB);
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric /// Invalidate MachineTraceMetrics before if-conversion.
invalidateTraces()8440b57cec5SDimitry Andric void AArch64ConditionalCompares::invalidateTraces() {
8450b57cec5SDimitry Andric   Traces->invalidate(CmpConv.Head);
8460b57cec5SDimitry Andric   Traces->invalidate(CmpConv.CmpBB);
8470b57cec5SDimitry Andric }
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric /// Apply cost model and heuristics to the if-conversion in IfConv.
8500b57cec5SDimitry Andric /// Return true if the conversion is a good idea.
8510b57cec5SDimitry Andric ///
shouldConvert()8520b57cec5SDimitry Andric bool AArch64ConditionalCompares::shouldConvert() {
8530b57cec5SDimitry Andric   // Stress testing mode disables all cost considerations.
8540b57cec5SDimitry Andric   if (Stress)
8550b57cec5SDimitry Andric     return true;
8560b57cec5SDimitry Andric   if (!MinInstr)
85706c3fb27SDimitry Andric     MinInstr = Traces->getEnsemble(MachineTraceStrategy::TS_MinInstrCount);
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric   // Head dominates CmpBB, so it is always included in its trace.
8600b57cec5SDimitry Andric   MachineTraceMetrics::Trace Trace = MinInstr->getTrace(CmpConv.CmpBB);
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   // If code size is the main concern
8630b57cec5SDimitry Andric   if (MinSize) {
8640b57cec5SDimitry Andric     int CodeSizeDelta = CmpConv.expectedCodeSizeDelta();
8650b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Code size delta:  " << CodeSizeDelta << '\n');
8660b57cec5SDimitry Andric     // If we are minimizing the code size, do the conversion whatever
8670b57cec5SDimitry Andric     // the cost is.
8680b57cec5SDimitry Andric     if (CodeSizeDelta < 0)
8690b57cec5SDimitry Andric       return true;
8700b57cec5SDimitry Andric     if (CodeSizeDelta > 0) {
8710b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Code size is increasing, give up on this one.\n");
8720b57cec5SDimitry Andric       return false;
8730b57cec5SDimitry Andric     }
8740b57cec5SDimitry Andric     // CodeSizeDelta == 0, continue with the regular heuristics
8750b57cec5SDimitry Andric   }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   // Heuristic: The compare conversion delays the execution of the branch
8780b57cec5SDimitry Andric   // instruction because we must wait for the inputs to the second compare as
8790b57cec5SDimitry Andric   // well. The branch has no dependent instructions, but delaying it increases
8800b57cec5SDimitry Andric   // the cost of a misprediction.
8810b57cec5SDimitry Andric   //
8820b57cec5SDimitry Andric   // Set a limit on the delay we will accept.
8830b57cec5SDimitry Andric   unsigned DelayLimit = SchedModel.MispredictPenalty * 3 / 4;
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric   // Instruction depths can be computed for all trace instructions above CmpBB.
8860b57cec5SDimitry Andric   unsigned HeadDepth =
8870b57cec5SDimitry Andric       Trace.getInstrCycles(*CmpConv.Head->getFirstTerminator()).Depth;
8880b57cec5SDimitry Andric   unsigned CmpBBDepth =
8890b57cec5SDimitry Andric       Trace.getInstrCycles(*CmpConv.CmpBB->getFirstTerminator()).Depth;
8900b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Head depth:  " << HeadDepth
8910b57cec5SDimitry Andric                     << "\nCmpBB depth: " << CmpBBDepth << '\n');
8920b57cec5SDimitry Andric   if (CmpBBDepth > HeadDepth + DelayLimit) {
8930b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Branch delay would be larger than " << DelayLimit
8940b57cec5SDimitry Andric                       << " cycles.\n");
8950b57cec5SDimitry Andric     return false;
8960b57cec5SDimitry Andric   }
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric   // Check the resource depth at the bottom of CmpBB - these instructions will
8990b57cec5SDimitry Andric   // be speculated.
9000b57cec5SDimitry Andric   unsigned ResDepth = Trace.getResourceDepth(true);
9010b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Resources:   " << ResDepth << '\n');
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric   // Heuristic: The speculatively executed instructions must all be able to
9040b57cec5SDimitry Andric   // merge into the Head block. The Head critical path should dominate the
9050b57cec5SDimitry Andric   // resource cost of the speculated instructions.
9060b57cec5SDimitry Andric   if (ResDepth > HeadDepth) {
9070b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Too many instructions to speculate.\n");
9080b57cec5SDimitry Andric     return false;
9090b57cec5SDimitry Andric   }
9100b57cec5SDimitry Andric   return true;
9110b57cec5SDimitry Andric }
9120b57cec5SDimitry Andric 
tryConvert(MachineBasicBlock * MBB)9130b57cec5SDimitry Andric bool AArch64ConditionalCompares::tryConvert(MachineBasicBlock *MBB) {
9140b57cec5SDimitry Andric   bool Changed = false;
9150b57cec5SDimitry Andric   while (CmpConv.canConvert(MBB) && shouldConvert()) {
9160b57cec5SDimitry Andric     invalidateTraces();
9170b57cec5SDimitry Andric     SmallVector<MachineBasicBlock *, 4> RemovedBlocks;
9180b57cec5SDimitry Andric     CmpConv.convert(RemovedBlocks);
9190b57cec5SDimitry Andric     Changed = true;
9200b57cec5SDimitry Andric     updateDomTree(RemovedBlocks);
9210b57cec5SDimitry Andric     updateLoops(RemovedBlocks);
9220b57cec5SDimitry Andric   }
9230b57cec5SDimitry Andric   return Changed;
9240b57cec5SDimitry Andric }
9250b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)9260b57cec5SDimitry Andric bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) {
9270b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** AArch64 Conditional Compares **********\n"
9280b57cec5SDimitry Andric                     << "********** Function: " << MF.getName() << '\n');
9290b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
9300b57cec5SDimitry Andric     return false;
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
9330b57cec5SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
9340b57cec5SDimitry Andric   SchedModel = MF.getSubtarget().getSchedModel();
9350b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
9360b57cec5SDimitry Andric   DomTree = &getAnalysis<MachineDominatorTree>();
9375f757f3fSDimitry Andric   Loops = &getAnalysis<MachineLoopInfo>();
9380b57cec5SDimitry Andric   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
9390b57cec5SDimitry Andric   Traces = &getAnalysis<MachineTraceMetrics>();
9400b57cec5SDimitry Andric   MinInstr = nullptr;
9410b57cec5SDimitry Andric   MinSize = MF.getFunction().hasMinSize();
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   bool Changed = false;
9440b57cec5SDimitry Andric   CmpConv.runOnMachineFunction(MF, MBPI);
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric   // Visit blocks in dominator tree pre-order. The pre-order enables multiple
9470b57cec5SDimitry Andric   // cmp-conversions from the same head block.
9480b57cec5SDimitry Andric   // Note that updateDomTree() modifies the children of the DomTree node
9490b57cec5SDimitry Andric   // currently being visited. The df_iterator supports that; it doesn't look at
9500b57cec5SDimitry Andric   // child_begin() / child_end() until after a node has been visited.
9510b57cec5SDimitry Andric   for (auto *I : depth_first(DomTree))
9520b57cec5SDimitry Andric     if (tryConvert(I->getBlock()))
9530b57cec5SDimitry Andric       Changed = true;
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric   return Changed;
9560b57cec5SDimitry Andric }
957