30 #define DEBUG_TYPE "ppc-expand-isel"
32 STATISTIC(NumExpanded,
"Number of ISEL instructions expanded");
33 STATISTIC(NumRemoved,
"Number of ISEL instructions removed");
34 STATISTIC(NumFolded,
"Number of ISEL instructions folded");
42 cl::desc(
"Enable generating the ISEL instruction."),
50 bool IsTrueBlockRequired;
51 bool IsFalseBlockRequired;
62 ISELInstructionList ISELInstructions;
69 void populateBlocks(BlockISELList &BIL);
70 void expandMergeableISELs(BlockISELList &BIL);
71 void expandAndMergeISELs();
98 bool collectISELInstructions();
123 void DumpISELInstructions()
const;
127 if (!isExpandISELEnabled(MF))
133 if (!collectISELInstructions()) {
134 DEBUG(
dbgs() <<
"No ISEL instructions in this function\n");
139 DumpISELInstructions();
142 expandAndMergeISELs();
152 ISELInstructions.clear();
159 bool PPCExpandISEL::collectISELInstructions() {
161 BlockISELList thisBlockISELs;
164 thisBlockISELs.push_back(&MI);
165 if (!thisBlockISELs.empty())
166 ISELInstructions.insert(std::make_pair(MBB.getNumber(), thisBlockISELs));
168 return !ISELInstructions.empty();
172 void PPCExpandISEL::DumpISELInstructions()
const {
173 for (
const auto &
I : ISELInstructions) {
175 for (
const auto &
VI :
I.second)
189 return (std::prev(MBBI) == PrevPushedMBBI);
192 void PPCExpandISEL::expandAndMergeISELs() {
193 for (
auto &BlockList : ISELInstructions) {
194 DEBUG(
dbgs() <<
"Expanding ISEL instructions in BB#" << BlockList.first
197 BlockISELList &CurrentISELList = BlockList.second;
198 auto I = CurrentISELList.begin();
199 auto E = CurrentISELList.end();
202 BlockISELList SubISELList;
204 SubISELList.push_back(*
I++);
207 while (
I !=
E && canMerge(SubISELList.back(), *
I))
208 SubISELList.push_back(*
I++);
210 expandMergeableISELs(SubISELList);
215 void PPCExpandISEL::handleSpecialCases(BlockISELList &BIL,
217 IsTrueBlockRequired =
false;
218 IsFalseBlockRequired =
false;
220 auto MI = BIL.begin();
221 while (MI != BIL.end()) {
222 assert(isISEL(**MI) &&
"Expecting an ISEL instruction");
223 DEBUG(
dbgs() <<
"ISEL: " << **MI <<
"\n");
236 bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
237 bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
240 if (!IsADDIInstRequired && !IsORIInstRequired) {
241 DEBUG(
dbgs() <<
"Remove redudant ISEL instruction.");
243 (*MI)->eraseFromParent();
257 if (useSameRegister(TrueValue, FalseValue) && (BIL.size() == 1)) {
258 DEBUG(
dbgs() <<
"Fold the ISEL instruction to an unconditonal copy.");
260 BuildMI(*MBB, (*MI), dl,
TII->get(isISEL8(**MI) ? PPC::ADDI8 : PPC::ADDI))
264 (*MI)->eraseFromParent();
270 IsTrueBlockRequired |= IsADDIInstRequired;
271 IsFalseBlockRequired |= IsORIInstRequired;
276 void PPCExpandISEL::reorganizeBlockLayout(BlockISELList &BIL,
281 assert((IsTrueBlockRequired || IsFalseBlockRequired) &&
282 "Should have been handled by special cases earlier!");
292 ? MF->CreateMachineBasicBlock(LLVM_BB)
309 Successor = NewSuccessor;
314 if (IsFalseBlockRequired) {
315 FalseBlock = MF->CreateMachineBasicBlock(LLVM_BB);
316 MF->
insert(It, FalseBlock);
319 if (IsTrueBlockRequired) {
320 TrueBlock = MF->CreateMachineBasicBlock(LLVM_BB);
321 MF->insert(It, TrueBlock);
325 MF->insert(It, NewSuccessor);
328 NewSuccessor->splice(NewSuccessor->end(), MBB,
331 NewSuccessor->transferSuccessorsAndUpdatePHIs(MBB);
334 for (
auto &LI : MBB->
liveins())
335 NewSuccessor->addLiveIn(LI);
345 LPR.stepForward(MI, Clobbers);
347 NewSuccessor->addLiveIn(LI);
350 MBB->removeSuccessor(Successor);
356 MBB->addSuccessor(IsTrueBlockRequired ? TrueBlock : Successor);
357 MBB->addSuccessor(IsFalseBlockRequired ? FalseBlock : Successor);
359 if (IsTrueBlockRequired) {
360 TrueBlockI = TrueBlock->
begin();
361 TrueBlock->addSuccessor(Successor);
364 if (IsFalseBlockRequired) {
365 FalseBlockI = FalseBlock->begin();
366 FalseBlock->addSuccessor(Successor);
370 BuildMI(*MBB, BIL.back(), dl,
TII->get(PPC::BC))
371 .
add(BIL.back()->getOperand(3))
372 .addMBB(IsTrueBlockRequired ? TrueBlock : Successor);
375 BuildMI(*(IsFalseBlockRequired ? FalseBlock : MBB),
376 (IsFalseBlockRequired ? FalseBlockI : BIL.back()), dl,
380 if (IsFalseBlockRequired)
381 FalseBlockI = FalseBlock->begin();
384 void PPCExpandISEL::populateBlocks(BlockISELList &BIL) {
385 for (
auto &MI : BIL) {
386 assert(isISEL(*MI) &&
"Expecting an ISEL instruction");
395 DEBUG(
dbgs() <<
"Dest: " << Dest <<
"\n");
396 DEBUG(
dbgs() <<
"TrueValue: " << TrueValue <<
"\n");
397 DEBUG(
dbgs() <<
"FalseValue: " << FalseValue <<
"\n");
398 DEBUG(
dbgs() <<
"ConditionRegister: " << ConditionRegister <<
"\n");
403 bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
404 bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
406 if (IsADDIInstRequired) {
408 BuildMI(*TrueBlock, TrueBlockI, dl,
409 TII->get(isISEL8(*MI) ? PPC::ADDI8 : PPC::ADDI))
415 TrueBlock->addLiveIn(TrueValue.
getReg());
418 if (IsORIInstRequired) {
420 FalseBlock->addLiveIn(FalseValue.
getReg());
425 NewSuccessor->addLiveIn(Dest.
getReg());
426 NewSuccessor->addLiveIn(TrueValue.
getReg());
427 NewSuccessor->addLiveIn(FalseValue.
getReg());
428 NewSuccessor->addLiveIn(ConditionRegister.
getReg());
432 if (IsORIInstRequired)
433 BuildMI(*FalseBlock, FalseBlockI, dl,
434 TII->get(isISEL8(*MI) ? PPC::ORI8 : PPC::ORI))
445 void PPCExpandISEL::expandMergeableISELs(BlockISELList &BIL) {
449 handleSpecialCases(BIL, MBB);
450 reorganizeBlockLayout(BIL, MBB);
456 char PPCExpandISEL::
ID = 0;
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
iterator_range< livein_iterator > liveins() const
STATISTIC(NumFunctions,"Total number of functions")
iterator_range< succ_iterator > successors()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const MachineOperand & getOperand(unsigned i) const
FunctionPass class - This class is used to implement most global optimizations.
self_iterator getIterator()
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
void dump() const
dump - Print the current MachineFunction to cerr, useful for debugger use.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const PPCRegisterInfo * getRegisterInfo() const override
Iterator for intrusive lists based on ilist_node.
void initializePPCExpandISELPass(PassRegistry &)
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringRef > StandardNames)
initialize - Initialize the set of available library functions based on the specified target triple...
Representation of each machine instruction.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
static MachineOperand CreateImm(int64_t Val)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
static cl::opt< bool > GenerateISEL("ppc-gen-isel", cl::desc("Enable generating the ISEL instruction."), cl::init(true), cl::Hidden)
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
FunctionPass * createPPCExpandISELPass()