Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion llvm/lib/Target/EVM/EVMCodegenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsEVM.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"

#include "EVM.h"

using namespace llvm;
using namespace llvm::PatternMatch;

#define DEBUG_TYPE "evm-codegen-prepare"

Expand Down Expand Up @@ -105,7 +108,7 @@ void EVMCodegenPrepare::processMemTransfer(MemTransferInst *M) {
bool EVMCodegenPrepare::runOnFunction(Function &F) {
bool Changed = false;
for (auto &BB : F) {
for (auto &I : BB) {
for (auto &I : make_early_inc_range(BB)) {
if (auto *M = dyn_cast<MemTransferInst>(&I)) {
processMemTransfer(M);
Changed = true;
Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/Target/EVM/EVMInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -1210,3 +1210,26 @@ let isCodeGenOnly = 1, hasSideEffects = 1, isNotDuplicable = 1 in
def PUSHDEPLOYADDRESS
: NRI<(outs GPR:$res), (ins), [(set GPR:$res, (int_evm_pushdeployaddress))],
"PUSHDEPLOYADDRESS $res">;

// Constant predicate: C % 8 == 0 and C <= 255
def ShAmtMul8U255 : PatLeaf<(imm), [{
auto *CN = dyn_cast<ConstantSDNode>(N);
if (!CN) return false;
const APInt &V = CN->getAPIntValue();
return V.ule(255) && (V.urem(8) == 0);
}]>;

// Transform shift-bits C -> byteIndex = (C/8) - 1
def CToByteIndex : SDNodeXForm<imm, [{
auto *CN = cast<ConstantSDNode>(N);
uint64_t C = CN->getZExtValue();
uint64_t idx = (256 - C) / 8 - 1;
return CurDAG->getTargetConstant(idx, SDLoc(N), MVT::i256);
}]>;

// Fold: (sra C, (shl C, x)) ==> SIGNEXTEND(byteIndex(C), x)
// Note: generic DAG uses 'sra'/'shl'; EVM op is SIGNEXTEND(byteIndex, value).
def : Pat<
(sra (shl GPR:$x, ShAmtMul8U255:$C), ShAmtMul8U255:$C),
(SIGNEXTEND (CONST_I256 (CToByteIndex ShAmtMul8U255:$C)), GPR:$x)
>;
2 changes: 1 addition & 1 deletion llvm/lib/Target/EVM/EVMTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ bool EVMPassConfig::addPreISel() {
}

void EVMPassConfig::addCodeGenPrepare() {
addPass(createEVMCodegenPreparePass());
TargetPassConfig::addCodeGenPrepare();
addPass(createEVMCodegenPreparePass());
}

bool EVMPassConfig::addInstSelector() {
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/Target/EVM/EVMTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ using namespace llvm::PatternMatch;

static std::optional<Instruction *> instCombineSignExtend(InstCombiner &IC,
IntrinsicInst &II) {
unsigned BitWidth = II.getType()->getIntegerBitWidth();
if (BitWidth != 256)
return std::nullopt;

// Unfold signextend(c, x) ->
// ashr(shl(x, 256 - (c + 1) * 8), 256 - (c + 1) * 8)
// where c is a constant integer.
ConstantInt *C = nullptr;
if (match(II.getArgOperand(0), m_ConstantInt(C))) {
const APInt &B = C->getValue();

// If the signextend is larger than 31 bits, leave constant
// folding to handle it.
if (B.uge(APInt(BitWidth, (BitWidth / 8) - 1)))
return std::nullopt;

unsigned ShiftAmt = BitWidth - ((B.getZExtValue() + 1) * 8);
auto *Shl = IC.Builder.CreateShl(II.getArgOperand(1), ShiftAmt);
auto *Ashr = IC.Builder.CreateAShr(Shl, ShiftAmt);
return IC.replaceInstUsesWith(II, Ashr);
}

// Fold signextend(b, signextend(b, x)) -> signextend(b, x)
Value *B = nullptr, *X = nullptr;
if (match(&II, m_Intrinsic<Intrinsic::evm_signextend>(
Expand Down
4 changes: 1 addition & 3 deletions llvm/test/CodeGen/EVM/O3-pipeline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ target triple = "evm"
; CHECK-NEXT: Expand reduction intrinsics
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: TLS Variable Hoist
; CHECK-NEXT: Final transformations before code generation
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Final transformations before code generation
; CHECK-NEXT: Lower invoke and unwind, for unwindless code generators
; CHECK-NEXT: Remove unreachable blocks from the CFG
; CHECK-NEXT: CallGraph Construction
Expand Down
7 changes: 4 additions & 3 deletions llvm/test/CodeGen/EVM/fold-signextend.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ target triple = "evm"
define i256 @test_const(i256 %x) {
; CHECK-LABEL: define i256 @test_const(
; CHECK-SAME: i256 [[X:%.*]]) {
; CHECK-NEXT: [[SIGNEXT1:%.*]] = call i256 @llvm.evm.signextend(i256 15, i256 [[X]])
; CHECK-NEXT: [[TMP1:%.*]] = shl i256 [[X]], 128
; CHECK-NEXT: [[SIGNEXT1:%.*]] = ashr exact i256 [[TMP1]], 128
; CHECK-NEXT: ret i256 [[SIGNEXT1]]
;
%signext1 = call i256 @llvm.evm.signextend(i256 15, i256 %x)
Expand All @@ -18,8 +19,8 @@ define i256 @test_const(i256 %x) {
define i256 @test_const_ne(i256 %x) {
; CHECK-LABEL: define i256 @test_const_ne(
; CHECK-SAME: i256 [[X:%.*]]) {
; CHECK-NEXT: [[SIGNEXT1:%.*]] = call i256 @llvm.evm.signextend(i256 15, i256 [[X]])
; CHECK-NEXT: [[SIGNEXT2:%.*]] = call i256 @llvm.evm.signextend(i256 10, i256 [[SIGNEXT1]])
; CHECK-NEXT: [[TMP1:%.*]] = shl i256 [[X]], 168
; CHECK-NEXT: [[SIGNEXT2:%.*]] = ashr exact i256 [[TMP1]], 168
; CHECK-NEXT: ret i256 [[SIGNEXT2]]
;
%signext1 = call i256 @llvm.evm.signextend(i256 15, i256 %x)
Expand Down
Loading