[PATCH] D109124: [ARM] Implement target hook function to decide folding (mul (add x, c1), c2)
Ben Shi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 2 04:44:10 PDT 2021
benshi001 updated this revision to Diff 370230.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D109124/new/
https://reviews.llvm.org/D109124
Files:
llvm/lib/Target/ARM/ARMISelLowering.cpp
llvm/lib/Target/ARM/ARMISelLowering.h
llvm/test/CodeGen/ARM/addimm-mulimm.ll
Index: llvm/test/CodeGen/ARM/addimm-mulimm.ll
===================================================================
--- llvm/test/CodeGen/ARM/addimm-mulimm.ll
+++ llvm/test/CodeGen/ARM/addimm-mulimm.ll
@@ -123,10 +123,9 @@
;
; CHECKV7-LABEL: fold_addimm_mulimm_i:
; CHECKV7: @ %bb.0:
-; CHECKV7-NEXT: movw r1, #23067
-; CHECKV7-NEXT: movw r2, #353
-; CHECKV7-NEXT: movt r1, #1
-; CHECKV7-NEXT: mla r0, r0, r2, r1
+; CHECKV7-NEXT: add r0, r0, #251
+; CHECKV7-NEXT: movw r1, #353
+; CHECKV7-NEXT: mul r0, r0, r1
; CHECKV7-NEXT: bx lr
%b = add i32 %a, 251
%c = mul i32 %b, 353
@@ -148,10 +147,9 @@
;
; CHECKV7-LABEL: fold_addimm_mulimm_j:
; CHECKV7: @ %bb.0:
-; CHECKV7-NEXT: movw r1, #42469
-; CHECKV7-NEXT: movw r2, #353
-; CHECKV7-NEXT: movt r1, #65534
-; CHECKV7-NEXT: mla r0, r0, r2, r1
+; CHECKV7-NEXT: sub r0, r0, #251
+; CHECKV7-NEXT: movw r1, #353
+; CHECKV7-NEXT: mul r0, r0, r1
; CHECKV7-NEXT: bx lr
%b = add i32 %a, -251
%c = mul i32 %b, 353
Index: llvm/lib/Target/ARM/ARMISelLowering.h
===================================================================
--- llvm/lib/Target/ARM/ARMISelLowering.h
+++ llvm/lib/Target/ARM/ARMISelLowering.h
@@ -518,6 +518,9 @@
const APInt &DemandedElts,
TargetLoweringOpt &TLO) const override;
+ bool isMulAddWithConstProfitable(const SDValue &AddNode,
+ const SDValue &ConstNode) const override;
+
bool ExpandInlineAsm(CallInst *CI) const override;
ConstraintType getConstraintType(StringRef Constraint) const override;
Index: llvm/lib/Target/ARM/ARMISelLowering.cpp
===================================================================
--- llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -18769,6 +18769,39 @@
return AbsImm >= 0 && AbsImm <= 255;
}
+// Return false to prevent folding
+// (mul (add r, c0), c1) -> (add (mul r, c1), c0*c1) in DAGCombine,
+// if the folding leads to worse code.
+bool ARMTargetLowering::isMulAddWithConstProfitable(
+ const SDValue &AddNode, const SDValue &ConstNode) const {
+ // TODO: Implement this hook for thumb.
+ if (Subtarget->isThumb())
+ return true;
+
+ // Let the DAGCombiner decide for vector types and large types.
+ const EVT VT = AddNode.getValueType();
+ if (VT.isVector() || VT.getScalarSizeInBits() > 32)
+ return true;
+
+ // It is worse if c0 is legal add immediate, while c1*c0 is not,
+ // and has to be composed by at least two instructions. That means c0*c1
+ // can not be obtained via a single MOV/MVN, and can not be
+ // obtained via a single MOVW (on ARMv6t2 and above).
+ const ConstantSDNode *C0Node = cast<ConstantSDNode>(AddNode.getOperand(1));
+ const ConstantSDNode *C1Node = cast<ConstantSDNode>(ConstNode);
+ const int64_t C0 = C0Node->getSExtValue();
+ const int64_t C1 = C1Node->getSExtValue();
+ if (isLegalAddImmediate(C0) && !isLegalAddImmediate(C0 * C1) &&
+ !isLegalAddImmediate(~std::abs(C0 * C1))) {
+ // TODO: Check for subtargets without movw/movt.
+ if (Subtarget->useMovt() && std::abs(C0 * C1) > 0xffff)
+ return false;
+ }
+
+ // Default to true and let the DAGCombiner decide.
+ return true;
+}
+
static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
bool isSEXTLoad, SDValue &Base,
SDValue &Offset, bool &isInc,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109124.370230.patch
Type: text/x-patch
Size: 3520 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210902/4c2aed7f/attachment.bin>
More information about the llvm-commits
mailing list