1 //===------------- PPCExpandISEL.cpp - Expand ISEL instruction ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // A pass that expands the ISEL instruction into an if-then-else sequence.
10 // This pass must be run post-RA since all operands must be physical registers.
12 //===----------------------------------------------------------------------===//
15 #include "PPCInstrInfo.h"
16 #include "PPCSubtarget.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/LivePhysRegs.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "ppc-expand-isel"
31 STATISTIC(NumExpanded
, "Number of ISEL instructions expanded");
32 STATISTIC(NumRemoved
, "Number of ISEL instructions removed");
33 STATISTIC(NumFolded
, "Number of ISEL instructions folded");
35 // If -ppc-gen-isel=false is set, we will disable generating the ISEL
36 // instruction on all PPC targets. Otherwise, if the user set option
37 // -misel or the platform supports ISEL by default, still generate the
38 // ISEL instruction, else expand it.
40 GenerateISEL("ppc-gen-isel",
41 cl::desc("Enable generating the ISEL instruction."),
42 cl::init(true), cl::Hidden
);
45 class PPCExpandISEL
: public MachineFunctionPass
{
48 const TargetInstrInfo
*TII
;
49 bool IsTrueBlockRequired
;
50 bool IsFalseBlockRequired
;
51 MachineBasicBlock
*TrueBlock
;
52 MachineBasicBlock
*FalseBlock
;
53 MachineBasicBlock
*NewSuccessor
;
54 MachineBasicBlock::iterator TrueBlockI
;
55 MachineBasicBlock::iterator FalseBlockI
;
57 typedef SmallVector
<MachineInstr
*, 4> BlockISELList
;
58 typedef SmallDenseMap
<int, BlockISELList
> ISELInstructionList
;
60 // A map of MBB numbers to their lists of contained ISEL instructions.
61 // Please note when we traverse this list and expand ISEL, we only remove
62 // the ISEL from the MBB not from this list.
63 ISELInstructionList ISELInstructions
;
65 /// Initialize the object.
66 void initialize(MachineFunction
&MFParam
);
68 void handleSpecialCases(BlockISELList
&BIL
, MachineBasicBlock
*MBB
);
69 void reorganizeBlockLayout(BlockISELList
&BIL
, MachineBasicBlock
*MBB
);
70 void populateBlocks(BlockISELList
&BIL
);
71 void expandMergeableISELs(BlockISELList
&BIL
);
72 void expandAndMergeISELs();
74 bool canMerge(MachineInstr
*PrevPushedMI
, MachineInstr
*MI
);
76 /// Is this instruction an ISEL or ISEL8?
77 static bool isISEL(const MachineInstr
&MI
) {
78 return (MI
.getOpcode() == PPC::ISEL
|| MI
.getOpcode() == PPC::ISEL8
);
81 /// Is this instruction an ISEL8?
82 static bool isISEL8(const MachineInstr
&MI
) {
83 return (MI
.getOpcode() == PPC::ISEL8
);
86 /// Are the two operands using the same register?
87 bool useSameRegister(const MachineOperand
&Op1
, const MachineOperand
&Op2
) {
88 return (Op1
.getReg() == Op2
.getReg());
92 /// Collect all ISEL instructions from the current function.
94 /// Walk the current function and collect all the ISEL instructions that are
95 /// found. The instructions are placed in the ISELInstructions vector.
97 /// \return true if any ISEL instructions were found, false otherwise
99 bool collectISELInstructions();
103 PPCExpandISEL() : MachineFunctionPass(ID
) {
104 initializePPCExpandISELPass(*PassRegistry::getPassRegistry());
108 /// Determine whether to generate the ISEL instruction or expand it.
110 /// Expand ISEL instruction into if-then-else sequence when one of
111 /// the following two conditions hold:
112 /// (1) -ppc-gen-isel=false
113 /// (2) hasISEL() return false
114 /// Otherwise, still generate ISEL instruction.
115 /// The -ppc-gen-isel option is set to true by default. Which means the ISEL
116 /// instruction is still generated by default on targets that support them.
118 /// \return true if ISEL should be expanded into if-then-else code sequence;
119 /// false if ISEL instruction should be generated, i.e. not expanded.
121 static bool isExpandISELEnabled(const MachineFunction
&MF
);
124 void DumpISELInstructions() const;
127 bool runOnMachineFunction(MachineFunction
&MF
) override
{
128 LLVM_DEBUG(dbgs() << "Function: "; MF
.dump(); dbgs() << "\n");
131 if (!collectISELInstructions()) {
132 LLVM_DEBUG(dbgs() << "No ISEL instructions in this function\n");
137 DumpISELInstructions();
140 expandAndMergeISELs();
145 } // end anonymous namespace
147 void PPCExpandISEL::initialize(MachineFunction
&MFParam
) {
149 TII
= MF
->getSubtarget().getInstrInfo();
150 ISELInstructions
.clear();
153 bool PPCExpandISEL::isExpandISELEnabled(const MachineFunction
&MF
) {
154 return !GenerateISEL
|| !MF
.getSubtarget
<PPCSubtarget
>().hasISEL();
157 bool PPCExpandISEL::collectISELInstructions() {
158 for (MachineBasicBlock
&MBB
: *MF
) {
159 BlockISELList thisBlockISELs
;
160 for (MachineInstr
&MI
: MBB
)
162 thisBlockISELs
.push_back(&MI
);
163 if (!thisBlockISELs
.empty())
164 ISELInstructions
.insert(std::make_pair(MBB
.getNumber(), thisBlockISELs
));
166 return !ISELInstructions
.empty();
170 void PPCExpandISEL::DumpISELInstructions() const {
171 for (const auto &I
: ISELInstructions
) {
172 LLVM_DEBUG(dbgs() << printMBBReference(*MF
->getBlockNumbered(I
.first
))
174 for (const auto &VI
: I
.second
)
175 LLVM_DEBUG(dbgs() << " "; VI
->print(dbgs()));
180 /// Contiguous ISELs that have the same condition can be merged.
181 bool PPCExpandISEL::canMerge(MachineInstr
*PrevPushedMI
, MachineInstr
*MI
) {
182 // Same Condition Register?
183 if (!useSameRegister(PrevPushedMI
->getOperand(3), MI
->getOperand(3)))
186 MachineBasicBlock::iterator PrevPushedMBBI
= *PrevPushedMI
;
187 MachineBasicBlock::iterator MBBI
= *MI
;
188 return (std::prev(MBBI
) == PrevPushedMBBI
); // Contiguous ISELs?
191 void PPCExpandISEL::expandAndMergeISELs() {
192 bool ExpandISELEnabled
= isExpandISELEnabled(*MF
);
194 for (auto &BlockList
: ISELInstructions
) {
196 dbgs() << "Expanding ISEL instructions in "
197 << printMBBReference(*MF
->getBlockNumbered(BlockList
.first
))
199 BlockISELList
&CurrentISELList
= BlockList
.second
;
200 auto I
= CurrentISELList
.begin();
201 auto E
= CurrentISELList
.end();
204 assert(isISEL(**I
) && "Expecting an ISEL instruction");
205 MachineOperand
&Dest
= (*I
)->getOperand(0);
206 MachineOperand
&TrueValue
= (*I
)->getOperand(1);
207 MachineOperand
&FalseValue
= (*I
)->getOperand(2);
209 // Special case 1, all registers used by ISEL are the same one.
210 // The non-redundant isel 0, 0, 0, N would not satisfy these conditions
211 // as it would be ISEL %R0, %ZERO, %R0, %CRN.
212 if (useSameRegister(Dest
, TrueValue
) &&
213 useSameRegister(Dest
, FalseValue
)) {
214 LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction: " << **I
216 // FIXME: if the CR field used has no other uses, we could eliminate the
217 // instruction that defines it. This would have to be done manually
218 // since this pass runs too late to run DCE after it.
220 (*I
)->eraseFromParent();
222 } else if (useSameRegister(TrueValue
, FalseValue
)) {
223 // Special case 2, the two input registers used by ISEL are the same.
224 // Note: the non-foldable isel RX, 0, 0, N would not satisfy this
225 // condition as it would be ISEL %RX, %ZERO, %R0, %CRN, which makes it
226 // safe to fold ISEL to MR(OR) instead of ADDI.
227 MachineBasicBlock
*MBB
= (*I
)->getParent();
229 dbgs() << "Fold the ISEL instruction to an unconditional copy:\n");
230 LLVM_DEBUG(dbgs() << "ISEL: " << **I
<< "\n");
232 // Note: we're using both the TrueValue and FalseValue operands so as
233 // not to lose the kill flag if it is set on either of them.
234 BuildMI(*MBB
, (*I
), dl
, TII
->get(isISEL8(**I
) ? PPC::OR8
: PPC::OR
))
238 (*I
)->eraseFromParent();
240 } else if (ExpandISELEnabled
) { // Normal cases expansion enabled
241 LLVM_DEBUG(dbgs() << "Expand ISEL instructions:\n");
242 LLVM_DEBUG(dbgs() << "ISEL: " << **I
<< "\n");
243 BlockISELList SubISELList
;
244 SubISELList
.push_back(*I
++);
245 // Collect the ISELs that can be merged together.
246 // This will eat up ISEL instructions without considering whether they
247 // may be redundant or foldable to a register copy. So we still keep
248 // the handleSpecialCases() downstream to handle them.
249 while (I
!= E
&& canMerge(SubISELList
.back(), *I
)) {
250 LLVM_DEBUG(dbgs() << "ISEL: " << **I
<< "\n");
251 SubISELList
.push_back(*I
++);
254 expandMergeableISELs(SubISELList
);
255 } else { // Normal cases expansion disabled
256 I
++; // leave the ISEL as it is
262 void PPCExpandISEL::handleSpecialCases(BlockISELList
&BIL
,
263 MachineBasicBlock
*MBB
) {
264 IsTrueBlockRequired
= false;
265 IsFalseBlockRequired
= false;
267 auto MI
= BIL
.begin();
268 while (MI
!= BIL
.end()) {
269 assert(isISEL(**MI
) && "Expecting an ISEL instruction");
270 LLVM_DEBUG(dbgs() << "ISEL: " << **MI
<< "\n");
272 MachineOperand
&Dest
= (*MI
)->getOperand(0);
273 MachineOperand
&TrueValue
= (*MI
)->getOperand(1);
274 MachineOperand
&FalseValue
= (*MI
)->getOperand(2);
276 // If at least one of the ISEL instructions satisfy the following
277 // condition, we need the True Block:
278 // The Dest Register and True Value Register are not the same
279 // Similarly, if at least one of the ISEL instructions satisfy the
280 // following condition, we need the False Block:
281 // The Dest Register and False Value Register are not the same.
282 bool IsADDIInstRequired
= !useSameRegister(Dest
, TrueValue
);
283 bool IsORIInstRequired
= !useSameRegister(Dest
, FalseValue
);
285 // Special case 1, all registers used by ISEL are the same one.
286 if (!IsADDIInstRequired
&& !IsORIInstRequired
) {
287 LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction.");
288 // FIXME: if the CR field used has no other uses, we could eliminate the
289 // instruction that defines it. This would have to be done manually
290 // since this pass runs too late to run DCE after it.
292 (*MI
)->eraseFromParent();
293 // Setting MI to the erase result keeps the iterator valid and increased.
298 // Special case 2, the two input registers used by ISEL are the same.
299 // Note 1: We favor merging ISEL expansions over folding a single one. If
300 // the passed list has multiple merge-able ISEL's, we won't fold any.
301 // Note 2: There is no need to test for PPC::R0/PPC::X0 because PPC::ZERO/
302 // PPC::ZERO8 will be used for the first operand if the value is meant to
303 // be zero. In this case, the useSameRegister method will return false,
304 // thereby preventing this ISEL from being folded.
305 if (useSameRegister(TrueValue
, FalseValue
) && (BIL
.size() == 1)) {
307 dbgs() << "Fold the ISEL instruction to an unconditional copy.");
309 // Note: we're using both the TrueValue and FalseValue operands so as
310 // not to lose the kill flag if it is set on either of them.
311 BuildMI(*MBB
, (*MI
), dl
, TII
->get(isISEL8(**MI
) ? PPC::OR8
: PPC::OR
))
315 (*MI
)->eraseFromParent();
316 // Setting MI to the erase result keeps the iterator valid and increased.
321 IsTrueBlockRequired
|= IsADDIInstRequired
;
322 IsFalseBlockRequired
|= IsORIInstRequired
;
327 void PPCExpandISEL::reorganizeBlockLayout(BlockISELList
&BIL
,
328 MachineBasicBlock
*MBB
) {
332 assert((IsTrueBlockRequired
|| IsFalseBlockRequired
) &&
333 "Should have been handled by special cases earlier!");
335 MachineBasicBlock
*Successor
= nullptr;
336 const BasicBlock
*LLVM_BB
= MBB
->getBasicBlock();
337 MachineBasicBlock::iterator MBBI
= (*BIL
.back());
338 NewSuccessor
= (MBBI
!= MBB
->getLastNonDebugInstr() || !MBB
->canFallThrough())
339 // Another BB is needed to move the instructions that
340 // follow this ISEL. If the ISEL is the last instruction
341 // in a block that can't fall through, we also need a block
343 ? MF
->CreateMachineBasicBlock(LLVM_BB
)
346 MachineFunction::iterator It
= MBB
->getIterator();
347 ++It
; // Point to the successor block of MBB.
349 // If NewSuccessor is NULL then the last ISEL in this group is the last
350 // non-debug instruction in this block. Find the fall-through successor
351 // of this block to use when updating the CFG below.
353 for (auto &Succ
: MBB
->successors()) {
354 if (MBB
->isLayoutSuccessor(Succ
)) {
360 Successor
= NewSuccessor
;
362 // The FalseBlock and TrueBlock are inserted after the MBB block but before
364 // Note this need to be done *after* the above setting the Successor code.
365 if (IsFalseBlockRequired
) {
366 FalseBlock
= MF
->CreateMachineBasicBlock(LLVM_BB
);
367 MF
->insert(It
, FalseBlock
);
370 if (IsTrueBlockRequired
) {
371 TrueBlock
= MF
->CreateMachineBasicBlock(LLVM_BB
);
372 MF
->insert(It
, TrueBlock
);
376 MF
->insert(It
, NewSuccessor
);
378 // Transfer the rest of this block into the new successor block.
379 NewSuccessor
->splice(NewSuccessor
->end(), MBB
,
380 std::next(MachineBasicBlock::iterator(BIL
.back())),
382 NewSuccessor
->transferSuccessorsAndUpdatePHIs(MBB
);
384 // Copy the original liveIns of MBB to NewSuccessor.
385 for (auto &LI
: MBB
->liveins())
386 NewSuccessor
->addLiveIn(LI
);
388 // After splitting the NewSuccessor block, Regs defined but not killed
389 // in MBB should be treated as liveins of NewSuccessor.
390 // Note: Cannot use stepBackward instead since we are using the Reg
391 // liveness state at the end of MBB (liveOut of MBB) as the liveIn for
392 // NewSuccessor. Otherwise, will cause cyclic dependence.
393 LivePhysRegs
LPR(*MF
->getSubtarget
<PPCSubtarget
>().getRegisterInfo());
394 SmallVector
<std::pair
<MCPhysReg
, const MachineOperand
*>, 2> Clobbers
;
395 for (MachineInstr
&MI
: *MBB
)
396 LPR
.stepForward(MI
, Clobbers
);
398 NewSuccessor
->addLiveIn(LI
);
400 // Remove successor from MBB.
401 MBB
->removeSuccessor(Successor
);
404 // Note that this needs to be done *after* transfering the successors from MBB
405 // to the NewSuccessor block, otherwise these blocks will also be transferred
407 MBB
->addSuccessor(IsTrueBlockRequired
? TrueBlock
: Successor
);
408 MBB
->addSuccessor(IsFalseBlockRequired
? FalseBlock
: Successor
);
410 if (IsTrueBlockRequired
) {
411 TrueBlockI
= TrueBlock
->begin();
412 TrueBlock
->addSuccessor(Successor
);
415 if (IsFalseBlockRequired
) {
416 FalseBlockI
= FalseBlock
->begin();
417 FalseBlock
->addSuccessor(Successor
);
420 // Conditional branch to the TrueBlock or Successor
421 BuildMI(*MBB
, BIL
.back(), dl
, TII
->get(PPC::BC
))
422 .add(BIL
.back()->getOperand(3))
423 .addMBB(IsTrueBlockRequired
? TrueBlock
: Successor
);
425 // Jump over the true block to the new successor if the condition is false.
426 BuildMI(*(IsFalseBlockRequired
? FalseBlock
: MBB
),
427 (IsFalseBlockRequired
? FalseBlockI
: BIL
.back()), dl
,
431 if (IsFalseBlockRequired
)
432 FalseBlockI
= FalseBlock
->begin(); // get the position of PPC::B
435 void PPCExpandISEL::populateBlocks(BlockISELList
&BIL
) {
436 for (auto &MI
: BIL
) {
437 assert(isISEL(*MI
) && "Expecting an ISEL instruction");
439 MachineOperand
&Dest
= MI
->getOperand(0); // location to store to
440 MachineOperand
&TrueValue
= MI
->getOperand(1); // Value to store if
442 MachineOperand
&FalseValue
= MI
->getOperand(2); // Value to store if
443 // condition is false
444 MachineOperand
&ConditionRegister
= MI
->getOperand(3); // Condition
446 LLVM_DEBUG(dbgs() << "Dest: " << Dest
<< "\n");
447 LLVM_DEBUG(dbgs() << "TrueValue: " << TrueValue
<< "\n");
448 LLVM_DEBUG(dbgs() << "FalseValue: " << FalseValue
<< "\n");
449 LLVM_DEBUG(dbgs() << "ConditionRegister: " << ConditionRegister
<< "\n");
451 // If the Dest Register and True Value Register are not the same one, we
452 // need the True Block.
453 bool IsADDIInstRequired
= !useSameRegister(Dest
, TrueValue
);
454 bool IsORIInstRequired
= !useSameRegister(Dest
, FalseValue
);
456 if (IsADDIInstRequired
) {
457 // Copy the result into the destination if the condition is true.
458 BuildMI(*TrueBlock
, TrueBlockI
, dl
,
459 TII
->get(isISEL8(*MI
) ? PPC::ADDI8
: PPC::ADDI
))
462 .add(MachineOperand::CreateImm(0));
464 // Add the LiveIn registers required by true block.
465 TrueBlock
->addLiveIn(TrueValue
.getReg());
468 if (IsORIInstRequired
) {
469 // Add the LiveIn registers required by false block.
470 FalseBlock
->addLiveIn(FalseValue
.getReg());
474 // Add the LiveIn registers required by NewSuccessor block.
475 NewSuccessor
->addLiveIn(Dest
.getReg());
476 NewSuccessor
->addLiveIn(TrueValue
.getReg());
477 NewSuccessor
->addLiveIn(FalseValue
.getReg());
478 NewSuccessor
->addLiveIn(ConditionRegister
.getReg());
481 // Copy the value into the destination if the condition is false.
482 if (IsORIInstRequired
)
483 BuildMI(*FalseBlock
, FalseBlockI
, dl
,
484 TII
->get(isISEL8(*MI
) ? PPC::ORI8
: PPC::ORI
))
487 .add(MachineOperand::CreateImm(0));
489 MI
->eraseFromParent(); // Remove the ISEL instruction.
495 void PPCExpandISEL::expandMergeableISELs(BlockISELList
&BIL
) {
496 // At this stage all the ISELs of BIL are in the same MBB.
497 MachineBasicBlock
*MBB
= BIL
.back()->getParent();
499 handleSpecialCases(BIL
, MBB
);
500 reorganizeBlockLayout(BIL
, MBB
);
504 INITIALIZE_PASS(PPCExpandISEL
, DEBUG_TYPE
, "PowerPC Expand ISEL Generation",
506 char PPCExpandISEL::ID
= 0;
508 FunctionPass
*llvm::createPPCExpandISELPass() { return new PPCExpandISEL(); }