1 //===-- AArch6464FastISel.cpp - AArch64 FastISel implementation -----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the AArch64-specific support for the FastISel class. Some 11 // of the target-specific code is generated by tablegen in the file 12 // AArch64GenFastISel.inc, which is #included here. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "AArch64.h" 17 #include "AArch64Subtarget.h" 18 #include "AArch64TargetMachine.h" 19 #include "MCTargetDesc/AArch64AddressingModes.h" 20 #include "llvm/Analysis/BranchProbabilityInfo.h" 21 #include "llvm/CodeGen/CallingConvLower.h" 22 #include "llvm/CodeGen/FastISel.h" 23 #include "llvm/CodeGen/FunctionLoweringInfo.h" 24 #include "llvm/CodeGen/MachineConstantPool.h" 25 #include "llvm/CodeGen/MachineFrameInfo.h" 26 #include "llvm/CodeGen/MachineInstrBuilder.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/IR/CallingConv.h" 29 #include "llvm/IR/DataLayout.h" 30 #include "llvm/IR/DerivedTypes.h" 31 #include "llvm/IR/Function.h" 32 #include "llvm/IR/GetElementPtrTypeIterator.h" 33 #include "llvm/IR/GlobalAlias.h" 34 #include "llvm/IR/GlobalVariable.h" 35 #include "llvm/IR/Instructions.h" 36 #include "llvm/IR/IntrinsicInst.h" 37 #include "llvm/IR/Operator.h" 38 #include "llvm/Support/CommandLine.h" 39 using namespace llvm; 40 41 namespace { 42 43 class AArch64FastISel : public FastISel { 44 45 class Address { 46 public: 47 typedef enum { 48 RegBase, 49 FrameIndexBase 50 } BaseKind; 51 52 private: 53 BaseKind Kind; 54 union { 55 unsigned Reg; 56 int FI; 57 } Base; 58 int64_t Offset; 59 const GlobalValue *GV; 60 61 public: 62 Address() : Kind(RegBase), Offset(0), GV(nullptr) { Base.Reg = 0; } 63 void setKind(BaseKind K) { Kind = K; } 64 BaseKind getKind() const { return Kind; } 65 bool isRegBase() const { return Kind == RegBase; } 66 bool isFIBase() const { return Kind == FrameIndexBase; } 67 void setReg(unsigned Reg) { 68 assert(isRegBase() && "Invalid base register access!"); 69 Base.Reg = Reg; 70 } 71 unsigned getReg() const { 72 assert(isRegBase() && "Invalid base register access!"); 73 return Base.Reg; 74 } 75 void setFI(unsigned FI) { 76 assert(isFIBase() && "Invalid base frame index access!"); 77 Base.FI = FI; 78 } 79 unsigned getFI() const { 80 assert(isFIBase() && "Invalid base frame index access!"); 81 return Base.FI; 82 } 83 void setOffset(int64_t O) { Offset = O; } 84 int64_t getOffset() { return Offset; } 85 86 void setGlobalValue(const GlobalValue *G) { GV = G; } 87 const GlobalValue *getGlobalValue() { return GV; } 88 89 bool isValid() { return isFIBase() || (isRegBase() && getReg() != 0); } 90 }; 91 92 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can 93 /// make the right decision when generating code for different targets. 94 const AArch64Subtarget *Subtarget; 95 LLVMContext *Context; 96 97 bool FastLowerArguments() override; 98 bool FastLowerCall(CallLoweringInfo &CLI) override; 99 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override; 100 101 private: 102 // Selection routines. 103 bool SelectLoad(const Instruction *I); 104 bool SelectStore(const Instruction *I); 105 bool SelectBranch(const Instruction *I); 106 bool SelectIndirectBr(const Instruction *I); 107 bool SelectCmp(const Instruction *I); 108 bool SelectSelect(const Instruction *I); 109 bool SelectFPExt(const Instruction *I); 110 bool SelectFPTrunc(const Instruction *I); 111 bool SelectFPToInt(const Instruction *I, bool Signed); 112 bool SelectIntToFP(const Instruction *I, bool Signed); 113 bool SelectRem(const Instruction *I, unsigned ISDOpcode); 114 bool SelectRet(const Instruction *I); 115 bool SelectTrunc(const Instruction *I); 116 bool SelectIntExt(const Instruction *I); 117 bool SelectMul(const Instruction *I); 118 bool SelectShift(const Instruction *I, bool IsLeftShift, bool IsArithmetic); 119 bool SelectBitCast(const Instruction *I); 120 121 // Utility helper routines. 122 bool isTypeLegal(Type *Ty, MVT &VT); 123 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT); 124 bool ComputeAddress(const Value *Obj, Address &Addr); 125 bool ComputeCallAddress(const Value *V, Address &Addr); 126 bool SimplifyAddress(Address &Addr, MVT VT, int64_t ScaleFactor, 127 bool UseUnscaled); 128 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB, 129 unsigned Flags, MachineMemOperand *MMO, 130 bool UseUnscaled); 131 bool IsMemCpySmall(uint64_t Len, unsigned Alignment); 132 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len, 133 unsigned Alignment); 134 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I, 135 const Value *Cond); 136 137 // Emit functions. 138 bool EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt); 139 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr, 140 MachineMemOperand *MMO = nullptr, bool UseUnscaled = false); 141 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr, 142 MachineMemOperand *MMO = nullptr, bool UseUnscaled = false); 143 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt); 144 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt); 145 unsigned Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 146 unsigned Op1, bool Op1IsKill); 147 unsigned Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 148 unsigned Op1, bool Op1IsKill); 149 unsigned Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 150 unsigned Op1, bool Op1IsKill); 151 unsigned Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm); 152 unsigned Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm); 153 unsigned Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm); 154 155 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT); 156 unsigned AArch64MaterializeGV(const GlobalValue *GV); 157 158 // Call handling routines. 159 private: 160 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const; 161 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs, 162 unsigned &NumBytes); 163 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes); 164 165 public: 166 // Backend specific FastISel code. 167 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override; 168 unsigned TargetMaterializeConstant(const Constant *C) override; 169 170 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo, 171 const TargetLibraryInfo *libInfo) 172 : FastISel(funcInfo, libInfo) { 173 Subtarget = &TM.getSubtarget<AArch64Subtarget>(); 174 Context = &funcInfo.Fn->getContext(); 175 } 176 177 bool TargetSelectInstruction(const Instruction *I) override; 178 179 #include "AArch64GenFastISel.inc" 180 }; 181 182 } // end anonymous namespace 183 184 #include "AArch64GenCallingConv.inc" 185 186 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const { 187 if (CC == CallingConv::WebKit_JS) 188 return CC_AArch64_WebKit_JS; 189 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS; 190 } 191 192 unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) { 193 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 && 194 "Alloca should always return a pointer."); 195 196 // Don't handle dynamic allocas. 197 if (!FuncInfo.StaticAllocaMap.count(AI)) 198 return 0; 199 200 DenseMap<const AllocaInst *, int>::iterator SI = 201 FuncInfo.StaticAllocaMap.find(AI); 202 203 if (SI != FuncInfo.StaticAllocaMap.end()) { 204 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass); 205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri), 206 ResultReg) 207 .addFrameIndex(SI->second) 208 .addImm(0) 209 .addImm(0); 210 return ResultReg; 211 } 212 213 return 0; 214 } 215 216 unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) { 217 if (VT != MVT::f32 && VT != MVT::f64) 218 return 0; 219 220 const APFloat Val = CFP->getValueAPF(); 221 bool is64bit = (VT == MVT::f64); 222 223 // This checks to see if we can use FMOV instructions to materialize 224 // a constant, otherwise we have to materialize via the constant pool. 225 if (TLI.isFPImmLegal(Val, VT)) { 226 int Imm; 227 unsigned Opc; 228 if (is64bit) { 229 Imm = AArch64_AM::getFP64Imm(Val); 230 Opc = AArch64::FMOVDi; 231 } else { 232 Imm = AArch64_AM::getFP32Imm(Val); 233 Opc = AArch64::FMOVSi; 234 } 235 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); 236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 237 .addImm(Imm); 238 return ResultReg; 239 } 240 241 // Materialize via constant pool. MachineConstantPool wants an explicit 242 // alignment. 243 unsigned Align = DL.getPrefTypeAlignment(CFP->getType()); 244 if (Align == 0) 245 Align = DL.getTypeAllocSize(CFP->getType()); 246 247 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); 248 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass); 249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP), 250 ADRPReg).addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGE); 251 252 unsigned Opc = is64bit ? AArch64::LDRDui : AArch64::LDRSui; 253 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); 254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 255 .addReg(ADRPReg) 256 .addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC); 257 return ResultReg; 258 } 259 260 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) { 261 // We can't handle thread-local variables quickly yet. 262 if (GV->isThreadLocal()) 263 return 0; 264 265 // MachO still uses GOT for large code-model accesses, but ELF requires 266 // movz/movk sequences, which FastISel doesn't handle yet. 267 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO()) 268 return 0; 269 270 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM); 271 272 EVT DestEVT = TLI.getValueType(GV->getType(), true); 273 if (!DestEVT.isSimple()) 274 return 0; 275 276 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass); 277 unsigned ResultReg; 278 279 if (OpFlags & AArch64II::MO_GOT) { 280 // ADRP + LDRX 281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP), 282 ADRPReg) 283 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE); 284 285 ResultReg = createResultReg(&AArch64::GPR64RegClass); 286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui), 287 ResultReg) 288 .addReg(ADRPReg) 289 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | 290 AArch64II::MO_NC); 291 } else { 292 // ADRP + ADDX 293 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP), 294 ADRPReg).addGlobalAddress(GV, 0, AArch64II::MO_PAGE); 295 296 ResultReg = createResultReg(&AArch64::GPR64spRegClass); 297 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri), 298 ResultReg) 299 .addReg(ADRPReg) 300 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC) 301 .addImm(0); 302 } 303 return ResultReg; 304 } 305 306 unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) { 307 EVT CEVT = TLI.getValueType(C->getType(), true); 308 309 // Only handle simple types. 310 if (!CEVT.isSimple()) 311 return 0; 312 MVT VT = CEVT.getSimpleVT(); 313 314 // FIXME: Handle ConstantInt. 315 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 316 return AArch64MaterializeFP(CFP, VT); 317 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 318 return AArch64MaterializeGV(GV); 319 320 return 0; 321 } 322 323 // Computes the address to get to an object. 324 bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) { 325 const User *U = nullptr; 326 unsigned Opcode = Instruction::UserOp1; 327 if (const Instruction *I = dyn_cast<Instruction>(Obj)) { 328 // Don't walk into other basic blocks unless the object is an alloca from 329 // another block, otherwise it may not have a virtual register assigned. 330 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || 331 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { 332 Opcode = I->getOpcode(); 333 U = I; 334 } 335 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { 336 Opcode = C->getOpcode(); 337 U = C; 338 } 339 340 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType())) 341 if (Ty->getAddressSpace() > 255) 342 // Fast instruction selection doesn't support the special 343 // address spaces. 344 return false; 345 346 switch (Opcode) { 347 default: 348 break; 349 case Instruction::BitCast: { 350 // Look through bitcasts. 351 return ComputeAddress(U->getOperand(0), Addr); 352 } 353 case Instruction::IntToPtr: { 354 // Look past no-op inttoptrs. 355 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 356 return ComputeAddress(U->getOperand(0), Addr); 357 break; 358 } 359 case Instruction::PtrToInt: { 360 // Look past no-op ptrtoints. 361 if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) 362 return ComputeAddress(U->getOperand(0), Addr); 363 break; 364 } 365 case Instruction::GetElementPtr: { 366 Address SavedAddr = Addr; 367 uint64_t TmpOffset = Addr.getOffset(); 368 369 // Iterate through the GEP folding the constants into offsets where 370 // we can. 371 gep_type_iterator GTI = gep_type_begin(U); 372 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; 373 ++i, ++GTI) { 374 const Value *Op = *i; 375 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 376 const StructLayout *SL = DL.getStructLayout(STy); 377 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 378 TmpOffset += SL->getElementOffset(Idx); 379 } else { 380 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); 381 for (;;) { 382 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 383 // Constant-offset addressing. 384 TmpOffset += CI->getSExtValue() * S; 385 break; 386 } 387 if (canFoldAddIntoGEP(U, Op)) { 388 // A compatible add with a constant operand. Fold the constant. 389 ConstantInt *CI = 390 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); 391 TmpOffset += CI->getSExtValue() * S; 392 // Iterate on the other operand. 393 Op = cast<AddOperator>(Op)->getOperand(0); 394 continue; 395 } 396 // Unsupported 397 goto unsupported_gep; 398 } 399 } 400 } 401 402 // Try to grab the base operand now. 403 Addr.setOffset(TmpOffset); 404 if (ComputeAddress(U->getOperand(0), Addr)) 405 return true; 406 407 // We failed, restore everything and try the other options. 408 Addr = SavedAddr; 409 410 unsupported_gep: 411 break; 412 } 413 case Instruction::Alloca: { 414 const AllocaInst *AI = cast<AllocaInst>(Obj); 415 DenseMap<const AllocaInst *, int>::iterator SI = 416 FuncInfo.StaticAllocaMap.find(AI); 417 if (SI != FuncInfo.StaticAllocaMap.end()) { 418 Addr.setKind(Address::FrameIndexBase); 419 Addr.setFI(SI->second); 420 return true; 421 } 422 break; 423 } 424 case Instruction::Add: 425 // Adds of constants are common and easy enough. 426 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { 427 Addr.setOffset(Addr.getOffset() + (uint64_t)CI->getSExtValue()); 428 return ComputeAddress(U->getOperand(0), Addr); 429 } 430 break; 431 } 432 433 // Try to get this in a register if nothing else has worked. 434 if (!Addr.isValid()) 435 Addr.setReg(getRegForValue(Obj)); 436 return Addr.isValid(); 437 } 438 439 bool AArch64FastISel::ComputeCallAddress(const Value *V, Address &Addr) { 440 const User *U = nullptr; 441 unsigned Opcode = Instruction::UserOp1; 442 bool InMBB = true; 443 444 if (const auto *I = dyn_cast<Instruction>(V)) { 445 Opcode = I->getOpcode(); 446 U = I; 447 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock(); 448 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) { 449 Opcode = C->getOpcode(); 450 U = C; 451 } 452 453 switch (Opcode) { 454 default: break; 455 case Instruction::BitCast: 456 // Look past bitcasts if its operand is in the same BB. 457 if (InMBB) 458 return ComputeCallAddress(U->getOperand(0), Addr); 459 break; 460 case Instruction::IntToPtr: 461 // Look past no-op inttoptrs if its operand is in the same BB. 462 if (InMBB && 463 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 464 return ComputeCallAddress(U->getOperand(0), Addr); 465 break; 466 case Instruction::PtrToInt: 467 // Look past no-op ptrtoints if its operand is in the same BB. 468 if (InMBB && 469 TLI.getValueType(U->getType()) == TLI.getPointerTy()) 470 return ComputeCallAddress(U->getOperand(0), Addr); 471 break; 472 } 473 474 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 475 Addr.setGlobalValue(GV); 476 return true; 477 } 478 479 // If all else fails, try to materialize the value in a register. 480 if (!Addr.getGlobalValue()) { 481 Addr.setReg(getRegForValue(V)); 482 return Addr.getReg() != 0; 483 } 484 485 return false; 486 } 487 488 489 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) { 490 EVT evt = TLI.getValueType(Ty, true); 491 492 // Only handle simple types. 493 if (evt == MVT::Other || !evt.isSimple()) 494 return false; 495 VT = evt.getSimpleVT(); 496 497 // This is a legal type, but it's not something we handle in fast-isel. 498 if (VT == MVT::f128) 499 return false; 500 501 // Handle all other legal types, i.e. a register that will directly hold this 502 // value. 503 return TLI.isTypeLegal(VT); 504 } 505 506 bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) { 507 if (isTypeLegal(Ty, VT)) 508 return true; 509 510 // If this is a type than can be sign or zero-extended to a basic operation 511 // go ahead and accept it now. For stores, this reflects truncation. 512 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 513 return true; 514 515 return false; 516 } 517 518 bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT, 519 int64_t ScaleFactor, bool UseUnscaled) { 520 bool needsLowering = false; 521 int64_t Offset = Addr.getOffset(); 522 switch (VT.SimpleTy) { 523 default: 524 return false; 525 case MVT::i1: 526 case MVT::i8: 527 case MVT::i16: 528 case MVT::i32: 529 case MVT::i64: 530 case MVT::f32: 531 case MVT::f64: 532 if (!UseUnscaled) 533 // Using scaled, 12-bit, unsigned immediate offsets. 534 needsLowering = ((Offset & 0xfff) != Offset); 535 else 536 // Using unscaled, 9-bit, signed immediate offsets. 537 needsLowering = (Offset > 256 || Offset < -256); 538 break; 539 } 540 541 //If this is a stack pointer and the offset needs to be simplified then put 542 // the alloca address into a register, set the base type back to register and 543 // continue. This should almost never happen. 544 if (needsLowering && Addr.getKind() == Address::FrameIndexBase) { 545 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass); 546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri), 547 ResultReg) 548 .addFrameIndex(Addr.getFI()) 549 .addImm(0) 550 .addImm(0); 551 Addr.setKind(Address::RegBase); 552 Addr.setReg(ResultReg); 553 } 554 555 // Since the offset is too large for the load/store instruction get the 556 // reg+offset into a register. 557 if (needsLowering) { 558 uint64_t UnscaledOffset = Addr.getOffset() * ScaleFactor; 559 unsigned ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), false, 560 UnscaledOffset, MVT::i64); 561 if (ResultReg == 0) 562 return false; 563 Addr.setReg(ResultReg); 564 Addr.setOffset(0); 565 } 566 return true; 567 } 568 569 void AArch64FastISel::AddLoadStoreOperands(Address &Addr, 570 const MachineInstrBuilder &MIB, 571 unsigned Flags, 572 MachineMemOperand *MMO, 573 bool UseUnscaled) { 574 int64_t Offset = Addr.getOffset(); 575 // Frame base works a bit differently. Handle it separately. 576 if (Addr.getKind() == Address::FrameIndexBase) { 577 int FI = Addr.getFI(); 578 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size 579 // and alignment should be based on the VT. 580 MMO = FuncInfo.MF->getMachineMemOperand( 581 MachinePointerInfo::getFixedStack(FI, Offset), Flags, 582 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI)); 583 // Now add the rest of the operands. 584 MIB.addFrameIndex(FI).addImm(Offset); 585 } else { 586 // Now add the rest of the operands. 587 MIB.addReg(Addr.getReg()); 588 MIB.addImm(Offset); 589 } 590 591 if (MMO) 592 MIB.addMemOperand(MMO); 593 } 594 595 bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr, 596 MachineMemOperand *MMO, bool UseUnscaled) { 597 // Negative offsets require unscaled, 9-bit, signed immediate offsets. 598 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets. 599 if (!UseUnscaled && Addr.getOffset() < 0) 600 UseUnscaled = true; 601 602 unsigned Opc; 603 const TargetRegisterClass *RC; 604 bool VTIsi1 = false; 605 int64_t ScaleFactor = 0; 606 switch (VT.SimpleTy) { 607 default: 608 return false; 609 case MVT::i1: 610 VTIsi1 = true; 611 // Intentional fall-through. 612 case MVT::i8: 613 Opc = UseUnscaled ? AArch64::LDURBBi : AArch64::LDRBBui; 614 RC = &AArch64::GPR32RegClass; 615 ScaleFactor = 1; 616 break; 617 case MVT::i16: 618 Opc = UseUnscaled ? AArch64::LDURHHi : AArch64::LDRHHui; 619 RC = &AArch64::GPR32RegClass; 620 ScaleFactor = 2; 621 break; 622 case MVT::i32: 623 Opc = UseUnscaled ? AArch64::LDURWi : AArch64::LDRWui; 624 RC = &AArch64::GPR32RegClass; 625 ScaleFactor = 4; 626 break; 627 case MVT::i64: 628 Opc = UseUnscaled ? AArch64::LDURXi : AArch64::LDRXui; 629 RC = &AArch64::GPR64RegClass; 630 ScaleFactor = 8; 631 break; 632 case MVT::f32: 633 Opc = UseUnscaled ? AArch64::LDURSi : AArch64::LDRSui; 634 RC = TLI.getRegClassFor(VT); 635 ScaleFactor = 4; 636 break; 637 case MVT::f64: 638 Opc = UseUnscaled ? AArch64::LDURDi : AArch64::LDRDui; 639 RC = TLI.getRegClassFor(VT); 640 ScaleFactor = 8; 641 break; 642 } 643 // Scale the offset. 644 if (!UseUnscaled) { 645 int64_t Offset = Addr.getOffset(); 646 if (Offset & (ScaleFactor - 1)) 647 // Retry using an unscaled, 9-bit, signed immediate offset. 648 return EmitLoad(VT, ResultReg, Addr, MMO, /*UseUnscaled*/ true); 649 650 Addr.setOffset(Offset / ScaleFactor); 651 } 652 653 // Simplify this down to something we can handle. 654 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled)) 655 return false; 656 657 // Create the base instruction, then add the operands. 658 ResultReg = createResultReg(RC); 659 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 660 TII.get(Opc), ResultReg); 661 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, MMO, UseUnscaled); 662 663 // Loading an i1 requires special handling. 664 if (VTIsi1) { 665 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass); 666 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass); 667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri), 668 ANDReg) 669 .addReg(ResultReg) 670 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 671 ResultReg = ANDReg; 672 } 673 return true; 674 } 675 676 bool AArch64FastISel::SelectLoad(const Instruction *I) { 677 MVT VT; 678 // Verify we have a legal type before going any further. Currently, we handle 679 // simple types that will directly fit in a register (i32/f32/i64/f64) or 680 // those that can be sign or zero-extended to a basic operation (i1/i8/i16). 681 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic()) 682 return false; 683 684 // See if we can handle this address. 685 Address Addr; 686 if (!ComputeAddress(I->getOperand(0), Addr)) 687 return false; 688 689 unsigned ResultReg; 690 if (!EmitLoad(VT, ResultReg, Addr, createMachineMemOperandFor(I))) 691 return false; 692 693 UpdateValueMap(I, ResultReg); 694 return true; 695 } 696 697 bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr, 698 MachineMemOperand *MMO, bool UseUnscaled) { 699 // Negative offsets require unscaled, 9-bit, signed immediate offsets. 700 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets. 701 if (!UseUnscaled && Addr.getOffset() < 0) 702 UseUnscaled = true; 703 704 unsigned StrOpc; 705 bool VTIsi1 = false; 706 int64_t ScaleFactor = 0; 707 // Using scaled, 12-bit, unsigned immediate offsets. 708 switch (VT.SimpleTy) { 709 default: 710 return false; 711 case MVT::i1: 712 VTIsi1 = true; 713 case MVT::i8: 714 StrOpc = UseUnscaled ? AArch64::STURBBi : AArch64::STRBBui; 715 ScaleFactor = 1; 716 break; 717 case MVT::i16: 718 StrOpc = UseUnscaled ? AArch64::STURHHi : AArch64::STRHHui; 719 ScaleFactor = 2; 720 break; 721 case MVT::i32: 722 StrOpc = UseUnscaled ? AArch64::STURWi : AArch64::STRWui; 723 ScaleFactor = 4; 724 break; 725 case MVT::i64: 726 StrOpc = UseUnscaled ? AArch64::STURXi : AArch64::STRXui; 727 ScaleFactor = 8; 728 break; 729 case MVT::f32: 730 StrOpc = UseUnscaled ? AArch64::STURSi : AArch64::STRSui; 731 ScaleFactor = 4; 732 break; 733 case MVT::f64: 734 StrOpc = UseUnscaled ? AArch64::STURDi : AArch64::STRDui; 735 ScaleFactor = 8; 736 break; 737 } 738 // Scale the offset. 739 if (!UseUnscaled) { 740 int64_t Offset = Addr.getOffset(); 741 if (Offset & (ScaleFactor - 1)) 742 // Retry using an unscaled, 9-bit, signed immediate offset. 743 return EmitStore(VT, SrcReg, Addr, MMO, /*UseUnscaled*/ true); 744 745 Addr.setOffset(Offset / ScaleFactor); 746 } 747 748 // Simplify this down to something we can handle. 749 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled)) 750 return false; 751 752 // Storing an i1 requires special handling. 753 if (VTIsi1) { 754 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass); 755 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass); 756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri), 757 ANDReg) 758 .addReg(SrcReg) 759 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 760 SrcReg = ANDReg; 761 } 762 // Create the base instruction, then add the operands. 763 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 764 TII.get(StrOpc)).addReg(SrcReg); 765 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, MMO, UseUnscaled); 766 767 return true; 768 } 769 770 bool AArch64FastISel::SelectStore(const Instruction *I) { 771 MVT VT; 772 Value *Op0 = I->getOperand(0); 773 // Verify we have a legal type before going any further. Currently, we handle 774 // simple types that will directly fit in a register (i32/f32/i64/f64) or 775 // those that can be sign or zero-extended to a basic operation (i1/i8/i16). 776 if (!isLoadStoreTypeLegal(Op0->getType(), VT) || 777 cast<StoreInst>(I)->isAtomic()) 778 return false; 779 780 // Get the value to be stored into a register. 781 unsigned SrcReg = getRegForValue(Op0); 782 if (SrcReg == 0) 783 return false; 784 785 // See if we can handle this address. 786 Address Addr; 787 if (!ComputeAddress(I->getOperand(1), Addr)) 788 return false; 789 790 if (!EmitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I))) 791 return false; 792 return true; 793 } 794 795 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) { 796 switch (Pred) { 797 case CmpInst::FCMP_ONE: 798 case CmpInst::FCMP_UEQ: 799 default: 800 // AL is our "false" for now. The other two need more compares. 801 return AArch64CC::AL; 802 case CmpInst::ICMP_EQ: 803 case CmpInst::FCMP_OEQ: 804 return AArch64CC::EQ; 805 case CmpInst::ICMP_SGT: 806 case CmpInst::FCMP_OGT: 807 return AArch64CC::GT; 808 case CmpInst::ICMP_SGE: 809 case CmpInst::FCMP_OGE: 810 return AArch64CC::GE; 811 case CmpInst::ICMP_UGT: 812 case CmpInst::FCMP_UGT: 813 return AArch64CC::HI; 814 case CmpInst::FCMP_OLT: 815 return AArch64CC::MI; 816 case CmpInst::ICMP_ULE: 817 case CmpInst::FCMP_OLE: 818 return AArch64CC::LS; 819 case CmpInst::FCMP_ORD: 820 return AArch64CC::VC; 821 case CmpInst::FCMP_UNO: 822 return AArch64CC::VS; 823 case CmpInst::FCMP_UGE: 824 return AArch64CC::PL; 825 case CmpInst::ICMP_SLT: 826 case CmpInst::FCMP_ULT: 827 return AArch64CC::LT; 828 case CmpInst::ICMP_SLE: 829 case CmpInst::FCMP_ULE: 830 return AArch64CC::LE; 831 case CmpInst::FCMP_UNE: 832 case CmpInst::ICMP_NE: 833 return AArch64CC::NE; 834 case CmpInst::ICMP_UGE: 835 return AArch64CC::HS; 836 case CmpInst::ICMP_ULT: 837 return AArch64CC::LO; 838 } 839 } 840 841 bool AArch64FastISel::SelectBranch(const Instruction *I) { 842 const BranchInst *BI = cast<BranchInst>(I); 843 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 844 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 845 846 AArch64CC::CondCode CC = AArch64CC::NE; 847 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 848 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) { 849 // We may not handle every CC for now. 850 CC = getCompareCC(CI->getPredicate()); 851 if (CC == AArch64CC::AL) 852 return false; 853 854 // Emit the cmp. 855 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) 856 return false; 857 858 // Emit the branch. 859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc)) 860 .addImm(CC) 861 .addMBB(TBB); 862 863 // Obtain the branch weight and add the TrueBB to the successor list. 864 uint32_t BranchWeight = 0; 865 if (FuncInfo.BPI) 866 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(), 867 TBB->getBasicBlock()); 868 FuncInfo.MBB->addSuccessor(TBB, BranchWeight); 869 870 FastEmitBranch(FBB, DbgLoc); 871 return true; 872 } 873 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) { 874 MVT SrcVT; 875 if (TI->hasOneUse() && TI->getParent() == I->getParent() && 876 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) { 877 unsigned CondReg = getRegForValue(TI->getOperand(0)); 878 if (CondReg == 0) 879 return false; 880 881 // Issue an extract_subreg to get the lower 32-bits. 882 if (SrcVT == MVT::i64) 883 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true, 884 AArch64::sub_32); 885 886 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass); 887 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass); 888 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 889 TII.get(AArch64::ANDWri), ANDReg) 890 .addReg(CondReg) 891 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 892 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 893 TII.get(AArch64::SUBSWri)) 894 .addReg(ANDReg) 895 .addReg(ANDReg) 896 .addImm(0) 897 .addImm(0); 898 899 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 900 std::swap(TBB, FBB); 901 CC = AArch64CC::EQ; 902 } 903 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc)) 904 .addImm(CC) 905 .addMBB(TBB); 906 907 // Obtain the branch weight and add the TrueBB to the successor list. 908 uint32_t BranchWeight = 0; 909 if (FuncInfo.BPI) 910 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(), 911 TBB->getBasicBlock()); 912 FuncInfo.MBB->addSuccessor(TBB, BranchWeight); 913 914 FastEmitBranch(FBB, DbgLoc); 915 return true; 916 } 917 } else if (const ConstantInt *CI = 918 dyn_cast<ConstantInt>(BI->getCondition())) { 919 uint64_t Imm = CI->getZExtValue(); 920 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; 921 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B)) 922 .addMBB(Target); 923 924 // Obtain the branch weight and add the target to the successor list. 925 uint32_t BranchWeight = 0; 926 if (FuncInfo.BPI) 927 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(), 928 Target->getBasicBlock()); 929 FuncInfo.MBB->addSuccessor(Target, BranchWeight); 930 return true; 931 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) { 932 // Fake request the condition, otherwise the intrinsic might be completely 933 // optimized away. 934 unsigned CondReg = getRegForValue(BI->getCondition()); 935 if (!CondReg) 936 return false; 937 938 // Emit the branch. 939 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc)) 940 .addImm(CC) 941 .addMBB(TBB); 942 943 // Obtain the branch weight and add the TrueBB to the successor list. 944 uint32_t BranchWeight = 0; 945 if (FuncInfo.BPI) 946 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(), 947 TBB->getBasicBlock()); 948 FuncInfo.MBB->addSuccessor(TBB, BranchWeight); 949 950 FastEmitBranch(FBB, DbgLoc); 951 return true; 952 } 953 954 unsigned CondReg = getRegForValue(BI->getCondition()); 955 if (CondReg == 0) 956 return false; 957 958 // We've been divorced from our compare! Our block was split, and 959 // now our compare lives in a predecessor block. We musn't 960 // re-compare here, as the children of the compare aren't guaranteed 961 // live across the block boundary (we *could* check for this). 962 // Regardless, the compare has been done in the predecessor block, 963 // and it left a value for us in a virtual register. Ergo, we test 964 // the one-bit value left in the virtual register. 965 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri), 966 AArch64::WZR) 967 .addReg(CondReg) 968 .addImm(0) 969 .addImm(0); 970 971 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 972 std::swap(TBB, FBB); 973 CC = AArch64CC::EQ; 974 } 975 976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc)) 977 .addImm(CC) 978 .addMBB(TBB); 979 980 // Obtain the branch weight and add the TrueBB to the successor list. 981 uint32_t BranchWeight = 0; 982 if (FuncInfo.BPI) 983 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(), 984 TBB->getBasicBlock()); 985 FuncInfo.MBB->addSuccessor(TBB, BranchWeight); 986 987 FastEmitBranch(FBB, DbgLoc); 988 return true; 989 } 990 991 bool AArch64FastISel::SelectIndirectBr(const Instruction *I) { 992 const IndirectBrInst *BI = cast<IndirectBrInst>(I); 993 unsigned AddrReg = getRegForValue(BI->getOperand(0)); 994 if (AddrReg == 0) 995 return false; 996 997 // Emit the indirect branch. 998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR)) 999 .addReg(AddrReg); 1000 1001 // Make sure the CFG is up-to-date. 1002 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i) 1003 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]); 1004 1005 return true; 1006 } 1007 1008 bool AArch64FastISel::EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt) { 1009 Type *Ty = Src1Value->getType(); 1010 EVT SrcEVT = TLI.getValueType(Ty, true); 1011 if (!SrcEVT.isSimple()) 1012 return false; 1013 MVT SrcVT = SrcEVT.getSimpleVT(); 1014 1015 // Check to see if the 2nd operand is a constant that we can encode directly 1016 // in the compare. 1017 uint64_t Imm; 1018 bool UseImm = false; 1019 bool isNegativeImm = false; 1020 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) { 1021 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 || 1022 SrcVT == MVT::i8 || SrcVT == MVT::i1) { 1023 const APInt &CIVal = ConstInt->getValue(); 1024 1025 Imm = (isZExt) ? CIVal.getZExtValue() : CIVal.getSExtValue(); 1026 if (CIVal.isNegative()) { 1027 isNegativeImm = true; 1028 Imm = -Imm; 1029 } 1030 // FIXME: We can handle more immediates using shifts. 1031 UseImm = ((Imm & 0xfff) == Imm); 1032 } 1033 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) { 1034 if (SrcVT == MVT::f32 || SrcVT == MVT::f64) 1035 if (ConstFP->isZero() && !ConstFP->isNegative()) 1036 UseImm = true; 1037 } 1038 1039 unsigned ZReg; 1040 unsigned CmpOpc; 1041 bool isICmp = true; 1042 bool needsExt = false; 1043 switch (SrcVT.SimpleTy) { 1044 default: 1045 return false; 1046 case MVT::i1: 1047 case MVT::i8: 1048 case MVT::i16: 1049 needsExt = true; 1050 // Intentional fall-through. 1051 case MVT::i32: 1052 ZReg = AArch64::WZR; 1053 if (UseImm) 1054 CmpOpc = isNegativeImm ? AArch64::ADDSWri : AArch64::SUBSWri; 1055 else 1056 CmpOpc = AArch64::SUBSWrr; 1057 break; 1058 case MVT::i64: 1059 ZReg = AArch64::XZR; 1060 if (UseImm) 1061 CmpOpc = isNegativeImm ? AArch64::ADDSXri : AArch64::SUBSXri; 1062 else 1063 CmpOpc = AArch64::SUBSXrr; 1064 break; 1065 case MVT::f32: 1066 isICmp = false; 1067 CmpOpc = UseImm ? AArch64::FCMPSri : AArch64::FCMPSrr; 1068 break; 1069 case MVT::f64: 1070 isICmp = false; 1071 CmpOpc = UseImm ? AArch64::FCMPDri : AArch64::FCMPDrr; 1072 break; 1073 } 1074 1075 unsigned SrcReg1 = getRegForValue(Src1Value); 1076 if (SrcReg1 == 0) 1077 return false; 1078 1079 unsigned SrcReg2; 1080 if (!UseImm) { 1081 SrcReg2 = getRegForValue(Src2Value); 1082 if (SrcReg2 == 0) 1083 return false; 1084 } 1085 1086 // We have i1, i8, or i16, we need to either zero extend or sign extend. 1087 if (needsExt) { 1088 SrcReg1 = EmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt); 1089 if (SrcReg1 == 0) 1090 return false; 1091 if (!UseImm) { 1092 SrcReg2 = EmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt); 1093 if (SrcReg2 == 0) 1094 return false; 1095 } 1096 } 1097 1098 if (isICmp) { 1099 if (UseImm) 1100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc)) 1101 .addReg(ZReg) 1102 .addReg(SrcReg1) 1103 .addImm(Imm) 1104 .addImm(0); 1105 else 1106 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc)) 1107 .addReg(ZReg) 1108 .addReg(SrcReg1) 1109 .addReg(SrcReg2); 1110 } else { 1111 if (UseImm) 1112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc)) 1113 .addReg(SrcReg1); 1114 else 1115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc)) 1116 .addReg(SrcReg1) 1117 .addReg(SrcReg2); 1118 } 1119 return true; 1120 } 1121 1122 bool AArch64FastISel::SelectCmp(const Instruction *I) { 1123 const CmpInst *CI = cast<CmpInst>(I); 1124 1125 // We may not handle every CC for now. 1126 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate()); 1127 if (CC == AArch64CC::AL) 1128 return false; 1129 1130 // Emit the cmp. 1131 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned())) 1132 return false; 1133 1134 // Now set a register based on the comparison. 1135 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC); 1136 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass); 1137 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr), 1138 ResultReg) 1139 .addReg(AArch64::WZR) 1140 .addReg(AArch64::WZR) 1141 .addImm(invertedCC); 1142 1143 UpdateValueMap(I, ResultReg); 1144 return true; 1145 } 1146 1147 bool AArch64FastISel::SelectSelect(const Instruction *I) { 1148 const SelectInst *SI = cast<SelectInst>(I); 1149 1150 EVT DestEVT = TLI.getValueType(SI->getType(), true); 1151 if (!DestEVT.isSimple()) 1152 return false; 1153 1154 MVT DestVT = DestEVT.getSimpleVT(); 1155 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 && 1156 DestVT != MVT::f64) 1157 return false; 1158 1159 unsigned SelectOpc; 1160 switch (DestVT.SimpleTy) { 1161 default: return false; 1162 case MVT::i32: SelectOpc = AArch64::CSELWr; break; 1163 case MVT::i64: SelectOpc = AArch64::CSELXr; break; 1164 case MVT::f32: SelectOpc = AArch64::FCSELSrrr; break; 1165 case MVT::f64: SelectOpc = AArch64::FCSELDrrr; break; 1166 } 1167 1168 const Value *Cond = SI->getCondition(); 1169 bool NeedTest = true; 1170 AArch64CC::CondCode CC = AArch64CC::NE; 1171 if (foldXALUIntrinsic(CC, I, Cond)) 1172 NeedTest = false; 1173 1174 unsigned CondReg = getRegForValue(Cond); 1175 if (!CondReg) 1176 return false; 1177 bool CondIsKill = hasTrivialKill(Cond); 1178 1179 if (NeedTest) { 1180 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass); 1181 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass); 1182 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri), 1183 ANDReg) 1184 .addReg(CondReg, getKillRegState(CondIsKill)) 1185 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 1186 1187 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri)) 1188 .addReg(ANDReg) 1189 .addReg(ANDReg) 1190 .addImm(0) 1191 .addImm(0); 1192 } 1193 1194 unsigned TrueReg = getRegForValue(SI->getTrueValue()); 1195 bool TrueIsKill = hasTrivialKill(SI->getTrueValue()); 1196 1197 unsigned FalseReg = getRegForValue(SI->getFalseValue()); 1198 bool FalseIsKill = hasTrivialKill(SI->getFalseValue()); 1199 1200 if (!TrueReg || !FalseReg) 1201 return false; 1202 1203 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT)); 1204 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc), 1205 ResultReg) 1206 .addReg(TrueReg, getKillRegState(TrueIsKill)) 1207 .addReg(FalseReg, getKillRegState(FalseIsKill)) 1208 .addImm(CC); 1209 1210 UpdateValueMap(I, ResultReg); 1211 return true; 1212 } 1213 1214 bool AArch64FastISel::SelectFPExt(const Instruction *I) { 1215 Value *V = I->getOperand(0); 1216 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy()) 1217 return false; 1218 1219 unsigned Op = getRegForValue(V); 1220 if (Op == 0) 1221 return false; 1222 1223 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass); 1224 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr), 1225 ResultReg).addReg(Op); 1226 UpdateValueMap(I, ResultReg); 1227 return true; 1228 } 1229 1230 bool AArch64FastISel::SelectFPTrunc(const Instruction *I) { 1231 Value *V = I->getOperand(0); 1232 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy()) 1233 return false; 1234 1235 unsigned Op = getRegForValue(V); 1236 if (Op == 0) 1237 return false; 1238 1239 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass); 1240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr), 1241 ResultReg).addReg(Op); 1242 UpdateValueMap(I, ResultReg); 1243 return true; 1244 } 1245 1246 // FPToUI and FPToSI 1247 bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) { 1248 MVT DestVT; 1249 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector()) 1250 return false; 1251 1252 unsigned SrcReg = getRegForValue(I->getOperand(0)); 1253 if (SrcReg == 0) 1254 return false; 1255 1256 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true); 1257 if (SrcVT == MVT::f128) 1258 return false; 1259 1260 unsigned Opc; 1261 if (SrcVT == MVT::f64) { 1262 if (Signed) 1263 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr; 1264 else 1265 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr; 1266 } else { 1267 if (Signed) 1268 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr; 1269 else 1270 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr; 1271 } 1272 unsigned ResultReg = createResultReg( 1273 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass); 1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 1275 .addReg(SrcReg); 1276 UpdateValueMap(I, ResultReg); 1277 return true; 1278 } 1279 1280 bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) { 1281 MVT DestVT; 1282 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector()) 1283 return false; 1284 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) && 1285 "Unexpected value type."); 1286 1287 unsigned SrcReg = getRegForValue(I->getOperand(0)); 1288 if (SrcReg == 0) 1289 return false; 1290 1291 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true); 1292 1293 // Handle sign-extension. 1294 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) { 1295 SrcReg = 1296 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed); 1297 if (SrcReg == 0) 1298 return false; 1299 } 1300 1301 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass 1302 : &AArch64::GPR32RegClass); 1303 1304 unsigned Opc; 1305 if (SrcVT == MVT::i64) { 1306 if (Signed) 1307 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri; 1308 else 1309 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri; 1310 } else { 1311 if (Signed) 1312 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri; 1313 else 1314 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri; 1315 } 1316 1317 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT)); 1318 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 1319 .addReg(SrcReg); 1320 UpdateValueMap(I, ResultReg); 1321 return true; 1322 } 1323 1324 bool AArch64FastISel::FastLowerArguments() { 1325 if (!FuncInfo.CanLowerReturn) 1326 return false; 1327 1328 const Function *F = FuncInfo.Fn; 1329 if (F->isVarArg()) 1330 return false; 1331 1332 CallingConv::ID CC = F->getCallingConv(); 1333 if (CC != CallingConv::C) 1334 return false; 1335 1336 // Only handle simple cases like i1/i8/i16/i32/i64/f32/f64 of up to 8 GPR and 1337 // FPR each. 1338 unsigned GPRCnt = 0; 1339 unsigned FPRCnt = 0; 1340 unsigned Idx = 0; 1341 for (auto const &Arg : F->args()) { 1342 // The first argument is at index 1. 1343 ++Idx; 1344 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) || 1345 F->getAttributes().hasAttribute(Idx, Attribute::InReg) || 1346 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) || 1347 F->getAttributes().hasAttribute(Idx, Attribute::Nest)) 1348 return false; 1349 1350 Type *ArgTy = Arg.getType(); 1351 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) 1352 return false; 1353 1354 EVT ArgVT = TLI.getValueType(ArgTy); 1355 if (!ArgVT.isSimple()) return false; 1356 switch (ArgVT.getSimpleVT().SimpleTy) { 1357 default: return false; 1358 case MVT::i1: 1359 case MVT::i8: 1360 case MVT::i16: 1361 case MVT::i32: 1362 case MVT::i64: 1363 ++GPRCnt; 1364 break; 1365 case MVT::f16: 1366 case MVT::f32: 1367 case MVT::f64: 1368 ++FPRCnt; 1369 break; 1370 } 1371 1372 if (GPRCnt > 8 || FPRCnt > 8) 1373 return false; 1374 } 1375 1376 static const MCPhysReg Registers[5][8] = { 1377 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4, 1378 AArch64::W5, AArch64::W6, AArch64::W7 }, 1379 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4, 1380 AArch64::X5, AArch64::X6, AArch64::X7 }, 1381 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4, 1382 AArch64::H5, AArch64::H6, AArch64::H7 }, 1383 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4, 1384 AArch64::S5, AArch64::S6, AArch64::S7 }, 1385 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4, 1386 AArch64::D5, AArch64::D6, AArch64::D7 } 1387 }; 1388 1389 unsigned GPRIdx = 0; 1390 unsigned FPRIdx = 0; 1391 for (auto const &Arg : F->args()) { 1392 MVT VT = TLI.getSimpleValueType(Arg.getType()); 1393 unsigned SrcReg; 1394 switch (VT.SimpleTy) { 1395 default: llvm_unreachable("Unexpected value type."); 1396 case MVT::i1: 1397 case MVT::i8: 1398 case MVT::i16: VT = MVT::i32; // fall-through 1399 case MVT::i32: SrcReg = Registers[0][GPRIdx++]; break; 1400 case MVT::i64: SrcReg = Registers[1][GPRIdx++]; break; 1401 case MVT::f16: SrcReg = Registers[2][FPRIdx++]; break; 1402 case MVT::f32: SrcReg = Registers[3][FPRIdx++]; break; 1403 case MVT::f64: SrcReg = Registers[4][FPRIdx++]; break; 1404 } 1405 1406 // Skip unused arguments. 1407 if (Arg.use_empty()) { 1408 UpdateValueMap(&Arg, 0); 1409 continue; 1410 } 1411 1412 const TargetRegisterClass *RC = TLI.getRegClassFor(VT); 1413 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC); 1414 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy. 1415 // Without this, EmitLiveInCopies may eliminate the livein if its only 1416 // use is a bitcast (which isn't turned into an instruction). 1417 unsigned ResultReg = createResultReg(RC); 1418 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1419 TII.get(TargetOpcode::COPY), ResultReg) 1420 .addReg(DstReg, getKillRegState(true)); 1421 UpdateValueMap(&Arg, ResultReg); 1422 } 1423 return true; 1424 } 1425 1426 bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI, 1427 SmallVectorImpl<MVT> &OutVTs, 1428 unsigned &NumBytes) { 1429 CallingConv::ID CC = CLI.CallConv; 1430 SmallVector<CCValAssign, 16> ArgLocs; 1431 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context); 1432 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC)); 1433 1434 // Get a count of how many bytes are to be pushed on the stack. 1435 NumBytes = CCInfo.getNextStackOffset(); 1436 1437 // Issue CALLSEQ_START 1438 unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); 1439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown)) 1440 .addImm(NumBytes); 1441 1442 // Process the args. 1443 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1444 CCValAssign &VA = ArgLocs[i]; 1445 const Value *ArgVal = CLI.OutVals[VA.getValNo()]; 1446 MVT ArgVT = OutVTs[VA.getValNo()]; 1447 1448 unsigned ArgReg = getRegForValue(ArgVal); 1449 if (!ArgReg) 1450 return false; 1451 1452 // Handle arg promotion: SExt, ZExt, AExt. 1453 switch (VA.getLocInfo()) { 1454 case CCValAssign::Full: 1455 break; 1456 case CCValAssign::SExt: { 1457 MVT DestVT = VA.getLocVT(); 1458 MVT SrcVT = ArgVT; 1459 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false); 1460 if (!ArgReg) 1461 return false; 1462 break; 1463 } 1464 case CCValAssign::AExt: 1465 // Intentional fall-through. 1466 case CCValAssign::ZExt: { 1467 MVT DestVT = VA.getLocVT(); 1468 MVT SrcVT = ArgVT; 1469 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true); 1470 if (!ArgReg) 1471 return false; 1472 break; 1473 } 1474 default: 1475 llvm_unreachable("Unknown arg promotion!"); 1476 } 1477 1478 // Now copy/store arg to correct locations. 1479 if (VA.isRegLoc() && !VA.needsCustom()) { 1480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1481 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg); 1482 CLI.OutRegs.push_back(VA.getLocReg()); 1483 } else if (VA.needsCustom()) { 1484 // FIXME: Handle custom args. 1485 return false; 1486 } else { 1487 assert(VA.isMemLoc() && "Assuming store on stack."); 1488 1489 // Don't emit stores for undef values. 1490 if (isa<UndefValue>(ArgVal)) 1491 continue; 1492 1493 // Need to store on the stack. 1494 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8; 1495 1496 unsigned BEAlign = 0; 1497 if (ArgSize < 8 && !Subtarget->isLittleEndian()) 1498 BEAlign = 8 - ArgSize; 1499 1500 Address Addr; 1501 Addr.setKind(Address::RegBase); 1502 Addr.setReg(AArch64::SP); 1503 Addr.setOffset(VA.getLocMemOffset() + BEAlign); 1504 1505 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType()); 1506 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( 1507 MachinePointerInfo::getStack(Addr.getOffset()), 1508 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment); 1509 1510 if (!EmitStore(ArgVT, ArgReg, Addr, MMO)) 1511 return false; 1512 } 1513 } 1514 return true; 1515 } 1516 1517 bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT, 1518 unsigned NumBytes) { 1519 CallingConv::ID CC = CLI.CallConv; 1520 1521 // Issue CALLSEQ_END 1522 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); 1523 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp)) 1524 .addImm(NumBytes).addImm(0); 1525 1526 // Now the return value. 1527 if (RetVT != MVT::isVoid) { 1528 SmallVector<CCValAssign, 16> RVLocs; 1529 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context); 1530 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC)); 1531 1532 // Only handle a single return value. 1533 if (RVLocs.size() != 1) 1534 return false; 1535 1536 // Copy all of the result registers out of their specified physreg. 1537 MVT CopyVT = RVLocs[0].getValVT(); 1538 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT)); 1539 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1540 TII.get(TargetOpcode::COPY), ResultReg) 1541 .addReg(RVLocs[0].getLocReg()); 1542 CLI.InRegs.push_back(RVLocs[0].getLocReg()); 1543 1544 CLI.ResultReg = ResultReg; 1545 CLI.NumResultRegs = 1; 1546 } 1547 1548 return true; 1549 } 1550 1551 bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) { 1552 CallingConv::ID CC = CLI.CallConv; 1553 bool IsVarArg = CLI.IsVarArg; 1554 const Value *Callee = CLI.Callee; 1555 const char *SymName = CLI.SymName; 1556 1557 CodeModel::Model CM = TM.getCodeModel(); 1558 // Only support the small and large code model. 1559 if (CM != CodeModel::Small && CM != CodeModel::Large) 1560 return false; 1561 1562 // FIXME: Add large code model support for ELF. 1563 if (CM == CodeModel::Large && !Subtarget->isTargetMachO()) 1564 return false; 1565 1566 // Let SDISel handle vararg functions. 1567 if (IsVarArg) 1568 return false; 1569 1570 // FIXME: Only handle *simple* calls for now. 1571 MVT RetVT; 1572 if (CLI.RetTy->isVoidTy()) 1573 RetVT = MVT::isVoid; 1574 else if (!isTypeLegal(CLI.RetTy, RetVT)) 1575 return false; 1576 1577 for (auto Flag : CLI.OutFlags) 1578 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal()) 1579 return false; 1580 1581 // Set up the argument vectors. 1582 SmallVector<MVT, 16> OutVTs; 1583 OutVTs.reserve(CLI.OutVals.size()); 1584 1585 for (auto *Val : CLI.OutVals) { 1586 MVT VT; 1587 if (!isTypeLegal(Val->getType(), VT) && 1588 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) 1589 return false; 1590 1591 // We don't handle vector parameters yet. 1592 if (VT.isVector() || VT.getSizeInBits() > 64) 1593 return false; 1594 1595 OutVTs.push_back(VT); 1596 } 1597 1598 Address Addr; 1599 if (!ComputeCallAddress(Callee, Addr)) 1600 return false; 1601 1602 // Handle the arguments now that we've gotten them. 1603 unsigned NumBytes; 1604 if (!ProcessCallArgs(CLI, OutVTs, NumBytes)) 1605 return false; 1606 1607 // Issue the call. 1608 MachineInstrBuilder MIB; 1609 if (CM == CodeModel::Small) { 1610 unsigned CallOpc = Addr.getReg() ? AArch64::BLR : AArch64::BL; 1611 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc)); 1612 if (SymName) 1613 MIB.addExternalSymbol(SymName, 0); 1614 else if (Addr.getGlobalValue()) 1615 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0); 1616 else if (Addr.getReg()) 1617 MIB.addReg(Addr.getReg()); 1618 else 1619 return false; 1620 } else { 1621 unsigned CallReg = 0; 1622 if (SymName) { 1623 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass); 1624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP), 1625 ADRPReg) 1626 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE); 1627 1628 CallReg = createResultReg(&AArch64::GPR64RegClass); 1629 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui), 1630 CallReg) 1631 .addReg(ADRPReg) 1632 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | 1633 AArch64II::MO_NC); 1634 } else if (Addr.getGlobalValue()) { 1635 CallReg = AArch64MaterializeGV(Addr.getGlobalValue()); 1636 } else if (Addr.getReg()) 1637 CallReg = Addr.getReg(); 1638 1639 if (!CallReg) 1640 return false; 1641 1642 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1643 TII.get(AArch64::BLR)).addReg(CallReg); 1644 } 1645 1646 // Add implicit physical register uses to the call. 1647 for (auto Reg : CLI.OutRegs) 1648 MIB.addReg(Reg, RegState::Implicit); 1649 1650 // Add a register mask with the call-preserved registers. 1651 // Proper defs for return values will be added by setPhysRegsDeadExcept(). 1652 MIB.addRegMask(TRI.getCallPreservedMask(CC)); 1653 1654 CLI.Call = MIB; 1655 1656 // Finish off the call including any return values. 1657 return FinishCall(CLI, RetVT, NumBytes); 1658 } 1659 1660 bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) { 1661 if (Alignment) 1662 return Len / Alignment <= 4; 1663 else 1664 return Len < 32; 1665 } 1666 1667 bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src, 1668 uint64_t Len, unsigned Alignment) { 1669 // Make sure we don't bloat code by inlining very large memcpy's. 1670 if (!IsMemCpySmall(Len, Alignment)) 1671 return false; 1672 1673 int64_t UnscaledOffset = 0; 1674 Address OrigDest = Dest; 1675 Address OrigSrc = Src; 1676 1677 while (Len) { 1678 MVT VT; 1679 if (!Alignment || Alignment >= 8) { 1680 if (Len >= 8) 1681 VT = MVT::i64; 1682 else if (Len >= 4) 1683 VT = MVT::i32; 1684 else if (Len >= 2) 1685 VT = MVT::i16; 1686 else { 1687 VT = MVT::i8; 1688 } 1689 } else { 1690 // Bound based on alignment. 1691 if (Len >= 4 && Alignment == 4) 1692 VT = MVT::i32; 1693 else if (Len >= 2 && Alignment == 2) 1694 VT = MVT::i16; 1695 else { 1696 VT = MVT::i8; 1697 } 1698 } 1699 1700 bool RV; 1701 unsigned ResultReg; 1702 RV = EmitLoad(VT, ResultReg, Src); 1703 if (!RV) 1704 return false; 1705 1706 RV = EmitStore(VT, ResultReg, Dest); 1707 if (!RV) 1708 return false; 1709 1710 int64_t Size = VT.getSizeInBits() / 8; 1711 Len -= Size; 1712 UnscaledOffset += Size; 1713 1714 // We need to recompute the unscaled offset for each iteration. 1715 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset); 1716 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset); 1717 } 1718 1719 return true; 1720 } 1721 1722 /// \brief Check if it is possible to fold the condition from the XALU intrinsic 1723 /// into the user. The condition code will only be updated on success. 1724 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC, 1725 const Instruction *I, 1726 const Value *Cond) { 1727 if (!isa<ExtractValueInst>(Cond)) 1728 return false; 1729 1730 const auto *EV = cast<ExtractValueInst>(Cond); 1731 if (!isa<IntrinsicInst>(EV->getAggregateOperand())) 1732 return false; 1733 1734 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand()); 1735 MVT RetVT; 1736 const Function *Callee = II->getCalledFunction(); 1737 Type *RetTy = 1738 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U); 1739 if (!isTypeLegal(RetTy, RetVT)) 1740 return false; 1741 1742 if (RetVT != MVT::i32 && RetVT != MVT::i64) 1743 return false; 1744 1745 AArch64CC::CondCode TmpCC; 1746 switch (II->getIntrinsicID()) { 1747 default: return false; 1748 case Intrinsic::sadd_with_overflow: 1749 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break; 1750 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break; 1751 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break; 1752 case Intrinsic::smul_with_overflow: 1753 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break; 1754 } 1755 1756 // Check if both instructions are in the same basic block. 1757 if (II->getParent() != I->getParent()) 1758 return false; 1759 1760 // Make sure nothing is in the way 1761 BasicBlock::const_iterator Start = I; 1762 BasicBlock::const_iterator End = II; 1763 for (auto Itr = std::prev(Start); Itr != End; --Itr) { 1764 // We only expect extractvalue instructions between the intrinsic and the 1765 // instruction to be selected. 1766 if (!isa<ExtractValueInst>(Itr)) 1767 return false; 1768 1769 // Check that the extractvalue operand comes from the intrinsic. 1770 const auto *EVI = cast<ExtractValueInst>(Itr); 1771 if (EVI->getAggregateOperand() != II) 1772 return false; 1773 } 1774 1775 CC = TmpCC; 1776 return true; 1777 } 1778 1779 bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) { 1780 // FIXME: Handle more intrinsics. 1781 switch (II->getIntrinsicID()) { 1782 default: return false; 1783 case Intrinsic::frameaddress: { 1784 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo(); 1785 MFI->setFrameAddressIsTaken(true); 1786 1787 const AArch64RegisterInfo *RegInfo = 1788 static_cast<const AArch64RegisterInfo *>( 1789 TM.getSubtargetImpl()->getRegisterInfo()); 1790 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF)); 1791 unsigned SrcReg = FramePtr; 1792 1793 // Recursively load frame address 1794 // ldr x0, [fp] 1795 // ldr x0, [x0] 1796 // ldr x0, [x0] 1797 // ... 1798 unsigned DestReg; 1799 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue(); 1800 while (Depth--) { 1801 DestReg = createResultReg(&AArch64::GPR64RegClass); 1802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1803 TII.get(AArch64::LDRXui), DestReg) 1804 .addReg(SrcReg).addImm(0); 1805 SrcReg = DestReg; 1806 } 1807 1808 UpdateValueMap(II, SrcReg); 1809 return true; 1810 } 1811 case Intrinsic::memcpy: 1812 case Intrinsic::memmove: { 1813 const auto *MTI = cast<MemTransferInst>(II); 1814 // Don't handle volatile. 1815 if (MTI->isVolatile()) 1816 return false; 1817 1818 // Disable inlining for memmove before calls to ComputeAddress. Otherwise, 1819 // we would emit dead code because we don't currently handle memmoves. 1820 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy); 1821 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) { 1822 // Small memcpy's are common enough that we want to do them without a call 1823 // if possible. 1824 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue(); 1825 unsigned Alignment = MTI->getAlignment(); 1826 if (IsMemCpySmall(Len, Alignment)) { 1827 Address Dest, Src; 1828 if (!ComputeAddress(MTI->getRawDest(), Dest) || 1829 !ComputeAddress(MTI->getRawSource(), Src)) 1830 return false; 1831 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment)) 1832 return true; 1833 } 1834 } 1835 1836 if (!MTI->getLength()->getType()->isIntegerTy(64)) 1837 return false; 1838 1839 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255) 1840 // Fast instruction selection doesn't support the special 1841 // address spaces. 1842 return false; 1843 1844 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove"; 1845 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2); 1846 } 1847 case Intrinsic::memset: { 1848 const MemSetInst *MSI = cast<MemSetInst>(II); 1849 // Don't handle volatile. 1850 if (MSI->isVolatile()) 1851 return false; 1852 1853 if (!MSI->getLength()->getType()->isIntegerTy(64)) 1854 return false; 1855 1856 if (MSI->getDestAddressSpace() > 255) 1857 // Fast instruction selection doesn't support the special 1858 // address spaces. 1859 return false; 1860 1861 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2); 1862 } 1863 case Intrinsic::trap: { 1864 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK)) 1865 .addImm(1); 1866 return true; 1867 } 1868 case Intrinsic::sqrt: { 1869 Type *RetTy = II->getCalledFunction()->getReturnType(); 1870 1871 MVT VT; 1872 if (!isTypeLegal(RetTy, VT)) 1873 return false; 1874 1875 unsigned Op0Reg = getRegForValue(II->getOperand(0)); 1876 if (!Op0Reg) 1877 return false; 1878 bool Op0IsKill = hasTrivialKill(II->getOperand(0)); 1879 1880 unsigned ResultReg = FastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill); 1881 if (!ResultReg) 1882 return false; 1883 1884 UpdateValueMap(II, ResultReg); 1885 return true; 1886 } 1887 case Intrinsic::sadd_with_overflow: 1888 case Intrinsic::uadd_with_overflow: 1889 case Intrinsic::ssub_with_overflow: 1890 case Intrinsic::usub_with_overflow: 1891 case Intrinsic::smul_with_overflow: 1892 case Intrinsic::umul_with_overflow: { 1893 // This implements the basic lowering of the xalu with overflow intrinsics. 1894 const Function *Callee = II->getCalledFunction(); 1895 auto *Ty = cast<StructType>(Callee->getReturnType()); 1896 Type *RetTy = Ty->getTypeAtIndex(0U); 1897 Type *CondTy = Ty->getTypeAtIndex(1); 1898 1899 MVT VT; 1900 if (!isTypeLegal(RetTy, VT)) 1901 return false; 1902 1903 if (VT != MVT::i32 && VT != MVT::i64) 1904 return false; 1905 1906 const Value *LHS = II->getArgOperand(0); 1907 const Value *RHS = II->getArgOperand(1); 1908 // Canonicalize immediate to the RHS. 1909 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) && 1910 isCommutativeIntrinsic(II)) 1911 std::swap(LHS, RHS); 1912 1913 unsigned LHSReg = getRegForValue(LHS); 1914 if (!LHSReg) 1915 return false; 1916 bool LHSIsKill = hasTrivialKill(LHS); 1917 1918 // Check if the immediate can be encoded in the instruction and if we should 1919 // invert the instruction (adds -> subs) to handle negative immediates. 1920 bool UseImm = false; 1921 bool UseInverse = false; 1922 uint64_t Imm = 0; 1923 if (const auto *C = dyn_cast<ConstantInt>(RHS)) { 1924 if (C->isNegative()) { 1925 UseInverse = true; 1926 Imm = -(C->getSExtValue()); 1927 } else 1928 Imm = C->getZExtValue(); 1929 1930 if (isUInt<12>(Imm)) 1931 UseImm = true; 1932 1933 UseInverse = UseImm && UseInverse; 1934 } 1935 1936 static const unsigned OpcTable[2][2][2] = { 1937 { {AArch64::ADDSWrr, AArch64::ADDSXrr}, 1938 {AArch64::ADDSWri, AArch64::ADDSXri} }, 1939 { {AArch64::SUBSWrr, AArch64::SUBSXrr}, 1940 {AArch64::SUBSWri, AArch64::SUBSXri} } 1941 }; 1942 unsigned Opc = 0; 1943 unsigned MulReg = 0; 1944 unsigned RHSReg = 0; 1945 bool RHSIsKill = false; 1946 AArch64CC::CondCode CC = AArch64CC::Invalid; 1947 bool Is64Bit = VT == MVT::i64; 1948 switch (II->getIntrinsicID()) { 1949 default: llvm_unreachable("Unexpected intrinsic!"); 1950 case Intrinsic::sadd_with_overflow: 1951 Opc = OpcTable[UseInverse][UseImm][Is64Bit]; CC = AArch64CC::VS; break; 1952 case Intrinsic::uadd_with_overflow: 1953 Opc = OpcTable[UseInverse][UseImm][Is64Bit]; CC = AArch64CC::HS; break; 1954 case Intrinsic::ssub_with_overflow: 1955 Opc = OpcTable[!UseInverse][UseImm][Is64Bit]; CC = AArch64CC::VS; break; 1956 case Intrinsic::usub_with_overflow: 1957 Opc = OpcTable[!UseInverse][UseImm][Is64Bit]; CC = AArch64CC::LO; break; 1958 case Intrinsic::smul_with_overflow: { 1959 CC = AArch64CC::NE; 1960 RHSReg = getRegForValue(RHS); 1961 if (!RHSReg) 1962 return false; 1963 RHSIsKill = hasTrivialKill(RHS); 1964 1965 if (VT == MVT::i32) { 1966 MulReg = Emit_SMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill); 1967 unsigned ShiftReg = Emit_LSR_ri(MVT::i64, MulReg, false, 32); 1968 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true, 1969 AArch64::sub_32); 1970 ShiftReg = FastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true, 1971 AArch64::sub_32); 1972 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT)); 1973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1974 TII.get(AArch64::SUBSWrs), CmpReg) 1975 .addReg(ShiftReg, getKillRegState(true)) 1976 .addReg(MulReg, getKillRegState(false)) 1977 .addImm(159); // 159 <-> asr #31 1978 } else { 1979 assert(VT == MVT::i64 && "Unexpected value type."); 1980 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill); 1981 unsigned SMULHReg = FastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill, 1982 RHSReg, RHSIsKill); 1983 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT)); 1984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1985 TII.get(AArch64::SUBSXrs), CmpReg) 1986 .addReg(SMULHReg, getKillRegState(true)) 1987 .addReg(MulReg, getKillRegState(false)) 1988 .addImm(191); // 191 <-> asr #63 1989 } 1990 break; 1991 } 1992 case Intrinsic::umul_with_overflow: { 1993 CC = AArch64CC::NE; 1994 RHSReg = getRegForValue(RHS); 1995 if (!RHSReg) 1996 return false; 1997 RHSIsKill = hasTrivialKill(RHS); 1998 1999 if (VT == MVT::i32) { 2000 MulReg = Emit_UMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill); 2001 unsigned CmpReg = createResultReg(TLI.getRegClassFor(MVT::i64)); 2002 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2003 TII.get(AArch64::SUBSXrs), CmpReg) 2004 .addReg(AArch64::XZR, getKillRegState(true)) 2005 .addReg(MulReg, getKillRegState(false)) 2006 .addImm(96); // 96 <-> lsr #32 2007 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true, 2008 AArch64::sub_32); 2009 } else { 2010 assert(VT == MVT::i64 && "Unexpected value type."); 2011 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill); 2012 unsigned UMULHReg = FastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill, 2013 RHSReg, RHSIsKill); 2014 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT)); 2015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2016 TII.get(AArch64::SUBSXrr), CmpReg) 2017 .addReg(AArch64::XZR, getKillRegState(true)) 2018 .addReg(UMULHReg, getKillRegState(false)); 2019 } 2020 break; 2021 } 2022 } 2023 2024 if (!UseImm) { 2025 RHSReg = getRegForValue(RHS); 2026 if (!RHSReg) 2027 return false; 2028 RHSIsKill = hasTrivialKill(RHS); 2029 } 2030 2031 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT)); 2032 if (Opc) { 2033 MachineInstrBuilder MIB; 2034 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), 2035 ResultReg) 2036 .addReg(LHSReg, getKillRegState(LHSIsKill)); 2037 if (UseImm) { 2038 MIB.addImm(Imm); 2039 MIB.addImm(0); 2040 } else 2041 MIB.addReg(RHSReg, getKillRegState(RHSIsKill)); 2042 } 2043 else 2044 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2045 TII.get(TargetOpcode::COPY), ResultReg) 2046 .addReg(MulReg); 2047 2048 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy); 2049 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers."); 2050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr), 2051 ResultReg2) 2052 .addReg(AArch64::WZR, getKillRegState(true)) 2053 .addReg(AArch64::WZR, getKillRegState(true)) 2054 .addImm(getInvertedCondCode(CC)); 2055 2056 UpdateValueMap(II, ResultReg, 2); 2057 return true; 2058 } 2059 } 2060 return false; 2061 } 2062 2063 bool AArch64FastISel::SelectRet(const Instruction *I) { 2064 const ReturnInst *Ret = cast<ReturnInst>(I); 2065 const Function &F = *I->getParent()->getParent(); 2066 2067 if (!FuncInfo.CanLowerReturn) 2068 return false; 2069 2070 if (F.isVarArg()) 2071 return false; 2072 2073 // Build a list of return value registers. 2074 SmallVector<unsigned, 4> RetRegs; 2075 2076 if (Ret->getNumOperands() > 0) { 2077 CallingConv::ID CC = F.getCallingConv(); 2078 SmallVector<ISD::OutputArg, 4> Outs; 2079 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI); 2080 2081 // Analyze operands of the call, assigning locations to each operand. 2082 SmallVector<CCValAssign, 16> ValLocs; 2083 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext()); 2084 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS 2085 : RetCC_AArch64_AAPCS; 2086 CCInfo.AnalyzeReturn(Outs, RetCC); 2087 2088 // Only handle a single return value for now. 2089 if (ValLocs.size() != 1) 2090 return false; 2091 2092 CCValAssign &VA = ValLocs[0]; 2093 const Value *RV = Ret->getOperand(0); 2094 2095 // Don't bother handling odd stuff for now. 2096 if (VA.getLocInfo() != CCValAssign::Full) 2097 return false; 2098 // Only handle register returns for now. 2099 if (!VA.isRegLoc()) 2100 return false; 2101 unsigned Reg = getRegForValue(RV); 2102 if (Reg == 0) 2103 return false; 2104 2105 unsigned SrcReg = Reg + VA.getValNo(); 2106 unsigned DestReg = VA.getLocReg(); 2107 // Avoid a cross-class copy. This is very unlikely. 2108 if (!MRI.getRegClass(SrcReg)->contains(DestReg)) 2109 return false; 2110 2111 EVT RVEVT = TLI.getValueType(RV->getType()); 2112 if (!RVEVT.isSimple()) 2113 return false; 2114 2115 // Vectors (of > 1 lane) in big endian need tricky handling. 2116 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1) 2117 return false; 2118 2119 MVT RVVT = RVEVT.getSimpleVT(); 2120 if (RVVT == MVT::f128) 2121 return false; 2122 MVT DestVT = VA.getValVT(); 2123 // Special handling for extended integers. 2124 if (RVVT != DestVT) { 2125 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) 2126 return false; 2127 2128 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt()) 2129 return false; 2130 2131 bool isZExt = Outs[0].Flags.isZExt(); 2132 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt); 2133 if (SrcReg == 0) 2134 return false; 2135 } 2136 2137 // Make the copy. 2138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2139 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg); 2140 2141 // Add register to return instruction. 2142 RetRegs.push_back(VA.getLocReg()); 2143 } 2144 2145 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2146 TII.get(AArch64::RET_ReallyLR)); 2147 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) 2148 MIB.addReg(RetRegs[i], RegState::Implicit); 2149 return true; 2150 } 2151 2152 bool AArch64FastISel::SelectTrunc(const Instruction *I) { 2153 Type *DestTy = I->getType(); 2154 Value *Op = I->getOperand(0); 2155 Type *SrcTy = Op->getType(); 2156 2157 EVT SrcEVT = TLI.getValueType(SrcTy, true); 2158 EVT DestEVT = TLI.getValueType(DestTy, true); 2159 if (!SrcEVT.isSimple()) 2160 return false; 2161 if (!DestEVT.isSimple()) 2162 return false; 2163 2164 MVT SrcVT = SrcEVT.getSimpleVT(); 2165 MVT DestVT = DestEVT.getSimpleVT(); 2166 2167 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 && 2168 SrcVT != MVT::i8) 2169 return false; 2170 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 && 2171 DestVT != MVT::i1) 2172 return false; 2173 2174 unsigned SrcReg = getRegForValue(Op); 2175 if (!SrcReg) 2176 return false; 2177 2178 // If we're truncating from i64 to a smaller non-legal type then generate an 2179 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't 2180 // generate any code. 2181 if (SrcVT == MVT::i64) { 2182 uint64_t Mask = 0; 2183 switch (DestVT.SimpleTy) { 2184 default: 2185 // Trunc i64 to i32 is handled by the target-independent fast-isel. 2186 return false; 2187 case MVT::i1: 2188 Mask = 0x1; 2189 break; 2190 case MVT::i8: 2191 Mask = 0xff; 2192 break; 2193 case MVT::i16: 2194 Mask = 0xffff; 2195 break; 2196 } 2197 // Issue an extract_subreg to get the lower 32-bits. 2198 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true, 2199 AArch64::sub_32); 2200 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass); 2201 // Create the AND instruction which performs the actual truncation. 2202 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass); 2203 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri), 2204 ANDReg) 2205 .addReg(Reg32) 2206 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32)); 2207 SrcReg = ANDReg; 2208 } 2209 2210 UpdateValueMap(I, SrcReg); 2211 return true; 2212 } 2213 2214 unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) { 2215 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 || 2216 DestVT == MVT::i64) && 2217 "Unexpected value type."); 2218 // Handle i8 and i16 as i32. 2219 if (DestVT == MVT::i8 || DestVT == MVT::i16) 2220 DestVT = MVT::i32; 2221 2222 if (isZExt) { 2223 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass); 2224 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass); 2225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri), 2226 ResultReg) 2227 .addReg(SrcReg) 2228 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32)); 2229 2230 if (DestVT == MVT::i64) { 2231 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the 2232 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd. 2233 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass); 2234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2235 TII.get(AArch64::SUBREG_TO_REG), Reg64) 2236 .addImm(0) 2237 .addReg(ResultReg) 2238 .addImm(AArch64::sub_32); 2239 ResultReg = Reg64; 2240 } 2241 return ResultReg; 2242 } else { 2243 if (DestVT == MVT::i64) { 2244 // FIXME: We're SExt i1 to i64. 2245 return 0; 2246 } 2247 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass); 2248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri), 2249 ResultReg) 2250 .addReg(SrcReg) 2251 .addImm(0) 2252 .addImm(0); 2253 return ResultReg; 2254 } 2255 } 2256 2257 unsigned AArch64FastISel::Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 2258 unsigned Op1, bool Op1IsKill) { 2259 unsigned Opc, ZReg; 2260 switch (RetVT.SimpleTy) { 2261 default: return 0; 2262 case MVT::i8: 2263 case MVT::i16: 2264 case MVT::i32: 2265 RetVT = MVT::i32; 2266 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break; 2267 case MVT::i64: 2268 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break; 2269 } 2270 2271 // Create the base instruction, then add the operands. 2272 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 2273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 2274 .addReg(Op0, getKillRegState(Op0IsKill)) 2275 .addReg(Op1, getKillRegState(Op1IsKill)) 2276 .addReg(ZReg, getKillRegState(true)); 2277 2278 return ResultReg; 2279 } 2280 2281 unsigned AArch64FastISel::Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 2282 unsigned Op1, bool Op1IsKill) { 2283 if (RetVT != MVT::i64) 2284 return 0; 2285 2286 // Create the base instruction, then add the operands. 2287 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass); 2288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SMADDLrrr), 2289 ResultReg) 2290 .addReg(Op0, getKillRegState(Op0IsKill)) 2291 .addReg(Op1, getKillRegState(Op1IsKill)) 2292 .addReg(AArch64::XZR, getKillRegState(true)); 2293 2294 return ResultReg; 2295 } 2296 2297 unsigned AArch64FastISel::Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill, 2298 unsigned Op1, bool Op1IsKill) { 2299 if (RetVT != MVT::i64) 2300 return 0; 2301 2302 // Create the base instruction, then add the operands. 2303 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass); 2304 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::UMADDLrrr), 2305 ResultReg) 2306 .addReg(Op0, getKillRegState(Op0IsKill)) 2307 .addReg(Op1, getKillRegState(Op1IsKill)) 2308 .addReg(AArch64::XZR, getKillRegState(true)); 2309 2310 return ResultReg; 2311 } 2312 2313 unsigned AArch64FastISel::Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, 2314 uint64_t Shift) { 2315 unsigned Opc, ImmR, ImmS; 2316 switch (RetVT.SimpleTy) { 2317 default: return 0; 2318 case MVT::i8: 2319 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 7 - Shift; break; 2320 case MVT::i16: 2321 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 15 - Shift; break; 2322 case MVT::i32: 2323 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 31 - Shift; break; 2324 case MVT::i64: 2325 Opc = AArch64::UBFMXri; ImmR = -Shift % 64; ImmS = 63 - Shift; break; 2326 } 2327 2328 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy); 2329 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, ImmR, 2330 ImmS); 2331 } 2332 2333 unsigned AArch64FastISel::Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, 2334 uint64_t Shift) { 2335 unsigned Opc, ImmS; 2336 switch (RetVT.SimpleTy) { 2337 default: return 0; 2338 case MVT::i8: Opc = AArch64::UBFMWri; ImmS = 7; break; 2339 case MVT::i16: Opc = AArch64::UBFMWri; ImmS = 15; break; 2340 case MVT::i32: Opc = AArch64::UBFMWri; ImmS = 31; break; 2341 case MVT::i64: Opc = AArch64::UBFMXri; ImmS = 63; break; 2342 } 2343 2344 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy); 2345 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift, 2346 ImmS); 2347 } 2348 2349 unsigned AArch64FastISel::Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, 2350 uint64_t Shift) { 2351 unsigned Opc, ImmS; 2352 switch (RetVT.SimpleTy) { 2353 default: return 0; 2354 case MVT::i8: Opc = AArch64::SBFMWri; ImmS = 7; break; 2355 case MVT::i16: Opc = AArch64::SBFMWri; ImmS = 15; break; 2356 case MVT::i32: Opc = AArch64::SBFMWri; ImmS = 31; break; 2357 case MVT::i64: Opc = AArch64::SBFMXri; ImmS = 63; break; 2358 } 2359 2360 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy); 2361 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift, 2362 ImmS); 2363 } 2364 2365 unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 2366 bool isZExt) { 2367 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?"); 2368 2369 // FastISel does not have plumbing to deal with extensions where the SrcVT or 2370 // DestVT are odd things, so test to make sure that they are both types we can 2371 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise 2372 // bail out to SelectionDAG. 2373 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && 2374 (DestVT != MVT::i32) && (DestVT != MVT::i64)) || 2375 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && 2376 (SrcVT != MVT::i16) && (SrcVT != MVT::i32))) 2377 return 0; 2378 2379 unsigned Opc; 2380 unsigned Imm = 0; 2381 2382 switch (SrcVT.SimpleTy) { 2383 default: 2384 return 0; 2385 case MVT::i1: 2386 return Emiti1Ext(SrcReg, DestVT, isZExt); 2387 case MVT::i8: 2388 if (DestVT == MVT::i64) 2389 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri; 2390 else 2391 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri; 2392 Imm = 7; 2393 break; 2394 case MVT::i16: 2395 if (DestVT == MVT::i64) 2396 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri; 2397 else 2398 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri; 2399 Imm = 15; 2400 break; 2401 case MVT::i32: 2402 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?"); 2403 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri; 2404 Imm = 31; 2405 break; 2406 } 2407 2408 // Handle i8 and i16 as i32. 2409 if (DestVT == MVT::i8 || DestVT == MVT::i16) 2410 DestVT = MVT::i32; 2411 else if (DestVT == MVT::i64) { 2412 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass); 2413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2414 TII.get(AArch64::SUBREG_TO_REG), Src64) 2415 .addImm(0) 2416 .addReg(SrcReg) 2417 .addImm(AArch64::sub_32); 2418 SrcReg = Src64; 2419 } 2420 2421 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT)); 2422 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 2423 .addReg(SrcReg) 2424 .addImm(0) 2425 .addImm(Imm); 2426 2427 return ResultReg; 2428 } 2429 2430 bool AArch64FastISel::SelectIntExt(const Instruction *I) { 2431 // On ARM, in general, integer casts don't involve legal types; this code 2432 // handles promotable integers. The high bits for a type smaller than 2433 // the register size are assumed to be undefined. 2434 Type *DestTy = I->getType(); 2435 Value *Src = I->getOperand(0); 2436 Type *SrcTy = Src->getType(); 2437 2438 bool isZExt = isa<ZExtInst>(I); 2439 unsigned SrcReg = getRegForValue(Src); 2440 if (!SrcReg) 2441 return false; 2442 2443 EVT SrcEVT = TLI.getValueType(SrcTy, true); 2444 EVT DestEVT = TLI.getValueType(DestTy, true); 2445 if (!SrcEVT.isSimple()) 2446 return false; 2447 if (!DestEVT.isSimple()) 2448 return false; 2449 2450 MVT SrcVT = SrcEVT.getSimpleVT(); 2451 MVT DestVT = DestEVT.getSimpleVT(); 2452 unsigned ResultReg = 0; 2453 2454 // Check if it is an argument and if it is already zero/sign-extended. 2455 if (const auto *Arg = dyn_cast<Argument>(Src)) { 2456 if ((isZExt && Arg->hasZExtAttr()) || (!isZExt && Arg->hasSExtAttr())) { 2457 if (DestVT == MVT::i64) { 2458 ResultReg = createResultReg(TLI.getRegClassFor(DestVT)); 2459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2460 TII.get(AArch64::SUBREG_TO_REG), ResultReg) 2461 .addImm(0) 2462 .addReg(SrcReg) 2463 .addImm(AArch64::sub_32); 2464 } else 2465 ResultReg = SrcReg; 2466 } 2467 } 2468 2469 if (!ResultReg) 2470 ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt); 2471 2472 if (!ResultReg) 2473 return false; 2474 2475 UpdateValueMap(I, ResultReg); 2476 return true; 2477 } 2478 2479 bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) { 2480 EVT DestEVT = TLI.getValueType(I->getType(), true); 2481 if (!DestEVT.isSimple()) 2482 return false; 2483 2484 MVT DestVT = DestEVT.getSimpleVT(); 2485 if (DestVT != MVT::i64 && DestVT != MVT::i32) 2486 return false; 2487 2488 unsigned DivOpc; 2489 bool is64bit = (DestVT == MVT::i64); 2490 switch (ISDOpcode) { 2491 default: 2492 return false; 2493 case ISD::SREM: 2494 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr; 2495 break; 2496 case ISD::UREM: 2497 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr; 2498 break; 2499 } 2500 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr; 2501 unsigned Src0Reg = getRegForValue(I->getOperand(0)); 2502 if (!Src0Reg) 2503 return false; 2504 2505 unsigned Src1Reg = getRegForValue(I->getOperand(1)); 2506 if (!Src1Reg) 2507 return false; 2508 2509 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT)); 2510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg) 2511 .addReg(Src0Reg) 2512 .addReg(Src1Reg); 2513 // The remainder is computed as numerator - (quotient * denominator) using the 2514 // MSUB instruction. 2515 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT)); 2516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg) 2517 .addReg(QuotReg) 2518 .addReg(Src1Reg) 2519 .addReg(Src0Reg); 2520 UpdateValueMap(I, ResultReg); 2521 return true; 2522 } 2523 2524 bool AArch64FastISel::SelectMul(const Instruction *I) { 2525 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true); 2526 if (!SrcEVT.isSimple()) 2527 return false; 2528 MVT SrcVT = SrcEVT.getSimpleVT(); 2529 2530 // Must be simple value type. Don't handle vectors. 2531 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 && 2532 SrcVT != MVT::i8) 2533 return false; 2534 2535 unsigned Src0Reg = getRegForValue(I->getOperand(0)); 2536 if (!Src0Reg) 2537 return false; 2538 bool Src0IsKill = hasTrivialKill(I->getOperand(0)); 2539 2540 unsigned Src1Reg = getRegForValue(I->getOperand(1)); 2541 if (!Src1Reg) 2542 return false; 2543 bool Src1IsKill = hasTrivialKill(I->getOperand(1)); 2544 2545 unsigned ResultReg = 2546 Emit_MUL_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill); 2547 2548 if (!ResultReg) 2549 return false; 2550 2551 UpdateValueMap(I, ResultReg); 2552 return true; 2553 } 2554 2555 bool AArch64FastISel::SelectShift(const Instruction *I, bool IsLeftShift, 2556 bool IsArithmetic) { 2557 EVT RetEVT = TLI.getValueType(I->getType(), true); 2558 if (!RetEVT.isSimple()) 2559 return false; 2560 MVT RetVT = RetEVT.getSimpleVT(); 2561 2562 if (!isa<ConstantInt>(I->getOperand(1))) 2563 return false; 2564 2565 unsigned Op0Reg = getRegForValue(I->getOperand(0)); 2566 if (!Op0Reg) 2567 return false; 2568 bool Op0IsKill = hasTrivialKill(I->getOperand(0)); 2569 2570 uint64_t ShiftVal = cast<ConstantInt>(I->getOperand(1))->getZExtValue(); 2571 2572 unsigned ResultReg; 2573 if (IsLeftShift) 2574 ResultReg = Emit_LSL_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal); 2575 else { 2576 if (IsArithmetic) 2577 ResultReg = Emit_ASR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal); 2578 else 2579 ResultReg = Emit_LSR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal); 2580 } 2581 2582 if (!ResultReg) 2583 return false; 2584 2585 UpdateValueMap(I, ResultReg); 2586 return true; 2587 } 2588 2589 bool AArch64FastISel::SelectBitCast(const Instruction *I) { 2590 MVT RetVT, SrcVT; 2591 2592 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT)) 2593 return false; 2594 if (!isTypeLegal(I->getType(), RetVT)) 2595 return false; 2596 2597 unsigned Opc; 2598 if (RetVT == MVT::f32 && SrcVT == MVT::i32) 2599 Opc = AArch64::FMOVWSr; 2600 else if (RetVT == MVT::f64 && SrcVT == MVT::i64) 2601 Opc = AArch64::FMOVXDr; 2602 else if (RetVT == MVT::i32 && SrcVT == MVT::f32) 2603 Opc = AArch64::FMOVSWr; 2604 else if (RetVT == MVT::i64 && SrcVT == MVT::f64) 2605 Opc = AArch64::FMOVDXr; 2606 else 2607 return false; 2608 2609 unsigned Op0Reg = getRegForValue(I->getOperand(0)); 2610 if (!Op0Reg) 2611 return false; 2612 bool Op0IsKill = hasTrivialKill(I->getOperand(0)); 2613 unsigned ResultReg = FastEmitInst_r(Opc, TLI.getRegClassFor(RetVT), 2614 Op0Reg, Op0IsKill); 2615 2616 if (!ResultReg) 2617 return false; 2618 2619 UpdateValueMap(I, ResultReg); 2620 return true; 2621 } 2622 2623 bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) { 2624 switch (I->getOpcode()) { 2625 default: 2626 break; 2627 case Instruction::Load: 2628 return SelectLoad(I); 2629 case Instruction::Store: 2630 return SelectStore(I); 2631 case Instruction::Br: 2632 return SelectBranch(I); 2633 case Instruction::IndirectBr: 2634 return SelectIndirectBr(I); 2635 case Instruction::FCmp: 2636 case Instruction::ICmp: 2637 return SelectCmp(I); 2638 case Instruction::Select: 2639 return SelectSelect(I); 2640 case Instruction::FPExt: 2641 return SelectFPExt(I); 2642 case Instruction::FPTrunc: 2643 return SelectFPTrunc(I); 2644 case Instruction::FPToSI: 2645 return SelectFPToInt(I, /*Signed=*/true); 2646 case Instruction::FPToUI: 2647 return SelectFPToInt(I, /*Signed=*/false); 2648 case Instruction::SIToFP: 2649 return SelectIntToFP(I, /*Signed=*/true); 2650 case Instruction::UIToFP: 2651 return SelectIntToFP(I, /*Signed=*/false); 2652 case Instruction::SRem: 2653 return SelectRem(I, ISD::SREM); 2654 case Instruction::URem: 2655 return SelectRem(I, ISD::UREM); 2656 case Instruction::Ret: 2657 return SelectRet(I); 2658 case Instruction::Trunc: 2659 return SelectTrunc(I); 2660 case Instruction::ZExt: 2661 case Instruction::SExt: 2662 return SelectIntExt(I); 2663 2664 // FIXME: All of these should really be handled by the target-independent 2665 // selector -> improve FastISel tblgen. 2666 case Instruction::Mul: 2667 return SelectMul(I); 2668 case Instruction::Shl: 2669 return SelectShift(I, /*IsLeftShift=*/true, /*IsArithmetic=*/false); 2670 case Instruction::LShr: 2671 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/false); 2672 case Instruction::AShr: 2673 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/true); 2674 case Instruction::BitCast: 2675 return SelectBitCast(I); 2676 } 2677 return false; 2678 // Silence warnings. 2679 (void)&CC_AArch64_DarwinPCS_VarArg; 2680 } 2681 2682 namespace llvm { 2683 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo, 2684 const TargetLibraryInfo *libInfo) { 2685 return new AArch64FastISel(funcInfo, libInfo); 2686 } 2687 } 2688