1*9880d681SAndroid Build Coastguard Worker //===-- AArch64ConditionalCompares.cpp --- CCMP formation for AArch64 -----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the AArch64ConditionalCompares pass which reduces
11*9880d681SAndroid Build Coastguard Worker // branching and code size by using the conditional compare instructions CCMP,
12*9880d681SAndroid Build Coastguard Worker // CCMN, and FCMP.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker // The CFG transformations for forming conditional compares are very similar to
15*9880d681SAndroid Build Coastguard Worker // if-conversion, and this pass should run immediately before the early
16*9880d681SAndroid Build Coastguard Worker // if-conversion pass.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker #include "AArch64.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineTraceMetrics.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker using namespace llvm;
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "aarch64-ccmp"
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker // Absolute maximum number of instructions allowed per speculated block.
45*9880d681SAndroid Build Coastguard Worker // This bypasses all other heuristics, so it should be set fairly high.
46*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> BlockInstrLimit(
47*9880d681SAndroid Build Coastguard Worker "aarch64-ccmp-limit", cl::init(30), cl::Hidden,
48*9880d681SAndroid Build Coastguard Worker cl::desc("Maximum number of instructions per speculated block."));
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker // Stress testing mode - disable heuristics.
51*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> Stress("aarch64-stress-ccmp", cl::Hidden,
52*9880d681SAndroid Build Coastguard Worker cl::desc("Turn all knobs to 11"));
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker STATISTIC(NumConsidered, "Number of ccmps considered");
55*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPhiRejs, "Number of ccmps rejected (PHI)");
56*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPhysRejs, "Number of ccmps rejected (Physregs)");
57*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPhi2Rejs, "Number of ccmps rejected (PHI2)");
58*9880d681SAndroid Build Coastguard Worker STATISTIC(NumHeadBranchRejs, "Number of ccmps rejected (Head branch)");
59*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCmpBranchRejs, "Number of ccmps rejected (CmpBB branch)");
60*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCmpTermRejs, "Number of ccmps rejected (CmpBB is cbz...)");
61*9880d681SAndroid Build Coastguard Worker STATISTIC(NumImmRangeRejs, "Number of ccmps rejected (Imm out of range)");
62*9880d681SAndroid Build Coastguard Worker STATISTIC(NumLiveDstRejs, "Number of ccmps rejected (Cmp dest live)");
63*9880d681SAndroid Build Coastguard Worker STATISTIC(NumMultNZCVUses, "Number of ccmps rejected (NZCV used)");
64*9880d681SAndroid Build Coastguard Worker STATISTIC(NumUnknNZCVDefs, "Number of ccmps rejected (NZCV def unknown)");
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSpeculateRejs, "Number of ccmps rejected (Can't speculate)");
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker STATISTIC(NumConverted, "Number of ccmp instructions created");
69*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCompBranches, "Number of cbz/cbnz branches converted");
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
72*9880d681SAndroid Build Coastguard Worker // SSACCmpConv
73*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
74*9880d681SAndroid Build Coastguard Worker //
75*9880d681SAndroid Build Coastguard Worker // The SSACCmpConv class performs ccmp-conversion on SSA form machine code
76*9880d681SAndroid Build Coastguard Worker // after determining if it is possible. The class contains no heuristics;
77*9880d681SAndroid Build Coastguard Worker // external code should be used to determine when ccmp-conversion is a good
78*9880d681SAndroid Build Coastguard Worker // idea.
79*9880d681SAndroid Build Coastguard Worker //
80*9880d681SAndroid Build Coastguard Worker // CCmp-formation works on a CFG representing chained conditions, typically
81*9880d681SAndroid Build Coastguard Worker // from C's short-circuit || and && operators:
82*9880d681SAndroid Build Coastguard Worker //
83*9880d681SAndroid Build Coastguard Worker // From: Head To: Head
84*9880d681SAndroid Build Coastguard Worker // / | CmpBB
85*9880d681SAndroid Build Coastguard Worker // / | / |
86*9880d681SAndroid Build Coastguard Worker // | CmpBB / |
87*9880d681SAndroid Build Coastguard Worker // | / | Tail |
88*9880d681SAndroid Build Coastguard Worker // | / | | |
89*9880d681SAndroid Build Coastguard Worker // Tail | | |
90*9880d681SAndroid Build Coastguard Worker // | | | |
91*9880d681SAndroid Build Coastguard Worker // ... ... ... ...
92*9880d681SAndroid Build Coastguard Worker //
93*9880d681SAndroid Build Coastguard Worker // The Head block is terminated by a br.cond instruction, and the CmpBB block
94*9880d681SAndroid Build Coastguard Worker // contains compare + br.cond. Tail must be a successor of both.
95*9880d681SAndroid Build Coastguard Worker //
96*9880d681SAndroid Build Coastguard Worker // The cmp-conversion turns the compare instruction in CmpBB into a conditional
97*9880d681SAndroid Build Coastguard Worker // compare, and merges CmpBB into Head, speculatively executing its
98*9880d681SAndroid Build Coastguard Worker // instructions. The AArch64 conditional compare instructions have an immediate
99*9880d681SAndroid Build Coastguard Worker // operand that specifies the NZCV flag values when the condition is false and
100*9880d681SAndroid Build Coastguard Worker // the compare isn't executed. This makes it possible to chain compares with
101*9880d681SAndroid Build Coastguard Worker // different condition codes.
102*9880d681SAndroid Build Coastguard Worker //
103*9880d681SAndroid Build Coastguard Worker // Example:
104*9880d681SAndroid Build Coastguard Worker //
105*9880d681SAndroid Build Coastguard Worker // if (a == 5 || b == 17)
106*9880d681SAndroid Build Coastguard Worker // foo();
107*9880d681SAndroid Build Coastguard Worker //
108*9880d681SAndroid Build Coastguard Worker // Head:
109*9880d681SAndroid Build Coastguard Worker // cmp w0, #5
110*9880d681SAndroid Build Coastguard Worker // b.eq Tail
111*9880d681SAndroid Build Coastguard Worker // CmpBB:
112*9880d681SAndroid Build Coastguard Worker // cmp w1, #17
113*9880d681SAndroid Build Coastguard Worker // b.eq Tail
114*9880d681SAndroid Build Coastguard Worker // ...
115*9880d681SAndroid Build Coastguard Worker // Tail:
116*9880d681SAndroid Build Coastguard Worker // bl _foo
117*9880d681SAndroid Build Coastguard Worker //
118*9880d681SAndroid Build Coastguard Worker // Becomes:
119*9880d681SAndroid Build Coastguard Worker //
120*9880d681SAndroid Build Coastguard Worker // Head:
121*9880d681SAndroid Build Coastguard Worker // cmp w0, #5
122*9880d681SAndroid Build Coastguard Worker // ccmp w1, #17, 4, ne ; 4 = nZcv
123*9880d681SAndroid Build Coastguard Worker // b.eq Tail
124*9880d681SAndroid Build Coastguard Worker // ...
125*9880d681SAndroid Build Coastguard Worker // Tail:
126*9880d681SAndroid Build Coastguard Worker // bl _foo
127*9880d681SAndroid Build Coastguard Worker //
128*9880d681SAndroid Build Coastguard Worker // The ccmp condition code is the one that would cause the Head terminator to
129*9880d681SAndroid Build Coastguard Worker // branch to CmpBB.
130*9880d681SAndroid Build Coastguard Worker //
131*9880d681SAndroid Build Coastguard Worker // FIXME: It should also be possible to speculate a block on the critical edge
132*9880d681SAndroid Build Coastguard Worker // between Head and Tail, just like if-converting a diamond.
133*9880d681SAndroid Build Coastguard Worker //
134*9880d681SAndroid Build Coastguard Worker // FIXME: Handle PHIs in Tail by turning them into selects (if-conversion).
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker namespace {
137*9880d681SAndroid Build Coastguard Worker class SSACCmpConv {
138*9880d681SAndroid Build Coastguard Worker MachineFunction *MF;
139*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
140*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
141*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker public:
144*9880d681SAndroid Build Coastguard Worker /// The first block containing a conditional branch, dominating everything
145*9880d681SAndroid Build Coastguard Worker /// else.
146*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Head;
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker /// The block containing cmp+br.cond with a successor shared with Head.
149*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *CmpBB;
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker /// The common successor for Head and CmpBB.
152*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Tail;
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker /// The compare instruction in CmpBB that can be converted to a ccmp.
155*9880d681SAndroid Build Coastguard Worker MachineInstr *CmpMI;
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker private:
158*9880d681SAndroid Build Coastguard Worker /// The branch condition in Head as determined by AnalyzeBranch.
159*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 4> HeadCond;
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker /// The condition code that makes Head branch to CmpBB.
162*9880d681SAndroid Build Coastguard Worker AArch64CC::CondCode HeadCmpBBCC;
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker /// The branch condition in CmpBB.
165*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 4> CmpBBCond;
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker /// The condition code that makes CmpBB branch to Tail.
168*9880d681SAndroid Build Coastguard Worker AArch64CC::CondCode CmpBBTailCC;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker /// Check if the Tail PHIs are trivially convertible.
171*9880d681SAndroid Build Coastguard Worker bool trivialTailPHIs();
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker /// Remove CmpBB from the Tail PHIs.
174*9880d681SAndroid Build Coastguard Worker void updateTailPHIs();
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker /// Check if an operand defining DstReg is dead.
177*9880d681SAndroid Build Coastguard Worker bool isDeadDef(unsigned DstReg);
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker /// Find the compare instruction in MBB that controls the conditional branch.
180*9880d681SAndroid Build Coastguard Worker /// Return NULL if a convertible instruction can't be found.
181*9880d681SAndroid Build Coastguard Worker MachineInstr *findConvertibleCompare(MachineBasicBlock *MBB);
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker /// Return true if all non-terminator instructions in MBB can be safely
184*9880d681SAndroid Build Coastguard Worker /// speculated.
185*9880d681SAndroid Build Coastguard Worker bool canSpeculateInstrs(MachineBasicBlock *MBB, const MachineInstr *CmpMI);
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker public:
188*9880d681SAndroid Build Coastguard Worker /// runOnMachineFunction - Initialize per-function data structures.
runOnMachineFunction(MachineFunction & MF)189*9880d681SAndroid Build Coastguard Worker void runOnMachineFunction(MachineFunction &MF) {
190*9880d681SAndroid Build Coastguard Worker this->MF = &MF;
191*9880d681SAndroid Build Coastguard Worker TII = MF.getSubtarget().getInstrInfo();
192*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
193*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
194*9880d681SAndroid Build Coastguard Worker }
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker /// If the sub-CFG headed by MBB can be cmp-converted, initialize the
197*9880d681SAndroid Build Coastguard Worker /// internal state, and return true.
198*9880d681SAndroid Build Coastguard Worker bool canConvert(MachineBasicBlock *MBB);
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker /// Cmo-convert the last block passed to canConvertCmp(), assuming
201*9880d681SAndroid Build Coastguard Worker /// it is possible. Add any erased blocks to RemovedBlocks.
202*9880d681SAndroid Build Coastguard Worker void convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks);
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker /// Return the expected code size delta if the conversion into a
205*9880d681SAndroid Build Coastguard Worker /// conditional compare is performed.
206*9880d681SAndroid Build Coastguard Worker int expectedCodeSizeDelta() const;
207*9880d681SAndroid Build Coastguard Worker };
208*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker // Check that all PHIs in Tail are selecting the same value from Head and CmpBB.
211*9880d681SAndroid Build Coastguard Worker // This means that no if-conversion is required when merging CmpBB into Head.
trivialTailPHIs()212*9880d681SAndroid Build Coastguard Worker bool SSACCmpConv::trivialTailPHIs() {
213*9880d681SAndroid Build Coastguard Worker for (auto &I : *Tail) {
214*9880d681SAndroid Build Coastguard Worker if (!I.isPHI())
215*9880d681SAndroid Build Coastguard Worker break;
216*9880d681SAndroid Build Coastguard Worker unsigned HeadReg = 0, CmpBBReg = 0;
217*9880d681SAndroid Build Coastguard Worker // PHI operands come in (VReg, MBB) pairs.
218*9880d681SAndroid Build Coastguard Worker for (unsigned oi = 1, oe = I.getNumOperands(); oi != oe; oi += 2) {
219*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = I.getOperand(oi + 1).getMBB();
220*9880d681SAndroid Build Coastguard Worker unsigned Reg = I.getOperand(oi).getReg();
221*9880d681SAndroid Build Coastguard Worker if (MBB == Head) {
222*9880d681SAndroid Build Coastguard Worker assert((!HeadReg || HeadReg == Reg) && "Inconsistent PHI operands");
223*9880d681SAndroid Build Coastguard Worker HeadReg = Reg;
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker if (MBB == CmpBB) {
226*9880d681SAndroid Build Coastguard Worker assert((!CmpBBReg || CmpBBReg == Reg) && "Inconsistent PHI operands");
227*9880d681SAndroid Build Coastguard Worker CmpBBReg = Reg;
228*9880d681SAndroid Build Coastguard Worker }
229*9880d681SAndroid Build Coastguard Worker }
230*9880d681SAndroid Build Coastguard Worker if (HeadReg != CmpBBReg)
231*9880d681SAndroid Build Coastguard Worker return false;
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker return true;
234*9880d681SAndroid Build Coastguard Worker }
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker // Assuming that trivialTailPHIs() is true, update the Tail PHIs by simply
237*9880d681SAndroid Build Coastguard Worker // removing the CmpBB operands. The Head operands will be identical.
updateTailPHIs()238*9880d681SAndroid Build Coastguard Worker void SSACCmpConv::updateTailPHIs() {
239*9880d681SAndroid Build Coastguard Worker for (auto &I : *Tail) {
240*9880d681SAndroid Build Coastguard Worker if (!I.isPHI())
241*9880d681SAndroid Build Coastguard Worker break;
242*9880d681SAndroid Build Coastguard Worker // I is a PHI. It can have multiple entries for CmpBB.
243*9880d681SAndroid Build Coastguard Worker for (unsigned oi = I.getNumOperands(); oi > 2; oi -= 2) {
244*9880d681SAndroid Build Coastguard Worker // PHI operands are (Reg, MBB) at (oi-2, oi-1).
245*9880d681SAndroid Build Coastguard Worker if (I.getOperand(oi - 1).getMBB() == CmpBB) {
246*9880d681SAndroid Build Coastguard Worker I.RemoveOperand(oi - 1);
247*9880d681SAndroid Build Coastguard Worker I.RemoveOperand(oi - 2);
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker }
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker // This pass runs before the AArch64DeadRegisterDefinitions pass, so compares
254*9880d681SAndroid Build Coastguard Worker // are still writing virtual registers without any uses.
isDeadDef(unsigned DstReg)255*9880d681SAndroid Build Coastguard Worker bool SSACCmpConv::isDeadDef(unsigned DstReg) {
256*9880d681SAndroid Build Coastguard Worker // Writes to the zero register are dead.
257*9880d681SAndroid Build Coastguard Worker if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
258*9880d681SAndroid Build Coastguard Worker return true;
259*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(DstReg))
260*9880d681SAndroid Build Coastguard Worker return false;
261*9880d681SAndroid Build Coastguard Worker // A virtual register def without any uses will be marked dead later, and
262*9880d681SAndroid Build Coastguard Worker // eventually replaced by the zero register.
263*9880d681SAndroid Build Coastguard Worker return MRI->use_nodbg_empty(DstReg);
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker // Parse a condition code returned by AnalyzeBranch, and compute the CondCode
267*9880d681SAndroid Build Coastguard Worker // corresponding to TBB.
268*9880d681SAndroid Build Coastguard Worker // Return
parseCond(ArrayRef<MachineOperand> Cond,AArch64CC::CondCode & CC)269*9880d681SAndroid Build Coastguard Worker static bool parseCond(ArrayRef<MachineOperand> Cond, AArch64CC::CondCode &CC) {
270*9880d681SAndroid Build Coastguard Worker // A normal br.cond simply has the condition code.
271*9880d681SAndroid Build Coastguard Worker if (Cond[0].getImm() != -1) {
272*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 1 && "Unknown Cond array format");
273*9880d681SAndroid Build Coastguard Worker CC = (AArch64CC::CondCode)(int)Cond[0].getImm();
274*9880d681SAndroid Build Coastguard Worker return true;
275*9880d681SAndroid Build Coastguard Worker }
276*9880d681SAndroid Build Coastguard Worker // For tbz and cbz instruction, the opcode is next.
277*9880d681SAndroid Build Coastguard Worker switch (Cond[1].getImm()) {
278*9880d681SAndroid Build Coastguard Worker default:
279*9880d681SAndroid Build Coastguard Worker // This includes tbz / tbnz branches which can't be converted to
280*9880d681SAndroid Build Coastguard Worker // ccmp + br.cond.
281*9880d681SAndroid Build Coastguard Worker return false;
282*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
283*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
284*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 3 && "Unknown Cond array format");
285*9880d681SAndroid Build Coastguard Worker CC = AArch64CC::EQ;
286*9880d681SAndroid Build Coastguard Worker return true;
287*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
288*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
289*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 3 && "Unknown Cond array format");
290*9880d681SAndroid Build Coastguard Worker CC = AArch64CC::NE;
291*9880d681SAndroid Build Coastguard Worker return true;
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
findConvertibleCompare(MachineBasicBlock * MBB)295*9880d681SAndroid Build Coastguard Worker MachineInstr *SSACCmpConv::findConvertibleCompare(MachineBasicBlock *MBB) {
296*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB->getFirstTerminator();
297*9880d681SAndroid Build Coastguard Worker if (I == MBB->end())
298*9880d681SAndroid Build Coastguard Worker return nullptr;
299*9880d681SAndroid Build Coastguard Worker // The terminator must be controlled by the flags.
300*9880d681SAndroid Build Coastguard Worker if (!I->readsRegister(AArch64::NZCV)) {
301*9880d681SAndroid Build Coastguard Worker switch (I->getOpcode()) {
302*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
303*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
304*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
305*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
306*9880d681SAndroid Build Coastguard Worker // These can be converted into a ccmp against #0.
307*9880d681SAndroid Build Coastguard Worker return &*I;
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker ++NumCmpTermRejs;
310*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Flags not used by terminator: " << *I);
311*9880d681SAndroid Build Coastguard Worker return nullptr;
312*9880d681SAndroid Build Coastguard Worker }
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard Worker // Now find the instruction controlling the terminator.
315*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator B = MBB->begin(); I != B;) {
316*9880d681SAndroid Build Coastguard Worker --I;
317*9880d681SAndroid Build Coastguard Worker assert(!I->isTerminator() && "Spurious terminator");
318*9880d681SAndroid Build Coastguard Worker switch (I->getOpcode()) {
319*9880d681SAndroid Build Coastguard Worker // cmp is an alias for subs with a dead destination register.
320*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSWri:
321*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSXri:
322*9880d681SAndroid Build Coastguard Worker // cmn is an alias for adds with a dead destination register.
323*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSWri:
324*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSXri:
325*9880d681SAndroid Build Coastguard Worker // Check that the immediate operand is within range, ccmp wants a uimm5.
326*9880d681SAndroid Build Coastguard Worker // Rd = SUBSri Rn, imm, shift
327*9880d681SAndroid Build Coastguard Worker if (I->getOperand(3).getImm() || !isUInt<5>(I->getOperand(2).getImm())) {
328*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Immediate out of range for ccmp: " << *I);
329*9880d681SAndroid Build Coastguard Worker ++NumImmRangeRejs;
330*9880d681SAndroid Build Coastguard Worker return nullptr;
331*9880d681SAndroid Build Coastguard Worker }
332*9880d681SAndroid Build Coastguard Worker // Fall through.
333*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSWrr:
334*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSXrr:
335*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSWrr:
336*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSXrr:
337*9880d681SAndroid Build Coastguard Worker if (isDeadDef(I->getOperand(0).getReg()))
338*9880d681SAndroid Build Coastguard Worker return &*I;
339*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't convert compare with live destination: " << *I);
340*9880d681SAndroid Build Coastguard Worker ++NumLiveDstRejs;
341*9880d681SAndroid Build Coastguard Worker return nullptr;
342*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPSrr:
343*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPDrr:
344*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPESrr:
345*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPEDrr:
346*9880d681SAndroid Build Coastguard Worker return &*I;
347*9880d681SAndroid Build Coastguard Worker }
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker // Check for flag reads and clobbers.
350*9880d681SAndroid Build Coastguard Worker MIOperands::PhysRegInfo PRI =
351*9880d681SAndroid Build Coastguard Worker MIOperands(*I).analyzePhysReg(AArch64::NZCV, TRI);
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker if (PRI.Read) {
354*9880d681SAndroid Build Coastguard Worker // The ccmp doesn't produce exactly the same flags as the original
355*9880d681SAndroid Build Coastguard Worker // compare, so reject the transform if there are uses of the flags
356*9880d681SAndroid Build Coastguard Worker // besides the terminators.
357*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't create ccmp with multiple uses: " << *I);
358*9880d681SAndroid Build Coastguard Worker ++NumMultNZCVUses;
359*9880d681SAndroid Build Coastguard Worker return nullptr;
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker if (PRI.Defined || PRI.Clobbered) {
363*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Not convertible compare: " << *I);
364*9880d681SAndroid Build Coastguard Worker ++NumUnknNZCVDefs;
365*9880d681SAndroid Build Coastguard Worker return nullptr;
366*9880d681SAndroid Build Coastguard Worker }
367*9880d681SAndroid Build Coastguard Worker }
368*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Flags not defined in BB#" << MBB->getNumber() << '\n');
369*9880d681SAndroid Build Coastguard Worker return nullptr;
370*9880d681SAndroid Build Coastguard Worker }
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker /// Determine if all the instructions in MBB can safely
373*9880d681SAndroid Build Coastguard Worker /// be speculated. The terminators are not considered.
374*9880d681SAndroid Build Coastguard Worker ///
375*9880d681SAndroid Build Coastguard Worker /// Only CmpMI is allowed to clobber the flags.
376*9880d681SAndroid Build Coastguard Worker ///
canSpeculateInstrs(MachineBasicBlock * MBB,const MachineInstr * CmpMI)377*9880d681SAndroid Build Coastguard Worker bool SSACCmpConv::canSpeculateInstrs(MachineBasicBlock *MBB,
378*9880d681SAndroid Build Coastguard Worker const MachineInstr *CmpMI) {
379*9880d681SAndroid Build Coastguard Worker // Reject any live-in physregs. It's probably NZCV/EFLAGS, and very hard to
380*9880d681SAndroid Build Coastguard Worker // get right.
381*9880d681SAndroid Build Coastguard Worker if (!MBB->livein_empty()) {
382*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BB#" << MBB->getNumber() << " has live-ins.\n");
383*9880d681SAndroid Build Coastguard Worker return false;
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker unsigned InstrCount = 0;
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker // Check all instructions, except the terminators. It is assumed that
389*9880d681SAndroid Build Coastguard Worker // terminators never have side effects or define any used register values.
390*9880d681SAndroid Build Coastguard Worker for (auto &I : make_range(MBB->begin(), MBB->getFirstTerminator())) {
391*9880d681SAndroid Build Coastguard Worker if (I.isDebugValue())
392*9880d681SAndroid Build Coastguard Worker continue;
393*9880d681SAndroid Build Coastguard Worker
394*9880d681SAndroid Build Coastguard Worker if (++InstrCount > BlockInstrLimit && !Stress) {
395*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BB#" << MBB->getNumber() << " has more than "
396*9880d681SAndroid Build Coastguard Worker << BlockInstrLimit << " instructions.\n");
397*9880d681SAndroid Build Coastguard Worker return false;
398*9880d681SAndroid Build Coastguard Worker }
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker // There shouldn't normally be any phis in a single-predecessor block.
401*9880d681SAndroid Build Coastguard Worker if (I.isPHI()) {
402*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't hoist: " << I);
403*9880d681SAndroid Build Coastguard Worker return false;
404*9880d681SAndroid Build Coastguard Worker }
405*9880d681SAndroid Build Coastguard Worker
406*9880d681SAndroid Build Coastguard Worker // Don't speculate loads. Note that it may be possible and desirable to
407*9880d681SAndroid Build Coastguard Worker // speculate GOT or constant pool loads that are guaranteed not to trap,
408*9880d681SAndroid Build Coastguard Worker // but we don't support that for now.
409*9880d681SAndroid Build Coastguard Worker if (I.mayLoad()) {
410*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Won't speculate load: " << I);
411*9880d681SAndroid Build Coastguard Worker return false;
412*9880d681SAndroid Build Coastguard Worker }
413*9880d681SAndroid Build Coastguard Worker
414*9880d681SAndroid Build Coastguard Worker // We never speculate stores, so an AA pointer isn't necessary.
415*9880d681SAndroid Build Coastguard Worker bool DontMoveAcrossStore = true;
416*9880d681SAndroid Build Coastguard Worker if (!I.isSafeToMove(nullptr, DontMoveAcrossStore)) {
417*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't speculate: " << I);
418*9880d681SAndroid Build Coastguard Worker return false;
419*9880d681SAndroid Build Coastguard Worker }
420*9880d681SAndroid Build Coastguard Worker
421*9880d681SAndroid Build Coastguard Worker // Only CmpMI is allowed to clobber the flags.
422*9880d681SAndroid Build Coastguard Worker if (&I != CmpMI && I.modifiesRegister(AArch64::NZCV, TRI)) {
423*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Clobbers flags: " << I);
424*9880d681SAndroid Build Coastguard Worker return false;
425*9880d681SAndroid Build Coastguard Worker }
426*9880d681SAndroid Build Coastguard Worker }
427*9880d681SAndroid Build Coastguard Worker return true;
428*9880d681SAndroid Build Coastguard Worker }
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker /// Analyze the sub-cfg rooted in MBB, and return true if it is a potential
431*9880d681SAndroid Build Coastguard Worker /// candidate for cmp-conversion. Fill out the internal state.
432*9880d681SAndroid Build Coastguard Worker ///
canConvert(MachineBasicBlock * MBB)433*9880d681SAndroid Build Coastguard Worker bool SSACCmpConv::canConvert(MachineBasicBlock *MBB) {
434*9880d681SAndroid Build Coastguard Worker Head = MBB;
435*9880d681SAndroid Build Coastguard Worker Tail = CmpBB = nullptr;
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker if (Head->succ_size() != 2)
438*9880d681SAndroid Build Coastguard Worker return false;
439*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Succ0 = Head->succ_begin()[0];
440*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Succ1 = Head->succ_begin()[1];
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker // CmpBB can only have a single predecessor. Tail is allowed many.
443*9880d681SAndroid Build Coastguard Worker if (Succ0->pred_size() != 1)
444*9880d681SAndroid Build Coastguard Worker std::swap(Succ0, Succ1);
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard Worker // Succ0 is our candidate for CmpBB.
447*9880d681SAndroid Build Coastguard Worker if (Succ0->pred_size() != 1 || Succ0->succ_size() != 2)
448*9880d681SAndroid Build Coastguard Worker return false;
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker CmpBB = Succ0;
451*9880d681SAndroid Build Coastguard Worker Tail = Succ1;
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker if (!CmpBB->isSuccessor(Tail))
454*9880d681SAndroid Build Coastguard Worker return false;
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker // The CFG topology checks out.
457*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\nTriangle: BB#" << Head->getNumber() << " -> BB#"
458*9880d681SAndroid Build Coastguard Worker << CmpBB->getNumber() << " -> BB#" << Tail->getNumber() << '\n');
459*9880d681SAndroid Build Coastguard Worker ++NumConsidered;
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker // Tail is allowed to have many predecessors, but we can't handle PHIs yet.
462*9880d681SAndroid Build Coastguard Worker //
463*9880d681SAndroid Build Coastguard Worker // FIXME: Real PHIs could be if-converted as long as the CmpBB values are
464*9880d681SAndroid Build Coastguard Worker // defined before The CmpBB cmp clobbers the flags. Alternatively, it should
465*9880d681SAndroid Build Coastguard Worker // always be safe to sink the ccmp down to immediately before the CmpBB
466*9880d681SAndroid Build Coastguard Worker // terminators.
467*9880d681SAndroid Build Coastguard Worker if (!trivialTailPHIs()) {
468*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't handle phis in Tail.\n");
469*9880d681SAndroid Build Coastguard Worker ++NumPhiRejs;
470*9880d681SAndroid Build Coastguard Worker return false;
471*9880d681SAndroid Build Coastguard Worker }
472*9880d681SAndroid Build Coastguard Worker
473*9880d681SAndroid Build Coastguard Worker if (!Tail->livein_empty()) {
474*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't handle live-in physregs in Tail.\n");
475*9880d681SAndroid Build Coastguard Worker ++NumPhysRejs;
476*9880d681SAndroid Build Coastguard Worker return false;
477*9880d681SAndroid Build Coastguard Worker }
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard Worker // CmpBB should never have PHIs since Head is its only predecessor.
480*9880d681SAndroid Build Coastguard Worker // FIXME: Clean them up if it happens.
481*9880d681SAndroid Build Coastguard Worker if (!CmpBB->empty() && CmpBB->front().isPHI()) {
482*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't handle phis in CmpBB.\n");
483*9880d681SAndroid Build Coastguard Worker ++NumPhi2Rejs;
484*9880d681SAndroid Build Coastguard Worker return false;
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker if (!CmpBB->livein_empty()) {
488*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Can't handle live-in physregs in CmpBB.\n");
489*9880d681SAndroid Build Coastguard Worker ++NumPhysRejs;
490*9880d681SAndroid Build Coastguard Worker return false;
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker
493*9880d681SAndroid Build Coastguard Worker // The branch we're looking to eliminate must be analyzable.
494*9880d681SAndroid Build Coastguard Worker HeadCond.clear();
495*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
496*9880d681SAndroid Build Coastguard Worker if (TII->analyzeBranch(*Head, TBB, FBB, HeadCond)) {
497*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Head branch not analyzable.\n");
498*9880d681SAndroid Build Coastguard Worker ++NumHeadBranchRejs;
499*9880d681SAndroid Build Coastguard Worker return false;
500*9880d681SAndroid Build Coastguard Worker }
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker // This is weird, probably some sort of degenerate CFG, or an edge to a
503*9880d681SAndroid Build Coastguard Worker // landing pad.
504*9880d681SAndroid Build Coastguard Worker if (!TBB || HeadCond.empty()) {
505*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "AnalyzeBranch didn't find conditional branch in Head.\n");
506*9880d681SAndroid Build Coastguard Worker ++NumHeadBranchRejs;
507*9880d681SAndroid Build Coastguard Worker return false;
508*9880d681SAndroid Build Coastguard Worker }
509*9880d681SAndroid Build Coastguard Worker
510*9880d681SAndroid Build Coastguard Worker if (!parseCond(HeadCond, HeadCmpBBCC)) {
511*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Unsupported branch type on Head\n");
512*9880d681SAndroid Build Coastguard Worker ++NumHeadBranchRejs;
513*9880d681SAndroid Build Coastguard Worker return false;
514*9880d681SAndroid Build Coastguard Worker }
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard Worker // Make sure the branch direction is right.
517*9880d681SAndroid Build Coastguard Worker if (TBB != CmpBB) {
518*9880d681SAndroid Build Coastguard Worker assert(TBB == Tail && "Unexpected TBB");
519*9880d681SAndroid Build Coastguard Worker HeadCmpBBCC = AArch64CC::getInvertedCondCode(HeadCmpBBCC);
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker
522*9880d681SAndroid Build Coastguard Worker CmpBBCond.clear();
523*9880d681SAndroid Build Coastguard Worker TBB = FBB = nullptr;
524*9880d681SAndroid Build Coastguard Worker if (TII->analyzeBranch(*CmpBB, TBB, FBB, CmpBBCond)) {
525*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "CmpBB branch not analyzable.\n");
526*9880d681SAndroid Build Coastguard Worker ++NumCmpBranchRejs;
527*9880d681SAndroid Build Coastguard Worker return false;
528*9880d681SAndroid Build Coastguard Worker }
529*9880d681SAndroid Build Coastguard Worker
530*9880d681SAndroid Build Coastguard Worker if (!TBB || CmpBBCond.empty()) {
531*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "AnalyzeBranch didn't find conditional branch in CmpBB.\n");
532*9880d681SAndroid Build Coastguard Worker ++NumCmpBranchRejs;
533*9880d681SAndroid Build Coastguard Worker return false;
534*9880d681SAndroid Build Coastguard Worker }
535*9880d681SAndroid Build Coastguard Worker
536*9880d681SAndroid Build Coastguard Worker if (!parseCond(CmpBBCond, CmpBBTailCC)) {
537*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Unsupported branch type on CmpBB\n");
538*9880d681SAndroid Build Coastguard Worker ++NumCmpBranchRejs;
539*9880d681SAndroid Build Coastguard Worker return false;
540*9880d681SAndroid Build Coastguard Worker }
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker if (TBB != Tail)
543*9880d681SAndroid Build Coastguard Worker CmpBBTailCC = AArch64CC::getInvertedCondCode(CmpBBTailCC);
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Head->CmpBB on " << AArch64CC::getCondCodeName(HeadCmpBBCC)
546*9880d681SAndroid Build Coastguard Worker << ", CmpBB->Tail on " << AArch64CC::getCondCodeName(CmpBBTailCC)
547*9880d681SAndroid Build Coastguard Worker << '\n');
548*9880d681SAndroid Build Coastguard Worker
549*9880d681SAndroid Build Coastguard Worker CmpMI = findConvertibleCompare(CmpBB);
550*9880d681SAndroid Build Coastguard Worker if (!CmpMI)
551*9880d681SAndroid Build Coastguard Worker return false;
552*9880d681SAndroid Build Coastguard Worker
553*9880d681SAndroid Build Coastguard Worker if (!canSpeculateInstrs(CmpBB, CmpMI)) {
554*9880d681SAndroid Build Coastguard Worker ++NumSpeculateRejs;
555*9880d681SAndroid Build Coastguard Worker return false;
556*9880d681SAndroid Build Coastguard Worker }
557*9880d681SAndroid Build Coastguard Worker return true;
558*9880d681SAndroid Build Coastguard Worker }
559*9880d681SAndroid Build Coastguard Worker
convert(SmallVectorImpl<MachineBasicBlock * > & RemovedBlocks)560*9880d681SAndroid Build Coastguard Worker void SSACCmpConv::convert(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks) {
561*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Merging BB#" << CmpBB->getNumber() << " into BB#"
562*9880d681SAndroid Build Coastguard Worker << Head->getNumber() << ":\n" << *CmpBB);
563*9880d681SAndroid Build Coastguard Worker
564*9880d681SAndroid Build Coastguard Worker // All CmpBB instructions are moved into Head, and CmpBB is deleted.
565*9880d681SAndroid Build Coastguard Worker // Update the CFG first.
566*9880d681SAndroid Build Coastguard Worker updateTailPHIs();
567*9880d681SAndroid Build Coastguard Worker Head->removeSuccessor(CmpBB, true);
568*9880d681SAndroid Build Coastguard Worker CmpBB->removeSuccessor(Tail, true);
569*9880d681SAndroid Build Coastguard Worker Head->transferSuccessorsAndUpdatePHIs(CmpBB);
570*9880d681SAndroid Build Coastguard Worker DebugLoc TermDL = Head->getFirstTerminator()->getDebugLoc();
571*9880d681SAndroid Build Coastguard Worker TII->RemoveBranch(*Head);
572*9880d681SAndroid Build Coastguard Worker
573*9880d681SAndroid Build Coastguard Worker // If the Head terminator was one of the cbz / tbz branches with built-in
574*9880d681SAndroid Build Coastguard Worker // compare, we need to insert an explicit compare instruction in its place.
575*9880d681SAndroid Build Coastguard Worker if (HeadCond[0].getImm() == -1) {
576*9880d681SAndroid Build Coastguard Worker ++NumCompBranches;
577*9880d681SAndroid Build Coastguard Worker unsigned Opc = 0;
578*9880d681SAndroid Build Coastguard Worker switch (HeadCond[1].getImm()) {
579*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
580*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
581*9880d681SAndroid Build Coastguard Worker Opc = AArch64::SUBSWri;
582*9880d681SAndroid Build Coastguard Worker break;
583*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
584*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
585*9880d681SAndroid Build Coastguard Worker Opc = AArch64::SUBSXri;
586*9880d681SAndroid Build Coastguard Worker break;
587*9880d681SAndroid Build Coastguard Worker default:
588*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Cannot convert Head branch");
589*9880d681SAndroid Build Coastguard Worker }
590*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = TII->get(Opc);
591*9880d681SAndroid Build Coastguard Worker // Create a dummy virtual register for the SUBS def.
592*9880d681SAndroid Build Coastguard Worker unsigned DestReg =
593*9880d681SAndroid Build Coastguard Worker MRI->createVirtualRegister(TII->getRegClass(MCID, 0, TRI, *MF));
594*9880d681SAndroid Build Coastguard Worker // Insert a SUBS Rn, #0 instruction instead of the cbz / cbnz.
595*9880d681SAndroid Build Coastguard Worker BuildMI(*Head, Head->end(), TermDL, MCID)
596*9880d681SAndroid Build Coastguard Worker .addReg(DestReg, RegState::Define | RegState::Dead)
597*9880d681SAndroid Build Coastguard Worker .addOperand(HeadCond[2])
598*9880d681SAndroid Build Coastguard Worker .addImm(0)
599*9880d681SAndroid Build Coastguard Worker .addImm(0);
600*9880d681SAndroid Build Coastguard Worker // SUBS uses the GPR*sp register classes.
601*9880d681SAndroid Build Coastguard Worker MRI->constrainRegClass(HeadCond[2].getReg(),
602*9880d681SAndroid Build Coastguard Worker TII->getRegClass(MCID, 1, TRI, *MF));
603*9880d681SAndroid Build Coastguard Worker }
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard Worker Head->splice(Head->end(), CmpBB, CmpBB->begin(), CmpBB->end());
606*9880d681SAndroid Build Coastguard Worker
607*9880d681SAndroid Build Coastguard Worker // Now replace CmpMI with a ccmp instruction that also considers the incoming
608*9880d681SAndroid Build Coastguard Worker // flags.
609*9880d681SAndroid Build Coastguard Worker unsigned Opc = 0;
610*9880d681SAndroid Build Coastguard Worker unsigned FirstOp = 1; // First CmpMI operand to copy.
611*9880d681SAndroid Build Coastguard Worker bool isZBranch = false; // CmpMI is a cbz/cbnz instruction.
612*9880d681SAndroid Build Coastguard Worker switch (CmpMI->getOpcode()) {
613*9880d681SAndroid Build Coastguard Worker default:
614*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unknown compare opcode");
615*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSWri: Opc = AArch64::CCMPWi; break;
616*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSWrr: Opc = AArch64::CCMPWr; break;
617*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSXri: Opc = AArch64::CCMPXi; break;
618*9880d681SAndroid Build Coastguard Worker case AArch64::SUBSXrr: Opc = AArch64::CCMPXr; break;
619*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSWri: Opc = AArch64::CCMNWi; break;
620*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSWrr: Opc = AArch64::CCMNWr; break;
621*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSXri: Opc = AArch64::CCMNXi; break;
622*9880d681SAndroid Build Coastguard Worker case AArch64::ADDSXrr: Opc = AArch64::CCMNXr; break;
623*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPSrr: Opc = AArch64::FCCMPSrr; FirstOp = 0; break;
624*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPDrr: Opc = AArch64::FCCMPDrr; FirstOp = 0; break;
625*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPESrr: Opc = AArch64::FCCMPESrr; FirstOp = 0; break;
626*9880d681SAndroid Build Coastguard Worker case AArch64::FCMPEDrr: Opc = AArch64::FCCMPEDrr; FirstOp = 0; break;
627*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
628*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
629*9880d681SAndroid Build Coastguard Worker Opc = AArch64::CCMPWi;
630*9880d681SAndroid Build Coastguard Worker FirstOp = 0;
631*9880d681SAndroid Build Coastguard Worker isZBranch = true;
632*9880d681SAndroid Build Coastguard Worker break;
633*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
634*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
635*9880d681SAndroid Build Coastguard Worker Opc = AArch64::CCMPXi;
636*9880d681SAndroid Build Coastguard Worker FirstOp = 0;
637*9880d681SAndroid Build Coastguard Worker isZBranch = true;
638*9880d681SAndroid Build Coastguard Worker break;
639*9880d681SAndroid Build Coastguard Worker }
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard Worker // The ccmp instruction should set the flags according to the comparison when
642*9880d681SAndroid Build Coastguard Worker // Head would have branched to CmpBB.
643*9880d681SAndroid Build Coastguard Worker // The NZCV immediate operand should provide flags for the case where Head
644*9880d681SAndroid Build Coastguard Worker // would have branched to Tail. These flags should cause the new Head
645*9880d681SAndroid Build Coastguard Worker // terminator to branch to tail.
646*9880d681SAndroid Build Coastguard Worker unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(CmpBBTailCC);
647*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = TII->get(Opc);
648*9880d681SAndroid Build Coastguard Worker MRI->constrainRegClass(CmpMI->getOperand(FirstOp).getReg(),
649*9880d681SAndroid Build Coastguard Worker TII->getRegClass(MCID, 0, TRI, *MF));
650*9880d681SAndroid Build Coastguard Worker if (CmpMI->getOperand(FirstOp + 1).isReg())
651*9880d681SAndroid Build Coastguard Worker MRI->constrainRegClass(CmpMI->getOperand(FirstOp + 1).getReg(),
652*9880d681SAndroid Build Coastguard Worker TII->getRegClass(MCID, 1, TRI, *MF));
653*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder MIB =
654*9880d681SAndroid Build Coastguard Worker BuildMI(*Head, CmpMI, CmpMI->getDebugLoc(), MCID)
655*9880d681SAndroid Build Coastguard Worker .addOperand(CmpMI->getOperand(FirstOp)); // Register Rn
656*9880d681SAndroid Build Coastguard Worker if (isZBranch)
657*9880d681SAndroid Build Coastguard Worker MIB.addImm(0); // cbz/cbnz Rn -> ccmp Rn, #0
658*9880d681SAndroid Build Coastguard Worker else
659*9880d681SAndroid Build Coastguard Worker MIB.addOperand(CmpMI->getOperand(FirstOp + 1)); // Register Rm / Immediate
660*9880d681SAndroid Build Coastguard Worker MIB.addImm(NZCV).addImm(HeadCmpBBCC);
661*9880d681SAndroid Build Coastguard Worker
662*9880d681SAndroid Build Coastguard Worker // If CmpMI was a terminator, we need a new conditional branch to replace it.
663*9880d681SAndroid Build Coastguard Worker // This now becomes a Head terminator.
664*9880d681SAndroid Build Coastguard Worker if (isZBranch) {
665*9880d681SAndroid Build Coastguard Worker bool isNZ = CmpMI->getOpcode() == AArch64::CBNZW ||
666*9880d681SAndroid Build Coastguard Worker CmpMI->getOpcode() == AArch64::CBNZX;
667*9880d681SAndroid Build Coastguard Worker BuildMI(*Head, CmpMI, CmpMI->getDebugLoc(), TII->get(AArch64::Bcc))
668*9880d681SAndroid Build Coastguard Worker .addImm(isNZ ? AArch64CC::NE : AArch64CC::EQ)
669*9880d681SAndroid Build Coastguard Worker .addOperand(CmpMI->getOperand(1)); // Branch target.
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker CmpMI->eraseFromParent();
672*9880d681SAndroid Build Coastguard Worker Head->updateTerminator();
673*9880d681SAndroid Build Coastguard Worker
674*9880d681SAndroid Build Coastguard Worker RemovedBlocks.push_back(CmpBB);
675*9880d681SAndroid Build Coastguard Worker CmpBB->eraseFromParent();
676*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Result:\n" << *Head);
677*9880d681SAndroid Build Coastguard Worker ++NumConverted;
678*9880d681SAndroid Build Coastguard Worker }
679*9880d681SAndroid Build Coastguard Worker
expectedCodeSizeDelta() const680*9880d681SAndroid Build Coastguard Worker int SSACCmpConv::expectedCodeSizeDelta() const {
681*9880d681SAndroid Build Coastguard Worker int delta = 0;
682*9880d681SAndroid Build Coastguard Worker // If the Head terminator was one of the cbz / tbz branches with built-in
683*9880d681SAndroid Build Coastguard Worker // compare, we need to insert an explicit compare instruction in its place
684*9880d681SAndroid Build Coastguard Worker // plus a branch instruction.
685*9880d681SAndroid Build Coastguard Worker if (HeadCond[0].getImm() == -1) {
686*9880d681SAndroid Build Coastguard Worker switch (HeadCond[1].getImm()) {
687*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
688*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
689*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
690*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
691*9880d681SAndroid Build Coastguard Worker // Therefore delta += 1
692*9880d681SAndroid Build Coastguard Worker delta = 1;
693*9880d681SAndroid Build Coastguard Worker break;
694*9880d681SAndroid Build Coastguard Worker default:
695*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Cannot convert Head branch");
696*9880d681SAndroid Build Coastguard Worker }
697*9880d681SAndroid Build Coastguard Worker }
698*9880d681SAndroid Build Coastguard Worker // If the Cmp terminator was one of the cbz / tbz branches with
699*9880d681SAndroid Build Coastguard Worker // built-in compare, it will be turned into a compare instruction
700*9880d681SAndroid Build Coastguard Worker // into Head, but we do not save any instruction.
701*9880d681SAndroid Build Coastguard Worker // Otherwise, we save the branch instruction.
702*9880d681SAndroid Build Coastguard Worker switch (CmpMI->getOpcode()) {
703*9880d681SAndroid Build Coastguard Worker default:
704*9880d681SAndroid Build Coastguard Worker --delta;
705*9880d681SAndroid Build Coastguard Worker break;
706*9880d681SAndroid Build Coastguard Worker case AArch64::CBZW:
707*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZW:
708*9880d681SAndroid Build Coastguard Worker case AArch64::CBZX:
709*9880d681SAndroid Build Coastguard Worker case AArch64::CBNZX:
710*9880d681SAndroid Build Coastguard Worker break;
711*9880d681SAndroid Build Coastguard Worker }
712*9880d681SAndroid Build Coastguard Worker return delta;
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
716*9880d681SAndroid Build Coastguard Worker // AArch64ConditionalCompares Pass
717*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
718*9880d681SAndroid Build Coastguard Worker
719*9880d681SAndroid Build Coastguard Worker namespace {
720*9880d681SAndroid Build Coastguard Worker class AArch64ConditionalCompares : public MachineFunctionPass {
721*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
722*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
723*9880d681SAndroid Build Coastguard Worker MCSchedModel SchedModel;
724*9880d681SAndroid Build Coastguard Worker // Does the proceeded function has Oz attribute.
725*9880d681SAndroid Build Coastguard Worker bool MinSize;
726*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI;
727*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DomTree;
728*9880d681SAndroid Build Coastguard Worker MachineLoopInfo *Loops;
729*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics *Traces;
730*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Ensemble *MinInstr;
731*9880d681SAndroid Build Coastguard Worker SSACCmpConv CmpConv;
732*9880d681SAndroid Build Coastguard Worker
733*9880d681SAndroid Build Coastguard Worker public:
734*9880d681SAndroid Build Coastguard Worker static char ID;
AArch64ConditionalCompares()735*9880d681SAndroid Build Coastguard Worker AArch64ConditionalCompares() : MachineFunctionPass(ID) {}
736*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override;
737*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
getPassName() const738*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
739*9880d681SAndroid Build Coastguard Worker return "AArch64 Conditional Compares";
740*9880d681SAndroid Build Coastguard Worker }
741*9880d681SAndroid Build Coastguard Worker
742*9880d681SAndroid Build Coastguard Worker private:
743*9880d681SAndroid Build Coastguard Worker bool tryConvert(MachineBasicBlock *);
744*9880d681SAndroid Build Coastguard Worker void updateDomTree(ArrayRef<MachineBasicBlock *> Removed);
745*9880d681SAndroid Build Coastguard Worker void updateLoops(ArrayRef<MachineBasicBlock *> Removed);
746*9880d681SAndroid Build Coastguard Worker void invalidateTraces();
747*9880d681SAndroid Build Coastguard Worker bool shouldConvert();
748*9880d681SAndroid Build Coastguard Worker };
749*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
750*9880d681SAndroid Build Coastguard Worker
751*9880d681SAndroid Build Coastguard Worker char AArch64ConditionalCompares::ID = 0;
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker namespace llvm {
754*9880d681SAndroid Build Coastguard Worker void initializeAArch64ConditionalComparesPass(PassRegistry &);
755*9880d681SAndroid Build Coastguard Worker }
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(AArch64ConditionalCompares, "aarch64-ccmp",
758*9880d681SAndroid Build Coastguard Worker "AArch64 CCMP Pass", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)759*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
760*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
761*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(AArch64ConditionalCompares, "aarch64-ccmp",
762*9880d681SAndroid Build Coastguard Worker "AArch64 CCMP Pass", false, false)
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAArch64ConditionalCompares() {
765*9880d681SAndroid Build Coastguard Worker return new AArch64ConditionalCompares();
766*9880d681SAndroid Build Coastguard Worker }
767*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const768*9880d681SAndroid Build Coastguard Worker void AArch64ConditionalCompares::getAnalysisUsage(AnalysisUsage &AU) const {
769*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
770*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
771*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineLoopInfo>();
772*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineLoopInfo>();
773*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineTraceMetrics>();
774*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineTraceMetrics>();
775*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
776*9880d681SAndroid Build Coastguard Worker }
777*9880d681SAndroid Build Coastguard Worker
778*9880d681SAndroid Build Coastguard Worker /// Update the dominator tree after if-conversion erased some blocks.
updateDomTree(ArrayRef<MachineBasicBlock * > Removed)779*9880d681SAndroid Build Coastguard Worker void AArch64ConditionalCompares::updateDomTree(
780*9880d681SAndroid Build Coastguard Worker ArrayRef<MachineBasicBlock *> Removed) {
781*9880d681SAndroid Build Coastguard Worker // convert() removes CmpBB which was previously dominated by Head.
782*9880d681SAndroid Build Coastguard Worker // CmpBB children should be transferred to Head.
783*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *HeadNode = DomTree->getNode(CmpConv.Head);
784*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *RemovedMBB : Removed) {
785*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = DomTree->getNode(RemovedMBB);
786*9880d681SAndroid Build Coastguard Worker assert(Node != HeadNode && "Cannot erase the head node");
787*9880d681SAndroid Build Coastguard Worker assert(Node->getIDom() == HeadNode && "CmpBB should be dominated by Head");
788*9880d681SAndroid Build Coastguard Worker while (Node->getNumChildren())
789*9880d681SAndroid Build Coastguard Worker DomTree->changeImmediateDominator(Node->getChildren().back(), HeadNode);
790*9880d681SAndroid Build Coastguard Worker DomTree->eraseNode(RemovedMBB);
791*9880d681SAndroid Build Coastguard Worker }
792*9880d681SAndroid Build Coastguard Worker }
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker /// Update LoopInfo after if-conversion.
795*9880d681SAndroid Build Coastguard Worker void
updateLoops(ArrayRef<MachineBasicBlock * > Removed)796*9880d681SAndroid Build Coastguard Worker AArch64ConditionalCompares::updateLoops(ArrayRef<MachineBasicBlock *> Removed) {
797*9880d681SAndroid Build Coastguard Worker if (!Loops)
798*9880d681SAndroid Build Coastguard Worker return;
799*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *RemovedMBB : Removed)
800*9880d681SAndroid Build Coastguard Worker Loops->removeBlock(RemovedMBB);
801*9880d681SAndroid Build Coastguard Worker }
802*9880d681SAndroid Build Coastguard Worker
803*9880d681SAndroid Build Coastguard Worker /// Invalidate MachineTraceMetrics before if-conversion.
invalidateTraces()804*9880d681SAndroid Build Coastguard Worker void AArch64ConditionalCompares::invalidateTraces() {
805*9880d681SAndroid Build Coastguard Worker Traces->invalidate(CmpConv.Head);
806*9880d681SAndroid Build Coastguard Worker Traces->invalidate(CmpConv.CmpBB);
807*9880d681SAndroid Build Coastguard Worker }
808*9880d681SAndroid Build Coastguard Worker
809*9880d681SAndroid Build Coastguard Worker /// Apply cost model and heuristics to the if-conversion in IfConv.
810*9880d681SAndroid Build Coastguard Worker /// Return true if the conversion is a good idea.
811*9880d681SAndroid Build Coastguard Worker ///
shouldConvert()812*9880d681SAndroid Build Coastguard Worker bool AArch64ConditionalCompares::shouldConvert() {
813*9880d681SAndroid Build Coastguard Worker // Stress testing mode disables all cost considerations.
814*9880d681SAndroid Build Coastguard Worker if (Stress)
815*9880d681SAndroid Build Coastguard Worker return true;
816*9880d681SAndroid Build Coastguard Worker if (!MinInstr)
817*9880d681SAndroid Build Coastguard Worker MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount);
818*9880d681SAndroid Build Coastguard Worker
819*9880d681SAndroid Build Coastguard Worker // Head dominates CmpBB, so it is always included in its trace.
820*9880d681SAndroid Build Coastguard Worker MachineTraceMetrics::Trace Trace = MinInstr->getTrace(CmpConv.CmpBB);
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard Worker // If code size is the main concern
823*9880d681SAndroid Build Coastguard Worker if (MinSize) {
824*9880d681SAndroid Build Coastguard Worker int CodeSizeDelta = CmpConv.expectedCodeSizeDelta();
825*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Code size delta: " << CodeSizeDelta << '\n');
826*9880d681SAndroid Build Coastguard Worker // If we are minimizing the code size, do the conversion whatever
827*9880d681SAndroid Build Coastguard Worker // the cost is.
828*9880d681SAndroid Build Coastguard Worker if (CodeSizeDelta < 0)
829*9880d681SAndroid Build Coastguard Worker return true;
830*9880d681SAndroid Build Coastguard Worker if (CodeSizeDelta > 0) {
831*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Code size is increasing, give up on this one.\n");
832*9880d681SAndroid Build Coastguard Worker return false;
833*9880d681SAndroid Build Coastguard Worker }
834*9880d681SAndroid Build Coastguard Worker // CodeSizeDelta == 0, continue with the regular heuristics
835*9880d681SAndroid Build Coastguard Worker }
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker // Heuristic: The compare conversion delays the execution of the branch
838*9880d681SAndroid Build Coastguard Worker // instruction because we must wait for the inputs to the second compare as
839*9880d681SAndroid Build Coastguard Worker // well. The branch has no dependent instructions, but delaying it increases
840*9880d681SAndroid Build Coastguard Worker // the cost of a misprediction.
841*9880d681SAndroid Build Coastguard Worker //
842*9880d681SAndroid Build Coastguard Worker // Set a limit on the delay we will accept.
843*9880d681SAndroid Build Coastguard Worker unsigned DelayLimit = SchedModel.MispredictPenalty * 3 / 4;
844*9880d681SAndroid Build Coastguard Worker
845*9880d681SAndroid Build Coastguard Worker // Instruction depths can be computed for all trace instructions above CmpBB.
846*9880d681SAndroid Build Coastguard Worker unsigned HeadDepth =
847*9880d681SAndroid Build Coastguard Worker Trace.getInstrCycles(*CmpConv.Head->getFirstTerminator()).Depth;
848*9880d681SAndroid Build Coastguard Worker unsigned CmpBBDepth =
849*9880d681SAndroid Build Coastguard Worker Trace.getInstrCycles(*CmpConv.CmpBB->getFirstTerminator()).Depth;
850*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Head depth: " << HeadDepth
851*9880d681SAndroid Build Coastguard Worker << "\nCmpBB depth: " << CmpBBDepth << '\n');
852*9880d681SAndroid Build Coastguard Worker if (CmpBBDepth > HeadDepth + DelayLimit) {
853*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Branch delay would be larger than " << DelayLimit
854*9880d681SAndroid Build Coastguard Worker << " cycles.\n");
855*9880d681SAndroid Build Coastguard Worker return false;
856*9880d681SAndroid Build Coastguard Worker }
857*9880d681SAndroid Build Coastguard Worker
858*9880d681SAndroid Build Coastguard Worker // Check the resource depth at the bottom of CmpBB - these instructions will
859*9880d681SAndroid Build Coastguard Worker // be speculated.
860*9880d681SAndroid Build Coastguard Worker unsigned ResDepth = Trace.getResourceDepth(true);
861*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Resources: " << ResDepth << '\n');
862*9880d681SAndroid Build Coastguard Worker
863*9880d681SAndroid Build Coastguard Worker // Heuristic: The speculatively executed instructions must all be able to
864*9880d681SAndroid Build Coastguard Worker // merge into the Head block. The Head critical path should dominate the
865*9880d681SAndroid Build Coastguard Worker // resource cost of the speculated instructions.
866*9880d681SAndroid Build Coastguard Worker if (ResDepth > HeadDepth) {
867*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Too many instructions to speculate.\n");
868*9880d681SAndroid Build Coastguard Worker return false;
869*9880d681SAndroid Build Coastguard Worker }
870*9880d681SAndroid Build Coastguard Worker return true;
871*9880d681SAndroid Build Coastguard Worker }
872*9880d681SAndroid Build Coastguard Worker
tryConvert(MachineBasicBlock * MBB)873*9880d681SAndroid Build Coastguard Worker bool AArch64ConditionalCompares::tryConvert(MachineBasicBlock *MBB) {
874*9880d681SAndroid Build Coastguard Worker bool Changed = false;
875*9880d681SAndroid Build Coastguard Worker while (CmpConv.canConvert(MBB) && shouldConvert()) {
876*9880d681SAndroid Build Coastguard Worker invalidateTraces();
877*9880d681SAndroid Build Coastguard Worker SmallVector<MachineBasicBlock *, 4> RemovedBlocks;
878*9880d681SAndroid Build Coastguard Worker CmpConv.convert(RemovedBlocks);
879*9880d681SAndroid Build Coastguard Worker Changed = true;
880*9880d681SAndroid Build Coastguard Worker updateDomTree(RemovedBlocks);
881*9880d681SAndroid Build Coastguard Worker updateLoops(RemovedBlocks);
882*9880d681SAndroid Build Coastguard Worker }
883*9880d681SAndroid Build Coastguard Worker return Changed;
884*9880d681SAndroid Build Coastguard Worker }
885*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)886*9880d681SAndroid Build Coastguard Worker bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) {
887*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "********** AArch64 Conditional Compares **********\n"
888*9880d681SAndroid Build Coastguard Worker << "********** Function: " << MF.getName() << '\n');
889*9880d681SAndroid Build Coastguard Worker if (skipFunction(*MF.getFunction()))
890*9880d681SAndroid Build Coastguard Worker return false;
891*9880d681SAndroid Build Coastguard Worker
892*9880d681SAndroid Build Coastguard Worker TII = MF.getSubtarget().getInstrInfo();
893*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
894*9880d681SAndroid Build Coastguard Worker SchedModel = MF.getSubtarget().getSchedModel();
895*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
896*9880d681SAndroid Build Coastguard Worker DomTree = &getAnalysis<MachineDominatorTree>();
897*9880d681SAndroid Build Coastguard Worker Loops = getAnalysisIfAvailable<MachineLoopInfo>();
898*9880d681SAndroid Build Coastguard Worker Traces = &getAnalysis<MachineTraceMetrics>();
899*9880d681SAndroid Build Coastguard Worker MinInstr = nullptr;
900*9880d681SAndroid Build Coastguard Worker MinSize = MF.getFunction()->optForMinSize();
901*9880d681SAndroid Build Coastguard Worker
902*9880d681SAndroid Build Coastguard Worker bool Changed = false;
903*9880d681SAndroid Build Coastguard Worker CmpConv.runOnMachineFunction(MF);
904*9880d681SAndroid Build Coastguard Worker
905*9880d681SAndroid Build Coastguard Worker // Visit blocks in dominator tree pre-order. The pre-order enables multiple
906*9880d681SAndroid Build Coastguard Worker // cmp-conversions from the same head block.
907*9880d681SAndroid Build Coastguard Worker // Note that updateDomTree() modifies the children of the DomTree node
908*9880d681SAndroid Build Coastguard Worker // currently being visited. The df_iterator supports that; it doesn't look at
909*9880d681SAndroid Build Coastguard Worker // child_begin() / child_end() until after a node has been visited.
910*9880d681SAndroid Build Coastguard Worker for (auto *I : depth_first(DomTree))
911*9880d681SAndroid Build Coastguard Worker if (tryConvert(I->getBlock()))
912*9880d681SAndroid Build Coastguard Worker Changed = true;
913*9880d681SAndroid Build Coastguard Worker
914*9880d681SAndroid Build Coastguard Worker return Changed;
915*9880d681SAndroid Build Coastguard Worker }
916