1//===-------------- PPCMIPeephole.cpp - MI Peephole Cleanups -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===---------------------------------------------------------------------===//
8//
9// This pass performs peephole optimizations to clean up ugly code
10// sequences at the MachineInstruction layer. It runs at the end of
11// the SSA phases, following VSX swap removal. A pass of dead code
12// elimination follows this one for quick clean-up of any dead
13// instructions introduced here. Although we could do this as callbacks
14// from the generic peephole pass, this would have a couple of bad
15// effects: it might remove optimization opportunities for VSX swap
16// removal, and it would miss cleanups made possible following VSX
17// swap removal.
18//
19// NOTE: We run the verifier after this pass in Asserts/Debug builds so it
20// is important to keep the code valid after transformations.
21// Common causes of errors stem from violating the contract specified
22// by kill flags. Whenever a transformation changes the live range of
23// a register, that register should be added to the work list using
24// addRegToUpdate(RegsToUpdate, <Reg>). Furthermore, if a transformation
25// is changing the definition of a register (i.e. removing the single
26// definition of the original vreg), it needs to provide a dummy
27// definition of that register using addDummyDef(<MBB>, <Reg>).
28//===---------------------------------------------------------------------===//
29
30#include "MCTargetDesc/PPCMCTargetDesc.h"
31#include "MCTargetDesc/PPCPredicates.h"
32#include "PPC.h"
33#include "PPCInstrInfo.h"
34#include "PPCMachineFunctionInfo.h"
35#include "PPCTargetMachine.h"
36#include "llvm/ADT/Statistic.h"
37#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
38#include "llvm/CodeGen/MachineDominators.h"
39#include "llvm/CodeGen/MachineFrameInfo.h"
40#include "llvm/CodeGen/MachineFunctionPass.h"
41#include "llvm/CodeGen/MachineInstrBuilder.h"
42#include "llvm/CodeGen/MachinePostDominators.h"
43#include "llvm/CodeGen/MachineRegisterInfo.h"
44#include "llvm/CodeGen/RegisterClassInfo.h"
45#include "llvm/IR/GlobalVariable.h"
46#include "llvm/InitializePasses.h"
47#include "llvm/Support/Debug.h"
48#include "llvm/Support/DebugCounter.h"
49
50using namespace llvm;
51
52#define DEBUG_TYPE "ppc-mi-peepholes"
53
54STATISTIC(RemoveTOCSave, "Number of TOC saves removed");
55STATISTIC(MultiTOCSaves,
56 "Number of functions with multiple TOC saves that must be kept");
57STATISTIC(NumTOCSavesInPrologue, "Number of TOC saves placed in the prologue");
58STATISTIC(NumEliminatedSExt, "Number of eliminated sign-extensions");
59STATISTIC(NumEliminatedZExt, "Number of eliminated zero-extensions");
60STATISTIC(NumOptADDLIs, "Number of optimized ADD instruction fed by LI");
61STATISTIC(NumConvertedToImmediateForm,
62 "Number of instructions converted to their immediate form");
63STATISTIC(NumFunctionsEnteredInMIPeephole,
64 "Number of functions entered in PPC MI Peepholes");
65STATISTIC(NumFixedPointIterations,
66 "Number of fixed-point iterations converting reg-reg instructions "
67 "to reg-imm ones");
68STATISTIC(NumRotatesCollapsed,
69 "Number of pairs of rotate left, clear left/right collapsed");
70STATISTIC(NumEXTSWAndSLDICombined,
71 "Number of pairs of EXTSW and SLDI combined as EXTSWSLI");
72STATISTIC(NumLoadImmZeroFoldedAndRemoved,
73 "Number of LI(8) reg, 0 that are folded to r0 and removed");
74
75static cl::opt<bool>
76FixedPointRegToImm("ppc-reg-to-imm-fixed-point", cl::Hidden, cl::init(Val: true),
77 cl::desc("Iterate to a fixed point when attempting to "
78 "convert reg-reg instructions to reg-imm"));
79
80static cl::opt<bool>
81ConvertRegReg("ppc-convert-rr-to-ri", cl::Hidden, cl::init(Val: true),
82 cl::desc("Convert eligible reg+reg instructions to reg+imm"));
83
84static cl::opt<bool>
85 EnableSExtElimination("ppc-eliminate-signext",
86 cl::desc("enable elimination of sign-extensions"),
87 cl::init(Val: true), cl::Hidden);
88
89static cl::opt<bool>
90 EnableZExtElimination("ppc-eliminate-zeroext",
91 cl::desc("enable elimination of zero-extensions"),
92 cl::init(Val: true), cl::Hidden);
93
94static cl::opt<bool>
95 EnableTrapOptimization("ppc-opt-conditional-trap",
96 cl::desc("enable optimization of conditional traps"),
97 cl::init(Val: false), cl::Hidden);
98
99DEBUG_COUNTER(
100 PeepholeXToICounter, "ppc-xtoi-peephole",
101 "Controls whether PPC reg+reg to reg+imm peephole is performed on a MI");
102
103DEBUG_COUNTER(PeepholePerOpCounter, "ppc-per-op-peephole",
104 "Controls whether PPC per opcode peephole is performed on a MI");
105
106namespace {
107
108struct PPCMIPeephole : public MachineFunctionPass {
109
110 static char ID;
111 const PPCInstrInfo *TII;
112 MachineFunction *MF;
113 MachineRegisterInfo *MRI;
114
115 PPCMIPeephole() : MachineFunctionPass(ID) {}
116
117private:
118 MachineDominatorTree *MDT;
119 MachinePostDominatorTree *MPDT;
120 MachineBlockFrequencyInfo *MBFI;
121 BlockFrequency EntryFreq;
122 SmallSet<Register, 16> RegsToUpdate;
123
124 // Initialize class variables.
125 void initialize(MachineFunction &MFParm);
126
127 // Perform peepholes.
128 bool simplifyCode();
129
130 // Perform peepholes.
131 bool eliminateRedundantCompare();
132 bool eliminateRedundantTOCSaves(std::map<MachineInstr *, bool> &TOCSaves);
133 bool combineSEXTAndSHL(MachineInstr &MI, MachineInstr *&ToErase);
134 bool emitRLDICWhenLoweringJumpTables(MachineInstr &MI,
135 MachineInstr *&ToErase);
136 void UpdateTOCSaves(std::map<MachineInstr *, bool> &TOCSaves,
137 MachineInstr *MI);
138
139 // A number of transformations will eliminate the definition of a register
140 // as all of its uses will be removed. However, this can leave a register
141 // used with no reaching definition until DCE removes the dead uses. Such
142 // transformations should use this function to provide a dummy definition of
143 // the register that will simply be removed by DCE.
144 void addDummyDef(MachineBasicBlock &MBB, MachineInstr *At, Register Reg) {
145 BuildMI(BB&: MBB, I: At, MIMD: At->getDebugLoc(), MCID: TII->get(Opcode: PPC::IMPLICIT_DEF), DestReg: Reg);
146 }
147 void addRegToUpdateWithLine(Register Reg, int Line);
148 void convertUnprimedAccPHIs(const PPCInstrInfo *TII, MachineRegisterInfo *MRI,
149 SmallVectorImpl<MachineInstr *> &PHIs,
150 Register Dst);
151
152public:
153 void getAnalysisUsage(AnalysisUsage &AU) const override {
154 AU.addRequired<MachineDominatorTreeWrapperPass>();
155 AU.addRequired<MachinePostDominatorTreeWrapperPass>();
156 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
157 AU.addPreserved<MachineDominatorTreeWrapperPass>();
158 AU.addPreserved<MachinePostDominatorTreeWrapperPass>();
159 AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>();
160 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
161 MachineFunctionPass::getAnalysisUsage(AU);
162 }
163
164 // Main entry point for this pass.
165 bool runOnMachineFunction(MachineFunction &MF) override {
166 initialize(MFParm&: MF);
167 // At this point, TOC pointer should not be used in a function that uses
168 // PC-Relative addressing.
169 assert((MF.getRegInfo().use_empty(PPC::X2) ||
170 !MF.getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls()) &&
171 "TOC pointer used in a function using PC-Relative addressing!");
172 if (skipFunction(F: MF.getFunction()))
173 return false;
174 return simplifyCode();
175 }
176};
177
178#define addRegToUpdate(R) addRegToUpdateWithLine(R, __LINE__)
179void PPCMIPeephole::addRegToUpdateWithLine(Register Reg, int Line) {
180 if (!Reg.isVirtual())
181 return;
182 if (RegsToUpdate.insert(V: Reg).second)
183 LLVM_DEBUG(dbgs() << "Adding register: " << printReg(Reg) << " on line "
184 << Line << " for re-computation of kill flags\n");
185}
186
187// Initialize class variables.
188void PPCMIPeephole::initialize(MachineFunction &MFParm) {
189 MF = &MFParm;
190 MRI = &MF->getRegInfo();
191 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
192 MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
193 MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
194 EntryFreq = MBFI->getEntryFreq();
195 TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
196 RegsToUpdate.clear();
197 LLVM_DEBUG(dbgs() << "*** PowerPC MI peephole pass ***\n\n");
198 LLVM_DEBUG(MF->dump());
199}
200
201static MachineInstr *getVRegDefOrNull(MachineOperand *Op,
202 MachineRegisterInfo *MRI) {
203 assert(Op && "Invalid Operand!");
204 if (!Op->isReg())
205 return nullptr;
206
207 Register Reg = Op->getReg();
208 if (!Reg.isVirtual())
209 return nullptr;
210
211 return MRI->getVRegDef(Reg);
212}
213
214// This function returns number of known zero bits in output of MI
215// starting from the most significant bit.
216static unsigned getKnownLeadingZeroCount(const unsigned Reg,
217 const PPCInstrInfo *TII,
218 const MachineRegisterInfo *MRI) {
219 MachineInstr *MI = MRI->getVRegDef(Reg);
220 unsigned Opcode = MI->getOpcode();
221 if (Opcode == PPC::RLDICL || Opcode == PPC::RLDICL_rec ||
222 Opcode == PPC::RLDCL || Opcode == PPC::RLDCL_rec)
223 return MI->getOperand(i: 3).getImm();
224
225 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDIC_rec) &&
226 MI->getOperand(i: 3).getImm() <= 63 - MI->getOperand(i: 2).getImm())
227 return MI->getOperand(i: 3).getImm();
228
229 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec ||
230 Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec ||
231 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) &&
232 MI->getOperand(i: 3).getImm() <= MI->getOperand(i: 4).getImm())
233 return 32 + MI->getOperand(i: 3).getImm();
234
235 if (Opcode == PPC::ANDI_rec) {
236 uint16_t Imm = MI->getOperand(i: 2).getImm();
237 return 48 + llvm::countl_zero(Val: Imm);
238 }
239
240 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZW_rec ||
241 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZW_rec ||
242 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8)
243 // The result ranges from 0 to 32.
244 return 58;
245
246 if (Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZD_rec ||
247 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZD_rec)
248 // The result ranges from 0 to 64.
249 return 57;
250
251 if (Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
252 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
253 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
254 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8)
255 return 48;
256
257 if (Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
258 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
259 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
260 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8)
261 return 56;
262
263 if (Opcode == PPC::AND || Opcode == PPC::AND8 || Opcode == PPC::AND_rec ||
264 Opcode == PPC::AND8_rec)
265 return std::max(
266 a: getKnownLeadingZeroCount(Reg: MI->getOperand(i: 1).getReg(), TII, MRI),
267 b: getKnownLeadingZeroCount(Reg: MI->getOperand(i: 2).getReg(), TII, MRI));
268
269 if (Opcode == PPC::OR || Opcode == PPC::OR8 || Opcode == PPC::XOR ||
270 Opcode == PPC::XOR8 || Opcode == PPC::OR_rec ||
271 Opcode == PPC::OR8_rec || Opcode == PPC::XOR_rec ||
272 Opcode == PPC::XOR8_rec)
273 return std::min(
274 a: getKnownLeadingZeroCount(Reg: MI->getOperand(i: 1).getReg(), TII, MRI),
275 b: getKnownLeadingZeroCount(Reg: MI->getOperand(i: 2).getReg(), TII, MRI));
276
277 if (TII->isZeroExtended(Reg, MRI))
278 return 32;
279
280 return 0;
281}
282
283// This function maintains a map for the pairs <TOC Save Instr, Keep>
284// Each time a new TOC save is encountered, it checks if any of the existing
285// ones are dominated by the new one. If so, it marks the existing one as
286// redundant by setting it's entry in the map as false. It then adds the new
287// instruction to the map with either true or false depending on if any
288// existing instructions dominated the new one.
289void PPCMIPeephole::UpdateTOCSaves(
290 std::map<MachineInstr *, bool> &TOCSaves, MachineInstr *MI) {
291 assert(TII->isTOCSaveMI(*MI) && "Expecting a TOC save instruction here");
292 // FIXME: Saving TOC in prologue hasn't been implemented well in AIX ABI part,
293 // here only support it under ELFv2.
294 if (MF->getSubtarget<PPCSubtarget>().isELFv2ABI()) {
295 PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
296
297 MachineBasicBlock *Entry = &MF->front();
298 BlockFrequency CurrBlockFreq = MBFI->getBlockFreq(MBB: MI->getParent());
299
300 // If the block in which the TOC save resides is in a block that
301 // post-dominates Entry, or a block that is hotter than entry (keep in mind
302 // that early MachineLICM has already run so the TOC save won't be hoisted)
303 // we can just do the save in the prologue.
304 if (CurrBlockFreq > EntryFreq || MPDT->dominates(A: MI->getParent(), B: Entry))
305 FI->setMustSaveTOC(true);
306
307 // If we are saving the TOC in the prologue, all the TOC saves can be
308 // removed from the code.
309 if (FI->mustSaveTOC()) {
310 for (auto &TOCSave : TOCSaves)
311 TOCSave.second = false;
312 // Add new instruction to map.
313 TOCSaves[MI] = false;
314 return;
315 }
316 }
317
318 bool Keep = true;
319 for (auto &I : TOCSaves) {
320 MachineInstr *CurrInst = I.first;
321 // If new instruction dominates an existing one, mark existing one as
322 // redundant.
323 if (I.second && MDT->dominates(A: MI, B: CurrInst))
324 I.second = false;
325 // Check if the new instruction is redundant.
326 if (MDT->dominates(A: CurrInst, B: MI)) {
327 Keep = false;
328 break;
329 }
330 }
331 // Add new instruction to map.
332 TOCSaves[MI] = Keep;
333}
334
335// This function returns a list of all PHI nodes in the tree starting from
336// the RootPHI node. We perform a BFS traversal to get an ordered list of nodes.
337// The list initially only contains the root PHI. When we visit a PHI node, we
338// add it to the list. We continue to look for other PHI node operands while
339// there are nodes to visit in the list. The function returns false if the
340// optimization cannot be applied on this tree.
341static bool collectUnprimedAccPHIs(MachineRegisterInfo *MRI,
342 MachineInstr *RootPHI,
343 SmallVectorImpl<MachineInstr *> &PHIs) {
344 PHIs.push_back(Elt: RootPHI);
345 unsigned VisitedIndex = 0;
346 while (VisitedIndex < PHIs.size()) {
347 MachineInstr *VisitedPHI = PHIs[VisitedIndex];
348 for (unsigned PHIOp = 1, NumOps = VisitedPHI->getNumOperands();
349 PHIOp != NumOps; PHIOp += 2) {
350 Register RegOp = VisitedPHI->getOperand(i: PHIOp).getReg();
351 if (!RegOp.isVirtual())
352 return false;
353 MachineInstr *Instr = MRI->getVRegDef(Reg: RegOp);
354 // While collecting the PHI nodes, we check if they can be converted (i.e.
355 // all the operands are either copies, implicit defs or PHI nodes).
356 unsigned Opcode = Instr->getOpcode();
357 if (Opcode == PPC::COPY) {
358 Register Reg = Instr->getOperand(i: 1).getReg();
359 if (!Reg.isVirtual() || MRI->getRegClass(Reg) != &PPC::ACCRCRegClass)
360 return false;
361 } else if (Opcode != PPC::IMPLICIT_DEF && Opcode != PPC::PHI)
362 return false;
363 // If we detect a cycle in the PHI nodes, we exit. It would be
364 // possible to change cycles as well, but that would add a lot
365 // of complexity for a case that is unlikely to occur with MMA
366 // code.
367 if (Opcode != PPC::PHI)
368 continue;
369 if (llvm::is_contained(Range&: PHIs, Element: Instr))
370 return false;
371 PHIs.push_back(Elt: Instr);
372 }
373 VisitedIndex++;
374 }
375 return true;
376}
377
378// This function changes the unprimed accumulator PHI nodes in the PHIs list to
379// primed accumulator PHI nodes. The list is traversed in reverse order to
380// change all the PHI operands of a PHI node before changing the node itself.
381// We keep a map to associate each changed PHI node to its non-changed form.
382void PPCMIPeephole::convertUnprimedAccPHIs(
383 const PPCInstrInfo *TII, MachineRegisterInfo *MRI,
384 SmallVectorImpl<MachineInstr *> &PHIs, Register Dst) {
385 DenseMap<MachineInstr *, MachineInstr *> ChangedPHIMap;
386 for (MachineInstr *PHI : llvm::reverse(C&: PHIs)) {
387 SmallVector<std::pair<MachineOperand, MachineOperand>, 4> PHIOps;
388 // We check if the current PHI node can be changed by looking at its
389 // operands. If all the operands are either copies from primed
390 // accumulators, implicit definitions or other unprimed accumulator
391 // PHI nodes, we change it.
392 for (unsigned PHIOp = 1, NumOps = PHI->getNumOperands(); PHIOp != NumOps;
393 PHIOp += 2) {
394 Register RegOp = PHI->getOperand(i: PHIOp).getReg();
395 MachineInstr *PHIInput = MRI->getVRegDef(Reg: RegOp);
396 unsigned Opcode = PHIInput->getOpcode();
397 assert((Opcode == PPC::COPY || Opcode == PPC::IMPLICIT_DEF ||
398 Opcode == PPC::PHI) &&
399 "Unexpected instruction");
400 if (Opcode == PPC::COPY) {
401 assert(MRI->getRegClass(PHIInput->getOperand(1).getReg()) ==
402 &PPC::ACCRCRegClass &&
403 "Unexpected register class");
404 PHIOps.push_back(Elt: {PHIInput->getOperand(i: 1), PHI->getOperand(i: PHIOp + 1)});
405 } else if (Opcode == PPC::IMPLICIT_DEF) {
406 Register AccReg = MRI->createVirtualRegister(RegClass: &PPC::ACCRCRegClass);
407 BuildMI(BB&: *PHIInput->getParent(), I: PHIInput, MIMD: PHIInput->getDebugLoc(),
408 MCID: TII->get(Opcode: PPC::IMPLICIT_DEF), DestReg: AccReg);
409 PHIOps.push_back(Elt: {MachineOperand::CreateReg(Reg: AccReg, isDef: false),
410 PHI->getOperand(i: PHIOp + 1)});
411 } else if (Opcode == PPC::PHI) {
412 // We found a PHI operand. At this point we know this operand
413 // has already been changed so we get its associated changed form
414 // from the map.
415 assert(ChangedPHIMap.count(PHIInput) == 1 &&
416 "This PHI node should have already been changed.");
417 MachineInstr *PrimedAccPHI = ChangedPHIMap.lookup(Val: PHIInput);
418 PHIOps.push_back(Elt: {MachineOperand::CreateReg(
419 Reg: PrimedAccPHI->getOperand(i: 0).getReg(), isDef: false),
420 PHI->getOperand(i: PHIOp + 1)});
421 }
422 }
423 Register AccReg = Dst;
424 // If the PHI node we are changing is the root node, the register it defines
425 // will be the destination register of the original copy (of the PHI def).
426 // For all other PHI's in the list, we need to create another primed
427 // accumulator virtual register as the PHI will no longer define the
428 // unprimed accumulator.
429 if (PHI != PHIs[0])
430 AccReg = MRI->createVirtualRegister(RegClass: &PPC::ACCRCRegClass);
431 MachineInstrBuilder NewPHI = BuildMI(
432 BB&: *PHI->getParent(), I: PHI, MIMD: PHI->getDebugLoc(), MCID: TII->get(Opcode: PPC::PHI), DestReg: AccReg);
433 for (auto RegMBB : PHIOps) {
434 NewPHI.add(MO: RegMBB.first).add(MO: RegMBB.second);
435 if (MRI->isSSA())
436 addRegToUpdate(RegMBB.first.getReg());
437 }
438 // The liveness of old PHI and new PHI have to be updated.
439 addRegToUpdate(PHI->getOperand(0).getReg());
440 addRegToUpdate(AccReg);
441 ChangedPHIMap[PHI] = NewPHI.getInstr();
442 LLVM_DEBUG(dbgs() << "Converting PHI: ");
443 LLVM_DEBUG(PHI->dump());
444 LLVM_DEBUG(dbgs() << "To: ");
445 LLVM_DEBUG(NewPHI.getInstr()->dump());
446 }
447}
448
449// Perform peephole optimizations.
450bool PPCMIPeephole::simplifyCode() {
451 bool Simplified = false;
452 bool TrapOpt = false;
453 MachineInstr* ToErase = nullptr;
454 std::map<MachineInstr *, bool> TOCSaves;
455 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
456 NumFunctionsEnteredInMIPeephole++;
457 if (ConvertRegReg) {
458 // Fixed-point conversion of reg/reg instructions fed by load-immediate
459 // into reg/imm instructions. FIXME: This is expensive, control it with
460 // an option.
461 bool SomethingChanged = false;
462 do {
463 NumFixedPointIterations++;
464 SomethingChanged = false;
465 for (MachineBasicBlock &MBB : *MF) {
466 for (MachineInstr &MI : MBB) {
467 if (MI.isDebugInstr())
468 continue;
469
470 if (!DebugCounter::shouldExecute(Counter&: PeepholeXToICounter))
471 continue;
472
473 SmallSet<Register, 4> RRToRIRegsToUpdate;
474 if (!TII->convertToImmediateForm(MI, RegsToUpdate&: RRToRIRegsToUpdate))
475 continue;
476 for (Register R : RRToRIRegsToUpdate)
477 addRegToUpdate(R);
478 // The updated instruction may now have new register operands.
479 // Conservatively add them to recompute the flags as well.
480 for (const MachineOperand &MO : MI.operands())
481 if (MO.isReg())
482 addRegToUpdate(MO.getReg());
483 // We don't erase anything in case the def has other uses. Let DCE
484 // remove it if it can be removed.
485 LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
486 LLVM_DEBUG(MI.dump());
487 NumConvertedToImmediateForm++;
488 SomethingChanged = true;
489 Simplified = true;
490 }
491 }
492 } while (SomethingChanged && FixedPointRegToImm);
493 }
494
495 // Since we are deleting this instruction, clear the kill flags on any of its
496 // definitions that are marked as needing an update: the transforms only ever
497 // invalidate kill flags by removing uses (turning a non-last use into the
498 // last one), so a conservative clear is sufficient.
499 auto clearKillsForDyingInstr = [&]() {
500 if (RegsToUpdate.empty())
501 return;
502 for (MachineOperand &MO : ToErase->operands()) {
503 if (!MO.isReg() || !MO.isDef() || !RegsToUpdate.count(V: MO.getReg()))
504 continue;
505 Register RegToUpdate = MO.getReg();
506 RegsToUpdate.erase(V: RegToUpdate);
507 // If some transformation has introduced an additional definition of
508 // this register (breaking SSA), we can safely convert this def to
509 // a def of an invalid register as the instruction is going away.
510 if (!MRI->getUniqueVRegDef(Reg: RegToUpdate))
511 MO.setReg(PPC::NoRegister);
512 for (MachineOperand &Def : MRI->def_operands(Reg: RegToUpdate))
513 Def.setIsDead(false);
514 }
515 };
516
517 for (MachineBasicBlock &MBB : *MF) {
518 for (MachineInstr &MI : MBB) {
519
520 // If the previous instruction was marked for elimination,
521 // remove it now.
522 if (ToErase) {
523 LLVM_DEBUG(dbgs() << "Deleting instruction: ");
524 LLVM_DEBUG(ToErase->dump());
525 clearKillsForDyingInstr();
526 ToErase->eraseFromParent();
527 ToErase = nullptr;
528 }
529 // If a conditional trap instruction got optimized to an
530 // unconditional trap, eliminate all the instructions after
531 // the trap.
532 if (EnableTrapOptimization && TrapOpt) {
533 ToErase = &MI;
534 continue;
535 }
536
537 // Ignore debug instructions.
538 if (MI.isDebugInstr())
539 continue;
540
541 if (!DebugCounter::shouldExecute(Counter&: PeepholePerOpCounter))
542 continue;
543
544 // Per-opcode peepholes.
545 switch (MI.getOpcode()) {
546
547 default:
548 break;
549 case PPC::COPY: {
550 Register Src = MI.getOperand(i: 1).getReg();
551 Register Dst = MI.getOperand(i: 0).getReg();
552 if (!Src.isVirtual() || !Dst.isVirtual())
553 break;
554 if (MRI->getRegClass(Reg: Src) != &PPC::UACCRCRegClass ||
555 MRI->getRegClass(Reg: Dst) != &PPC::ACCRCRegClass)
556 break;
557
558 // We are copying an unprimed accumulator to a primed accumulator.
559 // If the input to the copy is a PHI that is fed only by (i) copies in
560 // the other direction (ii) implicitly defined unprimed accumulators or
561 // (iii) other PHI nodes satisfying (i) and (ii), we can change
562 // the PHI to a PHI on primed accumulators (as long as we also change
563 // its operands). To detect and change such copies, we first get a list
564 // of all the PHI nodes starting from the root PHI node in BFS order.
565 // We then visit all these PHI nodes to check if they can be changed to
566 // primed accumulator PHI nodes and if so, we change them.
567 MachineInstr *RootPHI = MRI->getVRegDef(Reg: Src);
568 if (RootPHI->getOpcode() != PPC::PHI)
569 break;
570
571 SmallVector<MachineInstr *, 4> PHIs;
572 if (!collectUnprimedAccPHIs(MRI, RootPHI, PHIs))
573 break;
574
575 convertUnprimedAccPHIs(TII, MRI, PHIs, Dst);
576
577 ToErase = &MI;
578 break;
579 }
580 case PPC::LI:
581 case PPC::LI8: {
582 // If we are materializing a zero, look for any use operands for which
583 // zero means immediate zero. All such operands can be replaced with
584 // PPC::ZERO.
585 if (!MI.getOperand(i: 1).isImm() || MI.getOperand(i: 1).getImm() != 0)
586 break;
587 Register MIDestReg = MI.getOperand(i: 0).getReg();
588 bool Folded = false;
589 for (MachineInstr& UseMI : MRI->use_instructions(Reg: MIDestReg))
590 Folded |= TII->onlyFoldImmediate(UseMI, DefMI&: MI, Reg: MIDestReg);
591 if (MRI->use_nodbg_empty(RegNo: MIDestReg)) {
592 ++NumLoadImmZeroFoldedAndRemoved;
593 ToErase = &MI;
594 }
595 if (Folded)
596 addRegToUpdate(MIDestReg);
597 Simplified |= Folded;
598 break;
599 }
600 case PPC::STW:
601 case PPC::STD: {
602 MachineFrameInfo &MFI = MF->getFrameInfo();
603 if (MFI.hasVarSizedObjects() ||
604 (!MF->getSubtarget<PPCSubtarget>().isELFv2ABI() &&
605 !MF->getSubtarget<PPCSubtarget>().isAIXABI()))
606 break;
607 // When encountering a TOC save instruction, call UpdateTOCSaves
608 // to add it to the TOCSaves map and mark any existing TOC saves
609 // it dominates as redundant.
610 if (TII->isTOCSaveMI(MI))
611 UpdateTOCSaves(TOCSaves, MI: &MI);
612 break;
613 }
614 case PPC::XXPERMDI: {
615 // Perform simplifications of 2x64 vector swaps and splats.
616 // A swap is identified by an immediate value of 2, and a splat
617 // is identified by an immediate value of 0 or 3.
618 int Immed = MI.getOperand(i: 3).getImm();
619
620 if (Immed == 1)
621 break;
622
623 // For each of these simplifications, we need the two source
624 // regs to match. Unfortunately, MachineCSE ignores COPY and
625 // SUBREG_TO_REG, so for example we can see
626 // XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), immed.
627 // We have to look through chains of COPY and SUBREG_TO_REG
628 // to find the real source values for comparison.
629 Register TrueReg1 =
630 TRI->lookThruCopyLike(SrcReg: MI.getOperand(i: 1).getReg(), MRI);
631 Register TrueReg2 =
632 TRI->lookThruCopyLike(SrcReg: MI.getOperand(i: 2).getReg(), MRI);
633
634 if (!(TrueReg1 == TrueReg2 && TrueReg1.isVirtual()))
635 break;
636
637 MachineInstr *DefMI = MRI->getVRegDef(Reg: TrueReg1);
638
639 if (!DefMI)
640 break;
641
642 unsigned DefOpc = DefMI->getOpcode();
643
644 // If this is a splat fed by a splatting load, the splat is
645 // redundant. Replace with a copy. This doesn't happen directly due
646 // to code in PPCDAGToDAGISel.cpp, but it can happen when converting
647 // a load of a double to a vector of 64-bit integers.
648 auto isConversionOfLoadAndSplat = [=]() -> bool {
649 if (DefOpc != PPC::XVCVDPSXDS && DefOpc != PPC::XVCVDPUXDS)
650 return false;
651 Register FeedReg1 =
652 TRI->lookThruCopyLike(SrcReg: DefMI->getOperand(i: 1).getReg(), MRI);
653 if (FeedReg1.isVirtual()) {
654 MachineInstr *LoadMI = MRI->getVRegDef(Reg: FeedReg1);
655 if (LoadMI && LoadMI->getOpcode() == PPC::LXVDSX)
656 return true;
657 }
658 return false;
659 };
660 if ((Immed == 0 || Immed == 3) &&
661 (DefOpc == PPC::LXVDSX || isConversionOfLoadAndSplat())) {
662 LLVM_DEBUG(dbgs() << "Optimizing load-and-splat/splat "
663 "to load-and-splat/copy: ");
664 LLVM_DEBUG(MI.dump());
665 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
666 DestReg: MI.getOperand(i: 0).getReg())
667 .add(MO: MI.getOperand(i: 1));
668 addRegToUpdate(MI.getOperand(1).getReg());
669 ToErase = &MI;
670 Simplified = true;
671 }
672
673 // If this is a splat or a swap fed by another splat, we
674 // can replace it with a copy.
675 if (DefOpc == PPC::XXPERMDI) {
676 Register DefReg1 = DefMI->getOperand(i: 1).getReg();
677 Register DefReg2 = DefMI->getOperand(i: 2).getReg();
678 unsigned DefImmed = DefMI->getOperand(i: 3).getImm();
679
680 // If the two inputs are not the same register, check to see if
681 // they originate from the same virtual register after only
682 // copy-like instructions.
683 if (DefReg1 != DefReg2) {
684 Register FeedReg1 = TRI->lookThruCopyLike(SrcReg: DefReg1, MRI);
685 Register FeedReg2 = TRI->lookThruCopyLike(SrcReg: DefReg2, MRI);
686
687 if (!(FeedReg1 == FeedReg2 && FeedReg1.isVirtual()))
688 break;
689 }
690
691 if (DefImmed == 0 || DefImmed == 3) {
692 LLVM_DEBUG(dbgs() << "Optimizing splat/swap or splat/splat "
693 "to splat/copy: ");
694 LLVM_DEBUG(MI.dump());
695 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
696 DestReg: MI.getOperand(i: 0).getReg())
697 .add(MO: MI.getOperand(i: 1));
698 addRegToUpdate(MI.getOperand(1).getReg());
699 ToErase = &MI;
700 Simplified = true;
701 }
702
703 // If this is a splat fed by a swap, we can simplify modify
704 // the splat to splat the other value from the swap's input
705 // parameter.
706 else if ((Immed == 0 || Immed == 3) && DefImmed == 2) {
707 LLVM_DEBUG(dbgs() << "Optimizing swap/splat => splat: ");
708 LLVM_DEBUG(MI.dump());
709 addRegToUpdate(MI.getOperand(1).getReg());
710 addRegToUpdate(MI.getOperand(2).getReg());
711 MI.getOperand(i: 1).setReg(DefReg1);
712 MI.getOperand(i: 2).setReg(DefReg2);
713 MI.getOperand(i: 3).setImm(3 - Immed);
714 addRegToUpdate(DefReg1);
715 addRegToUpdate(DefReg2);
716 Simplified = true;
717 }
718
719 // If this is a swap fed by a swap, we can replace it
720 // with a copy from the first swap's input.
721 else if (Immed == 2 && DefImmed == 2) {
722 LLVM_DEBUG(dbgs() << "Optimizing swap/swap => copy: ");
723 LLVM_DEBUG(MI.dump());
724 addRegToUpdate(MI.getOperand(1).getReg());
725
726 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
727 DestReg: MI.getOperand(i: 0).getReg())
728 .add(MO: DefMI->getOperand(i: 1));
729 addRegToUpdate(DefMI->getOperand(0).getReg());
730 addRegToUpdate(DefMI->getOperand(1).getReg());
731 ToErase = &MI;
732 Simplified = true;
733 }
734 } else if ((Immed == 0 || Immed == 3 || Immed == 2) &&
735 DefOpc == PPC::XXPERMDIs &&
736 (DefMI->getOperand(i: 2).getImm() == 0 ||
737 DefMI->getOperand(i: 2).getImm() == 3)) {
738
739 if (!MRI->hasOneNonDBGUser(RegNo: DefMI->getOperand(i: 0).getReg()))
740 break;
741 Simplified = true;
742 // Swap of a splat, convert to copy.
743 if (Immed == 2) {
744 LLVM_DEBUG(dbgs() << "Optimizing swap(splat) => copy(splat): ");
745 LLVM_DEBUG(MI.dump());
746 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
747 DestReg: MI.getOperand(i: 0).getReg())
748 .add(MO: MI.getOperand(i: 1));
749 addRegToUpdate(MI.getOperand(1).getReg());
750 ToErase = &MI;
751 break;
752 }
753 // Splat fed by another splat - switch the output of the first
754 // and remove the second.
755 ToErase = &MI;
756 DefMI->getOperand(i: 0).setReg(MI.getOperand(i: 0).getReg());
757 LLVM_DEBUG(dbgs() << "Removing redundant splat: ");
758 LLVM_DEBUG(MI.dump());
759 } else if (Immed == 2 &&
760 (DefOpc == PPC::VSPLTB || DefOpc == PPC::VSPLTH ||
761 DefOpc == PPC::VSPLTW || DefOpc == PPC::XXSPLTW ||
762 DefOpc == PPC::VSPLTISB || DefOpc == PPC::VSPLTISH ||
763 DefOpc == PPC::VSPLTISW)) {
764 // Swap of various vector splats, convert to copy.
765 ToErase = &MI;
766 Simplified = true;
767 LLVM_DEBUG(dbgs() << "Optimizing swap(vsplt(is)?[b|h|w]|xxspltw) => "
768 "copy(vsplt(is)?[b|h|w]|xxspltw): ");
769 LLVM_DEBUG(MI.dump());
770 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
771 DestReg: MI.getOperand(i: 0).getReg())
772 .add(MO: MI.getOperand(i: 1));
773 addRegToUpdate(MI.getOperand(1).getReg());
774 } else if ((Immed == 0 || Immed == 3 || Immed == 2) &&
775 TII->isLoadFromConstantPool(I: DefMI)) {
776 const Constant *C = TII->getConstantFromConstantPool(I: DefMI);
777 if (C && C->getType()->isVectorTy() && C->getSplatValue()) {
778 ToErase = &MI;
779 Simplified = true;
780 LLVM_DEBUG(dbgs()
781 << "Optimizing swap(splat pattern from constant-pool) "
782 "=> copy(splat pattern from constant-pool): ");
783 LLVM_DEBUG(MI.dump());
784 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
785 DestReg: MI.getOperand(i: 0).getReg())
786 .add(MO: MI.getOperand(i: 1));
787 addRegToUpdate(MI.getOperand(1).getReg());
788 }
789 }
790 break;
791 }
792 case PPC::VSPLTB:
793 case PPC::VSPLTH:
794 case PPC::XXSPLTW: {
795 unsigned MyOpcode = MI.getOpcode();
796 // The operand number of the source register in the splat instruction.
797 unsigned OpNo = MyOpcode == PPC::XXSPLTW ? 1 : 2;
798 Register TrueReg =
799 TRI->lookThruCopyLike(SrcReg: MI.getOperand(i: OpNo).getReg(), MRI);
800 if (!TrueReg.isVirtual())
801 break;
802 MachineInstr *DefMI = MRI->getVRegDef(Reg: TrueReg);
803 if (!DefMI)
804 break;
805 unsigned DefOpcode = DefMI->getOpcode();
806 auto isConvertOfSplat = [=]() -> bool {
807 if (DefOpcode != PPC::XVCVSPSXWS && DefOpcode != PPC::XVCVSPUXWS)
808 return false;
809 Register ConvReg = DefMI->getOperand(i: 1).getReg();
810 if (!ConvReg.isVirtual())
811 return false;
812 MachineInstr *Splt = MRI->getVRegDef(Reg: ConvReg);
813 return Splt && (Splt->getOpcode() == PPC::LXVWSX ||
814 Splt->getOpcode() == PPC::XXSPLTW);
815 };
816 bool AlreadySplat = (MyOpcode == DefOpcode) ||
817 (MyOpcode == PPC::VSPLTB && DefOpcode == PPC::VSPLTBs) ||
818 (MyOpcode == PPC::VSPLTH && DefOpcode == PPC::VSPLTHs) ||
819 (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::XXSPLTWs) ||
820 (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::LXVWSX) ||
821 (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::MTVSRWS)||
822 (MyOpcode == PPC::XXSPLTW && isConvertOfSplat());
823
824 // If the instruction[s] that feed this splat have already splat
825 // the value, this splat is redundant.
826 if (AlreadySplat) {
827 LLVM_DEBUG(dbgs() << "Changing redundant splat to a copy: ");
828 LLVM_DEBUG(MI.dump());
829 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
830 DestReg: MI.getOperand(i: 0).getReg())
831 .add(MO: MI.getOperand(i: OpNo));
832 addRegToUpdate(MI.getOperand(OpNo).getReg());
833 ToErase = &MI;
834 Simplified = true;
835 }
836
837 // Splat fed by a shift. Usually when we align value to splat into
838 // vector element zero.
839 if (DefOpcode == PPC::XXSLDWI) {
840 Register ShiftOp1 = DefMI->getOperand(i: 1).getReg();
841
842 if (ShiftOp1 == DefMI->getOperand(i: 2).getReg()) {
843 // For example, We can erase XXSLDWI from in following:
844 // %2:vrrc = XXSLDWI killed %1:vrrc, %1:vrrc, 1
845 // %6:vrrc = VSPLTB 15, killed %2:vrrc
846 // %7:vsrc = XXLAND killed %6:vrrc, killed %1:vrrc
847 //
848 // --->
849 //
850 // %6:vrrc = VSPLTB 3, killed %1:vrrc
851 // %7:vsrc = XXLAND killed %6:vrrc, killed %1:vrrc
852
853 if (MRI->hasOneNonDBGUse(RegNo: DefMI->getOperand(i: 0).getReg())) {
854 LLVM_DEBUG(dbgs() << "Removing redundant shift: ");
855 LLVM_DEBUG(DefMI->dump());
856 ToErase = DefMI;
857 }
858 Simplified = true;
859 unsigned ShiftImm = DefMI->getOperand(i: 3).getImm();
860 // The operand number of the splat Imm in the instruction.
861 unsigned SplatImmNo = MyOpcode == PPC::XXSPLTW ? 2 : 1;
862 unsigned SplatImm = MI.getOperand(i: SplatImmNo).getImm();
863
864 // Calculate the new splat-element immediate. We need to convert the
865 // element index into the proper unit (byte for VSPLTB, halfword for
866 // VSPLTH, word for VSPLTW) because PPC::XXSLDWI interprets its
867 // ShiftImm in 32-bit word units.
868 auto CalculateNewElementIdx = [&](unsigned Opcode) {
869 if (Opcode == PPC::VSPLTB)
870 return (SplatImm + ShiftImm * 4) & 0xF;
871 else if (Opcode == PPC::VSPLTH)
872 return (SplatImm + ShiftImm * 2) & 0x7;
873 else
874 return (SplatImm + ShiftImm) & 0x3;
875 };
876
877 unsigned NewElem = CalculateNewElementIdx(MyOpcode);
878
879 LLVM_DEBUG(dbgs() << "Changing splat immediate from " << SplatImm
880 << " to " << NewElem << " in instruction: ");
881 LLVM_DEBUG(MI.dump());
882 if (!MRI->constrainRegClass(Reg: ShiftOp1, RC: &PPC::VRRCRegClass))
883 llvm_unreachable("Can't fail because vrrc is subset of vsrc");
884 addRegToUpdate(MI.getOperand(OpNo).getReg());
885 addRegToUpdate(ShiftOp1);
886 MI.getOperand(i: OpNo).setReg(ShiftOp1);
887 MI.getOperand(i: SplatImmNo).setImm(NewElem);
888 }
889 }
890 break;
891 }
892 case PPC::XVCVDPSP: {
893 // If this is a DP->SP conversion fed by an FRSP, the FRSP is redundant.
894 Register TrueReg =
895 TRI->lookThruCopyLike(SrcReg: MI.getOperand(i: 1).getReg(), MRI);
896 if (!TrueReg.isVirtual())
897 break;
898 MachineInstr *DefMI = MRI->getVRegDef(Reg: TrueReg);
899
900 // This can occur when building a vector of single precision or integer
901 // values.
902 if (DefMI && DefMI->getOpcode() == PPC::XXPERMDI) {
903 Register DefsReg1 =
904 TRI->lookThruCopyLike(SrcReg: DefMI->getOperand(i: 1).getReg(), MRI);
905 Register DefsReg2 =
906 TRI->lookThruCopyLike(SrcReg: DefMI->getOperand(i: 2).getReg(), MRI);
907 if (!DefsReg1.isVirtual() || !DefsReg2.isVirtual())
908 break;
909 MachineInstr *P1 = MRI->getVRegDef(Reg: DefsReg1);
910 MachineInstr *P2 = MRI->getVRegDef(Reg: DefsReg2);
911
912 if (!P1 || !P2)
913 break;
914
915 // Remove the passed FRSP/XSRSP instruction if it only feeds this MI
916 // and set any uses of that FRSP/XSRSP (in this MI) to the source of
917 // the FRSP/XSRSP.
918 auto removeFRSPIfPossible = [&](MachineInstr *RoundInstr) {
919 unsigned Opc = RoundInstr->getOpcode();
920 if ((Opc == PPC::FRSP || Opc == PPC::XSRSP) &&
921 MRI->hasOneNonDBGUse(RegNo: RoundInstr->getOperand(i: 0).getReg())) {
922 Simplified = true;
923 Register ConvReg1 = RoundInstr->getOperand(i: 1).getReg();
924 Register FRSPDefines = RoundInstr->getOperand(i: 0).getReg();
925 MachineInstr &Use = *(MRI->use_instr_nodbg_begin(RegNo: FRSPDefines));
926 for (int i = 0, e = Use.getNumOperands(); i < e; ++i)
927 if (Use.getOperand(i).isReg() &&
928 Use.getOperand(i).getReg() == FRSPDefines)
929 Use.getOperand(i).setReg(ConvReg1);
930 LLVM_DEBUG(dbgs() << "Removing redundant FRSP/XSRSP:\n");
931 LLVM_DEBUG(RoundInstr->dump());
932 LLVM_DEBUG(dbgs() << "As it feeds instruction:\n");
933 LLVM_DEBUG(MI.dump());
934 LLVM_DEBUG(dbgs() << "Through instruction:\n");
935 LLVM_DEBUG(DefMI->dump());
936 addRegToUpdate(ConvReg1);
937 addRegToUpdate(FRSPDefines);
938 ToErase = RoundInstr;
939 }
940 };
941
942 // If the input to XVCVDPSP is a vector that was built (even
943 // partially) out of FRSP's, the FRSP(s) can safely be removed
944 // since this instruction performs the same operation.
945 if (P1 != P2) {
946 removeFRSPIfPossible(P1);
947 removeFRSPIfPossible(P2);
948 break;
949 }
950 removeFRSPIfPossible(P1);
951 }
952 break;
953 }
954 case PPC::EXTSH:
955 case PPC::EXTSH8:
956 case PPC::EXTSH8_32_64: {
957 if (!EnableSExtElimination) break;
958 Register NarrowReg = MI.getOperand(i: 1).getReg();
959 if (!NarrowReg.isVirtual())
960 break;
961
962 MachineInstr *SrcMI = MRI->getVRegDef(Reg: NarrowReg);
963 unsigned SrcOpcode = SrcMI->getOpcode();
964 // If we've used a zero-extending load that we will sign-extend,
965 // just do a sign-extending load.
966 if (SrcOpcode == PPC::LHZ || SrcOpcode == PPC::LHZX) {
967 if (!MRI->hasOneNonDBGUse(RegNo: SrcMI->getOperand(i: 0).getReg()))
968 break;
969 // Determine the new opcode. We need to make sure that if the original
970 // instruction has a 64 bit opcode we keep using a 64 bit opcode.
971 // Likewise if the source is X-Form the new opcode should also be
972 // X-Form.
973 unsigned Opc = PPC::LHA;
974 bool SourceIsXForm = SrcOpcode == PPC::LHZX;
975 bool MIIs64Bit = MI.getOpcode() == PPC::EXTSH8 ||
976 MI.getOpcode() == PPC::EXTSH8_32_64;
977
978 if (SourceIsXForm && MIIs64Bit)
979 Opc = PPC::LHAX8;
980 else if (SourceIsXForm && !MIIs64Bit)
981 Opc = PPC::LHAX;
982 else if (MIIs64Bit)
983 Opc = PPC::LHA8;
984
985 addRegToUpdate(NarrowReg);
986 addRegToUpdate(MI.getOperand(0).getReg());
987
988 // We are removing a definition of NarrowReg which will cause
989 // problems in AliveBlocks. Add an implicit def that will be
990 // removed so that AliveBlocks are updated correctly.
991 addDummyDef(MBB, At: &MI, Reg: NarrowReg);
992 LLVM_DEBUG(dbgs() << "Zero-extending load\n");
993 LLVM_DEBUG(SrcMI->dump());
994 LLVM_DEBUG(dbgs() << "and sign-extension\n");
995 LLVM_DEBUG(MI.dump());
996 LLVM_DEBUG(dbgs() << "are merged into sign-extending load\n");
997 SrcMI->setDesc(TII->get(Opcode: Opc));
998 SrcMI->getOperand(i: 0).setReg(MI.getOperand(i: 0).getReg());
999 ToErase = &MI;
1000 Simplified = true;
1001 NumEliminatedSExt++;
1002 }
1003 break;
1004 }
1005 case PPC::EXTSW:
1006 case PPC::EXTSW_32:
1007 case PPC::EXTSW_32_64: {
1008 if (!EnableSExtElimination) break;
1009 Register NarrowReg = MI.getOperand(i: 1).getReg();
1010 if (!NarrowReg.isVirtual())
1011 break;
1012
1013 MachineInstr *SrcMI = MRI->getVRegDef(Reg: NarrowReg);
1014 unsigned SrcOpcode = SrcMI->getOpcode();
1015 // If we've used a zero-extending load that we will sign-extend,
1016 // just do a sign-extending load. The source may be a 32-bit gprc load
1017 // consumed directly, or a 64-bit g8rc load consumed through its sub_32
1018 // subregister.
1019 bool SrcIsGPRCWordZextLoad =
1020 SrcOpcode == PPC::LWZ || SrcOpcode == PPC::LWZX;
1021 bool SrcIsG8RCWordZextLoad =
1022 SrcOpcode == PPC::LWZ8 || SrcOpcode == PPC::LWZX8;
1023 unsigned NarrowSubReg = MI.getOperand(i: 1).getSubReg();
1024
1025 if ((!NarrowSubReg && SrcIsGPRCWordZextLoad) ||
1026 (NarrowSubReg == PPC::sub_32 && SrcIsG8RCWordZextLoad)) {
1027 if (!MRI->hasOneNonDBGUse(RegNo: SrcMI->getOperand(i: 0).getReg()))
1028 break;
1029
1030 // The transformation from a zero-extending load to a sign-extending
1031 // load is only legal when the displacement is a multiple of 4.
1032 // If the displacement is not at least 4 byte aligned, don't perform
1033 // the transformation.
1034 bool IsWordAligned = false;
1035 if (SrcMI->getOperand(i: 1).isGlobal()) {
1036 const GlobalVariable *GV =
1037 dyn_cast<GlobalVariable>(Val: SrcMI->getOperand(i: 1).getGlobal());
1038 if (GV && GV->getAlign() && *GV->getAlign() >= 4 &&
1039 (SrcMI->getOperand(i: 1).getOffset() % 4 == 0))
1040 IsWordAligned = true;
1041 } else if (SrcMI->getOperand(i: 1).isImm()) {
1042 int64_t Value = SrcMI->getOperand(i: 1).getImm();
1043 if (Value % 4 == 0)
1044 IsWordAligned = true;
1045 }
1046
1047 // Determine the new opcode. We need to make sure that if the original
1048 // instruction has a 64 bit opcode we keep using a 64 bit opcode.
1049 // Likewise if the source is X-Form the new opcode should also be
1050 // X-Form.
1051 unsigned Opc = PPC::LWA_32;
1052 bool SourceIsXForm =
1053 SrcOpcode == PPC::LWZX || SrcOpcode == PPC::LWZX8;
1054 bool MIIs64Bit = MI.getOpcode() == PPC::EXTSW ||
1055 MI.getOpcode() == PPC::EXTSW_32_64;
1056
1057 if (SourceIsXForm && MIIs64Bit)
1058 Opc = PPC::LWAX;
1059 else if (SourceIsXForm && !MIIs64Bit)
1060 Opc = PPC::LWAX_32;
1061 else if (MIIs64Bit)
1062 Opc = PPC::LWA;
1063
1064 if (!IsWordAligned && (Opc == PPC::LWA || Opc == PPC::LWA_32))
1065 break;
1066
1067 addRegToUpdate(NarrowReg);
1068 addRegToUpdate(MI.getOperand(0).getReg());
1069
1070 // We are removing a definition of NarrowReg which will cause
1071 // problems in AliveBlocks. Add an implicit def that will be
1072 // removed so that AliveBlocks are updated correctly.
1073 addDummyDef(MBB, At: &MI, Reg: NarrowReg);
1074 LLVM_DEBUG(dbgs() << "Zero-extending load\n");
1075 LLVM_DEBUG(SrcMI->dump());
1076 LLVM_DEBUG(dbgs() << "and sign-extension\n");
1077 LLVM_DEBUG(MI.dump());
1078 LLVM_DEBUG(dbgs() << "are merged into sign-extending load\n");
1079 SrcMI->setDesc(TII->get(Opcode: Opc));
1080 SrcMI->getOperand(i: 0).setReg(MI.getOperand(i: 0).getReg());
1081 ToErase = &MI;
1082 Simplified = true;
1083 NumEliminatedSExt++;
1084 } else if (MI.getOpcode() == PPC::EXTSW_32_64 &&
1085 TII->isSignExtended(Reg: NarrowReg, MRI)) {
1086 // We can eliminate EXTSW if the input is known to be already
1087 // sign-extended. However, we are not sure whether a spill will occur
1088 // during register allocation. If there is no promotion, it will use
1089 // 'stw' instead of 'std', and 'lwz' instead of 'ld' when spilling,
1090 // since the register class is 32-bits. Consequently, the high 32-bit
1091 // information will be lost. Therefore, all these instructions in the
1092 // chain used to deduce sign extension to eliminate the 'extsw' will
1093 // need to be promoted to 64-bit pseudo instructions when the 'extsw'
1094 // is eliminated.
1095 TII->promoteInstr32To64ForElimEXTSW(Reg: NarrowReg, MRI, BinOpDepth: 0);
1096
1097 LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n");
1098
1099 unsigned NarrowSubReg = MI.getOperand(i: 1).getSubReg();
1100 if (NarrowSubReg) {
1101 // The input reads a subregister of a wider register which we can
1102 // use directly by dropping the subreg.
1103 assert(NarrowSubReg == PPC::sub_32 &&
1104 "EXTSW_32_64 input must read the sub_32 subregister");
1105 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
1106 DestReg: MI.getOperand(i: 0).getReg())
1107 .addReg(RegNo: NarrowReg);
1108 } else {
1109 Register TmpReg =
1110 MF->getRegInfo().createVirtualRegister(RegClass: &PPC::G8RCRegClass);
1111 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::IMPLICIT_DEF),
1112 DestReg: TmpReg);
1113 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::INSERT_SUBREG),
1114 DestReg: MI.getOperand(i: 0).getReg())
1115 .addReg(RegNo: TmpReg)
1116 .addReg(RegNo: NarrowReg)
1117 .addImm(Val: PPC::sub_32);
1118 addRegToUpdate(NarrowReg);
1119 }
1120 ToErase = &MI;
1121 Simplified = true;
1122 NumEliminatedSExt++;
1123 }
1124 break;
1125 }
1126 case PPC::RLDICL: {
1127 // We can eliminate RLDICL (e.g. for zero-extension)
1128 // if all bits to clear are already zero in the input.
1129 // This code assume following code sequence for zero-extension.
1130 // %6 = COPY %5:sub_32; (optional)
1131 // %8 = IMPLICIT_DEF;
1132 // %7<def,tied1> = INSERT_SUBREG %8<tied0>, %6, sub_32;
1133 if (!EnableZExtElimination) break;
1134
1135 if (MI.getOperand(i: 2).getImm() != 0)
1136 break;
1137
1138 Register SrcReg = MI.getOperand(i: 1).getReg();
1139 if (!SrcReg.isVirtual())
1140 break;
1141
1142 MachineInstr *SrcMI = MRI->getVRegDef(Reg: SrcReg);
1143 if (!(SrcMI && SrcMI->getOpcode() == PPC::INSERT_SUBREG &&
1144 SrcMI->getOperand(i: 0).isReg() && SrcMI->getOperand(i: 1).isReg()))
1145 break;
1146
1147 MachineInstr *ImpDefMI, *SubRegMI;
1148 ImpDefMI = MRI->getVRegDef(Reg: SrcMI->getOperand(i: 1).getReg());
1149 SubRegMI = MRI->getVRegDef(Reg: SrcMI->getOperand(i: 2).getReg());
1150 if (ImpDefMI->getOpcode() != PPC::IMPLICIT_DEF) break;
1151
1152 SrcMI = SubRegMI;
1153 if (SubRegMI->getOpcode() == PPC::COPY) {
1154 Register CopyReg = SubRegMI->getOperand(i: 1).getReg();
1155 if (CopyReg.isVirtual())
1156 SrcMI = MRI->getVRegDef(Reg: CopyReg);
1157 }
1158 if (!SrcMI->getOperand(i: 0).isReg())
1159 break;
1160
1161 unsigned KnownZeroCount =
1162 getKnownLeadingZeroCount(Reg: SrcMI->getOperand(i: 0).getReg(), TII, MRI);
1163 if (MI.getOperand(i: 3).getImm() <= KnownZeroCount) {
1164 LLVM_DEBUG(dbgs() << "Removing redundant zero-extension\n");
1165 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
1166 DestReg: MI.getOperand(i: 0).getReg())
1167 .addReg(RegNo: SrcReg);
1168 addRegToUpdate(SrcReg);
1169 ToErase = &MI;
1170 Simplified = true;
1171 NumEliminatedZExt++;
1172 }
1173 break;
1174 }
1175
1176 // TODO: Any instruction that has an immediate form fed only by a PHI
1177 // whose operands are all load immediate can be folded away. We currently
1178 // do this for ADD instructions, but should expand it to arithmetic and
1179 // binary instructions with immediate forms in the future.
1180 case PPC::ADD4:
1181 case PPC::ADD8: {
1182 auto isSingleUsePHI = [&](MachineOperand *PhiOp) {
1183 assert(PhiOp && "Invalid Operand!");
1184 MachineInstr *DefPhiMI = getVRegDefOrNull(Op: PhiOp, MRI);
1185
1186 return DefPhiMI && (DefPhiMI->getOpcode() == PPC::PHI) &&
1187 MRI->hasOneNonDBGUse(RegNo: DefPhiMI->getOperand(i: 0).getReg());
1188 };
1189
1190 auto dominatesAllSingleUseLIs = [&](MachineOperand *DominatorOp,
1191 MachineOperand *PhiOp) {
1192 assert(PhiOp && "Invalid Operand!");
1193 assert(DominatorOp && "Invalid Operand!");
1194 MachineInstr *DefPhiMI = getVRegDefOrNull(Op: PhiOp, MRI);
1195 MachineInstr *DefDomMI = getVRegDefOrNull(Op: DominatorOp, MRI);
1196
1197 // Note: the vregs only show up at odd indices position of PHI Node,
1198 // the even indices position save the BB info.
1199 for (unsigned i = 1; i < DefPhiMI->getNumOperands(); i += 2) {
1200 MachineInstr *LiMI =
1201 getVRegDefOrNull(Op: &DefPhiMI->getOperand(i), MRI);
1202 if (!LiMI ||
1203 (LiMI->getOpcode() != PPC::LI && LiMI->getOpcode() != PPC::LI8)
1204 || !MRI->hasOneNonDBGUse(RegNo: LiMI->getOperand(i: 0).getReg()) ||
1205 !MDT->dominates(A: DefDomMI, B: LiMI))
1206 return false;
1207 }
1208
1209 return true;
1210 };
1211
1212 MachineOperand Op1 = MI.getOperand(i: 1);
1213 MachineOperand Op2 = MI.getOperand(i: 2);
1214 if (isSingleUsePHI(&Op2) && dominatesAllSingleUseLIs(&Op1, &Op2))
1215 std::swap(a&: Op1, b&: Op2);
1216 else if (!isSingleUsePHI(&Op1) || !dominatesAllSingleUseLIs(&Op2, &Op1))
1217 break; // We don't have an ADD fed by LI's that can be transformed
1218
1219 // Now we know that Op1 is the PHI node and Op2 is the dominator
1220 Register DominatorReg = Op2.getReg();
1221
1222 const TargetRegisterClass *TRC = MI.getOpcode() == PPC::ADD8
1223 ? &PPC::G8RC_and_G8RC_NOX0RegClass
1224 : &PPC::GPRC_and_GPRC_NOR0RegClass;
1225 MRI->setRegClass(Reg: DominatorReg, RC: TRC);
1226
1227 // replace LIs with ADDIs
1228 MachineInstr *DefPhiMI = getVRegDefOrNull(Op: &Op1, MRI);
1229 for (unsigned i = 1; i < DefPhiMI->getNumOperands(); i += 2) {
1230 MachineInstr *LiMI = getVRegDefOrNull(Op: &DefPhiMI->getOperand(i), MRI);
1231 LLVM_DEBUG(dbgs() << "Optimizing LI to ADDI: ");
1232 LLVM_DEBUG(LiMI->dump());
1233
1234 // There could be repeated registers in the PHI, e.g: %1 =
1235 // PHI %6, <%bb.2>, %8, <%bb.3>, %8, <%bb.6>; So if we've
1236 // already replaced the def instruction, skip.
1237 if (LiMI->getOpcode() == PPC::ADDI || LiMI->getOpcode() == PPC::ADDI8)
1238 continue;
1239
1240 assert((LiMI->getOpcode() == PPC::LI ||
1241 LiMI->getOpcode() == PPC::LI8) &&
1242 "Invalid Opcode!");
1243 auto LiImm = LiMI->getOperand(i: 1).getImm(); // save the imm of LI
1244 LiMI->removeOperand(OpNo: 1); // remove the imm of LI
1245 LiMI->setDesc(TII->get(Opcode: LiMI->getOpcode() == PPC::LI ? PPC::ADDI
1246 : PPC::ADDI8));
1247 MachineInstrBuilder(*LiMI->getParent()->getParent(), *LiMI)
1248 .addReg(RegNo: DominatorReg)
1249 .addImm(Val: LiImm); // restore the imm of LI
1250 LLVM_DEBUG(LiMI->dump());
1251 }
1252
1253 // Replace ADD with COPY
1254 LLVM_DEBUG(dbgs() << "Optimizing ADD to COPY: ");
1255 LLVM_DEBUG(MI.dump());
1256 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::COPY),
1257 DestReg: MI.getOperand(i: 0).getReg())
1258 .add(MO: Op1);
1259 addRegToUpdate(Op1.getReg());
1260 addRegToUpdate(Op2.getReg());
1261 ToErase = &MI;
1262 Simplified = true;
1263 NumOptADDLIs++;
1264 break;
1265 }
1266 case PPC::RLDICR: {
1267 Simplified |= emitRLDICWhenLoweringJumpTables(MI, ToErase) ||
1268 combineSEXTAndSHL(MI, ToErase);
1269 break;
1270 }
1271 case PPC::ANDI_rec:
1272 case PPC::ANDI8_rec:
1273 case PPC::ANDIS_rec:
1274 case PPC::ANDIS8_rec: {
1275 Register TrueReg =
1276 TRI->lookThruCopyLike(SrcReg: MI.getOperand(i: 1).getReg(), MRI);
1277 if (!TrueReg.isVirtual() || !MRI->hasOneNonDBGUse(RegNo: TrueReg))
1278 break;
1279
1280 MachineInstr *SrcMI = MRI->getVRegDef(Reg: TrueReg);
1281 if (!SrcMI)
1282 break;
1283
1284 unsigned SrcOpCode = SrcMI->getOpcode();
1285 if (SrcOpCode != PPC::RLDICL && SrcOpCode != PPC::RLDICR)
1286 break;
1287
1288 Register SrcReg, DstReg;
1289 SrcReg = SrcMI->getOperand(i: 1).getReg();
1290 DstReg = MI.getOperand(i: 1).getReg();
1291 const TargetRegisterClass *SrcRC = MRI->getRegClassOrNull(Reg: SrcReg);
1292 const TargetRegisterClass *DstRC = MRI->getRegClassOrNull(Reg: DstReg);
1293 if (DstRC != SrcRC)
1294 break;
1295
1296 uint64_t AndImm = MI.getOperand(i: 2).getImm();
1297 if (MI.getOpcode() == PPC::ANDIS_rec ||
1298 MI.getOpcode() == PPC::ANDIS8_rec)
1299 AndImm <<= 16;
1300 uint64_t LZeroAndImm = llvm::countl_zero<uint64_t>(Val: AndImm);
1301 uint64_t RZeroAndImm = llvm::countr_zero<uint64_t>(Val: AndImm);
1302 uint64_t ImmSrc = SrcMI->getOperand(i: 3).getImm();
1303
1304 // We can transfer `RLDICL/RLDICR + ANDI_rec/ANDIS_rec` to `ANDI_rec 0`
1305 // if all bits to AND are already zero in the input.
1306 bool PatternResultZero =
1307 (SrcOpCode == PPC::RLDICL && (RZeroAndImm + ImmSrc > 63)) ||
1308 (SrcOpCode == PPC::RLDICR && LZeroAndImm > ImmSrc);
1309
1310 // We can eliminate RLDICL/RLDICR if it's used to clear bits and all
1311 // bits cleared will be ANDed with 0 by ANDI_rec/ANDIS_rec.
1312 bool PatternRemoveRotate =
1313 SrcMI->getOperand(i: 2).getImm() == 0 &&
1314 ((SrcOpCode == PPC::RLDICL && LZeroAndImm >= ImmSrc) ||
1315 (SrcOpCode == PPC::RLDICR && (RZeroAndImm + ImmSrc > 63)));
1316
1317 if (!PatternResultZero && !PatternRemoveRotate)
1318 break;
1319
1320 LLVM_DEBUG(dbgs() << "Combining pair: ");
1321 LLVM_DEBUG(SrcMI->dump());
1322 LLVM_DEBUG(MI.dump());
1323 if (PatternResultZero)
1324 MI.getOperand(i: 2).setImm(0);
1325 MI.getOperand(i: 1).setReg(SrcMI->getOperand(i: 1).getReg());
1326 LLVM_DEBUG(dbgs() << "To: ");
1327 LLVM_DEBUG(MI.dump());
1328 addRegToUpdate(MI.getOperand(1).getReg());
1329 addRegToUpdate(SrcMI->getOperand(0).getReg());
1330 Simplified = true;
1331 break;
1332 }
1333 case PPC::RLWINM:
1334 case PPC::RLWINM_rec:
1335 case PPC::RLWINM8:
1336 case PPC::RLWINM8_rec: {
1337 // We might replace operand 1 of the instruction which will
1338 // require we recompute kill flags for it.
1339 Register OrigOp1Reg = MI.getOperand(i: 1).isReg()
1340 ? MI.getOperand(i: 1).getReg()
1341 : PPC::NoRegister;
1342 Simplified = TII->combineRLWINM(MI, ToErase: &ToErase);
1343 if (Simplified) {
1344 addRegToUpdate(OrigOp1Reg);
1345 if (MI.getOperand(i: 1).isReg())
1346 addRegToUpdate(MI.getOperand(1).getReg());
1347 if (ToErase && ToErase->getOperand(i: 1).isReg())
1348 for (auto UseReg : ToErase->explicit_uses())
1349 if (UseReg.isReg())
1350 addRegToUpdate(UseReg.getReg());
1351 ++NumRotatesCollapsed;
1352 }
1353 break;
1354 }
1355 // We will replace TD/TW/TDI/TWI with an unconditional trap if it will
1356 // always trap, we will delete the node if it will never trap.
1357 case PPC::TDI:
1358 case PPC::TWI:
1359 case PPC::TD:
1360 case PPC::TW: {
1361 if (!EnableTrapOptimization) break;
1362 MachineInstr *LiMI1 = getVRegDefOrNull(Op: &MI.getOperand(i: 1), MRI);
1363 MachineInstr *LiMI2 = getVRegDefOrNull(Op: &MI.getOperand(i: 2), MRI);
1364 bool IsOperand2Immediate = MI.getOperand(i: 2).isImm();
1365 // We can only do the optimization if we can get immediates
1366 // from both operands
1367 if (!(LiMI1 && (LiMI1->getOpcode() == PPC::LI ||
1368 LiMI1->getOpcode() == PPC::LI8)))
1369 break;
1370 if (!IsOperand2Immediate &&
1371 !(LiMI2 && (LiMI2->getOpcode() == PPC::LI ||
1372 LiMI2->getOpcode() == PPC::LI8)))
1373 break;
1374
1375 auto ImmOperand0 = MI.getOperand(i: 0).getImm();
1376 auto ImmOperand1 = LiMI1->getOperand(i: 1).getImm();
1377 auto ImmOperand2 = IsOperand2Immediate ? MI.getOperand(i: 2).getImm()
1378 : LiMI2->getOperand(i: 1).getImm();
1379
1380 // We will replace the MI with an unconditional trap if it will always
1381 // trap.
1382 if ((ImmOperand0 == 31) ||
1383 ((ImmOperand0 & 0x10) &&
1384 ((int64_t)ImmOperand1 < (int64_t)ImmOperand2)) ||
1385 ((ImmOperand0 & 0x8) &&
1386 ((int64_t)ImmOperand1 > (int64_t)ImmOperand2)) ||
1387 ((ImmOperand0 & 0x2) &&
1388 ((uint64_t)ImmOperand1 < (uint64_t)ImmOperand2)) ||
1389 ((ImmOperand0 & 0x1) &&
1390 ((uint64_t)ImmOperand1 > (uint64_t)ImmOperand2)) ||
1391 ((ImmOperand0 & 0x4) && (ImmOperand1 == ImmOperand2))) {
1392 BuildMI(BB&: MBB, I: &MI, MIMD: MI.getDebugLoc(), MCID: TII->get(Opcode: PPC::TRAP));
1393 TrapOpt = true;
1394 }
1395 // We will delete the MI if it will never trap.
1396 ToErase = &MI;
1397 Simplified = true;
1398 break;
1399 }
1400 }
1401 }
1402
1403 // If the last instruction was marked for elimination,
1404 // remove it now.
1405 if (ToErase) {
1406 clearKillsForDyingInstr();
1407 ToErase->eraseFromParent();
1408 ToErase = nullptr;
1409 }
1410 // Reset TrapOpt to false at the end of the basic block.
1411 if (EnableTrapOptimization)
1412 TrapOpt = false;
1413 }
1414
1415 // Eliminate all the TOC save instructions which are redundant.
1416 Simplified |= eliminateRedundantTOCSaves(TOCSaves);
1417 PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
1418 if (FI->mustSaveTOC())
1419 NumTOCSavesInPrologue++;
1420
1421 // We try to eliminate redundant compare instruction.
1422 Simplified |= eliminateRedundantCompare();
1423
1424 // If we have made any modifications and added any registers to the set of
1425 // registers whose liveness flags may now be stale, clear those flags. A
1426 // transform may remove a use (leaving a stale kill on an earlier use) or add
1427 // a use of a previously dead def (leaving a stale dead flag), so clear both
1428 // kinds conservatively.
1429 for (Register Reg : RegsToUpdate) {
1430 if (MRI->reg_empty(RegNo: Reg))
1431 continue;
1432 for (MachineOperand &Def : MRI->def_operands(Reg))
1433 Def.setIsDead(false);
1434 }
1435 return Simplified;
1436}
1437
1438// helper functions for eliminateRedundantCompare
1439static bool isEqOrNe(MachineInstr *BI) {
1440 PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(i: 0).getImm();
1441 unsigned PredCond = PPC::getPredicateCondition(Opcode: Pred);
1442 return (PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE);
1443}
1444
1445static bool isSupportedCmpOp(unsigned opCode) {
1446 return (opCode == PPC::CMPLD || opCode == PPC::CMPD ||
1447 opCode == PPC::CMPLW || opCode == PPC::CMPW ||
1448 opCode == PPC::CMPLDI || opCode == PPC::CMPDI ||
1449 opCode == PPC::CMPLWI || opCode == PPC::CMPWI);
1450}
1451
1452static bool is64bitCmpOp(unsigned opCode) {
1453 return (opCode == PPC::CMPLD || opCode == PPC::CMPD ||
1454 opCode == PPC::CMPLDI || opCode == PPC::CMPDI);
1455}
1456
1457static bool isSignedCmpOp(unsigned opCode) {
1458 return (opCode == PPC::CMPD || opCode == PPC::CMPW ||
1459 opCode == PPC::CMPDI || opCode == PPC::CMPWI);
1460}
1461
1462static unsigned getSignedCmpOpCode(unsigned opCode) {
1463 if (opCode == PPC::CMPLD) return PPC::CMPD;
1464 if (opCode == PPC::CMPLW) return PPC::CMPW;
1465 if (opCode == PPC::CMPLDI) return PPC::CMPDI;
1466 if (opCode == PPC::CMPLWI) return PPC::CMPWI;
1467 return opCode;
1468}
1469
1470// We can decrement immediate x in (GE x) by changing it to (GT x-1) or
1471// (LT x) to (LE x-1)
1472static unsigned getPredicateToDecImm(MachineInstr *BI, MachineInstr *CMPI) {
1473 uint64_t Imm = CMPI->getOperand(i: 2).getImm();
1474 bool SignedCmp = isSignedCmpOp(opCode: CMPI->getOpcode());
1475 if ((!SignedCmp && Imm == 0) || (SignedCmp && Imm == 0x8000))
1476 return 0;
1477
1478 PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(i: 0).getImm();
1479 unsigned PredCond = PPC::getPredicateCondition(Opcode: Pred);
1480 unsigned PredHint = PPC::getPredicateHint(Opcode: Pred);
1481 if (PredCond == PPC::PRED_GE)
1482 return PPC::getPredicate(Condition: PPC::PRED_GT, Hint: PredHint);
1483 if (PredCond == PPC::PRED_LT)
1484 return PPC::getPredicate(Condition: PPC::PRED_LE, Hint: PredHint);
1485
1486 return 0;
1487}
1488
1489// We can increment immediate x in (GT x) by changing it to (GE x+1) or
1490// (LE x) to (LT x+1)
1491static unsigned getPredicateToIncImm(MachineInstr *BI, MachineInstr *CMPI) {
1492 uint64_t Imm = CMPI->getOperand(i: 2).getImm();
1493 bool SignedCmp = isSignedCmpOp(opCode: CMPI->getOpcode());
1494 if ((!SignedCmp && Imm == 0xFFFF) || (SignedCmp && Imm == 0x7FFF))
1495 return 0;
1496
1497 PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(i: 0).getImm();
1498 unsigned PredCond = PPC::getPredicateCondition(Opcode: Pred);
1499 unsigned PredHint = PPC::getPredicateHint(Opcode: Pred);
1500 if (PredCond == PPC::PRED_GT)
1501 return PPC::getPredicate(Condition: PPC::PRED_GE, Hint: PredHint);
1502 if (PredCond == PPC::PRED_LE)
1503 return PPC::getPredicate(Condition: PPC::PRED_LT, Hint: PredHint);
1504
1505 return 0;
1506}
1507
1508// This takes a Phi node and returns a register value for the specified BB.
1509static unsigned getIncomingRegForBlock(MachineInstr *Phi,
1510 MachineBasicBlock *MBB) {
1511 for (unsigned I = 2, E = Phi->getNumOperands() + 1; I != E; I += 2) {
1512 MachineOperand &MO = Phi->getOperand(i: I);
1513 if (MO.getMBB() == MBB)
1514 return Phi->getOperand(i: I-1).getReg();
1515 }
1516 llvm_unreachable("invalid src basic block for this Phi node\n");
1517 return 0;
1518}
1519
1520// This function tracks the source of the register through register copy.
1521// If BB1 and BB2 are non-NULL, we also track PHI instruction in BB2
1522// assuming that the control comes from BB1 into BB2.
1523static unsigned getSrcVReg(unsigned Reg, MachineBasicBlock *BB1,
1524 MachineBasicBlock *BB2, MachineRegisterInfo *MRI) {
1525 unsigned SrcReg = Reg;
1526 while (true) {
1527 unsigned NextReg = SrcReg;
1528 MachineInstr *Inst = MRI->getVRegDef(Reg: SrcReg);
1529 if (BB1 && Inst->getOpcode() == PPC::PHI && Inst->getParent() == BB2) {
1530 NextReg = getIncomingRegForBlock(Phi: Inst, MBB: BB1);
1531 // We track through PHI only once to avoid infinite loop.
1532 BB1 = nullptr;
1533 }
1534 else if (Inst->isFullCopy())
1535 NextReg = Inst->getOperand(i: 1).getReg();
1536 if (NextReg == SrcReg || !Register::isVirtualRegister(Reg: NextReg))
1537 break;
1538 SrcReg = NextReg;
1539 }
1540 return SrcReg;
1541}
1542
1543static bool eligibleForCompareElimination(MachineBasicBlock &MBB,
1544 MachineBasicBlock *&PredMBB,
1545 MachineBasicBlock *&MBBtoMoveCmp,
1546 MachineRegisterInfo *MRI) {
1547
1548 auto isEligibleBB = [&](MachineBasicBlock &BB) {
1549 auto BII = BB.getFirstInstrTerminator();
1550 // We optimize BBs ending with a conditional branch.
1551 // We check only for BCC here, not BCCLR, because BCCLR
1552 // will be formed only later in the pipeline.
1553 if (BB.succ_size() == 2 &&
1554 BII != BB.instr_end() &&
1555 (*BII).getOpcode() == PPC::BCC &&
1556 (*BII).getOperand(i: 1).isReg()) {
1557 // We optimize only if the condition code is used only by one BCC.
1558 Register CndReg = (*BII).getOperand(i: 1).getReg();
1559 if (!CndReg.isVirtual() || !MRI->hasOneNonDBGUse(RegNo: CndReg))
1560 return false;
1561
1562 MachineInstr *CMPI = MRI->getVRegDef(Reg: CndReg);
1563 // We assume compare and branch are in the same BB for ease of analysis.
1564 if (CMPI->getParent() != &BB)
1565 return false;
1566
1567 // We skip this BB if a physical register is used in comparison.
1568 for (MachineOperand &MO : CMPI->operands())
1569 if (MO.isReg() && !MO.getReg().isVirtual())
1570 return false;
1571
1572 return true;
1573 }
1574 return false;
1575 };
1576
1577 // If this BB has more than one successor, we can create a new BB and
1578 // move the compare instruction in the new BB.
1579 // So far, we do not move compare instruction to a BB having multiple
1580 // successors to avoid potentially increasing code size.
1581 auto isEligibleForMoveCmp = [](MachineBasicBlock &BB) {
1582 return BB.succ_size() == 1;
1583 };
1584
1585 if (!isEligibleBB(MBB))
1586 return false;
1587
1588 unsigned NumPredBBs = MBB.pred_size();
1589 if (NumPredBBs == 1) {
1590 MachineBasicBlock *TmpMBB = *MBB.pred_begin();
1591 if (isEligibleBB(*TmpMBB)) {
1592 PredMBB = TmpMBB;
1593 MBBtoMoveCmp = nullptr;
1594 return true;
1595 }
1596 }
1597 else if (NumPredBBs == 2) {
1598 // We check for partially redundant case.
1599 // So far, we support cases with only two predecessors
1600 // to avoid increasing the number of instructions.
1601 MachineBasicBlock::pred_iterator PI = MBB.pred_begin();
1602 MachineBasicBlock *Pred1MBB = *PI;
1603 MachineBasicBlock *Pred2MBB = *(PI+1);
1604
1605 if (isEligibleBB(*Pred1MBB) && isEligibleForMoveCmp(*Pred2MBB)) {
1606 // We assume Pred1MBB is the BB containing the compare to be merged and
1607 // Pred2MBB is the BB to which we will append a compare instruction.
1608 // Proceed as is if Pred1MBB is different from MBB.
1609 }
1610 else if (isEligibleBB(*Pred2MBB) && isEligibleForMoveCmp(*Pred1MBB)) {
1611 // We need to swap Pred1MBB and Pred2MBB to canonicalize.
1612 std::swap(a&: Pred1MBB, b&: Pred2MBB);
1613 }
1614 else return false;
1615
1616 if (Pred1MBB == &MBB)
1617 return false;
1618
1619 // Here, Pred2MBB is the BB to which we need to append a compare inst.
1620 // We cannot move the compare instruction if operands are not available
1621 // in Pred2MBB (i.e. defined in MBB by an instruction other than PHI).
1622 MachineInstr *BI = &*MBB.getFirstInstrTerminator();
1623 MachineInstr *CMPI = MRI->getVRegDef(Reg: BI->getOperand(i: 1).getReg());
1624 for (int I = 1; I <= 2; I++)
1625 if (CMPI->getOperand(i: I).isReg()) {
1626 MachineInstr *Inst = MRI->getVRegDef(Reg: CMPI->getOperand(i: I).getReg());
1627 if (Inst->getParent() == &MBB && Inst->getOpcode() != PPC::PHI)
1628 return false;
1629 }
1630
1631 PredMBB = Pred1MBB;
1632 MBBtoMoveCmp = Pred2MBB;
1633 return true;
1634 }
1635
1636 return false;
1637}
1638
1639// This function will iterate over the input map containing a pair of TOC save
1640// instruction and a flag. The flag will be set to false if the TOC save is
1641// proven redundant. This function will erase from the basic block all the TOC
1642// saves marked as redundant.
1643bool PPCMIPeephole::eliminateRedundantTOCSaves(
1644 std::map<MachineInstr *, bool> &TOCSaves) {
1645 bool Simplified = false;
1646 int NumKept = 0;
1647 for (auto TOCSave : TOCSaves) {
1648 if (!TOCSave.second) {
1649 TOCSave.first->eraseFromParent();
1650 RemoveTOCSave++;
1651 Simplified = true;
1652 } else {
1653 NumKept++;
1654 }
1655 }
1656
1657 if (NumKept > 1)
1658 MultiTOCSaves++;
1659
1660 return Simplified;
1661}
1662
1663// If multiple conditional branches are executed based on the (essentially)
1664// same comparison, we merge compare instructions into one and make multiple
1665// conditional branches on this comparison.
1666// For example,
1667// if (a == 0) { ... }
1668// else if (a < 0) { ... }
1669// can be executed by one compare and two conditional branches instead of
1670// two pairs of a compare and a conditional branch.
1671//
1672// This method merges two compare instructions in two MBBs and modifies the
1673// compare and conditional branch instructions if needed.
1674// For the above example, the input for this pass looks like:
1675// cmplwi r3, 0
1676// beq 0, .LBB0_3
1677// cmpwi r3, -1
1678// bgt 0, .LBB0_4
1679// So, before merging two compares, we need to modify these instructions as
1680// cmpwi r3, 0 ; cmplwi and cmpwi yield same result for beq
1681// beq 0, .LBB0_3
1682// cmpwi r3, 0 ; greather than -1 means greater or equal to 0
1683// bge 0, .LBB0_4
1684
1685bool PPCMIPeephole::eliminateRedundantCompare() {
1686 bool Simplified = false;
1687
1688 for (MachineBasicBlock &MBB2 : *MF) {
1689 MachineBasicBlock *MBB1 = nullptr, *MBBtoMoveCmp = nullptr;
1690
1691 // For fully redundant case, we select two basic blocks MBB1 and MBB2
1692 // as an optimization target if
1693 // - both MBBs end with a conditional branch,
1694 // - MBB1 is the only predecessor of MBB2, and
1695 // - compare does not take a physical register as a operand in both MBBs.
1696 // In this case, eligibleForCompareElimination sets MBBtoMoveCmp nullptr.
1697 //
1698 // As partially redundant case, we additionally handle if MBB2 has one
1699 // additional predecessor, which has only one successor (MBB2).
1700 // In this case, we move the compare instruction originally in MBB2 into
1701 // MBBtoMoveCmp. This partially redundant case is typically appear by
1702 // compiling a while loop; here, MBBtoMoveCmp is the loop preheader.
1703 //
1704 // Overview of CFG of related basic blocks
1705 // Fully redundant case Partially redundant case
1706 // -------- ---------------- --------
1707 // | MBB1 | (w/ 2 succ) | MBBtoMoveCmp | | MBB1 | (w/ 2 succ)
1708 // -------- ---------------- --------
1709 // | \ (w/ 1 succ) \ | \
1710 // | \ \ | \
1711 // | \ |
1712 // -------- --------
1713 // | MBB2 | (w/ 1 pred | MBB2 | (w/ 2 pred
1714 // -------- and 2 succ) -------- and 2 succ)
1715 // | \ | \
1716 // | \ | \
1717 //
1718 if (!eligibleForCompareElimination(MBB&: MBB2, PredMBB&: MBB1, MBBtoMoveCmp, MRI))
1719 continue;
1720
1721 MachineInstr *BI1 = &*MBB1->getFirstInstrTerminator();
1722 MachineInstr *CMPI1 = MRI->getVRegDef(Reg: BI1->getOperand(i: 1).getReg());
1723
1724 MachineInstr *BI2 = &*MBB2.getFirstInstrTerminator();
1725 MachineInstr *CMPI2 = MRI->getVRegDef(Reg: BI2->getOperand(i: 1).getReg());
1726 bool IsPartiallyRedundant = (MBBtoMoveCmp != nullptr);
1727
1728 // We cannot optimize an unsupported compare opcode or
1729 // a mix of 32-bit and 64-bit comparisons
1730 if (!isSupportedCmpOp(opCode: CMPI1->getOpcode()) ||
1731 !isSupportedCmpOp(opCode: CMPI2->getOpcode()) ||
1732 is64bitCmpOp(opCode: CMPI1->getOpcode()) != is64bitCmpOp(opCode: CMPI2->getOpcode()))
1733 continue;
1734
1735 unsigned NewOpCode = 0;
1736 unsigned NewPredicate1 = 0, NewPredicate2 = 0;
1737 int16_t Imm1 = 0, NewImm1 = 0, Imm2 = 0, NewImm2 = 0;
1738 bool SwapOperands = false;
1739
1740 if (CMPI1->getOpcode() != CMPI2->getOpcode()) {
1741 // Typically, unsigned comparison is used for equality check, but
1742 // we replace it with a signed comparison if the comparison
1743 // to be merged is a signed comparison.
1744 // In other cases of opcode mismatch, we cannot optimize this.
1745
1746 // We cannot change opcode when comparing against an immediate
1747 // if the most significant bit of the immediate is one
1748 // due to the difference in sign extension.
1749 auto CmpAgainstImmWithSignBit = [](MachineInstr *I) {
1750 if (!I->getOperand(i: 2).isImm())
1751 return false;
1752 int16_t Imm = (int16_t)I->getOperand(i: 2).getImm();
1753 return Imm < 0;
1754 };
1755
1756 if (isEqOrNe(BI: BI2) && !CmpAgainstImmWithSignBit(CMPI2) &&
1757 CMPI1->getOpcode() == getSignedCmpOpCode(opCode: CMPI2->getOpcode()))
1758 NewOpCode = CMPI1->getOpcode();
1759 else if (isEqOrNe(BI: BI1) && !CmpAgainstImmWithSignBit(CMPI1) &&
1760 getSignedCmpOpCode(opCode: CMPI1->getOpcode()) == CMPI2->getOpcode())
1761 NewOpCode = CMPI2->getOpcode();
1762 else continue;
1763 }
1764
1765 if (CMPI1->getOperand(i: 2).isReg() && CMPI2->getOperand(i: 2).isReg()) {
1766 // In case of comparisons between two registers, these two registers
1767 // must be same to merge two comparisons.
1768 unsigned Cmp1Operand1 = getSrcVReg(Reg: CMPI1->getOperand(i: 1).getReg(),
1769 BB1: nullptr, BB2: nullptr, MRI);
1770 unsigned Cmp1Operand2 = getSrcVReg(Reg: CMPI1->getOperand(i: 2).getReg(),
1771 BB1: nullptr, BB2: nullptr, MRI);
1772 unsigned Cmp2Operand1 = getSrcVReg(Reg: CMPI2->getOperand(i: 1).getReg(),
1773 BB1: MBB1, BB2: &MBB2, MRI);
1774 unsigned Cmp2Operand2 = getSrcVReg(Reg: CMPI2->getOperand(i: 2).getReg(),
1775 BB1: MBB1, BB2: &MBB2, MRI);
1776
1777 if (Cmp1Operand1 == Cmp2Operand1 && Cmp1Operand2 == Cmp2Operand2) {
1778 // Same pair of registers in the same order; ready to merge as is.
1779 }
1780 else if (Cmp1Operand1 == Cmp2Operand2 && Cmp1Operand2 == Cmp2Operand1) {
1781 // Same pair of registers in different order.
1782 // We reverse the predicate to merge compare instructions.
1783 PPC::Predicate Pred = (PPC::Predicate)BI2->getOperand(i: 0).getImm();
1784 NewPredicate2 = (unsigned)PPC::getSwappedPredicate(Opcode: Pred);
1785 // In case of partial redundancy, we need to swap operands
1786 // in another compare instruction.
1787 SwapOperands = true;
1788 }
1789 else continue;
1790 }
1791 else if (CMPI1->getOperand(i: 2).isImm() && CMPI2->getOperand(i: 2).isImm()) {
1792 // In case of comparisons between a register and an immediate,
1793 // the operand register must be same for two compare instructions.
1794 unsigned Cmp1Operand1 = getSrcVReg(Reg: CMPI1->getOperand(i: 1).getReg(),
1795 BB1: nullptr, BB2: nullptr, MRI);
1796 unsigned Cmp2Operand1 = getSrcVReg(Reg: CMPI2->getOperand(i: 1).getReg(),
1797 BB1: MBB1, BB2: &MBB2, MRI);
1798 if (Cmp1Operand1 != Cmp2Operand1)
1799 continue;
1800
1801 NewImm1 = Imm1 = (int16_t)CMPI1->getOperand(i: 2).getImm();
1802 NewImm2 = Imm2 = (int16_t)CMPI2->getOperand(i: 2).getImm();
1803
1804 // If immediate are not same, we try to adjust by changing predicate;
1805 // e.g. GT imm means GE (imm+1).
1806 if (Imm1 != Imm2 && (!isEqOrNe(BI: BI2) || !isEqOrNe(BI: BI1))) {
1807 int Diff = Imm1 - Imm2;
1808 if (Diff < -2 || Diff > 2)
1809 continue;
1810
1811 unsigned PredToInc1 = getPredicateToIncImm(BI: BI1, CMPI: CMPI1);
1812 unsigned PredToDec1 = getPredicateToDecImm(BI: BI1, CMPI: CMPI1);
1813 unsigned PredToInc2 = getPredicateToIncImm(BI: BI2, CMPI: CMPI2);
1814 unsigned PredToDec2 = getPredicateToDecImm(BI: BI2, CMPI: CMPI2);
1815 if (Diff == 2) {
1816 if (PredToInc2 && PredToDec1) {
1817 NewPredicate2 = PredToInc2;
1818 NewPredicate1 = PredToDec1;
1819 NewImm2++;
1820 NewImm1--;
1821 }
1822 }
1823 else if (Diff == 1) {
1824 if (PredToInc2) {
1825 NewImm2++;
1826 NewPredicate2 = PredToInc2;
1827 }
1828 else if (PredToDec1) {
1829 NewImm1--;
1830 NewPredicate1 = PredToDec1;
1831 }
1832 }
1833 else if (Diff == -1) {
1834 if (PredToDec2) {
1835 NewImm2--;
1836 NewPredicate2 = PredToDec2;
1837 }
1838 else if (PredToInc1) {
1839 NewImm1++;
1840 NewPredicate1 = PredToInc1;
1841 }
1842 }
1843 else if (Diff == -2) {
1844 if (PredToDec2 && PredToInc1) {
1845 NewPredicate2 = PredToDec2;
1846 NewPredicate1 = PredToInc1;
1847 NewImm2--;
1848 NewImm1++;
1849 }
1850 }
1851 }
1852
1853 // We cannot merge two compares if the immediates are not same.
1854 if (NewImm2 != NewImm1)
1855 continue;
1856 }
1857
1858 LLVM_DEBUG(dbgs() << "Optimize two pairs of compare and branch:\n");
1859 LLVM_DEBUG(CMPI1->dump());
1860 LLVM_DEBUG(BI1->dump());
1861 LLVM_DEBUG(CMPI2->dump());
1862 LLVM_DEBUG(BI2->dump());
1863 for (const MachineOperand &MO : CMPI1->operands())
1864 if (MO.isReg())
1865 addRegToUpdate(MO.getReg());
1866 for (const MachineOperand &MO : CMPI2->operands())
1867 if (MO.isReg())
1868 addRegToUpdate(MO.getReg());
1869
1870 // We adjust opcode, predicates and immediate as we determined above.
1871 if (NewOpCode != 0 && NewOpCode != CMPI1->getOpcode()) {
1872 CMPI1->setDesc(TII->get(Opcode: NewOpCode));
1873 }
1874 if (NewPredicate1) {
1875 BI1->getOperand(i: 0).setImm(NewPredicate1);
1876 }
1877 if (NewPredicate2) {
1878 BI2->getOperand(i: 0).setImm(NewPredicate2);
1879 }
1880 if (NewImm1 != Imm1) {
1881 CMPI1->getOperand(i: 2).setImm(NewImm1);
1882 }
1883
1884 if (IsPartiallyRedundant) {
1885 // We touch up the compare instruction in MBB2 and move it to
1886 // a previous BB to handle partially redundant case.
1887 if (SwapOperands) {
1888 Register Op1 = CMPI2->getOperand(i: 1).getReg();
1889 Register Op2 = CMPI2->getOperand(i: 2).getReg();
1890 CMPI2->getOperand(i: 1).setReg(Op2);
1891 CMPI2->getOperand(i: 2).setReg(Op1);
1892 }
1893 if (NewImm2 != Imm2)
1894 CMPI2->getOperand(i: 2).setImm(NewImm2);
1895
1896 for (int I = 1; I <= 2; I++) {
1897 if (CMPI2->getOperand(i: I).isReg()) {
1898 MachineInstr *Inst = MRI->getVRegDef(Reg: CMPI2->getOperand(i: I).getReg());
1899 if (Inst->getParent() != &MBB2)
1900 continue;
1901
1902 assert(Inst->getOpcode() == PPC::PHI &&
1903 "We cannot support if an operand comes from this BB.");
1904 unsigned SrcReg = getIncomingRegForBlock(Phi: Inst, MBB: MBBtoMoveCmp);
1905 CMPI2->getOperand(i: I).setReg(SrcReg);
1906 addRegToUpdate(SrcReg);
1907 }
1908 }
1909 auto I = MachineBasicBlock::iterator(MBBtoMoveCmp->getFirstTerminator());
1910 MBBtoMoveCmp->splice(Where: I, Other: &MBB2, From: MachineBasicBlock::iterator(CMPI2));
1911
1912 DebugLoc DL = CMPI2->getDebugLoc();
1913 Register NewVReg = MRI->createVirtualRegister(RegClass: &PPC::CRRCRegClass);
1914 BuildMI(BB&: MBB2, I: MBB2.begin(), MIMD: DL,
1915 MCID: TII->get(Opcode: PPC::PHI), DestReg: NewVReg)
1916 .addReg(RegNo: BI1->getOperand(i: 1).getReg()).addMBB(MBB: MBB1)
1917 .addReg(RegNo: BI2->getOperand(i: 1).getReg()).addMBB(MBB: MBBtoMoveCmp);
1918 BI2->getOperand(i: 1).setReg(NewVReg);
1919 addRegToUpdate(NewVReg);
1920 }
1921 else {
1922 // We finally eliminate compare instruction in MBB2.
1923 // We do not need to treat CMPI2 specially here in terms of re-computing
1924 // live variables even though it is being deleted because:
1925 // - It defines a register that has a single use (already checked in
1926 // eligibleForCompareElimination())
1927 // - The only user (BI2) is no longer using it so the register is dead (no
1928 // def, no uses)
1929 // - We do not attempt to recompute live variables for dead registers
1930 BI2->getOperand(i: 1).setReg(BI1->getOperand(i: 1).getReg());
1931 CMPI2->eraseFromParent();
1932 }
1933
1934 LLVM_DEBUG(dbgs() << "into a compare and two branches:\n");
1935 LLVM_DEBUG(CMPI1->dump());
1936 LLVM_DEBUG(BI1->dump());
1937 LLVM_DEBUG(BI2->dump());
1938 if (IsPartiallyRedundant) {
1939 LLVM_DEBUG(dbgs() << "The following compare is moved into "
1940 << printMBBReference(*MBBtoMoveCmp)
1941 << " to handle partial redundancy.\n");
1942 LLVM_DEBUG(CMPI2->dump());
1943 }
1944 Simplified = true;
1945 }
1946
1947 return Simplified;
1948}
1949
1950// We miss the opportunity to emit an RLDIC when lowering jump tables
1951// since ISEL sees only a single basic block. When selecting, the clear
1952// and shift left will be in different blocks.
1953bool PPCMIPeephole::emitRLDICWhenLoweringJumpTables(MachineInstr &MI,
1954 MachineInstr *&ToErase) {
1955 if (MI.getOpcode() != PPC::RLDICR)
1956 return false;
1957
1958 Register SrcReg = MI.getOperand(i: 1).getReg();
1959 if (!SrcReg.isVirtual())
1960 return false;
1961
1962 MachineInstr *SrcMI = MRI->getVRegDef(Reg: SrcReg);
1963 if (SrcMI->getOpcode() != PPC::RLDICL)
1964 return false;
1965
1966 MachineOperand MOpSHSrc = SrcMI->getOperand(i: 2);
1967 MachineOperand MOpMBSrc = SrcMI->getOperand(i: 3);
1968 MachineOperand MOpSHMI = MI.getOperand(i: 2);
1969 MachineOperand MOpMEMI = MI.getOperand(i: 3);
1970 if (!(MOpSHSrc.isImm() && MOpMBSrc.isImm() && MOpSHMI.isImm() &&
1971 MOpMEMI.isImm()))
1972 return false;
1973
1974 uint64_t SHSrc = MOpSHSrc.getImm();
1975 uint64_t MBSrc = MOpMBSrc.getImm();
1976 uint64_t SHMI = MOpSHMI.getImm();
1977 uint64_t MEMI = MOpMEMI.getImm();
1978 uint64_t NewSH = SHSrc + SHMI;
1979 uint64_t NewMB = MBSrc - SHMI;
1980 if (NewMB > 63 || NewSH > 63)
1981 return false;
1982
1983 // The bits cleared with RLDICL are [0, MBSrc).
1984 // The bits cleared with RLDICR are (MEMI, 63].
1985 // After the sequence, the bits cleared are:
1986 // [0, MBSrc-SHMI) and (MEMI, 63).
1987 //
1988 // The bits cleared with RLDIC are [0, NewMB) and (63-NewSH, 63].
1989 if ((63 - NewSH) != MEMI)
1990 return false;
1991
1992 LLVM_DEBUG(dbgs() << "Converting pair: ");
1993 LLVM_DEBUG(SrcMI->dump());
1994 LLVM_DEBUG(MI.dump());
1995
1996 MI.setDesc(TII->get(Opcode: PPC::RLDIC));
1997 MI.getOperand(i: 1).setReg(SrcMI->getOperand(i: 1).getReg());
1998 MI.getOperand(i: 2).setImm(NewSH);
1999 MI.getOperand(i: 3).setImm(NewMB);
2000 addRegToUpdate(MI.getOperand(1).getReg());
2001 addRegToUpdate(SrcMI->getOperand(0).getReg());
2002
2003 LLVM_DEBUG(dbgs() << "To: ");
2004 LLVM_DEBUG(MI.dump());
2005 NumRotatesCollapsed++;
2006 // If SrcReg has no non-debug use it's safe to delete its def SrcMI.
2007 if (MRI->use_nodbg_empty(RegNo: SrcReg)) {
2008 assert(!SrcMI->hasImplicitDef() &&
2009 "Not expecting an implicit def with this instr.");
2010 ToErase = SrcMI;
2011 }
2012 return true;
2013}
2014
2015// For case in LLVM IR
2016// entry:
2017// %iconv = sext i32 %index to i64
2018// br i1 undef label %true, label %false
2019// true:
2020// %ptr = getelementptr inbounds i32, i32* null, i64 %iconv
2021// ...
2022// PPCISelLowering::combineSHL fails to combine, because sext and shl are in
2023// different BBs when conducting instruction selection. We can do a peephole
2024// optimization to combine these two instructions into extswsli after
2025// instruction selection.
2026bool PPCMIPeephole::combineSEXTAndSHL(MachineInstr &MI,
2027 MachineInstr *&ToErase) {
2028 if (MI.getOpcode() != PPC::RLDICR)
2029 return false;
2030
2031 if (!MF->getSubtarget<PPCSubtarget>().isISA3_0())
2032 return false;
2033
2034 assert(MI.getNumOperands() == 4 && "RLDICR should have 4 operands");
2035
2036 MachineOperand MOpSHMI = MI.getOperand(i: 2);
2037 MachineOperand MOpMEMI = MI.getOperand(i: 3);
2038 if (!(MOpSHMI.isImm() && MOpMEMI.isImm()))
2039 return false;
2040
2041 uint64_t SHMI = MOpSHMI.getImm();
2042 uint64_t MEMI = MOpMEMI.getImm();
2043 if (SHMI + MEMI != 63)
2044 return false;
2045
2046 Register SrcReg = MI.getOperand(i: 1).getReg();
2047 if (!SrcReg.isVirtual())
2048 return false;
2049
2050 MachineInstr *SrcMI = MRI->getVRegDef(Reg: SrcReg);
2051 if (SrcMI->getOpcode() != PPC::EXTSW &&
2052 SrcMI->getOpcode() != PPC::EXTSW_32_64)
2053 return false;
2054
2055 // If the register defined by extsw has more than one use, combination is not
2056 // needed.
2057 if (!MRI->hasOneNonDBGUse(RegNo: SrcReg))
2058 return false;
2059
2060 assert(SrcMI->getNumOperands() == 2 && "EXTSW should have 2 operands");
2061 assert(SrcMI->getOperand(1).isReg() &&
2062 "EXTSW's second operand should be a register");
2063 if (!SrcMI->getOperand(i: 1).getReg().isVirtual())
2064 return false;
2065
2066 LLVM_DEBUG(dbgs() << "Combining pair: ");
2067 LLVM_DEBUG(SrcMI->dump());
2068 LLVM_DEBUG(MI.dump());
2069
2070 MachineInstr *NewInstr =
2071 BuildMI(BB&: *MI.getParent(), I: &MI, MIMD: MI.getDebugLoc(),
2072 MCID: SrcMI->getOpcode() == PPC::EXTSW ? TII->get(Opcode: PPC::EXTSWSLI)
2073 : TII->get(Opcode: PPC::EXTSWSLI_32_64),
2074 DestReg: MI.getOperand(i: 0).getReg())
2075 .add(MO: SrcMI->getOperand(i: 1))
2076 .add(MO: MOpSHMI);
2077 (void)NewInstr;
2078
2079 LLVM_DEBUG(dbgs() << "TO: ");
2080 LLVM_DEBUG(NewInstr->dump());
2081 ++NumEXTSWAndSLDICombined;
2082 ToErase = &MI;
2083 // SrcMI, which is extsw, is of no use now, but we don't erase it here so we
2084 // can recompute its kill flags. We run DCE immediately after this pass
2085 // to clean up dead instructions such as this.
2086 addRegToUpdate(NewInstr->getOperand(1).getReg());
2087 addRegToUpdate(SrcMI->getOperand(0).getReg());
2088 return true;
2089}
2090
2091} // end default namespace
2092
2093INITIALIZE_PASS_BEGIN(PPCMIPeephole, DEBUG_TYPE,
2094 "PowerPC MI Peephole Optimization", false, false)
2095INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
2096INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
2097INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
2098INITIALIZE_PASS_END(PPCMIPeephole, DEBUG_TYPE,
2099 "PowerPC MI Peephole Optimization", false, false)
2100
2101char PPCMIPeephole::ID = 0;
2102FunctionPass*
2103llvm::createPPCMIPeepholePass() { return new PPCMIPeephole(); }
2104