LLVM 24.0.0git
ARMFastISel.cpp
Go to the documentation of this file.
1//===- ARMFastISel.cpp - ARM FastISel implementation ----------------------===//
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//
9// This file defines the ARM-specific support for the FastISel class. Some
10// of the target-specific code is generated by tablegen in the file
11// ARMGenFastISel.inc, which is #included here.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARM.h"
16#include "ARMBaseInstrInfo.h"
17#include "ARMBaseRegisterInfo.h"
18#include "ARMCallingConv.h"
20#include "ARMISelLowering.h"
22#include "ARMSubtarget.h"
25#include "Utils/ARMBaseInfo.h"
26#include "llvm/ADT/APFloat.h"
27#include "llvm/ADT/APInt.h"
28#include "llvm/ADT/DenseMap.h"
49#include "llvm/IR/Argument.h"
50#include "llvm/IR/Attributes.h"
51#include "llvm/IR/CallingConv.h"
52#include "llvm/IR/Constant.h"
53#include "llvm/IR/Constants.h"
54#include "llvm/IR/DataLayout.h"
56#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
60#include "llvm/IR/InstrTypes.h"
61#include "llvm/IR/Instruction.h"
64#include "llvm/IR/Intrinsics.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
67#include "llvm/IR/Type.h"
68#include "llvm/IR/User.h"
69#include "llvm/IR/Value.h"
70#include "llvm/MC/MCInstrDesc.h"
77#include <cassert>
78#include <cstdint>
79#include <utility>
80
81using namespace llvm;
82
83namespace {
84
85 // All possible address modes, plus some.
86class Address {
87public:
88 enum BaseKind { RegBase, FrameIndexBase };
89
90private:
91 BaseKind Kind = RegBase;
92 union {
93 unsigned Reg;
94 int FI;
95 } Base;
96
97 int Offset = 0;
98
99public:
100 // Innocuous defaults for our address.
101 Address() { Base.Reg = 0; }
102
103 void setKind(BaseKind K) { Kind = K; }
104 BaseKind getKind() const { return Kind; }
105 bool isRegBase() const { return Kind == RegBase; }
106 bool isFIBase() const { return Kind == FrameIndexBase; }
107
108 void setReg(Register Reg) {
109 assert(isRegBase() && "Invalid base register access!");
110 Base.Reg = Reg.id();
111 }
112
113 Register getReg() const {
114 assert(isRegBase() && "Invalid base register access!");
115 return Base.Reg;
116 }
117
118 void setFI(int FI) {
119 assert(isFIBase() && "Invalid base frame index access!");
120 Base.FI = FI;
121 }
122
123 int getFI() const {
124 assert(isFIBase() && "Invalid base frame index access!");
125 return Base.FI;
126 }
127
128 void setOffset(int O) { Offset = O; }
129 int getOffset() { return Offset; }
130};
131
132class ARMFastISel final : public FastISel {
133 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
134 /// make the right decision when generating code for different targets.
135 const ARMSubtarget *Subtarget;
136 Module &M;
137 const ARMBaseInstrInfo &TII;
138 const ARMTargetLowering &TLI;
139 ARMFunctionInfo *AFI;
140
141 // Convenience variables to avoid some queries.
142 bool isThumb2;
143 LLVMContext *Context;
144
145 public:
146 explicit ARMFastISel(FunctionLoweringInfo &funcInfo,
147 const TargetLibraryInfo *libInfo,
148 const LibcallLoweringInfo *libcallLowering)
149 : FastISel(funcInfo, libInfo, libcallLowering),
150 Subtarget(&funcInfo.MF->getSubtarget<ARMSubtarget>()),
151 M(const_cast<Module &>(*funcInfo.Fn->getParent())),
152 TII(*Subtarget->getInstrInfo()),
153 TLI(*Subtarget->getTargetLowering()) {
154 AFI = funcInfo.MF->getInfo<ARMFunctionInfo>();
155 isThumb2 = AFI->isThumbFunction();
156 Context = &funcInfo.Fn->getContext();
157 }
158
159 private:
160 // Code from FastISel.cpp.
161
162 Register fastEmitInst_r(unsigned MachineInstOpcode,
163 const TargetRegisterClass *RC, Register Op0);
164 Register fastEmitInst_rr(unsigned MachineInstOpcode,
165 const TargetRegisterClass *RC, Register Op0,
166 Register Op1);
167 Register fastEmitInst_ri(unsigned MachineInstOpcode,
168 const TargetRegisterClass *RC, Register Op0,
169 uint64_t Imm);
170 Register fastEmitInst_i(unsigned MachineInstOpcode,
171 const TargetRegisterClass *RC, uint64_t Imm);
172
173 // Backend specific FastISel code.
174
175 bool fastSelectInstruction(const Instruction *I) override;
176 Register fastMaterializeConstant(const Constant *C) override;
177 Register fastMaterializeAlloca(const AllocaInst *AI) override;
178 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
179 const LoadInst *LI) override;
180 bool fastLowerArguments() override;
181
182#include "ARMGenFastISel.inc"
183
184 // Instruction selection routines.
185
186 bool SelectLoad(const Instruction *I);
187 bool SelectStore(const Instruction *I);
188 bool SelectBranch(const Instruction *I);
189 bool SelectIndirectBr(const Instruction *I);
190 bool SelectCmp(const Instruction *I);
191 bool SelectFPExt(const Instruction *I);
192 bool SelectFPTrunc(const Instruction *I);
193 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
194 bool SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode);
195 bool SelectIToFP(const Instruction *I, bool isSigned);
196 bool SelectFPToI(const Instruction *I, bool isSigned);
197 bool SelectDiv(const Instruction *I, bool isSigned);
198 bool SelectRem(const Instruction *I, bool isSigned);
199 bool SelectCall(const Instruction *I, const char *IntrMemName);
200 bool SelectIntrinsicCall(const IntrinsicInst &I);
201 bool SelectSelect(const Instruction *I);
202 bool SelectRet(const Instruction *I);
203 bool SelectTrunc(const Instruction *I);
204 bool SelectIntExt(const Instruction *I);
205 bool SelectShift(const Instruction *I, ARM_AM::ShiftOpc ShiftTy);
206
207 // Utility routines.
208
209 bool isPositionIndependent() const;
210 bool isTypeLegal(Type *Ty, MVT &VT);
211 bool isLoadTypeLegal(Type *Ty, MVT &VT);
212 bool ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
213 bool isZExt);
214 bool ARMEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
215 MaybeAlign Alignment = std::nullopt, bool isZExt = true,
216 bool allocReg = true);
217 bool ARMEmitStore(MVT VT, Register SrcReg, Address &Addr,
218 MaybeAlign Alignment = std::nullopt);
219 bool ARMComputeAddress(const Value *Obj, Address &Addr);
220 void ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3);
221 bool ARMIsMemCpySmall(uint64_t Len);
222 bool ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
223 MaybeAlign Alignment);
224 Register ARMEmitIntExt(MVT SrcVT, Register SrcReg, MVT DestVT, bool isZExt);
225 Register ARMMaterializeFP(const ConstantFP *CFP, MVT VT);
226 Register ARMMaterializeInt(const Constant *C, MVT VT);
227 Register ARMMaterializeGV(const GlobalValue *GV, MVT VT);
228 Register ARMMoveToFPReg(MVT VT, Register SrcReg);
229 Register ARMMoveToIntReg(MVT VT, Register SrcReg);
230 unsigned ARMSelectCallOp(bool UseReg);
231 Register ARMLowerPICELF(const GlobalValue *GV, MVT VT);
232
233 const TargetLowering *getTargetLowering() { return &TLI; }
234
235 // Call handling routines.
236
237 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC,
238 bool Return,
239 bool isVarArg);
240 bool ProcessCallArgs(SmallVectorImpl<Value*> &Args,
241 SmallVectorImpl<Register> &ArgRegs,
242 SmallVectorImpl<MVT> &ArgVTs,
243 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
244 SmallVectorImpl<Register> &RegArgs,
245 CallingConv::ID CC,
246 unsigned &NumBytes,
247 bool isVarArg);
248 Register getLibcallReg(const Twine &Name);
249 bool FinishCall(MVT RetVT, SmallVectorImpl<Register> &UsedRegs,
250 const Instruction *I, CallingConv::ID CC,
251 unsigned &NumBytes, bool isVarArg);
252 bool ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call);
253
254 // OptionalDef handling routines.
255
256 bool isARMNEONPred(const MachineInstr *MI);
257 bool DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR);
258 const MachineInstrBuilder &AddOptionalDefs(const MachineInstrBuilder &MIB);
259 void AddLoadStoreOperands(MVT VT, Address &Addr,
260 const MachineInstrBuilder &MIB,
261 MachineMemOperand::Flags Flags, bool useAM3);
262};
263
264} // end anonymous namespace
265
266// DefinesOptionalPredicate - This is different from DefinesPredicate in that
267// we don't care about implicit defs here, just places we'll need to add a
268// default CCReg argument. Sets CPSR if we're setting CPSR instead of CCR.
269bool ARMFastISel::DefinesOptionalPredicate(MachineInstr *MI, bool *CPSR) {
270 if (!MI->hasOptionalDef())
271 return false;
272
273 // Look to see if our OptionalDef is defining CPSR or CCR.
274 for (const MachineOperand &MO : MI->operands()) {
275 if (!MO.isReg() || !MO.isDef()) continue;
276 if (MO.getReg() == ARM::CPSR)
277 *CPSR = true;
278 }
279 return true;
280}
281
282bool ARMFastISel::isARMNEONPred(const MachineInstr *MI) {
283 const MCInstrDesc &MCID = MI->getDesc();
284
285 // If we're a thumb2 or not NEON function we'll be handled via isPredicable.
287 AFI->isThumb2Function())
288 return MI->isPredicable();
289
290 for (const MCOperandInfo &opInfo : MCID.operands())
291 if (opInfo.isPredicate())
292 return true;
293
294 return false;
295}
296
297// If the machine is predicable go ahead and add the predicate operands, if
298// it needs default CC operands add those.
299// TODO: If we want to support thumb1 then we'll need to deal with optional
300// CPSR defs that need to be added before the remaining operands. See s_cc_out
301// for descriptions why.
302const MachineInstrBuilder &
303ARMFastISel::AddOptionalDefs(const MachineInstrBuilder &MIB) {
304 MachineInstr *MI = &*MIB;
305
306 // Do we use a predicate? or...
307 // Are we NEON in ARM mode and have a predicate operand? If so, I know
308 // we're not predicable but add it anyways.
309 if (isARMNEONPred(MI))
310 MIB.add(predOps(ARMCC::AL));
311
312 // Do we optionally set a predicate? Preds is size > 0 iff the predicate
313 // defines CPSR. All other OptionalDefines in ARM are the CCR register.
314 bool CPSR = false;
315 if (DefinesOptionalPredicate(MI, &CPSR))
316 MIB.add(CPSR ? t1CondCodeOp() : condCodeOp());
317 return MIB;
318}
319
320Register ARMFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
321 const TargetRegisterClass *RC,
322 Register Op0) {
323 Register ResultReg = createResultReg(RC);
324 const MCInstrDesc &II = TII.get(MachineInstOpcode);
325
326 // Make sure the input operand is sufficiently constrained to be legal
327 // for this instruction.
328 Op0 = constrainOperandRegClass(II, Op0, 1);
329 if (II.getNumDefs() >= 1) {
330 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II,
331 ResultReg).addReg(Op0));
332 } else {
333 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
334 .addReg(Op0));
335 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
336 TII.get(TargetOpcode::COPY), ResultReg)
337 .addReg(II.implicit_defs()[0]));
338 }
339 return ResultReg;
340}
341
342Register ARMFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
343 const TargetRegisterClass *RC,
344 Register Op0, Register Op1) {
345 Register ResultReg = createResultReg(RC);
346 const MCInstrDesc &II = TII.get(MachineInstOpcode);
347
348 // Make sure the input operands are sufficiently constrained to be legal
349 // for this instruction.
350 Op0 = constrainOperandRegClass(II, Op0, 1);
351 Op1 = constrainOperandRegClass(II, Op1, 2);
352
353 if (II.getNumDefs() >= 1) {
354 AddOptionalDefs(
355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II, ResultReg)
356 .addReg(Op0)
357 .addReg(Op1));
358 } else {
359 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
360 .addReg(Op0)
361 .addReg(Op1));
362 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
363 TII.get(TargetOpcode::COPY), ResultReg)
364 .addReg(II.implicit_defs()[0]));
365 }
366 return ResultReg;
367}
368
369Register ARMFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
370 const TargetRegisterClass *RC,
371 Register Op0, uint64_t Imm) {
372 Register ResultReg = createResultReg(RC);
373 const MCInstrDesc &II = TII.get(MachineInstOpcode);
374
375 // Make sure the input operand is sufficiently constrained to be legal
376 // for this instruction.
377 Op0 = constrainOperandRegClass(II, Op0, 1);
378 if (II.getNumDefs() >= 1) {
379 AddOptionalDefs(
380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II, ResultReg)
381 .addReg(Op0)
382 .addImm(Imm));
383 } else {
384 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
385 .addReg(Op0)
386 .addImm(Imm));
387 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
388 TII.get(TargetOpcode::COPY), ResultReg)
389 .addReg(II.implicit_defs()[0]));
390 }
391 return ResultReg;
392}
393
394Register ARMFastISel::fastEmitInst_i(unsigned MachineInstOpcode,
395 const TargetRegisterClass *RC,
396 uint64_t Imm) {
397 Register ResultReg = createResultReg(RC);
398 const MCInstrDesc &II = TII.get(MachineInstOpcode);
399
400 if (II.getNumDefs() >= 1) {
401 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II,
402 ResultReg).addImm(Imm));
403 } else {
404 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
405 .addImm(Imm));
406 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
407 TII.get(TargetOpcode::COPY), ResultReg)
408 .addReg(II.implicit_defs()[0]));
409 }
410 return ResultReg;
411}
412
413// TODO: Don't worry about 64-bit now, but when this is fixed remove the
414// checks from the various callers.
415Register ARMFastISel::ARMMoveToFPReg(MVT VT, Register SrcReg) {
416 if (VT == MVT::f64)
417 return Register();
418
419 Register MoveReg = createResultReg(TLI.getRegClassFor(VT));
420 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
421 TII.get(ARM::VMOVSR), MoveReg)
422 .addReg(SrcReg));
423 return MoveReg;
424}
425
426Register ARMFastISel::ARMMoveToIntReg(MVT VT, Register SrcReg) {
427 if (VT == MVT::i64)
428 return Register();
429
430 Register MoveReg = createResultReg(TLI.getRegClassFor(VT));
431 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
432 TII.get(ARM::VMOVRS), MoveReg)
433 .addReg(SrcReg));
434 return MoveReg;
435}
436
437// For double width floating point we need to materialize two constants
438// (the high and the low) into integer registers then use a move to get
439// the combined constant into an FP reg.
440Register ARMFastISel::ARMMaterializeFP(const ConstantFP *CFP, MVT VT) {
441 if (VT != MVT::f32 && VT != MVT::f64)
442 return Register();
443
444 const APFloat Val = CFP->getValueAPF();
445 bool is64bit = VT == MVT::f64;
446
447 // This checks to see if we can use VFP3 instructions to materialize
448 // a constant, otherwise we have to go through the constant pool.
449 if (TLI.isFPImmLegal(Val, VT)) {
450 int Imm;
451 unsigned Opc;
452 if (is64bit) {
453 Imm = ARM_AM::getFP64Imm(Val);
454 Opc = ARM::FCONSTD;
455 } else {
456 Imm = ARM_AM::getFP32Imm(Val);
457 Opc = ARM::FCONSTS;
458 }
459 Register DestReg = createResultReg(TLI.getRegClassFor(VT));
460 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
461 TII.get(Opc), DestReg).addImm(Imm));
462 return DestReg;
463 }
464
465 // Require VFP2 for loading fp constants.
466 if (!Subtarget->hasVFP2Base()) return false;
467
468 // MachineConstantPool wants an explicit alignment.
469 Align Alignment = DL.getPrefTypeAlign(CFP->getType());
470 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);
471 Register DestReg = createResultReg(TLI.getRegClassFor(VT));
472 unsigned Opc = is64bit ? ARM::VLDRD : ARM::VLDRS;
473
474 // The extra reg is for addrmode5.
475 AddOptionalDefs(
476 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
478 .addReg(0));
479 return DestReg;
480}
481
482Register ARMFastISel::ARMMaterializeInt(const Constant *C, MVT VT) {
483 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
484 return Register();
485
486 // If we can do this in a single instruction without a constant pool entry
487 // do so now.
488 const ConstantInt *CI = cast<ConstantInt>(C);
489 if (Subtarget->hasV6T2Ops() && isUInt<16>(CI->getZExtValue())) {
490 unsigned Opc = isThumb2 ? ARM::t2MOVi16 : ARM::MOVi16;
491 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
492 &ARM::GPRRegClass;
493 Register ImmReg = createResultReg(RC);
494 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
495 TII.get(Opc), ImmReg)
496 .addImm(CI->getZExtValue()));
497 return ImmReg;
498 }
499
500 // Use MVN to emit negative constants.
501 if (VT == MVT::i32 && Subtarget->hasV6T2Ops() && CI->isNegative()) {
502 unsigned Imm = (unsigned)~(CI->getSExtValue());
503 bool UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
504 (ARM_AM::getSOImmVal(Imm) != -1);
505 if (UseImm) {
506 unsigned Opc = isThumb2 ? ARM::t2MVNi : ARM::MVNi;
507 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass :
508 &ARM::GPRRegClass;
509 Register ImmReg = createResultReg(RC);
510 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
511 TII.get(Opc), ImmReg)
512 .addImm(Imm));
513 return ImmReg;
514 }
515 }
516
517 Register ResultReg;
518 if (Subtarget->useMovt())
519 ResultReg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
520
521 if (ResultReg)
522 return ResultReg;
523
524 // Load from constant pool. For now 32-bit only.
525 if (VT != MVT::i32)
526 return Register();
527
528 // MachineConstantPool wants an explicit alignment.
529 Align Alignment = DL.getPrefTypeAlign(C->getType());
530 unsigned Idx = MCP.getConstantPoolIndex(C, Alignment);
531 ResultReg = createResultReg(TLI.getRegClassFor(VT));
532 if (isThumb2)
533 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
534 TII.get(ARM::t2LDRpci), ResultReg)
536 else {
537 // The extra immediate is for addrmode2.
538 ResultReg = constrainOperandRegClass(TII.get(ARM::LDRcp), ResultReg, 0);
539 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
540 TII.get(ARM::LDRcp), ResultReg)
542 .addImm(0));
543 }
544 return ResultReg;
545}
546
547bool ARMFastISel::isPositionIndependent() const {
548 return TLI.isPositionIndependent();
549}
550
551Register ARMFastISel::ARMMaterializeGV(const GlobalValue *GV, MVT VT) {
552 // For now 32-bit only.
553 if (VT != MVT::i32 || GV->isThreadLocal())
554 return Register();
555
556 // ROPI/RWPI not currently supported.
557 if (Subtarget->isROPI() || Subtarget->isRWPI())
558 return Register();
559
560 bool IsIndirect = Subtarget->isGVIndirectSymbol(GV);
561 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
562 : &ARM::GPRRegClass;
563 Register DestReg = createResultReg(RC);
564
565 // FastISel TLS support on non-MachO is broken, punt to SelectionDAG.
566 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
567 bool IsThreadLocal = GVar && GVar->isThreadLocal();
568 if (!Subtarget->isTargetMachO() && IsThreadLocal)
569 return Register();
570
571 bool IsPositionIndependent = isPositionIndependent();
572 // Use movw+movt when possible, it avoids constant pool entries.
573 // Non-darwin targets only support static movt relocations in FastISel.
574 if (Subtarget->useMovt() &&
575 (Subtarget->isTargetMachO() || !IsPositionIndependent)) {
576 unsigned Opc;
577 unsigned char TF = 0;
580
581 if (IsPositionIndependent)
582 Opc = isThumb2 ? ARM::t2MOV_ga_pcrel : ARM::MOV_ga_pcrel;
583 else
584 Opc = isThumb2 ? ARM::t2MOVi32imm : ARM::MOVi32imm;
585 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
586 TII.get(Opc), DestReg).addGlobalAddress(GV, 0, TF));
587 } else {
588 // MachineConstantPool wants an explicit alignment.
589 Align Alignment = DL.getPrefTypeAlign(GV->getType());
590
591 if (Subtarget->isTargetELF() && IsPositionIndependent)
592 return ARMLowerPICELF(GV, VT);
593
594 // Grab index.
595 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
596 unsigned Id = AFI->createPICLabelUId();
597 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(GV, Id,
599 PCAdj);
600 unsigned Idx = MCP.getConstantPoolIndex(CPV, Alignment);
601
602 // Load value.
603 MachineInstrBuilder MIB;
604 if (isThumb2) {
605 unsigned Opc = IsPositionIndependent ? ARM::t2LDRpci_pic : ARM::t2LDRpci;
606 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),
607 DestReg).addConstantPoolIndex(Idx);
608 if (IsPositionIndependent)
609 MIB.addImm(Id);
610 AddOptionalDefs(MIB);
611 } else {
612 // The extra immediate is for addrmode2.
613 DestReg = constrainOperandRegClass(TII.get(ARM::LDRcp), DestReg, 0);
614 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
615 TII.get(ARM::LDRcp), DestReg)
617 .addImm(0);
618 AddOptionalDefs(MIB);
619
620 if (IsPositionIndependent) {
621 unsigned Opc = IsIndirect ? ARM::PICLDR : ARM::PICADD;
622 Register NewDestReg = createResultReg(TLI.getRegClassFor(VT));
623
624 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
625 MIMD, TII.get(Opc), NewDestReg)
626 .addReg(DestReg)
627 .addImm(Id);
628 AddOptionalDefs(MIB);
629 return NewDestReg;
630 }
631 }
632 }
633
634 if ((Subtarget->isTargetELF() && Subtarget->isGVInGOT(GV)) ||
635 (Subtarget->isTargetMachO() && IsIndirect)) {
636 MachineInstrBuilder MIB;
637 Register NewDestReg = createResultReg(TLI.getRegClassFor(VT));
638 if (isThumb2)
639 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
640 TII.get(ARM::t2LDRi12), NewDestReg)
641 .addReg(DestReg)
642 .addImm(0);
643 else
644 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
645 TII.get(ARM::LDRi12), NewDestReg)
646 .addReg(DestReg)
647 .addImm(0);
648 DestReg = NewDestReg;
649 AddOptionalDefs(MIB);
650 }
651
652 return DestReg;
653}
654
655Register ARMFastISel::fastMaterializeConstant(const Constant *C) {
656 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
657
658 // Only handle simple types.
659 if (!CEVT.isSimple())
660 return Register();
661 MVT VT = CEVT.getSimpleVT();
662
663 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
664 return ARMMaterializeFP(CFP, VT);
665 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
666 return ARMMaterializeGV(GV, VT);
667 else if (isa<ConstantInt>(C))
668 return ARMMaterializeInt(C, VT);
669
670 return Register();
671}
672
673// TODO: Register ARMFastISel::TargetMaterializeFloatZero(const ConstantFP *CF);
674
675Register ARMFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
676 // Don't handle dynamic allocas.
677 if (!FuncInfo.StaticAllocaMap.count(AI))
678 return Register();
679
680 MVT VT;
681 if (!isLoadTypeLegal(AI->getType(), VT))
682 return Register();
683
684 auto SI = FuncInfo.StaticAllocaMap.find(AI);
685
686 // This will get lowered later into the correct offsets and registers
687 // via rewriteXFrameIndex.
688 if (SI != FuncInfo.StaticAllocaMap.end()) {
689 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
690 const TargetRegisterClass* RC = TLI.getRegClassFor(VT);
691 Register ResultReg = createResultReg(RC);
692 ResultReg = constrainOperandRegClass(TII.get(Opc), ResultReg, 0);
693
694 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
695 TII.get(Opc), ResultReg)
696 .addFrameIndex(SI->second)
697 .addImm(0));
698 return ResultReg;
699 }
700
701 return Register();
702}
703
704bool ARMFastISel::isTypeLegal(Type *Ty, MVT &VT) {
705 EVT evt = TLI.getValueType(DL, Ty, true);
706
707 // Only handle simple types.
708 if (evt == MVT::Other || !evt.isSimple()) return false;
709 VT = evt.getSimpleVT();
710
711 // Handle all legal types, i.e. a register that will directly hold this
712 // value.
713 return TLI.isTypeLegal(VT);
714}
715
716bool ARMFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
717 if (isTypeLegal(Ty, VT)) return true;
718
719 // If this is a type than can be sign or zero-extended to a basic operation
720 // go ahead and accept it now.
721 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
722 return true;
723
724 return false;
725}
726
727// Computes the address to get to an object.
728bool ARMFastISel::ARMComputeAddress(const Value *Obj, Address &Addr) {
729 // Some boilerplate from the X86 FastISel.
730 const User *U = nullptr;
731 unsigned Opcode = Instruction::UserOp1;
732 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
733 // Don't walk into other basic blocks unless the object is an alloca from
734 // another block, otherwise it may not have a virtual register assigned.
735 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
736 FuncInfo.getMBB(I->getParent()) == FuncInfo.MBB) {
737 Opcode = I->getOpcode();
738 U = I;
739 }
740 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
741 Opcode = C->getOpcode();
742 U = C;
743 }
744
745 if (PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
746 if (Ty->getAddressSpace() > 255)
747 // Fast instruction selection doesn't support the special
748 // address spaces.
749 return false;
750
751 switch (Opcode) {
752 default:
753 break;
754 case Instruction::BitCast:
755 // Look through bitcasts.
756 return ARMComputeAddress(U->getOperand(0), Addr);
757 case Instruction::IntToPtr:
758 // Look past no-op inttoptrs.
759 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
761 return ARMComputeAddress(U->getOperand(0), Addr);
762 break;
763 case Instruction::PtrToInt:
764 // Look past no-op ptrtoints.
765 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
766 return ARMComputeAddress(U->getOperand(0), Addr);
767 break;
768 case Instruction::GetElementPtr: {
769 Address SavedAddr = Addr;
770 int TmpOffset = Addr.getOffset();
771
772 // Iterate through the GEP folding the constants into offsets where
773 // we can.
775 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
776 i != e; ++i, ++GTI) {
777 const Value *Op = *i;
778 if (StructType *STy = GTI.getStructTypeOrNull()) {
779 const StructLayout *SL = DL.getStructLayout(STy);
780 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
781 TmpOffset += SL->getElementOffset(Idx);
782 } else {
784 while (true) {
785 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
786 // Constant-offset addressing.
787 TmpOffset += CI->getSExtValue() * S;
788 break;
789 }
790 if (canFoldAddIntoGEP(U, Op)) {
791 // A compatible add with a constant operand. Fold the constant.
792 ConstantInt *CI =
793 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
794 TmpOffset += CI->getSExtValue() * S;
795 // Iterate on the other operand.
796 Op = cast<AddOperator>(Op)->getOperand(0);
797 continue;
798 }
799 // Unsupported
800 goto unsupported_gep;
801 }
802 }
803 }
804
805 // Try to grab the base operand now.
806 Addr.setOffset(TmpOffset);
807 if (ARMComputeAddress(U->getOperand(0), Addr)) return true;
808
809 // We failed, restore everything and try the other options.
810 Addr = SavedAddr;
811
812 unsupported_gep:
813 break;
814 }
815 case Instruction::Alloca: {
816 const AllocaInst *AI = cast<AllocaInst>(Obj);
817 auto SI = FuncInfo.StaticAllocaMap.find(AI);
818 if (SI != FuncInfo.StaticAllocaMap.end()) {
819 Addr.setKind(Address::FrameIndexBase);
820 Addr.setFI(SI->second);
821 return true;
822 }
823 break;
824 }
825 }
826
827 // Try to get this in a register if nothing else has worked.
828 if (!Addr.getReg())
829 Addr.setReg(getRegForValue(Obj));
830 return Addr.getReg();
831}
832
833void ARMFastISel::ARMSimplifyAddress(Address &Addr, MVT VT, bool useAM3) {
834 bool needsLowering = false;
835 switch (VT.SimpleTy) {
836 default: llvm_unreachable("Unhandled load/store type!");
837 case MVT::i1:
838 case MVT::i8:
839 case MVT::i16:
840 case MVT::i32:
841 if (!useAM3) {
842 // Integer loads/stores handle 12-bit offsets.
843 needsLowering = ((Addr.getOffset() & 0xfff) != Addr.getOffset());
844 // Handle negative offsets.
845 if (needsLowering && isThumb2)
846 needsLowering = !(Subtarget->hasV6T2Ops() && Addr.getOffset() < 0 &&
847 Addr.getOffset() > -256);
848 } else {
849 // ARM halfword load/stores and signed byte loads use +/-imm8 offsets.
850 needsLowering = (Addr.getOffset() > 255 || Addr.getOffset() < -255);
851 }
852 break;
853 case MVT::f32:
854 case MVT::f64:
855 // Floating point operands handle 8-bit offsets.
856 needsLowering = ((Addr.getOffset() & 0xff) != Addr.getOffset());
857 break;
858 }
859
860 // If this is a stack pointer and the offset needs to be simplified then
861 // put the alloca address into a register, set the base type back to
862 // register and continue. This should almost never happen.
863 if (needsLowering && Addr.isFIBase()) {
864 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
865 : &ARM::GPRRegClass;
866 Register ResultReg = createResultReg(RC);
867 unsigned Opc = isThumb2 ? ARM::t2ADDri : ARM::ADDri;
868 AddOptionalDefs(
869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
870 .addFrameIndex(Addr.getFI())
871 .addImm(0));
872 Addr.setKind(Address::RegBase);
873 Addr.setReg(ResultReg);
874 }
875
876 // Since the offset is too large for the load/store instruction
877 // get the reg+offset into a register.
878 if (needsLowering) {
879 Addr.setReg(fastEmit_ri_(MVT::i32, ISD::ADD, Addr.getReg(),
880 Addr.getOffset(), MVT::i32));
881 Addr.setOffset(0);
882 }
883}
884
885void ARMFastISel::AddLoadStoreOperands(MVT VT, Address &Addr,
886 const MachineInstrBuilder &MIB,
888 bool useAM3) {
889 // addrmode5 output depends on the selection dag addressing dividing the
890 // offset by 4 that it then later multiplies. Do this here as well.
891 if (VT.SimpleTy == MVT::f32 || VT.SimpleTy == MVT::f64)
892 Addr.setOffset(Addr.getOffset() / 4);
893
894 // Frame base works a bit differently. Handle it separately.
895 if (Addr.isFIBase()) {
896 int FI = Addr.getFI();
897 int Offset = Addr.getOffset();
898 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
899 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
900 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
901 // Now add the rest of the operands.
902 MIB.addFrameIndex(FI);
903
904 // ARM halfword load/stores and signed byte loads need an additional
905 // operand.
906 if (useAM3) {
907 int Imm = (Addr.getOffset() < 0) ? (0x100 | -Addr.getOffset())
908 : Addr.getOffset();
909 MIB.addReg(0);
910 MIB.addImm(Imm);
911 } else {
912 MIB.addImm(Addr.getOffset());
913 }
914 MIB.addMemOperand(MMO);
915 } else {
916 // Now add the rest of the operands.
917 MIB.addReg(Addr.getReg());
918
919 // ARM halfword load/stores and signed byte loads need an additional
920 // operand.
921 if (useAM3) {
922 int Imm = (Addr.getOffset() < 0) ? (0x100 | -Addr.getOffset())
923 : Addr.getOffset();
924 MIB.addReg(0);
925 MIB.addImm(Imm);
926 } else {
927 MIB.addImm(Addr.getOffset());
928 }
929 }
930 AddOptionalDefs(MIB);
931}
932
933bool ARMFastISel::ARMEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
934 MaybeAlign Alignment, bool isZExt,
935 bool allocReg) {
936 unsigned Opc;
937 bool useAM3 = false;
938 bool needVMOV = false;
939 const TargetRegisterClass *RC;
940 switch (VT.SimpleTy) {
941 // This is mostly going to be Neon/vector support.
942 default: return false;
943 case MVT::i1:
944 case MVT::i8:
945 if (isThumb2) {
946 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
947 Subtarget->hasV6T2Ops())
948 Opc = isZExt ? ARM::t2LDRBi8 : ARM::t2LDRSBi8;
949 else
950 Opc = isZExt ? ARM::t2LDRBi12 : ARM::t2LDRSBi12;
951 } else {
952 if (isZExt) {
953 Opc = ARM::LDRBi12;
954 } else {
955 Opc = ARM::LDRSB;
956 useAM3 = true;
957 }
958 }
959 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
960 break;
961 case MVT::i16:
962 if (Alignment && *Alignment < Align(2) &&
963 !Subtarget->allowsUnalignedMem())
964 return false;
965
966 if (isThumb2) {
967 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
968 Subtarget->hasV6T2Ops())
969 Opc = isZExt ? ARM::t2LDRHi8 : ARM::t2LDRSHi8;
970 else
971 Opc = isZExt ? ARM::t2LDRHi12 : ARM::t2LDRSHi12;
972 } else {
973 Opc = isZExt ? ARM::LDRH : ARM::LDRSH;
974 useAM3 = true;
975 }
976 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
977 break;
978 case MVT::i32:
979 if (Alignment && *Alignment < Align(4) &&
980 !Subtarget->allowsUnalignedMem())
981 return false;
982
983 if (isThumb2) {
984 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
985 Subtarget->hasV6T2Ops())
986 Opc = ARM::t2LDRi8;
987 else
988 Opc = ARM::t2LDRi12;
989 } else {
990 Opc = ARM::LDRi12;
991 }
992 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
993 break;
994 case MVT::f32:
995 if (!Subtarget->hasVFP2Base()) return false;
996 // Unaligned loads need special handling. Floats require word-alignment.
997 if (Alignment && *Alignment < Align(4)) {
998 needVMOV = true;
999 VT = MVT::i32;
1000 Opc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
1001 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRnopcRegClass;
1002 } else {
1003 Opc = ARM::VLDRS;
1004 RC = TLI.getRegClassFor(VT);
1005 }
1006 break;
1007 case MVT::f64:
1008 // Can load and store double precision even without FeatureFP64
1009 if (!Subtarget->hasVFP2Base()) return false;
1010 // FIXME: Unaligned loads need special handling. Doublewords require
1011 // word-alignment.
1012 if (Alignment && *Alignment < Align(4))
1013 return false;
1014
1015 Opc = ARM::VLDRD;
1016 RC = TLI.getRegClassFor(VT);
1017 break;
1018 }
1019 // Simplify this down to something we can handle.
1020 ARMSimplifyAddress(Addr, VT, useAM3);
1021
1022 // Create the base instruction, then add the operands.
1023 if (allocReg)
1024 ResultReg = createResultReg(RC);
1025 assert(ResultReg.isVirtual() && "Expected an allocated virtual register.");
1026 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1027 TII.get(Opc), ResultReg);
1028 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOLoad, useAM3);
1029
1030 // If we had an unaligned load of a float we've converted it to an regular
1031 // load. Now we must move from the GRP to the FP register.
1032 if (needVMOV) {
1033 Register MoveReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1034 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1035 TII.get(ARM::VMOVSR), MoveReg)
1036 .addReg(ResultReg));
1037 ResultReg = MoveReg;
1038 }
1039 return true;
1040}
1041
1042bool ARMFastISel::SelectLoad(const Instruction *I) {
1043 // Atomic loads need special handling.
1044 if (cast<LoadInst>(I)->isAtomic())
1045 return false;
1046
1047 const Value *SV = I->getOperand(0);
1048 if (TLI.supportSwiftError()) {
1049 // Swifterror values can come from either a function parameter with
1050 // swifterror attribute or an alloca with swifterror attribute.
1051 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1052 if (Arg->hasSwiftErrorAttr())
1053 return false;
1054 }
1055
1056 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1057 if (Alloca->isSwiftError())
1058 return false;
1059 }
1060 }
1061
1062 // Verify we have a legal type before going any further.
1063 MVT VT;
1064 if (!isLoadTypeLegal(I->getType(), VT))
1065 return false;
1066
1067 // See if we can handle this address.
1068 Address Addr;
1069 if (!ARMComputeAddress(I->getOperand(0), Addr)) return false;
1070
1071 Register ResultReg;
1072 if (!ARMEmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlign()))
1073 return false;
1074 updateValueMap(I, ResultReg);
1075 return true;
1076}
1077
1078bool ARMFastISel::ARMEmitStore(MVT VT, Register SrcReg, Address &Addr,
1079 MaybeAlign Alignment) {
1080 unsigned StrOpc;
1081 bool useAM3 = false;
1082 switch (VT.SimpleTy) {
1083 // This is mostly going to be Neon/vector support.
1084 default: return false;
1085 case MVT::i1: {
1086 Register Res = createResultReg(isThumb2 ? &ARM::tGPRRegClass
1087 : &ARM::GPRRegClass);
1088 unsigned Opc = isThumb2 ? ARM::t2ANDri : ARM::ANDri;
1089 SrcReg = constrainOperandRegClass(TII.get(Opc), SrcReg, 1);
1090 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1091 TII.get(Opc), Res)
1092 .addReg(SrcReg).addImm(1));
1093 SrcReg = Res;
1094 [[fallthrough]];
1095 }
1096 case MVT::i8:
1097 if (isThumb2) {
1098 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
1099 Subtarget->hasV6T2Ops())
1100 StrOpc = ARM::t2STRBi8;
1101 else
1102 StrOpc = ARM::t2STRBi12;
1103 } else {
1104 StrOpc = ARM::STRBi12;
1105 }
1106 break;
1107 case MVT::i16:
1108 if (Alignment && *Alignment < Align(2) &&
1109 !Subtarget->allowsUnalignedMem())
1110 return false;
1111
1112 if (isThumb2) {
1113 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
1114 Subtarget->hasV6T2Ops())
1115 StrOpc = ARM::t2STRHi8;
1116 else
1117 StrOpc = ARM::t2STRHi12;
1118 } else {
1119 StrOpc = ARM::STRH;
1120 useAM3 = true;
1121 }
1122 break;
1123 case MVT::i32:
1124 if (Alignment && *Alignment < Align(4) &&
1125 !Subtarget->allowsUnalignedMem())
1126 return false;
1127
1128 if (isThumb2) {
1129 if (Addr.getOffset() < 0 && Addr.getOffset() > -256 &&
1130 Subtarget->hasV6T2Ops())
1131 StrOpc = ARM::t2STRi8;
1132 else
1133 StrOpc = ARM::t2STRi12;
1134 } else {
1135 StrOpc = ARM::STRi12;
1136 }
1137 break;
1138 case MVT::f32:
1139 if (!Subtarget->hasVFP2Base()) return false;
1140 // Unaligned stores need special handling. Floats require word-alignment.
1141 if (Alignment && *Alignment < Align(4)) {
1142 Register MoveReg = createResultReg(TLI.getRegClassFor(MVT::i32));
1143 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1144 TII.get(ARM::VMOVRS), MoveReg)
1145 .addReg(SrcReg));
1146 SrcReg = MoveReg;
1147 VT = MVT::i32;
1148 StrOpc = isThumb2 ? ARM::t2STRi12 : ARM::STRi12;
1149 } else {
1150 StrOpc = ARM::VSTRS;
1151 }
1152 break;
1153 case MVT::f64:
1154 // Can load and store double precision even without FeatureFP64
1155 if (!Subtarget->hasVFP2Base()) return false;
1156 // FIXME: Unaligned stores need special handling. Doublewords require
1157 // word-alignment.
1158 if (Alignment && *Alignment < Align(4))
1159 return false;
1160
1161 StrOpc = ARM::VSTRD;
1162 break;
1163 }
1164 // Simplify this down to something we can handle.
1165 ARMSimplifyAddress(Addr, VT, useAM3);
1166
1167 // Create the base instruction, then add the operands.
1168 SrcReg = constrainOperandRegClass(TII.get(StrOpc), SrcReg, 0);
1169 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1170 TII.get(StrOpc))
1171 .addReg(SrcReg);
1172 AddLoadStoreOperands(VT, Addr, MIB, MachineMemOperand::MOStore, useAM3);
1173 return true;
1174}
1175
1176bool ARMFastISel::SelectStore(const Instruction *I) {
1177 Value *Op0 = I->getOperand(0);
1178 Register SrcReg;
1179
1180 // Atomic stores need special handling.
1181 if (cast<StoreInst>(I)->isAtomic())
1182 return false;
1183
1184 const Value *PtrV = I->getOperand(1);
1185 if (TLI.supportSwiftError()) {
1186 // Swifterror values can come from either a function parameter with
1187 // swifterror attribute or an alloca with swifterror attribute.
1188 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1189 if (Arg->hasSwiftErrorAttr())
1190 return false;
1191 }
1192
1193 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1194 if (Alloca->isSwiftError())
1195 return false;
1196 }
1197 }
1198
1199 // Verify we have a legal type before going any further.
1200 MVT VT;
1201 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
1202 return false;
1203
1204 // Get the value to be stored into a register.
1205 SrcReg = getRegForValue(Op0);
1206 if (!SrcReg)
1207 return false;
1208
1209 // See if we can handle this address.
1210 Address Addr;
1211 if (!ARMComputeAddress(I->getOperand(1), Addr))
1212 return false;
1213
1214 if (!ARMEmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlign()))
1215 return false;
1216 return true;
1217}
1218
1220 switch (Pred) {
1221 // Needs two compares...
1222 case CmpInst::FCMP_ONE:
1223 case CmpInst::FCMP_UEQ:
1224 default:
1225 // AL is our "false" for now. The other two need more compares.
1226 return ARMCC::AL;
1227 case CmpInst::ICMP_EQ:
1228 case CmpInst::FCMP_OEQ:
1229 return ARMCC::EQ;
1230 case CmpInst::ICMP_SGT:
1231 case CmpInst::FCMP_OGT:
1232 return ARMCC::GT;
1233 case CmpInst::ICMP_SGE:
1234 case CmpInst::FCMP_OGE:
1235 return ARMCC::GE;
1236 case CmpInst::ICMP_UGT:
1237 case CmpInst::FCMP_UGT:
1238 return ARMCC::HI;
1239 case CmpInst::FCMP_OLT:
1240 return ARMCC::MI;
1241 case CmpInst::ICMP_ULE:
1242 case CmpInst::FCMP_OLE:
1243 return ARMCC::LS;
1244 case CmpInst::FCMP_ORD:
1245 return ARMCC::VC;
1246 case CmpInst::FCMP_UNO:
1247 return ARMCC::VS;
1248 case CmpInst::FCMP_UGE:
1249 return ARMCC::PL;
1250 case CmpInst::ICMP_SLT:
1251 case CmpInst::FCMP_ULT:
1252 return ARMCC::LT;
1253 case CmpInst::ICMP_SLE:
1254 case CmpInst::FCMP_ULE:
1255 return ARMCC::LE;
1256 case CmpInst::FCMP_UNE:
1257 case CmpInst::ICMP_NE:
1258 return ARMCC::NE;
1259 case CmpInst::ICMP_UGE:
1260 return ARMCC::HS;
1261 case CmpInst::ICMP_ULT:
1262 return ARMCC::LO;
1263 }
1264}
1265
1266bool ARMFastISel::SelectBranch(const Instruction *I) {
1267 const CondBrInst *BI = cast<CondBrInst>(I);
1268 MachineBasicBlock *TBB = FuncInfo.getMBB(BI->getSuccessor(0));
1269 MachineBasicBlock *FBB = FuncInfo.getMBB(BI->getSuccessor(1));
1270
1271 // Simple branch support.
1272
1273 // If we can, avoid recomputing the compare - redoing it could lead to wonky
1274 // behavior.
1275 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1276 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1277 // Get the compare predicate.
1278 // Try to take advantage of fallthrough opportunities.
1279 CmpInst::Predicate Predicate = CI->getPredicate();
1280 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1281 std::swap(TBB, FBB);
1283 }
1284
1285 ARMCC::CondCodes ARMPred = getComparePred(Predicate);
1286
1287 // We may not handle every CC for now.
1288 if (ARMPred == ARMCC::AL) return false;
1289
1290 // Emit the compare.
1291 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1292 return false;
1293
1294 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(BrOpc))
1296 .addMBB(TBB).addImm(ARMPred).addReg(ARM::CPSR);
1297 finishCondBranch(BI->getParent(), TBB, FBB);
1298 return true;
1299 }
1300 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1301 MVT SourceVT;
1302 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1303 (isLoadTypeLegal(TI->getOperand(0)->getType(), SourceVT))) {
1304 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1305 Register OpReg = getRegForValue(TI->getOperand(0));
1306 OpReg = constrainOperandRegClass(TII.get(TstOpc), OpReg, 0);
1307 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1308 TII.get(TstOpc))
1309 .addReg(OpReg).addImm(1));
1310
1311 unsigned CCMode = ARMCC::NE;
1312 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1313 std::swap(TBB, FBB);
1314 CCMode = ARMCC::EQ;
1315 }
1316
1317 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1318 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(BrOpc))
1319 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1320
1321 finishCondBranch(BI->getParent(), TBB, FBB);
1322 return true;
1323 }
1324 } else if (const ConstantInt *CI =
1326 uint64_t Imm = CI->getZExtValue();
1327 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1328 fastEmitBranch(Target, MIMD.getDL());
1329 return true;
1330 }
1331
1332 Register CmpReg = getRegForValue(BI->getCondition());
1333 if (!CmpReg)
1334 return false;
1335
1336 // We've been divorced from our compare! Our block was split, and
1337 // now our compare lives in a predecessor block. We musn't
1338 // re-compare here, as the children of the compare aren't guaranteed
1339 // live across the block boundary (we *could* check for this).
1340 // Regardless, the compare has been done in the predecessor block,
1341 // and it left a value for us in a virtual register. Ergo, we test
1342 // the one-bit value left in the virtual register.
1343 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1344 CmpReg = constrainOperandRegClass(TII.get(TstOpc), CmpReg, 0);
1345 AddOptionalDefs(
1346 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(TstOpc))
1347 .addReg(CmpReg)
1348 .addImm(1));
1349
1350 unsigned CCMode = ARMCC::NE;
1351 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1352 std::swap(TBB, FBB);
1353 CCMode = ARMCC::EQ;
1354 }
1355
1356 unsigned BrOpc = isThumb2 ? ARM::t2Bcc : ARM::Bcc;
1357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(BrOpc))
1358 .addMBB(TBB).addImm(CCMode).addReg(ARM::CPSR);
1359 finishCondBranch(BI->getParent(), TBB, FBB);
1360 return true;
1361}
1362
1363bool ARMFastISel::SelectIndirectBr(const Instruction *I) {
1364 Register AddrReg = getRegForValue(I->getOperand(0));
1365 if (!AddrReg)
1366 return false;
1367
1368 unsigned Opc = isThumb2 ? ARM::tBRIND : ARM::BX;
1369 assert(isThumb2 || Subtarget->hasV4TOps());
1370
1371 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1372 TII.get(Opc)).addReg(AddrReg));
1373
1374 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1375 for (const BasicBlock *SuccBB : IB->successors())
1376 FuncInfo.MBB->addSuccessor(FuncInfo.getMBB(SuccBB));
1377
1378 return true;
1379}
1380
1381bool ARMFastISel::ARMEmitCmp(const Value *Src1Value, const Value *Src2Value,
1382 bool isZExt) {
1383 Type *Ty = Src1Value->getType();
1384 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
1385 if (!SrcEVT.isSimple()) return false;
1386 MVT SrcVT = SrcEVT.getSimpleVT();
1387
1388 if (Ty->isFloatTy() && !Subtarget->hasVFP2Base())
1389 return false;
1390
1391 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2Base() || !Subtarget->hasFP64()))
1392 return false;
1393
1394 // Check to see if the 2nd operand is a constant that we can encode directly
1395 // in the compare.
1396 int Imm = 0;
1397 bool UseImm = false;
1398 bool isNegativeImm = false;
1399 // FIXME: At -O0 we don't have anything that canonicalizes operand order.
1400 // Thus, Src1Value may be a ConstantInt, but we're missing it.
1401 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
1402 if (SrcVT == MVT::i32 || SrcVT == MVT::i16 || SrcVT == MVT::i8 ||
1403 SrcVT == MVT::i1) {
1404 const APInt &CIVal = ConstInt->getValue();
1405 Imm = (isZExt) ? (int)CIVal.getZExtValue() : (int)CIVal.getSExtValue();
1406 // For INT_MIN/LONG_MIN (i.e., 0x80000000) we need to use a cmp, rather
1407 // then a cmn, because there is no way to represent 2147483648 as a
1408 // signed 32-bit int.
1409 if (Imm < 0 && Imm != (int)0x80000000) {
1410 isNegativeImm = true;
1411 Imm = -Imm;
1412 }
1413 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1414 (ARM_AM::getSOImmVal(Imm) != -1);
1415 }
1416 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
1417 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
1418 if (ConstFP->isZero() && !ConstFP->isNegative())
1419 UseImm = true;
1420 }
1421
1422 unsigned CmpOpc;
1423 bool isICmp = true;
1424 bool needsExt = false;
1425 switch (SrcVT.SimpleTy) {
1426 default: return false;
1427 // TODO: Verify compares.
1428 case MVT::f32:
1429 isICmp = false;
1430 CmpOpc = UseImm ? ARM::VCMPZS : ARM::VCMPS;
1431 break;
1432 case MVT::f64:
1433 isICmp = false;
1434 CmpOpc = UseImm ? ARM::VCMPZD : ARM::VCMPD;
1435 break;
1436 case MVT::i1:
1437 case MVT::i8:
1438 case MVT::i16:
1439 needsExt = true;
1440 [[fallthrough]];
1441 case MVT::i32:
1442 if (isThumb2) {
1443 if (!UseImm)
1444 CmpOpc = ARM::t2CMPrr;
1445 else
1446 CmpOpc = isNegativeImm ? ARM::t2CMNri : ARM::t2CMPri;
1447 } else {
1448 if (!UseImm)
1449 CmpOpc = ARM::CMPrr;
1450 else
1451 CmpOpc = isNegativeImm ? ARM::CMNri : ARM::CMPri;
1452 }
1453 break;
1454 }
1455
1456 Register SrcReg1 = getRegForValue(Src1Value);
1457 if (!SrcReg1)
1458 return false;
1459
1460 Register SrcReg2;
1461 if (!UseImm) {
1462 SrcReg2 = getRegForValue(Src2Value);
1463 if (!SrcReg2)
1464 return false;
1465 }
1466
1467 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1468 if (needsExt) {
1469 SrcReg1 = ARMEmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1470 if (!SrcReg1)
1471 return false;
1472 if (!UseImm) {
1473 SrcReg2 = ARMEmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1474 if (!SrcReg2)
1475 return false;
1476 }
1477 }
1478
1479 const MCInstrDesc &II = TII.get(CmpOpc);
1480 SrcReg1 = constrainOperandRegClass(II, SrcReg1, 0);
1481 if (!UseImm) {
1482 SrcReg2 = constrainOperandRegClass(II, SrcReg2, 1);
1483 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
1484 .addReg(SrcReg1).addReg(SrcReg2));
1485 } else {
1486 MachineInstrBuilder MIB;
1487 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, II)
1488 .addReg(SrcReg1);
1489
1490 // Only add immediate for icmp as the immediate for fcmp is an implicit 0.0.
1491 if (isICmp)
1492 MIB.addImm(Imm);
1493 AddOptionalDefs(MIB);
1494 }
1495
1496 // For floating point we need to move the result to a comparison register
1497 // that we can then use for branches.
1498 if (Ty->isFloatTy() || Ty->isDoubleTy())
1499 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1500 TII.get(ARM::FMSTAT)));
1501 return true;
1502}
1503
1504bool ARMFastISel::SelectCmp(const Instruction *I) {
1505 const CmpInst *CI = cast<CmpInst>(I);
1506
1507 // Get the compare predicate.
1509
1510 // We may not handle every CC for now.
1511 if (ARMPred == ARMCC::AL) return false;
1512
1513 // Emit the compare.
1514 if (!ARMEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1515 return false;
1516
1517 // Now set a register based on the comparison. Explicitly set the predicates
1518 // here.
1519 unsigned MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1520 const TargetRegisterClass *RC = isThumb2 ? &ARM::rGPRRegClass
1521 : &ARM::GPRRegClass;
1522 Register DestReg = createResultReg(RC);
1523 Constant *Zero = ConstantInt::get(Type::getInt32Ty(*Context), 0);
1524 Register ZeroReg = fastMaterializeConstant(Zero);
1525 // ARMEmitCmp emits a FMSTAT when necessary, so it's always safe to use CPSR.
1526 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(MovCCOpc), DestReg)
1527 .addReg(ZeroReg).addImm(1)
1528 .addImm(ARMPred).addReg(ARM::CPSR);
1529
1530 updateValueMap(I, DestReg);
1531 return true;
1532}
1533
1534bool ARMFastISel::SelectFPExt(const Instruction *I) {
1535 // Make sure we have VFP and that we're extending float to double.
1536 if (!Subtarget->hasVFP2Base() || !Subtarget->hasFP64()) return false;
1537
1538 Value *V = I->getOperand(0);
1539 if (!I->getType()->isDoubleTy() ||
1540 !V->getType()->isFloatTy()) return false;
1541
1542 Register Op = getRegForValue(V);
1543 if (!Op)
1544 return false;
1545
1546 Register Result = createResultReg(&ARM::DPRRegClass);
1547 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1548 TII.get(ARM::VCVTDS), Result)
1549 .addReg(Op));
1550 updateValueMap(I, Result);
1551 return true;
1552}
1553
1554bool ARMFastISel::SelectFPTrunc(const Instruction *I) {
1555 // Make sure we have VFP and that we're truncating double to float.
1556 if (!Subtarget->hasVFP2Base() || !Subtarget->hasFP64()) return false;
1557
1558 Value *V = I->getOperand(0);
1559 if (!(I->getType()->isFloatTy() &&
1560 V->getType()->isDoubleTy())) return false;
1561
1562 Register Op = getRegForValue(V);
1563 if (!Op)
1564 return false;
1565
1566 Register Result = createResultReg(&ARM::SPRRegClass);
1567 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1568 TII.get(ARM::VCVTSD), Result)
1569 .addReg(Op));
1570 updateValueMap(I, Result);
1571 return true;
1572}
1573
1574bool ARMFastISel::SelectIToFP(const Instruction *I, bool isSigned) {
1575 // Make sure we have VFP.
1576 if (!Subtarget->hasVFP2Base()) return false;
1577
1578 MVT DstVT;
1579 Type *Ty = I->getType();
1580 if (!isTypeLegal(Ty, DstVT))
1581 return false;
1582
1583 Value *Src = I->getOperand(0);
1584 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
1585 if (!SrcEVT.isSimple())
1586 return false;
1587 MVT SrcVT = SrcEVT.getSimpleVT();
1588 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1589 return false;
1590
1591 Register SrcReg = getRegForValue(Src);
1592 if (!SrcReg)
1593 return false;
1594
1595 // Handle sign-extension.
1596 if (SrcVT == MVT::i16 || SrcVT == MVT::i8) {
1597 SrcReg = ARMEmitIntExt(SrcVT, SrcReg, MVT::i32,
1598 /*isZExt*/!isSigned);
1599 if (!SrcReg)
1600 return false;
1601 }
1602
1603 // The conversion routine works on fp-reg to fp-reg and the operand above
1604 // was an integer, move it to the fp registers if possible.
1605 Register FP = ARMMoveToFPReg(MVT::f32, SrcReg);
1606 if (!FP)
1607 return false;
1608
1609 unsigned Opc;
1610 if (Ty->isFloatTy()) Opc = isSigned ? ARM::VSITOS : ARM::VUITOS;
1611 else if (Ty->isDoubleTy() && Subtarget->hasFP64())
1612 Opc = isSigned ? ARM::VSITOD : ARM::VUITOD;
1613 else return false;
1614
1615 Register ResultReg = createResultReg(TLI.getRegClassFor(DstVT));
1616 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1617 TII.get(Opc), ResultReg).addReg(FP));
1618 updateValueMap(I, ResultReg);
1619 return true;
1620}
1621
1622bool ARMFastISel::SelectFPToI(const Instruction *I, bool isSigned) {
1623 // Make sure we have VFP.
1624 if (!Subtarget->hasVFP2Base()) return false;
1625
1626 MVT DstVT;
1627 Type *RetTy = I->getType();
1628 if (!isTypeLegal(RetTy, DstVT))
1629 return false;
1630
1631 Register Op = getRegForValue(I->getOperand(0));
1632 if (!Op)
1633 return false;
1634
1635 unsigned Opc;
1636 Type *OpTy = I->getOperand(0)->getType();
1637 if (OpTy->isFloatTy()) Opc = isSigned ? ARM::VTOSIZS : ARM::VTOUIZS;
1638 else if (OpTy->isDoubleTy() && Subtarget->hasFP64())
1639 Opc = isSigned ? ARM::VTOSIZD : ARM::VTOUIZD;
1640 else return false;
1641
1642 // f64->s32/u32 or f32->s32/u32 both need an intermediate f32 reg.
1643 Register ResultReg = createResultReg(TLI.getRegClassFor(MVT::f32));
1644 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1645 TII.get(Opc), ResultReg).addReg(Op));
1646
1647 // This result needs to be in an integer register, but the conversion only
1648 // takes place in fp-regs.
1649 Register IntReg = ARMMoveToIntReg(DstVT, ResultReg);
1650 if (!IntReg)
1651 return false;
1652
1653 updateValueMap(I, IntReg);
1654 return true;
1655}
1656
1657bool ARMFastISel::SelectSelect(const Instruction *I) {
1658 MVT VT;
1659 if (!isTypeLegal(I->getType(), VT))
1660 return false;
1661
1662 // Things need to be register sized for register moves.
1663 if (VT != MVT::i32) return false;
1664
1665 Register CondReg = getRegForValue(I->getOperand(0));
1666 if (!CondReg)
1667 return false;
1668 Register Op1Reg = getRegForValue(I->getOperand(1));
1669 if (!Op1Reg)
1670 return false;
1671
1672 // Check to see if we can use an immediate in the conditional move.
1673 int Imm = 0;
1674 bool UseImm = false;
1675 bool isNegativeImm = false;
1676 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(2))) {
1677 assert(VT == MVT::i32 && "Expecting an i32.");
1678 Imm = (int)ConstInt->getValue().getZExtValue();
1679 if (Imm < 0) {
1680 isNegativeImm = true;
1681 Imm = ~Imm;
1682 }
1683 UseImm = isThumb2 ? (ARM_AM::getT2SOImmVal(Imm) != -1) :
1684 (ARM_AM::getSOImmVal(Imm) != -1);
1685 }
1686
1687 Register Op2Reg;
1688 if (!UseImm) {
1689 Op2Reg = getRegForValue(I->getOperand(2));
1690 if (!Op2Reg)
1691 return false;
1692 }
1693
1694 unsigned TstOpc = isThumb2 ? ARM::t2TSTri : ARM::TSTri;
1695 CondReg = constrainOperandRegClass(TII.get(TstOpc), CondReg, 0);
1696 AddOptionalDefs(
1697 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(TstOpc))
1698 .addReg(CondReg)
1699 .addImm(1));
1700
1701 unsigned MovCCOpc;
1702 const TargetRegisterClass *RC;
1703 if (!UseImm) {
1704 RC = isThumb2 ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
1705 MovCCOpc = isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr;
1706 } else {
1707 RC = isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass;
1708 if (!isNegativeImm)
1709 MovCCOpc = isThumb2 ? ARM::t2MOVCCi : ARM::MOVCCi;
1710 else
1711 MovCCOpc = isThumb2 ? ARM::t2MVNCCi : ARM::MVNCCi;
1712 }
1713 Register ResultReg = createResultReg(RC);
1714 if (!UseImm) {
1715 Op2Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op2Reg, 1);
1716 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 2);
1717 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(MovCCOpc),
1718 ResultReg)
1719 .addReg(Op2Reg)
1720 .addReg(Op1Reg)
1722 .addReg(ARM::CPSR);
1723 } else {
1724 Op1Reg = constrainOperandRegClass(TII.get(MovCCOpc), Op1Reg, 1);
1725 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(MovCCOpc),
1726 ResultReg)
1727 .addReg(Op1Reg)
1728 .addImm(Imm)
1730 .addReg(ARM::CPSR);
1731 }
1732 updateValueMap(I, ResultReg);
1733 return true;
1734}
1735
1736bool ARMFastISel::SelectDiv(const Instruction *I, bool isSigned) {
1737 MVT VT;
1738 Type *Ty = I->getType();
1739 if (!isTypeLegal(Ty, VT))
1740 return false;
1741
1742 // If we have integer div support we should have selected this automagically.
1743 // In case we have a real miss go ahead and return false and we'll pick
1744 // it up later.
1745 if (Subtarget->hasDivideInThumbMode())
1746 return false;
1747
1748 // Otherwise emit a libcall.
1749 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1750 if (VT == MVT::i8)
1751 LC = isSigned ? RTLIB::SDIV_I8 : RTLIB::UDIV_I8;
1752 else if (VT == MVT::i16)
1753 LC = isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16;
1754 else if (VT == MVT::i32)
1755 LC = isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32;
1756 else if (VT == MVT::i64)
1757 LC = isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64;
1758 else if (VT == MVT::i128)
1759 LC = isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128;
1760 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1761
1762 return ARMEmitLibcall(I, LC);
1763}
1764
1765bool ARMFastISel::SelectRem(const Instruction *I, bool isSigned) {
1766 MVT VT;
1767 Type *Ty = I->getType();
1768 if (!isTypeLegal(Ty, VT))
1769 return false;
1770
1771 // Many ABIs do not provide a libcall for standalone remainder, so we need to
1772 // use divrem (see the RTABI 4.3.1). Since FastISel can't handle non-double
1773 // multi-reg returns, we'll have to bail out.
1774 if (!TLI.hasStandaloneRem(VT)) {
1775 return false;
1776 }
1777
1778 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1779 if (VT == MVT::i8)
1780 LC = isSigned ? RTLIB::SREM_I8 : RTLIB::UREM_I8;
1781 else if (VT == MVT::i16)
1782 LC = isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16;
1783 else if (VT == MVT::i32)
1784 LC = isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32;
1785 else if (VT == MVT::i64)
1786 LC = isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64;
1787 else if (VT == MVT::i128)
1788 LC = isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128;
1789 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1790
1791 return ARMEmitLibcall(I, LC);
1792}
1793
1794bool ARMFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1795 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1796
1797 // We can get here in the case when we have a binary operation on a non-legal
1798 // type and the target independent selector doesn't know how to handle it.
1799 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1800 return false;
1801
1802 unsigned Opc;
1803 switch (ISDOpcode) {
1804 default: return false;
1805 case ISD::ADD:
1806 Opc = isThumb2 ? ARM::t2ADDrr : ARM::ADDrr;
1807 break;
1808 case ISD::OR:
1809 Opc = isThumb2 ? ARM::t2ORRrr : ARM::ORRrr;
1810 break;
1811 case ISD::SUB:
1812 Opc = isThumb2 ? ARM::t2SUBrr : ARM::SUBrr;
1813 break;
1814 }
1815
1816 Register SrcReg1 = getRegForValue(I->getOperand(0));
1817 if (!SrcReg1)
1818 return false;
1819
1820 // TODO: Often the 2nd operand is an immediate, which can be encoded directly
1821 // in the instruction, rather then materializing the value in a register.
1822 Register SrcReg2 = getRegForValue(I->getOperand(1));
1823 if (!SrcReg2)
1824 return false;
1825
1826 Register ResultReg = createResultReg(&ARM::GPRnopcRegClass);
1827 SrcReg1 = constrainOperandRegClass(TII.get(Opc), SrcReg1, 1);
1828 SrcReg2 = constrainOperandRegClass(TII.get(Opc), SrcReg2, 2);
1829 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1830 TII.get(Opc), ResultReg)
1831 .addReg(SrcReg1).addReg(SrcReg2));
1832 updateValueMap(I, ResultReg);
1833 return true;
1834}
1835
1836bool ARMFastISel::SelectBinaryFPOp(const Instruction *I, unsigned ISDOpcode) {
1837 EVT FPVT = TLI.getValueType(DL, I->getType(), true);
1838 if (!FPVT.isSimple()) return false;
1839 MVT VT = FPVT.getSimpleVT();
1840
1841 // FIXME: Support vector types where possible.
1842 if (VT.isVector())
1843 return false;
1844
1845 // We can get here in the case when we want to use NEON for our fp
1846 // operations, but can't figure out how to. Just use the vfp instructions
1847 // if we have them.
1848 // FIXME: It'd be nice to use NEON instructions.
1849 Type *Ty = I->getType();
1850 if (Ty->isFloatTy() && !Subtarget->hasVFP2Base())
1851 return false;
1852 if (Ty->isDoubleTy() && (!Subtarget->hasVFP2Base() || !Subtarget->hasFP64()))
1853 return false;
1854
1855 unsigned Opc;
1856 bool is64bit = VT == MVT::f64 || VT == MVT::i64;
1857 switch (ISDOpcode) {
1858 default: return false;
1859 case ISD::FADD:
1860 Opc = is64bit ? ARM::VADDD : ARM::VADDS;
1861 break;
1862 case ISD::FSUB:
1863 Opc = is64bit ? ARM::VSUBD : ARM::VSUBS;
1864 break;
1865 case ISD::FMUL:
1866 Opc = is64bit ? ARM::VMULD : ARM::VMULS;
1867 break;
1868 }
1869 Register Op1 = getRegForValue(I->getOperand(0));
1870 if (!Op1)
1871 return false;
1872
1873 Register Op2 = getRegForValue(I->getOperand(1));
1874 if (!Op2)
1875 return false;
1876
1877 Register ResultReg = createResultReg(TLI.getRegClassFor(VT.SimpleTy));
1878 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1879 TII.get(Opc), ResultReg)
1880 .addReg(Op1).addReg(Op2));
1881 updateValueMap(I, ResultReg);
1882 return true;
1883}
1884
1885// Call Handling Code
1886
1887// This is largely taken directly from CCAssignFnForNode
1888// TODO: We may not support all of this.
1889CCAssignFn *ARMFastISel::CCAssignFnForCall(CallingConv::ID CC,
1890 bool Return,
1891 bool isVarArg) {
1892 switch (CC) {
1893 default:
1894 report_fatal_error("Unsupported calling convention");
1895 case CallingConv::Fast:
1896 if (Subtarget->hasFPRegs() && !isVarArg) {
1897 if (!Subtarget->isAAPCS_ABI())
1898 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1899 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1900 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1901 }
1902 [[fallthrough]];
1903 case CallingConv::C:
1904 case CallingConv::CXX_FAST_TLS:
1905 // Use target triple & subtarget features to do actual dispatch.
1906 if (Subtarget->isAAPCS_ABI()) {
1907 if (Subtarget->hasFPRegs() && Subtarget->isTargetHardFloat() && !isVarArg)
1908 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1909 else
1910 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1911 } else {
1912 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1913 }
1914 case CallingConv::ARM_AAPCS_VFP:
1915 case CallingConv::Swift:
1916 case CallingConv::SwiftTail:
1917 if (!isVarArg)
1918 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
1919 // Fall through to soft float variant, variadic functions don't
1920 // use hard floating point ABI.
1921 [[fallthrough]];
1922 case CallingConv::ARM_AAPCS:
1923 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
1924 case CallingConv::ARM_APCS:
1925 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
1926 case CallingConv::GHC:
1927 if (Return)
1928 report_fatal_error("Can't return in GHC call convention");
1929 else
1930 return CC_ARM_APCS_GHC;
1931 case CallingConv::CFGuard_Check:
1932 return (Return ? RetCC_ARM_AAPCS : CC_ARM_Win32_CFGuard_Check);
1933 }
1934}
1935
1936bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
1937 SmallVectorImpl<Register> &ArgRegs,
1938 SmallVectorImpl<MVT> &ArgVTs,
1939 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1940 SmallVectorImpl<Register> &RegArgs,
1941 CallingConv::ID CC,
1942 unsigned &NumBytes,
1943 bool isVarArg) {
1946 for (Value *Arg : Args)
1947 OrigTys.push_back(Arg->getType());
1948 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, ArgLocs, *Context);
1949 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, OrigTys,
1950 CCAssignFnForCall(CC, false, isVarArg));
1951
1952 // Check that we can handle all of the arguments. If we can't, then bail out
1953 // now before we add code to the MBB.
1954 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1955 CCValAssign &VA = ArgLocs[i];
1956 MVT ArgVT = ArgVTs[VA.getValNo()];
1957
1958 // We don't handle NEON/vector parameters yet.
1959 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1960 return false;
1961
1962 // Now copy/store arg to correct locations.
1963 if (VA.isRegLoc() && !VA.needsCustom()) {
1964 continue;
1965 } else if (VA.needsCustom()) {
1966 // TODO: We need custom lowering for vector (v2f64) args.
1967 if (VA.getLocVT() != MVT::f64 ||
1968 // TODO: Only handle register args for now.
1969 !VA.isRegLoc() || !ArgLocs[++i].isRegLoc())
1970 return false;
1971 } else {
1972 switch (ArgVT.SimpleTy) {
1973 default:
1974 return false;
1975 case MVT::i1:
1976 case MVT::i8:
1977 case MVT::i16:
1978 case MVT::i32:
1979 break;
1980 case MVT::f32:
1981 if (!Subtarget->hasVFP2Base())
1982 return false;
1983 break;
1984 case MVT::f64:
1985 if (!Subtarget->hasVFP2Base())
1986 return false;
1987 break;
1988 }
1989 }
1990 }
1991
1992 // At the point, we are able to handle the call's arguments in fast isel.
1993
1994 // Get a count of how many bytes are to be pushed on the stack.
1995 NumBytes = CCInfo.getStackSize();
1996
1997 // Issue CALLSEQ_START
1998 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1999 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2000 TII.get(AdjStackDown))
2001 .addImm(NumBytes).addImm(0));
2002
2003 // Process the args.
2004 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2005 CCValAssign &VA = ArgLocs[i];
2006 const Value *ArgVal = Args[VA.getValNo()];
2007 Register Arg = ArgRegs[VA.getValNo()];
2008 MVT ArgVT = ArgVTs[VA.getValNo()];
2009
2010 assert((!ArgVT.isVector() && ArgVT.getSizeInBits() <= 64) &&
2011 "We don't handle NEON/vector parameters yet.");
2012
2013 // Handle arg promotion, etc.
2014 switch (VA.getLocInfo()) {
2015 case CCValAssign::Full: break;
2016 case CCValAssign::SExt: {
2017 MVT DestVT = VA.getLocVT();
2018 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/false);
2019 assert(Arg && "Failed to emit a sext");
2020 ArgVT = DestVT;
2021 break;
2022 }
2023 case CCValAssign::AExt:
2024 // Intentional fall-through. Handle AExt and ZExt.
2025 case CCValAssign::ZExt: {
2026 MVT DestVT = VA.getLocVT();
2027 Arg = ARMEmitIntExt(ArgVT, Arg, DestVT, /*isZExt*/true);
2028 assert(Arg && "Failed to emit a zext");
2029 ArgVT = DestVT;
2030 break;
2031 }
2032 case CCValAssign::BCvt: {
2033 Register BC = fastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, Arg);
2034 assert(BC && "Failed to emit a bitcast!");
2035 Arg = BC;
2036 ArgVT = VA.getLocVT();
2037 break;
2038 }
2039 default: llvm_unreachable("Unknown arg promotion!");
2040 }
2041
2042 // Now copy/store arg to correct locations.
2043 if (VA.isRegLoc() && !VA.needsCustom()) {
2044 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2045 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
2046 RegArgs.push_back(VA.getLocReg());
2047 } else if (VA.needsCustom()) {
2048 // TODO: We need custom lowering for vector (v2f64) args.
2049 assert(VA.getLocVT() == MVT::f64 &&
2050 "Custom lowering for v2f64 args not available");
2051
2052 // FIXME: ArgLocs[++i] may extend beyond ArgLocs.size()
2053 CCValAssign &NextVA = ArgLocs[++i];
2054
2055 assert(VA.isRegLoc() && NextVA.isRegLoc() &&
2056 "We only handle register args!");
2057
2058 // VMOVRRD moves the low word to Rt and the high word to Rt2; on
2059 // big-endian targets the high word goes in the first register.
2060 Register Lo = VA.getLocReg();
2061 Register Hi = NextVA.getLocReg();
2062 if (DL.isBigEndian())
2063 std::swap(Lo, Hi);
2064 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2065 TII.get(ARM::VMOVRRD), Lo)
2066 .addReg(Hi, RegState::Define)
2067 .addReg(Arg));
2068 RegArgs.push_back(VA.getLocReg());
2069 RegArgs.push_back(NextVA.getLocReg());
2070 } else {
2071 assert(VA.isMemLoc());
2072 // Need to store on the stack.
2073
2074 // Don't emit stores for undef values.
2075 if (isa<UndefValue>(ArgVal))
2076 continue;
2077
2078 Address Addr;
2079 Addr.setKind(Address::RegBase);
2080 Addr.setReg(ARM::SP);
2081 Addr.setOffset(VA.getLocMemOffset());
2082
2083 bool EmitRet = ARMEmitStore(ArgVT, Arg, Addr); (void)EmitRet;
2084 assert(EmitRet && "Could not emit a store for argument!");
2085 }
2086 }
2087
2088 return true;
2089}
2090
2091bool ARMFastISel::FinishCall(MVT RetVT, SmallVectorImpl<Register> &UsedRegs,
2092 const Instruction *I, CallingConv::ID CC,
2093 unsigned &NumBytes, bool isVarArg) {
2094 // Issue CALLSEQ_END
2095 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2096 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2097 TII.get(AdjStackUp))
2098 .addImm(NumBytes).addImm(-1ULL));
2099
2100 // Now the return value.
2101 if (RetVT != MVT::isVoid) {
2103 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
2104 CCInfo.AnalyzeCallResult(RetVT, I->getType(),
2105 CCAssignFnForCall(CC, true, isVarArg));
2106
2107 // Copy all of the result registers out of their specified physreg.
2108 if (RVLocs.size() == 2 && RetVT == MVT::f64) {
2109 // For this move we copy into two registers and then move into the
2110 // double fp reg we want.
2111 MVT DestVT = RVLocs[0].getValVT();
2112 const TargetRegisterClass* DstRC = TLI.getRegClassFor(DestVT);
2113 Register ResultReg = createResultReg(DstRC);
2114 // VMOVDRR takes the low word from Rt and the high word from Rt2; on
2115 // big-endian targets the first register holds the high word.
2116 Register Lo = RVLocs[0].getLocReg();
2117 Register Hi = RVLocs[1].getLocReg();
2118 if (DL.isBigEndian())
2119 std::swap(Lo, Hi);
2120 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2121 TII.get(ARM::VMOVDRR), ResultReg)
2122 .addReg(Lo)
2123 .addReg(Hi));
2124
2125 UsedRegs.push_back(RVLocs[0].getLocReg());
2126 UsedRegs.push_back(RVLocs[1].getLocReg());
2127
2128 // Finally update the result.
2129 updateValueMap(I, ResultReg);
2130 } else {
2131 assert(RVLocs.size() == 1 &&"Can't handle non-double multi-reg retvals!");
2132 MVT CopyVT = RVLocs[0].getValVT();
2133
2134 // Special handling for extended integers.
2135 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
2136 CopyVT = MVT::i32;
2137
2138 const TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
2139
2140 Register ResultReg = createResultReg(DstRC);
2141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2142 TII.get(TargetOpcode::COPY),
2143 ResultReg).addReg(RVLocs[0].getLocReg());
2144 UsedRegs.push_back(RVLocs[0].getLocReg());
2145
2146 // Finally update the result.
2147 updateValueMap(I, ResultReg);
2148 }
2149 }
2150
2151 return true;
2152}
2153
2154bool ARMFastISel::SelectRet(const Instruction *I) {
2155 const ReturnInst *Ret = cast<ReturnInst>(I);
2156 const Function &F = *I->getParent()->getParent();
2157 const bool IsCmseNSEntry = F.hasFnAttribute("cmse_nonsecure_entry");
2158
2159 if (!FuncInfo.CanLowerReturn)
2160 return false;
2161
2162 if (TLI.supportSwiftError() &&
2163 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
2164 return false;
2165
2166 if (TLI.supportSplitCSR(FuncInfo.MF))
2167 return false;
2168
2169 // Build a list of return value registers.
2171
2172 CallingConv::ID CC = F.getCallingConv();
2173 if (Ret->getNumOperands() > 0) {
2175 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
2176
2177 // Analyze operands of the call, assigning locations to each operand.
2179 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
2180 CCInfo.AnalyzeReturn(Outs, CCAssignFnForCall(CC, true /* is Ret */,
2181 F.isVarArg()));
2182
2183 const Value *RV = Ret->getOperand(0);
2184 Register Reg = getRegForValue(RV);
2185 if (!Reg)
2186 return false;
2187
2188 // Only handle a single return value for now.
2189 if (ValLocs.size() != 1)
2190 return false;
2191
2192 CCValAssign &VA = ValLocs[0];
2193
2194 // Don't bother handling odd stuff for now.
2195 if (VA.getLocInfo() != CCValAssign::Full)
2196 return false;
2197 // Only handle register returns for now.
2198 if (!VA.isRegLoc())
2199 return false;
2200
2201 Register SrcReg = Reg + VA.getValNo();
2202 EVT RVEVT = TLI.getValueType(DL, RV->getType());
2203 if (!RVEVT.isSimple()) return false;
2204 MVT RVVT = RVEVT.getSimpleVT();
2205 MVT DestVT = VA.getValVT();
2206 // Special handling for extended integers.
2207 if (RVVT != DestVT) {
2208 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2209 return false;
2210
2211 assert(DestVT == MVT::i32 && "ARM should always ext to i32");
2212
2213 // Perform extension if flagged as either zext or sext. Otherwise, do
2214 // nothing.
2215 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
2216 SrcReg = ARMEmitIntExt(RVVT, SrcReg, DestVT, Outs[0].Flags.isZExt());
2217 if (!SrcReg)
2218 return false;
2219 }
2220 }
2221
2222 // Make the copy.
2223 Register DstReg = VA.getLocReg();
2224 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
2225 // Avoid a cross-class copy. This is very unlikely.
2226 if (!SrcRC->contains(DstReg))
2227 return false;
2228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2229 TII.get(TargetOpcode::COPY), DstReg).addReg(SrcReg);
2230
2231 // Add register to return instruction.
2232 RetRegs.push_back(VA.getLocReg());
2233 }
2234
2235 unsigned RetOpc;
2236 if (IsCmseNSEntry)
2237 if (isThumb2)
2238 RetOpc = ARM::tBXNS_RET;
2239 else
2240 llvm_unreachable("CMSE not valid for non-Thumb targets");
2241 else
2242 RetOpc = Subtarget->getReturnOpcode();
2243
2244 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2245 TII.get(RetOpc));
2246 AddOptionalDefs(MIB);
2247 for (Register R : RetRegs)
2248 MIB.addReg(R, RegState::Implicit);
2249 return true;
2250}
2251
2252unsigned ARMFastISel::ARMSelectCallOp(bool UseReg) {
2253 if (UseReg)
2254 return isThumb2 ? gettBLXrOpcode(*MF) : getBLXOpcode(*MF);
2255 else
2256 return isThumb2 ? ARM::tBL : ARM::BL;
2257}
2258
2259Register ARMFastISel::getLibcallReg(const Twine &Name) {
2260 // Manually compute the global's type to avoid building it when unnecessary.
2261 Type *GVTy = PointerType::get(*Context, /*AS=*/0);
2262 EVT LCREVT = TLI.getValueType(DL, GVTy);
2263 if (!LCREVT.isSimple())
2264 return Register();
2265
2266 GlobalValue *GV = M.getNamedGlobal(Name.str());
2267 if (!GV)
2268 GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
2269 GlobalValue::ExternalLinkage, nullptr, Name);
2270
2271 return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
2272}
2273
2274// A quick function that will emit a call for a named libcall in F with the
2275// vector of passed arguments for the Instruction in I. We can assume that we
2276// can emit a call for any libcall we can produce. This is an abridged version
2277// of the full call infrastructure since we won't need to worry about things
2278// like computed function pointers or strange arguments at call sites.
2279// TODO: Try to unify this and the normal call bits for ARM, then try to unify
2280// with X86.
2281bool ARMFastISel::ARMEmitLibcall(const Instruction *I, RTLIB::Libcall Call) {
2282 RTLIB::LibcallImpl LCImpl = LibcallLowering->getLibcallImpl(Call);
2283 if (LCImpl == RTLIB::Unsupported)
2284 return false;
2285
2286 // Handle *simple* calls for now.
2287 Type *RetTy = I->getType();
2288 MVT RetVT;
2289 if (RetTy->isVoidTy())
2290 RetVT = MVT::isVoid;
2291 else if (!isTypeLegal(RetTy, RetVT))
2292 return false;
2293
2294 CallingConv::ID CC = LibcallLowering->getLibcallImplCallingConv(LCImpl);
2295
2296 // Can't handle non-double multi-reg retvals.
2297 if (RetVT != MVT::isVoid && RetVT != MVT::i32) {
2299 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2300 CCInfo.AnalyzeCallResult(RetVT, RetTy, CCAssignFnForCall(CC, true, false));
2301 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2302 return false;
2303 }
2304
2305 // Set up the argument vectors.
2306 SmallVector<Value*, 8> Args;
2308 SmallVector<MVT, 8> ArgVTs;
2310 Args.reserve(I->getNumOperands());
2311 ArgRegs.reserve(I->getNumOperands());
2312 ArgVTs.reserve(I->getNumOperands());
2313 ArgFlags.reserve(I->getNumOperands());
2314 for (Value *Op : I->operands()) {
2315 Register Arg = getRegForValue(Op);
2316 if (!Arg)
2317 return false;
2318
2319 Type *ArgTy = Op->getType();
2320 MVT ArgVT;
2321 if (!isTypeLegal(ArgTy, ArgVT)) return false;
2322
2323 ISD::ArgFlagsTy Flags;
2324 Flags.setOrigAlign(DL.getABITypeAlign(ArgTy));
2325
2326 Args.push_back(Op);
2327 ArgRegs.push_back(Arg);
2328 ArgVTs.push_back(ArgVT);
2329 ArgFlags.push_back(Flags);
2330 }
2331
2332 // Handle the arguments now that we've gotten them.
2334 unsigned NumBytes;
2335 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2336 RegArgs, CC, NumBytes, false))
2337 return false;
2338
2339 StringRef FuncName = RTLIB::RuntimeLibcallsInfo::getLibcallImplName(LCImpl);
2340
2341 Register CalleeReg;
2342 if (Subtarget->genLongCalls()) {
2343 CalleeReg = getLibcallReg(FuncName);
2344 if (!CalleeReg)
2345 return false;
2346 }
2347
2348 // Issue the call.
2349 unsigned CallOpc = ARMSelectCallOp(Subtarget->genLongCalls());
2350 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2351 MIMD, TII.get(CallOpc));
2352 // BL / BLX don't take a predicate, but tBL / tBLX do.
2353 if (isThumb2)
2354 MIB.add(predOps(ARMCC::AL));
2355 if (Subtarget->genLongCalls()) {
2356 CalleeReg =
2357 constrainOperandRegClass(TII.get(CallOpc), CalleeReg, isThumb2 ? 2 : 0);
2358 MIB.addReg(CalleeReg);
2359 } else
2360 MIB.addExternalSymbol(FuncName.data());
2361
2362 // Add implicit physical register uses to the call.
2363 for (Register R : RegArgs)
2364 MIB.addReg(R, RegState::Implicit);
2365
2366 // Add a register mask with the call-preserved registers.
2367 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2368 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
2369
2370 // Finish off the call including any return values.
2371 SmallVector<Register, 4> UsedRegs;
2372 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, false)) return false;
2373
2374 // Set all unused physreg defs as dead.
2375 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2376
2377 return true;
2378}
2379
2380bool ARMFastISel::SelectCall(const Instruction *I,
2381 const char *IntrMemName = nullptr) {
2382 const CallInst *CI = cast<CallInst>(I);
2383 const Value *Callee = CI->getCalledOperand();
2384
2385 // Can't handle inline asm.
2386 if (isa<InlineAsm>(Callee)) return false;
2387
2388 // Allow SelectionDAG isel to handle tail calls.
2389 if (CI->isTailCall()) return false;
2390
2391 // Check the calling convention.
2392 CallingConv::ID CC = CI->getCallingConv();
2393
2394 // TODO: Avoid some calling conventions?
2395
2396 FunctionType *FTy = CI->getFunctionType();
2397 bool isVarArg = FTy->isVarArg();
2398
2399 // Handle *simple* calls for now.
2400 Type *RetTy = I->getType();
2401 MVT RetVT;
2402 if (RetTy->isVoidTy())
2403 RetVT = MVT::isVoid;
2404 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
2405 RetVT != MVT::i8 && RetVT != MVT::i1)
2406 return false;
2407
2408 // Can't handle non-double multi-reg retvals.
2409 if (RetVT != MVT::isVoid && RetVT != MVT::i1 && RetVT != MVT::i8 &&
2410 RetVT != MVT::i16 && RetVT != MVT::i32) {
2412 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, RVLocs, *Context);
2413 CCInfo.AnalyzeCallResult(RetVT, RetTy,
2414 CCAssignFnForCall(CC, true, isVarArg));
2415 if (RVLocs.size() >= 2 && RetVT != MVT::f64)
2416 return false;
2417 }
2418
2419 // Set up the argument vectors.
2420 SmallVector<Value*, 8> Args;
2422 SmallVector<MVT, 8> ArgVTs;
2424 unsigned arg_size = CI->arg_size();
2425 Args.reserve(arg_size);
2426 ArgRegs.reserve(arg_size);
2427 ArgVTs.reserve(arg_size);
2428 ArgFlags.reserve(arg_size);
2429 for (auto ArgI = CI->arg_begin(), ArgE = CI->arg_end(); ArgI != ArgE; ++ArgI) {
2430 // If we're lowering a memory intrinsic instead of a regular call, skip the
2431 // last argument, which shouldn't be passed to the underlying function.
2432 if (IntrMemName && ArgE - ArgI <= 1)
2433 break;
2434
2435 ISD::ArgFlagsTy Flags;
2436 unsigned ArgIdx = ArgI - CI->arg_begin();
2437 if (CI->paramHasAttr(ArgIdx, Attribute::SExt))
2438 Flags.setSExt();
2439 if (CI->paramHasAttr(ArgIdx, Attribute::ZExt))
2440 Flags.setZExt();
2441
2442 // FIXME: Only handle *easy* calls for now.
2443 if (CI->paramHasAttr(ArgIdx, Attribute::InReg) ||
2444 CI->paramHasAttr(ArgIdx, Attribute::StructRet) ||
2445 CI->paramHasAttr(ArgIdx, Attribute::SwiftSelf) ||
2446 CI->paramHasAttr(ArgIdx, Attribute::SwiftError) ||
2447 CI->paramHasAttr(ArgIdx, Attribute::Nest) ||
2448 CI->paramHasAttr(ArgIdx, Attribute::ByVal))
2449 return false;
2450
2451 Type *ArgTy = (*ArgI)->getType();
2452 MVT ArgVT;
2453 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8 &&
2454 ArgVT != MVT::i1)
2455 return false;
2456
2457 Register Arg = getRegForValue(*ArgI);
2458 if (!Arg.isValid())
2459 return false;
2460
2461 Flags.setOrigAlign(DL.getABITypeAlign(ArgTy));
2462
2463 Args.push_back(*ArgI);
2464 ArgRegs.push_back(Arg);
2465 ArgVTs.push_back(ArgVT);
2466 ArgFlags.push_back(Flags);
2467 }
2468
2469 // Handle the arguments now that we've gotten them.
2471 unsigned NumBytes;
2472 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
2473 RegArgs, CC, NumBytes, isVarArg))
2474 return false;
2475
2476 bool UseReg = false;
2477 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
2478 if (!GV || Subtarget->genLongCalls()) UseReg = true;
2479
2480 Register CalleeReg;
2481 if (UseReg) {
2482 if (IntrMemName)
2483 CalleeReg = getLibcallReg(IntrMemName);
2484 else
2485 CalleeReg = getRegForValue(Callee);
2486
2487 if (!CalleeReg)
2488 return false;
2489 }
2490
2491 // Issue the call.
2492 unsigned CallOpc = ARMSelectCallOp(UseReg);
2493 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
2494 MIMD, TII.get(CallOpc));
2495
2496 // ARM calls don't take a predicate, but tBL / tBLX do.
2497 if(isThumb2)
2498 MIB.add(predOps(ARMCC::AL));
2499 if (UseReg) {
2500 CalleeReg =
2501 constrainOperandRegClass(TII.get(CallOpc), CalleeReg, isThumb2 ? 2 : 0);
2502 MIB.addReg(CalleeReg);
2503 } else if (!IntrMemName)
2504 MIB.addGlobalAddress(GV, 0, 0);
2505 else
2506 MIB.addExternalSymbol(IntrMemName, 0);
2507
2508 // Add implicit physical register uses to the call.
2509 for (Register R : RegArgs)
2510 MIB.addReg(R, RegState::Implicit);
2511
2512 // Add a register mask with the call-preserved registers.
2513 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2514 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
2515
2516 // Finish off the call including any return values.
2517 SmallVector<Register, 4> UsedRegs;
2518 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes, isVarArg))
2519 return false;
2520
2521 // Set all unused physreg defs as dead.
2522 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2523
2524 diagnoseDontCall(*CI);
2525 return true;
2526}
2527
2528bool ARMFastISel::ARMIsMemCpySmall(uint64_t Len) {
2529 return Len <= 16;
2530}
2531
2532bool ARMFastISel::ARMTryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
2533 MaybeAlign Alignment) {
2534 // Make sure we don't bloat code by inlining very large memcpy's.
2535 if (!ARMIsMemCpySmall(Len))
2536 return false;
2537
2538 while (Len) {
2539 MVT VT;
2540 if (!Alignment || *Alignment >= 4) {
2541 if (Len >= 4)
2542 VT = MVT::i32;
2543 else if (Len >= 2)
2544 VT = MVT::i16;
2545 else {
2546 assert(Len == 1 && "Expected a length of 1!");
2547 VT = MVT::i8;
2548 }
2549 } else {
2550 assert(Alignment && "Alignment is set in this branch");
2551 // Bound based on alignment.
2552 if (Len >= 2 && *Alignment == 2)
2553 VT = MVT::i16;
2554 else {
2555 VT = MVT::i8;
2556 }
2557 }
2558
2559 bool RV;
2560 Register ResultReg;
2561 RV = ARMEmitLoad(VT, ResultReg, Src);
2562 assert(RV && "Should be able to handle this load.");
2563 RV = ARMEmitStore(VT, ResultReg, Dest);
2564 assert(RV && "Should be able to handle this store.");
2565 (void)RV;
2566
2567 unsigned Size = VT.getSizeInBits()/8;
2568 Len -= Size;
2569 Dest.setOffset(Dest.getOffset() + Size);
2570 Src.setOffset(Src.getOffset() + Size);
2571 }
2572
2573 return true;
2574}
2575
2576bool ARMFastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
2577 // FIXME: Handle more intrinsics.
2578 switch (I.getIntrinsicID()) {
2579 default: return false;
2580 case Intrinsic::frameaddress: {
2581 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
2582 MFI.setFrameAddressIsTaken(true);
2583
2584 unsigned LdrOpc = isThumb2 ? ARM::t2LDRi12 : ARM::LDRi12;
2585 const TargetRegisterClass *RC = isThumb2 ? &ARM::tGPRRegClass
2586 : &ARM::GPRRegClass;
2587
2588 const ARMBaseRegisterInfo *RegInfo = Subtarget->getRegisterInfo();
2589 Register FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2590 Register SrcReg = FramePtr;
2591
2592 // Recursively load frame address
2593 // ldr r0 [fp]
2594 // ldr r0 [r0]
2595 // ldr r0 [r0]
2596 // ...
2597 Register DestReg;
2598 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
2599 while (Depth--) {
2600 DestReg = createResultReg(RC);
2601 AddOptionalDefs(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2602 TII.get(LdrOpc), DestReg)
2603 .addReg(SrcReg).addImm(0));
2604 SrcReg = DestReg;
2605 }
2606 updateValueMap(&I, SrcReg);
2607 return true;
2608 }
2609 case Intrinsic::memcpy:
2610 case Intrinsic::memmove: {
2611 const MemTransferInst &MTI = cast<MemTransferInst>(I);
2612 // Don't handle volatile.
2613 if (MTI.isVolatile())
2614 return false;
2615
2616 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2617 // we would emit dead code because we don't currently handle memmoves.
2618 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
2619 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
2620 // Small memcpy's are common enough that we want to do them without a call
2621 // if possible.
2622 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
2623 if (ARMIsMemCpySmall(Len)) {
2624 Address Dest, Src;
2625 if (!ARMComputeAddress(MTI.getRawDest(), Dest) ||
2626 !ARMComputeAddress(MTI.getRawSource(), Src))
2627 return false;
2628 MaybeAlign Alignment;
2629 if (MTI.getDestAlign() || MTI.getSourceAlign())
2630 Alignment = std::min(MTI.getDestAlign().valueOrOne(),
2631 MTI.getSourceAlign().valueOrOne());
2632 if (ARMTryEmitSmallMemCpy(Dest, Src, Len, Alignment))
2633 return true;
2634 }
2635 }
2636
2637 if (!MTI.getLength()->getType()->isIntegerTy(32))
2638 return false;
2639
2640 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
2641 return false;
2642
2643 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
2644 return SelectCall(&I, IntrMemName);
2645 }
2646 case Intrinsic::memset: {
2647 const MemSetInst &MSI = cast<MemSetInst>(I);
2648 // Don't handle volatile.
2649 if (MSI.isVolatile())
2650 return false;
2651
2652 if (!MSI.getLength()->getType()->isIntegerTy(32))
2653 return false;
2654
2655 if (MSI.getDestAddressSpace() > 255)
2656 return false;
2657
2658 return SelectCall(&I, "memset");
2659 }
2660 case Intrinsic::trap: {
2661 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2662 TII.get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
2663 return true;
2664 }
2665 }
2666}
2667
2668bool ARMFastISel::SelectTrunc(const Instruction *I) {
2669 // The high bits for a type smaller than the register size are assumed to be
2670 // undefined.
2671 Value *Op = I->getOperand(0);
2672
2673 EVT SrcVT, DestVT;
2674 SrcVT = TLI.getValueType(DL, Op->getType(), true);
2675 DestVT = TLI.getValueType(DL, I->getType(), true);
2676
2677 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
2678 return false;
2679 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
2680 return false;
2681
2682 Register SrcReg = getRegForValue(Op);
2683 if (!SrcReg) return false;
2684
2685 // Because the high bits are undefined, a truncate doesn't generate
2686 // any code.
2687 updateValueMap(I, SrcReg);
2688 return true;
2689}
2690
2691Register ARMFastISel::ARMEmitIntExt(MVT SrcVT, Register SrcReg, MVT DestVT,
2692 bool isZExt) {
2693 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
2694 return Register();
2695 if (SrcVT != MVT::i16 && SrcVT != MVT::i8 && SrcVT != MVT::i1)
2696 return Register();
2697
2698 // Table of which combinations can be emitted as a single instruction,
2699 // and which will require two.
2700 static const uint8_t isSingleInstrTbl[3][2][2][2] = {
2701 // ARM Thumb
2702 // !hasV6Ops hasV6Ops !hasV6Ops hasV6Ops
2703 // ext: s z s z s z s z
2704 /* 1 */ { { { 0, 1 }, { 0, 1 } }, { { 0, 0 }, { 0, 1 } } },
2705 /* 8 */ { { { 0, 1 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } },
2706 /* 16 */ { { { 0, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 } } }
2707 };
2708
2709 // Target registers for:
2710 // - For ARM can never be PC.
2711 // - For 16-bit Thumb are restricted to lower 8 registers.
2712 // - For 32-bit Thumb are restricted to non-SP and non-PC.
2713 static const TargetRegisterClass *RCTbl[2][2] = {
2714 // Instructions: Two Single
2715 /* ARM */ { &ARM::GPRnopcRegClass, &ARM::GPRnopcRegClass },
2716 /* Thumb */ { &ARM::tGPRRegClass, &ARM::rGPRRegClass }
2717 };
2718
2719 // Table governing the instruction(s) to be emitted.
2720 static const struct InstructionTable {
2721 uint32_t Opc : 16;
2722 uint32_t hasS : 1; // Some instructions have an S bit, always set it to 0.
2723 uint32_t Shift : 7; // For shift operand addressing mode, used by MOVsi.
2724 uint32_t Imm : 8; // All instructions have either a shift or a mask.
2725 } IT[2][2][3][2] = {
2726 { // Two instructions (first is left shift, second is in this table).
2727 { // ARM Opc S Shift Imm
2728 /* 1 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 31 },
2729 /* 1 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 31 } },
2730 /* 8 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 24 },
2731 /* 8 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 24 } },
2732 /* 16 bit sext */ { { ARM::MOVsi , 1, ARM_AM::asr , 16 },
2733 /* 16 bit zext */ { ARM::MOVsi , 1, ARM_AM::lsr , 16 } }
2734 },
2735 { // Thumb Opc S Shift Imm
2736 /* 1 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 31 },
2737 /* 1 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 31 } },
2738 /* 8 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 24 },
2739 /* 8 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 24 } },
2740 /* 16 bit sext */ { { ARM::tASRri , 0, ARM_AM::no_shift, 16 },
2741 /* 16 bit zext */ { ARM::tLSRri , 0, ARM_AM::no_shift, 16 } }
2742 }
2743 },
2744 { // Single instruction.
2745 { // ARM Opc S Shift Imm
2746 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2747 /* 1 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 1 } },
2748 /* 8 bit sext */ { { ARM::SXTB , 0, ARM_AM::no_shift, 0 },
2749 /* 8 bit zext */ { ARM::ANDri , 1, ARM_AM::no_shift, 255 } },
2750 /* 16 bit sext */ { { ARM::SXTH , 0, ARM_AM::no_shift, 0 },
2751 /* 16 bit zext */ { ARM::UXTH , 0, ARM_AM::no_shift, 0 } }
2752 },
2753 { // Thumb Opc S Shift Imm
2754 /* 1 bit sext */ { { ARM::KILL , 0, ARM_AM::no_shift, 0 },
2755 /* 1 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 1 } },
2756 /* 8 bit sext */ { { ARM::t2SXTB , 0, ARM_AM::no_shift, 0 },
2757 /* 8 bit zext */ { ARM::t2ANDri, 1, ARM_AM::no_shift, 255 } },
2758 /* 16 bit sext */ { { ARM::t2SXTH , 0, ARM_AM::no_shift, 0 },
2759 /* 16 bit zext */ { ARM::t2UXTH , 0, ARM_AM::no_shift, 0 } }
2760 }
2761 }
2762 };
2763
2764 unsigned SrcBits = SrcVT.getSizeInBits();
2765 unsigned DestBits = DestVT.getSizeInBits();
2766 (void) DestBits;
2767 assert((SrcBits < DestBits) && "can only extend to larger types");
2768 assert((DestBits == 32 || DestBits == 16 || DestBits == 8) &&
2769 "other sizes unimplemented");
2770 assert((SrcBits == 16 || SrcBits == 8 || SrcBits == 1) &&
2771 "other sizes unimplemented");
2772
2773 bool hasV6Ops = Subtarget->hasV6Ops();
2774 unsigned Bitness = SrcBits / 8; // {1,8,16}=>{0,1,2}
2775 assert((Bitness < 3) && "sanity-check table bounds");
2776
2777 bool isSingleInstr = isSingleInstrTbl[Bitness][isThumb2][hasV6Ops][isZExt];
2778 const TargetRegisterClass *RC = RCTbl[isThumb2][isSingleInstr];
2779 const InstructionTable *ITP = &IT[isSingleInstr][isThumb2][Bitness][isZExt];
2780 unsigned Opc = ITP->Opc;
2781 assert(ARM::KILL != Opc && "Invalid table entry");
2782 unsigned hasS = ITP->hasS;
2783 ARM_AM::ShiftOpc Shift = (ARM_AM::ShiftOpc) ITP->Shift;
2784 assert(((Shift == ARM_AM::no_shift) == (Opc != ARM::MOVsi)) &&
2785 "only MOVsi has shift operand addressing mode");
2786 unsigned Imm = ITP->Imm;
2787
2788 // 16-bit Thumb instructions always set CPSR (unless they're in an IT block).
2789 bool setsCPSR = &ARM::tGPRRegClass == RC;
2790 unsigned LSLOpc = isThumb2 ? ARM::tLSLri : ARM::MOVsi;
2791 Register ResultReg;
2792 // MOVsi encodes shift and immediate in shift operand addressing mode.
2793 // The following condition has the same value when emitting two
2794 // instruction sequences: both are shifts.
2795 bool ImmIsSO = (Shift != ARM_AM::no_shift);
2796
2797 // Either one or two instructions are emitted.
2798 // They're always of the form:
2799 // dst = in OP imm
2800 // CPSR is set only by 16-bit Thumb instructions.
2801 // Predicate, if any, is AL.
2802 // S bit, if available, is always 0.
2803 // When two are emitted the first's result will feed as the second's input,
2804 // that value is then dead.
2805 unsigned NumInstrsEmitted = isSingleInstr ? 1 : 2;
2806 for (unsigned Instr = 0; Instr != NumInstrsEmitted; ++Instr) {
2807 ResultReg = createResultReg(RC);
2808 bool isLsl = (0 == Instr) && !isSingleInstr;
2809 unsigned Opcode = isLsl ? LSLOpc : Opc;
2810 ARM_AM::ShiftOpc ShiftAM = isLsl ? ARM_AM::lsl : Shift;
2811 unsigned ImmEnc = ImmIsSO ? ARM_AM::getSORegOpc(ShiftAM, Imm) : Imm;
2812 MachineInstrBuilder MIB = BuildMI(
2813 *FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opcode), ResultReg);
2814 if (setsCPSR)
2815 MIB.addReg(ARM::CPSR, RegState::Define | RegState::Dead);
2816 SrcReg = constrainOperandRegClass(TII.get(Opcode), SrcReg, 1 + setsCPSR);
2817 MIB.addReg(SrcReg)
2818 .addImm(ImmEnc)
2820 if (hasS)
2821 MIB.add(condCodeOp());
2822 // Second instruction consumes the first's result.
2823 SrcReg = ResultReg;
2824 }
2825
2826 return ResultReg;
2827}
2828
2829bool ARMFastISel::SelectIntExt(const Instruction *I) {
2830 // On ARM, in general, integer casts don't involve legal types; this code
2831 // handles promotable integers.
2832 Type *DestTy = I->getType();
2833 Value *Src = I->getOperand(0);
2834 Type *SrcTy = Src->getType();
2835
2836 bool isZExt = isa<ZExtInst>(I);
2837 Register SrcReg = getRegForValue(Src);
2838 if (!SrcReg) return false;
2839
2840 EVT SrcEVT, DestEVT;
2841 SrcEVT = TLI.getValueType(DL, SrcTy, true);
2842 DestEVT = TLI.getValueType(DL, DestTy, true);
2843 if (!SrcEVT.isSimple()) return false;
2844 if (!DestEVT.isSimple()) return false;
2845
2846 MVT SrcVT = SrcEVT.getSimpleVT();
2847 MVT DestVT = DestEVT.getSimpleVT();
2848 Register ResultReg = ARMEmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2849 if (!ResultReg)
2850 return false;
2851 updateValueMap(I, ResultReg);
2852 return true;
2853}
2854
2855bool ARMFastISel::SelectShift(const Instruction *I,
2856 ARM_AM::ShiftOpc ShiftTy) {
2857 // We handle thumb2 mode by target independent selector
2858 // or SelectionDAG ISel.
2859 if (isThumb2)
2860 return false;
2861
2862 // Only handle i32 now.
2863 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
2864 if (DestVT != MVT::i32)
2865 return false;
2866
2867 unsigned Opc = ARM::MOVsr;
2868 unsigned ShiftImm;
2869 Value *Src2Value = I->getOperand(1);
2870 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Src2Value)) {
2871 ShiftImm = CI->getZExtValue();
2872
2873 // Fall back to selection DAG isel if the shift amount
2874 // is zero or greater than the width of the value type.
2875 if (ShiftImm == 0 || ShiftImm >=32)
2876 return false;
2877
2878 Opc = ARM::MOVsi;
2879 }
2880
2881 Value *Src1Value = I->getOperand(0);
2882 Register Reg1 = getRegForValue(Src1Value);
2883 if (!Reg1)
2884 return false;
2885
2886 Register Reg2;
2887 if (Opc == ARM::MOVsr) {
2888 Reg2 = getRegForValue(Src2Value);
2889 if (!Reg2)
2890 return false;
2891 }
2892
2893 Register ResultReg = createResultReg(&ARM::GPRnopcRegClass);
2894 if (!ResultReg)
2895 return false;
2896
2897 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
2898 TII.get(Opc), ResultReg)
2899 .addReg(Reg1);
2900
2901 if (Opc == ARM::MOVsi)
2902 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, ShiftImm));
2903 else if (Opc == ARM::MOVsr) {
2904 MIB.addReg(Reg2);
2905 MIB.addImm(ARM_AM::getSORegOpc(ShiftTy, 0));
2906 }
2907
2908 AddOptionalDefs(MIB);
2909 updateValueMap(I, ResultReg);
2910 return true;
2911}
2912
2913// TODO: SoftFP support.
2914bool ARMFastISel::fastSelectInstruction(const Instruction *I) {
2915 switch (I->getOpcode()) {
2916 case Instruction::Load:
2917 return SelectLoad(I);
2918 case Instruction::Store:
2919 return SelectStore(I);
2920 case Instruction::CondBr:
2921 return SelectBranch(I);
2922 case Instruction::IndirectBr:
2923 return SelectIndirectBr(I);
2924 case Instruction::ICmp:
2925 case Instruction::FCmp:
2926 return SelectCmp(I);
2927 case Instruction::FPExt:
2928 return SelectFPExt(I);
2929 case Instruction::FPTrunc:
2930 return SelectFPTrunc(I);
2931 case Instruction::SIToFP:
2932 return SelectIToFP(I, /*isSigned*/ true);
2933 case Instruction::UIToFP:
2934 return SelectIToFP(I, /*isSigned*/ false);
2935 case Instruction::FPToSI:
2936 return SelectFPToI(I, /*isSigned*/ true);
2937 case Instruction::FPToUI:
2938 return SelectFPToI(I, /*isSigned*/ false);
2939 case Instruction::Add:
2940 return SelectBinaryIntOp(I, ISD::ADD);
2941 case Instruction::Or:
2942 return SelectBinaryIntOp(I, ISD::OR);
2943 case Instruction::Sub:
2944 return SelectBinaryIntOp(I, ISD::SUB);
2945 case Instruction::FAdd:
2946 return SelectBinaryFPOp(I, ISD::FADD);
2947 case Instruction::FSub:
2948 return SelectBinaryFPOp(I, ISD::FSUB);
2949 case Instruction::FMul:
2950 return SelectBinaryFPOp(I, ISD::FMUL);
2951 case Instruction::SDiv:
2952 return SelectDiv(I, /*isSigned*/ true);
2953 case Instruction::UDiv:
2954 return SelectDiv(I, /*isSigned*/ false);
2955 case Instruction::SRem:
2956 return SelectRem(I, /*isSigned*/ true);
2957 case Instruction::URem:
2958 return SelectRem(I, /*isSigned*/ false);
2959 case Instruction::Call:
2960 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
2961 return SelectIntrinsicCall(*II);
2962 return SelectCall(I);
2963 case Instruction::Select:
2964 return SelectSelect(I);
2965 case Instruction::Ret:
2966 return SelectRet(I);
2967 case Instruction::Trunc:
2968 return SelectTrunc(I);
2969 case Instruction::ZExt:
2970 case Instruction::SExt:
2971 return SelectIntExt(I);
2972 case Instruction::Shl:
2973 return SelectShift(I, ARM_AM::lsl);
2974 case Instruction::LShr:
2975 return SelectShift(I, ARM_AM::lsr);
2976 case Instruction::AShr:
2977 return SelectShift(I, ARM_AM::asr);
2978 default: break;
2979 }
2980 return false;
2981}
2982
2983// This table describes sign- and zero-extend instructions which can be
2984// folded into a preceding load. All of these extends have an immediate
2985// (sometimes a mask and sometimes a shift) that's applied after
2986// extension.
2987static const struct FoldableLoadExtendsStruct {
2988 uint16_t Opc[2]; // ARM, Thumb.
2992} FoldableLoadExtends[] = {
2993 { { ARM::SXTH, ARM::t2SXTH }, 0, 0, MVT::i16 },
2994 { { ARM::UXTH, ARM::t2UXTH }, 0, 1, MVT::i16 },
2995 { { ARM::ANDri, ARM::t2ANDri }, 255, 1, MVT::i8 },
2996 { { ARM::SXTB, ARM::t2SXTB }, 0, 0, MVT::i8 },
2997 { { ARM::UXTB, ARM::t2UXTB }, 0, 1, MVT::i8 }
2999
3000/// The specified machine instr operand is a vreg, and that
3001/// vreg is being provided by the specified load instruction. If possible,
3002/// try to fold the load as an operand to the instruction, returning true if
3003/// successful.
3004bool ARMFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3005 const LoadInst *LI) {
3006 // Verify we have a legal type before going any further.
3007 MVT VT;
3008 if (!isLoadTypeLegal(LI->getType(), VT))
3009 return false;
3010
3011 // Combine load followed by zero- or sign-extend.
3012 // ldrb r1, [r0] ldrb r1, [r0]
3013 // uxtb r2, r1 =>
3014 // mov r3, r2 mov r3, r1
3015 if (MI->getNumOperands() < 3 || !MI->getOperand(2).isImm())
3016 return false;
3017 const uint64_t Imm = MI->getOperand(2).getImm();
3018
3019 bool Found = false;
3020 bool isZExt;
3021 for (const FoldableLoadExtendsStruct &FLE : FoldableLoadExtends) {
3022 if (FLE.Opc[isThumb2] == MI->getOpcode() &&
3023 (uint64_t)FLE.ExpectedImm == Imm &&
3024 MVT((MVT::SimpleValueType)FLE.ExpectedVT) == VT) {
3025 Found = true;
3026 isZExt = FLE.isZExt;
3027 }
3028 }
3029 if (!Found) return false;
3030
3031 // See if we can handle this address.
3032 Address Addr;
3033 if (!ARMComputeAddress(LI->getOperand(0), Addr)) return false;
3034
3035 Register ResultReg = MI->getOperand(0).getReg();
3036 if (!ARMEmitLoad(VT, ResultReg, Addr, LI->getAlign(), isZExt, false))
3037 return false;
3039 removeDeadCode(I, std::next(I));
3040 return true;
3041}
3042
3043Register ARMFastISel::ARMLowerPICELF(const GlobalValue *GV, MVT VT) {
3044 bool UseGOT_PREL = !GV->isDSOLocal();
3045 LLVMContext *Context = &MF->getFunction().getContext();
3046 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
3047 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
3048 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
3049 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
3050 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
3051 /*AddCurrentAddress=*/UseGOT_PREL);
3052
3053 Align ConstAlign =
3054 MF->getDataLayout().getPrefTypeAlign(PointerType::get(*Context, 0));
3055 unsigned Idx = MF->getConstantPool()->getConstantPoolIndex(CPV, ConstAlign);
3056 MachineMemOperand *CPMMO =
3057 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
3059
3060 Register TempReg = MF->getRegInfo().createVirtualRegister(&ARM::rGPRRegClass);
3061 unsigned Opc = isThumb2 ? ARM::t2LDRpci : ARM::LDRcp;
3062 MachineInstrBuilder MIB =
3063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), TempReg)
3065 .addMemOperand(CPMMO);
3066 if (Opc == ARM::LDRcp)
3067 MIB.addImm(0);
3068 MIB.add(predOps(ARMCC::AL));
3069
3070 // Fix the address by adding pc.
3071 Register DestReg = createResultReg(TLI.getRegClassFor(VT));
3072 Opc = Subtarget->isThumb() ? ARM::tPICADD : UseGOT_PREL ? ARM::PICLDR
3073 : ARM::PICADD;
3074 DestReg = constrainOperandRegClass(TII.get(Opc), DestReg, 0);
3075 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
3076 .addReg(TempReg)
3077 .addImm(ARMPCLabelIndex);
3078
3079 if (!Subtarget->isThumb())
3080 MIB.add(predOps(ARMCC::AL));
3081
3082 if (UseGOT_PREL && Subtarget->isThumb()) {
3083 Register NewDestReg = createResultReg(TLI.getRegClassFor(VT));
3084 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
3085 TII.get(ARM::t2LDRi12), NewDestReg)
3086 .addReg(DestReg)
3087 .addImm(0);
3088 DestReg = NewDestReg;
3089 AddOptionalDefs(MIB);
3090 }
3091 return DestReg;
3092}
3093
3094bool ARMFastISel::fastLowerArguments() {
3095 if (!FuncInfo.CanLowerReturn)
3096 return false;
3097
3098 const Function *F = FuncInfo.Fn;
3099 if (F->isVarArg())
3100 return false;
3101
3102 CallingConv::ID CC = F->getCallingConv();
3103 switch (CC) {
3104 default:
3105 return false;
3106 case CallingConv::Fast:
3107 case CallingConv::C:
3108 case CallingConv::ARM_AAPCS_VFP:
3109 case CallingConv::ARM_AAPCS:
3110 case CallingConv::ARM_APCS:
3111 case CallingConv::Swift:
3112 case CallingConv::SwiftTail:
3113 break;
3114 }
3115
3116 // Only handle simple cases. i.e. Up to 4 i8/i16/i32 scalar arguments
3117 // which are passed in r0 - r3.
3118 for (const Argument &Arg : F->args()) {
3119 if (Arg.getArgNo() >= 4)
3120 return false;
3121
3122 if (Arg.hasAttribute(Attribute::InReg) ||
3123 Arg.hasAttribute(Attribute::StructRet) ||
3124 Arg.hasAttribute(Attribute::SwiftSelf) ||
3125 Arg.hasAttribute(Attribute::SwiftError) ||
3126 Arg.hasAttribute(Attribute::ByVal))
3127 return false;
3128
3129 Type *ArgTy = Arg.getType();
3130 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
3131 return false;
3132
3133 EVT ArgVT = TLI.getValueType(DL, ArgTy);
3134 if (!ArgVT.isSimple()) return false;
3135 switch (ArgVT.getSimpleVT().SimpleTy) {
3136 case MVT::i8:
3137 case MVT::i16:
3138 case MVT::i32:
3139 break;
3140 default:
3141 return false;
3142 }
3143 }
3144
3145 static const MCPhysReg GPRArgRegs[] = {
3146 ARM::R0, ARM::R1, ARM::R2, ARM::R3
3147 };
3148
3149 const TargetRegisterClass *RC = &ARM::rGPRRegClass;
3150 for (const Argument &Arg : F->args()) {
3151 unsigned ArgNo = Arg.getArgNo();
3152 MCRegister SrcReg = GPRArgRegs[ArgNo];
3153 Register DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3154 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3155 // Without this, EmitLiveInCopies may eliminate the livein if its only
3156 // use is a bitcast (which isn't turned into an instruction).
3157 Register ResultReg = createResultReg(RC);
3158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(TargetOpcode::COPY),
3159 ResultReg)
3160 .addReg(DstReg);
3161 updateValueMap(&Arg, ResultReg);
3162 }
3163
3164 return true;
3165}
3166
3167namespace llvm {
3168
3170 const TargetLibraryInfo *libInfo,
3171 const LibcallLoweringInfo *libcallLowering) {
3172 if (funcInfo.MF->getSubtarget<ARMSubtarget>().useFastISel())
3173 return new ARMFastISel(funcInfo, libInfo, libcallLowering);
3174
3175 return nullptr;
3176}
3177
3178} // end namespace llvm
static const MCPhysReg GPRArgRegs[]
const AArch64Subtarget & Subtarget
const TargetLowering & TLI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static ARMCC::CondCodes getComparePred(CmpInst::Predicate Pred)
static const struct FoldableLoadExtendsStruct FoldableLoadExtends[]
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool isSigned(unsigned Opcode)
This file defines the FastISel class.
static Register UseReg(const MachineOperand &MO)
const HexagonInstrInfo * TII
static MaybeAlign getAlign(Value *Ptr)
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
static bool isAtomic(MachineBasicBlock::iterator I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
uint64_t IntrinsicInst * II
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
MachineInstr *& Lo
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static const unsigned FramePtr
const AArch64RegisterInfo * getRegisterInfo() const override
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
Register getFrameRegister(const MachineFunction &MF) const override
static ARMConstantPoolConstant * Create(const Constant *C, unsigned ID)
bool useFastISel() const
True if fast-isel is used.
PointerType * getType() const
Overload to return most specific pointer type.
Register getLocReg() const
LocInfo getLocInfo() const
bool needsCustom() const
int64_t getLocMemOffset() const
unsigned getValNo() const
CallingConv::ID getCallingConv() const
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Value * getCalledOperand() const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
FunctionType * getFunctionType() const
unsigned arg_size() const
bool isTailCall() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
bool isUnsigned() const
Definition InstrTypes.h:999
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
const APFloat & getValueAPF() const
Definition Constants.h:463
bool isNegative() const
Definition Constants.h:214
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition Constants.h:174
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:67
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:352
bool isDSOLocal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
PointerType * getType() const
Global values are always pointers.
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
Tracks which library functions to use for a particular subtarget or function.
Align getAlign() const
Return the alignment of the access that is being performed.
ArrayRef< MCOperandInfo > operands() const
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
SimpleValueType SimpleTy
bool isVector() const
Return true if this is a vector value type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
MachineInstrBundleIterator< MachineInstr > iterator
void setFrameAddressIsTaken(bool T)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
Flags
Flags values. These may be or'd together.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
Value * getLength() const
Value * getRawDest() const
MaybeAlign getDestAlign() const
unsigned getDestAddressSpace() const
bool isVolatile() const
Value * getRawSource() const
Return the arguments to the instruction.
unsigned getSourceAddressSpace() const
MaybeAlign getSourceAlign() const
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
void reserve(size_type N)
void push_back(const T &Elt)
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:777
Provides information about what library functions are available for the current target.
virtual bool hasStandaloneRem(EVT VT) const
Return true if the target can handle a standalone remainder operation.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
bool isFPImmLegal(const APFloat &Imm, EVT VT, bool ForCodeSize=false) const
Returns true if the specified FP immediate can be materialized without a load from the constant pool.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
bool isPositionIndependent() const
virtual bool supportSplitCSR(MachineFunction *MF) const
Return true if the target supports that a subset of CSRs for the given machine function is handled ex...
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:274
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:271
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
const Use * const_op_iterator
Definition User.h:255
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ GOT_PREL
Thread Local Storage (General Dynamic Mode)
@ MO_NONLAZY
MO_NONLAZY - This is an independent flag, on a symbol operand "FOO" it represents a symbol which,...
int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
int getFP32Imm(const APInt &Imm)
getFP32Imm - Return an 8-bit floating-point version of the 32-bit floating-point value.
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
int getFP64Imm(const APInt &Imm)
getFP64Imm - Return an 8-bit floating-point version of the 64-bit floating-point value.
unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm)
FastISel * createFastISel(FunctionLoweringInfo &funcInfo, const TargetLibraryInfo *libInfo, const LibcallLoweringInfo *libcallLowering)
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
@ User
could "use" a pointer
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
bool RetFastCC_ARM_APCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
@ Offset
Definition DWP.cpp:577
LLVM_ABI Register constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const TargetRegisterClass &RegClass, MachineOperand &RegMO)
Constrain the Register operand OpIdx, so that it is now constrained to the TargetRegisterClass passed...
Definition Utils.cpp:64
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
bool CC_ARM_AAPCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
bool RetCC_ARM_AAPCS_VFP(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
bool RetCC_ARM_APCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
bool RetCC_ARM_AAPCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
bool CC_ARM_APCS_GHC(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
static std::array< MachineOperand, 2 > predOps(ARMCC::CondCodes Pred, unsigned PredReg=0)
Get the operands corresponding to the given Pred value.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool FastCC_ARM_APCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
bool CC_ARM_Win32_CFGuard_Check(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
generic_gep_type_iterator<> gep_type_iterator
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
static MachineOperand t1CondCodeOp(bool isDead=false)
Get the operand corresponding to the conditional code result for Thumb1.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
gep_type_iterator gep_type_begin(const User *GEP)
static MachineOperand condCodeOp(unsigned CCReg=0)
Get the operand corresponding to the conditional code result.
unsigned gettBLXrOpcode(const MachineFunction &MF)
bool CC_ARM_APCS(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
unsigned getBLXOpcode(const MachineFunction &MF)
bool CC_ARM_AAPCS_VFP(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:160
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:135
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.