Skip to content

Commit 1e506a0

Browse files
vladimirradosavljevicakiramenai
authored andcommitted
[CGP] Disable optimizeMemoryInst only for EraVM
This enables optimization for EVM and implements isLegalAddressingMode, allowing r + imm addressing mode for all address spaces except CALLDATALOAD. Benchmarks indicate that enabling it for CALLDATALOAD is not beneficial. This change primarily allows address mode calculations (r + imm) to be sunk to their uses, which can reduce spills and reloads in some cases. Signed-off-by: Vladimir Radosavljevic <[email protected]>
1 parent d6b7b65 commit 1e506a0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

llvm/lib/Target/EVM/EVMISelLowering.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ const char *EVMTargetLowering::getTargetNodeName(unsigned Opcode) const {
110110
return nullptr;
111111
}
112112

113+
bool EVMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
114+
const AddrMode &AM, Type *Ty,
115+
unsigned AS,
116+
Instruction *I) const {
117+
// We don't support global and scaled addresses.
118+
if (AM.BaseGV || AM.Scale)
119+
return false;
120+
121+
// For CALLDATALOAD, benchmark numbers showed it is not profitable to set
122+
// that we support r + imm.
123+
if (AS == EVMAS::AS_CALL_DATA)
124+
return !(AM.HasBaseReg && AM.BaseOffs);
125+
126+
// Allow r + imm addressing mode for other address spaces. This can help
127+
// to reduce register pressure by sinking add with immediate to its uses,
128+
// minimizing the need for extra registers and reducing spills and reloads.
129+
return true;
130+
}
131+
113132
//===----------------------------------------------------------------------===//
114133
// EVM Lowering private implementation.
115134
//===----------------------------------------------------------------------===//

llvm/lib/Target/EVM/EVMISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class EVMTargetLowering final : public TargetLowering {
4040
/// DAG node.
4141
const char *getTargetNodeName(unsigned Opcode) const override;
4242

43+
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
44+
unsigned AS,
45+
Instruction *I = nullptr) const override;
46+
4347
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
4448
EVT VT) const override {
4549
return MVT::i256;

llvm/test/CodeGen/EVM/cgp-sink-addrmode.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ define i256 @test_as1(i256 %arg) {
1010
; CHECK-LABEL: define i256 @test_as1(
1111
; CHECK-SAME: i256 [[ARG:%.*]]) {
1212
; CHECK-NEXT: [[ENTRY:.*]]:
13-
; CHECK-NEXT: [[ADD:%.*]] = add i256 [[ARG]], 4
1413
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i256 [[ARG]], 10
1514
; CHECK-NEXT: br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
1615
; CHECK: [[THEN]]:
17-
; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i256 [[ADD]] to ptr addrspace(1)
16+
; CHECK-NEXT: [[SUNKADDR:%.*]] = inttoptr i256 [[ARG]] to ptr addrspace(1)
17+
; CHECK-NEXT: [[INTTOPTR:%.*]] = getelementptr i8, ptr addrspace(1) [[SUNKADDR]], i256 4
1818
; CHECK-NEXT: [[LOAD:%.*]] = load i256, ptr addrspace(1) [[INTTOPTR]], align 1
1919
; CHECK-NEXT: br label %[[EXIT]]
2020
; CHECK: [[EXIT]]:
@@ -40,11 +40,11 @@ define i256 @test_as5(i256 %arg) {
4040
; CHECK-LABEL: define i256 @test_as5(
4141
; CHECK-SAME: i256 [[ARG:%.*]]) {
4242
; CHECK-NEXT: [[ENTRY:.*]]:
43-
; CHECK-NEXT: [[ADD:%.*]] = add i256 [[ARG]], 4
4443
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i256 [[ARG]], 10
4544
; CHECK-NEXT: br i1 [[CMP]], label %[[THEN:.*]], label %[[EXIT:.*]]
4645
; CHECK: [[THEN]]:
47-
; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i256 [[ADD]] to ptr addrspace(5)
46+
; CHECK-NEXT: [[SUNKADDR:%.*]] = inttoptr i256 [[ARG]] to ptr addrspace(5)
47+
; CHECK-NEXT: [[INTTOPTR:%.*]] = getelementptr i8, ptr addrspace(5) [[SUNKADDR]], i256 4
4848
; CHECK-NEXT: [[LOAD:%.*]] = load i256, ptr addrspace(5) [[INTTOPTR]], align 1
4949
; CHECK-NEXT: br label %[[EXIT]]
5050
; CHECK: [[EXIT]]:

0 commit comments

Comments
 (0)