LLVM 23.0.0git
WebAssemblyLegalizerInfo.cpp
Go to the documentation of this file.
1//===- WebAssemblyLegalizerInfo.cpp ------------------------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the Machinelegalizer class for
10/// WebAssembly.
11//===----------------------------------------------------------------------===//
12
18
19#define DEBUG_TYPE "wasm-legalinfo"
20
21using namespace llvm;
22using namespace LegalizeActions;
23
25 const WebAssemblySubtarget &ST) {
26 using namespace TargetOpcode;
27
28 const LLT s32 = LLT::scalar(32);
29 const LLT s64 = LLT::scalar(64);
30
31 getActionDefinitionsBuilder({G_CONSTANT, G_IMPLICIT_DEF, G_ADD, G_SUB, G_MUL,
32 G_UDIV, G_SDIV, G_UREM, G_SREM, G_AND, G_OR,
33 G_XOR})
34 .legalFor({s32, s64})
35 .widenScalarToNextPow2(0)
36 .clampScalar(0, s32, s64);
37
38 getActionDefinitionsBuilder({G_ASHR, G_LSHR, G_SHL})
39 .legalFor({{s32, s32}, {s64, s64}})
40 .widenScalarToNextPow2(0)
41 .clampScalar(0, s32, s64)
42 .scalarSameSizeAs(1, 0);
43
44 getActionDefinitionsBuilder({G_CTLZ, G_CTTZ, G_CTPOP})
45 .legalFor({{s32, s32}, {s64, s64}})
46 .widenScalarToNextPow2(1)
47 .clampScalar(1, s32, s64)
48 .scalarSameSizeAs(0, 1);
49
50 getActionDefinitionsBuilder({G_CTLZ_ZERO_UNDEF, G_CTTZ_ZERO_UNDEF}).lower();
51
52 getActionDefinitionsBuilder({G_ROTL, G_ROTR})
53 .legalFor({{s32, s32}, {s64, s64}})
54 .scalarSameSizeAs(1, 0)
55 .lower();
56
57 getActionDefinitionsBuilder({G_FSHL, G_FSHR}).lower();
58
59 getActionDefinitionsBuilder({G_ANYEXT, G_SEXT, G_ZEXT})
60 .legalFor({{s64, s32}})
61 .clampScalar(0, s64, s64)
62 .clampScalar(1, s32, s32);
63
65 .legalFor({{s32, s64}})
66 .clampScalar(0, s32, s32)
67 .clampScalar(1, s64, s64);
68
69 getActionDefinitionsBuilder(G_SEXT_INREG)
70 .customFor(ST.hasSignExt(), {s32, s64})
71 .clampScalar(0, s32, s64)
72 .lower();
73
75}
76
79 LostDebugLocObserver &LocObserver) const {
80 switch (MI.getOpcode()) {
81 case TargetOpcode::G_SEXT_INREG: {
82 assert(MI.getOperand(2).isImm() && "Expected immediate");
83
84 // Mark only 8/16/32-bit SEXT_INREG as legal
85 auto [DstType, SrcType] = MI.getFirst2LLTs();
86 auto ExtFromWidth = MI.getOperand(2).getImm();
87
88 if (ExtFromWidth == 8 || ExtFromWidth == 16 ||
89 (DstType.getScalarSizeInBits() == 64 && ExtFromWidth == 32)) {
90 return true;
91 }
92
93 return Helper.lower(MI, 0, DstType) != LegalizerHelper::UnableToLegalize;
94 }
95 default:
96 break;
97 }
98 return false;
99}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
Interface for Targets to specify which operations they can successfully select and how the others sho...
This file declares the targeting of the Machinelegalizer class for WebAssembly.
This file provides WebAssembly-specific target descriptions.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
LLVM_ABI void computeTables()
Compute any ancillary tables needed to quickly decide how an operation should be handled.
LegalizeRuleSet & legalFor(std::initializer_list< LLT > Types)
The instruction is legal when type index 0 is any type in the given list.
LegalizeRuleSet & scalarSameSizeAs(unsigned TypeIdx, unsigned SameSizeIdx)
Change the type TypeIdx to have the same scalar size as type SameSizeIdx.
LegalizeRuleSet & lower()
The instruction is lowered.
LegalizeRuleSet & clampScalar(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the range of scalar sizes to MinTy and MaxTy.
LegalizeRuleSet & customFor(std::initializer_list< LLT > Types)
@ UnableToLegalize
Some kind of error has occurred and we could not legalize this instruction.
LLVM_ABI LegalizeResult lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty)
Legalize an instruction by splitting it into simpler parts, hopefully understood by the target.
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
const LegacyLegalizerInfo & getLegacyLegalizerInfo() const
Representation of each machine instruction.
bool legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI, LostDebugLocObserver &LocObserver) const override
Called for instructions with the Custom LegalizationAction.
WebAssemblyLegalizerInfo(const WebAssemblySubtarget &ST)
Invariant opcodes: All instruction sets have these as their low opcodes.
This is an optimization pass for GlobalISel generic memory operations.