1 //===- MipsLegalizerInfo.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 Mips.
10 /// \todo This should be generated by TableGen.
11 //===----------------------------------------------------------------------===//
12 
13 #include "MipsLegalizerInfo.h"
14 #include "MipsTargetMachine.h"
15 #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
16 
17 using namespace llvm;
18 
19 MipsLegalizerInfo::MipsLegalizerInfo(const MipsSubtarget &ST) {
20   using namespace TargetOpcode;
21 
22   const LLT s1 = LLT::scalar(1);
23   const LLT s32 = LLT::scalar(32);
24   const LLT s64 = LLT::scalar(64);
25   const LLT p0 = LLT::pointer(0, 32);
26 
27   getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL})
28       .legalFor({s32})
29       .clampScalar(0, s32, s32);
30 
31   getActionDefinitionsBuilder({G_UADDO, G_UADDE, G_USUBO, G_USUBE, G_UMULO})
32       .lowerFor({{s32, s1}});
33 
34   getActionDefinitionsBuilder(G_UMULH)
35       .legalFor({s32})
36       .maxScalar(0, s32);
37 
38   getActionDefinitionsBuilder({G_LOAD, G_STORE})
39       .legalForTypesWithMemDesc({{s32, p0, 8, 8},
40                                  {s32, p0, 16, 8},
41                                  {s32, p0, 32, 8},
42                                  {s64, p0, 64, 8},
43                                  {p0, p0, 32, 8}})
44       .minScalar(0, s32);
45 
46   getActionDefinitionsBuilder(G_UNMERGE_VALUES)
47      .legalFor({{s32, s64}});
48 
49   getActionDefinitionsBuilder(G_MERGE_VALUES)
50      .legalFor({{s64, s32}});
51 
52   getActionDefinitionsBuilder({G_ZEXTLOAD, G_SEXTLOAD})
53     .legalForTypesWithMemDesc({{s32, p0, 8, 8},
54                                {s32, p0, 16, 8}})
55       .minScalar(0, s32);
56 
57   getActionDefinitionsBuilder(G_SELECT)
58       .legalForCartesianProduct({p0, s32, s64}, {s32})
59       .minScalar(0, s32)
60       .minScalar(1, s32);
61 
62   getActionDefinitionsBuilder(G_BRCOND)
63       .legalFor({s32})
64       .minScalar(0, s32);
65 
66   getActionDefinitionsBuilder(G_BRJT)
67       .legalFor({{p0, s32}});
68 
69   getActionDefinitionsBuilder(G_PHI)
70       .legalFor({p0, s32, s64})
71       .minScalar(0, s32);
72 
73   getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
74       .legalFor({s32})
75       .clampScalar(0, s32, s32);
76 
77   getActionDefinitionsBuilder({G_SDIV, G_SREM, G_UREM, G_UDIV})
78       .legalFor({s32})
79       .minScalar(0, s32)
80       .libcallFor({s64});
81 
82   getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
83     .legalFor({s32, s32})
84     .minScalar(1, s32);
85 
86   getActionDefinitionsBuilder(G_ICMP)
87       .legalForCartesianProduct({s32}, {s32, p0})
88       .clampScalar(1, s32, s32)
89       .minScalar(0, s32);
90 
91   getActionDefinitionsBuilder(G_CONSTANT)
92       .legalFor({s32})
93       .clampScalar(0, s32, s32);
94 
95   getActionDefinitionsBuilder({G_GEP, G_INTTOPTR})
96       .legalFor({{p0, s32}});
97 
98   getActionDefinitionsBuilder(G_PTRTOINT)
99       .legalFor({{s32, p0}});
100 
101   getActionDefinitionsBuilder(G_FRAME_INDEX)
102       .legalFor({p0});
103 
104   getActionDefinitionsBuilder({G_GLOBAL_VALUE, G_JUMP_TABLE})
105       .legalFor({p0});
106 
107   // FP instructions
108   getActionDefinitionsBuilder(G_FCONSTANT)
109       .legalFor({s32, s64});
110 
111   getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FABS, G_FSQRT})
112       .legalFor({s32, s64});
113 
114   getActionDefinitionsBuilder(G_FCMP)
115       .legalFor({{s32, s32}, {s32, s64}})
116       .minScalar(0, s32);
117 
118   getActionDefinitionsBuilder({G_FCEIL, G_FFLOOR})
119       .libcallFor({s32, s64});
120 
121   getActionDefinitionsBuilder(G_FPEXT)
122       .legalFor({{s64, s32}});
123 
124   getActionDefinitionsBuilder(G_FPTRUNC)
125       .legalFor({{s32, s64}});
126 
127   // FP to int conversion instructions
128   getActionDefinitionsBuilder(G_FPTOSI)
129       .legalForCartesianProduct({s32}, {s64, s32})
130       .libcallForCartesianProduct({s64}, {s64, s32})
131       .minScalar(0, s32);
132 
133   getActionDefinitionsBuilder(G_FPTOUI)
134       .libcallForCartesianProduct({s64}, {s64, s32})
135       .minScalar(0, s32);
136 
137   // Int to FP conversion instructions
138   getActionDefinitionsBuilder(G_SITOFP)
139       .legalForCartesianProduct({s64, s32}, {s32})
140       .libcallForCartesianProduct({s64, s32}, {s64})
141       .minScalar(1, s32);
142 
143   getActionDefinitionsBuilder(G_UITOFP)
144       .libcallForCartesianProduct({s64, s32}, {s64})
145       .minScalar(1, s32);
146 
147   getActionDefinitionsBuilder(G_SEXT_INREG).lower();
148 
149   computeTables();
150   verify(*ST.getInstrInfo());
151 }
152 
153 bool MipsLegalizerInfo::legalizeCustom(MachineInstr &MI,
154                                        MachineRegisterInfo &MRI,
155                                        MachineIRBuilder &MIRBuilder,
156                                        GISelChangeObserver &Observer) const {
157 
158   using namespace TargetOpcode;
159 
160   MIRBuilder.setInstr(MI);
161 
162   return false;
163 }
164 
165 bool MipsLegalizerInfo::legalizeIntrinsic(MachineInstr &MI, MachineRegisterInfo &MRI,
166                                           MachineIRBuilder &MIRBuilder) const {
167   switch (MI.getIntrinsicID()) {
168   case Intrinsic::memcpy:
169   case Intrinsic::memset:
170   case Intrinsic::memmove:
171     if (createMemLibcall(MIRBuilder, MRI, MI) ==
172         LegalizerHelper::UnableToLegalize)
173       return false;
174     MI.eraseFromParent();
175     return true;
176   default:
177     break;
178   }
179   return true;
180 }
181