1 //===- FastISel.cpp - Implementation of the FastISel class ----------------===// 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 contains the implementation of the FastISel class. 10 // 11 // "Fast" instruction selection is designed to emit very poor code quickly. 12 // Also, it is not designed to be able to do much lowering, so most illegal 13 // types (e.g. i64 on 32-bit targets) and operations are not supported. It is 14 // also not intended to be able to do much optimization, except in a few cases 15 // where doing optimizations reduces overall compile time. For example, folding 16 // constants into immediate fields is often done, because it's cheap and it 17 // reduces the number of instructions later phases have to examine. 18 // 19 // "Fast" instruction selection is able to fail gracefully and transfer 20 // control to the SelectionDAG selector for operations that it doesn't 21 // support. In many cases, this allows us to avoid duplicating a lot of 22 // the complicated lowering logic that SelectionDAG currently has. 23 // 24 // The intended use for "fast" instruction selection is "-O0" mode 25 // compilation, where the quality of the generated code is irrelevant when 26 // weighed against the speed at which the code can be generated. Also, 27 // at -O0, the LLVM optimizers are not running, and this makes the 28 // compile time of codegen a much higher portion of the overall compile 29 // time. Despite its limitations, "fast" instruction selection is able to 30 // handle enough code on its own to provide noticeable overall speedups 31 // in -O0 compiles. 32 // 33 // Basic operations are supported in a target-independent way, by reading 34 // the same instruction descriptions that the SelectionDAG selector reads, 35 // and identifying simple arithmetic operations that can be directly selected 36 // from simple operators. More complicated operations currently require 37 // target-specific code. 38 // 39 //===----------------------------------------------------------------------===// 40 41 #include "llvm/CodeGen/FastISel.h" 42 #include "llvm/ADT/APFloat.h" 43 #include "llvm/ADT/APSInt.h" 44 #include "llvm/ADT/DenseMap.h" 45 #include "llvm/ADT/Optional.h" 46 #include "llvm/ADT/SmallPtrSet.h" 47 #include "llvm/ADT/SmallString.h" 48 #include "llvm/ADT/SmallVector.h" 49 #include "llvm/ADT/Statistic.h" 50 #include "llvm/Analysis/BranchProbabilityInfo.h" 51 #include "llvm/Analysis/TargetLibraryInfo.h" 52 #include "llvm/CodeGen/Analysis.h" 53 #include "llvm/CodeGen/FunctionLoweringInfo.h" 54 #include "llvm/CodeGen/ISDOpcodes.h" 55 #include "llvm/CodeGen/MachineBasicBlock.h" 56 #include "llvm/CodeGen/MachineFrameInfo.h" 57 #include "llvm/CodeGen/MachineInstr.h" 58 #include "llvm/CodeGen/MachineInstrBuilder.h" 59 #include "llvm/CodeGen/MachineMemOperand.h" 60 #include "llvm/CodeGen/MachineModuleInfo.h" 61 #include "llvm/CodeGen/MachineOperand.h" 62 #include "llvm/CodeGen/MachineRegisterInfo.h" 63 #include "llvm/CodeGen/StackMaps.h" 64 #include "llvm/CodeGen/TargetInstrInfo.h" 65 #include "llvm/CodeGen/TargetLowering.h" 66 #include "llvm/CodeGen/TargetSubtargetInfo.h" 67 #include "llvm/CodeGen/ValueTypes.h" 68 #include "llvm/IR/Argument.h" 69 #include "llvm/IR/Attributes.h" 70 #include "llvm/IR/BasicBlock.h" 71 #include "llvm/IR/CallingConv.h" 72 #include "llvm/IR/Constant.h" 73 #include "llvm/IR/Constants.h" 74 #include "llvm/IR/DataLayout.h" 75 #include "llvm/IR/DebugInfo.h" 76 #include "llvm/IR/DebugLoc.h" 77 #include "llvm/IR/DerivedTypes.h" 78 #include "llvm/IR/DiagnosticInfo.h" 79 #include "llvm/IR/Function.h" 80 #include "llvm/IR/GetElementPtrTypeIterator.h" 81 #include "llvm/IR/GlobalValue.h" 82 #include "llvm/IR/InlineAsm.h" 83 #include "llvm/IR/InstrTypes.h" 84 #include "llvm/IR/Instruction.h" 85 #include "llvm/IR/Instructions.h" 86 #include "llvm/IR/IntrinsicInst.h" 87 #include "llvm/IR/LLVMContext.h" 88 #include "llvm/IR/Mangler.h" 89 #include "llvm/IR/Metadata.h" 90 #include "llvm/IR/Operator.h" 91 #include "llvm/IR/PatternMatch.h" 92 #include "llvm/IR/Type.h" 93 #include "llvm/IR/User.h" 94 #include "llvm/IR/Value.h" 95 #include "llvm/MC/MCContext.h" 96 #include "llvm/MC/MCInstrDesc.h" 97 #include "llvm/MC/MCRegisterInfo.h" 98 #include "llvm/Support/Casting.h" 99 #include "llvm/Support/Debug.h" 100 #include "llvm/Support/ErrorHandling.h" 101 #include "llvm/Support/MachineValueType.h" 102 #include "llvm/Support/MathExtras.h" 103 #include "llvm/Support/raw_ostream.h" 104 #include "llvm/Target/TargetMachine.h" 105 #include "llvm/Target/TargetOptions.h" 106 #include <algorithm> 107 #include <cassert> 108 #include <cstdint> 109 #include <iterator> 110 #include <utility> 111 112 using namespace llvm; 113 using namespace PatternMatch; 114 115 #define DEBUG_TYPE "isel" 116 117 STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by " 118 "target-independent selector"); 119 STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by " 120 "target-specific selector"); 121 STATISTIC(NumFastIselDead, "Number of dead insts removed on failure"); 122 123 /// Set the current block to which generated machine instructions will be 124 /// appended. 125 void FastISel::startNewBlock() { 126 assert(LocalValueMap.empty() && 127 "local values should be cleared after finishing a BB"); 128 129 // Instructions are appended to FuncInfo.MBB. If the basic block already 130 // contains labels or copies, use the last instruction as the last local 131 // value. 132 EmitStartPt = nullptr; 133 if (!FuncInfo.MBB->empty()) 134 EmitStartPt = &FuncInfo.MBB->back(); 135 LastLocalValue = EmitStartPt; 136 } 137 138 void FastISel::finishBasicBlock() { flushLocalValueMap(); } 139 140 bool FastISel::lowerArguments() { 141 if (!FuncInfo.CanLowerReturn) 142 // Fallback to SDISel argument lowering code to deal with sret pointer 143 // parameter. 144 return false; 145 146 if (!fastLowerArguments()) 147 return false; 148 149 // Enter arguments into ValueMap for uses in non-entry BBs. 150 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(), 151 E = FuncInfo.Fn->arg_end(); 152 I != E; ++I) { 153 DenseMap<const Value *, Register>::iterator VI = LocalValueMap.find(&*I); 154 assert(VI != LocalValueMap.end() && "Missed an argument?"); 155 FuncInfo.ValueMap[&*I] = VI->second; 156 } 157 return true; 158 } 159 160 /// Return the defined register if this instruction defines exactly one 161 /// virtual register and uses no other virtual registers. Otherwise return 0. 162 static Register findLocalRegDef(MachineInstr &MI) { 163 Register RegDef; 164 for (const MachineOperand &MO : MI.operands()) { 165 if (!MO.isReg()) 166 continue; 167 if (MO.isDef()) { 168 if (RegDef) 169 return Register(); 170 RegDef = MO.getReg(); 171 } else if (MO.getReg().isVirtual()) { 172 // This is another use of a vreg. Don't delete it. 173 return Register(); 174 } 175 } 176 return RegDef; 177 } 178 179 static bool isRegUsedByPhiNodes(Register DefReg, 180 FunctionLoweringInfo &FuncInfo) { 181 for (auto &P : FuncInfo.PHINodesToUpdate) 182 if (P.second == DefReg) 183 return true; 184 return false; 185 } 186 187 void FastISel::flushLocalValueMap() { 188 // If FastISel bails out, it could leave local value instructions behind 189 // that aren't used for anything. Detect and erase those. 190 if (LastLocalValue != EmitStartPt) { 191 // Save the first instruction after local values, for later. 192 MachineBasicBlock::iterator FirstNonValue(LastLocalValue); 193 ++FirstNonValue; 194 195 MachineBasicBlock::reverse_iterator RE = 196 EmitStartPt ? MachineBasicBlock::reverse_iterator(EmitStartPt) 197 : FuncInfo.MBB->rend(); 198 MachineBasicBlock::reverse_iterator RI(LastLocalValue); 199 for (MachineInstr &LocalMI : 200 llvm::make_early_inc_range(llvm::make_range(RI, RE))) { 201 Register DefReg = findLocalRegDef(LocalMI); 202 if (!DefReg) 203 continue; 204 if (FuncInfo.RegsWithFixups.count(DefReg)) 205 continue; 206 bool UsedByPHI = isRegUsedByPhiNodes(DefReg, FuncInfo); 207 if (!UsedByPHI && MRI.use_nodbg_empty(DefReg)) { 208 if (EmitStartPt == &LocalMI) 209 EmitStartPt = EmitStartPt->getPrevNode(); 210 LLVM_DEBUG(dbgs() << "removing dead local value materialization" 211 << LocalMI); 212 LocalMI.eraseFromParent(); 213 } 214 } 215 216 if (FirstNonValue != FuncInfo.MBB->end()) { 217 // See if there are any local value instructions left. If so, we want to 218 // make sure the first one has a debug location; if it doesn't, use the 219 // first non-value instruction's debug location. 220 221 // If EmitStartPt is non-null, this block had copies at the top before 222 // FastISel started doing anything; it points to the last one, so the 223 // first local value instruction is the one after EmitStartPt. 224 // If EmitStartPt is null, the first local value instruction is at the 225 // top of the block. 226 MachineBasicBlock::iterator FirstLocalValue = 227 EmitStartPt ? ++MachineBasicBlock::iterator(EmitStartPt) 228 : FuncInfo.MBB->begin(); 229 if (FirstLocalValue != FirstNonValue && !FirstLocalValue->getDebugLoc()) 230 FirstLocalValue->setDebugLoc(FirstNonValue->getDebugLoc()); 231 } 232 } 233 234 LocalValueMap.clear(); 235 LastLocalValue = EmitStartPt; 236 recomputeInsertPt(); 237 SavedInsertPt = FuncInfo.InsertPt; 238 } 239 240 Register FastISel::getRegForValue(const Value *V) { 241 EVT RealVT = TLI.getValueType(DL, V->getType(), /*AllowUnknown=*/true); 242 // Don't handle non-simple values in FastISel. 243 if (!RealVT.isSimple()) 244 return Register(); 245 246 // Ignore illegal types. We must do this before looking up the value 247 // in ValueMap because Arguments are given virtual registers regardless 248 // of whether FastISel can handle them. 249 MVT VT = RealVT.getSimpleVT(); 250 if (!TLI.isTypeLegal(VT)) { 251 // Handle integer promotions, though, because they're common and easy. 252 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 253 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); 254 else 255 return Register(); 256 } 257 258 // Look up the value to see if we already have a register for it. 259 Register Reg = lookUpRegForValue(V); 260 if (Reg) 261 return Reg; 262 263 // In bottom-up mode, just create the virtual register which will be used 264 // to hold the value. It will be materialized later. 265 if (isa<Instruction>(V) && 266 (!isa<AllocaInst>(V) || 267 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) 268 return FuncInfo.InitializeRegForValue(V); 269 270 SavePoint SaveInsertPt = enterLocalValueArea(); 271 272 // Materialize the value in a register. Emit any instructions in the 273 // local value area. 274 Reg = materializeRegForValue(V, VT); 275 276 leaveLocalValueArea(SaveInsertPt); 277 278 return Reg; 279 } 280 281 Register FastISel::materializeConstant(const Value *V, MVT VT) { 282 Register Reg; 283 if (const auto *CI = dyn_cast<ConstantInt>(V)) { 284 if (CI->getValue().getActiveBits() <= 64) 285 Reg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); 286 } else if (isa<AllocaInst>(V)) 287 Reg = fastMaterializeAlloca(cast<AllocaInst>(V)); 288 else if (isa<ConstantPointerNull>(V)) 289 // Translate this as an integer zero so that it can be 290 // local-CSE'd with actual integer zeros. 291 Reg = 292 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getType()))); 293 else if (const auto *CF = dyn_cast<ConstantFP>(V)) { 294 if (CF->isNullValue()) 295 Reg = fastMaterializeFloatZero(CF); 296 else 297 // Try to emit the constant directly. 298 Reg = fastEmit_f(VT, VT, ISD::ConstantFP, CF); 299 300 if (!Reg) { 301 // Try to emit the constant by using an integer constant with a cast. 302 const APFloat &Flt = CF->getValueAPF(); 303 EVT IntVT = TLI.getPointerTy(DL); 304 uint32_t IntBitWidth = IntVT.getSizeInBits(); 305 APSInt SIntVal(IntBitWidth, /*isUnsigned=*/false); 306 bool isExact; 307 (void)Flt.convertToInteger(SIntVal, APFloat::rmTowardZero, &isExact); 308 if (isExact) { 309 Register IntegerReg = 310 getRegForValue(ConstantInt::get(V->getContext(), SIntVal)); 311 if (IntegerReg) 312 Reg = fastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, 313 IntegerReg); 314 } 315 } 316 } else if (const auto *Op = dyn_cast<Operator>(V)) { 317 if (!selectOperator(Op, Op->getOpcode())) 318 if (!isa<Instruction>(Op) || 319 !fastSelectInstruction(cast<Instruction>(Op))) 320 return 0; 321 Reg = lookUpRegForValue(Op); 322 } else if (isa<UndefValue>(V)) { 323 Reg = createResultReg(TLI.getRegClassFor(VT)); 324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 325 TII.get(TargetOpcode::IMPLICIT_DEF), Reg); 326 } 327 return Reg; 328 } 329 330 /// Helper for getRegForValue. This function is called when the value isn't 331 /// already available in a register and must be materialized with new 332 /// instructions. 333 Register FastISel::materializeRegForValue(const Value *V, MVT VT) { 334 Register Reg; 335 // Give the target-specific code a try first. 336 if (isa<Constant>(V)) 337 Reg = fastMaterializeConstant(cast<Constant>(V)); 338 339 // If target-specific code couldn't or didn't want to handle the value, then 340 // give target-independent code a try. 341 if (!Reg) 342 Reg = materializeConstant(V, VT); 343 344 // Don't cache constant materializations in the general ValueMap. 345 // To do so would require tracking what uses they dominate. 346 if (Reg) { 347 LocalValueMap[V] = Reg; 348 LastLocalValue = MRI.getVRegDef(Reg); 349 } 350 return Reg; 351 } 352 353 Register FastISel::lookUpRegForValue(const Value *V) { 354 // Look up the value to see if we already have a register for it. We 355 // cache values defined by Instructions across blocks, and other values 356 // only locally. This is because Instructions already have the SSA 357 // def-dominates-use requirement enforced. 358 DenseMap<const Value *, Register>::iterator I = FuncInfo.ValueMap.find(V); 359 if (I != FuncInfo.ValueMap.end()) 360 return I->second; 361 return LocalValueMap[V]; 362 } 363 364 void FastISel::updateValueMap(const Value *I, Register Reg, unsigned NumRegs) { 365 if (!isa<Instruction>(I)) { 366 LocalValueMap[I] = Reg; 367 return; 368 } 369 370 Register &AssignedReg = FuncInfo.ValueMap[I]; 371 if (!AssignedReg) 372 // Use the new register. 373 AssignedReg = Reg; 374 else if (Reg != AssignedReg) { 375 // Arrange for uses of AssignedReg to be replaced by uses of Reg. 376 for (unsigned i = 0; i < NumRegs; i++) { 377 FuncInfo.RegFixups[AssignedReg + i] = Reg + i; 378 FuncInfo.RegsWithFixups.insert(Reg + i); 379 } 380 381 AssignedReg = Reg; 382 } 383 } 384 385 Register FastISel::getRegForGEPIndex(const Value *Idx) { 386 Register IdxN = getRegForValue(Idx); 387 if (!IdxN) 388 // Unhandled operand. Halt "fast" selection and bail. 389 return Register(); 390 391 // If the index is smaller or larger than intptr_t, truncate or extend it. 392 MVT PtrVT = TLI.getPointerTy(DL); 393 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); 394 if (IdxVT.bitsLT(PtrVT)) { 395 IdxN = fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN); 396 } else if (IdxVT.bitsGT(PtrVT)) { 397 IdxN = 398 fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN); 399 } 400 return IdxN; 401 } 402 403 void FastISel::recomputeInsertPt() { 404 if (getLastLocalValue()) { 405 FuncInfo.InsertPt = getLastLocalValue(); 406 FuncInfo.MBB = FuncInfo.InsertPt->getParent(); 407 ++FuncInfo.InsertPt; 408 } else 409 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI(); 410 411 // Now skip past any EH_LABELs, which must remain at the beginning. 412 while (FuncInfo.InsertPt != FuncInfo.MBB->end() && 413 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL) 414 ++FuncInfo.InsertPt; 415 } 416 417 void FastISel::removeDeadCode(MachineBasicBlock::iterator I, 418 MachineBasicBlock::iterator E) { 419 assert(I.isValid() && E.isValid() && std::distance(I, E) > 0 && 420 "Invalid iterator!"); 421 while (I != E) { 422 if (SavedInsertPt == I) 423 SavedInsertPt = E; 424 if (EmitStartPt == I) 425 EmitStartPt = E.isValid() ? &*E : nullptr; 426 if (LastLocalValue == I) 427 LastLocalValue = E.isValid() ? &*E : nullptr; 428 429 MachineInstr *Dead = &*I; 430 ++I; 431 Dead->eraseFromParent(); 432 ++NumFastIselDead; 433 } 434 recomputeInsertPt(); 435 } 436 437 FastISel::SavePoint FastISel::enterLocalValueArea() { 438 SavePoint OldInsertPt = FuncInfo.InsertPt; 439 recomputeInsertPt(); 440 return OldInsertPt; 441 } 442 443 void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) { 444 if (FuncInfo.InsertPt != FuncInfo.MBB->begin()) 445 LastLocalValue = &*std::prev(FuncInfo.InsertPt); 446 447 // Restore the previous insert position. 448 FuncInfo.InsertPt = OldInsertPt; 449 } 450 451 bool FastISel::selectBinaryOp(const User *I, unsigned ISDOpcode) { 452 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); 453 if (VT == MVT::Other || !VT.isSimple()) 454 // Unhandled type. Halt "fast" selection and bail. 455 return false; 456 457 // We only handle legal types. For example, on x86-32 the instruction 458 // selector contains all of the 64-bit instructions from x86-64, 459 // under the assumption that i64 won't be used if the target doesn't 460 // support it. 461 if (!TLI.isTypeLegal(VT)) { 462 // MVT::i1 is special. Allow AND, OR, or XOR because they 463 // don't require additional zeroing, which makes them easy. 464 if (VT == MVT::i1 && (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || 465 ISDOpcode == ISD::XOR)) 466 VT = TLI.getTypeToTransformTo(I->getContext(), VT); 467 else 468 return false; 469 } 470 471 // Check if the first operand is a constant, and handle it as "ri". At -O0, 472 // we don't have anything that canonicalizes operand order. 473 if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(0))) 474 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) { 475 Register Op1 = getRegForValue(I->getOperand(1)); 476 if (!Op1) 477 return false; 478 479 Register ResultReg = 480 fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, CI->getZExtValue(), 481 VT.getSimpleVT()); 482 if (!ResultReg) 483 return false; 484 485 // We successfully emitted code for the given LLVM Instruction. 486 updateValueMap(I, ResultReg); 487 return true; 488 } 489 490 Register Op0 = getRegForValue(I->getOperand(0)); 491 if (!Op0) // Unhandled operand. Halt "fast" selection and bail. 492 return false; 493 494 // Check if the second operand is a constant and handle it appropriately. 495 if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 496 uint64_t Imm = CI->getSExtValue(); 497 498 // Transform "sdiv exact X, 8" -> "sra X, 3". 499 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) && 500 cast<BinaryOperator>(I)->isExact() && isPowerOf2_64(Imm)) { 501 Imm = Log2_64(Imm); 502 ISDOpcode = ISD::SRA; 503 } 504 505 // Transform "urem x, pow2" -> "and x, pow2-1". 506 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) && 507 isPowerOf2_64(Imm)) { 508 --Imm; 509 ISDOpcode = ISD::AND; 510 } 511 512 Register ResultReg = fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, Imm, 513 VT.getSimpleVT()); 514 if (!ResultReg) 515 return false; 516 517 // We successfully emitted code for the given LLVM Instruction. 518 updateValueMap(I, ResultReg); 519 return true; 520 } 521 522 Register Op1 = getRegForValue(I->getOperand(1)); 523 if (!Op1) // Unhandled operand. Halt "fast" selection and bail. 524 return false; 525 526 // Now we have both operands in registers. Emit the instruction. 527 Register ResultReg = fastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), 528 ISDOpcode, Op0, Op1); 529 if (!ResultReg) 530 // Target-specific code wasn't able to find a machine opcode for 531 // the given ISD opcode and type. Halt "fast" selection and bail. 532 return false; 533 534 // We successfully emitted code for the given LLVM Instruction. 535 updateValueMap(I, ResultReg); 536 return true; 537 } 538 539 bool FastISel::selectGetElementPtr(const User *I) { 540 Register N = getRegForValue(I->getOperand(0)); 541 if (!N) // Unhandled operand. Halt "fast" selection and bail. 542 return false; 543 544 // FIXME: The code below does not handle vector GEPs. Halt "fast" selection 545 // and bail. 546 if (isa<VectorType>(I->getType())) 547 return false; 548 549 // Keep a running tab of the total offset to coalesce multiple N = N + Offset 550 // into a single N = N + TotalOffset. 551 uint64_t TotalOffs = 0; 552 // FIXME: What's a good SWAG number for MaxOffs? 553 uint64_t MaxOffs = 2048; 554 MVT VT = TLI.getPointerTy(DL); 555 for (gep_type_iterator GTI = gep_type_begin(I), E = gep_type_end(I); 556 GTI != E; ++GTI) { 557 const Value *Idx = GTI.getOperand(); 558 if (StructType *StTy = GTI.getStructTypeOrNull()) { 559 uint64_t Field = cast<ConstantInt>(Idx)->getZExtValue(); 560 if (Field) { 561 // N = N + Offset 562 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field); 563 if (TotalOffs >= MaxOffs) { 564 N = fastEmit_ri_(VT, ISD::ADD, N, TotalOffs, VT); 565 if (!N) // Unhandled operand. Halt "fast" selection and bail. 566 return false; 567 TotalOffs = 0; 568 } 569 } 570 } else { 571 Type *Ty = GTI.getIndexedType(); 572 573 // If this is a constant subscript, handle it quickly. 574 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) { 575 if (CI->isZero()) 576 continue; 577 // N = N + Offset 578 uint64_t IdxN = CI->getValue().sextOrTrunc(64).getSExtValue(); 579 TotalOffs += DL.getTypeAllocSize(Ty) * IdxN; 580 if (TotalOffs >= MaxOffs) { 581 N = fastEmit_ri_(VT, ISD::ADD, N, TotalOffs, VT); 582 if (!N) // Unhandled operand. Halt "fast" selection and bail. 583 return false; 584 TotalOffs = 0; 585 } 586 continue; 587 } 588 if (TotalOffs) { 589 N = fastEmit_ri_(VT, ISD::ADD, N, TotalOffs, VT); 590 if (!N) // Unhandled operand. Halt "fast" selection and bail. 591 return false; 592 TotalOffs = 0; 593 } 594 595 // N = N + Idx * ElementSize; 596 uint64_t ElementSize = DL.getTypeAllocSize(Ty); 597 Register IdxN = getRegForGEPIndex(Idx); 598 if (!IdxN) // Unhandled operand. Halt "fast" selection and bail. 599 return false; 600 601 if (ElementSize != 1) { 602 IdxN = fastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); 603 if (!IdxN) // Unhandled operand. Halt "fast" selection and bail. 604 return false; 605 } 606 N = fastEmit_rr(VT, VT, ISD::ADD, N, IdxN); 607 if (!N) // Unhandled operand. Halt "fast" selection and bail. 608 return false; 609 } 610 } 611 if (TotalOffs) { 612 N = fastEmit_ri_(VT, ISD::ADD, N, TotalOffs, VT); 613 if (!N) // Unhandled operand. Halt "fast" selection and bail. 614 return false; 615 } 616 617 // We successfully emitted code for the given LLVM Instruction. 618 updateValueMap(I, N); 619 return true; 620 } 621 622 bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops, 623 const CallInst *CI, unsigned StartIdx) { 624 for (unsigned i = StartIdx, e = CI->arg_size(); i != e; ++i) { 625 Value *Val = CI->getArgOperand(i); 626 // Check for constants and encode them with a StackMaps::ConstantOp prefix. 627 if (const auto *C = dyn_cast<ConstantInt>(Val)) { 628 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp)); 629 Ops.push_back(MachineOperand::CreateImm(C->getSExtValue())); 630 } else if (isa<ConstantPointerNull>(Val)) { 631 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp)); 632 Ops.push_back(MachineOperand::CreateImm(0)); 633 } else if (auto *AI = dyn_cast<AllocaInst>(Val)) { 634 // Values coming from a stack location also require a special encoding, 635 // but that is added later on by the target specific frame index 636 // elimination implementation. 637 auto SI = FuncInfo.StaticAllocaMap.find(AI); 638 if (SI != FuncInfo.StaticAllocaMap.end()) 639 Ops.push_back(MachineOperand::CreateFI(SI->second)); 640 else 641 return false; 642 } else { 643 Register Reg = getRegForValue(Val); 644 if (!Reg) 645 return false; 646 Ops.push_back(MachineOperand::CreateReg(Reg, /*isDef=*/false)); 647 } 648 } 649 return true; 650 } 651 652 bool FastISel::selectStackmap(const CallInst *I) { 653 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>, 654 // [live variables...]) 655 assert(I->getCalledFunction()->getReturnType()->isVoidTy() && 656 "Stackmap cannot return a value."); 657 658 // The stackmap intrinsic only records the live variables (the arguments 659 // passed to it) and emits NOPS (if requested). Unlike the patchpoint 660 // intrinsic, this won't be lowered to a function call. This means we don't 661 // have to worry about calling conventions and target-specific lowering code. 662 // Instead we perform the call lowering right here. 663 // 664 // CALLSEQ_START(0, 0...) 665 // STACKMAP(id, nbytes, ...) 666 // CALLSEQ_END(0, 0) 667 // 668 SmallVector<MachineOperand, 32> Ops; 669 670 // Add the <id> and <numBytes> constants. 671 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) && 672 "Expected a constant integer."); 673 const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)); 674 Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue())); 675 676 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) && 677 "Expected a constant integer."); 678 const auto *NumBytes = 679 cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)); 680 Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue())); 681 682 // Push live variables for the stack map (skipping the first two arguments 683 // <id> and <numBytes>). 684 if (!addStackMapLiveVars(Ops, I, 2)) 685 return false; 686 687 // We are not adding any register mask info here, because the stackmap doesn't 688 // clobber anything. 689 690 // Add scratch registers as implicit def and early clobber. 691 CallingConv::ID CC = I->getCallingConv(); 692 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC); 693 for (unsigned i = 0; ScratchRegs[i]; ++i) 694 Ops.push_back(MachineOperand::CreateReg( 695 ScratchRegs[i], /*isDef=*/true, /*isImp=*/true, /*isKill=*/false, 696 /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/true)); 697 698 // Issue CALLSEQ_START 699 unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); 700 auto Builder = 701 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown)); 702 const MCInstrDesc &MCID = Builder.getInstr()->getDesc(); 703 for (unsigned I = 0, E = MCID.getNumOperands(); I < E; ++I) 704 Builder.addImm(0); 705 706 // Issue STACKMAP. 707 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 708 TII.get(TargetOpcode::STACKMAP)); 709 for (auto const &MO : Ops) 710 MIB.add(MO); 711 712 // Issue CALLSEQ_END 713 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); 714 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp)) 715 .addImm(0) 716 .addImm(0); 717 718 // Inform the Frame Information that we have a stackmap in this function. 719 FuncInfo.MF->getFrameInfo().setHasStackMap(); 720 721 return true; 722 } 723 724 /// Lower an argument list according to the target calling convention. 725 /// 726 /// This is a helper for lowering intrinsics that follow a target calling 727 /// convention or require stack pointer adjustment. Only a subset of the 728 /// intrinsic's operands need to participate in the calling convention. 729 bool FastISel::lowerCallOperands(const CallInst *CI, unsigned ArgIdx, 730 unsigned NumArgs, const Value *Callee, 731 bool ForceRetVoidTy, CallLoweringInfo &CLI) { 732 ArgListTy Args; 733 Args.reserve(NumArgs); 734 735 // Populate the argument list. 736 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs; ArgI != ArgE; ++ArgI) { 737 Value *V = CI->getOperand(ArgI); 738 739 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic."); 740 741 ArgListEntry Entry; 742 Entry.Val = V; 743 Entry.Ty = V->getType(); 744 Entry.setAttributes(CI, ArgI); 745 Args.push_back(Entry); 746 } 747 748 Type *RetTy = ForceRetVoidTy ? Type::getVoidTy(CI->getType()->getContext()) 749 : CI->getType(); 750 CLI.setCallee(CI->getCallingConv(), RetTy, Callee, std::move(Args), NumArgs); 751 752 return lowerCallTo(CLI); 753 } 754 755 FastISel::CallLoweringInfo &FastISel::CallLoweringInfo::setCallee( 756 const DataLayout &DL, MCContext &Ctx, CallingConv::ID CC, Type *ResultTy, 757 StringRef Target, ArgListTy &&ArgsList, unsigned FixedArgs) { 758 SmallString<32> MangledName; 759 Mangler::getNameWithPrefix(MangledName, Target, DL); 760 MCSymbol *Sym = Ctx.getOrCreateSymbol(MangledName); 761 return setCallee(CC, ResultTy, Sym, std::move(ArgsList), FixedArgs); 762 } 763 764 bool FastISel::selectPatchpoint(const CallInst *I) { 765 // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>, 766 // i32 <numBytes>, 767 // i8* <target>, 768 // i32 <numArgs>, 769 // [Args...], 770 // [live variables...]) 771 CallingConv::ID CC = I->getCallingConv(); 772 bool IsAnyRegCC = CC == CallingConv::AnyReg; 773 bool HasDef = !I->getType()->isVoidTy(); 774 Value *Callee = I->getOperand(PatchPointOpers::TargetPos)->stripPointerCasts(); 775 776 // Get the real number of arguments participating in the call <numArgs> 777 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)) && 778 "Expected a constant integer."); 779 const auto *NumArgsVal = 780 cast<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)); 781 unsigned NumArgs = NumArgsVal->getZExtValue(); 782 783 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs> 784 // This includes all meta-operands up to but not including CC. 785 unsigned NumMetaOpers = PatchPointOpers::CCPos; 786 assert(I->arg_size() >= NumMetaOpers + NumArgs && 787 "Not enough arguments provided to the patchpoint intrinsic"); 788 789 // For AnyRegCC the arguments are lowered later on manually. 790 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs; 791 CallLoweringInfo CLI; 792 CLI.setIsPatchPoint(); 793 if (!lowerCallOperands(I, NumMetaOpers, NumCallArgs, Callee, IsAnyRegCC, CLI)) 794 return false; 795 796 assert(CLI.Call && "No call instruction specified."); 797 798 SmallVector<MachineOperand, 32> Ops; 799 800 // Add an explicit result reg if we use the anyreg calling convention. 801 if (IsAnyRegCC && HasDef) { 802 assert(CLI.NumResultRegs == 0 && "Unexpected result register."); 803 CLI.ResultReg = createResultReg(TLI.getRegClassFor(MVT::i64)); 804 CLI.NumResultRegs = 1; 805 Ops.push_back(MachineOperand::CreateReg(CLI.ResultReg, /*isDef=*/true)); 806 } 807 808 // Add the <id> and <numBytes> constants. 809 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) && 810 "Expected a constant integer."); 811 const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)); 812 Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue())); 813 814 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) && 815 "Expected a constant integer."); 816 const auto *NumBytes = 817 cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)); 818 Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue())); 819 820 // Add the call target. 821 if (const auto *C = dyn_cast<IntToPtrInst>(Callee)) { 822 uint64_t CalleeConstAddr = 823 cast<ConstantInt>(C->getOperand(0))->getZExtValue(); 824 Ops.push_back(MachineOperand::CreateImm(CalleeConstAddr)); 825 } else if (const auto *C = dyn_cast<ConstantExpr>(Callee)) { 826 if (C->getOpcode() == Instruction::IntToPtr) { 827 uint64_t CalleeConstAddr = 828 cast<ConstantInt>(C->getOperand(0))->getZExtValue(); 829 Ops.push_back(MachineOperand::CreateImm(CalleeConstAddr)); 830 } else 831 llvm_unreachable("Unsupported ConstantExpr."); 832 } else if (const auto *GV = dyn_cast<GlobalValue>(Callee)) { 833 Ops.push_back(MachineOperand::CreateGA(GV, 0)); 834 } else if (isa<ConstantPointerNull>(Callee)) 835 Ops.push_back(MachineOperand::CreateImm(0)); 836 else 837 llvm_unreachable("Unsupported callee address."); 838 839 // Adjust <numArgs> to account for any arguments that have been passed on 840 // the stack instead. 841 unsigned NumCallRegArgs = IsAnyRegCC ? NumArgs : CLI.OutRegs.size(); 842 Ops.push_back(MachineOperand::CreateImm(NumCallRegArgs)); 843 844 // Add the calling convention 845 Ops.push_back(MachineOperand::CreateImm((unsigned)CC)); 846 847 // Add the arguments we omitted previously. The register allocator should 848 // place these in any free register. 849 if (IsAnyRegCC) { 850 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) { 851 Register Reg = getRegForValue(I->getArgOperand(i)); 852 if (!Reg) 853 return false; 854 Ops.push_back(MachineOperand::CreateReg(Reg, /*isDef=*/false)); 855 } 856 } 857 858 // Push the arguments from the call instruction. 859 for (auto Reg : CLI.OutRegs) 860 Ops.push_back(MachineOperand::CreateReg(Reg, /*isDef=*/false)); 861 862 // Push live variables for the stack map. 863 if (!addStackMapLiveVars(Ops, I, NumMetaOpers + NumArgs)) 864 return false; 865 866 // Push the register mask info. 867 Ops.push_back(MachineOperand::CreateRegMask( 868 TRI.getCallPreservedMask(*FuncInfo.MF, CC))); 869 870 // Add scratch registers as implicit def and early clobber. 871 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC); 872 for (unsigned i = 0; ScratchRegs[i]; ++i) 873 Ops.push_back(MachineOperand::CreateReg( 874 ScratchRegs[i], /*isDef=*/true, /*isImp=*/true, /*isKill=*/false, 875 /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/true)); 876 877 // Add implicit defs (return values). 878 for (auto Reg : CLI.InRegs) 879 Ops.push_back(MachineOperand::CreateReg(Reg, /*isDef=*/true, 880 /*isImp=*/true)); 881 882 // Insert the patchpoint instruction before the call generated by the target. 883 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, CLI.Call, DbgLoc, 884 TII.get(TargetOpcode::PATCHPOINT)); 885 886 for (auto &MO : Ops) 887 MIB.add(MO); 888 889 MIB->setPhysRegsDeadExcept(CLI.InRegs, TRI); 890 891 // Delete the original call instruction. 892 CLI.Call->eraseFromParent(); 893 894 // Inform the Frame Information that we have a patchpoint in this function. 895 FuncInfo.MF->getFrameInfo().setHasPatchPoint(); 896 897 if (CLI.NumResultRegs) 898 updateValueMap(I, CLI.ResultReg, CLI.NumResultRegs); 899 return true; 900 } 901 902 bool FastISel::selectXRayCustomEvent(const CallInst *I) { 903 const auto &Triple = TM.getTargetTriple(); 904 if (Triple.getArch() != Triple::x86_64 || !Triple.isOSLinux()) 905 return true; // don't do anything to this instruction. 906 SmallVector<MachineOperand, 8> Ops; 907 Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(0)), 908 /*isDef=*/false)); 909 Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(1)), 910 /*isDef=*/false)); 911 MachineInstrBuilder MIB = 912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 913 TII.get(TargetOpcode::PATCHABLE_EVENT_CALL)); 914 for (auto &MO : Ops) 915 MIB.add(MO); 916 917 // Insert the Patchable Event Call instruction, that gets lowered properly. 918 return true; 919 } 920 921 bool FastISel::selectXRayTypedEvent(const CallInst *I) { 922 const auto &Triple = TM.getTargetTriple(); 923 if (Triple.getArch() != Triple::x86_64 || !Triple.isOSLinux()) 924 return true; // don't do anything to this instruction. 925 SmallVector<MachineOperand, 8> Ops; 926 Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(0)), 927 /*isDef=*/false)); 928 Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(1)), 929 /*isDef=*/false)); 930 Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(2)), 931 /*isDef=*/false)); 932 MachineInstrBuilder MIB = 933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 934 TII.get(TargetOpcode::PATCHABLE_TYPED_EVENT_CALL)); 935 for (auto &MO : Ops) 936 MIB.add(MO); 937 938 // Insert the Patchable Typed Event Call instruction, that gets lowered properly. 939 return true; 940 } 941 942 /// Returns an AttributeList representing the attributes applied to the return 943 /// value of the given call. 944 static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI) { 945 SmallVector<Attribute::AttrKind, 2> Attrs; 946 if (CLI.RetSExt) 947 Attrs.push_back(Attribute::SExt); 948 if (CLI.RetZExt) 949 Attrs.push_back(Attribute::ZExt); 950 if (CLI.IsInReg) 951 Attrs.push_back(Attribute::InReg); 952 953 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex, 954 Attrs); 955 } 956 957 bool FastISel::lowerCallTo(const CallInst *CI, const char *SymName, 958 unsigned NumArgs) { 959 MCContext &Ctx = MF->getContext(); 960 SmallString<32> MangledName; 961 Mangler::getNameWithPrefix(MangledName, SymName, DL); 962 MCSymbol *Sym = Ctx.getOrCreateSymbol(MangledName); 963 return lowerCallTo(CI, Sym, NumArgs); 964 } 965 966 bool FastISel::lowerCallTo(const CallInst *CI, MCSymbol *Symbol, 967 unsigned NumArgs) { 968 FunctionType *FTy = CI->getFunctionType(); 969 Type *RetTy = CI->getType(); 970 971 ArgListTy Args; 972 Args.reserve(NumArgs); 973 974 // Populate the argument list. 975 // Attributes for args start at offset 1, after the return attribute. 976 for (unsigned ArgI = 0; ArgI != NumArgs; ++ArgI) { 977 Value *V = CI->getOperand(ArgI); 978 979 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic."); 980 981 ArgListEntry Entry; 982 Entry.Val = V; 983 Entry.Ty = V->getType(); 984 Entry.setAttributes(CI, ArgI); 985 Args.push_back(Entry); 986 } 987 TLI.markLibCallAttributes(MF, CI->getCallingConv(), Args); 988 989 CallLoweringInfo CLI; 990 CLI.setCallee(RetTy, FTy, Symbol, std::move(Args), *CI, NumArgs); 991 992 return lowerCallTo(CLI); 993 } 994 995 bool FastISel::lowerCallTo(CallLoweringInfo &CLI) { 996 // Handle the incoming return values from the call. 997 CLI.clearIns(); 998 SmallVector<EVT, 4> RetTys; 999 ComputeValueVTs(TLI, DL, CLI.RetTy, RetTys); 1000 1001 SmallVector<ISD::OutputArg, 4> Outs; 1002 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, TLI, DL); 1003 1004 bool CanLowerReturn = TLI.CanLowerReturn( 1005 CLI.CallConv, *FuncInfo.MF, CLI.IsVarArg, Outs, CLI.RetTy->getContext()); 1006 1007 // FIXME: sret demotion isn't supported yet - bail out. 1008 if (!CanLowerReturn) 1009 return false; 1010 1011 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { 1012 EVT VT = RetTys[I]; 1013 MVT RegisterVT = TLI.getRegisterType(CLI.RetTy->getContext(), VT); 1014 unsigned NumRegs = TLI.getNumRegisters(CLI.RetTy->getContext(), VT); 1015 for (unsigned i = 0; i != NumRegs; ++i) { 1016 ISD::InputArg MyFlags; 1017 MyFlags.VT = RegisterVT; 1018 MyFlags.ArgVT = VT; 1019 MyFlags.Used = CLI.IsReturnValueUsed; 1020 if (CLI.RetSExt) 1021 MyFlags.Flags.setSExt(); 1022 if (CLI.RetZExt) 1023 MyFlags.Flags.setZExt(); 1024 if (CLI.IsInReg) 1025 MyFlags.Flags.setInReg(); 1026 CLI.Ins.push_back(MyFlags); 1027 } 1028 } 1029 1030 // Handle all of the outgoing arguments. 1031 CLI.clearOuts(); 1032 for (auto &Arg : CLI.getArgs()) { 1033 Type *FinalType = Arg.Ty; 1034 if (Arg.IsByVal) 1035 FinalType = Arg.IndirectType; 1036 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters( 1037 FinalType, CLI.CallConv, CLI.IsVarArg, DL); 1038 1039 ISD::ArgFlagsTy Flags; 1040 if (Arg.IsZExt) 1041 Flags.setZExt(); 1042 if (Arg.IsSExt) 1043 Flags.setSExt(); 1044 if (Arg.IsInReg) 1045 Flags.setInReg(); 1046 if (Arg.IsSRet) 1047 Flags.setSRet(); 1048 if (Arg.IsSwiftSelf) 1049 Flags.setSwiftSelf(); 1050 if (Arg.IsSwiftAsync) 1051 Flags.setSwiftAsync(); 1052 if (Arg.IsSwiftError) 1053 Flags.setSwiftError(); 1054 if (Arg.IsCFGuardTarget) 1055 Flags.setCFGuardTarget(); 1056 if (Arg.IsByVal) 1057 Flags.setByVal(); 1058 if (Arg.IsInAlloca) { 1059 Flags.setInAlloca(); 1060 // Set the byval flag for CCAssignFn callbacks that don't know about 1061 // inalloca. This way we can know how many bytes we should've allocated 1062 // and how many bytes a callee cleanup function will pop. If we port 1063 // inalloca to more targets, we'll have to add custom inalloca handling in 1064 // the various CC lowering callbacks. 1065 Flags.setByVal(); 1066 } 1067 if (Arg.IsPreallocated) { 1068 Flags.setPreallocated(); 1069 // Set the byval flag for CCAssignFn callbacks that don't know about 1070 // preallocated. This way we can know how many bytes we should've 1071 // allocated and how many bytes a callee cleanup function will pop. If we 1072 // port preallocated to more targets, we'll have to add custom 1073 // preallocated handling in the various CC lowering callbacks. 1074 Flags.setByVal(); 1075 } 1076 MaybeAlign MemAlign = Arg.Alignment; 1077 if (Arg.IsByVal || Arg.IsInAlloca || Arg.IsPreallocated) { 1078 unsigned FrameSize = DL.getTypeAllocSize(Arg.IndirectType); 1079 1080 // For ByVal, alignment should come from FE. BE will guess if this info 1081 // is not there, but there are cases it cannot get right. 1082 if (!MemAlign) 1083 MemAlign = Align(TLI.getByValTypeAlignment(Arg.IndirectType, DL)); 1084 Flags.setByValSize(FrameSize); 1085 } else if (!MemAlign) { 1086 MemAlign = DL.getABITypeAlign(Arg.Ty); 1087 } 1088 Flags.setMemAlign(*MemAlign); 1089 if (Arg.IsNest) 1090 Flags.setNest(); 1091 if (NeedsRegBlock) 1092 Flags.setInConsecutiveRegs(); 1093 Flags.setOrigAlign(DL.getABITypeAlign(Arg.Ty)); 1094 CLI.OutVals.push_back(Arg.Val); 1095 CLI.OutFlags.push_back(Flags); 1096 } 1097 1098 if (!fastLowerCall(CLI)) 1099 return false; 1100 1101 // Set all unused physreg defs as dead. 1102 assert(CLI.Call && "No call instruction specified."); 1103 CLI.Call->setPhysRegsDeadExcept(CLI.InRegs, TRI); 1104 1105 if (CLI.NumResultRegs && CLI.CB) 1106 updateValueMap(CLI.CB, CLI.ResultReg, CLI.NumResultRegs); 1107 1108 // Set labels for heapallocsite call. 1109 if (CLI.CB) 1110 if (MDNode *MD = CLI.CB->getMetadata("heapallocsite")) 1111 CLI.Call->setHeapAllocMarker(*MF, MD); 1112 1113 return true; 1114 } 1115 1116 bool FastISel::lowerCall(const CallInst *CI) { 1117 FunctionType *FuncTy = CI->getFunctionType(); 1118 Type *RetTy = CI->getType(); 1119 1120 ArgListTy Args; 1121 ArgListEntry Entry; 1122 Args.reserve(CI->arg_size()); 1123 1124 for (auto i = CI->arg_begin(), e = CI->arg_end(); i != e; ++i) { 1125 Value *V = *i; 1126 1127 // Skip empty types 1128 if (V->getType()->isEmptyTy()) 1129 continue; 1130 1131 Entry.Val = V; 1132 Entry.Ty = V->getType(); 1133 1134 // Skip the first return-type Attribute to get to params. 1135 Entry.setAttributes(CI, i - CI->arg_begin()); 1136 Args.push_back(Entry); 1137 } 1138 1139 // Check if target-independent constraints permit a tail call here. 1140 // Target-dependent constraints are checked within fastLowerCall. 1141 bool IsTailCall = CI->isTailCall(); 1142 if (IsTailCall && !isInTailCallPosition(*CI, TM)) 1143 IsTailCall = false; 1144 if (IsTailCall && MF->getFunction() 1145 .getFnAttribute("disable-tail-calls") 1146 .getValueAsBool()) 1147 IsTailCall = false; 1148 1149 CallLoweringInfo CLI; 1150 CLI.setCallee(RetTy, FuncTy, CI->getCalledOperand(), std::move(Args), *CI) 1151 .setTailCall(IsTailCall); 1152 1153 diagnoseDontCall(*CI); 1154 1155 return lowerCallTo(CLI); 1156 } 1157 1158 bool FastISel::selectCall(const User *I) { 1159 const CallInst *Call = cast<CallInst>(I); 1160 1161 // Handle simple inline asms. 1162 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledOperand())) { 1163 // Don't attempt to handle constraints. 1164 if (!IA->getConstraintString().empty()) 1165 return false; 1166 1167 unsigned ExtraInfo = 0; 1168 if (IA->hasSideEffects()) 1169 ExtraInfo |= InlineAsm::Extra_HasSideEffects; 1170 if (IA->isAlignStack()) 1171 ExtraInfo |= InlineAsm::Extra_IsAlignStack; 1172 if (Call->isConvergent()) 1173 ExtraInfo |= InlineAsm::Extra_IsConvergent; 1174 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect; 1175 1176 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1177 TII.get(TargetOpcode::INLINEASM)); 1178 MIB.addExternalSymbol(IA->getAsmString().c_str()); 1179 MIB.addImm(ExtraInfo); 1180 1181 const MDNode *SrcLoc = Call->getMetadata("srcloc"); 1182 if (SrcLoc) 1183 MIB.addMetadata(SrcLoc); 1184 1185 return true; 1186 } 1187 1188 // Handle intrinsic function calls. 1189 if (const auto *II = dyn_cast<IntrinsicInst>(Call)) 1190 return selectIntrinsicCall(II); 1191 1192 return lowerCall(Call); 1193 } 1194 1195 bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) { 1196 switch (II->getIntrinsicID()) { 1197 default: 1198 break; 1199 // At -O0 we don't care about the lifetime intrinsics. 1200 case Intrinsic::lifetime_start: 1201 case Intrinsic::lifetime_end: 1202 // The donothing intrinsic does, well, nothing. 1203 case Intrinsic::donothing: 1204 // Neither does the sideeffect intrinsic. 1205 case Intrinsic::sideeffect: 1206 // Neither does the assume intrinsic; it's also OK not to codegen its operand. 1207 case Intrinsic::assume: 1208 // Neither does the llvm.experimental.noalias.scope.decl intrinsic 1209 case Intrinsic::experimental_noalias_scope_decl: 1210 return true; 1211 case Intrinsic::dbg_declare: { 1212 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II); 1213 assert(DI->getVariable() && "Missing variable"); 1214 if (!FuncInfo.MF->getMMI().hasDebugInfo()) { 1215 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI 1216 << " (!hasDebugInfo)\n"); 1217 return true; 1218 } 1219 1220 const Value *Address = DI->getAddress(); 1221 if (!Address || isa<UndefValue>(Address)) { 1222 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI 1223 << " (bad/undef address)\n"); 1224 return true; 1225 } 1226 1227 // Byval arguments with frame indices were already handled after argument 1228 // lowering and before isel. 1229 const auto *Arg = 1230 dyn_cast<Argument>(Address->stripInBoundsConstantOffsets()); 1231 if (Arg && FuncInfo.getArgumentFrameIndex(Arg) != INT_MAX) 1232 return true; 1233 1234 Optional<MachineOperand> Op; 1235 if (Register Reg = lookUpRegForValue(Address)) 1236 Op = MachineOperand::CreateReg(Reg, false); 1237 1238 // If we have a VLA that has a "use" in a metadata node that's then used 1239 // here but it has no other uses, then we have a problem. E.g., 1240 // 1241 // int foo (const int *x) { 1242 // char a[*x]; 1243 // return 0; 1244 // } 1245 // 1246 // If we assign 'a' a vreg and fast isel later on has to use the selection 1247 // DAG isel, it will want to copy the value to the vreg. However, there are 1248 // no uses, which goes counter to what selection DAG isel expects. 1249 if (!Op && !Address->use_empty() && isa<Instruction>(Address) && 1250 (!isa<AllocaInst>(Address) || 1251 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address)))) 1252 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address), 1253 false); 1254 1255 if (Op) { 1256 assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) && 1257 "Expected inlined-at fields to agree"); 1258 // A dbg.declare describes the address of a source variable, so lower it 1259 // into an indirect DBG_VALUE. 1260 auto Builder = 1261 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1262 TII.get(TargetOpcode::DBG_VALUE), /*IsIndirect*/ true, *Op, 1263 DI->getVariable(), DI->getExpression()); 1264 1265 // If using instruction referencing, mutate this into a DBG_INSTR_REF, 1266 // to be later patched up by finalizeDebugInstrRefs. Tack a deref onto 1267 // the expression, we don't have an "indirect" flag in DBG_INSTR_REF. 1268 if (UseInstrRefDebugInfo && Op->isReg()) { 1269 Builder->setDesc(TII.get(TargetOpcode::DBG_INSTR_REF)); 1270 Builder->getOperand(1).ChangeToImmediate(0); 1271 auto *NewExpr = 1272 DIExpression::prepend(DI->getExpression(), DIExpression::DerefBefore); 1273 Builder->getOperand(3).setMetadata(NewExpr); 1274 } 1275 } else { 1276 // We can't yet handle anything else here because it would require 1277 // generating code, thus altering codegen because of debug info. 1278 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI 1279 << " (no materialized reg for address)\n"); 1280 } 1281 return true; 1282 } 1283 case Intrinsic::dbg_value: { 1284 // This form of DBG_VALUE is target-independent. 1285 const DbgValueInst *DI = cast<DbgValueInst>(II); 1286 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); 1287 const Value *V = DI->getValue(); 1288 assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) && 1289 "Expected inlined-at fields to agree"); 1290 if (!V || isa<UndefValue>(V) || DI->hasArgList()) { 1291 // DI is either undef or cannot produce a valid DBG_VALUE, so produce an 1292 // undef DBG_VALUE to terminate any prior location. 1293 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, false, 0U, 1294 DI->getVariable(), DI->getExpression()); 1295 } else if (const auto *CI = dyn_cast<ConstantInt>(V)) { 1296 // See if there's an expression to constant-fold. 1297 DIExpression *Expr = DI->getExpression(); 1298 if (Expr) 1299 std::tie(Expr, CI) = Expr->constantFold(CI); 1300 if (CI->getBitWidth() > 64) 1301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1302 .addCImm(CI) 1303 .addImm(0U) 1304 .addMetadata(DI->getVariable()) 1305 .addMetadata(Expr); 1306 else 1307 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1308 .addImm(CI->getZExtValue()) 1309 .addImm(0U) 1310 .addMetadata(DI->getVariable()) 1311 .addMetadata(Expr); 1312 } else if (const auto *CF = dyn_cast<ConstantFP>(V)) { 1313 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1314 .addFPImm(CF) 1315 .addImm(0U) 1316 .addMetadata(DI->getVariable()) 1317 .addMetadata(DI->getExpression()); 1318 } else if (Register Reg = lookUpRegForValue(V)) { 1319 // FIXME: This does not handle register-indirect values at offset 0. 1320 bool IsIndirect = false; 1321 auto Builder = 1322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, Reg, 1323 DI->getVariable(), DI->getExpression()); 1324 1325 // If using instruction referencing, mutate this into a DBG_INSTR_REF, 1326 // to be later patched up by finalizeDebugInstrRefs. 1327 if (UseInstrRefDebugInfo) { 1328 Builder->setDesc(TII.get(TargetOpcode::DBG_INSTR_REF)); 1329 Builder->getOperand(1).ChangeToImmediate(0); 1330 } 1331 } else { 1332 // We don't know how to handle other cases, so we drop. 1333 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 1334 } 1335 return true; 1336 } 1337 case Intrinsic::dbg_label: { 1338 const DbgLabelInst *DI = cast<DbgLabelInst>(II); 1339 assert(DI->getLabel() && "Missing label"); 1340 if (!FuncInfo.MF->getMMI().hasDebugInfo()) { 1341 LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 1342 return true; 1343 } 1344 1345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1346 TII.get(TargetOpcode::DBG_LABEL)).addMetadata(DI->getLabel()); 1347 return true; 1348 } 1349 case Intrinsic::objectsize: 1350 llvm_unreachable("llvm.objectsize.* should have been lowered already"); 1351 1352 case Intrinsic::is_constant: 1353 llvm_unreachable("llvm.is.constant.* should have been lowered already"); 1354 1355 case Intrinsic::launder_invariant_group: 1356 case Intrinsic::strip_invariant_group: 1357 case Intrinsic::expect: { 1358 Register ResultReg = getRegForValue(II->getArgOperand(0)); 1359 if (!ResultReg) 1360 return false; 1361 updateValueMap(II, ResultReg); 1362 return true; 1363 } 1364 case Intrinsic::experimental_stackmap: 1365 return selectStackmap(II); 1366 case Intrinsic::experimental_patchpoint_void: 1367 case Intrinsic::experimental_patchpoint_i64: 1368 return selectPatchpoint(II); 1369 1370 case Intrinsic::xray_customevent: 1371 return selectXRayCustomEvent(II); 1372 case Intrinsic::xray_typedevent: 1373 return selectXRayTypedEvent(II); 1374 } 1375 1376 return fastLowerIntrinsicCall(II); 1377 } 1378 1379 bool FastISel::selectCast(const User *I, unsigned Opcode) { 1380 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); 1381 EVT DstVT = TLI.getValueType(DL, I->getType()); 1382 1383 if (SrcVT == MVT::Other || !SrcVT.isSimple() || DstVT == MVT::Other || 1384 !DstVT.isSimple()) 1385 // Unhandled type. Halt "fast" selection and bail. 1386 return false; 1387 1388 // Check if the destination type is legal. 1389 if (!TLI.isTypeLegal(DstVT)) 1390 return false; 1391 1392 // Check if the source operand is legal. 1393 if (!TLI.isTypeLegal(SrcVT)) 1394 return false; 1395 1396 Register InputReg = getRegForValue(I->getOperand(0)); 1397 if (!InputReg) 1398 // Unhandled operand. Halt "fast" selection and bail. 1399 return false; 1400 1401 Register ResultReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), 1402 Opcode, InputReg); 1403 if (!ResultReg) 1404 return false; 1405 1406 updateValueMap(I, ResultReg); 1407 return true; 1408 } 1409 1410 bool FastISel::selectBitCast(const User *I) { 1411 // If the bitcast doesn't change the type, just use the operand value. 1412 if (I->getType() == I->getOperand(0)->getType()) { 1413 Register Reg = getRegForValue(I->getOperand(0)); 1414 if (!Reg) 1415 return false; 1416 updateValueMap(I, Reg); 1417 return true; 1418 } 1419 1420 // Bitcasts of other values become reg-reg copies or BITCAST operators. 1421 EVT SrcEVT = TLI.getValueType(DL, I->getOperand(0)->getType()); 1422 EVT DstEVT = TLI.getValueType(DL, I->getType()); 1423 if (SrcEVT == MVT::Other || DstEVT == MVT::Other || 1424 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT)) 1425 // Unhandled type. Halt "fast" selection and bail. 1426 return false; 1427 1428 MVT SrcVT = SrcEVT.getSimpleVT(); 1429 MVT DstVT = DstEVT.getSimpleVT(); 1430 Register Op0 = getRegForValue(I->getOperand(0)); 1431 if (!Op0) // Unhandled operand. Halt "fast" selection and bail. 1432 return false; 1433 1434 // First, try to perform the bitcast by inserting a reg-reg copy. 1435 Register ResultReg; 1436 if (SrcVT == DstVT) { 1437 const TargetRegisterClass *SrcClass = TLI.getRegClassFor(SrcVT); 1438 const TargetRegisterClass *DstClass = TLI.getRegClassFor(DstVT); 1439 // Don't attempt a cross-class copy. It will likely fail. 1440 if (SrcClass == DstClass) { 1441 ResultReg = createResultReg(DstClass); 1442 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1443 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0); 1444 } 1445 } 1446 1447 // If the reg-reg copy failed, select a BITCAST opcode. 1448 if (!ResultReg) 1449 ResultReg = fastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0); 1450 1451 if (!ResultReg) 1452 return false; 1453 1454 updateValueMap(I, ResultReg); 1455 return true; 1456 } 1457 1458 bool FastISel::selectFreeze(const User *I) { 1459 Register Reg = getRegForValue(I->getOperand(0)); 1460 if (!Reg) 1461 // Unhandled operand. 1462 return false; 1463 1464 EVT ETy = TLI.getValueType(DL, I->getOperand(0)->getType()); 1465 if (ETy == MVT::Other || !TLI.isTypeLegal(ETy)) 1466 // Unhandled type, bail out. 1467 return false; 1468 1469 MVT Ty = ETy.getSimpleVT(); 1470 const TargetRegisterClass *TyRegClass = TLI.getRegClassFor(Ty); 1471 Register ResultReg = createResultReg(TyRegClass); 1472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1473 TII.get(TargetOpcode::COPY), ResultReg).addReg(Reg); 1474 1475 updateValueMap(I, ResultReg); 1476 return true; 1477 } 1478 1479 // Remove local value instructions starting from the instruction after 1480 // SavedLastLocalValue to the current function insert point. 1481 void FastISel::removeDeadLocalValueCode(MachineInstr *SavedLastLocalValue) 1482 { 1483 MachineInstr *CurLastLocalValue = getLastLocalValue(); 1484 if (CurLastLocalValue != SavedLastLocalValue) { 1485 // Find the first local value instruction to be deleted. 1486 // This is the instruction after SavedLastLocalValue if it is non-NULL. 1487 // Otherwise it's the first instruction in the block. 1488 MachineBasicBlock::iterator FirstDeadInst(SavedLastLocalValue); 1489 if (SavedLastLocalValue) 1490 ++FirstDeadInst; 1491 else 1492 FirstDeadInst = FuncInfo.MBB->getFirstNonPHI(); 1493 setLastLocalValue(SavedLastLocalValue); 1494 removeDeadCode(FirstDeadInst, FuncInfo.InsertPt); 1495 } 1496 } 1497 1498 bool FastISel::selectInstruction(const Instruction *I) { 1499 // Flush the local value map before starting each instruction. 1500 // This improves locality and debugging, and can reduce spills. 1501 // Reuse of values across IR instructions is relatively uncommon. 1502 flushLocalValueMap(); 1503 1504 MachineInstr *SavedLastLocalValue = getLastLocalValue(); 1505 // Just before the terminator instruction, insert instructions to 1506 // feed PHI nodes in successor blocks. 1507 if (I->isTerminator()) { 1508 if (!handlePHINodesInSuccessorBlocks(I->getParent())) { 1509 // PHI node handling may have generated local value instructions, 1510 // even though it failed to handle all PHI nodes. 1511 // We remove these instructions because SelectionDAGISel will generate 1512 // them again. 1513 removeDeadLocalValueCode(SavedLastLocalValue); 1514 return false; 1515 } 1516 } 1517 1518 // FastISel does not handle any operand bundles except OB_funclet. 1519 if (auto *Call = dyn_cast<CallBase>(I)) 1520 for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) 1521 if (Call->getOperandBundleAt(i).getTagID() != LLVMContext::OB_funclet) 1522 return false; 1523 1524 DbgLoc = I->getDebugLoc(); 1525 1526 SavedInsertPt = FuncInfo.InsertPt; 1527 1528 if (const auto *Call = dyn_cast<CallInst>(I)) { 1529 const Function *F = Call->getCalledFunction(); 1530 LibFunc Func; 1531 1532 // As a special case, don't handle calls to builtin library functions that 1533 // may be translated directly to target instructions. 1534 if (F && !F->hasLocalLinkage() && F->hasName() && 1535 LibInfo->getLibFunc(F->getName(), Func) && 1536 LibInfo->hasOptimizedCodeGen(Func)) 1537 return false; 1538 1539 // Don't handle Intrinsic::trap if a trap function is specified. 1540 if (F && F->getIntrinsicID() == Intrinsic::trap && 1541 Call->hasFnAttr("trap-func-name")) 1542 return false; 1543 } 1544 1545 // First, try doing target-independent selection. 1546 if (!SkipTargetIndependentISel) { 1547 if (selectOperator(I, I->getOpcode())) { 1548 ++NumFastIselSuccessIndependent; 1549 DbgLoc = DebugLoc(); 1550 return true; 1551 } 1552 // Remove dead code. 1553 recomputeInsertPt(); 1554 if (SavedInsertPt != FuncInfo.InsertPt) 1555 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); 1556 SavedInsertPt = FuncInfo.InsertPt; 1557 } 1558 // Next, try calling the target to attempt to handle the instruction. 1559 if (fastSelectInstruction(I)) { 1560 ++NumFastIselSuccessTarget; 1561 DbgLoc = DebugLoc(); 1562 return true; 1563 } 1564 // Remove dead code. 1565 recomputeInsertPt(); 1566 if (SavedInsertPt != FuncInfo.InsertPt) 1567 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); 1568 1569 DbgLoc = DebugLoc(); 1570 // Undo phi node updates, because they will be added again by SelectionDAG. 1571 if (I->isTerminator()) { 1572 // PHI node handling may have generated local value instructions. 1573 // We remove them because SelectionDAGISel will generate them again. 1574 removeDeadLocalValueCode(SavedLastLocalValue); 1575 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); 1576 } 1577 return false; 1578 } 1579 1580 /// Emit an unconditional branch to the given block, unless it is the immediate 1581 /// (fall-through) successor, and update the CFG. 1582 void FastISel::fastEmitBranch(MachineBasicBlock *MSucc, 1583 const DebugLoc &DbgLoc) { 1584 if (FuncInfo.MBB->getBasicBlock()->sizeWithoutDebug() > 1 && 1585 FuncInfo.MBB->isLayoutSuccessor(MSucc)) { 1586 // For more accurate line information if this is the only non-debug 1587 // instruction in the block then emit it, otherwise we have the 1588 // unconditional fall-through case, which needs no instructions. 1589 } else { 1590 // The unconditional branch case. 1591 TII.insertBranch(*FuncInfo.MBB, MSucc, nullptr, 1592 SmallVector<MachineOperand, 0>(), DbgLoc); 1593 } 1594 if (FuncInfo.BPI) { 1595 auto BranchProbability = FuncInfo.BPI->getEdgeProbability( 1596 FuncInfo.MBB->getBasicBlock(), MSucc->getBasicBlock()); 1597 FuncInfo.MBB->addSuccessor(MSucc, BranchProbability); 1598 } else 1599 FuncInfo.MBB->addSuccessorWithoutProb(MSucc); 1600 } 1601 1602 void FastISel::finishCondBranch(const BasicBlock *BranchBB, 1603 MachineBasicBlock *TrueMBB, 1604 MachineBasicBlock *FalseMBB) { 1605 // Add TrueMBB as successor unless it is equal to the FalseMBB: This can 1606 // happen in degenerate IR and MachineIR forbids to have a block twice in the 1607 // successor/predecessor lists. 1608 if (TrueMBB != FalseMBB) { 1609 if (FuncInfo.BPI) { 1610 auto BranchProbability = 1611 FuncInfo.BPI->getEdgeProbability(BranchBB, TrueMBB->getBasicBlock()); 1612 FuncInfo.MBB->addSuccessor(TrueMBB, BranchProbability); 1613 } else 1614 FuncInfo.MBB->addSuccessorWithoutProb(TrueMBB); 1615 } 1616 1617 fastEmitBranch(FalseMBB, DbgLoc); 1618 } 1619 1620 /// Emit an FNeg operation. 1621 bool FastISel::selectFNeg(const User *I, const Value *In) { 1622 Register OpReg = getRegForValue(In); 1623 if (!OpReg) 1624 return false; 1625 1626 // If the target has ISD::FNEG, use it. 1627 EVT VT = TLI.getValueType(DL, I->getType()); 1628 Register ResultReg = fastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), ISD::FNEG, 1629 OpReg); 1630 if (ResultReg) { 1631 updateValueMap(I, ResultReg); 1632 return true; 1633 } 1634 1635 // Bitcast the value to integer, twiddle the sign bit with xor, 1636 // and then bitcast it back to floating-point. 1637 if (VT.getSizeInBits() > 64) 1638 return false; 1639 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); 1640 if (!TLI.isTypeLegal(IntVT)) 1641 return false; 1642 1643 Register IntReg = fastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), 1644 ISD::BITCAST, OpReg); 1645 if (!IntReg) 1646 return false; 1647 1648 Register IntResultReg = fastEmit_ri_( 1649 IntVT.getSimpleVT(), ISD::XOR, IntReg, 1650 UINT64_C(1) << (VT.getSizeInBits() - 1), IntVT.getSimpleVT()); 1651 if (!IntResultReg) 1652 return false; 1653 1654 ResultReg = fastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), ISD::BITCAST, 1655 IntResultReg); 1656 if (!ResultReg) 1657 return false; 1658 1659 updateValueMap(I, ResultReg); 1660 return true; 1661 } 1662 1663 bool FastISel::selectExtractValue(const User *U) { 1664 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); 1665 if (!EVI) 1666 return false; 1667 1668 // Make sure we only try to handle extracts with a legal result. But also 1669 // allow i1 because it's easy. 1670 EVT RealVT = TLI.getValueType(DL, EVI->getType(), /*AllowUnknown=*/true); 1671 if (!RealVT.isSimple()) 1672 return false; 1673 MVT VT = RealVT.getSimpleVT(); 1674 if (!TLI.isTypeLegal(VT) && VT != MVT::i1) 1675 return false; 1676 1677 const Value *Op0 = EVI->getOperand(0); 1678 Type *AggTy = Op0->getType(); 1679 1680 // Get the base result register. 1681 unsigned ResultReg; 1682 DenseMap<const Value *, Register>::iterator I = FuncInfo.ValueMap.find(Op0); 1683 if (I != FuncInfo.ValueMap.end()) 1684 ResultReg = I->second; 1685 else if (isa<Instruction>(Op0)) 1686 ResultReg = FuncInfo.InitializeRegForValue(Op0); 1687 else 1688 return false; // fast-isel can't handle aggregate constants at the moment 1689 1690 // Get the actual result register, which is an offset from the base register. 1691 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices()); 1692 1693 SmallVector<EVT, 4> AggValueVTs; 1694 ComputeValueVTs(TLI, DL, AggTy, AggValueVTs); 1695 1696 for (unsigned i = 0; i < VTIndex; i++) 1697 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]); 1698 1699 updateValueMap(EVI, ResultReg); 1700 return true; 1701 } 1702 1703 bool FastISel::selectOperator(const User *I, unsigned Opcode) { 1704 switch (Opcode) { 1705 case Instruction::Add: 1706 return selectBinaryOp(I, ISD::ADD); 1707 case Instruction::FAdd: 1708 return selectBinaryOp(I, ISD::FADD); 1709 case Instruction::Sub: 1710 return selectBinaryOp(I, ISD::SUB); 1711 case Instruction::FSub: 1712 return selectBinaryOp(I, ISD::FSUB); 1713 case Instruction::Mul: 1714 return selectBinaryOp(I, ISD::MUL); 1715 case Instruction::FMul: 1716 return selectBinaryOp(I, ISD::FMUL); 1717 case Instruction::SDiv: 1718 return selectBinaryOp(I, ISD::SDIV); 1719 case Instruction::UDiv: 1720 return selectBinaryOp(I, ISD::UDIV); 1721 case Instruction::FDiv: 1722 return selectBinaryOp(I, ISD::FDIV); 1723 case Instruction::SRem: 1724 return selectBinaryOp(I, ISD::SREM); 1725 case Instruction::URem: 1726 return selectBinaryOp(I, ISD::UREM); 1727 case Instruction::FRem: 1728 return selectBinaryOp(I, ISD::FREM); 1729 case Instruction::Shl: 1730 return selectBinaryOp(I, ISD::SHL); 1731 case Instruction::LShr: 1732 return selectBinaryOp(I, ISD::SRL); 1733 case Instruction::AShr: 1734 return selectBinaryOp(I, ISD::SRA); 1735 case Instruction::And: 1736 return selectBinaryOp(I, ISD::AND); 1737 case Instruction::Or: 1738 return selectBinaryOp(I, ISD::OR); 1739 case Instruction::Xor: 1740 return selectBinaryOp(I, ISD::XOR); 1741 1742 case Instruction::FNeg: 1743 return selectFNeg(I, I->getOperand(0)); 1744 1745 case Instruction::GetElementPtr: 1746 return selectGetElementPtr(I); 1747 1748 case Instruction::Br: { 1749 const BranchInst *BI = cast<BranchInst>(I); 1750 1751 if (BI->isUnconditional()) { 1752 const BasicBlock *LLVMSucc = BI->getSuccessor(0); 1753 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc]; 1754 fastEmitBranch(MSucc, BI->getDebugLoc()); 1755 return true; 1756 } 1757 1758 // Conditional branches are not handed yet. 1759 // Halt "fast" selection and bail. 1760 return false; 1761 } 1762 1763 case Instruction::Unreachable: 1764 if (TM.Options.TrapUnreachable) 1765 return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0; 1766 else 1767 return true; 1768 1769 case Instruction::Alloca: 1770 // FunctionLowering has the static-sized case covered. 1771 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I))) 1772 return true; 1773 1774 // Dynamic-sized alloca is not handled yet. 1775 return false; 1776 1777 case Instruction::Call: 1778 // On AIX, normal call lowering uses the DAG-ISEL path currently so that the 1779 // callee of the direct function call instruction will be mapped to the 1780 // symbol for the function's entry point, which is distinct from the 1781 // function descriptor symbol. The latter is the symbol whose XCOFF symbol 1782 // name is the C-linkage name of the source level function. 1783 // But fast isel still has the ability to do selection for intrinsics. 1784 if (TM.getTargetTriple().isOSAIX() && !isa<IntrinsicInst>(I)) 1785 return false; 1786 return selectCall(I); 1787 1788 case Instruction::BitCast: 1789 return selectBitCast(I); 1790 1791 case Instruction::FPToSI: 1792 return selectCast(I, ISD::FP_TO_SINT); 1793 case Instruction::ZExt: 1794 return selectCast(I, ISD::ZERO_EXTEND); 1795 case Instruction::SExt: 1796 return selectCast(I, ISD::SIGN_EXTEND); 1797 case Instruction::Trunc: 1798 return selectCast(I, ISD::TRUNCATE); 1799 case Instruction::SIToFP: 1800 return selectCast(I, ISD::SINT_TO_FP); 1801 1802 case Instruction::IntToPtr: // Deliberate fall-through. 1803 case Instruction::PtrToInt: { 1804 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); 1805 EVT DstVT = TLI.getValueType(DL, I->getType()); 1806 if (DstVT.bitsGT(SrcVT)) 1807 return selectCast(I, ISD::ZERO_EXTEND); 1808 if (DstVT.bitsLT(SrcVT)) 1809 return selectCast(I, ISD::TRUNCATE); 1810 Register Reg = getRegForValue(I->getOperand(0)); 1811 if (!Reg) 1812 return false; 1813 updateValueMap(I, Reg); 1814 return true; 1815 } 1816 1817 case Instruction::ExtractValue: 1818 return selectExtractValue(I); 1819 1820 case Instruction::Freeze: 1821 return selectFreeze(I); 1822 1823 case Instruction::PHI: 1824 llvm_unreachable("FastISel shouldn't visit PHI nodes!"); 1825 1826 default: 1827 // Unhandled instruction. Halt "fast" selection and bail. 1828 return false; 1829 } 1830 } 1831 1832 FastISel::FastISel(FunctionLoweringInfo &FuncInfo, 1833 const TargetLibraryInfo *LibInfo, 1834 bool SkipTargetIndependentISel) 1835 : FuncInfo(FuncInfo), MF(FuncInfo.MF), MRI(FuncInfo.MF->getRegInfo()), 1836 MFI(FuncInfo.MF->getFrameInfo()), MCP(*FuncInfo.MF->getConstantPool()), 1837 TM(FuncInfo.MF->getTarget()), DL(MF->getDataLayout()), 1838 TII(*MF->getSubtarget().getInstrInfo()), 1839 TLI(*MF->getSubtarget().getTargetLowering()), 1840 TRI(*MF->getSubtarget().getRegisterInfo()), LibInfo(LibInfo), 1841 SkipTargetIndependentISel(SkipTargetIndependentISel) {} 1842 1843 FastISel::~FastISel() = default; 1844 1845 bool FastISel::fastLowerArguments() { return false; } 1846 1847 bool FastISel::fastLowerCall(CallLoweringInfo & /*CLI*/) { return false; } 1848 1849 bool FastISel::fastLowerIntrinsicCall(const IntrinsicInst * /*II*/) { 1850 return false; 1851 } 1852 1853 unsigned FastISel::fastEmit_(MVT, MVT, unsigned) { return 0; } 1854 1855 unsigned FastISel::fastEmit_r(MVT, MVT, unsigned, unsigned /*Op0*/) { 1856 return 0; 1857 } 1858 1859 unsigned FastISel::fastEmit_rr(MVT, MVT, unsigned, unsigned /*Op0*/, 1860 unsigned /*Op1*/) { 1861 return 0; 1862 } 1863 1864 unsigned FastISel::fastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { 1865 return 0; 1866 } 1867 1868 unsigned FastISel::fastEmit_f(MVT, MVT, unsigned, 1869 const ConstantFP * /*FPImm*/) { 1870 return 0; 1871 } 1872 1873 unsigned FastISel::fastEmit_ri(MVT, MVT, unsigned, unsigned /*Op0*/, 1874 uint64_t /*Imm*/) { 1875 return 0; 1876 } 1877 1878 /// This method is a wrapper of fastEmit_ri. It first tries to emit an 1879 /// instruction with an immediate operand using fastEmit_ri. 1880 /// If that fails, it materializes the immediate into a register and try 1881 /// fastEmit_rr instead. 1882 Register FastISel::fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0, 1883 uint64_t Imm, MVT ImmType) { 1884 // If this is a multiply by a power of two, emit this as a shift left. 1885 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) { 1886 Opcode = ISD::SHL; 1887 Imm = Log2_64(Imm); 1888 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) { 1889 // div x, 8 -> srl x, 3 1890 Opcode = ISD::SRL; 1891 Imm = Log2_64(Imm); 1892 } 1893 1894 // Horrible hack (to be removed), check to make sure shift amounts are 1895 // in-range. 1896 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) && 1897 Imm >= VT.getSizeInBits()) 1898 return 0; 1899 1900 // First check if immediate type is legal. If not, we can't use the ri form. 1901 Register ResultReg = fastEmit_ri(VT, VT, Opcode, Op0, Imm); 1902 if (ResultReg) 1903 return ResultReg; 1904 Register MaterialReg = fastEmit_i(ImmType, ImmType, ISD::Constant, Imm); 1905 if (!MaterialReg) { 1906 // This is a bit ugly/slow, but failing here means falling out of 1907 // fast-isel, which would be very slow. 1908 IntegerType *ITy = 1909 IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits()); 1910 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm)); 1911 if (!MaterialReg) 1912 return 0; 1913 } 1914 return fastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); 1915 } 1916 1917 Register FastISel::createResultReg(const TargetRegisterClass *RC) { 1918 return MRI.createVirtualRegister(RC); 1919 } 1920 1921 Register FastISel::constrainOperandRegClass(const MCInstrDesc &II, Register Op, 1922 unsigned OpNum) { 1923 if (Op.isVirtual()) { 1924 const TargetRegisterClass *RegClass = 1925 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF); 1926 if (!MRI.constrainRegClass(Op, RegClass)) { 1927 // If it's not legal to COPY between the register classes, something 1928 // has gone very wrong before we got here. 1929 Register NewOp = createResultReg(RegClass); 1930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1931 TII.get(TargetOpcode::COPY), NewOp).addReg(Op); 1932 return NewOp; 1933 } 1934 } 1935 return Op; 1936 } 1937 1938 Register FastISel::fastEmitInst_(unsigned MachineInstOpcode, 1939 const TargetRegisterClass *RC) { 1940 Register ResultReg = createResultReg(RC); 1941 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1942 1943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg); 1944 return ResultReg; 1945 } 1946 1947 Register FastISel::fastEmitInst_r(unsigned MachineInstOpcode, 1948 const TargetRegisterClass *RC, unsigned Op0) { 1949 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1950 1951 Register ResultReg = createResultReg(RC); 1952 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1953 1954 if (II.getNumDefs() >= 1) 1955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1956 .addReg(Op0); 1957 else { 1958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1959 .addReg(Op0); 1960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1961 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1962 } 1963 1964 return ResultReg; 1965 } 1966 1967 Register FastISel::fastEmitInst_rr(unsigned MachineInstOpcode, 1968 const TargetRegisterClass *RC, unsigned Op0, 1969 unsigned Op1) { 1970 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1971 1972 Register ResultReg = createResultReg(RC); 1973 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1974 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1975 1976 if (II.getNumDefs() >= 1) 1977 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1978 .addReg(Op0) 1979 .addReg(Op1); 1980 else { 1981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1982 .addReg(Op0) 1983 .addReg(Op1); 1984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1985 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1986 } 1987 return ResultReg; 1988 } 1989 1990 Register FastISel::fastEmitInst_rrr(unsigned MachineInstOpcode, 1991 const TargetRegisterClass *RC, unsigned Op0, 1992 unsigned Op1, unsigned Op2) { 1993 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1994 1995 Register ResultReg = createResultReg(RC); 1996 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1997 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1998 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2); 1999 2000 if (II.getNumDefs() >= 1) 2001 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2002 .addReg(Op0) 2003 .addReg(Op1) 2004 .addReg(Op2); 2005 else { 2006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 2007 .addReg(Op0) 2008 .addReg(Op1) 2009 .addReg(Op2); 2010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2011 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2012 } 2013 return ResultReg; 2014 } 2015 2016 Register FastISel::fastEmitInst_ri(unsigned MachineInstOpcode, 2017 const TargetRegisterClass *RC, unsigned Op0, 2018 uint64_t Imm) { 2019 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2020 2021 Register ResultReg = createResultReg(RC); 2022 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 2023 2024 if (II.getNumDefs() >= 1) 2025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2026 .addReg(Op0) 2027 .addImm(Imm); 2028 else { 2029 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 2030 .addReg(Op0) 2031 .addImm(Imm); 2032 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2033 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2034 } 2035 return ResultReg; 2036 } 2037 2038 Register FastISel::fastEmitInst_rii(unsigned MachineInstOpcode, 2039 const TargetRegisterClass *RC, unsigned Op0, 2040 uint64_t Imm1, uint64_t Imm2) { 2041 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2042 2043 Register ResultReg = createResultReg(RC); 2044 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 2045 2046 if (II.getNumDefs() >= 1) 2047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2048 .addReg(Op0) 2049 .addImm(Imm1) 2050 .addImm(Imm2); 2051 else { 2052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 2053 .addReg(Op0) 2054 .addImm(Imm1) 2055 .addImm(Imm2); 2056 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2057 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2058 } 2059 return ResultReg; 2060 } 2061 2062 Register FastISel::fastEmitInst_f(unsigned MachineInstOpcode, 2063 const TargetRegisterClass *RC, 2064 const ConstantFP *FPImm) { 2065 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2066 2067 Register ResultReg = createResultReg(RC); 2068 2069 if (II.getNumDefs() >= 1) 2070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2071 .addFPImm(FPImm); 2072 else { 2073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 2074 .addFPImm(FPImm); 2075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2076 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2077 } 2078 return ResultReg; 2079 } 2080 2081 Register FastISel::fastEmitInst_rri(unsigned MachineInstOpcode, 2082 const TargetRegisterClass *RC, unsigned Op0, 2083 unsigned Op1, uint64_t Imm) { 2084 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2085 2086 Register ResultReg = createResultReg(RC); 2087 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 2088 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 2089 2090 if (II.getNumDefs() >= 1) 2091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2092 .addReg(Op0) 2093 .addReg(Op1) 2094 .addImm(Imm); 2095 else { 2096 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 2097 .addReg(Op0) 2098 .addReg(Op1) 2099 .addImm(Imm); 2100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2101 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2102 } 2103 return ResultReg; 2104 } 2105 2106 Register FastISel::fastEmitInst_i(unsigned MachineInstOpcode, 2107 const TargetRegisterClass *RC, uint64_t Imm) { 2108 Register ResultReg = createResultReg(RC); 2109 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2110 2111 if (II.getNumDefs() >= 1) 2112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2113 .addImm(Imm); 2114 else { 2115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm); 2116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2117 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 2118 } 2119 return ResultReg; 2120 } 2121 2122 Register FastISel::fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0, 2123 uint32_t Idx) { 2124 Register ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 2125 assert(Register::isVirtualRegister(Op0) && 2126 "Cannot yet extract from physregs"); 2127 const TargetRegisterClass *RC = MRI.getRegClass(Op0); 2128 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx)); 2129 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY), 2130 ResultReg).addReg(Op0, 0, Idx); 2131 return ResultReg; 2132 } 2133 2134 /// Emit MachineInstrs to compute the value of Op with all but the least 2135 /// significant bit set to zero. 2136 Register FastISel::fastEmitZExtFromI1(MVT VT, unsigned Op0) { 2137 return fastEmit_ri(VT, VT, ISD::AND, Op0, 1); 2138 } 2139 2140 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. 2141 /// Emit code to ensure constants are copied into registers when needed. 2142 /// Remember the virtual registers that need to be added to the Machine PHI 2143 /// nodes as input. We cannot just directly add them, because expansion 2144 /// might result in multiple MBB's for one BB. As such, the start of the 2145 /// BB might correspond to a different MBB than the end. 2146 bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { 2147 const Instruction *TI = LLVMBB->getTerminator(); 2148 2149 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; 2150 FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size(); 2151 2152 // Check successor nodes' PHI nodes that expect a constant to be available 2153 // from this block. 2154 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { 2155 const BasicBlock *SuccBB = TI->getSuccessor(succ); 2156 if (!isa<PHINode>(SuccBB->begin())) 2157 continue; 2158 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; 2159 2160 // If this terminator has multiple identical successors (common for 2161 // switches), only handle each succ once. 2162 if (!SuccsHandled.insert(SuccMBB).second) 2163 continue; 2164 2165 MachineBasicBlock::iterator MBBI = SuccMBB->begin(); 2166 2167 // At this point we know that there is a 1-1 correspondence between LLVM PHI 2168 // nodes and Machine PHI nodes, but the incoming operands have not been 2169 // emitted yet. 2170 for (const PHINode &PN : SuccBB->phis()) { 2171 // Ignore dead phi's. 2172 if (PN.use_empty()) 2173 continue; 2174 2175 // Only handle legal types. Two interesting things to note here. First, 2176 // by bailing out early, we may leave behind some dead instructions, 2177 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its 2178 // own moves. Second, this check is necessary because FastISel doesn't 2179 // use CreateRegs to create registers, so it always creates 2180 // exactly one register for each non-void instruction. 2181 EVT VT = TLI.getValueType(DL, PN.getType(), /*AllowUnknown=*/true); 2182 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { 2183 // Handle integer promotions, though, because they're common and easy. 2184 if (!(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) { 2185 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); 2186 return false; 2187 } 2188 } 2189 2190 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB); 2191 2192 // Set the DebugLoc for the copy. Use the location of the operand if 2193 // there is one; otherwise no location, flushLocalValueMap will fix it. 2194 DbgLoc = DebugLoc(); 2195 if (const auto *Inst = dyn_cast<Instruction>(PHIOp)) 2196 DbgLoc = Inst->getDebugLoc(); 2197 2198 Register Reg = getRegForValue(PHIOp); 2199 if (!Reg) { 2200 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); 2201 return false; 2202 } 2203 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(&*MBBI++, Reg)); 2204 DbgLoc = DebugLoc(); 2205 } 2206 } 2207 2208 return true; 2209 } 2210 2211 bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) { 2212 assert(LI->hasOneUse() && 2213 "tryToFoldLoad expected a LoadInst with a single use"); 2214 // We know that the load has a single use, but don't know what it is. If it 2215 // isn't one of the folded instructions, then we can't succeed here. Handle 2216 // this by scanning the single-use users of the load until we get to FoldInst. 2217 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs. 2218 2219 const Instruction *TheUser = LI->user_back(); 2220 while (TheUser != FoldInst && // Scan up until we find FoldInst. 2221 // Stay in the right block. 2222 TheUser->getParent() == FoldInst->getParent() && 2223 --MaxUsers) { // Don't scan too far. 2224 // If there are multiple or no uses of this instruction, then bail out. 2225 if (!TheUser->hasOneUse()) 2226 return false; 2227 2228 TheUser = TheUser->user_back(); 2229 } 2230 2231 // If we didn't find the fold instruction, then we failed to collapse the 2232 // sequence. 2233 if (TheUser != FoldInst) 2234 return false; 2235 2236 // Don't try to fold volatile loads. Target has to deal with alignment 2237 // constraints. 2238 if (LI->isVolatile()) 2239 return false; 2240 2241 // Figure out which vreg this is going into. If there is no assigned vreg yet 2242 // then there actually was no reference to it. Perhaps the load is referenced 2243 // by a dead instruction. 2244 Register LoadReg = getRegForValue(LI); 2245 if (!LoadReg) 2246 return false; 2247 2248 // We can't fold if this vreg has no uses or more than one use. Multiple uses 2249 // may mean that the instruction got lowered to multiple MIs, or the use of 2250 // the loaded value ended up being multiple operands of the result. 2251 if (!MRI.hasOneUse(LoadReg)) 2252 return false; 2253 2254 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg); 2255 MachineInstr *User = RI->getParent(); 2256 2257 // Set the insertion point properly. Folding the load can cause generation of 2258 // other random instructions (like sign extends) for addressing modes; make 2259 // sure they get inserted in a logical place before the new instruction. 2260 FuncInfo.InsertPt = User; 2261 FuncInfo.MBB = User->getParent(); 2262 2263 // Ask the target to try folding the load. 2264 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI); 2265 } 2266 2267 bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) { 2268 // Must be an add. 2269 if (!isa<AddOperator>(Add)) 2270 return false; 2271 // Type size needs to match. 2272 if (DL.getTypeSizeInBits(GEP->getType()) != 2273 DL.getTypeSizeInBits(Add->getType())) 2274 return false; 2275 // Must be in the same basic block. 2276 if (isa<Instruction>(Add) && 2277 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB) 2278 return false; 2279 // Must have a constant operand. 2280 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1)); 2281 } 2282 2283 MachineMemOperand * 2284 FastISel::createMachineMemOperandFor(const Instruction *I) const { 2285 const Value *Ptr; 2286 Type *ValTy; 2287 MaybeAlign Alignment; 2288 MachineMemOperand::Flags Flags; 2289 bool IsVolatile; 2290 2291 if (const auto *LI = dyn_cast<LoadInst>(I)) { 2292 Alignment = LI->getAlign(); 2293 IsVolatile = LI->isVolatile(); 2294 Flags = MachineMemOperand::MOLoad; 2295 Ptr = LI->getPointerOperand(); 2296 ValTy = LI->getType(); 2297 } else if (const auto *SI = dyn_cast<StoreInst>(I)) { 2298 Alignment = SI->getAlign(); 2299 IsVolatile = SI->isVolatile(); 2300 Flags = MachineMemOperand::MOStore; 2301 Ptr = SI->getPointerOperand(); 2302 ValTy = SI->getValueOperand()->getType(); 2303 } else 2304 return nullptr; 2305 2306 bool IsNonTemporal = I->hasMetadata(LLVMContext::MD_nontemporal); 2307 bool IsInvariant = I->hasMetadata(LLVMContext::MD_invariant_load); 2308 bool IsDereferenceable = I->hasMetadata(LLVMContext::MD_dereferenceable); 2309 const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range); 2310 2311 AAMDNodes AAInfo = I->getAAMetadata(); 2312 2313 if (!Alignment) // Ensure that codegen never sees alignment 0. 2314 Alignment = DL.getABITypeAlign(ValTy); 2315 2316 unsigned Size = DL.getTypeStoreSize(ValTy); 2317 2318 if (IsVolatile) 2319 Flags |= MachineMemOperand::MOVolatile; 2320 if (IsNonTemporal) 2321 Flags |= MachineMemOperand::MONonTemporal; 2322 if (IsDereferenceable) 2323 Flags |= MachineMemOperand::MODereferenceable; 2324 if (IsInvariant) 2325 Flags |= MachineMemOperand::MOInvariant; 2326 2327 return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size, 2328 *Alignment, AAInfo, Ranges); 2329 } 2330 2331 CmpInst::Predicate FastISel::optimizeCmpPredicate(const CmpInst *CI) const { 2332 // If both operands are the same, then try to optimize or fold the cmp. 2333 CmpInst::Predicate Predicate = CI->getPredicate(); 2334 if (CI->getOperand(0) != CI->getOperand(1)) 2335 return Predicate; 2336 2337 switch (Predicate) { 2338 default: llvm_unreachable("Invalid predicate!"); 2339 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break; 2340 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break; 2341 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break; 2342 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break; 2343 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break; 2344 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break; 2345 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break; 2346 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break; 2347 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break; 2348 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break; 2349 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break; 2350 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break; 2351 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break; 2352 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break; 2353 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break; 2354 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break; 2355 2356 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break; 2357 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break; 2358 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break; 2359 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break; 2360 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break; 2361 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break; 2362 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break; 2363 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break; 2364 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break; 2365 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break; 2366 } 2367 2368 return Predicate; 2369 } 2370