aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Nacke <kai@redstar.de>2022-08-22 22:05:50 -0400
committerKai Nacke <kai@redstar.de>2022-11-13 11:07:51 -0500
commitc36128019d2e008e138f9e069b570ba3abaaf1aa (patch)
treea418bf5675381c6742a2ba4c6a558804eb18dc9b
parent667bbc2319cbb064666a4bc79343f42504d0b618 (diff)
downloadllvm-c36128019d2e008e138f9e069b570ba3abaaf1aa.zip
llvm-c36128019d2e008e138f9e069b570ba3abaaf1aa.tar.gz
llvm-c36128019d2e008e138f9e069b570ba3abaaf1aa.tar.bz2
[m88k] Enable further matching of immediates
The matcher generated from the SDAG patterns does not honor the isCommutable flag. An easy solution is to swap the operands if the instruction is commutable and the first operand is constant.
-rw-r--r--llvm/lib/Target/M88k/GISel/M88kPostLegalizerLowering.cpp17
-rw-r--r--llvm/test/CodeGen/M88k/add.ll20
2 files changed, 37 insertions, 0 deletions
diff --git a/llvm/lib/Target/M88k/GISel/M88kPostLegalizerLowering.cpp b/llvm/lib/Target/M88k/GISel/M88kPostLegalizerLowering.cpp
index 49b7c84..aa29756 100644
--- a/llvm/lib/Target/M88k/GISel/M88kPostLegalizerLowering.cpp
+++ b/llvm/lib/Target/M88k/GISel/M88kPostLegalizerLowering.cpp
@@ -394,6 +394,23 @@ bool M88kPostLegalizerLoweringInfo::combine(GISelChangeObserver &Observer,
MachineIRBuilder &B) const {
M88kGenPostLegalizerLoweringHelper Generated(GeneratedRuleCfg);
+ // If the instruction is commutable and the first operand is a constant, then
+ // swap the operands. The matcher generated from the SDAG patterns expects the
+ // constant always as the second operand, otherwise operand is not matched as
+ // immediate.
+ if (MI.isCommutable()) {
+ assert(MI.getNumExplicitOperands() == 3 && "Not a binary operation");
+ unsigned SrcOpc =
+ getDefIgnoringCopies(MI.getOperand(1).getReg(), *B.getMRI())
+ ->getOpcode();
+ if (SrcOpc == TargetOpcode::G_CONSTANT ||
+ SrcOpc == TargetOpcode::G_FCONSTANT) {
+ Observer.changingInstr(MI);
+ B.getTII().commuteInstruction(MI, false, 1, 2);
+ Observer.changedInstr(MI);
+ }
+ }
+
if (Generated.tryCombineAll(Observer, MI, B, KB, ReplaceSignedDiv,
AddZeroDivCheck))
return true;
diff --git a/llvm/test/CodeGen/M88k/add.ll b/llvm/test/CodeGen/M88k/add.ll
new file mode 100644
index 0000000..3e15d9e
--- /dev/null
+++ b/llvm/test/CodeGen/M88k/add.ll
@@ -0,0 +1,20 @@
+; Test addition/subtraction.
+;
+; RUN: llc < %s -mtriple=m88k-openbsd -mcpu=mc88100 -verify-machineinstrs -m88k-enable-delay-slot-filler=false | FileCheck %s
+; RUN: llc < %s -mtriple=m88k-openbsd -mcpu=mc88110 -verify-machineinstrs -m88k-enable-delay-slot-filler=false | FileCheck %s
+
+define i32 @f1(i32 %a) {
+; CHECK-LABEL: f1:
+; CHECK: addu %r2, %r2, 512
+; CHECK-NEXT: jmp %r1
+ %sum = add i32 %a, 512
+ ret i32 %sum
+}
+
+define i32 @f2(i32 %a) {
+; CHECK-LABEL: f2:
+; CHECK: addu %r2, %r2, 512
+; CHECK-NEXT: jmp %r1
+ %sum = add i32 512, %a
+ ret i32 %sum
+}