[llvm] [LoongArch] Implement COPY instruction between CFRs (PR #69300)
Xi Ruoyao via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 17 02:46:22 PDT 2023
================
@@ -513,15 +515,134 @@ bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
return true;
}
+class LoongArchExpandPseudo : public MachineFunctionPass {
+public:
+ const LoongArchInstrInfo *TII;
+ static char ID;
+
+ LoongArchExpandPseudo() : MachineFunctionPass(ID) {
+ initializeLoongArchExpandPseudoPass(*PassRegistry::getPassRegistry());
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ StringRef getPassName() const override {
+ return LOONGARCH_EXPAND_PSEUDO_NAME;
+ }
+
+private:
+ bool expandMBB(MachineBasicBlock &MBB);
+ bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+ MachineBasicBlock::iterator &NextMBBI);
+ bool expandCopyCFR(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+ MachineBasicBlock::iterator &NextMBBI);
+};
+
+char LoongArchExpandPseudo::ID = 0;
+
+bool LoongArchExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
+ TII =
+ static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
+
+ bool Modified = false;
+ for (auto &MBB : MF)
+ Modified |= expandMBB(MBB);
+
+ return Modified;
+}
+
+bool LoongArchExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
+ bool Modified = false;
+
+ MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
+ while (MBBI != E) {
+ MachineBasicBlock::iterator NMBBI = std::next(MBBI);
+ Modified |= expandMI(MBB, MBBI, NMBBI);
+ MBBI = NMBBI;
+ }
+
+ return Modified;
+}
+
+bool LoongArchExpandPseudo::expandMI(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator MBBI,
+ MachineBasicBlock::iterator &NextMBBI) {
+ switch (MBBI->getOpcode()) {
+ case LoongArch::PseudoCopyCFR:
+ return expandCopyCFR(MBB, MBBI, NextMBBI);
+ }
+
+ return false;
+}
+
+bool LoongArchExpandPseudo::expandCopyCFR(
+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+ MachineBasicBlock::iterator &NextMBBI) {
+ MachineFunction *MF = MBB.getParent();
+ MachineInstr &MI = *MBBI;
+ DebugLoc DL = MI.getDebugLoc();
+
+ // Expand:
+ // MBB:
+ // fcmp.caf.s $dst, $fa0, $fa0 # set $dst 0(false)
----------------
xry111 wrote:
Phew:
```
$ cat t.S
.globl main
main:
li.w $a0, 1000000
.L0:
.rept 100
#if USE_MOVGR2CF
movgr2cf $fcc0, $r0
#else
fcmp.caf.s $fcc0, $f0, $f0
#endif
.endr
addi.w $a0, $a0, -1
bnez $a0, .L0
li.w $a0, 0
jr $ra
$ gcc t.S -DUSE_MOVGR2CF
$ time ./a.out
real 0m0.688s
user 0m0.687s
sys 0m0.001s
$ gcc t.S
$ time ./a.out
real 0m0.024s
user 0m0.023s
sys 0m0.000s
```
So `fcmp.caf.s` is indeed better...
https://github.com/llvm/llvm-project/pull/69300
More information about the llvm-commits
mailing list