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");
41 cl::desc(
"Enable generating the ISEL instruction."),
49 bool IsTrueBlockRequired;
50 bool IsFalseBlockRequired;
63 ISELInstructionList ISELInstructions;
70 void populateBlocks(BlockISELList &BIL);
71 void expandMergeableISELs(BlockISELList &BIL);
72 void expandAndMergeISELs();
99 bool collectISELInstructions();
124 void DumpISELInstructions()
const;
131 if (!collectISELInstructions()) {
137 DumpISELInstructions();
140 expandAndMergeISELs();
150 ISELInstructions.clear();
157 bool PPCExpandISEL::collectISELInstructions() {
159 BlockISELList thisBlockISELs;
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) {
174 for (
const auto &
VI :
I.second)
188 return (std::prev(MBBI) == PrevPushedMBBI);
191 void PPCExpandISEL::expandAndMergeISELs() {
192 bool ExpandISELEnabled = isExpandISELEnabled(*MF);
194 for (
auto &BlockList : ISELInstructions) {
196 dbgs() <<
"Expanding ISEL instructions in " 199 BlockISELList &CurrentISELList = BlockList.second;
200 auto I = CurrentISELList.begin();
201 auto E = CurrentISELList.end();
204 assert(isISEL(**
I) &&
"Expecting an ISEL instruction");
212 if (useSameRegister(Dest, TrueValue) &&
213 useSameRegister(Dest, FalseValue)) {
220 (*I)->eraseFromParent();
222 }
else if (useSameRegister(TrueValue, FalseValue)) {
229 dbgs() <<
"Fold the ISEL instruction to an unconditional copy:\n");
238 (*I)->eraseFromParent();
240 }
else if (ExpandISELEnabled) {
243 BlockISELList SubISELList;
244 SubISELList.push_back(*
I++);
249 while (
I !=
E && canMerge(SubISELList.back(), *
I)) {
251 SubISELList.push_back(*
I++);
254 expandMergeableISELs(SubISELList);
262 void PPCExpandISEL::handleSpecialCases(BlockISELList &BIL,
264 IsTrueBlockRequired =
false;
265 IsFalseBlockRequired =
false;
267 auto MI = BIL.begin();
268 while (MI != BIL.end()) {
269 assert(isISEL(**MI) &&
"Expecting an ISEL instruction");
282 bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
283 bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
286 if (!IsADDIInstRequired && !IsORIInstRequired) {
292 (*MI)->eraseFromParent();
305 if (useSameRegister(TrueValue, FalseValue) && (BIL.size() == 1)) {
307 dbgs() <<
"Fold the ISEL instruction to an unconditional copy.");
311 BuildMI(*MBB, (*MI), dl, TII->
get(isISEL8(**MI) ? PPC::OR8 : PPC::OR))
315 (*MI)->eraseFromParent();
321 IsTrueBlockRequired |= IsADDIInstRequired;
322 IsFalseBlockRequired |= IsORIInstRequired;
327 void PPCExpandISEL::reorganizeBlockLayout(BlockISELList &BIL,
332 assert((IsTrueBlockRequired || IsFalseBlockRequired) &&
333 "Should have been handled by special cases earlier!");
343 ? MF->CreateMachineBasicBlock(LLVM_BB)
360 Successor = NewSuccessor;
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);
379 NewSuccessor->
splice(NewSuccessor->
end(), MBB,
385 for (
auto &LI : MBB->
liveins())
396 LPR.stepForward(MI, Clobbers);
401 MBB->removeSuccessor(Successor);
407 MBB->addSuccessor(IsTrueBlockRequired ? TrueBlock : Successor);
408 MBB->addSuccessor(IsFalseBlockRequired ? FalseBlock : Successor);
410 if (IsTrueBlockRequired) {
411 TrueBlockI = TrueBlock->
begin();
415 if (IsFalseBlockRequired) {
416 FalseBlockI = FalseBlock->
begin();
421 BuildMI(*MBB, BIL.back(), dl, TII->
get(PPC::BC))
422 .
add(BIL.back()->getOperand(3))
423 .addMBB(IsTrueBlockRequired ? TrueBlock : Successor);
426 BuildMI(*(IsFalseBlockRequired ? FalseBlock : MBB),
427 (IsFalseBlockRequired ? FalseBlockI : BIL.back()), dl,
431 if (IsFalseBlockRequired)
432 FalseBlockI = FalseBlock->
begin();
435 void PPCExpandISEL::populateBlocks(BlockISELList &BIL) {
436 for (
auto &MI : BIL) {
437 assert(isISEL(*MI) &&
"Expecting an ISEL instruction");
449 LLVM_DEBUG(
dbgs() <<
"ConditionRegister: " << ConditionRegister <<
"\n");
453 bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
454 bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
456 if (IsADDIInstRequired) {
458 BuildMI(*TrueBlock, TrueBlockI, dl,
459 TII->
get(isISEL8(*MI) ? PPC::ADDI8 : PPC::ADDI))
468 if (IsORIInstRequired) {
482 if (IsORIInstRequired)
483 BuildMI(*FalseBlock, FalseBlockI, dl,
484 TII->
get(isISEL8(*MI) ? PPC::ORI8 : PPC::ORI))
495 void PPCExpandISEL::expandMergeableISELs(BlockISELList &BIL) {
499 handleSpecialCases(BIL, MBB);
500 reorganizeBlockLayout(BIL, MBB);
506 char PPCExpandISEL::ID = 0;
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
STATISTIC(NumFunctions, "Total number of functions")
void dump() const
dump - Print the current MachineFunction to cerr, useful for debugger use.
iterator_range< succ_iterator > successors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
virtual const TargetInstrInfo * getInstrInfo() const
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
TargetInstrInfo - Interface to description of machine instruction set.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
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)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const PPCRegisterInfo * getRegisterInfo() const override
Iterator for intrusive lists based on ilist_node.
void initializePPCExpandISELPass(PassRegistry &)
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const MachineBasicBlock * getParent() const
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
A set of physical registers with utility functions to track liveness when walking backward/forward th...
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
static MachineOperand CreateImm(int64_t Val)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
iterator_range< livein_iterator > liveins() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static cl::opt< bool > GenerateISEL("ppc-gen-isel", cl::desc("Enable generating the ISEL instruction."), cl::init(true), cl::Hidden)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
FunctionPass * createPPCExpandISELPass()