1 //===-- FastISel.cpp - Implementation of the FastISel class ---------------===// 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 contains the implementation of the FastISel class. 11 // 12 // "Fast" instruction selection is designed to emit very poor code quickly. 13 // Also, it is not designed to be able to do much lowering, so most illegal 14 // types (e.g. i64 on 32-bit targets) and operations are not supported. It is 15 // also not intended to be able to do much optimization, except in a few cases 16 // where doing optimizations reduces overall compile time. For example, folding 17 // constants into immediate fields is often done, because it's cheap and it 18 // reduces the number of instructions later phases have to examine. 19 // 20 // "Fast" instruction selection is able to fail gracefully and transfer 21 // control to the SelectionDAG selector for operations that it doesn't 22 // support. In many cases, this allows us to avoid duplicating a lot of 23 // the complicated lowering logic that SelectionDAG currently has. 24 // 25 // The intended use for "fast" instruction selection is "-O0" mode 26 // compilation, where the quality of the generated code is irrelevant when 27 // weighed against the speed at which the code can be generated. Also, 28 // at -O0, the LLVM optimizers are not running, and this makes the 29 // compile time of codegen a much higher portion of the overall compile 30 // time. Despite its limitations, "fast" instruction selection is able to 31 // handle enough code on its own to provide noticeable overall speedups 32 // in -O0 compiles. 33 // 34 // Basic operations are supported in a target-independent way, by reading 35 // the same instruction descriptions that the SelectionDAG selector reads, 36 // and identifying simple arithmetic operations that can be directly selected 37 // from simple operators. More complicated operations currently require 38 // target-specific code. 39 // 40 //===----------------------------------------------------------------------===// 41 42 #define DEBUG_TYPE "isel" 43 #include "llvm/CodeGen/FastISel.h" 44 #include "llvm/ADT/Optional.h" 45 #include "llvm/ADT/Statistic.h" 46 #include "llvm/Analysis/Loads.h" 47 #include "llvm/CodeGen/Analysis.h" 48 #include "llvm/CodeGen/FunctionLoweringInfo.h" 49 #include "llvm/CodeGen/MachineInstrBuilder.h" 50 #include "llvm/CodeGen/MachineModuleInfo.h" 51 #include "llvm/CodeGen/MachineRegisterInfo.h" 52 #include "llvm/IR/DataLayout.h" 53 #include "llvm/IR/DebugInfo.h" 54 #include "llvm/IR/Function.h" 55 #include "llvm/IR/GlobalVariable.h" 56 #include "llvm/IR/Instructions.h" 57 #include "llvm/IR/IntrinsicInst.h" 58 #include "llvm/IR/Operator.h" 59 #include "llvm/Support/Debug.h" 60 #include "llvm/Support/ErrorHandling.h" 61 #include "llvm/Target/TargetInstrInfo.h" 62 #include "llvm/Target/TargetLibraryInfo.h" 63 #include "llvm/Target/TargetLowering.h" 64 #include "llvm/Target/TargetMachine.h" 65 using namespace llvm; 66 67 STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by " 68 "target-independent selector"); 69 STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by " 70 "target-specific selector"); 71 STATISTIC(NumFastIselDead, "Number of dead insts removed on failure"); 72 73 /// startNewBlock - Set the current block to which generated machine 74 /// instructions will be appended, and clear the local CSE map. 75 /// 76 void FastISel::startNewBlock() { 77 LocalValueMap.clear(); 78 79 // Instructions are appended to FuncInfo.MBB. If the basic block already 80 // contains labels or copies, use the last instruction as the last local 81 // value. 82 EmitStartPt = nullptr; 83 if (!FuncInfo.MBB->empty()) 84 EmitStartPt = &FuncInfo.MBB->back(); 85 LastLocalValue = EmitStartPt; 86 } 87 88 bool FastISel::LowerArguments() { 89 if (!FuncInfo.CanLowerReturn) 90 // Fallback to SDISel argument lowering code to deal with sret pointer 91 // parameter. 92 return false; 93 94 if (!FastLowerArguments()) 95 return false; 96 97 // Enter arguments into ValueMap for uses in non-entry BBs. 98 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(), 99 E = FuncInfo.Fn->arg_end(); I != E; ++I) { 100 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I); 101 assert(VI != LocalValueMap.end() && "Missed an argument?"); 102 FuncInfo.ValueMap[I] = VI->second; 103 } 104 return true; 105 } 106 107 void FastISel::flushLocalValueMap() { 108 LocalValueMap.clear(); 109 LastLocalValue = EmitStartPt; 110 recomputeInsertPt(); 111 } 112 113 bool FastISel::hasTrivialKill(const Value *V) const { 114 // Don't consider constants or arguments to have trivial kills. 115 const Instruction *I = dyn_cast<Instruction>(V); 116 if (!I) 117 return false; 118 119 // No-op casts are trivially coalesced by fast-isel. 120 if (const CastInst *Cast = dyn_cast<CastInst>(I)) 121 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) && 122 !hasTrivialKill(Cast->getOperand(0))) 123 return false; 124 125 // GEPs with all zero indices are trivially coalesced by fast-isel. 126 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) 127 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0))) 128 return false; 129 130 // Only instructions with a single use in the same basic block are considered 131 // to have trivial kills. 132 return I->hasOneUse() && 133 !(I->getOpcode() == Instruction::BitCast || 134 I->getOpcode() == Instruction::PtrToInt || 135 I->getOpcode() == Instruction::IntToPtr) && 136 cast<Instruction>(*I->user_begin())->getParent() == I->getParent(); 137 } 138 139 unsigned FastISel::getRegForValue(const Value *V) { 140 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true); 141 // Don't handle non-simple values in FastISel. 142 if (!RealVT.isSimple()) 143 return 0; 144 145 // Ignore illegal types. We must do this before looking up the value 146 // in ValueMap because Arguments are given virtual registers regardless 147 // of whether FastISel can handle them. 148 MVT VT = RealVT.getSimpleVT(); 149 if (!TLI.isTypeLegal(VT)) { 150 // Handle integer promotions, though, because they're common and easy. 151 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 152 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); 153 else 154 return 0; 155 } 156 157 // Look up the value to see if we already have a register for it. 158 unsigned Reg = lookUpRegForValue(V); 159 if (Reg != 0) 160 return Reg; 161 162 // In bottom-up mode, just create the virtual register which will be used 163 // to hold the value. It will be materialized later. 164 if (isa<Instruction>(V) && 165 (!isa<AllocaInst>(V) || 166 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) 167 return FuncInfo.InitializeRegForValue(V); 168 169 SavePoint SaveInsertPt = enterLocalValueArea(); 170 171 // Materialize the value in a register. Emit any instructions in the 172 // local value area. 173 Reg = materializeRegForValue(V, VT); 174 175 leaveLocalValueArea(SaveInsertPt); 176 177 return Reg; 178 } 179 180 /// materializeRegForValue - Helper for getRegForValue. This function is 181 /// called when the value isn't already available in a register and must 182 /// be materialized with new instructions. 183 unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) { 184 unsigned Reg = 0; 185 186 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 187 if (CI->getValue().getActiveBits() <= 64) 188 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); 189 } else if (isa<AllocaInst>(V)) { 190 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); 191 } else if (isa<ConstantPointerNull>(V)) { 192 // Translate this as an integer zero so that it can be 193 // local-CSE'd with actual integer zeros. 194 Reg = 195 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext()))); 196 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 197 if (CF->isNullValue()) { 198 Reg = TargetMaterializeFloatZero(CF); 199 } else { 200 // Try to emit the constant directly. 201 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); 202 } 203 204 if (!Reg) { 205 // Try to emit the constant by using an integer constant with a cast. 206 const APFloat &Flt = CF->getValueAPF(); 207 EVT IntVT = TLI.getPointerTy(); 208 209 uint64_t x[2]; 210 uint32_t IntBitWidth = IntVT.getSizeInBits(); 211 bool isExact; 212 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, 213 APFloat::rmTowardZero, &isExact); 214 if (isExact) { 215 APInt IntVal(IntBitWidth, x); 216 217 unsigned IntegerReg = 218 getRegForValue(ConstantInt::get(V->getContext(), IntVal)); 219 if (IntegerReg != 0) 220 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, 221 IntegerReg, /*Kill=*/false); 222 } 223 } 224 } else if (const Operator *Op = dyn_cast<Operator>(V)) { 225 if (!SelectOperator(Op, Op->getOpcode())) 226 if (!isa<Instruction>(Op) || 227 !TargetSelectInstruction(cast<Instruction>(Op))) 228 return 0; 229 Reg = lookUpRegForValue(Op); 230 } else if (isa<UndefValue>(V)) { 231 Reg = createResultReg(TLI.getRegClassFor(VT)); 232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 233 TII.get(TargetOpcode::IMPLICIT_DEF), Reg); 234 } 235 236 // If target-independent code couldn't handle the value, give target-specific 237 // code a try. 238 if (!Reg && isa<Constant>(V)) 239 Reg = TargetMaterializeConstant(cast<Constant>(V)); 240 241 // Don't cache constant materializations in the general ValueMap. 242 // To do so would require tracking what uses they dominate. 243 if (Reg != 0) { 244 LocalValueMap[V] = Reg; 245 LastLocalValue = MRI.getVRegDef(Reg); 246 } 247 return Reg; 248 } 249 250 unsigned FastISel::lookUpRegForValue(const Value *V) { 251 // Look up the value to see if we already have a register for it. We 252 // cache values defined by Instructions across blocks, and other values 253 // only locally. This is because Instructions already have the SSA 254 // def-dominates-use requirement enforced. 255 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V); 256 if (I != FuncInfo.ValueMap.end()) 257 return I->second; 258 return LocalValueMap[V]; 259 } 260 261 /// UpdateValueMap - Update the value map to include the new mapping for this 262 /// instruction, or insert an extra copy to get the result in a previous 263 /// determined register. 264 /// NOTE: This is only necessary because we might select a block that uses 265 /// a value before we select the block that defines the value. It might be 266 /// possible to fix this by selecting blocks in reverse postorder. 267 void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) { 268 if (!isa<Instruction>(I)) { 269 LocalValueMap[I] = Reg; 270 return; 271 } 272 273 unsigned &AssignedReg = FuncInfo.ValueMap[I]; 274 if (AssignedReg == 0) 275 // Use the new register. 276 AssignedReg = Reg; 277 else if (Reg != AssignedReg) { 278 // Arrange for uses of AssignedReg to be replaced by uses of Reg. 279 for (unsigned i = 0; i < NumRegs; i++) 280 FuncInfo.RegFixups[AssignedReg+i] = Reg+i; 281 282 AssignedReg = Reg; 283 } 284 } 285 286 std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) { 287 unsigned IdxN = getRegForValue(Idx); 288 if (IdxN == 0) 289 // Unhandled operand. Halt "fast" selection and bail. 290 return std::pair<unsigned, bool>(0, false); 291 292 bool IdxNIsKill = hasTrivialKill(Idx); 293 294 // If the index is smaller or larger than intptr_t, truncate or extend it. 295 MVT PtrVT = TLI.getPointerTy(); 296 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); 297 if (IdxVT.bitsLT(PtrVT)) { 298 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, 299 IdxN, IdxNIsKill); 300 IdxNIsKill = true; 301 } 302 else if (IdxVT.bitsGT(PtrVT)) { 303 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, 304 IdxN, IdxNIsKill); 305 IdxNIsKill = true; 306 } 307 return std::pair<unsigned, bool>(IdxN, IdxNIsKill); 308 } 309 310 void FastISel::recomputeInsertPt() { 311 if (getLastLocalValue()) { 312 FuncInfo.InsertPt = getLastLocalValue(); 313 FuncInfo.MBB = FuncInfo.InsertPt->getParent(); 314 ++FuncInfo.InsertPt; 315 } else 316 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI(); 317 318 // Now skip past any EH_LABELs, which must remain at the beginning. 319 while (FuncInfo.InsertPt != FuncInfo.MBB->end() && 320 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL) 321 ++FuncInfo.InsertPt; 322 } 323 324 void FastISel::removeDeadCode(MachineBasicBlock::iterator I, 325 MachineBasicBlock::iterator E) { 326 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!"); 327 while (I != E) { 328 MachineInstr *Dead = &*I; 329 ++I; 330 Dead->eraseFromParent(); 331 ++NumFastIselDead; 332 } 333 recomputeInsertPt(); 334 } 335 336 FastISel::SavePoint FastISel::enterLocalValueArea() { 337 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt; 338 DebugLoc OldDL = DbgLoc; 339 recomputeInsertPt(); 340 DbgLoc = DebugLoc(); 341 SavePoint SP = { OldInsertPt, OldDL }; 342 return SP; 343 } 344 345 void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) { 346 if (FuncInfo.InsertPt != FuncInfo.MBB->begin()) 347 LastLocalValue = std::prev(FuncInfo.InsertPt); 348 349 // Restore the previous insert position. 350 FuncInfo.InsertPt = OldInsertPt.InsertPt; 351 DbgLoc = OldInsertPt.DL; 352 } 353 354 /// SelectBinaryOp - Select and emit code for a binary operator instruction, 355 /// which has an opcode which directly corresponds to the given ISD opcode. 356 /// 357 bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) { 358 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); 359 if (VT == MVT::Other || !VT.isSimple()) 360 // Unhandled type. Halt "fast" selection and bail. 361 return false; 362 363 // We only handle legal types. For example, on x86-32 the instruction 364 // selector contains all of the 64-bit instructions from x86-64, 365 // under the assumption that i64 won't be used if the target doesn't 366 // support it. 367 if (!TLI.isTypeLegal(VT)) { 368 // MVT::i1 is special. Allow AND, OR, or XOR because they 369 // don't require additional zeroing, which makes them easy. 370 if (VT == MVT::i1 && 371 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || 372 ISDOpcode == ISD::XOR)) 373 VT = TLI.getTypeToTransformTo(I->getContext(), VT); 374 else 375 return false; 376 } 377 378 // Check if the first operand is a constant, and handle it as "ri". At -O0, 379 // we don't have anything that canonicalizes operand order. 380 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0))) 381 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) { 382 unsigned Op1 = getRegForValue(I->getOperand(1)); 383 if (Op1 == 0) return false; 384 385 bool Op1IsKill = hasTrivialKill(I->getOperand(1)); 386 387 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, 388 Op1IsKill, CI->getZExtValue(), 389 VT.getSimpleVT()); 390 if (ResultReg == 0) return false; 391 392 // We successfully emitted code for the given LLVM Instruction. 393 UpdateValueMap(I, ResultReg); 394 return true; 395 } 396 397 398 unsigned Op0 = getRegForValue(I->getOperand(0)); 399 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail. 400 return false; 401 402 bool Op0IsKill = hasTrivialKill(I->getOperand(0)); 403 404 // Check if the second operand is a constant and handle it appropriately. 405 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 406 uint64_t Imm = CI->getZExtValue(); 407 408 // Transform "sdiv exact X, 8" -> "sra X, 3". 409 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) && 410 cast<BinaryOperator>(I)->isExact() && 411 isPowerOf2_64(Imm)) { 412 Imm = Log2_64(Imm); 413 ISDOpcode = ISD::SRA; 414 } 415 416 // Transform "urem x, pow2" -> "and x, pow2-1". 417 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) && 418 isPowerOf2_64(Imm)) { 419 --Imm; 420 ISDOpcode = ISD::AND; 421 } 422 423 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, 424 Op0IsKill, Imm, VT.getSimpleVT()); 425 if (ResultReg == 0) return false; 426 427 // We successfully emitted code for the given LLVM Instruction. 428 UpdateValueMap(I, ResultReg); 429 return true; 430 } 431 432 // Check if the second operand is a constant float. 433 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { 434 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), 435 ISDOpcode, Op0, Op0IsKill, CF); 436 if (ResultReg != 0) { 437 // We successfully emitted code for the given LLVM Instruction. 438 UpdateValueMap(I, ResultReg); 439 return true; 440 } 441 } 442 443 unsigned Op1 = getRegForValue(I->getOperand(1)); 444 if (Op1 == 0) 445 // Unhandled operand. Halt "fast" selection and bail. 446 return false; 447 448 bool Op1IsKill = hasTrivialKill(I->getOperand(1)); 449 450 // Now we have both operands in registers. Emit the instruction. 451 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), 452 ISDOpcode, 453 Op0, Op0IsKill, 454 Op1, Op1IsKill); 455 if (ResultReg == 0) 456 // Target-specific code wasn't able to find a machine opcode for 457 // the given ISD opcode and type. Halt "fast" selection and bail. 458 return false; 459 460 // We successfully emitted code for the given LLVM Instruction. 461 UpdateValueMap(I, ResultReg); 462 return true; 463 } 464 465 bool FastISel::SelectGetElementPtr(const User *I) { 466 unsigned N = getRegForValue(I->getOperand(0)); 467 if (N == 0) 468 // Unhandled operand. Halt "fast" selection and bail. 469 return false; 470 471 bool NIsKill = hasTrivialKill(I->getOperand(0)); 472 473 // Keep a running tab of the total offset to coalesce multiple N = N + Offset 474 // into a single N = N + TotalOffset. 475 uint64_t TotalOffs = 0; 476 // FIXME: What's a good SWAG number for MaxOffs? 477 uint64_t MaxOffs = 2048; 478 Type *Ty = I->getOperand(0)->getType(); 479 MVT VT = TLI.getPointerTy(); 480 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1, 481 E = I->op_end(); OI != E; ++OI) { 482 const Value *Idx = *OI; 483 if (StructType *StTy = dyn_cast<StructType>(Ty)) { 484 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); 485 if (Field) { 486 // N = N + Offset 487 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field); 488 if (TotalOffs >= MaxOffs) { 489 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); 490 if (N == 0) 491 // Unhandled operand. Halt "fast" selection and bail. 492 return false; 493 NIsKill = true; 494 TotalOffs = 0; 495 } 496 } 497 Ty = StTy->getElementType(Field); 498 } else { 499 Ty = cast<SequentialType>(Ty)->getElementType(); 500 501 // If this is a constant subscript, handle it quickly. 502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { 503 if (CI->isZero()) continue; 504 // N = N + Offset 505 TotalOffs += 506 DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); 507 if (TotalOffs >= MaxOffs) { 508 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); 509 if (N == 0) 510 // Unhandled operand. Halt "fast" selection and bail. 511 return false; 512 NIsKill = true; 513 TotalOffs = 0; 514 } 515 continue; 516 } 517 if (TotalOffs) { 518 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); 519 if (N == 0) 520 // Unhandled operand. Halt "fast" selection and bail. 521 return false; 522 NIsKill = true; 523 TotalOffs = 0; 524 } 525 526 // N = N + Idx * ElementSize; 527 uint64_t ElementSize = DL.getTypeAllocSize(Ty); 528 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx); 529 unsigned IdxN = Pair.first; 530 bool IdxNIsKill = Pair.second; 531 if (IdxN == 0) 532 // Unhandled operand. Halt "fast" selection and bail. 533 return false; 534 535 if (ElementSize != 1) { 536 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT); 537 if (IdxN == 0) 538 // Unhandled operand. Halt "fast" selection and bail. 539 return false; 540 IdxNIsKill = true; 541 } 542 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill); 543 if (N == 0) 544 // Unhandled operand. Halt "fast" selection and bail. 545 return false; 546 } 547 } 548 if (TotalOffs) { 549 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); 550 if (N == 0) 551 // Unhandled operand. Halt "fast" selection and bail. 552 return false; 553 } 554 555 // We successfully emitted code for the given LLVM Instruction. 556 UpdateValueMap(I, N); 557 return true; 558 } 559 560 bool FastISel::SelectCall(const User *I) { 561 const CallInst *Call = cast<CallInst>(I); 562 563 // Handle simple inline asms. 564 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) { 565 // Don't attempt to handle constraints. 566 if (!IA->getConstraintString().empty()) 567 return false; 568 569 unsigned ExtraInfo = 0; 570 if (IA->hasSideEffects()) 571 ExtraInfo |= InlineAsm::Extra_HasSideEffects; 572 if (IA->isAlignStack()) 573 ExtraInfo |= InlineAsm::Extra_IsAlignStack; 574 575 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 576 TII.get(TargetOpcode::INLINEASM)) 577 .addExternalSymbol(IA->getAsmString().c_str()) 578 .addImm(ExtraInfo); 579 return true; 580 } 581 582 MachineModuleInfo &MMI = FuncInfo.MF->getMMI(); 583 ComputeUsesVAFloatArgument(*Call, &MMI); 584 585 const Function *F = Call->getCalledFunction(); 586 if (!F) return false; 587 588 // Handle selected intrinsic function calls. 589 switch (F->getIntrinsicID()) { 590 default: break; 591 // At -O0 we don't care about the lifetime intrinsics. 592 case Intrinsic::lifetime_start: 593 case Intrinsic::lifetime_end: 594 // The donothing intrinsic does, well, nothing. 595 case Intrinsic::donothing: 596 return true; 597 598 case Intrinsic::dbg_declare: { 599 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call); 600 DIVariable DIVar(DI->getVariable()); 601 assert((!DIVar || DIVar.isVariable()) && 602 "Variable in DbgDeclareInst should be either null or a DIVariable."); 603 if (!DIVar || 604 !FuncInfo.MF->getMMI().hasDebugInfo()) { 605 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 606 return true; 607 } 608 609 const Value *Address = DI->getAddress(); 610 if (!Address || isa<UndefValue>(Address)) { 611 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 612 return true; 613 } 614 615 unsigned Offset = 0; 616 Optional<MachineOperand> Op; 617 if (const Argument *Arg = dyn_cast<Argument>(Address)) 618 // Some arguments' frame index is recorded during argument lowering. 619 Offset = FuncInfo.getArgumentFrameIndex(Arg); 620 if (Offset) 621 Op = MachineOperand::CreateFI(Offset); 622 if (!Op) 623 if (unsigned Reg = lookUpRegForValue(Address)) 624 Op = MachineOperand::CreateReg(Reg, false); 625 626 // If we have a VLA that has a "use" in a metadata node that's then used 627 // here but it has no other uses, then we have a problem. E.g., 628 // 629 // int foo (const int *x) { 630 // char a[*x]; 631 // return 0; 632 // } 633 // 634 // If we assign 'a' a vreg and fast isel later on has to use the selection 635 // DAG isel, it will want to copy the value to the vreg. However, there are 636 // no uses, which goes counter to what selection DAG isel expects. 637 if (!Op && !Address->use_empty() && isa<Instruction>(Address) && 638 (!isa<AllocaInst>(Address) || 639 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address)))) 640 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address), 641 false); 642 643 if (Op) { 644 if (Op->isReg()) { 645 Op->setIsDebug(true); 646 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 647 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0, 648 DI->getVariable()); 649 } else 650 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 651 TII.get(TargetOpcode::DBG_VALUE)) 652 .addOperand(*Op) 653 .addImm(0) 654 .addMetadata(DI->getVariable()); 655 } else { 656 // We can't yet handle anything else here because it would require 657 // generating code, thus altering codegen because of debug info. 658 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 659 } 660 return true; 661 } 662 case Intrinsic::dbg_value: { 663 // This form of DBG_VALUE is target-independent. 664 const DbgValueInst *DI = cast<DbgValueInst>(Call); 665 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); 666 const Value *V = DI->getValue(); 667 if (!V) { 668 // Currently the optimizer can produce this; insert an undef to 669 // help debugging. Probably the optimizer should not do this. 670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 671 .addReg(0U).addImm(DI->getOffset()) 672 .addMetadata(DI->getVariable()); 673 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 674 if (CI->getBitWidth() > 64) 675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 676 .addCImm(CI).addImm(DI->getOffset()) 677 .addMetadata(DI->getVariable()); 678 else 679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 680 .addImm(CI->getZExtValue()).addImm(DI->getOffset()) 681 .addMetadata(DI->getVariable()); 682 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { 683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 684 .addFPImm(CF).addImm(DI->getOffset()) 685 .addMetadata(DI->getVariable()); 686 } else if (unsigned Reg = lookUpRegForValue(V)) { 687 // FIXME: This does not handle register-indirect values at offset 0. 688 bool IsIndirect = DI->getOffset() != 0; 689 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, 690 Reg, DI->getOffset(), DI->getVariable()); 691 } else { 692 // We can't yet handle anything else here because it would require 693 // generating code, thus altering codegen because of debug info. 694 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); 695 } 696 return true; 697 } 698 case Intrinsic::objectsize: { 699 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1)); 700 unsigned long long Res = CI->isZero() ? -1ULL : 0; 701 Constant *ResCI = ConstantInt::get(Call->getType(), Res); 702 unsigned ResultReg = getRegForValue(ResCI); 703 if (ResultReg == 0) 704 return false; 705 UpdateValueMap(Call, ResultReg); 706 return true; 707 } 708 case Intrinsic::expect: { 709 unsigned ResultReg = getRegForValue(Call->getArgOperand(0)); 710 if (ResultReg == 0) 711 return false; 712 UpdateValueMap(Call, ResultReg); 713 return true; 714 } 715 } 716 717 // Usually, it does not make sense to initialize a value, 718 // make an unrelated function call and use the value, because 719 // it tends to be spilled on the stack. So, we move the pointer 720 // to the last local value to the beginning of the block, so that 721 // all the values which have already been materialized, 722 // appear after the call. It also makes sense to skip intrinsics 723 // since they tend to be inlined. 724 if (!isa<IntrinsicInst>(Call)) 725 flushLocalValueMap(); 726 727 // An arbitrary call. Bail. 728 return false; 729 } 730 731 bool FastISel::SelectCast(const User *I, unsigned Opcode) { 732 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 733 EVT DstVT = TLI.getValueType(I->getType()); 734 735 if (SrcVT == MVT::Other || !SrcVT.isSimple() || 736 DstVT == MVT::Other || !DstVT.isSimple()) 737 // Unhandled type. Halt "fast" selection and bail. 738 return false; 739 740 // Check if the destination type is legal. 741 if (!TLI.isTypeLegal(DstVT)) 742 return false; 743 744 // Check if the source operand is legal. 745 if (!TLI.isTypeLegal(SrcVT)) 746 return false; 747 748 unsigned InputReg = getRegForValue(I->getOperand(0)); 749 if (!InputReg) 750 // Unhandled operand. Halt "fast" selection and bail. 751 return false; 752 753 bool InputRegIsKill = hasTrivialKill(I->getOperand(0)); 754 755 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), 756 DstVT.getSimpleVT(), 757 Opcode, 758 InputReg, InputRegIsKill); 759 if (!ResultReg) 760 return false; 761 762 UpdateValueMap(I, ResultReg); 763 return true; 764 } 765 766 bool FastISel::SelectBitCast(const User *I) { 767 // If the bitcast doesn't change the type, just use the operand value. 768 if (I->getType() == I->getOperand(0)->getType()) { 769 unsigned Reg = getRegForValue(I->getOperand(0)); 770 if (Reg == 0) 771 return false; 772 UpdateValueMap(I, Reg); 773 return true; 774 } 775 776 // Bitcasts of other values become reg-reg copies or BITCAST operators. 777 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType()); 778 EVT DstEVT = TLI.getValueType(I->getType()); 779 if (SrcEVT == MVT::Other || DstEVT == MVT::Other || 780 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT)) 781 // Unhandled type. Halt "fast" selection and bail. 782 return false; 783 784 MVT SrcVT = SrcEVT.getSimpleVT(); 785 MVT DstVT = DstEVT.getSimpleVT(); 786 unsigned Op0 = getRegForValue(I->getOperand(0)); 787 if (Op0 == 0) 788 // Unhandled operand. Halt "fast" selection and bail. 789 return false; 790 791 bool Op0IsKill = hasTrivialKill(I->getOperand(0)); 792 793 // First, try to perform the bitcast by inserting a reg-reg copy. 794 unsigned ResultReg = 0; 795 if (SrcVT == DstVT) { 796 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); 797 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); 798 // Don't attempt a cross-class copy. It will likely fail. 799 if (SrcClass == DstClass) { 800 ResultReg = createResultReg(DstClass); 801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 802 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0); 803 } 804 } 805 806 // If the reg-reg copy failed, select a BITCAST opcode. 807 if (!ResultReg) 808 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill); 809 810 if (!ResultReg) 811 return false; 812 813 UpdateValueMap(I, ResultReg); 814 return true; 815 } 816 817 bool 818 FastISel::SelectInstruction(const Instruction *I) { 819 // Just before the terminator instruction, insert instructions to 820 // feed PHI nodes in successor blocks. 821 if (isa<TerminatorInst>(I)) 822 if (!HandlePHINodesInSuccessorBlocks(I->getParent())) 823 return false; 824 825 DbgLoc = I->getDebugLoc(); 826 827 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt; 828 829 if (const CallInst *Call = dyn_cast<CallInst>(I)) { 830 const Function *F = Call->getCalledFunction(); 831 LibFunc::Func Func; 832 833 // As a special case, don't handle calls to builtin library functions that 834 // may be translated directly to target instructions. 835 if (F && !F->hasLocalLinkage() && F->hasName() && 836 LibInfo->getLibFunc(F->getName(), Func) && 837 LibInfo->hasOptimizedCodeGen(Func)) 838 return false; 839 840 // Don't handle Intrinsic::trap if a trap funciton is specified. 841 if (F && F->getIntrinsicID() == Intrinsic::trap && 842 !TM.Options.getTrapFunctionName().empty()) 843 return false; 844 } 845 846 // First, try doing target-independent selection. 847 if (SelectOperator(I, I->getOpcode())) { 848 ++NumFastIselSuccessIndependent; 849 DbgLoc = DebugLoc(); 850 return true; 851 } 852 // Remove dead code. However, ignore call instructions since we've flushed 853 // the local value map and recomputed the insert point. 854 if (!isa<CallInst>(I)) { 855 recomputeInsertPt(); 856 if (SavedInsertPt != FuncInfo.InsertPt) 857 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); 858 } 859 860 // Next, try calling the target to attempt to handle the instruction. 861 SavedInsertPt = FuncInfo.InsertPt; 862 if (TargetSelectInstruction(I)) { 863 ++NumFastIselSuccessTarget; 864 DbgLoc = DebugLoc(); 865 return true; 866 } 867 // Check for dead code and remove as necessary. 868 recomputeInsertPt(); 869 if (SavedInsertPt != FuncInfo.InsertPt) 870 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); 871 872 DbgLoc = DebugLoc(); 873 return false; 874 } 875 876 /// FastEmitBranch - Emit an unconditional branch to the given block, 877 /// unless it is the immediate (fall-through) successor, and update 878 /// the CFG. 879 void 880 FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) { 881 882 if (FuncInfo.MBB->getBasicBlock()->size() > 1 && 883 FuncInfo.MBB->isLayoutSuccessor(MSucc)) { 884 // For more accurate line information if this is the only instruction 885 // in the block then emit it, otherwise we have the unconditional 886 // fall-through case, which needs no instructions. 887 } else { 888 // The unconditional branch case. 889 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr, 890 SmallVector<MachineOperand, 0>(), DbgLoc); 891 } 892 FuncInfo.MBB->addSuccessor(MSucc); 893 } 894 895 /// SelectFNeg - Emit an FNeg operation. 896 /// 897 bool 898 FastISel::SelectFNeg(const User *I) { 899 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); 900 if (OpReg == 0) return false; 901 902 bool OpRegIsKill = hasTrivialKill(I); 903 904 // If the target has ISD::FNEG, use it. 905 EVT VT = TLI.getValueType(I->getType()); 906 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), 907 ISD::FNEG, OpReg, OpRegIsKill); 908 if (ResultReg != 0) { 909 UpdateValueMap(I, ResultReg); 910 return true; 911 } 912 913 // Bitcast the value to integer, twiddle the sign bit with xor, 914 // and then bitcast it back to floating-point. 915 if (VT.getSizeInBits() > 64) return false; 916 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); 917 if (!TLI.isTypeLegal(IntVT)) 918 return false; 919 920 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), 921 ISD::BITCAST, OpReg, OpRegIsKill); 922 if (IntReg == 0) 923 return false; 924 925 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, 926 IntReg, /*Kill=*/true, 927 UINT64_C(1) << (VT.getSizeInBits()-1), 928 IntVT.getSimpleVT()); 929 if (IntResultReg == 0) 930 return false; 931 932 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), 933 ISD::BITCAST, IntResultReg, /*Kill=*/true); 934 if (ResultReg == 0) 935 return false; 936 937 UpdateValueMap(I, ResultReg); 938 return true; 939 } 940 941 bool 942 FastISel::SelectExtractValue(const User *U) { 943 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); 944 if (!EVI) 945 return false; 946 947 // Make sure we only try to handle extracts with a legal result. But also 948 // allow i1 because it's easy. 949 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true); 950 if (!RealVT.isSimple()) 951 return false; 952 MVT VT = RealVT.getSimpleVT(); 953 if (!TLI.isTypeLegal(VT) && VT != MVT::i1) 954 return false; 955 956 const Value *Op0 = EVI->getOperand(0); 957 Type *AggTy = Op0->getType(); 958 959 // Get the base result register. 960 unsigned ResultReg; 961 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0); 962 if (I != FuncInfo.ValueMap.end()) 963 ResultReg = I->second; 964 else if (isa<Instruction>(Op0)) 965 ResultReg = FuncInfo.InitializeRegForValue(Op0); 966 else 967 return false; // fast-isel can't handle aggregate constants at the moment 968 969 // Get the actual result register, which is an offset from the base register. 970 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices()); 971 972 SmallVector<EVT, 4> AggValueVTs; 973 ComputeValueVTs(TLI, AggTy, AggValueVTs); 974 975 for (unsigned i = 0; i < VTIndex; i++) 976 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]); 977 978 UpdateValueMap(EVI, ResultReg); 979 return true; 980 } 981 982 bool 983 FastISel::SelectOperator(const User *I, unsigned Opcode) { 984 switch (Opcode) { 985 case Instruction::Add: 986 return SelectBinaryOp(I, ISD::ADD); 987 case Instruction::FAdd: 988 return SelectBinaryOp(I, ISD::FADD); 989 case Instruction::Sub: 990 return SelectBinaryOp(I, ISD::SUB); 991 case Instruction::FSub: 992 // FNeg is currently represented in LLVM IR as a special case of FSub. 993 if (BinaryOperator::isFNeg(I)) 994 return SelectFNeg(I); 995 return SelectBinaryOp(I, ISD::FSUB); 996 case Instruction::Mul: 997 return SelectBinaryOp(I, ISD::MUL); 998 case Instruction::FMul: 999 return SelectBinaryOp(I, ISD::FMUL); 1000 case Instruction::SDiv: 1001 return SelectBinaryOp(I, ISD::SDIV); 1002 case Instruction::UDiv: 1003 return SelectBinaryOp(I, ISD::UDIV); 1004 case Instruction::FDiv: 1005 return SelectBinaryOp(I, ISD::FDIV); 1006 case Instruction::SRem: 1007 return SelectBinaryOp(I, ISD::SREM); 1008 case Instruction::URem: 1009 return SelectBinaryOp(I, ISD::UREM); 1010 case Instruction::FRem: 1011 return SelectBinaryOp(I, ISD::FREM); 1012 case Instruction::Shl: 1013 return SelectBinaryOp(I, ISD::SHL); 1014 case Instruction::LShr: 1015 return SelectBinaryOp(I, ISD::SRL); 1016 case Instruction::AShr: 1017 return SelectBinaryOp(I, ISD::SRA); 1018 case Instruction::And: 1019 return SelectBinaryOp(I, ISD::AND); 1020 case Instruction::Or: 1021 return SelectBinaryOp(I, ISD::OR); 1022 case Instruction::Xor: 1023 return SelectBinaryOp(I, ISD::XOR); 1024 1025 case Instruction::GetElementPtr: 1026 return SelectGetElementPtr(I); 1027 1028 case Instruction::Br: { 1029 const BranchInst *BI = cast<BranchInst>(I); 1030 1031 if (BI->isUnconditional()) { 1032 const BasicBlock *LLVMSucc = BI->getSuccessor(0); 1033 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc]; 1034 FastEmitBranch(MSucc, BI->getDebugLoc()); 1035 return true; 1036 } 1037 1038 // Conditional branches are not handed yet. 1039 // Halt "fast" selection and bail. 1040 return false; 1041 } 1042 1043 case Instruction::Unreachable: 1044 // Nothing to emit. 1045 return true; 1046 1047 case Instruction::Alloca: 1048 // FunctionLowering has the static-sized case covered. 1049 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I))) 1050 return true; 1051 1052 // Dynamic-sized alloca is not handled yet. 1053 return false; 1054 1055 case Instruction::Call: 1056 return SelectCall(I); 1057 1058 case Instruction::BitCast: 1059 return SelectBitCast(I); 1060 1061 case Instruction::FPToSI: 1062 return SelectCast(I, ISD::FP_TO_SINT); 1063 case Instruction::ZExt: 1064 return SelectCast(I, ISD::ZERO_EXTEND); 1065 case Instruction::SExt: 1066 return SelectCast(I, ISD::SIGN_EXTEND); 1067 case Instruction::Trunc: 1068 return SelectCast(I, ISD::TRUNCATE); 1069 case Instruction::SIToFP: 1070 return SelectCast(I, ISD::SINT_TO_FP); 1071 1072 case Instruction::IntToPtr: // Deliberate fall-through. 1073 case Instruction::PtrToInt: { 1074 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); 1075 EVT DstVT = TLI.getValueType(I->getType()); 1076 if (DstVT.bitsGT(SrcVT)) 1077 return SelectCast(I, ISD::ZERO_EXTEND); 1078 if (DstVT.bitsLT(SrcVT)) 1079 return SelectCast(I, ISD::TRUNCATE); 1080 unsigned Reg = getRegForValue(I->getOperand(0)); 1081 if (Reg == 0) return false; 1082 UpdateValueMap(I, Reg); 1083 return true; 1084 } 1085 1086 case Instruction::ExtractValue: 1087 return SelectExtractValue(I); 1088 1089 case Instruction::PHI: 1090 llvm_unreachable("FastISel shouldn't visit PHI nodes!"); 1091 1092 default: 1093 // Unhandled instruction. Halt "fast" selection and bail. 1094 return false; 1095 } 1096 } 1097 1098 FastISel::FastISel(FunctionLoweringInfo &funcInfo, 1099 const TargetLibraryInfo *libInfo) 1100 : FuncInfo(funcInfo), 1101 MRI(FuncInfo.MF->getRegInfo()), 1102 MFI(*FuncInfo.MF->getFrameInfo()), 1103 MCP(*FuncInfo.MF->getConstantPool()), 1104 TM(FuncInfo.MF->getTarget()), 1105 DL(*TM.getDataLayout()), 1106 TII(*TM.getInstrInfo()), 1107 TLI(*TM.getTargetLowering()), 1108 TRI(*TM.getRegisterInfo()), 1109 LibInfo(libInfo) { 1110 } 1111 1112 FastISel::~FastISel() {} 1113 1114 bool FastISel::FastLowerArguments() { 1115 return false; 1116 } 1117 1118 unsigned FastISel::FastEmit_(MVT, MVT, 1119 unsigned) { 1120 return 0; 1121 } 1122 1123 unsigned FastISel::FastEmit_r(MVT, MVT, 1124 unsigned, 1125 unsigned /*Op0*/, bool /*Op0IsKill*/) { 1126 return 0; 1127 } 1128 1129 unsigned FastISel::FastEmit_rr(MVT, MVT, 1130 unsigned, 1131 unsigned /*Op0*/, bool /*Op0IsKill*/, 1132 unsigned /*Op1*/, bool /*Op1IsKill*/) { 1133 return 0; 1134 } 1135 1136 unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { 1137 return 0; 1138 } 1139 1140 unsigned FastISel::FastEmit_f(MVT, MVT, 1141 unsigned, const ConstantFP * /*FPImm*/) { 1142 return 0; 1143 } 1144 1145 unsigned FastISel::FastEmit_ri(MVT, MVT, 1146 unsigned, 1147 unsigned /*Op0*/, bool /*Op0IsKill*/, 1148 uint64_t /*Imm*/) { 1149 return 0; 1150 } 1151 1152 unsigned FastISel::FastEmit_rf(MVT, MVT, 1153 unsigned, 1154 unsigned /*Op0*/, bool /*Op0IsKill*/, 1155 const ConstantFP * /*FPImm*/) { 1156 return 0; 1157 } 1158 1159 unsigned FastISel::FastEmit_rri(MVT, MVT, 1160 unsigned, 1161 unsigned /*Op0*/, bool /*Op0IsKill*/, 1162 unsigned /*Op1*/, bool /*Op1IsKill*/, 1163 uint64_t /*Imm*/) { 1164 return 0; 1165 } 1166 1167 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries 1168 /// to emit an instruction with an immediate operand using FastEmit_ri. 1169 /// If that fails, it materializes the immediate into a register and try 1170 /// FastEmit_rr instead. 1171 unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode, 1172 unsigned Op0, bool Op0IsKill, 1173 uint64_t Imm, MVT ImmType) { 1174 // If this is a multiply by a power of two, emit this as a shift left. 1175 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) { 1176 Opcode = ISD::SHL; 1177 Imm = Log2_64(Imm); 1178 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) { 1179 // div x, 8 -> srl x, 3 1180 Opcode = ISD::SRL; 1181 Imm = Log2_64(Imm); 1182 } 1183 1184 // Horrible hack (to be removed), check to make sure shift amounts are 1185 // in-range. 1186 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) && 1187 Imm >= VT.getSizeInBits()) 1188 return 0; 1189 1190 // First check if immediate type is legal. If not, we can't use the ri form. 1191 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm); 1192 if (ResultReg != 0) 1193 return ResultReg; 1194 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); 1195 if (MaterialReg == 0) { 1196 // This is a bit ugly/slow, but failing here means falling out of 1197 // fast-isel, which would be very slow. 1198 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(), 1199 VT.getSizeInBits()); 1200 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm)); 1201 assert (MaterialReg != 0 && "Unable to materialize imm."); 1202 if (MaterialReg == 0) return 0; 1203 } 1204 return FastEmit_rr(VT, VT, Opcode, 1205 Op0, Op0IsKill, 1206 MaterialReg, /*Kill=*/true); 1207 } 1208 1209 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { 1210 return MRI.createVirtualRegister(RC); 1211 } 1212 1213 unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II, 1214 unsigned Op, unsigned OpNum) { 1215 if (TargetRegisterInfo::isVirtualRegister(Op)) { 1216 const TargetRegisterClass *RegClass = 1217 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF); 1218 if (!MRI.constrainRegClass(Op, RegClass)) { 1219 // If it's not legal to COPY between the register classes, something 1220 // has gone very wrong before we got here. 1221 unsigned NewOp = createResultReg(RegClass); 1222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1223 TII.get(TargetOpcode::COPY), NewOp).addReg(Op); 1224 return NewOp; 1225 } 1226 } 1227 return Op; 1228 } 1229 1230 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, 1231 const TargetRegisterClass* RC) { 1232 unsigned ResultReg = createResultReg(RC); 1233 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1234 1235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg); 1236 return ResultReg; 1237 } 1238 1239 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, 1240 const TargetRegisterClass *RC, 1241 unsigned Op0, bool Op0IsKill) { 1242 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1243 1244 unsigned ResultReg = createResultReg(RC); 1245 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1246 1247 if (II.getNumDefs() >= 1) 1248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1249 .addReg(Op0, Op0IsKill * RegState::Kill); 1250 else { 1251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1252 .addReg(Op0, Op0IsKill * RegState::Kill); 1253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1254 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1255 } 1256 1257 return ResultReg; 1258 } 1259 1260 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, 1261 const TargetRegisterClass *RC, 1262 unsigned Op0, bool Op0IsKill, 1263 unsigned Op1, bool Op1IsKill) { 1264 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1265 1266 unsigned ResultReg = createResultReg(RC); 1267 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1268 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1269 1270 if (II.getNumDefs() >= 1) 1271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1272 .addReg(Op0, Op0IsKill * RegState::Kill) 1273 .addReg(Op1, Op1IsKill * RegState::Kill); 1274 else { 1275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1276 .addReg(Op0, Op0IsKill * RegState::Kill) 1277 .addReg(Op1, Op1IsKill * RegState::Kill); 1278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1279 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1280 } 1281 return ResultReg; 1282 } 1283 1284 unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, 1285 const TargetRegisterClass *RC, 1286 unsigned Op0, bool Op0IsKill, 1287 unsigned Op1, bool Op1IsKill, 1288 unsigned Op2, bool Op2IsKill) { 1289 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1290 1291 unsigned ResultReg = createResultReg(RC); 1292 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1293 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1294 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2); 1295 1296 if (II.getNumDefs() >= 1) 1297 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1298 .addReg(Op0, Op0IsKill * RegState::Kill) 1299 .addReg(Op1, Op1IsKill * RegState::Kill) 1300 .addReg(Op2, Op2IsKill * RegState::Kill); 1301 else { 1302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1303 .addReg(Op0, Op0IsKill * RegState::Kill) 1304 .addReg(Op1, Op1IsKill * RegState::Kill) 1305 .addReg(Op2, Op2IsKill * RegState::Kill); 1306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1307 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1308 } 1309 return ResultReg; 1310 } 1311 1312 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, 1313 const TargetRegisterClass *RC, 1314 unsigned Op0, bool Op0IsKill, 1315 uint64_t Imm) { 1316 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1317 1318 unsigned ResultReg = createResultReg(RC); 1319 RC = TII.getRegClass(II, II.getNumDefs(), &TRI, *FuncInfo.MF); 1320 MRI.constrainRegClass(Op0, RC); 1321 1322 if (II.getNumDefs() >= 1) 1323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1324 .addReg(Op0, Op0IsKill * RegState::Kill) 1325 .addImm(Imm); 1326 else { 1327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1328 .addReg(Op0, Op0IsKill * RegState::Kill) 1329 .addImm(Imm); 1330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1331 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1332 } 1333 return ResultReg; 1334 } 1335 1336 unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode, 1337 const TargetRegisterClass *RC, 1338 unsigned Op0, bool Op0IsKill, 1339 uint64_t Imm1, uint64_t Imm2) { 1340 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1341 1342 unsigned ResultReg = createResultReg(RC); 1343 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1344 1345 if (II.getNumDefs() >= 1) 1346 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1347 .addReg(Op0, Op0IsKill * RegState::Kill) 1348 .addImm(Imm1) 1349 .addImm(Imm2); 1350 else { 1351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1352 .addReg(Op0, Op0IsKill * RegState::Kill) 1353 .addImm(Imm1) 1354 .addImm(Imm2); 1355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1356 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1357 } 1358 return ResultReg; 1359 } 1360 1361 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, 1362 const TargetRegisterClass *RC, 1363 unsigned Op0, bool Op0IsKill, 1364 const ConstantFP *FPImm) { 1365 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1366 1367 unsigned ResultReg = createResultReg(RC); 1368 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1369 1370 if (II.getNumDefs() >= 1) 1371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1372 .addReg(Op0, Op0IsKill * RegState::Kill) 1373 .addFPImm(FPImm); 1374 else { 1375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1376 .addReg(Op0, Op0IsKill * RegState::Kill) 1377 .addFPImm(FPImm); 1378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1379 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1380 } 1381 return ResultReg; 1382 } 1383 1384 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, 1385 const TargetRegisterClass *RC, 1386 unsigned Op0, bool Op0IsKill, 1387 unsigned Op1, bool Op1IsKill, 1388 uint64_t Imm) { 1389 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1390 1391 unsigned ResultReg = createResultReg(RC); 1392 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1393 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1394 1395 if (II.getNumDefs() >= 1) 1396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1397 .addReg(Op0, Op0IsKill * RegState::Kill) 1398 .addReg(Op1, Op1IsKill * RegState::Kill) 1399 .addImm(Imm); 1400 else { 1401 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1402 .addReg(Op0, Op0IsKill * RegState::Kill) 1403 .addReg(Op1, Op1IsKill * RegState::Kill) 1404 .addImm(Imm); 1405 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1406 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1407 } 1408 return ResultReg; 1409 } 1410 1411 unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode, 1412 const TargetRegisterClass *RC, 1413 unsigned Op0, bool Op0IsKill, 1414 unsigned Op1, bool Op1IsKill, 1415 uint64_t Imm1, uint64_t Imm2) { 1416 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1417 1418 unsigned ResultReg = createResultReg(RC); 1419 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1420 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1421 1422 if (II.getNumDefs() >= 1) 1423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1424 .addReg(Op0, Op0IsKill * RegState::Kill) 1425 .addReg(Op1, Op1IsKill * RegState::Kill) 1426 .addImm(Imm1).addImm(Imm2); 1427 else { 1428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) 1429 .addReg(Op0, Op0IsKill * RegState::Kill) 1430 .addReg(Op1, Op1IsKill * RegState::Kill) 1431 .addImm(Imm1).addImm(Imm2); 1432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1433 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1434 } 1435 return ResultReg; 1436 } 1437 1438 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, 1439 const TargetRegisterClass *RC, 1440 uint64_t Imm) { 1441 unsigned ResultReg = createResultReg(RC); 1442 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1443 1444 if (II.getNumDefs() >= 1) 1445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm); 1446 else { 1447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm); 1448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1449 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1450 } 1451 return ResultReg; 1452 } 1453 1454 unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode, 1455 const TargetRegisterClass *RC, 1456 uint64_t Imm1, uint64_t Imm2) { 1457 unsigned ResultReg = createResultReg(RC); 1458 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1459 1460 if (II.getNumDefs() >= 1) 1461 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1462 .addImm(Imm1).addImm(Imm2); 1463 else { 1464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2); 1465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1466 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); 1467 } 1468 return ResultReg; 1469 } 1470 1471 unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, 1472 unsigned Op0, bool Op0IsKill, 1473 uint32_t Idx) { 1474 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 1475 assert(TargetRegisterInfo::isVirtualRegister(Op0) && 1476 "Cannot yet extract from physregs"); 1477 const TargetRegisterClass *RC = MRI.getRegClass(Op0); 1478 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx)); 1479 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, 1480 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg) 1481 .addReg(Op0, getKillRegState(Op0IsKill), Idx); 1482 return ResultReg; 1483 } 1484 1485 /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op 1486 /// with all but the least significant bit set to zero. 1487 unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) { 1488 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1); 1489 } 1490 1491 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. 1492 /// Emit code to ensure constants are copied into registers when needed. 1493 /// Remember the virtual registers that need to be added to the Machine PHI 1494 /// nodes as input. We cannot just directly add them, because expansion 1495 /// might result in multiple MBB's for one BB. As such, the start of the 1496 /// BB might correspond to a different MBB than the end. 1497 bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { 1498 const TerminatorInst *TI = LLVMBB->getTerminator(); 1499 1500 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; 1501 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size(); 1502 1503 // Check successor nodes' PHI nodes that expect a constant to be available 1504 // from this block. 1505 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { 1506 const BasicBlock *SuccBB = TI->getSuccessor(succ); 1507 if (!isa<PHINode>(SuccBB->begin())) continue; 1508 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; 1509 1510 // If this terminator has multiple identical successors (common for 1511 // switches), only handle each succ once. 1512 if (!SuccsHandled.insert(SuccMBB)) continue; 1513 1514 MachineBasicBlock::iterator MBBI = SuccMBB->begin(); 1515 1516 // At this point we know that there is a 1-1 correspondence between LLVM PHI 1517 // nodes and Machine PHI nodes, but the incoming operands have not been 1518 // emitted yet. 1519 for (BasicBlock::const_iterator I = SuccBB->begin(); 1520 const PHINode *PN = dyn_cast<PHINode>(I); ++I) { 1521 1522 // Ignore dead phi's. 1523 if (PN->use_empty()) continue; 1524 1525 // Only handle legal types. Two interesting things to note here. First, 1526 // by bailing out early, we may leave behind some dead instructions, 1527 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its 1528 // own moves. Second, this check is necessary because FastISel doesn't 1529 // use CreateRegs to create registers, so it always creates 1530 // exactly one register for each non-void instruction. 1531 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); 1532 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { 1533 // Handle integer promotions, though, because they're common and easy. 1534 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 1535 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT); 1536 else { 1537 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); 1538 return false; 1539 } 1540 } 1541 1542 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); 1543 1544 // Set the DebugLoc for the copy. Prefer the location of the operand 1545 // if there is one; use the location of the PHI otherwise. 1546 DbgLoc = PN->getDebugLoc(); 1547 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp)) 1548 DbgLoc = Inst->getDebugLoc(); 1549 1550 unsigned Reg = getRegForValue(PHIOp); 1551 if (Reg == 0) { 1552 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); 1553 return false; 1554 } 1555 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); 1556 DbgLoc = DebugLoc(); 1557 } 1558 } 1559 1560 return true; 1561 } 1562 1563 bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) { 1564 assert(LI->hasOneUse() && 1565 "tryToFoldLoad expected a LoadInst with a single use"); 1566 // We know that the load has a single use, but don't know what it is. If it 1567 // isn't one of the folded instructions, then we can't succeed here. Handle 1568 // this by scanning the single-use users of the load until we get to FoldInst. 1569 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs. 1570 1571 const Instruction *TheUser = LI->user_back(); 1572 while (TheUser != FoldInst && // Scan up until we find FoldInst. 1573 // Stay in the right block. 1574 TheUser->getParent() == FoldInst->getParent() && 1575 --MaxUsers) { // Don't scan too far. 1576 // If there are multiple or no uses of this instruction, then bail out. 1577 if (!TheUser->hasOneUse()) 1578 return false; 1579 1580 TheUser = TheUser->user_back(); 1581 } 1582 1583 // If we didn't find the fold instruction, then we failed to collapse the 1584 // sequence. 1585 if (TheUser != FoldInst) 1586 return false; 1587 1588 // Don't try to fold volatile loads. Target has to deal with alignment 1589 // constraints. 1590 if (LI->isVolatile()) 1591 return false; 1592 1593 // Figure out which vreg this is going into. If there is no assigned vreg yet 1594 // then there actually was no reference to it. Perhaps the load is referenced 1595 // by a dead instruction. 1596 unsigned LoadReg = getRegForValue(LI); 1597 if (LoadReg == 0) 1598 return false; 1599 1600 // We can't fold if this vreg has no uses or more than one use. Multiple uses 1601 // may mean that the instruction got lowered to multiple MIs, or the use of 1602 // the loaded value ended up being multiple operands of the result. 1603 if (!MRI.hasOneUse(LoadReg)) 1604 return false; 1605 1606 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg); 1607 MachineInstr *User = RI->getParent(); 1608 1609 // Set the insertion point properly. Folding the load can cause generation of 1610 // other random instructions (like sign extends) for addressing modes; make 1611 // sure they get inserted in a logical place before the new instruction. 1612 FuncInfo.InsertPt = User; 1613 FuncInfo.MBB = User->getParent(); 1614 1615 // Ask the target to try folding the load. 1616 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI); 1617 } 1618 1619 bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) { 1620 // Must be an add. 1621 if (!isa<AddOperator>(Add)) 1622 return false; 1623 // Type size needs to match. 1624 if (DL.getTypeSizeInBits(GEP->getType()) != 1625 DL.getTypeSizeInBits(Add->getType())) 1626 return false; 1627 // Must be in the same basic block. 1628 if (isa<Instruction>(Add) && 1629 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB) 1630 return false; 1631 // Must have a constant operand. 1632 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1)); 1633 } 1634 1635