blob: 81c1d6aad49a1bdb2dcfb8b32dda5f06f04075be [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- FastISel.h - Definition of the FastISel class ------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the FastISel class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_FASTISEL_H
15#define LLVM_CODEGEN_FASTISEL_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/TargetLowering.h"
22#include "llvm/IR/Attributes.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023#include "llvm/IR/CallingConv.h"
24#include "llvm/IR/DebugLoc.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/InstrTypes.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/Support/MachineValueType.h"
29#include <algorithm>
30#include <cstdint>
31#include <utility>
32
33namespace llvm {
34
35class AllocaInst;
36class BasicBlock;
37class CallInst;
38class Constant;
39class ConstantFP;
40class DataLayout;
41class FunctionLoweringInfo;
42class LoadInst;
43class MachineConstantPool;
44class MachineFrameInfo;
45class MachineFunction;
46class MachineInstr;
47class MachineMemOperand;
48class MachineOperand;
49class MachineRegisterInfo;
50class MCContext;
51class MCInstrDesc;
52class MCSymbol;
53class TargetInstrInfo;
54class TargetLibraryInfo;
55class TargetMachine;
56class TargetRegisterClass;
57class TargetRegisterInfo;
58class Type;
59class User;
60class Value;
61
Andrew Scullcdfcccc2018-10-05 20:58:37 +010062/// This is a fast-path instruction selection class that generates poor
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063/// code and doesn't support illegal types or non-trivial lowering, but runs
64/// quickly.
65class FastISel {
66public:
67 using ArgListEntry = TargetLoweringBase::ArgListEntry;
68 using ArgListTy = TargetLoweringBase::ArgListTy;
69 struct CallLoweringInfo {
70 Type *RetTy = nullptr;
71 bool RetSExt : 1;
72 bool RetZExt : 1;
73 bool IsVarArg : 1;
74 bool IsInReg : 1;
75 bool DoesNotReturn : 1;
76 bool IsReturnValueUsed : 1;
77 bool IsPatchPoint : 1;
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 // IsTailCall Should be modified by implementations of FastLowerCall
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080 // that perform tail call conversions.
81 bool IsTailCall = false;
82
83 unsigned NumFixedArgs = -1;
84 CallingConv::ID CallConv = CallingConv::C;
85 const Value *Callee = nullptr;
86 MCSymbol *Symbol = nullptr;
87 ArgListTy Args;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020088 const CallBase *CB = nullptr;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 MachineInstr *Call = nullptr;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020090 Register ResultReg;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 unsigned NumResultRegs = 0;
92
93 SmallVector<Value *, 16> OutVals;
94 SmallVector<ISD::ArgFlagsTy, 16> OutFlags;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020095 SmallVector<Register, 16> OutRegs;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 SmallVector<ISD::InputArg, 4> Ins;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020097 SmallVector<Register, 4> InRegs;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098
99 CallLoweringInfo()
100 : RetSExt(false), RetZExt(false), IsVarArg(false), IsInReg(false),
101 DoesNotReturn(false), IsReturnValueUsed(true), IsPatchPoint(false) {}
102
103 CallLoweringInfo &setCallee(Type *ResultTy, FunctionType *FuncTy,
104 const Value *Target, ArgListTy &&ArgsList,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200105 const CallBase &Call) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 RetTy = ResultTy;
107 Callee = Target;
108
109 IsInReg = Call.hasRetAttr(Attribute::InReg);
110 DoesNotReturn = Call.doesNotReturn();
111 IsVarArg = FuncTy->isVarArg();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200112 IsReturnValueUsed = !Call.use_empty();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 RetSExt = Call.hasRetAttr(Attribute::SExt);
114 RetZExt = Call.hasRetAttr(Attribute::ZExt);
115
116 CallConv = Call.getCallingConv();
117 Args = std::move(ArgsList);
118 NumFixedArgs = FuncTy->getNumParams();
119
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200120 CB = &Call;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121
122 return *this;
123 }
124
125 CallLoweringInfo &setCallee(Type *ResultTy, FunctionType *FuncTy,
126 MCSymbol *Target, ArgListTy &&ArgsList,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200127 const CallBase &Call,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 unsigned FixedArgs = ~0U) {
129 RetTy = ResultTy;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200130 Callee = Call.getCalledOperand();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 Symbol = Target;
132
133 IsInReg = Call.hasRetAttr(Attribute::InReg);
134 DoesNotReturn = Call.doesNotReturn();
135 IsVarArg = FuncTy->isVarArg();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200136 IsReturnValueUsed = !Call.use_empty();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100137 RetSExt = Call.hasRetAttr(Attribute::SExt);
138 RetZExt = Call.hasRetAttr(Attribute::ZExt);
139
140 CallConv = Call.getCallingConv();
141 Args = std::move(ArgsList);
142 NumFixedArgs = (FixedArgs == ~0U) ? FuncTy->getNumParams() : FixedArgs;
143
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200144 CB = &Call;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145
146 return *this;
147 }
148
149 CallLoweringInfo &setCallee(CallingConv::ID CC, Type *ResultTy,
150 const Value *Target, ArgListTy &&ArgsList,
151 unsigned FixedArgs = ~0U) {
152 RetTy = ResultTy;
153 Callee = Target;
154 CallConv = CC;
155 Args = std::move(ArgsList);
156 NumFixedArgs = (FixedArgs == ~0U) ? Args.size() : FixedArgs;
157 return *this;
158 }
159
160 CallLoweringInfo &setCallee(const DataLayout &DL, MCContext &Ctx,
161 CallingConv::ID CC, Type *ResultTy,
162 StringRef Target, ArgListTy &&ArgsList,
163 unsigned FixedArgs = ~0U);
164
165 CallLoweringInfo &setCallee(CallingConv::ID CC, Type *ResultTy,
166 MCSymbol *Target, ArgListTy &&ArgsList,
167 unsigned FixedArgs = ~0U) {
168 RetTy = ResultTy;
169 Symbol = Target;
170 CallConv = CC;
171 Args = std::move(ArgsList);
172 NumFixedArgs = (FixedArgs == ~0U) ? Args.size() : FixedArgs;
173 return *this;
174 }
175
176 CallLoweringInfo &setTailCall(bool Value = true) {
177 IsTailCall = Value;
178 return *this;
179 }
180
181 CallLoweringInfo &setIsPatchPoint(bool Value = true) {
182 IsPatchPoint = Value;
183 return *this;
184 }
185
186 ArgListTy &getArgs() { return Args; }
187
188 void clearOuts() {
189 OutVals.clear();
190 OutFlags.clear();
191 OutRegs.clear();
192 }
193
194 void clearIns() {
195 Ins.clear();
196 InRegs.clear();
197 }
198 };
199
200protected:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200201 DenseMap<const Value *, Register> LocalValueMap;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100202 FunctionLoweringInfo &FuncInfo;
203 MachineFunction *MF;
204 MachineRegisterInfo &MRI;
205 MachineFrameInfo &MFI;
206 MachineConstantPool &MCP;
207 DebugLoc DbgLoc;
208 const TargetMachine &TM;
209 const DataLayout &DL;
210 const TargetInstrInfo &TII;
211 const TargetLowering &TLI;
212 const TargetRegisterInfo &TRI;
213 const TargetLibraryInfo *LibInfo;
214 bool SkipTargetIndependentISel;
215
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100216 /// The position of the last instruction for materializing constants
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 /// for use in the current block. It resets to EmitStartPt when it makes sense
218 /// (for example, it's usually profitable to avoid function calls between the
219 /// definition and the use)
220 MachineInstr *LastLocalValue;
221
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100222 /// The top most instruction in the current block that is allowed for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100223 /// emitting local variables. LastLocalValue resets to EmitStartPt when it
224 /// makes sense (for example, on function calls)
225 MachineInstr *EmitStartPt;
226
227public:
228 virtual ~FastISel();
229
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100230 /// Return the position of the last instruction emitted for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100231 /// materializing constants for use in the current block.
232 MachineInstr *getLastLocalValue() { return LastLocalValue; }
233
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100234 /// Update the position of the last instruction emitted for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100235 /// materializing constants for use in the current block.
236 void setLastLocalValue(MachineInstr *I) {
237 EmitStartPt = I;
238 LastLocalValue = I;
239 }
240
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100241 /// Set the current block to which generated machine instructions will
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100242 /// be appended.
243 void startNewBlock();
244
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200245 /// Flush the local value map.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100246 void finishBasicBlock();
247
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100248 /// Return current debug location information.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249 DebugLoc getCurDebugLoc() const { return DbgLoc; }
250
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100251 /// Do "fast" instruction selection for function arguments and append
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100252 /// the machine instructions to the current block. Returns true when
253 /// successful.
254 bool lowerArguments();
255
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100256 /// Do "fast" instruction selection for the given LLVM IR instruction
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100257 /// and append the generated machine instructions to the current block.
258 /// Returns true if selection was successful.
259 bool selectInstruction(const Instruction *I);
260
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100261 /// Do "fast" instruction selection for the given LLVM IR operator
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262 /// (Instruction or ConstantExpr), and append generated machine instructions
263 /// to the current block. Return true if selection was successful.
264 bool selectOperator(const User *I, unsigned Opcode);
265
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100266 /// Create a virtual register and arrange for it to be assigned the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100267 /// value for the given LLVM value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200268 Register getRegForValue(const Value *V);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100269
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100270 /// Look up the value to see if its value is already cached in a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100271 /// register. It may be defined by instructions across blocks or defined
272 /// locally.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200273 Register lookUpRegForValue(const Value *V);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100274
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100275 /// This is a wrapper around getRegForValue that also takes care of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100276 /// truncating or sign-extending the given getelementptr index value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200277 std::pair<Register, bool> getRegForGEPIndex(const Value *Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100278
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100279 /// We're checking to see if we can fold \p LI into \p FoldInst. Note
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100280 /// that we could have a sequence where multiple LLVM IR instructions are
281 /// folded into the same machineinstr. For example we could have:
282 ///
283 /// A: x = load i32 *P
284 /// B: y = icmp A, 42
285 /// C: br y, ...
286 ///
287 /// In this scenario, \p LI is "A", and \p FoldInst is "C". We know about "B"
288 /// (and any other folded instructions) because it is between A and C.
289 ///
290 /// If we succeed folding, return true.
291 bool tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst);
292
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100293 /// The specified machine instr operand is a vreg, and that vreg is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100294 /// being provided by the specified load instruction. If possible, try to
295 /// fold the load as an operand to the instruction, returning true if
296 /// possible.
297 ///
298 /// This method should be implemented by targets.
299 virtual bool tryToFoldLoadIntoMI(MachineInstr * /*MI*/, unsigned /*OpNo*/,
300 const LoadInst * /*LI*/) {
301 return false;
302 }
303
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100304 /// Reset InsertPt to prepare for inserting instructions into the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100305 /// current block.
306 void recomputeInsertPt();
307
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100308 /// Remove all dead instructions between the I and E.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100309 void removeDeadCode(MachineBasicBlock::iterator I,
310 MachineBasicBlock::iterator E);
311
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200312 using SavePoint = MachineBasicBlock::iterator;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100313
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100314 /// Prepare InsertPt to begin inserting instructions into the local
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100315 /// value area and return the old insert position.
316 SavePoint enterLocalValueArea();
317
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100318 /// Reset InsertPt to the given old insert position.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100319 void leaveLocalValueArea(SavePoint Old);
320
321protected:
322 explicit FastISel(FunctionLoweringInfo &FuncInfo,
323 const TargetLibraryInfo *LibInfo,
324 bool SkipTargetIndependentISel = false);
325
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100326 /// This method is called by target-independent code when the normal
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100327 /// FastISel process fails to select an instruction. This gives targets a
328 /// chance to emit code for anything that doesn't fit into FastISel's
329 /// framework. It returns true if it was successful.
330 virtual bool fastSelectInstruction(const Instruction *I) = 0;
331
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100332 /// This method is called by target-independent code to do target-
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100333 /// specific argument lowering. It returns true if it was successful.
334 virtual bool fastLowerArguments();
335
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100336 /// This method is called by target-independent code to do target-
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100337 /// specific call lowering. It returns true if it was successful.
338 virtual bool fastLowerCall(CallLoweringInfo &CLI);
339
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100340 /// This method is called by target-independent code to do target-
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100341 /// specific intrinsic lowering. It returns true if it was successful.
342 virtual bool fastLowerIntrinsicCall(const IntrinsicInst *II);
343
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100344 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100345 /// instruction with the given type and opcode be emitted.
346 virtual unsigned fastEmit_(MVT VT, MVT RetVT, unsigned Opcode);
347
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100348 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100349 /// instruction with the given type, opcode, and register operand be emitted.
350 virtual unsigned fastEmit_r(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0,
351 bool Op0IsKill);
352
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100353 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100354 /// instruction with the given type, opcode, and register operands be emitted.
355 virtual unsigned fastEmit_rr(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0,
356 bool Op0IsKill, unsigned Op1, bool Op1IsKill);
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359 /// instruction with the given type, opcode, and register and immediate
360 /// operands be emitted.
361 virtual unsigned fastEmit_ri(MVT VT, MVT RetVT, unsigned Opcode, unsigned Op0,
362 bool Op0IsKill, uint64_t Imm);
363
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100364 /// This method is a wrapper of fastEmit_ri.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100365 ///
366 /// It first tries to emit an instruction with an immediate operand using
367 /// fastEmit_ri. If that fails, it materializes the immediate into a register
368 /// and try fastEmit_rr instead.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200369 Register fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0, bool Op0IsKill,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100370 uint64_t Imm, MVT ImmType);
371
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100372 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100373 /// instruction with the given type, opcode, and immediate operand be emitted.
374 virtual unsigned fastEmit_i(MVT VT, MVT RetVT, unsigned Opcode, uint64_t Imm);
375
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100376 /// This method is called by target-independent code to request that an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100377 /// instruction with the given type, opcode, and floating-point immediate
378 /// operand be emitted.
379 virtual unsigned fastEmit_f(MVT VT, MVT RetVT, unsigned Opcode,
380 const ConstantFP *FPImm);
381
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100382 /// Emit a MachineInstr with no operands and a result register in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100383 /// given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200384 Register fastEmitInst_(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100385 const TargetRegisterClass *RC);
386
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100387 /// Emit a MachineInstr with one register operand and a result register
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100388 /// in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200389 Register fastEmitInst_r(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100390 const TargetRegisterClass *RC, unsigned Op0,
391 bool Op0IsKill);
392
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100393 /// Emit a MachineInstr with two register operands and a result
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100394 /// register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200395 Register fastEmitInst_rr(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100396 const TargetRegisterClass *RC, unsigned Op0,
397 bool Op0IsKill, unsigned Op1, bool Op1IsKill);
398
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100399 /// Emit a MachineInstr with three register operands and a result
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100400 /// register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200401 Register fastEmitInst_rrr(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100402 const TargetRegisterClass *RC, unsigned Op0,
403 bool Op0IsKill, unsigned Op1, bool Op1IsKill,
404 unsigned Op2, bool Op2IsKill);
405
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100406 /// Emit a MachineInstr with a register operand, an immediate, and a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100407 /// result register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200408 Register fastEmitInst_ri(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100409 const TargetRegisterClass *RC, unsigned Op0,
410 bool Op0IsKill, uint64_t Imm);
411
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100412 /// Emit a MachineInstr with one register operand and two immediate
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100413 /// operands.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200414 Register fastEmitInst_rii(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415 const TargetRegisterClass *RC, unsigned Op0,
416 bool Op0IsKill, uint64_t Imm1, uint64_t Imm2);
417
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100418 /// Emit a MachineInstr with a floating point immediate, and a result
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100419 /// register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200420 Register fastEmitInst_f(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100421 const TargetRegisterClass *RC,
422 const ConstantFP *FPImm);
423
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100424 /// Emit a MachineInstr with two register operands, an immediate, and a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100425 /// result register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200426 Register fastEmitInst_rri(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100427 const TargetRegisterClass *RC, unsigned Op0,
428 bool Op0IsKill, unsigned Op1, bool Op1IsKill,
429 uint64_t Imm);
430
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100431 /// Emit a MachineInstr with a single immediate operand, and a result
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100432 /// register in the given register class.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200433 Register fastEmitInst_i(unsigned MachineInstOpcode,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100434 const TargetRegisterClass *RC, uint64_t Imm);
435
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100436 /// Emit a MachineInstr for an extract_subreg from a specified index of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100437 /// a superregister to a specified type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200438 Register fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0, bool Op0IsKill,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100439 uint32_t Idx);
440
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100441 /// Emit MachineInstrs to compute the value of Op with all but the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100442 /// least significant bit set to zero.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200443 Register fastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100444
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100445 /// Emit an unconditional branch to the given block, unless it is the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100446 /// immediate (fall-through) successor, and update the CFG.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100447 void fastEmitBranch(MachineBasicBlock *MSucc, const DebugLoc &DbgLoc);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448
449 /// Emit an unconditional branch to \p FalseMBB, obtains the branch weight
450 /// and adds TrueMBB and FalseMBB to the successor list.
451 void finishCondBranch(const BasicBlock *BranchBB, MachineBasicBlock *TrueMBB,
452 MachineBasicBlock *FalseMBB);
453
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100454 /// Update the value map to include the new mapping for this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100455 /// instruction, or insert an extra copy to get the result in a previous
456 /// determined register.
457 ///
458 /// NOTE: This is only necessary because we might select a block that uses a
459 /// value before we select the block that defines the value. It might be
460 /// possible to fix this by selecting blocks in reverse postorder.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200461 void updateValueMap(const Value *I, Register Reg, unsigned NumRegs = 1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100462
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200463 Register createResultReg(const TargetRegisterClass *RC);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100464
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100465 /// Try to constrain Op so that it is usable by argument OpNum of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100466 /// provided MCInstrDesc. If this fails, create a new virtual register in the
467 /// correct class and COPY the value there.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200468 Register constrainOperandRegClass(const MCInstrDesc &II, Register Op,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100469 unsigned OpNum);
470
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100471 /// Emit a constant in a register using target-specific logic, such as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100472 /// constant pool loads.
473 virtual unsigned fastMaterializeConstant(const Constant *C) { return 0; }
474
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100475 /// Emit an alloca address in a register using target-specific logic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100476 virtual unsigned fastMaterializeAlloca(const AllocaInst *C) { return 0; }
477
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100478 /// Emit the floating-point constant +0.0 in a register using target-
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100479 /// specific logic.
480 virtual unsigned fastMaterializeFloatZero(const ConstantFP *CF) {
481 return 0;
482 }
483
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100484 /// Check if \c Add is an add that can be safely folded into \c GEP.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100485 ///
486 /// \c Add can be folded into \c GEP if:
487 /// - \c Add is an add,
488 /// - \c Add's size matches \c GEP's,
489 /// - \c Add is in the same basic block as \c GEP, and
490 /// - \c Add has a constant operand.
491 bool canFoldAddIntoGEP(const User *GEP, const Value *Add);
492
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100493 /// Test whether the given value has exactly one use.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100494 bool hasTrivialKill(const Value *V);
495
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100496 /// Create a machine mem operand from the given instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100497 MachineMemOperand *createMachineMemOperandFor(const Instruction *I) const;
498
499 CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) const;
500
501 bool lowerCallTo(const CallInst *CI, MCSymbol *Symbol, unsigned NumArgs);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100502 bool lowerCallTo(const CallInst *CI, const char *SymName,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100503 unsigned NumArgs);
504 bool lowerCallTo(CallLoweringInfo &CLI);
505
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100506 bool lowerCall(const CallInst *I);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100507 /// Select and emit code for a binary operator instruction, which has
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100508 /// an opcode which directly corresponds to the given ISD opcode.
509 bool selectBinaryOp(const User *I, unsigned ISDOpcode);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100510 bool selectFNeg(const User *I, const Value *In);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100511 bool selectGetElementPtr(const User *I);
512 bool selectStackmap(const CallInst *I);
513 bool selectPatchpoint(const CallInst *I);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100514 bool selectCall(const User *I);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100515 bool selectIntrinsicCall(const IntrinsicInst *II);
516 bool selectBitCast(const User *I);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200517 bool selectFreeze(const User *I);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100518 bool selectCast(const User *I, unsigned Opcode);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100519 bool selectExtractValue(const User *U);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100520 bool selectXRayCustomEvent(const CallInst *II);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100521 bool selectXRayTypedEvent(const CallInst *II);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100522
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200523 bool shouldOptForSize(const MachineFunction *MF) const {
524 // TODO: Implement PGSO.
525 return MF->getFunction().hasOptSize();
526 }
527
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100528private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100529 /// Handle PHI nodes in successor blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100530 ///
531 /// Emit code to ensure constants are copied into registers when needed.
532 /// Remember the virtual registers that need to be added to the Machine PHI
533 /// nodes as input. We cannot just directly add them, because expansion might
534 /// result in multiple MBB's for one BB. As such, the start of the BB might
535 /// correspond to a different MBB than the end.
536 bool handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
537
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100538 /// Helper for materializeRegForValue to materialize a constant in a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100539 /// target-independent way.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200540 Register materializeConstant(const Value *V, MVT VT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100541
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100542 /// Helper for getRegForVale. This function is called when the value
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100543 /// isn't already available in a register and must be materialized with new
544 /// instructions.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200545 Register materializeRegForValue(const Value *V, MVT VT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100547 /// Clears LocalValueMap and moves the area for the new local variables
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100548 /// to the beginning of the block. It helps to avoid spilling cached variables
549 /// across heavy instructions like calls.
550 void flushLocalValueMap();
551
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100552 /// Removes dead local value instructions after SavedLastLocalvalue.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100553 void removeDeadLocalValueCode(MachineInstr *SavedLastLocalValue);
554
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100555 /// Insertion point before trying to select the current instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100556 MachineBasicBlock::iterator SavedInsertPt;
557
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100558 /// Add a stackmap or patchpoint intrinsic call's live variable
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100559 /// operands to a stackmap or patchpoint machine instruction.
560 bool addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
561 const CallInst *CI, unsigned StartIdx);
562 bool lowerCallOperands(const CallInst *CI, unsigned ArgIdx, unsigned NumArgs,
563 const Value *Callee, bool ForceRetVoidTy,
564 CallLoweringInfo &CLI);
565};
566
567} // end namespace llvm
568
569#endif // LLVM_CODEGEN_FASTISEL_H