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