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