1 //===- Instructions.cpp - Implement the LLVM instructions -----------------===// 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 implements all of the non-inline methods for the LLVM instruction 10 // classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/IR/Instructions.h" 15 #include "LLVMContextImpl.h" 16 #include "llvm/ADT/None.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/IR/Attributes.h" 20 #include "llvm/IR/BasicBlock.h" 21 #include "llvm/IR/Constant.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/DerivedTypes.h" 25 #include "llvm/IR/Function.h" 26 #include "llvm/IR/InstrTypes.h" 27 #include "llvm/IR/Instruction.h" 28 #include "llvm/IR/Intrinsics.h" 29 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/MDBuilder.h" 31 #include "llvm/IR/Metadata.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/Operator.h" 34 #include "llvm/IR/Type.h" 35 #include "llvm/IR/Value.h" 36 #include "llvm/Support/AtomicOrdering.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/MathExtras.h" 40 #include "llvm/Support/TypeSize.h" 41 #include <algorithm> 42 #include <cassert> 43 #include <cstdint> 44 #include <vector> 45 46 using namespace llvm; 47 48 static cl::opt<bool> DisableI2pP2iOpt( 49 "disable-i2p-p2i-opt", cl::init(false), 50 cl::desc("Disables inttoptr/ptrtoint roundtrip optimization")); 51 52 //===----------------------------------------------------------------------===// 53 // AllocaInst Class 54 //===----------------------------------------------------------------------===// 55 56 Optional<TypeSize> 57 AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { 58 TypeSize Size = DL.getTypeAllocSizeInBits(getAllocatedType()); 59 if (isArrayAllocation()) { 60 auto *C = dyn_cast<ConstantInt>(getArraySize()); 61 if (!C) 62 return None; 63 assert(!Size.isScalable() && "Array elements cannot have a scalable size"); 64 Size *= C->getZExtValue(); 65 } 66 return Size; 67 } 68 69 //===----------------------------------------------------------------------===// 70 // SelectInst Class 71 //===----------------------------------------------------------------------===// 72 73 /// areInvalidOperands - Return a string if the specified operands are invalid 74 /// for a select operation, otherwise return null. 75 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { 76 if (Op1->getType() != Op2->getType()) 77 return "both values to select must have same type"; 78 79 if (Op1->getType()->isTokenTy()) 80 return "select values cannot have token type"; 81 82 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { 83 // Vector select. 84 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) 85 return "vector select condition element type must be i1"; 86 VectorType *ET = dyn_cast<VectorType>(Op1->getType()); 87 if (!ET) 88 return "selected values for vector select must be vectors"; 89 if (ET->getElementCount() != VT->getElementCount()) 90 return "vector select requires selected vectors to have " 91 "the same vector length as select condition"; 92 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { 93 return "select condition must be i1 or <n x i1>"; 94 } 95 return nullptr; 96 } 97 98 //===----------------------------------------------------------------------===// 99 // PHINode Class 100 //===----------------------------------------------------------------------===// 101 102 PHINode::PHINode(const PHINode &PN) 103 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), 104 ReservedSpace(PN.getNumOperands()) { 105 allocHungoffUses(PN.getNumOperands()); 106 std::copy(PN.op_begin(), PN.op_end(), op_begin()); 107 std::copy(PN.block_begin(), PN.block_end(), block_begin()); 108 SubclassOptionalData = PN.SubclassOptionalData; 109 } 110 111 // removeIncomingValue - Remove an incoming value. This is useful if a 112 // predecessor basic block is deleted. 113 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { 114 Value *Removed = getIncomingValue(Idx); 115 116 // Move everything after this operand down. 117 // 118 // FIXME: we could just swap with the end of the list, then erase. However, 119 // clients might not expect this to happen. The code as it is thrashes the 120 // use/def lists, which is kinda lame. 121 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); 122 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); 123 124 // Nuke the last value. 125 Op<-1>().set(nullptr); 126 setNumHungOffUseOperands(getNumOperands() - 1); 127 128 // If the PHI node is dead, because it has zero entries, nuke it now. 129 if (getNumOperands() == 0 && DeletePHIIfEmpty) { 130 // If anyone is using this PHI, make them use a dummy value instead... 131 replaceAllUsesWith(UndefValue::get(getType())); 132 eraseFromParent(); 133 } 134 return Removed; 135 } 136 137 /// growOperands - grow operands - This grows the operand list in response 138 /// to a push_back style of operation. This grows the number of ops by 1.5 139 /// times. 140 /// 141 void PHINode::growOperands() { 142 unsigned e = getNumOperands(); 143 unsigned NumOps = e + e / 2; 144 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. 145 146 ReservedSpace = NumOps; 147 growHungoffUses(ReservedSpace, /* IsPhi */ true); 148 } 149 150 /// hasConstantValue - If the specified PHI node always merges together the same 151 /// value, return the value, otherwise return null. 152 Value *PHINode::hasConstantValue() const { 153 // Exploit the fact that phi nodes always have at least one entry. 154 Value *ConstantValue = getIncomingValue(0); 155 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) 156 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { 157 if (ConstantValue != this) 158 return nullptr; // Incoming values not all the same. 159 // The case where the first value is this PHI. 160 ConstantValue = getIncomingValue(i); 161 } 162 if (ConstantValue == this) 163 return UndefValue::get(getType()); 164 return ConstantValue; 165 } 166 167 /// hasConstantOrUndefValue - Whether the specified PHI node always merges 168 /// together the same value, assuming that undefs result in the same value as 169 /// non-undefs. 170 /// Unlike \ref hasConstantValue, this does not return a value because the 171 /// unique non-undef incoming value need not dominate the PHI node. 172 bool PHINode::hasConstantOrUndefValue() const { 173 Value *ConstantValue = nullptr; 174 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { 175 Value *Incoming = getIncomingValue(i); 176 if (Incoming != this && !isa<UndefValue>(Incoming)) { 177 if (ConstantValue && ConstantValue != Incoming) 178 return false; 179 ConstantValue = Incoming; 180 } 181 } 182 return true; 183 } 184 185 //===----------------------------------------------------------------------===// 186 // LandingPadInst Implementation 187 //===----------------------------------------------------------------------===// 188 189 LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, 190 const Twine &NameStr, Instruction *InsertBefore) 191 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { 192 init(NumReservedValues, NameStr); 193 } 194 195 LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, 196 const Twine &NameStr, BasicBlock *InsertAtEnd) 197 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { 198 init(NumReservedValues, NameStr); 199 } 200 201 LandingPadInst::LandingPadInst(const LandingPadInst &LP) 202 : Instruction(LP.getType(), Instruction::LandingPad, nullptr, 203 LP.getNumOperands()), 204 ReservedSpace(LP.getNumOperands()) { 205 allocHungoffUses(LP.getNumOperands()); 206 Use *OL = getOperandList(); 207 const Use *InOL = LP.getOperandList(); 208 for (unsigned I = 0, E = ReservedSpace; I != E; ++I) 209 OL[I] = InOL[I]; 210 211 setCleanup(LP.isCleanup()); 212 } 213 214 LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, 215 const Twine &NameStr, 216 Instruction *InsertBefore) { 217 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); 218 } 219 220 LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, 221 const Twine &NameStr, 222 BasicBlock *InsertAtEnd) { 223 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); 224 } 225 226 void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { 227 ReservedSpace = NumReservedValues; 228 setNumHungOffUseOperands(0); 229 allocHungoffUses(ReservedSpace); 230 setName(NameStr); 231 setCleanup(false); 232 } 233 234 /// growOperands - grow operands - This grows the operand list in response to a 235 /// push_back style of operation. This grows the number of ops by 2 times. 236 void LandingPadInst::growOperands(unsigned Size) { 237 unsigned e = getNumOperands(); 238 if (ReservedSpace >= e + Size) return; 239 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; 240 growHungoffUses(ReservedSpace); 241 } 242 243 void LandingPadInst::addClause(Constant *Val) { 244 unsigned OpNo = getNumOperands(); 245 growOperands(1); 246 assert(OpNo < ReservedSpace && "Growing didn't work!"); 247 setNumHungOffUseOperands(getNumOperands() + 1); 248 getOperandList()[OpNo] = Val; 249 } 250 251 //===----------------------------------------------------------------------===// 252 // CallBase Implementation 253 //===----------------------------------------------------------------------===// 254 255 CallBase *CallBase::Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, 256 Instruction *InsertPt) { 257 switch (CB->getOpcode()) { 258 case Instruction::Call: 259 return CallInst::Create(cast<CallInst>(CB), Bundles, InsertPt); 260 case Instruction::Invoke: 261 return InvokeInst::Create(cast<InvokeInst>(CB), Bundles, InsertPt); 262 case Instruction::CallBr: 263 return CallBrInst::Create(cast<CallBrInst>(CB), Bundles, InsertPt); 264 default: 265 llvm_unreachable("Unknown CallBase sub-class!"); 266 } 267 } 268 269 CallBase *CallBase::Create(CallBase *CI, OperandBundleDef OpB, 270 Instruction *InsertPt) { 271 SmallVector<OperandBundleDef, 2> OpDefs; 272 for (unsigned i = 0, e = CI->getNumOperandBundles(); i < e; ++i) { 273 auto ChildOB = CI->getOperandBundleAt(i); 274 if (ChildOB.getTagName() != OpB.getTag()) 275 OpDefs.emplace_back(ChildOB); 276 } 277 OpDefs.emplace_back(OpB); 278 return CallBase::Create(CI, OpDefs, InsertPt); 279 } 280 281 282 Function *CallBase::getCaller() { return getParent()->getParent(); } 283 284 unsigned CallBase::getNumSubclassExtraOperandsDynamic() const { 285 assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!"); 286 return cast<CallBrInst>(this)->getNumIndirectDests() + 1; 287 } 288 289 bool CallBase::isIndirectCall() const { 290 const Value *V = getCalledOperand(); 291 if (isa<Function>(V) || isa<Constant>(V)) 292 return false; 293 return !isInlineAsm(); 294 } 295 296 /// Tests if this call site must be tail call optimized. Only a CallInst can 297 /// be tail call optimized. 298 bool CallBase::isMustTailCall() const { 299 if (auto *CI = dyn_cast<CallInst>(this)) 300 return CI->isMustTailCall(); 301 return false; 302 } 303 304 /// Tests if this call site is marked as a tail call. 305 bool CallBase::isTailCall() const { 306 if (auto *CI = dyn_cast<CallInst>(this)) 307 return CI->isTailCall(); 308 return false; 309 } 310 311 Intrinsic::ID CallBase::getIntrinsicID() const { 312 if (auto *F = getCalledFunction()) 313 return F->getIntrinsicID(); 314 return Intrinsic::not_intrinsic; 315 } 316 317 bool CallBase::isReturnNonNull() const { 318 if (hasRetAttr(Attribute::NonNull)) 319 return true; 320 321 if (getRetDereferenceableBytes() > 0 && 322 !NullPointerIsDefined(getCaller(), getType()->getPointerAddressSpace())) 323 return true; 324 325 return false; 326 } 327 328 Value *CallBase::getArgOperandWithAttribute(Attribute::AttrKind Kind) const { 329 unsigned Index; 330 331 if (Attrs.hasAttrSomewhere(Kind, &Index)) 332 return getArgOperand(Index - AttributeList::FirstArgIndex); 333 if (const Function *F = getCalledFunction()) 334 if (F->getAttributes().hasAttrSomewhere(Kind, &Index)) 335 return getArgOperand(Index - AttributeList::FirstArgIndex); 336 337 return nullptr; 338 } 339 340 /// Determine whether the argument or parameter has the given attribute. 341 bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { 342 assert(ArgNo < arg_size() && "Param index out of bounds!"); 343 344 if (Attrs.hasParamAttr(ArgNo, Kind)) 345 return true; 346 if (const Function *F = getCalledFunction()) 347 return F->getAttributes().hasParamAttr(ArgNo, Kind); 348 return false; 349 } 350 351 bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { 352 Value *V = getCalledOperand(); 353 if (auto *CE = dyn_cast<ConstantExpr>(V)) 354 if (CE->getOpcode() == BitCast) 355 V = CE->getOperand(0); 356 357 if (auto *F = dyn_cast<Function>(V)) 358 return F->getAttributes().hasFnAttr(Kind); 359 360 return false; 361 } 362 363 bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { 364 Value *V = getCalledOperand(); 365 if (auto *CE = dyn_cast<ConstantExpr>(V)) 366 if (CE->getOpcode() == BitCast) 367 V = CE->getOperand(0); 368 369 if (auto *F = dyn_cast<Function>(V)) 370 return F->getAttributes().hasFnAttr(Kind); 371 372 return false; 373 } 374 375 template <typename AK> 376 Attribute CallBase::getFnAttrOnCalledFunction(AK Kind) const { 377 // Operand bundles override attributes on the called function, but don't 378 // override attributes directly present on the call instruction. 379 if (isFnAttrDisallowedByOpBundle(Kind)) 380 return Attribute(); 381 Value *V = getCalledOperand(); 382 if (auto *CE = dyn_cast<ConstantExpr>(V)) 383 if (CE->getOpcode() == BitCast) 384 V = CE->getOperand(0); 385 386 if (auto *F = dyn_cast<Function>(V)) 387 return F->getAttributes().getFnAttr(Kind); 388 389 return Attribute(); 390 } 391 392 template Attribute 393 CallBase::getFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; 394 template Attribute CallBase::getFnAttrOnCalledFunction(StringRef Kind) const; 395 396 void CallBase::getOperandBundlesAsDefs( 397 SmallVectorImpl<OperandBundleDef> &Defs) const { 398 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) 399 Defs.emplace_back(getOperandBundleAt(i)); 400 } 401 402 CallBase::op_iterator 403 CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, 404 const unsigned BeginIndex) { 405 auto It = op_begin() + BeginIndex; 406 for (auto &B : Bundles) 407 It = std::copy(B.input_begin(), B.input_end(), It); 408 409 auto *ContextImpl = getContext().pImpl; 410 auto BI = Bundles.begin(); 411 unsigned CurrentIndex = BeginIndex; 412 413 for (auto &BOI : bundle_op_infos()) { 414 assert(BI != Bundles.end() && "Incorrect allocation?"); 415 416 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); 417 BOI.Begin = CurrentIndex; 418 BOI.End = CurrentIndex + BI->input_size(); 419 CurrentIndex = BOI.End; 420 BI++; 421 } 422 423 assert(BI == Bundles.end() && "Incorrect allocation?"); 424 425 return It; 426 } 427 428 CallBase::BundleOpInfo &CallBase::getBundleOpInfoForOperand(unsigned OpIdx) { 429 /// When there isn't many bundles, we do a simple linear search. 430 /// Else fallback to a binary-search that use the fact that bundles usually 431 /// have similar number of argument to get faster convergence. 432 if (bundle_op_info_end() - bundle_op_info_begin() < 8) { 433 for (auto &BOI : bundle_op_infos()) 434 if (BOI.Begin <= OpIdx && OpIdx < BOI.End) 435 return BOI; 436 437 llvm_unreachable("Did not find operand bundle for operand!"); 438 } 439 440 assert(OpIdx >= arg_size() && "the Idx is not in the operand bundles"); 441 assert(bundle_op_info_end() - bundle_op_info_begin() > 0 && 442 OpIdx < std::prev(bundle_op_info_end())->End && 443 "The Idx isn't in the operand bundle"); 444 445 /// We need a decimal number below and to prevent using floating point numbers 446 /// we use an intergal value multiplied by this constant. 447 constexpr unsigned NumberScaling = 1024; 448 449 bundle_op_iterator Begin = bundle_op_info_begin(); 450 bundle_op_iterator End = bundle_op_info_end(); 451 bundle_op_iterator Current = Begin; 452 453 while (Begin != End) { 454 unsigned ScaledOperandPerBundle = 455 NumberScaling * (std::prev(End)->End - Begin->Begin) / (End - Begin); 456 Current = Begin + (((OpIdx - Begin->Begin) * NumberScaling) / 457 ScaledOperandPerBundle); 458 if (Current >= End) 459 Current = std::prev(End); 460 assert(Current < End && Current >= Begin && 461 "the operand bundle doesn't cover every value in the range"); 462 if (OpIdx >= Current->Begin && OpIdx < Current->End) 463 break; 464 if (OpIdx >= Current->End) 465 Begin = Current + 1; 466 else 467 End = Current; 468 } 469 470 assert(OpIdx >= Current->Begin && OpIdx < Current->End && 471 "the operand bundle doesn't cover every value in the range"); 472 return *Current; 473 } 474 475 CallBase *CallBase::addOperandBundle(CallBase *CB, uint32_t ID, 476 OperandBundleDef OB, 477 Instruction *InsertPt) { 478 if (CB->getOperandBundle(ID)) 479 return CB; 480 481 SmallVector<OperandBundleDef, 1> Bundles; 482 CB->getOperandBundlesAsDefs(Bundles); 483 Bundles.push_back(OB); 484 return Create(CB, Bundles, InsertPt); 485 } 486 487 CallBase *CallBase::removeOperandBundle(CallBase *CB, uint32_t ID, 488 Instruction *InsertPt) { 489 SmallVector<OperandBundleDef, 1> Bundles; 490 bool CreateNew = false; 491 492 for (unsigned I = 0, E = CB->getNumOperandBundles(); I != E; ++I) { 493 auto Bundle = CB->getOperandBundleAt(I); 494 if (Bundle.getTagID() == ID) { 495 CreateNew = true; 496 continue; 497 } 498 Bundles.emplace_back(Bundle); 499 } 500 501 return CreateNew ? Create(CB, Bundles, InsertPt) : CB; 502 } 503 504 bool CallBase::hasReadingOperandBundles() const { 505 // Implementation note: this is a conservative implementation of operand 506 // bundle semantics, where *any* non-assume operand bundle (other than 507 // ptrauth) forces a callsite to be at least readonly. 508 return hasOperandBundlesOtherThan(LLVMContext::OB_ptrauth) && 509 getIntrinsicID() != Intrinsic::assume; 510 } 511 512 //===----------------------------------------------------------------------===// 513 // CallInst Implementation 514 //===----------------------------------------------------------------------===// 515 516 void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, 517 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { 518 this->FTy = FTy; 519 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && 520 "NumOperands not set up?"); 521 522 #ifndef NDEBUG 523 assert((Args.size() == FTy->getNumParams() || 524 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 525 "Calling a function with bad signature!"); 526 527 for (unsigned i = 0; i != Args.size(); ++i) 528 assert((i >= FTy->getNumParams() || 529 FTy->getParamType(i) == Args[i]->getType()) && 530 "Calling a function with a bad signature!"); 531 #endif 532 533 // Set operands in order of their index to match use-list-order 534 // prediction. 535 llvm::copy(Args, op_begin()); 536 setCalledOperand(Func); 537 538 auto It = populateBundleOperandInfos(Bundles, Args.size()); 539 (void)It; 540 assert(It + 1 == op_end() && "Should add up!"); 541 542 setName(NameStr); 543 } 544 545 void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { 546 this->FTy = FTy; 547 assert(getNumOperands() == 1 && "NumOperands not set up?"); 548 setCalledOperand(Func); 549 550 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); 551 552 setName(NameStr); 553 } 554 555 CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, 556 Instruction *InsertBefore) 557 : CallBase(Ty->getReturnType(), Instruction::Call, 558 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { 559 init(Ty, Func, Name); 560 } 561 562 CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, 563 BasicBlock *InsertAtEnd) 564 : CallBase(Ty->getReturnType(), Instruction::Call, 565 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { 566 init(Ty, Func, Name); 567 } 568 569 CallInst::CallInst(const CallInst &CI) 570 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, 571 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), 572 CI.getNumOperands()) { 573 setTailCallKind(CI.getTailCallKind()); 574 setCallingConv(CI.getCallingConv()); 575 576 std::copy(CI.op_begin(), CI.op_end(), op_begin()); 577 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), 578 bundle_op_info_begin()); 579 SubclassOptionalData = CI.SubclassOptionalData; 580 } 581 582 CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, 583 Instruction *InsertPt) { 584 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); 585 586 auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledOperand(), 587 Args, OpB, CI->getName(), InsertPt); 588 NewCI->setTailCallKind(CI->getTailCallKind()); 589 NewCI->setCallingConv(CI->getCallingConv()); 590 NewCI->SubclassOptionalData = CI->SubclassOptionalData; 591 NewCI->setAttributes(CI->getAttributes()); 592 NewCI->setDebugLoc(CI->getDebugLoc()); 593 return NewCI; 594 } 595 596 // Update profile weight for call instruction by scaling it using the ratio 597 // of S/T. The meaning of "branch_weights" meta data for call instruction is 598 // transfered to represent call count. 599 void CallInst::updateProfWeight(uint64_t S, uint64_t T) { 600 auto *ProfileData = getMetadata(LLVMContext::MD_prof); 601 if (ProfileData == nullptr) 602 return; 603 604 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); 605 if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && 606 !ProfDataName->getString().equals("VP"))) 607 return; 608 609 if (T == 0) { 610 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in " 611 "div by 0. Ignoring. Likely the function " 612 << getParent()->getParent()->getName() 613 << " has 0 entry count, and contains call instructions " 614 "with non-zero prof info."); 615 return; 616 } 617 618 MDBuilder MDB(getContext()); 619 SmallVector<Metadata *, 3> Vals; 620 Vals.push_back(ProfileData->getOperand(0)); 621 APInt APS(128, S), APT(128, T); 622 if (ProfDataName->getString().equals("branch_weights") && 623 ProfileData->getNumOperands() > 0) { 624 // Using APInt::div may be expensive, but most cases should fit 64 bits. 625 APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)) 626 ->getValue() 627 .getZExtValue()); 628 Val *= APS; 629 Vals.push_back(MDB.createConstant( 630 ConstantInt::get(Type::getInt32Ty(getContext()), 631 Val.udiv(APT).getLimitedValue(UINT32_MAX)))); 632 } else if (ProfDataName->getString().equals("VP")) 633 for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { 634 // The first value is the key of the value profile, which will not change. 635 Vals.push_back(ProfileData->getOperand(i)); 636 uint64_t Count = 637 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) 638 ->getValue() 639 .getZExtValue(); 640 // Don't scale the magic number. 641 if (Count == NOMORE_ICP_MAGICNUM) { 642 Vals.push_back(ProfileData->getOperand(i + 1)); 643 continue; 644 } 645 // Using APInt::div may be expensive, but most cases should fit 64 bits. 646 APInt Val(128, Count); 647 Val *= APS; 648 Vals.push_back(MDB.createConstant( 649 ConstantInt::get(Type::getInt64Ty(getContext()), 650 Val.udiv(APT).getLimitedValue()))); 651 } 652 setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); 653 } 654 655 /// IsConstantOne - Return true only if val is constant int 1 656 static bool IsConstantOne(Value *val) { 657 assert(val && "IsConstantOne does not work with nullptr val"); 658 const ConstantInt *CVal = dyn_cast<ConstantInt>(val); 659 return CVal && CVal->isOne(); 660 } 661 662 static Instruction *createMalloc(Instruction *InsertBefore, 663 BasicBlock *InsertAtEnd, Type *IntPtrTy, 664 Type *AllocTy, Value *AllocSize, 665 Value *ArraySize, 666 ArrayRef<OperandBundleDef> OpB, 667 Function *MallocF, const Twine &Name) { 668 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && 669 "createMalloc needs either InsertBefore or InsertAtEnd"); 670 671 // malloc(type) becomes: 672 // bitcast (i8* malloc(typeSize)) to type* 673 // malloc(type, arraySize) becomes: 674 // bitcast (i8* malloc(typeSize*arraySize)) to type* 675 if (!ArraySize) 676 ArraySize = ConstantInt::get(IntPtrTy, 1); 677 else if (ArraySize->getType() != IntPtrTy) { 678 if (InsertBefore) 679 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, 680 "", InsertBefore); 681 else 682 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, 683 "", InsertAtEnd); 684 } 685 686 if (!IsConstantOne(ArraySize)) { 687 if (IsConstantOne(AllocSize)) { 688 AllocSize = ArraySize; // Operand * 1 = Operand 689 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { 690 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, 691 false /*ZExt*/); 692 // Malloc arg is constant product of type size and array size 693 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); 694 } else { 695 // Multiply type size by the array size... 696 if (InsertBefore) 697 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, 698 "mallocsize", InsertBefore); 699 else 700 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, 701 "mallocsize", InsertAtEnd); 702 } 703 } 704 705 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); 706 // Create the call to Malloc. 707 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; 708 Module *M = BB->getParent()->getParent(); 709 Type *BPTy = Type::getInt8PtrTy(BB->getContext()); 710 FunctionCallee MallocFunc = MallocF; 711 if (!MallocFunc) 712 // prototype malloc as "void *malloc(size_t)" 713 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); 714 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); 715 CallInst *MCall = nullptr; 716 Instruction *Result = nullptr; 717 if (InsertBefore) { 718 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", 719 InsertBefore); 720 Result = MCall; 721 if (Result->getType() != AllocPtrType) 722 // Create a cast instruction to convert to the right type... 723 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); 724 } else { 725 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); 726 Result = MCall; 727 if (Result->getType() != AllocPtrType) { 728 InsertAtEnd->getInstList().push_back(MCall); 729 // Create a cast instruction to convert to the right type... 730 Result = new BitCastInst(MCall, AllocPtrType, Name); 731 } 732 } 733 MCall->setTailCall(); 734 if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) { 735 MCall->setCallingConv(F->getCallingConv()); 736 if (!F->returnDoesNotAlias()) 737 F->setReturnDoesNotAlias(); 738 } 739 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); 740 741 return Result; 742 } 743 744 /// CreateMalloc - Generate the IR for a call to malloc: 745 /// 1. Compute the malloc call's argument as the specified type's size, 746 /// possibly multiplied by the array size if the array size is not 747 /// constant 1. 748 /// 2. Call malloc with that argument. 749 /// 3. Bitcast the result of the malloc call to the specified type. 750 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, 751 Type *IntPtrTy, Type *AllocTy, 752 Value *AllocSize, Value *ArraySize, 753 Function *MallocF, 754 const Twine &Name) { 755 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, 756 ArraySize, None, MallocF, Name); 757 } 758 Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, 759 Type *IntPtrTy, Type *AllocTy, 760 Value *AllocSize, Value *ArraySize, 761 ArrayRef<OperandBundleDef> OpB, 762 Function *MallocF, 763 const Twine &Name) { 764 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, 765 ArraySize, OpB, MallocF, Name); 766 } 767 768 /// CreateMalloc - Generate the IR for a call to malloc: 769 /// 1. Compute the malloc call's argument as the specified type's size, 770 /// possibly multiplied by the array size if the array size is not 771 /// constant 1. 772 /// 2. Call malloc with that argument. 773 /// 3. Bitcast the result of the malloc call to the specified type. 774 /// Note: This function does not add the bitcast to the basic block, that is the 775 /// responsibility of the caller. 776 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, 777 Type *IntPtrTy, Type *AllocTy, 778 Value *AllocSize, Value *ArraySize, 779 Function *MallocF, const Twine &Name) { 780 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, 781 ArraySize, None, MallocF, Name); 782 } 783 Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, 784 Type *IntPtrTy, Type *AllocTy, 785 Value *AllocSize, Value *ArraySize, 786 ArrayRef<OperandBundleDef> OpB, 787 Function *MallocF, const Twine &Name) { 788 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, 789 ArraySize, OpB, MallocF, Name); 790 } 791 792 static Instruction *createFree(Value *Source, 793 ArrayRef<OperandBundleDef> Bundles, 794 Instruction *InsertBefore, 795 BasicBlock *InsertAtEnd) { 796 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && 797 "createFree needs either InsertBefore or InsertAtEnd"); 798 assert(Source->getType()->isPointerTy() && 799 "Can not free something of nonpointer type!"); 800 801 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; 802 Module *M = BB->getParent()->getParent(); 803 804 Type *VoidTy = Type::getVoidTy(M->getContext()); 805 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); 806 // prototype free as "void free(void*)" 807 FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); 808 CallInst *Result = nullptr; 809 Value *PtrCast = Source; 810 if (InsertBefore) { 811 if (Source->getType() != IntPtrTy) 812 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); 813 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); 814 } else { 815 if (Source->getType() != IntPtrTy) 816 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); 817 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); 818 } 819 Result->setTailCall(); 820 if (Function *F = dyn_cast<Function>(FreeFunc.getCallee())) 821 Result->setCallingConv(F->getCallingConv()); 822 823 return Result; 824 } 825 826 /// CreateFree - Generate the IR for a call to the builtin free function. 827 Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { 828 return createFree(Source, None, InsertBefore, nullptr); 829 } 830 Instruction *CallInst::CreateFree(Value *Source, 831 ArrayRef<OperandBundleDef> Bundles, 832 Instruction *InsertBefore) { 833 return createFree(Source, Bundles, InsertBefore, nullptr); 834 } 835 836 /// CreateFree - Generate the IR for a call to the builtin free function. 837 /// Note: This function does not add the call to the basic block, that is the 838 /// responsibility of the caller. 839 Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { 840 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); 841 assert(FreeCall && "CreateFree did not create a CallInst"); 842 return FreeCall; 843 } 844 Instruction *CallInst::CreateFree(Value *Source, 845 ArrayRef<OperandBundleDef> Bundles, 846 BasicBlock *InsertAtEnd) { 847 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); 848 assert(FreeCall && "CreateFree did not create a CallInst"); 849 return FreeCall; 850 } 851 852 //===----------------------------------------------------------------------===// 853 // InvokeInst Implementation 854 //===----------------------------------------------------------------------===// 855 856 void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, 857 BasicBlock *IfException, ArrayRef<Value *> Args, 858 ArrayRef<OperandBundleDef> Bundles, 859 const Twine &NameStr) { 860 this->FTy = FTy; 861 862 assert((int)getNumOperands() == 863 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) && 864 "NumOperands not set up?"); 865 866 #ifndef NDEBUG 867 assert(((Args.size() == FTy->getNumParams()) || 868 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 869 "Invoking a function with bad signature"); 870 871 for (unsigned i = 0, e = Args.size(); i != e; i++) 872 assert((i >= FTy->getNumParams() || 873 FTy->getParamType(i) == Args[i]->getType()) && 874 "Invoking a function with a bad signature!"); 875 #endif 876 877 // Set operands in order of their index to match use-list-order 878 // prediction. 879 llvm::copy(Args, op_begin()); 880 setNormalDest(IfNormal); 881 setUnwindDest(IfException); 882 setCalledOperand(Fn); 883 884 auto It = populateBundleOperandInfos(Bundles, Args.size()); 885 (void)It; 886 assert(It + 3 == op_end() && "Should add up!"); 887 888 setName(NameStr); 889 } 890 891 InvokeInst::InvokeInst(const InvokeInst &II) 892 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, 893 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), 894 II.getNumOperands()) { 895 setCallingConv(II.getCallingConv()); 896 std::copy(II.op_begin(), II.op_end(), op_begin()); 897 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), 898 bundle_op_info_begin()); 899 SubclassOptionalData = II.SubclassOptionalData; 900 } 901 902 InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, 903 Instruction *InsertPt) { 904 std::vector<Value *> Args(II->arg_begin(), II->arg_end()); 905 906 auto *NewII = InvokeInst::Create( 907 II->getFunctionType(), II->getCalledOperand(), II->getNormalDest(), 908 II->getUnwindDest(), Args, OpB, II->getName(), InsertPt); 909 NewII->setCallingConv(II->getCallingConv()); 910 NewII->SubclassOptionalData = II->SubclassOptionalData; 911 NewII->setAttributes(II->getAttributes()); 912 NewII->setDebugLoc(II->getDebugLoc()); 913 return NewII; 914 } 915 916 LandingPadInst *InvokeInst::getLandingPadInst() const { 917 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); 918 } 919 920 //===----------------------------------------------------------------------===// 921 // CallBrInst Implementation 922 //===----------------------------------------------------------------------===// 923 924 void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough, 925 ArrayRef<BasicBlock *> IndirectDests, 926 ArrayRef<Value *> Args, 927 ArrayRef<OperandBundleDef> Bundles, 928 const Twine &NameStr) { 929 this->FTy = FTy; 930 931 assert((int)getNumOperands() == 932 ComputeNumOperands(Args.size(), IndirectDests.size(), 933 CountBundleInputs(Bundles)) && 934 "NumOperands not set up?"); 935 936 #ifndef NDEBUG 937 assert(((Args.size() == FTy->getNumParams()) || 938 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 939 "Calling a function with bad signature"); 940 941 for (unsigned i = 0, e = Args.size(); i != e; i++) 942 assert((i >= FTy->getNumParams() || 943 FTy->getParamType(i) == Args[i]->getType()) && 944 "Calling a function with a bad signature!"); 945 #endif 946 947 // Set operands in order of their index to match use-list-order 948 // prediction. 949 std::copy(Args.begin(), Args.end(), op_begin()); 950 NumIndirectDests = IndirectDests.size(); 951 setDefaultDest(Fallthrough); 952 for (unsigned i = 0; i != NumIndirectDests; ++i) 953 setIndirectDest(i, IndirectDests[i]); 954 setCalledOperand(Fn); 955 956 auto It = populateBundleOperandInfos(Bundles, Args.size()); 957 (void)It; 958 assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!"); 959 960 setName(NameStr); 961 } 962 963 void CallBrInst::updateArgBlockAddresses(unsigned i, BasicBlock *B) { 964 assert(getNumIndirectDests() > i && "IndirectDest # out of range for callbr"); 965 if (BasicBlock *OldBB = getIndirectDest(i)) { 966 BlockAddress *Old = BlockAddress::get(OldBB); 967 BlockAddress *New = BlockAddress::get(B); 968 for (unsigned ArgNo = 0, e = arg_size(); ArgNo != e; ++ArgNo) 969 if (dyn_cast<BlockAddress>(getArgOperand(ArgNo)) == Old) 970 setArgOperand(ArgNo, New); 971 } 972 } 973 974 CallBrInst::CallBrInst(const CallBrInst &CBI) 975 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, 976 OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), 977 CBI.getNumOperands()) { 978 setCallingConv(CBI.getCallingConv()); 979 std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); 980 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), 981 bundle_op_info_begin()); 982 SubclassOptionalData = CBI.SubclassOptionalData; 983 NumIndirectDests = CBI.NumIndirectDests; 984 } 985 986 CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, 987 Instruction *InsertPt) { 988 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); 989 990 auto *NewCBI = CallBrInst::Create( 991 CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(), 992 CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt); 993 NewCBI->setCallingConv(CBI->getCallingConv()); 994 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; 995 NewCBI->setAttributes(CBI->getAttributes()); 996 NewCBI->setDebugLoc(CBI->getDebugLoc()); 997 NewCBI->NumIndirectDests = CBI->NumIndirectDests; 998 return NewCBI; 999 } 1000 1001 //===----------------------------------------------------------------------===// 1002 // ReturnInst Implementation 1003 //===----------------------------------------------------------------------===// 1004 1005 ReturnInst::ReturnInst(const ReturnInst &RI) 1006 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, 1007 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), 1008 RI.getNumOperands()) { 1009 if (RI.getNumOperands()) 1010 Op<0>() = RI.Op<0>(); 1011 SubclassOptionalData = RI.SubclassOptionalData; 1012 } 1013 1014 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) 1015 : Instruction(Type::getVoidTy(C), Instruction::Ret, 1016 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 1017 InsertBefore) { 1018 if (retVal) 1019 Op<0>() = retVal; 1020 } 1021 1022 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) 1023 : Instruction(Type::getVoidTy(C), Instruction::Ret, 1024 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 1025 InsertAtEnd) { 1026 if (retVal) 1027 Op<0>() = retVal; 1028 } 1029 1030 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 1031 : Instruction(Type::getVoidTy(Context), Instruction::Ret, 1032 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} 1033 1034 //===----------------------------------------------------------------------===// 1035 // ResumeInst Implementation 1036 //===----------------------------------------------------------------------===// 1037 1038 ResumeInst::ResumeInst(const ResumeInst &RI) 1039 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, 1040 OperandTraits<ResumeInst>::op_begin(this), 1) { 1041 Op<0>() = RI.Op<0>(); 1042 } 1043 1044 ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) 1045 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 1046 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { 1047 Op<0>() = Exn; 1048 } 1049 1050 ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) 1051 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 1052 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { 1053 Op<0>() = Exn; 1054 } 1055 1056 //===----------------------------------------------------------------------===// 1057 // CleanupReturnInst Implementation 1058 //===----------------------------------------------------------------------===// 1059 1060 CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) 1061 : Instruction(CRI.getType(), Instruction::CleanupRet, 1062 OperandTraits<CleanupReturnInst>::op_end(this) - 1063 CRI.getNumOperands(), 1064 CRI.getNumOperands()) { 1065 setSubclassData<Instruction::OpaqueField>( 1066 CRI.getSubclassData<Instruction::OpaqueField>()); 1067 Op<0>() = CRI.Op<0>(); 1068 if (CRI.hasUnwindDest()) 1069 Op<1>() = CRI.Op<1>(); 1070 } 1071 1072 void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { 1073 if (UnwindBB) 1074 setSubclassData<UnwindDestField>(true); 1075 1076 Op<0>() = CleanupPad; 1077 if (UnwindBB) 1078 Op<1>() = UnwindBB; 1079 } 1080 1081 CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, 1082 unsigned Values, Instruction *InsertBefore) 1083 : Instruction(Type::getVoidTy(CleanupPad->getContext()), 1084 Instruction::CleanupRet, 1085 OperandTraits<CleanupReturnInst>::op_end(this) - Values, 1086 Values, InsertBefore) { 1087 init(CleanupPad, UnwindBB); 1088 } 1089 1090 CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, 1091 unsigned Values, BasicBlock *InsertAtEnd) 1092 : Instruction(Type::getVoidTy(CleanupPad->getContext()), 1093 Instruction::CleanupRet, 1094 OperandTraits<CleanupReturnInst>::op_end(this) - Values, 1095 Values, InsertAtEnd) { 1096 init(CleanupPad, UnwindBB); 1097 } 1098 1099 //===----------------------------------------------------------------------===// 1100 // CatchReturnInst Implementation 1101 //===----------------------------------------------------------------------===// 1102 void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { 1103 Op<0>() = CatchPad; 1104 Op<1>() = BB; 1105 } 1106 1107 CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) 1108 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, 1109 OperandTraits<CatchReturnInst>::op_begin(this), 2) { 1110 Op<0>() = CRI.Op<0>(); 1111 Op<1>() = CRI.Op<1>(); 1112 } 1113 1114 CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, 1115 Instruction *InsertBefore) 1116 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, 1117 OperandTraits<CatchReturnInst>::op_begin(this), 2, 1118 InsertBefore) { 1119 init(CatchPad, BB); 1120 } 1121 1122 CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, 1123 BasicBlock *InsertAtEnd) 1124 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, 1125 OperandTraits<CatchReturnInst>::op_begin(this), 2, 1126 InsertAtEnd) { 1127 init(CatchPad, BB); 1128 } 1129 1130 //===----------------------------------------------------------------------===// 1131 // CatchSwitchInst Implementation 1132 //===----------------------------------------------------------------------===// 1133 1134 CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 1135 unsigned NumReservedValues, 1136 const Twine &NameStr, 1137 Instruction *InsertBefore) 1138 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, 1139 InsertBefore) { 1140 if (UnwindDest) 1141 ++NumReservedValues; 1142 init(ParentPad, UnwindDest, NumReservedValues + 1); 1143 setName(NameStr); 1144 } 1145 1146 CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 1147 unsigned NumReservedValues, 1148 const Twine &NameStr, BasicBlock *InsertAtEnd) 1149 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, 1150 InsertAtEnd) { 1151 if (UnwindDest) 1152 ++NumReservedValues; 1153 init(ParentPad, UnwindDest, NumReservedValues + 1); 1154 setName(NameStr); 1155 } 1156 1157 CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) 1158 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, 1159 CSI.getNumOperands()) { 1160 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); 1161 setNumHungOffUseOperands(ReservedSpace); 1162 Use *OL = getOperandList(); 1163 const Use *InOL = CSI.getOperandList(); 1164 for (unsigned I = 1, E = ReservedSpace; I != E; ++I) 1165 OL[I] = InOL[I]; 1166 } 1167 1168 void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, 1169 unsigned NumReservedValues) { 1170 assert(ParentPad && NumReservedValues); 1171 1172 ReservedSpace = NumReservedValues; 1173 setNumHungOffUseOperands(UnwindDest ? 2 : 1); 1174 allocHungoffUses(ReservedSpace); 1175 1176 Op<0>() = ParentPad; 1177 if (UnwindDest) { 1178 setSubclassData<UnwindDestField>(true); 1179 setUnwindDest(UnwindDest); 1180 } 1181 } 1182 1183 /// growOperands - grow operands - This grows the operand list in response to a 1184 /// push_back style of operation. This grows the number of ops by 2 times. 1185 void CatchSwitchInst::growOperands(unsigned Size) { 1186 unsigned NumOperands = getNumOperands(); 1187 assert(NumOperands >= 1); 1188 if (ReservedSpace >= NumOperands + Size) 1189 return; 1190 ReservedSpace = (NumOperands + Size / 2) * 2; 1191 growHungoffUses(ReservedSpace); 1192 } 1193 1194 void CatchSwitchInst::addHandler(BasicBlock *Handler) { 1195 unsigned OpNo = getNumOperands(); 1196 growOperands(1); 1197 assert(OpNo < ReservedSpace && "Growing didn't work!"); 1198 setNumHungOffUseOperands(getNumOperands() + 1); 1199 getOperandList()[OpNo] = Handler; 1200 } 1201 1202 void CatchSwitchInst::removeHandler(handler_iterator HI) { 1203 // Move all subsequent handlers up one. 1204 Use *EndDst = op_end() - 1; 1205 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) 1206 *CurDst = *(CurDst + 1); 1207 // Null out the last handler use. 1208 *EndDst = nullptr; 1209 1210 setNumHungOffUseOperands(getNumOperands() - 1); 1211 } 1212 1213 //===----------------------------------------------------------------------===// 1214 // FuncletPadInst Implementation 1215 //===----------------------------------------------------------------------===// 1216 void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, 1217 const Twine &NameStr) { 1218 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); 1219 llvm::copy(Args, op_begin()); 1220 setParentPad(ParentPad); 1221 setName(NameStr); 1222 } 1223 1224 FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) 1225 : Instruction(FPI.getType(), FPI.getOpcode(), 1226 OperandTraits<FuncletPadInst>::op_end(this) - 1227 FPI.getNumOperands(), 1228 FPI.getNumOperands()) { 1229 std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); 1230 setParentPad(FPI.getParentPad()); 1231 } 1232 1233 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, 1234 ArrayRef<Value *> Args, unsigned Values, 1235 const Twine &NameStr, Instruction *InsertBefore) 1236 : Instruction(ParentPad->getType(), Op, 1237 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, 1238 InsertBefore) { 1239 init(ParentPad, Args, NameStr); 1240 } 1241 1242 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, 1243 ArrayRef<Value *> Args, unsigned Values, 1244 const Twine &NameStr, BasicBlock *InsertAtEnd) 1245 : Instruction(ParentPad->getType(), Op, 1246 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, 1247 InsertAtEnd) { 1248 init(ParentPad, Args, NameStr); 1249 } 1250 1251 //===----------------------------------------------------------------------===// 1252 // UnreachableInst Implementation 1253 //===----------------------------------------------------------------------===// 1254 1255 UnreachableInst::UnreachableInst(LLVMContext &Context, 1256 Instruction *InsertBefore) 1257 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, 1258 0, InsertBefore) {} 1259 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 1260 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, 1261 0, InsertAtEnd) {} 1262 1263 //===----------------------------------------------------------------------===// 1264 // BranchInst Implementation 1265 //===----------------------------------------------------------------------===// 1266 1267 void BranchInst::AssertOK() { 1268 if (isConditional()) 1269 assert(getCondition()->getType()->isIntegerTy(1) && 1270 "May only branch on boolean predicates!"); 1271 } 1272 1273 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) 1274 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1275 OperandTraits<BranchInst>::op_end(this) - 1, 1, 1276 InsertBefore) { 1277 assert(IfTrue && "Branch destination may not be null!"); 1278 Op<-1>() = IfTrue; 1279 } 1280 1281 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1282 Instruction *InsertBefore) 1283 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1284 OperandTraits<BranchInst>::op_end(this) - 3, 3, 1285 InsertBefore) { 1286 // Assign in order of operand index to make use-list order predictable. 1287 Op<-3>() = Cond; 1288 Op<-2>() = IfFalse; 1289 Op<-1>() = IfTrue; 1290 #ifndef NDEBUG 1291 AssertOK(); 1292 #endif 1293 } 1294 1295 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) 1296 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1297 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { 1298 assert(IfTrue && "Branch destination may not be null!"); 1299 Op<-1>() = IfTrue; 1300 } 1301 1302 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1303 BasicBlock *InsertAtEnd) 1304 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1305 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { 1306 // Assign in order of operand index to make use-list order predictable. 1307 Op<-3>() = Cond; 1308 Op<-2>() = IfFalse; 1309 Op<-1>() = IfTrue; 1310 #ifndef NDEBUG 1311 AssertOK(); 1312 #endif 1313 } 1314 1315 BranchInst::BranchInst(const BranchInst &BI) 1316 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, 1317 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), 1318 BI.getNumOperands()) { 1319 // Assign in order of operand index to make use-list order predictable. 1320 if (BI.getNumOperands() != 1) { 1321 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); 1322 Op<-3>() = BI.Op<-3>(); 1323 Op<-2>() = BI.Op<-2>(); 1324 } 1325 Op<-1>() = BI.Op<-1>(); 1326 SubclassOptionalData = BI.SubclassOptionalData; 1327 } 1328 1329 void BranchInst::swapSuccessors() { 1330 assert(isConditional() && 1331 "Cannot swap successors of an unconditional branch"); 1332 Op<-1>().swap(Op<-2>()); 1333 1334 // Update profile metadata if present and it matches our structural 1335 // expectations. 1336 swapProfMetadata(); 1337 } 1338 1339 //===----------------------------------------------------------------------===// 1340 // AllocaInst Implementation 1341 //===----------------------------------------------------------------------===// 1342 1343 static Value *getAISize(LLVMContext &Context, Value *Amt) { 1344 if (!Amt) 1345 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); 1346 else { 1347 assert(!isa<BasicBlock>(Amt) && 1348 "Passed basic block into allocation size parameter! Use other ctor"); 1349 assert(Amt->getType()->isIntegerTy() && 1350 "Allocation array size is not an integer!"); 1351 } 1352 return Amt; 1353 } 1354 1355 static Align computeAllocaDefaultAlign(Type *Ty, BasicBlock *BB) { 1356 assert(BB && "Insertion BB cannot be null when alignment not provided!"); 1357 assert(BB->getParent() && 1358 "BB must be in a Function when alignment not provided!"); 1359 const DataLayout &DL = BB->getModule()->getDataLayout(); 1360 return DL.getPrefTypeAlign(Ty); 1361 } 1362 1363 static Align computeAllocaDefaultAlign(Type *Ty, Instruction *I) { 1364 assert(I && "Insertion position cannot be null when alignment not provided!"); 1365 return computeAllocaDefaultAlign(Ty, I->getParent()); 1366 } 1367 1368 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, 1369 Instruction *InsertBefore) 1370 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} 1371 1372 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, 1373 BasicBlock *InsertAtEnd) 1374 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} 1375 1376 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1377 const Twine &Name, Instruction *InsertBefore) 1378 : AllocaInst(Ty, AddrSpace, ArraySize, 1379 computeAllocaDefaultAlign(Ty, InsertBefore), Name, 1380 InsertBefore) {} 1381 1382 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1383 const Twine &Name, BasicBlock *InsertAtEnd) 1384 : AllocaInst(Ty, AddrSpace, ArraySize, 1385 computeAllocaDefaultAlign(Ty, InsertAtEnd), Name, 1386 InsertAtEnd) {} 1387 1388 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1389 Align Align, const Twine &Name, 1390 Instruction *InsertBefore) 1391 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, 1392 getAISize(Ty->getContext(), ArraySize), InsertBefore), 1393 AllocatedType(Ty) { 1394 setAlignment(Align); 1395 assert(!Ty->isVoidTy() && "Cannot allocate void!"); 1396 setName(Name); 1397 } 1398 1399 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1400 Align Align, const Twine &Name, BasicBlock *InsertAtEnd) 1401 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, 1402 getAISize(Ty->getContext(), ArraySize), InsertAtEnd), 1403 AllocatedType(Ty) { 1404 setAlignment(Align); 1405 assert(!Ty->isVoidTy() && "Cannot allocate void!"); 1406 setName(Name); 1407 } 1408 1409 1410 bool AllocaInst::isArrayAllocation() const { 1411 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) 1412 return !CI->isOne(); 1413 return true; 1414 } 1415 1416 /// isStaticAlloca - Return true if this alloca is in the entry block of the 1417 /// function and is a constant size. If so, the code generator will fold it 1418 /// into the prolog/epilog code, so it is basically free. 1419 bool AllocaInst::isStaticAlloca() const { 1420 // Must be constant size. 1421 if (!isa<ConstantInt>(getArraySize())) return false; 1422 1423 // Must be in the entry block. 1424 const BasicBlock *Parent = getParent(); 1425 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); 1426 } 1427 1428 //===----------------------------------------------------------------------===// 1429 // LoadInst Implementation 1430 //===----------------------------------------------------------------------===// 1431 1432 void LoadInst::AssertOK() { 1433 assert(getOperand(0)->getType()->isPointerTy() && 1434 "Ptr must have pointer type."); 1435 } 1436 1437 static Align computeLoadStoreDefaultAlign(Type *Ty, BasicBlock *BB) { 1438 assert(BB && "Insertion BB cannot be null when alignment not provided!"); 1439 assert(BB->getParent() && 1440 "BB must be in a Function when alignment not provided!"); 1441 const DataLayout &DL = BB->getModule()->getDataLayout(); 1442 return DL.getABITypeAlign(Ty); 1443 } 1444 1445 static Align computeLoadStoreDefaultAlign(Type *Ty, Instruction *I) { 1446 assert(I && "Insertion position cannot be null when alignment not provided!"); 1447 return computeLoadStoreDefaultAlign(Ty, I->getParent()); 1448 } 1449 1450 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, 1451 Instruction *InsertBef) 1452 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} 1453 1454 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, 1455 BasicBlock *InsertAE) 1456 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} 1457 1458 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1459 Instruction *InsertBef) 1460 : LoadInst(Ty, Ptr, Name, isVolatile, 1461 computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {} 1462 1463 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1464 BasicBlock *InsertAE) 1465 : LoadInst(Ty, Ptr, Name, isVolatile, 1466 computeLoadStoreDefaultAlign(Ty, InsertAE), InsertAE) {} 1467 1468 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1469 Align Align, Instruction *InsertBef) 1470 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, 1471 SyncScope::System, InsertBef) {} 1472 1473 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1474 Align Align, BasicBlock *InsertAE) 1475 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, 1476 SyncScope::System, InsertAE) {} 1477 1478 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1479 Align Align, AtomicOrdering Order, SyncScope::ID SSID, 1480 Instruction *InsertBef) 1481 : UnaryInstruction(Ty, Load, Ptr, InsertBef) { 1482 assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty)); 1483 setVolatile(isVolatile); 1484 setAlignment(Align); 1485 setAtomic(Order, SSID); 1486 AssertOK(); 1487 setName(Name); 1488 } 1489 1490 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1491 Align Align, AtomicOrdering Order, SyncScope::ID SSID, 1492 BasicBlock *InsertAE) 1493 : UnaryInstruction(Ty, Load, Ptr, InsertAE) { 1494 assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty)); 1495 setVolatile(isVolatile); 1496 setAlignment(Align); 1497 setAtomic(Order, SSID); 1498 AssertOK(); 1499 setName(Name); 1500 } 1501 1502 //===----------------------------------------------------------------------===// 1503 // StoreInst Implementation 1504 //===----------------------------------------------------------------------===// 1505 1506 void StoreInst::AssertOK() { 1507 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); 1508 assert(getOperand(1)->getType()->isPointerTy() && 1509 "Ptr must have pointer type!"); 1510 assert(cast<PointerType>(getOperand(1)->getType()) 1511 ->isOpaqueOrPointeeTypeMatches(getOperand(0)->getType()) && 1512 "Ptr must be a pointer to Val type!"); 1513 } 1514 1515 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) 1516 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} 1517 1518 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) 1519 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} 1520 1521 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1522 Instruction *InsertBefore) 1523 : StoreInst(val, addr, isVolatile, 1524 computeLoadStoreDefaultAlign(val->getType(), InsertBefore), 1525 InsertBefore) {} 1526 1527 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1528 BasicBlock *InsertAtEnd) 1529 : StoreInst(val, addr, isVolatile, 1530 computeLoadStoreDefaultAlign(val->getType(), InsertAtEnd), 1531 InsertAtEnd) {} 1532 1533 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1534 Instruction *InsertBefore) 1535 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, 1536 SyncScope::System, InsertBefore) {} 1537 1538 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1539 BasicBlock *InsertAtEnd) 1540 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, 1541 SyncScope::System, InsertAtEnd) {} 1542 1543 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1544 AtomicOrdering Order, SyncScope::ID SSID, 1545 Instruction *InsertBefore) 1546 : Instruction(Type::getVoidTy(val->getContext()), Store, 1547 OperandTraits<StoreInst>::op_begin(this), 1548 OperandTraits<StoreInst>::operands(this), InsertBefore) { 1549 Op<0>() = val; 1550 Op<1>() = addr; 1551 setVolatile(isVolatile); 1552 setAlignment(Align); 1553 setAtomic(Order, SSID); 1554 AssertOK(); 1555 } 1556 1557 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1558 AtomicOrdering Order, SyncScope::ID SSID, 1559 BasicBlock *InsertAtEnd) 1560 : Instruction(Type::getVoidTy(val->getContext()), Store, 1561 OperandTraits<StoreInst>::op_begin(this), 1562 OperandTraits<StoreInst>::operands(this), InsertAtEnd) { 1563 Op<0>() = val; 1564 Op<1>() = addr; 1565 setVolatile(isVolatile); 1566 setAlignment(Align); 1567 setAtomic(Order, SSID); 1568 AssertOK(); 1569 } 1570 1571 1572 //===----------------------------------------------------------------------===// 1573 // AtomicCmpXchgInst Implementation 1574 //===----------------------------------------------------------------------===// 1575 1576 void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, 1577 Align Alignment, AtomicOrdering SuccessOrdering, 1578 AtomicOrdering FailureOrdering, 1579 SyncScope::ID SSID) { 1580 Op<0>() = Ptr; 1581 Op<1>() = Cmp; 1582 Op<2>() = NewVal; 1583 setSuccessOrdering(SuccessOrdering); 1584 setFailureOrdering(FailureOrdering); 1585 setSyncScopeID(SSID); 1586 setAlignment(Alignment); 1587 1588 assert(getOperand(0) && getOperand(1) && getOperand(2) && 1589 "All operands must be non-null!"); 1590 assert(getOperand(0)->getType()->isPointerTy() && 1591 "Ptr must have pointer type!"); 1592 assert(cast<PointerType>(getOperand(0)->getType()) 1593 ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) && 1594 "Ptr must be a pointer to Cmp type!"); 1595 assert(cast<PointerType>(getOperand(0)->getType()) 1596 ->isOpaqueOrPointeeTypeMatches(getOperand(2)->getType()) && 1597 "Ptr must be a pointer to NewVal type!"); 1598 assert(getOperand(1)->getType() == getOperand(2)->getType() && 1599 "Cmp type and NewVal type must be same!"); 1600 } 1601 1602 AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1603 Align Alignment, 1604 AtomicOrdering SuccessOrdering, 1605 AtomicOrdering FailureOrdering, 1606 SyncScope::ID SSID, 1607 Instruction *InsertBefore) 1608 : Instruction( 1609 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), 1610 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1611 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { 1612 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); 1613 } 1614 1615 AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1616 Align Alignment, 1617 AtomicOrdering SuccessOrdering, 1618 AtomicOrdering FailureOrdering, 1619 SyncScope::ID SSID, 1620 BasicBlock *InsertAtEnd) 1621 : Instruction( 1622 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), 1623 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1624 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { 1625 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); 1626 } 1627 1628 //===----------------------------------------------------------------------===// 1629 // AtomicRMWInst Implementation 1630 //===----------------------------------------------------------------------===// 1631 1632 void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, 1633 Align Alignment, AtomicOrdering Ordering, 1634 SyncScope::ID SSID) { 1635 Op<0>() = Ptr; 1636 Op<1>() = Val; 1637 setOperation(Operation); 1638 setOrdering(Ordering); 1639 setSyncScopeID(SSID); 1640 setAlignment(Alignment); 1641 1642 assert(getOperand(0) && getOperand(1) && 1643 "All operands must be non-null!"); 1644 assert(getOperand(0)->getType()->isPointerTy() && 1645 "Ptr must have pointer type!"); 1646 assert(cast<PointerType>(getOperand(0)->getType()) 1647 ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) && 1648 "Ptr must be a pointer to Val type!"); 1649 assert(Ordering != AtomicOrdering::NotAtomic && 1650 "AtomicRMW instructions must be atomic!"); 1651 } 1652 1653 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1654 Align Alignment, AtomicOrdering Ordering, 1655 SyncScope::ID SSID, Instruction *InsertBefore) 1656 : Instruction(Val->getType(), AtomicRMW, 1657 OperandTraits<AtomicRMWInst>::op_begin(this), 1658 OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) { 1659 Init(Operation, Ptr, Val, Alignment, Ordering, SSID); 1660 } 1661 1662 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1663 Align Alignment, AtomicOrdering Ordering, 1664 SyncScope::ID SSID, BasicBlock *InsertAtEnd) 1665 : Instruction(Val->getType(), AtomicRMW, 1666 OperandTraits<AtomicRMWInst>::op_begin(this), 1667 OperandTraits<AtomicRMWInst>::operands(this), InsertAtEnd) { 1668 Init(Operation, Ptr, Val, Alignment, Ordering, SSID); 1669 } 1670 1671 StringRef AtomicRMWInst::getOperationName(BinOp Op) { 1672 switch (Op) { 1673 case AtomicRMWInst::Xchg: 1674 return "xchg"; 1675 case AtomicRMWInst::Add: 1676 return "add"; 1677 case AtomicRMWInst::Sub: 1678 return "sub"; 1679 case AtomicRMWInst::And: 1680 return "and"; 1681 case AtomicRMWInst::Nand: 1682 return "nand"; 1683 case AtomicRMWInst::Or: 1684 return "or"; 1685 case AtomicRMWInst::Xor: 1686 return "xor"; 1687 case AtomicRMWInst::Max: 1688 return "max"; 1689 case AtomicRMWInst::Min: 1690 return "min"; 1691 case AtomicRMWInst::UMax: 1692 return "umax"; 1693 case AtomicRMWInst::UMin: 1694 return "umin"; 1695 case AtomicRMWInst::FAdd: 1696 return "fadd"; 1697 case AtomicRMWInst::FSub: 1698 return "fsub"; 1699 case AtomicRMWInst::BAD_BINOP: 1700 return "<invalid operation>"; 1701 } 1702 1703 llvm_unreachable("invalid atomicrmw operation"); 1704 } 1705 1706 //===----------------------------------------------------------------------===// 1707 // FenceInst Implementation 1708 //===----------------------------------------------------------------------===// 1709 1710 FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1711 SyncScope::ID SSID, 1712 Instruction *InsertBefore) 1713 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { 1714 setOrdering(Ordering); 1715 setSyncScopeID(SSID); 1716 } 1717 1718 FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1719 SyncScope::ID SSID, 1720 BasicBlock *InsertAtEnd) 1721 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { 1722 setOrdering(Ordering); 1723 setSyncScopeID(SSID); 1724 } 1725 1726 //===----------------------------------------------------------------------===// 1727 // GetElementPtrInst Implementation 1728 //===----------------------------------------------------------------------===// 1729 1730 void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, 1731 const Twine &Name) { 1732 assert(getNumOperands() == 1 + IdxList.size() && 1733 "NumOperands not initialized?"); 1734 Op<0>() = Ptr; 1735 llvm::copy(IdxList, op_begin() + 1); 1736 setName(Name); 1737 } 1738 1739 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) 1740 : Instruction(GEPI.getType(), GetElementPtr, 1741 OperandTraits<GetElementPtrInst>::op_end(this) - 1742 GEPI.getNumOperands(), 1743 GEPI.getNumOperands()), 1744 SourceElementType(GEPI.SourceElementType), 1745 ResultElementType(GEPI.ResultElementType) { 1746 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); 1747 SubclassOptionalData = GEPI.SubclassOptionalData; 1748 } 1749 1750 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) { 1751 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1752 if (!Struct->indexValid(Idx)) 1753 return nullptr; 1754 return Struct->getTypeAtIndex(Idx); 1755 } 1756 if (!Idx->getType()->isIntOrIntVectorTy()) 1757 return nullptr; 1758 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1759 return Array->getElementType(); 1760 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1761 return Vector->getElementType(); 1762 return nullptr; 1763 } 1764 1765 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) { 1766 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1767 if (Idx >= Struct->getNumElements()) 1768 return nullptr; 1769 return Struct->getElementType(Idx); 1770 } 1771 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1772 return Array->getElementType(); 1773 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1774 return Vector->getElementType(); 1775 return nullptr; 1776 } 1777 1778 template <typename IndexTy> 1779 static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) { 1780 if (IdxList.empty()) 1781 return Ty; 1782 for (IndexTy V : IdxList.slice(1)) { 1783 Ty = GetElementPtrInst::getTypeAtIndex(Ty, V); 1784 if (!Ty) 1785 return Ty; 1786 } 1787 return Ty; 1788 } 1789 1790 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { 1791 return getIndexedTypeInternal(Ty, IdxList); 1792 } 1793 1794 Type *GetElementPtrInst::getIndexedType(Type *Ty, 1795 ArrayRef<Constant *> IdxList) { 1796 return getIndexedTypeInternal(Ty, IdxList); 1797 } 1798 1799 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { 1800 return getIndexedTypeInternal(Ty, IdxList); 1801 } 1802 1803 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 1804 /// zeros. If so, the result pointer and the first operand have the same 1805 /// value, just potentially different types. 1806 bool GetElementPtrInst::hasAllZeroIndices() const { 1807 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1808 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { 1809 if (!CI->isZero()) return false; 1810 } else { 1811 return false; 1812 } 1813 } 1814 return true; 1815 } 1816 1817 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 1818 /// constant integers. If so, the result pointer and the first operand have 1819 /// a constant offset between them. 1820 bool GetElementPtrInst::hasAllConstantIndices() const { 1821 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1822 if (!isa<ConstantInt>(getOperand(i))) 1823 return false; 1824 } 1825 return true; 1826 } 1827 1828 void GetElementPtrInst::setIsInBounds(bool B) { 1829 cast<GEPOperator>(this)->setIsInBounds(B); 1830 } 1831 1832 bool GetElementPtrInst::isInBounds() const { 1833 return cast<GEPOperator>(this)->isInBounds(); 1834 } 1835 1836 bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, 1837 APInt &Offset) const { 1838 // Delegate to the generic GEPOperator implementation. 1839 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); 1840 } 1841 1842 bool GetElementPtrInst::collectOffset( 1843 const DataLayout &DL, unsigned BitWidth, 1844 MapVector<Value *, APInt> &VariableOffsets, 1845 APInt &ConstantOffset) const { 1846 // Delegate to the generic GEPOperator implementation. 1847 return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets, 1848 ConstantOffset); 1849 } 1850 1851 //===----------------------------------------------------------------------===// 1852 // ExtractElementInst Implementation 1853 //===----------------------------------------------------------------------===// 1854 1855 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1856 const Twine &Name, 1857 Instruction *InsertBef) 1858 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1859 ExtractElement, 1860 OperandTraits<ExtractElementInst>::op_begin(this), 1861 2, InsertBef) { 1862 assert(isValidOperands(Val, Index) && 1863 "Invalid extractelement instruction operands!"); 1864 Op<0>() = Val; 1865 Op<1>() = Index; 1866 setName(Name); 1867 } 1868 1869 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1870 const Twine &Name, 1871 BasicBlock *InsertAE) 1872 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1873 ExtractElement, 1874 OperandTraits<ExtractElementInst>::op_begin(this), 1875 2, InsertAE) { 1876 assert(isValidOperands(Val, Index) && 1877 "Invalid extractelement instruction operands!"); 1878 1879 Op<0>() = Val; 1880 Op<1>() = Index; 1881 setName(Name); 1882 } 1883 1884 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { 1885 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) 1886 return false; 1887 return true; 1888 } 1889 1890 //===----------------------------------------------------------------------===// 1891 // InsertElementInst Implementation 1892 //===----------------------------------------------------------------------===// 1893 1894 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1895 const Twine &Name, 1896 Instruction *InsertBef) 1897 : Instruction(Vec->getType(), InsertElement, 1898 OperandTraits<InsertElementInst>::op_begin(this), 1899 3, InsertBef) { 1900 assert(isValidOperands(Vec, Elt, Index) && 1901 "Invalid insertelement instruction operands!"); 1902 Op<0>() = Vec; 1903 Op<1>() = Elt; 1904 Op<2>() = Index; 1905 setName(Name); 1906 } 1907 1908 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1909 const Twine &Name, 1910 BasicBlock *InsertAE) 1911 : Instruction(Vec->getType(), InsertElement, 1912 OperandTraits<InsertElementInst>::op_begin(this), 1913 3, InsertAE) { 1914 assert(isValidOperands(Vec, Elt, Index) && 1915 "Invalid insertelement instruction operands!"); 1916 1917 Op<0>() = Vec; 1918 Op<1>() = Elt; 1919 Op<2>() = Index; 1920 setName(Name); 1921 } 1922 1923 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 1924 const Value *Index) { 1925 if (!Vec->getType()->isVectorTy()) 1926 return false; // First operand of insertelement must be vector type. 1927 1928 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) 1929 return false;// Second operand of insertelement must be vector element type. 1930 1931 if (!Index->getType()->isIntegerTy()) 1932 return false; // Third operand of insertelement must be i32. 1933 return true; 1934 } 1935 1936 //===----------------------------------------------------------------------===// 1937 // ShuffleVectorInst Implementation 1938 //===----------------------------------------------------------------------===// 1939 1940 static Value *createPlaceholderForShuffleVector(Value *V) { 1941 assert(V && "Cannot create placeholder of nullptr V"); 1942 return PoisonValue::get(V->getType()); 1943 } 1944 1945 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *Mask, const Twine &Name, 1946 Instruction *InsertBefore) 1947 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1948 InsertBefore) {} 1949 1950 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *Mask, const Twine &Name, 1951 BasicBlock *InsertAtEnd) 1952 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1953 InsertAtEnd) {} 1954 1955 ShuffleVectorInst::ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, 1956 const Twine &Name, 1957 Instruction *InsertBefore) 1958 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1959 InsertBefore) {} 1960 1961 ShuffleVectorInst::ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, 1962 const Twine &Name, BasicBlock *InsertAtEnd) 1963 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1964 InsertAtEnd) {} 1965 1966 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1967 const Twine &Name, 1968 Instruction *InsertBefore) 1969 : Instruction( 1970 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1971 cast<VectorType>(Mask->getType())->getElementCount()), 1972 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1973 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 1974 assert(isValidOperands(V1, V2, Mask) && 1975 "Invalid shuffle vector instruction operands!"); 1976 1977 Op<0>() = V1; 1978 Op<1>() = V2; 1979 SmallVector<int, 16> MaskArr; 1980 getShuffleMask(cast<Constant>(Mask), MaskArr); 1981 setShuffleMask(MaskArr); 1982 setName(Name); 1983 } 1984 1985 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1986 const Twine &Name, BasicBlock *InsertAtEnd) 1987 : Instruction( 1988 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1989 cast<VectorType>(Mask->getType())->getElementCount()), 1990 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1991 OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { 1992 assert(isValidOperands(V1, V2, Mask) && 1993 "Invalid shuffle vector instruction operands!"); 1994 1995 Op<0>() = V1; 1996 Op<1>() = V2; 1997 SmallVector<int, 16> MaskArr; 1998 getShuffleMask(cast<Constant>(Mask), MaskArr); 1999 setShuffleMask(MaskArr); 2000 setName(Name); 2001 } 2002 2003 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, 2004 const Twine &Name, 2005 Instruction *InsertBefore) 2006 : Instruction( 2007 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 2008 Mask.size(), isa<ScalableVectorType>(V1->getType())), 2009 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 2010 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 2011 assert(isValidOperands(V1, V2, Mask) && 2012 "Invalid shuffle vector instruction operands!"); 2013 Op<0>() = V1; 2014 Op<1>() = V2; 2015 setShuffleMask(Mask); 2016 setName(Name); 2017 } 2018 2019 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, 2020 const Twine &Name, BasicBlock *InsertAtEnd) 2021 : Instruction( 2022 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 2023 Mask.size(), isa<ScalableVectorType>(V1->getType())), 2024 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 2025 OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { 2026 assert(isValidOperands(V1, V2, Mask) && 2027 "Invalid shuffle vector instruction operands!"); 2028 2029 Op<0>() = V1; 2030 Op<1>() = V2; 2031 setShuffleMask(Mask); 2032 setName(Name); 2033 } 2034 2035 void ShuffleVectorInst::commute() { 2036 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2037 int NumMaskElts = ShuffleMask.size(); 2038 SmallVector<int, 16> NewMask(NumMaskElts); 2039 for (int i = 0; i != NumMaskElts; ++i) { 2040 int MaskElt = getMaskValue(i); 2041 if (MaskElt == UndefMaskElem) { 2042 NewMask[i] = UndefMaskElem; 2043 continue; 2044 } 2045 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask"); 2046 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; 2047 NewMask[i] = MaskElt; 2048 } 2049 setShuffleMask(NewMask); 2050 Op<0>().swap(Op<1>()); 2051 } 2052 2053 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 2054 ArrayRef<int> Mask) { 2055 // V1 and V2 must be vectors of the same type. 2056 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) 2057 return false; 2058 2059 // Make sure the mask elements make sense. 2060 int V1Size = 2061 cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue(); 2062 for (int Elem : Mask) 2063 if (Elem != UndefMaskElem && Elem >= V1Size * 2) 2064 return false; 2065 2066 if (isa<ScalableVectorType>(V1->getType())) 2067 if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !is_splat(Mask)) 2068 return false; 2069 2070 return true; 2071 } 2072 2073 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 2074 const Value *Mask) { 2075 // V1 and V2 must be vectors of the same type. 2076 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) 2077 return false; 2078 2079 // Mask must be vector of i32, and must be the same kind of vector as the 2080 // input vectors 2081 auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); 2082 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) || 2083 isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType())) 2084 return false; 2085 2086 // Check to see if Mask is valid. 2087 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) 2088 return true; 2089 2090 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { 2091 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 2092 for (Value *Op : MV->operands()) { 2093 if (auto *CI = dyn_cast<ConstantInt>(Op)) { 2094 if (CI->uge(V1Size*2)) 2095 return false; 2096 } else if (!isa<UndefValue>(Op)) { 2097 return false; 2098 } 2099 } 2100 return true; 2101 } 2102 2103 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 2104 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 2105 for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements(); 2106 i != e; ++i) 2107 if (CDS->getElementAsInteger(i) >= V1Size*2) 2108 return false; 2109 return true; 2110 } 2111 2112 return false; 2113 } 2114 2115 void ShuffleVectorInst::getShuffleMask(const Constant *Mask, 2116 SmallVectorImpl<int> &Result) { 2117 ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount(); 2118 2119 if (isa<ConstantAggregateZero>(Mask)) { 2120 Result.resize(EC.getKnownMinValue(), 0); 2121 return; 2122 } 2123 2124 Result.reserve(EC.getKnownMinValue()); 2125 2126 if (EC.isScalable()) { 2127 assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) && 2128 "Scalable vector shuffle mask must be undef or zeroinitializer"); 2129 int MaskVal = isa<UndefValue>(Mask) ? -1 : 0; 2130 for (unsigned I = 0; I < EC.getKnownMinValue(); ++I) 2131 Result.emplace_back(MaskVal); 2132 return; 2133 } 2134 2135 unsigned NumElts = EC.getKnownMinValue(); 2136 2137 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 2138 for (unsigned i = 0; i != NumElts; ++i) 2139 Result.push_back(CDS->getElementAsInteger(i)); 2140 return; 2141 } 2142 for (unsigned i = 0; i != NumElts; ++i) { 2143 Constant *C = Mask->getAggregateElement(i); 2144 Result.push_back(isa<UndefValue>(C) ? -1 : 2145 cast<ConstantInt>(C)->getZExtValue()); 2146 } 2147 } 2148 2149 void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) { 2150 ShuffleMask.assign(Mask.begin(), Mask.end()); 2151 ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType()); 2152 } 2153 2154 Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask, 2155 Type *ResultTy) { 2156 Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext()); 2157 if (isa<ScalableVectorType>(ResultTy)) { 2158 assert(is_splat(Mask) && "Unexpected shuffle"); 2159 Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true); 2160 if (Mask[0] == 0) 2161 return Constant::getNullValue(VecTy); 2162 return UndefValue::get(VecTy); 2163 } 2164 SmallVector<Constant *, 16> MaskConst; 2165 for (int Elem : Mask) { 2166 if (Elem == UndefMaskElem) 2167 MaskConst.push_back(UndefValue::get(Int32Ty)); 2168 else 2169 MaskConst.push_back(ConstantInt::get(Int32Ty, Elem)); 2170 } 2171 return ConstantVector::get(MaskConst); 2172 } 2173 2174 static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 2175 assert(!Mask.empty() && "Shuffle mask must contain elements"); 2176 bool UsesLHS = false; 2177 bool UsesRHS = false; 2178 for (int I : Mask) { 2179 if (I == -1) 2180 continue; 2181 assert(I >= 0 && I < (NumOpElts * 2) && 2182 "Out-of-bounds shuffle mask element"); 2183 UsesLHS |= (I < NumOpElts); 2184 UsesRHS |= (I >= NumOpElts); 2185 if (UsesLHS && UsesRHS) 2186 return false; 2187 } 2188 // Allow for degenerate case: completely undef mask means neither source is used. 2189 return UsesLHS || UsesRHS; 2190 } 2191 2192 bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { 2193 // We don't have vector operand size information, so assume operands are the 2194 // same size as the mask. 2195 return isSingleSourceMaskImpl(Mask, Mask.size()); 2196 } 2197 2198 static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 2199 if (!isSingleSourceMaskImpl(Mask, NumOpElts)) 2200 return false; 2201 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { 2202 if (Mask[i] == -1) 2203 continue; 2204 if (Mask[i] != i && Mask[i] != (NumOpElts + i)) 2205 return false; 2206 } 2207 return true; 2208 } 2209 2210 bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { 2211 // We don't have vector operand size information, so assume operands are the 2212 // same size as the mask. 2213 return isIdentityMaskImpl(Mask, Mask.size()); 2214 } 2215 2216 bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { 2217 if (!isSingleSourceMask(Mask)) 2218 return false; 2219 2220 // The number of elements in the mask must be at least 2. 2221 int NumElts = Mask.size(); 2222 if (NumElts < 2) 2223 return false; 2224 2225 for (int i = 0; i < NumElts; ++i) { 2226 if (Mask[i] == -1) 2227 continue; 2228 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) 2229 return false; 2230 } 2231 return true; 2232 } 2233 2234 bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { 2235 if (!isSingleSourceMask(Mask)) 2236 return false; 2237 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { 2238 if (Mask[i] == -1) 2239 continue; 2240 if (Mask[i] != 0 && Mask[i] != NumElts) 2241 return false; 2242 } 2243 return true; 2244 } 2245 2246 bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { 2247 // Select is differentiated from identity. It requires using both sources. 2248 if (isSingleSourceMask(Mask)) 2249 return false; 2250 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { 2251 if (Mask[i] == -1) 2252 continue; 2253 if (Mask[i] != i && Mask[i] != (NumElts + i)) 2254 return false; 2255 } 2256 return true; 2257 } 2258 2259 bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { 2260 // Example masks that will return true: 2261 // v1 = <a, b, c, d> 2262 // v2 = <e, f, g, h> 2263 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> 2264 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> 2265 2266 // 1. The number of elements in the mask must be a power-of-2 and at least 2. 2267 int NumElts = Mask.size(); 2268 if (NumElts < 2 || !isPowerOf2_32(NumElts)) 2269 return false; 2270 2271 // 2. The first element of the mask must be either a 0 or a 1. 2272 if (Mask[0] != 0 && Mask[0] != 1) 2273 return false; 2274 2275 // 3. The difference between the first 2 elements must be equal to the 2276 // number of elements in the mask. 2277 if ((Mask[1] - Mask[0]) != NumElts) 2278 return false; 2279 2280 // 4. The difference between consecutive even-numbered and odd-numbered 2281 // elements must be equal to 2. 2282 for (int i = 2; i < NumElts; ++i) { 2283 int MaskEltVal = Mask[i]; 2284 if (MaskEltVal == -1) 2285 return false; 2286 int MaskEltPrevVal = Mask[i - 2]; 2287 if (MaskEltVal - MaskEltPrevVal != 2) 2288 return false; 2289 } 2290 return true; 2291 } 2292 2293 bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, 2294 int NumSrcElts, int &Index) { 2295 // Must extract from a single source. 2296 if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) 2297 return false; 2298 2299 // Must be smaller (else this is an Identity shuffle). 2300 if (NumSrcElts <= (int)Mask.size()) 2301 return false; 2302 2303 // Find start of extraction, accounting that we may start with an UNDEF. 2304 int SubIndex = -1; 2305 for (int i = 0, e = Mask.size(); i != e; ++i) { 2306 int M = Mask[i]; 2307 if (M < 0) 2308 continue; 2309 int Offset = (M % NumSrcElts) - i; 2310 if (0 <= SubIndex && SubIndex != Offset) 2311 return false; 2312 SubIndex = Offset; 2313 } 2314 2315 if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) { 2316 Index = SubIndex; 2317 return true; 2318 } 2319 return false; 2320 } 2321 2322 bool ShuffleVectorInst::isInsertSubvectorMask(ArrayRef<int> Mask, 2323 int NumSrcElts, int &NumSubElts, 2324 int &Index) { 2325 int NumMaskElts = Mask.size(); 2326 2327 // Don't try to match if we're shuffling to a smaller size. 2328 if (NumMaskElts < NumSrcElts) 2329 return false; 2330 2331 // TODO: We don't recognize self-insertion/widening. 2332 if (isSingleSourceMaskImpl(Mask, NumSrcElts)) 2333 return false; 2334 2335 // Determine which mask elements are attributed to which source. 2336 APInt UndefElts = APInt::getZero(NumMaskElts); 2337 APInt Src0Elts = APInt::getZero(NumMaskElts); 2338 APInt Src1Elts = APInt::getZero(NumMaskElts); 2339 bool Src0Identity = true; 2340 bool Src1Identity = true; 2341 2342 for (int i = 0; i != NumMaskElts; ++i) { 2343 int M = Mask[i]; 2344 if (M < 0) { 2345 UndefElts.setBit(i); 2346 continue; 2347 } 2348 if (M < NumSrcElts) { 2349 Src0Elts.setBit(i); 2350 Src0Identity &= (M == i); 2351 continue; 2352 } 2353 Src1Elts.setBit(i); 2354 Src1Identity &= (M == (i + NumSrcElts)); 2355 } 2356 assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() && 2357 "unknown shuffle elements"); 2358 assert(!Src0Elts.isZero() && !Src1Elts.isZero() && 2359 "2-source shuffle not found"); 2360 2361 // Determine lo/hi span ranges. 2362 // TODO: How should we handle undefs at the start of subvector insertions? 2363 int Src0Lo = Src0Elts.countTrailingZeros(); 2364 int Src1Lo = Src1Elts.countTrailingZeros(); 2365 int Src0Hi = NumMaskElts - Src0Elts.countLeadingZeros(); 2366 int Src1Hi = NumMaskElts - Src1Elts.countLeadingZeros(); 2367 2368 // If src0 is in place, see if the src1 elements is inplace within its own 2369 // span. 2370 if (Src0Identity) { 2371 int NumSub1Elts = Src1Hi - Src1Lo; 2372 ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts); 2373 if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) { 2374 NumSubElts = NumSub1Elts; 2375 Index = Src1Lo; 2376 return true; 2377 } 2378 } 2379 2380 // If src1 is in place, see if the src0 elements is inplace within its own 2381 // span. 2382 if (Src1Identity) { 2383 int NumSub0Elts = Src0Hi - Src0Lo; 2384 ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts); 2385 if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) { 2386 NumSubElts = NumSub0Elts; 2387 Index = Src0Lo; 2388 return true; 2389 } 2390 } 2391 2392 return false; 2393 } 2394 2395 bool ShuffleVectorInst::isIdentityWithPadding() const { 2396 if (isa<UndefValue>(Op<2>())) 2397 return false; 2398 2399 // FIXME: Not currently possible to express a shuffle mask for a scalable 2400 // vector for this case. 2401 if (isa<ScalableVectorType>(getType())) 2402 return false; 2403 2404 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2405 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2406 if (NumMaskElts <= NumOpElts) 2407 return false; 2408 2409 // The first part of the mask must choose elements from exactly 1 source op. 2410 ArrayRef<int> Mask = getShuffleMask(); 2411 if (!isIdentityMaskImpl(Mask, NumOpElts)) 2412 return false; 2413 2414 // All extending must be with undef elements. 2415 for (int i = NumOpElts; i < NumMaskElts; ++i) 2416 if (Mask[i] != -1) 2417 return false; 2418 2419 return true; 2420 } 2421 2422 bool ShuffleVectorInst::isIdentityWithExtract() const { 2423 if (isa<UndefValue>(Op<2>())) 2424 return false; 2425 2426 // FIXME: Not currently possible to express a shuffle mask for a scalable 2427 // vector for this case. 2428 if (isa<ScalableVectorType>(getType())) 2429 return false; 2430 2431 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2432 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2433 if (NumMaskElts >= NumOpElts) 2434 return false; 2435 2436 return isIdentityMaskImpl(getShuffleMask(), NumOpElts); 2437 } 2438 2439 bool ShuffleVectorInst::isConcat() const { 2440 // Vector concatenation is differentiated from identity with padding. 2441 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) || 2442 isa<UndefValue>(Op<2>())) 2443 return false; 2444 2445 // FIXME: Not currently possible to express a shuffle mask for a scalable 2446 // vector for this case. 2447 if (isa<ScalableVectorType>(getType())) 2448 return false; 2449 2450 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2451 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2452 if (NumMaskElts != NumOpElts * 2) 2453 return false; 2454 2455 // Use the mask length rather than the operands' vector lengths here. We 2456 // already know that the shuffle returns a vector twice as long as the inputs, 2457 // and neither of the inputs are undef vectors. If the mask picks consecutive 2458 // elements from both inputs, then this is a concatenation of the inputs. 2459 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); 2460 } 2461 2462 static bool isReplicationMaskWithParams(ArrayRef<int> Mask, 2463 int ReplicationFactor, int VF) { 2464 assert(Mask.size() == (unsigned)ReplicationFactor * VF && 2465 "Unexpected mask size."); 2466 2467 for (int CurrElt : seq(0, VF)) { 2468 ArrayRef<int> CurrSubMask = Mask.take_front(ReplicationFactor); 2469 assert(CurrSubMask.size() == (unsigned)ReplicationFactor && 2470 "Run out of mask?"); 2471 Mask = Mask.drop_front(ReplicationFactor); 2472 if (!all_of(CurrSubMask, [CurrElt](int MaskElt) { 2473 return MaskElt == UndefMaskElem || MaskElt == CurrElt; 2474 })) 2475 return false; 2476 } 2477 assert(Mask.empty() && "Did not consume the whole mask?"); 2478 2479 return true; 2480 } 2481 2482 bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask, 2483 int &ReplicationFactor, int &VF) { 2484 // undef-less case is trivial. 2485 if (none_of(Mask, [](int MaskElt) { return MaskElt == UndefMaskElem; })) { 2486 ReplicationFactor = 2487 Mask.take_while([](int MaskElt) { return MaskElt == 0; }).size(); 2488 if (ReplicationFactor == 0 || Mask.size() % ReplicationFactor != 0) 2489 return false; 2490 VF = Mask.size() / ReplicationFactor; 2491 return isReplicationMaskWithParams(Mask, ReplicationFactor, VF); 2492 } 2493 2494 // However, if the mask contains undef's, we have to enumerate possible tuples 2495 // and pick one. There are bounds on replication factor: [1, mask size] 2496 // (where RF=1 is an identity shuffle, RF=mask size is a broadcast shuffle) 2497 // Additionally, mask size is a replication factor multiplied by vector size, 2498 // which further significantly reduces the search space. 2499 2500 // Before doing that, let's perform basic correctness checking first. 2501 int Largest = -1; 2502 for (int MaskElt : Mask) { 2503 if (MaskElt == UndefMaskElem) 2504 continue; 2505 // Elements must be in non-decreasing order. 2506 if (MaskElt < Largest) 2507 return false; 2508 Largest = std::max(Largest, MaskElt); 2509 } 2510 2511 // Prefer larger replication factor if all else equal. 2512 for (int PossibleReplicationFactor : 2513 reverse(seq_inclusive<unsigned>(1, Mask.size()))) { 2514 if (Mask.size() % PossibleReplicationFactor != 0) 2515 continue; 2516 int PossibleVF = Mask.size() / PossibleReplicationFactor; 2517 if (!isReplicationMaskWithParams(Mask, PossibleReplicationFactor, 2518 PossibleVF)) 2519 continue; 2520 ReplicationFactor = PossibleReplicationFactor; 2521 VF = PossibleVF; 2522 return true; 2523 } 2524 2525 return false; 2526 } 2527 2528 bool ShuffleVectorInst::isReplicationMask(int &ReplicationFactor, 2529 int &VF) const { 2530 // Not possible to express a shuffle mask for a scalable vector for this 2531 // case. 2532 if (isa<ScalableVectorType>(getType())) 2533 return false; 2534 2535 VF = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2536 if (ShuffleMask.size() % VF != 0) 2537 return false; 2538 ReplicationFactor = ShuffleMask.size() / VF; 2539 2540 return isReplicationMaskWithParams(ShuffleMask, ReplicationFactor, VF); 2541 } 2542 2543 //===----------------------------------------------------------------------===// 2544 // InsertValueInst Class 2545 //===----------------------------------------------------------------------===// 2546 2547 void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 2548 const Twine &Name) { 2549 assert(getNumOperands() == 2 && "NumOperands not initialized?"); 2550 2551 // There's no fundamental reason why we require at least one index 2552 // (other than weirdness with &*IdxBegin being invalid; see 2553 // getelementptr's init routine for example). But there's no 2554 // present need to support it. 2555 assert(!Idxs.empty() && "InsertValueInst must have at least one index"); 2556 2557 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == 2558 Val->getType() && "Inserted value must match indexed type!"); 2559 Op<0>() = Agg; 2560 Op<1>() = Val; 2561 2562 Indices.append(Idxs.begin(), Idxs.end()); 2563 setName(Name); 2564 } 2565 2566 InsertValueInst::InsertValueInst(const InsertValueInst &IVI) 2567 : Instruction(IVI.getType(), InsertValue, 2568 OperandTraits<InsertValueInst>::op_begin(this), 2), 2569 Indices(IVI.Indices) { 2570 Op<0>() = IVI.getOperand(0); 2571 Op<1>() = IVI.getOperand(1); 2572 SubclassOptionalData = IVI.SubclassOptionalData; 2573 } 2574 2575 //===----------------------------------------------------------------------===// 2576 // ExtractValueInst Class 2577 //===----------------------------------------------------------------------===// 2578 2579 void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { 2580 assert(getNumOperands() == 1 && "NumOperands not initialized?"); 2581 2582 // There's no fundamental reason why we require at least one index. 2583 // But there's no present need to support it. 2584 assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); 2585 2586 Indices.append(Idxs.begin(), Idxs.end()); 2587 setName(Name); 2588 } 2589 2590 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) 2591 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), 2592 Indices(EVI.Indices) { 2593 SubclassOptionalData = EVI.SubclassOptionalData; 2594 } 2595 2596 // getIndexedType - Returns the type of the element that would be extracted 2597 // with an extractvalue instruction with the specified parameters. 2598 // 2599 // A null type is returned if the indices are invalid for the specified 2600 // pointer type. 2601 // 2602 Type *ExtractValueInst::getIndexedType(Type *Agg, 2603 ArrayRef<unsigned> Idxs) { 2604 for (unsigned Index : Idxs) { 2605 // We can't use CompositeType::indexValid(Index) here. 2606 // indexValid() always returns true for arrays because getelementptr allows 2607 // out-of-bounds indices. Since we don't allow those for extractvalue and 2608 // insertvalue we need to check array indexing manually. 2609 // Since the only other types we can index into are struct types it's just 2610 // as easy to check those manually as well. 2611 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { 2612 if (Index >= AT->getNumElements()) 2613 return nullptr; 2614 Agg = AT->getElementType(); 2615 } else if (StructType *ST = dyn_cast<StructType>(Agg)) { 2616 if (Index >= ST->getNumElements()) 2617 return nullptr; 2618 Agg = ST->getElementType(Index); 2619 } else { 2620 // Not a valid type to index into. 2621 return nullptr; 2622 } 2623 } 2624 return const_cast<Type*>(Agg); 2625 } 2626 2627 //===----------------------------------------------------------------------===// 2628 // UnaryOperator Class 2629 //===----------------------------------------------------------------------===// 2630 2631 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, 2632 Type *Ty, const Twine &Name, 2633 Instruction *InsertBefore) 2634 : UnaryInstruction(Ty, iType, S, InsertBefore) { 2635 Op<0>() = S; 2636 setName(Name); 2637 AssertOK(); 2638 } 2639 2640 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, 2641 Type *Ty, const Twine &Name, 2642 BasicBlock *InsertAtEnd) 2643 : UnaryInstruction(Ty, iType, S, InsertAtEnd) { 2644 Op<0>() = S; 2645 setName(Name); 2646 AssertOK(); 2647 } 2648 2649 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, 2650 const Twine &Name, 2651 Instruction *InsertBefore) { 2652 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); 2653 } 2654 2655 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, 2656 const Twine &Name, 2657 BasicBlock *InsertAtEnd) { 2658 UnaryOperator *Res = Create(Op, S, Name); 2659 InsertAtEnd->getInstList().push_back(Res); 2660 return Res; 2661 } 2662 2663 void UnaryOperator::AssertOK() { 2664 Value *LHS = getOperand(0); 2665 (void)LHS; // Silence warnings. 2666 #ifndef NDEBUG 2667 switch (getOpcode()) { 2668 case FNeg: 2669 assert(getType() == LHS->getType() && 2670 "Unary operation should return same type as operand!"); 2671 assert(getType()->isFPOrFPVectorTy() && 2672 "Tried to create a floating-point operation on a " 2673 "non-floating-point type!"); 2674 break; 2675 default: llvm_unreachable("Invalid opcode provided"); 2676 } 2677 #endif 2678 } 2679 2680 //===----------------------------------------------------------------------===// 2681 // BinaryOperator Class 2682 //===----------------------------------------------------------------------===// 2683 2684 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 2685 Type *Ty, const Twine &Name, 2686 Instruction *InsertBefore) 2687 : Instruction(Ty, iType, 2688 OperandTraits<BinaryOperator>::op_begin(this), 2689 OperandTraits<BinaryOperator>::operands(this), 2690 InsertBefore) { 2691 Op<0>() = S1; 2692 Op<1>() = S2; 2693 setName(Name); 2694 AssertOK(); 2695 } 2696 2697 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 2698 Type *Ty, const Twine &Name, 2699 BasicBlock *InsertAtEnd) 2700 : Instruction(Ty, iType, 2701 OperandTraits<BinaryOperator>::op_begin(this), 2702 OperandTraits<BinaryOperator>::operands(this), 2703 InsertAtEnd) { 2704 Op<0>() = S1; 2705 Op<1>() = S2; 2706 setName(Name); 2707 AssertOK(); 2708 } 2709 2710 void BinaryOperator::AssertOK() { 2711 Value *LHS = getOperand(0), *RHS = getOperand(1); 2712 (void)LHS; (void)RHS; // Silence warnings. 2713 assert(LHS->getType() == RHS->getType() && 2714 "Binary operator operand types must match!"); 2715 #ifndef NDEBUG 2716 switch (getOpcode()) { 2717 case Add: case Sub: 2718 case Mul: 2719 assert(getType() == LHS->getType() && 2720 "Arithmetic operation should return same type as operands!"); 2721 assert(getType()->isIntOrIntVectorTy() && 2722 "Tried to create an integer operation on a non-integer type!"); 2723 break; 2724 case FAdd: case FSub: 2725 case FMul: 2726 assert(getType() == LHS->getType() && 2727 "Arithmetic operation should return same type as operands!"); 2728 assert(getType()->isFPOrFPVectorTy() && 2729 "Tried to create a floating-point operation on a " 2730 "non-floating-point type!"); 2731 break; 2732 case UDiv: 2733 case SDiv: 2734 assert(getType() == LHS->getType() && 2735 "Arithmetic operation should return same type as operands!"); 2736 assert(getType()->isIntOrIntVectorTy() && 2737 "Incorrect operand type (not integer) for S/UDIV"); 2738 break; 2739 case FDiv: 2740 assert(getType() == LHS->getType() && 2741 "Arithmetic operation should return same type as operands!"); 2742 assert(getType()->isFPOrFPVectorTy() && 2743 "Incorrect operand type (not floating point) for FDIV"); 2744 break; 2745 case URem: 2746 case SRem: 2747 assert(getType() == LHS->getType() && 2748 "Arithmetic operation should return same type as operands!"); 2749 assert(getType()->isIntOrIntVectorTy() && 2750 "Incorrect operand type (not integer) for S/UREM"); 2751 break; 2752 case FRem: 2753 assert(getType() == LHS->getType() && 2754 "Arithmetic operation should return same type as operands!"); 2755 assert(getType()->isFPOrFPVectorTy() && 2756 "Incorrect operand type (not floating point) for FREM"); 2757 break; 2758 case Shl: 2759 case LShr: 2760 case AShr: 2761 assert(getType() == LHS->getType() && 2762 "Shift operation should return same type as operands!"); 2763 assert(getType()->isIntOrIntVectorTy() && 2764 "Tried to create a shift operation on a non-integral type!"); 2765 break; 2766 case And: case Or: 2767 case Xor: 2768 assert(getType() == LHS->getType() && 2769 "Logical operation should return same type as operands!"); 2770 assert(getType()->isIntOrIntVectorTy() && 2771 "Tried to create a logical operation on a non-integral type!"); 2772 break; 2773 default: llvm_unreachable("Invalid opcode provided"); 2774 } 2775 #endif 2776 } 2777 2778 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 2779 const Twine &Name, 2780 Instruction *InsertBefore) { 2781 assert(S1->getType() == S2->getType() && 2782 "Cannot create binary operator with two operands of differing type!"); 2783 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); 2784 } 2785 2786 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 2787 const Twine &Name, 2788 BasicBlock *InsertAtEnd) { 2789 BinaryOperator *Res = Create(Op, S1, S2, Name); 2790 InsertAtEnd->getInstList().push_back(Res); 2791 return Res; 2792 } 2793 2794 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 2795 Instruction *InsertBefore) { 2796 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2797 return new BinaryOperator(Instruction::Sub, 2798 zero, Op, 2799 Op->getType(), Name, InsertBefore); 2800 } 2801 2802 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 2803 BasicBlock *InsertAtEnd) { 2804 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2805 return new BinaryOperator(Instruction::Sub, 2806 zero, Op, 2807 Op->getType(), Name, InsertAtEnd); 2808 } 2809 2810 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 2811 Instruction *InsertBefore) { 2812 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2813 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); 2814 } 2815 2816 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 2817 BasicBlock *InsertAtEnd) { 2818 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2819 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); 2820 } 2821 2822 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 2823 Instruction *InsertBefore) { 2824 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2825 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); 2826 } 2827 2828 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 2829 BasicBlock *InsertAtEnd) { 2830 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2831 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); 2832 } 2833 2834 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 2835 Instruction *InsertBefore) { 2836 Constant *C = Constant::getAllOnesValue(Op->getType()); 2837 return new BinaryOperator(Instruction::Xor, Op, C, 2838 Op->getType(), Name, InsertBefore); 2839 } 2840 2841 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 2842 BasicBlock *InsertAtEnd) { 2843 Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); 2844 return new BinaryOperator(Instruction::Xor, Op, AllOnes, 2845 Op->getType(), Name, InsertAtEnd); 2846 } 2847 2848 // Exchange the two operands to this instruction. This instruction is safe to 2849 // use on any binary instruction and does not modify the semantics of the 2850 // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode 2851 // is changed. 2852 bool BinaryOperator::swapOperands() { 2853 if (!isCommutative()) 2854 return true; // Can't commute operands 2855 Op<0>().swap(Op<1>()); 2856 return false; 2857 } 2858 2859 //===----------------------------------------------------------------------===// 2860 // FPMathOperator Class 2861 //===----------------------------------------------------------------------===// 2862 2863 float FPMathOperator::getFPAccuracy() const { 2864 const MDNode *MD = 2865 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); 2866 if (!MD) 2867 return 0.0; 2868 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); 2869 return Accuracy->getValueAPF().convertToFloat(); 2870 } 2871 2872 //===----------------------------------------------------------------------===// 2873 // CastInst Class 2874 //===----------------------------------------------------------------------===// 2875 2876 // Just determine if this cast only deals with integral->integral conversion. 2877 bool CastInst::isIntegerCast() const { 2878 switch (getOpcode()) { 2879 default: return false; 2880 case Instruction::ZExt: 2881 case Instruction::SExt: 2882 case Instruction::Trunc: 2883 return true; 2884 case Instruction::BitCast: 2885 return getOperand(0)->getType()->isIntegerTy() && 2886 getType()->isIntegerTy(); 2887 } 2888 } 2889 2890 bool CastInst::isLosslessCast() const { 2891 // Only BitCast can be lossless, exit fast if we're not BitCast 2892 if (getOpcode() != Instruction::BitCast) 2893 return false; 2894 2895 // Identity cast is always lossless 2896 Type *SrcTy = getOperand(0)->getType(); 2897 Type *DstTy = getType(); 2898 if (SrcTy == DstTy) 2899 return true; 2900 2901 // Pointer to pointer is always lossless. 2902 if (SrcTy->isPointerTy()) 2903 return DstTy->isPointerTy(); 2904 return false; // Other types have no identity values 2905 } 2906 2907 /// This function determines if the CastInst does not require any bits to be 2908 /// changed in order to effect the cast. Essentially, it identifies cases where 2909 /// no code gen is necessary for the cast, hence the name no-op cast. For 2910 /// example, the following are all no-op casts: 2911 /// # bitcast i32* %x to i8* 2912 /// # bitcast <2 x i32> %x to <4 x i16> 2913 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only 2914 /// Determine if the described cast is a no-op. 2915 bool CastInst::isNoopCast(Instruction::CastOps Opcode, 2916 Type *SrcTy, 2917 Type *DestTy, 2918 const DataLayout &DL) { 2919 assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition"); 2920 switch (Opcode) { 2921 default: llvm_unreachable("Invalid CastOp"); 2922 case Instruction::Trunc: 2923 case Instruction::ZExt: 2924 case Instruction::SExt: 2925 case Instruction::FPTrunc: 2926 case Instruction::FPExt: 2927 case Instruction::UIToFP: 2928 case Instruction::SIToFP: 2929 case Instruction::FPToUI: 2930 case Instruction::FPToSI: 2931 case Instruction::AddrSpaceCast: 2932 // TODO: Target informations may give a more accurate answer here. 2933 return false; 2934 case Instruction::BitCast: 2935 return true; // BitCast never modifies bits. 2936 case Instruction::PtrToInt: 2937 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == 2938 DestTy->getScalarSizeInBits(); 2939 case Instruction::IntToPtr: 2940 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == 2941 SrcTy->getScalarSizeInBits(); 2942 } 2943 } 2944 2945 bool CastInst::isNoopCast(const DataLayout &DL) const { 2946 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); 2947 } 2948 2949 /// This function determines if a pair of casts can be eliminated and what 2950 /// opcode should be used in the elimination. This assumes that there are two 2951 /// instructions like this: 2952 /// * %F = firstOpcode SrcTy %x to MidTy 2953 /// * %S = secondOpcode MidTy %F to DstTy 2954 /// The function returns a resultOpcode so these two casts can be replaced with: 2955 /// * %Replacement = resultOpcode %SrcTy %x to DstTy 2956 /// If no such cast is permitted, the function returns 0. 2957 unsigned CastInst::isEliminableCastPair( 2958 Instruction::CastOps firstOp, Instruction::CastOps secondOp, 2959 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, 2960 Type *DstIntPtrTy) { 2961 // Define the 144 possibilities for these two cast instructions. The values 2962 // in this matrix determine what to do in a given situation and select the 2963 // case in the switch below. The rows correspond to firstOp, the columns 2964 // correspond to secondOp. In looking at the table below, keep in mind 2965 // the following cast properties: 2966 // 2967 // Size Compare Source Destination 2968 // Operator Src ? Size Type Sign Type Sign 2969 // -------- ------------ ------------------- --------------------- 2970 // TRUNC > Integer Any Integral Any 2971 // ZEXT < Integral Unsigned Integer Any 2972 // SEXT < Integral Signed Integer Any 2973 // FPTOUI n/a FloatPt n/a Integral Unsigned 2974 // FPTOSI n/a FloatPt n/a Integral Signed 2975 // UITOFP n/a Integral Unsigned FloatPt n/a 2976 // SITOFP n/a Integral Signed FloatPt n/a 2977 // FPTRUNC > FloatPt n/a FloatPt n/a 2978 // FPEXT < FloatPt n/a FloatPt n/a 2979 // PTRTOINT n/a Pointer n/a Integral Unsigned 2980 // INTTOPTR n/a Integral Unsigned Pointer n/a 2981 // BITCAST = FirstClass n/a FirstClass n/a 2982 // ADDRSPCST n/a Pointer n/a Pointer n/a 2983 // 2984 // NOTE: some transforms are safe, but we consider them to be non-profitable. 2985 // For example, we could merge "fptoui double to i32" + "zext i32 to i64", 2986 // into "fptoui double to i64", but this loses information about the range 2987 // of the produced value (we no longer know the top-part is all zeros). 2988 // Further this conversion is often much more expensive for typical hardware, 2989 // and causes issues when building libgcc. We disallow fptosi+sext for the 2990 // same reason. 2991 const unsigned numCastOps = 2992 Instruction::CastOpsEnd - Instruction::CastOpsBegin; 2993 static const uint8_t CastResults[numCastOps][numCastOps] = { 2994 // T F F U S F F P I B A -+ 2995 // R Z S P P I I T P 2 N T S | 2996 // U E E 2 2 2 2 R E I T C C +- secondOp 2997 // N X X U S F F N X N 2 V V | 2998 // C T T I I P P C T T P T T -+ 2999 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ 3000 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | 3001 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | 3002 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | 3003 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | 3004 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp 3005 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | 3006 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | 3007 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | 3008 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | 3009 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | 3010 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | 3011 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ 3012 }; 3013 3014 // TODO: This logic could be encoded into the table above and handled in the 3015 // switch below. 3016 // If either of the casts are a bitcast from scalar to vector, disallow the 3017 // merging. However, any pair of bitcasts are allowed. 3018 bool IsFirstBitcast = (firstOp == Instruction::BitCast); 3019 bool IsSecondBitcast = (secondOp == Instruction::BitCast); 3020 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; 3021 3022 // Check if any of the casts convert scalars <-> vectors. 3023 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || 3024 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) 3025 if (!AreBothBitcasts) 3026 return 0; 3027 3028 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] 3029 [secondOp-Instruction::CastOpsBegin]; 3030 switch (ElimCase) { 3031 case 0: 3032 // Categorically disallowed. 3033 return 0; 3034 case 1: 3035 // Allowed, use first cast's opcode. 3036 return firstOp; 3037 case 2: 3038 // Allowed, use second cast's opcode. 3039 return secondOp; 3040 case 3: 3041 // No-op cast in second op implies firstOp as long as the DestTy 3042 // is integer and we are not converting between a vector and a 3043 // non-vector type. 3044 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) 3045 return firstOp; 3046 return 0; 3047 case 4: 3048 // No-op cast in second op implies firstOp as long as the DestTy 3049 // is floating point. 3050 if (DstTy->isFloatingPointTy()) 3051 return firstOp; 3052 return 0; 3053 case 5: 3054 // No-op cast in first op implies secondOp as long as the SrcTy 3055 // is an integer. 3056 if (SrcTy->isIntegerTy()) 3057 return secondOp; 3058 return 0; 3059 case 6: 3060 // No-op cast in first op implies secondOp as long as the SrcTy 3061 // is a floating point. 3062 if (SrcTy->isFloatingPointTy()) 3063 return secondOp; 3064 return 0; 3065 case 7: { 3066 // Disable inttoptr/ptrtoint optimization if enabled. 3067 if (DisableI2pP2iOpt) 3068 return 0; 3069 3070 // Cannot simplify if address spaces are different! 3071 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 3072 return 0; 3073 3074 unsigned MidSize = MidTy->getScalarSizeInBits(); 3075 // We can still fold this without knowing the actual sizes as long we 3076 // know that the intermediate pointer is the largest possible 3077 // pointer size. 3078 // FIXME: Is this always true? 3079 if (MidSize == 64) 3080 return Instruction::BitCast; 3081 3082 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. 3083 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) 3084 return 0; 3085 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); 3086 if (MidSize >= PtrSize) 3087 return Instruction::BitCast; 3088 return 0; 3089 } 3090 case 8: { 3091 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size 3092 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) 3093 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) 3094 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 3095 unsigned DstSize = DstTy->getScalarSizeInBits(); 3096 if (SrcSize == DstSize) 3097 return Instruction::BitCast; 3098 else if (SrcSize < DstSize) 3099 return firstOp; 3100 return secondOp; 3101 } 3102 case 9: 3103 // zext, sext -> zext, because sext can't sign extend after zext 3104 return Instruction::ZExt; 3105 case 11: { 3106 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize 3107 if (!MidIntPtrTy) 3108 return 0; 3109 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); 3110 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 3111 unsigned DstSize = DstTy->getScalarSizeInBits(); 3112 if (SrcSize <= PtrSize && SrcSize == DstSize) 3113 return Instruction::BitCast; 3114 return 0; 3115 } 3116 case 12: 3117 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS 3118 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS 3119 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 3120 return Instruction::AddrSpaceCast; 3121 return Instruction::BitCast; 3122 case 13: 3123 // FIXME: this state can be merged with (1), but the following assert 3124 // is useful to check the correcteness of the sequence due to semantic 3125 // change of bitcast. 3126 assert( 3127 SrcTy->isPtrOrPtrVectorTy() && 3128 MidTy->isPtrOrPtrVectorTy() && 3129 DstTy->isPtrOrPtrVectorTy() && 3130 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && 3131 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 3132 "Illegal addrspacecast, bitcast sequence!"); 3133 // Allowed, use first cast's opcode 3134 return firstOp; 3135 case 14: { 3136 // bitcast, addrspacecast -> addrspacecast if the element type of 3137 // bitcast's source is the same as that of addrspacecast's destination. 3138 PointerType *SrcPtrTy = cast<PointerType>(SrcTy->getScalarType()); 3139 PointerType *DstPtrTy = cast<PointerType>(DstTy->getScalarType()); 3140 if (SrcPtrTy->hasSameElementTypeAs(DstPtrTy)) 3141 return Instruction::AddrSpaceCast; 3142 return 0; 3143 } 3144 case 15: 3145 // FIXME: this state can be merged with (1), but the following assert 3146 // is useful to check the correcteness of the sequence due to semantic 3147 // change of bitcast. 3148 assert( 3149 SrcTy->isIntOrIntVectorTy() && 3150 MidTy->isPtrOrPtrVectorTy() && 3151 DstTy->isPtrOrPtrVectorTy() && 3152 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 3153 "Illegal inttoptr, bitcast sequence!"); 3154 // Allowed, use first cast's opcode 3155 return firstOp; 3156 case 16: 3157 // FIXME: this state can be merged with (2), but the following assert 3158 // is useful to check the correcteness of the sequence due to semantic 3159 // change of bitcast. 3160 assert( 3161 SrcTy->isPtrOrPtrVectorTy() && 3162 MidTy->isPtrOrPtrVectorTy() && 3163 DstTy->isIntOrIntVectorTy() && 3164 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && 3165 "Illegal bitcast, ptrtoint sequence!"); 3166 // Allowed, use second cast's opcode 3167 return secondOp; 3168 case 17: 3169 // (sitofp (zext x)) -> (uitofp x) 3170 return Instruction::UIToFP; 3171 case 99: 3172 // Cast combination can't happen (error in input). This is for all cases 3173 // where the MidTy is not the same for the two cast instructions. 3174 llvm_unreachable("Invalid Cast Combination"); 3175 default: 3176 llvm_unreachable("Error in CastResults table!!!"); 3177 } 3178 } 3179 3180 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 3181 const Twine &Name, Instruction *InsertBefore) { 3182 assert(castIsValid(op, S, Ty) && "Invalid cast!"); 3183 // Construct and return the appropriate CastInst subclass 3184 switch (op) { 3185 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); 3186 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); 3187 case SExt: return new SExtInst (S, Ty, Name, InsertBefore); 3188 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); 3189 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); 3190 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); 3191 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); 3192 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); 3193 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); 3194 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); 3195 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); 3196 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); 3197 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); 3198 default: llvm_unreachable("Invalid opcode provided"); 3199 } 3200 } 3201 3202 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 3203 const Twine &Name, BasicBlock *InsertAtEnd) { 3204 assert(castIsValid(op, S, Ty) && "Invalid cast!"); 3205 // Construct and return the appropriate CastInst subclass 3206 switch (op) { 3207 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); 3208 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); 3209 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); 3210 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); 3211 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); 3212 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); 3213 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); 3214 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); 3215 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); 3216 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); 3217 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); 3218 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); 3219 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); 3220 default: llvm_unreachable("Invalid opcode provided"); 3221 } 3222 } 3223 3224 CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 3225 const Twine &Name, 3226 Instruction *InsertBefore) { 3227 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3228 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3229 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); 3230 } 3231 3232 CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 3233 const Twine &Name, 3234 BasicBlock *InsertAtEnd) { 3235 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3236 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3237 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); 3238 } 3239 3240 CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 3241 const Twine &Name, 3242 Instruction *InsertBefore) { 3243 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3244 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3245 return Create(Instruction::SExt, S, Ty, Name, InsertBefore); 3246 } 3247 3248 CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 3249 const Twine &Name, 3250 BasicBlock *InsertAtEnd) { 3251 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3252 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3253 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); 3254 } 3255 3256 CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 3257 const Twine &Name, 3258 Instruction *InsertBefore) { 3259 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3260 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3261 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); 3262 } 3263 3264 CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 3265 const Twine &Name, 3266 BasicBlock *InsertAtEnd) { 3267 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3268 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3269 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); 3270 } 3271 3272 CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 3273 const Twine &Name, 3274 BasicBlock *InsertAtEnd) { 3275 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3276 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 3277 "Invalid cast"); 3278 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 3279 assert((!Ty->isVectorTy() || 3280 cast<VectorType>(Ty)->getElementCount() == 3281 cast<VectorType>(S->getType())->getElementCount()) && 3282 "Invalid cast"); 3283 3284 if (Ty->isIntOrIntVectorTy()) 3285 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); 3286 3287 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); 3288 } 3289 3290 /// Create a BitCast or a PtrToInt cast instruction 3291 CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 3292 const Twine &Name, 3293 Instruction *InsertBefore) { 3294 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3295 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 3296 "Invalid cast"); 3297 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 3298 assert((!Ty->isVectorTy() || 3299 cast<VectorType>(Ty)->getElementCount() == 3300 cast<VectorType>(S->getType())->getElementCount()) && 3301 "Invalid cast"); 3302 3303 if (Ty->isIntOrIntVectorTy()) 3304 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3305 3306 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); 3307 } 3308 3309 CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( 3310 Value *S, Type *Ty, 3311 const Twine &Name, 3312 BasicBlock *InsertAtEnd) { 3313 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3314 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); 3315 3316 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 3317 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); 3318 3319 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3320 } 3321 3322 CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( 3323 Value *S, Type *Ty, 3324 const Twine &Name, 3325 Instruction *InsertBefore) { 3326 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3327 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); 3328 3329 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 3330 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); 3331 3332 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3333 } 3334 3335 CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, 3336 const Twine &Name, 3337 Instruction *InsertBefore) { 3338 if (S->getType()->isPointerTy() && Ty->isIntegerTy()) 3339 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3340 if (S->getType()->isIntegerTy() && Ty->isPointerTy()) 3341 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); 3342 3343 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3344 } 3345 3346 CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 3347 bool isSigned, const Twine &Name, 3348 Instruction *InsertBefore) { 3349 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 3350 "Invalid integer cast"); 3351 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3352 unsigned DstBits = Ty->getScalarSizeInBits(); 3353 Instruction::CastOps opcode = 3354 (SrcBits == DstBits ? Instruction::BitCast : 3355 (SrcBits > DstBits ? Instruction::Trunc : 3356 (isSigned ? Instruction::SExt : Instruction::ZExt))); 3357 return Create(opcode, C, Ty, Name, InsertBefore); 3358 } 3359 3360 CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 3361 bool isSigned, const Twine &Name, 3362 BasicBlock *InsertAtEnd) { 3363 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 3364 "Invalid cast"); 3365 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3366 unsigned DstBits = Ty->getScalarSizeInBits(); 3367 Instruction::CastOps opcode = 3368 (SrcBits == DstBits ? Instruction::BitCast : 3369 (SrcBits > DstBits ? Instruction::Trunc : 3370 (isSigned ? Instruction::SExt : Instruction::ZExt))); 3371 return Create(opcode, C, Ty, Name, InsertAtEnd); 3372 } 3373 3374 CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 3375 const Twine &Name, 3376 Instruction *InsertBefore) { 3377 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 3378 "Invalid cast"); 3379 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3380 unsigned DstBits = Ty->getScalarSizeInBits(); 3381 Instruction::CastOps opcode = 3382 (SrcBits == DstBits ? Instruction::BitCast : 3383 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 3384 return Create(opcode, C, Ty, Name, InsertBefore); 3385 } 3386 3387 CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 3388 const Twine &Name, 3389 BasicBlock *InsertAtEnd) { 3390 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 3391 "Invalid cast"); 3392 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3393 unsigned DstBits = Ty->getScalarSizeInBits(); 3394 Instruction::CastOps opcode = 3395 (SrcBits == DstBits ? Instruction::BitCast : 3396 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 3397 return Create(opcode, C, Ty, Name, InsertAtEnd); 3398 } 3399 3400 bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { 3401 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) 3402 return false; 3403 3404 if (SrcTy == DestTy) 3405 return true; 3406 3407 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { 3408 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { 3409 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3410 // An element by element cast. Valid if casting the elements is valid. 3411 SrcTy = SrcVecTy->getElementType(); 3412 DestTy = DestVecTy->getElementType(); 3413 } 3414 } 3415 } 3416 3417 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { 3418 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { 3419 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); 3420 } 3421 } 3422 3423 TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3424 TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3425 3426 // Could still have vectors of pointers if the number of elements doesn't 3427 // match 3428 if (SrcBits.getKnownMinSize() == 0 || DestBits.getKnownMinSize() == 0) 3429 return false; 3430 3431 if (SrcBits != DestBits) 3432 return false; 3433 3434 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) 3435 return false; 3436 3437 return true; 3438 } 3439 3440 bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, 3441 const DataLayout &DL) { 3442 // ptrtoint and inttoptr are not allowed on non-integral pointers 3443 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) 3444 if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) 3445 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3446 !DL.isNonIntegralPointerType(PtrTy)); 3447 if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) 3448 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) 3449 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3450 !DL.isNonIntegralPointerType(PtrTy)); 3451 3452 return isBitCastable(SrcTy, DestTy); 3453 } 3454 3455 // Provide a way to get a "cast" where the cast opcode is inferred from the 3456 // types and size of the operand. This, basically, is a parallel of the 3457 // logic in the castIsValid function below. This axiom should hold: 3458 // castIsValid( getCastOpcode(Val, Ty), Val, Ty) 3459 // should not assert in castIsValid. In other words, this produces a "correct" 3460 // casting opcode for the arguments passed to it. 3461 Instruction::CastOps 3462 CastInst::getCastOpcode( 3463 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { 3464 Type *SrcTy = Src->getType(); 3465 3466 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && 3467 "Only first class types are castable!"); 3468 3469 if (SrcTy == DestTy) 3470 return BitCast; 3471 3472 // FIXME: Check address space sizes here 3473 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) 3474 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) 3475 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3476 // An element by element cast. Find the appropriate opcode based on the 3477 // element types. 3478 SrcTy = SrcVecTy->getElementType(); 3479 DestTy = DestVecTy->getElementType(); 3480 } 3481 3482 // Get the bit sizes, we'll need these 3483 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3484 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3485 3486 // Run through the possibilities ... 3487 if (DestTy->isIntegerTy()) { // Casting to integral 3488 if (SrcTy->isIntegerTy()) { // Casting from integral 3489 if (DestBits < SrcBits) 3490 return Trunc; // int -> smaller int 3491 else if (DestBits > SrcBits) { // its an extension 3492 if (SrcIsSigned) 3493 return SExt; // signed -> SEXT 3494 else 3495 return ZExt; // unsigned -> ZEXT 3496 } else { 3497 return BitCast; // Same size, No-op cast 3498 } 3499 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3500 if (DestIsSigned) 3501 return FPToSI; // FP -> sint 3502 else 3503 return FPToUI; // FP -> uint 3504 } else if (SrcTy->isVectorTy()) { 3505 assert(DestBits == SrcBits && 3506 "Casting vector to integer of different width"); 3507 return BitCast; // Same size, no-op cast 3508 } else { 3509 assert(SrcTy->isPointerTy() && 3510 "Casting from a value that is not first-class type"); 3511 return PtrToInt; // ptr -> int 3512 } 3513 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt 3514 if (SrcTy->isIntegerTy()) { // Casting from integral 3515 if (SrcIsSigned) 3516 return SIToFP; // sint -> FP 3517 else 3518 return UIToFP; // uint -> FP 3519 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3520 if (DestBits < SrcBits) { 3521 return FPTrunc; // FP -> smaller FP 3522 } else if (DestBits > SrcBits) { 3523 return FPExt; // FP -> larger FP 3524 } else { 3525 return BitCast; // same size, no-op cast 3526 } 3527 } else if (SrcTy->isVectorTy()) { 3528 assert(DestBits == SrcBits && 3529 "Casting vector to floating point of different width"); 3530 return BitCast; // same size, no-op cast 3531 } 3532 llvm_unreachable("Casting pointer or non-first class to float"); 3533 } else if (DestTy->isVectorTy()) { 3534 assert(DestBits == SrcBits && 3535 "Illegal cast to vector (wrong type or size)"); 3536 return BitCast; 3537 } else if (DestTy->isPointerTy()) { 3538 if (SrcTy->isPointerTy()) { 3539 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) 3540 return AddrSpaceCast; 3541 return BitCast; // ptr -> ptr 3542 } else if (SrcTy->isIntegerTy()) { 3543 return IntToPtr; // int -> ptr 3544 } 3545 llvm_unreachable("Casting pointer to other than pointer or int"); 3546 } else if (DestTy->isX86_MMXTy()) { 3547 if (SrcTy->isVectorTy()) { 3548 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); 3549 return BitCast; // 64-bit vector to MMX 3550 } 3551 llvm_unreachable("Illegal cast to X86_MMX"); 3552 } 3553 llvm_unreachable("Casting to type that is not first-class"); 3554 } 3555 3556 //===----------------------------------------------------------------------===// 3557 // CastInst SubClass Constructors 3558 //===----------------------------------------------------------------------===// 3559 3560 /// Check that the construction parameters for a CastInst are correct. This 3561 /// could be broken out into the separate constructors but it is useful to have 3562 /// it in one place and to eliminate the redundant code for getting the sizes 3563 /// of the types involved. 3564 bool 3565 CastInst::castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy) { 3566 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || 3567 SrcTy->isAggregateType() || DstTy->isAggregateType()) 3568 return false; 3569 3570 // Get the size of the types in bits, and whether we are dealing 3571 // with vector types, we'll need this later. 3572 bool SrcIsVec = isa<VectorType>(SrcTy); 3573 bool DstIsVec = isa<VectorType>(DstTy); 3574 unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits(); 3575 unsigned DstScalarBitSize = DstTy->getScalarSizeInBits(); 3576 3577 // If these are vector types, get the lengths of the vectors (using zero for 3578 // scalar types means that checking that vector lengths match also checks that 3579 // scalars are not being converted to vectors or vectors to scalars). 3580 ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount() 3581 : ElementCount::getFixed(0); 3582 ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount() 3583 : ElementCount::getFixed(0); 3584 3585 // Switch on the opcode provided 3586 switch (op) { 3587 default: return false; // This is an input error 3588 case Instruction::Trunc: 3589 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3590 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3591 case Instruction::ZExt: 3592 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3593 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3594 case Instruction::SExt: 3595 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3596 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3597 case Instruction::FPTrunc: 3598 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3599 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3600 case Instruction::FPExt: 3601 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3602 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3603 case Instruction::UIToFP: 3604 case Instruction::SIToFP: 3605 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && 3606 SrcEC == DstEC; 3607 case Instruction::FPToUI: 3608 case Instruction::FPToSI: 3609 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && 3610 SrcEC == DstEC; 3611 case Instruction::PtrToInt: 3612 if (SrcEC != DstEC) 3613 return false; 3614 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); 3615 case Instruction::IntToPtr: 3616 if (SrcEC != DstEC) 3617 return false; 3618 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); 3619 case Instruction::BitCast: { 3620 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3621 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3622 3623 // BitCast implies a no-op cast of type only. No bits change. 3624 // However, you can't cast pointers to anything but pointers. 3625 if (!SrcPtrTy != !DstPtrTy) 3626 return false; 3627 3628 // For non-pointer cases, the cast is okay if the source and destination bit 3629 // widths are identical. 3630 if (!SrcPtrTy) 3631 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); 3632 3633 // If both are pointers then the address spaces must match. 3634 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) 3635 return false; 3636 3637 // A vector of pointers must have the same number of elements. 3638 if (SrcIsVec && DstIsVec) 3639 return SrcEC == DstEC; 3640 if (SrcIsVec) 3641 return SrcEC == ElementCount::getFixed(1); 3642 if (DstIsVec) 3643 return DstEC == ElementCount::getFixed(1); 3644 3645 return true; 3646 } 3647 case Instruction::AddrSpaceCast: { 3648 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3649 if (!SrcPtrTy) 3650 return false; 3651 3652 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3653 if (!DstPtrTy) 3654 return false; 3655 3656 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) 3657 return false; 3658 3659 return SrcEC == DstEC; 3660 } 3661 } 3662 } 3663 3664 TruncInst::TruncInst( 3665 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3666 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { 3667 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 3668 } 3669 3670 TruncInst::TruncInst( 3671 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3672 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { 3673 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 3674 } 3675 3676 ZExtInst::ZExtInst( 3677 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3678 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { 3679 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 3680 } 3681 3682 ZExtInst::ZExtInst( 3683 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3684 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { 3685 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 3686 } 3687 SExtInst::SExtInst( 3688 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3689 ) : CastInst(Ty, SExt, S, Name, InsertBefore) { 3690 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 3691 } 3692 3693 SExtInst::SExtInst( 3694 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3695 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { 3696 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 3697 } 3698 3699 FPTruncInst::FPTruncInst( 3700 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3701 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 3702 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 3703 } 3704 3705 FPTruncInst::FPTruncInst( 3706 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3707 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { 3708 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 3709 } 3710 3711 FPExtInst::FPExtInst( 3712 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3713 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { 3714 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 3715 } 3716 3717 FPExtInst::FPExtInst( 3718 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3719 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { 3720 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 3721 } 3722 3723 UIToFPInst::UIToFPInst( 3724 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3725 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 3726 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 3727 } 3728 3729 UIToFPInst::UIToFPInst( 3730 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3731 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { 3732 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 3733 } 3734 3735 SIToFPInst::SIToFPInst( 3736 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3737 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 3738 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 3739 } 3740 3741 SIToFPInst::SIToFPInst( 3742 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3743 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { 3744 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 3745 } 3746 3747 FPToUIInst::FPToUIInst( 3748 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3749 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 3750 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 3751 } 3752 3753 FPToUIInst::FPToUIInst( 3754 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3755 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { 3756 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 3757 } 3758 3759 FPToSIInst::FPToSIInst( 3760 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3761 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 3762 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 3763 } 3764 3765 FPToSIInst::FPToSIInst( 3766 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3767 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { 3768 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 3769 } 3770 3771 PtrToIntInst::PtrToIntInst( 3772 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3773 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 3774 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 3775 } 3776 3777 PtrToIntInst::PtrToIntInst( 3778 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3779 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { 3780 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 3781 } 3782 3783 IntToPtrInst::IntToPtrInst( 3784 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3785 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 3786 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 3787 } 3788 3789 IntToPtrInst::IntToPtrInst( 3790 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3791 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { 3792 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 3793 } 3794 3795 BitCastInst::BitCastInst( 3796 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3797 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { 3798 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 3799 } 3800 3801 BitCastInst::BitCastInst( 3802 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3803 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { 3804 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 3805 } 3806 3807 AddrSpaceCastInst::AddrSpaceCastInst( 3808 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3809 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { 3810 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3811 } 3812 3813 AddrSpaceCastInst::AddrSpaceCastInst( 3814 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3815 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { 3816 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3817 } 3818 3819 //===----------------------------------------------------------------------===// 3820 // CmpInst Classes 3821 //===----------------------------------------------------------------------===// 3822 3823 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, 3824 Value *RHS, const Twine &Name, Instruction *InsertBefore, 3825 Instruction *FlagsSource) 3826 : Instruction(ty, op, 3827 OperandTraits<CmpInst>::op_begin(this), 3828 OperandTraits<CmpInst>::operands(this), 3829 InsertBefore) { 3830 Op<0>() = LHS; 3831 Op<1>() = RHS; 3832 setPredicate((Predicate)predicate); 3833 setName(Name); 3834 if (FlagsSource) 3835 copyIRFlags(FlagsSource); 3836 } 3837 3838 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, 3839 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) 3840 : Instruction(ty, op, 3841 OperandTraits<CmpInst>::op_begin(this), 3842 OperandTraits<CmpInst>::operands(this), 3843 InsertAtEnd) { 3844 Op<0>() = LHS; 3845 Op<1>() = RHS; 3846 setPredicate((Predicate)predicate); 3847 setName(Name); 3848 } 3849 3850 CmpInst * 3851 CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, 3852 const Twine &Name, Instruction *InsertBefore) { 3853 if (Op == Instruction::ICmp) { 3854 if (InsertBefore) 3855 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), 3856 S1, S2, Name); 3857 else 3858 return new ICmpInst(CmpInst::Predicate(predicate), 3859 S1, S2, Name); 3860 } 3861 3862 if (InsertBefore) 3863 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), 3864 S1, S2, Name); 3865 else 3866 return new FCmpInst(CmpInst::Predicate(predicate), 3867 S1, S2, Name); 3868 } 3869 3870 CmpInst * 3871 CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, 3872 const Twine &Name, BasicBlock *InsertAtEnd) { 3873 if (Op == Instruction::ICmp) { 3874 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3875 S1, S2, Name); 3876 } 3877 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3878 S1, S2, Name); 3879 } 3880 3881 void CmpInst::swapOperands() { 3882 if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3883 IC->swapOperands(); 3884 else 3885 cast<FCmpInst>(this)->swapOperands(); 3886 } 3887 3888 bool CmpInst::isCommutative() const { 3889 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3890 return IC->isCommutative(); 3891 return cast<FCmpInst>(this)->isCommutative(); 3892 } 3893 3894 bool CmpInst::isEquality(Predicate P) { 3895 if (ICmpInst::isIntPredicate(P)) 3896 return ICmpInst::isEquality(P); 3897 if (FCmpInst::isFPPredicate(P)) 3898 return FCmpInst::isEquality(P); 3899 llvm_unreachable("Unsupported predicate kind"); 3900 } 3901 3902 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { 3903 switch (pred) { 3904 default: llvm_unreachable("Unknown cmp predicate!"); 3905 case ICMP_EQ: return ICMP_NE; 3906 case ICMP_NE: return ICMP_EQ; 3907 case ICMP_UGT: return ICMP_ULE; 3908 case ICMP_ULT: return ICMP_UGE; 3909 case ICMP_UGE: return ICMP_ULT; 3910 case ICMP_ULE: return ICMP_UGT; 3911 case ICMP_SGT: return ICMP_SLE; 3912 case ICMP_SLT: return ICMP_SGE; 3913 case ICMP_SGE: return ICMP_SLT; 3914 case ICMP_SLE: return ICMP_SGT; 3915 3916 case FCMP_OEQ: return FCMP_UNE; 3917 case FCMP_ONE: return FCMP_UEQ; 3918 case FCMP_OGT: return FCMP_ULE; 3919 case FCMP_OLT: return FCMP_UGE; 3920 case FCMP_OGE: return FCMP_ULT; 3921 case FCMP_OLE: return FCMP_UGT; 3922 case FCMP_UEQ: return FCMP_ONE; 3923 case FCMP_UNE: return FCMP_OEQ; 3924 case FCMP_UGT: return FCMP_OLE; 3925 case FCMP_ULT: return FCMP_OGE; 3926 case FCMP_UGE: return FCMP_OLT; 3927 case FCMP_ULE: return FCMP_OGT; 3928 case FCMP_ORD: return FCMP_UNO; 3929 case FCMP_UNO: return FCMP_ORD; 3930 case FCMP_TRUE: return FCMP_FALSE; 3931 case FCMP_FALSE: return FCMP_TRUE; 3932 } 3933 } 3934 3935 StringRef CmpInst::getPredicateName(Predicate Pred) { 3936 switch (Pred) { 3937 default: return "unknown"; 3938 case FCmpInst::FCMP_FALSE: return "false"; 3939 case FCmpInst::FCMP_OEQ: return "oeq"; 3940 case FCmpInst::FCMP_OGT: return "ogt"; 3941 case FCmpInst::FCMP_OGE: return "oge"; 3942 case FCmpInst::FCMP_OLT: return "olt"; 3943 case FCmpInst::FCMP_OLE: return "ole"; 3944 case FCmpInst::FCMP_ONE: return "one"; 3945 case FCmpInst::FCMP_ORD: return "ord"; 3946 case FCmpInst::FCMP_UNO: return "uno"; 3947 case FCmpInst::FCMP_UEQ: return "ueq"; 3948 case FCmpInst::FCMP_UGT: return "ugt"; 3949 case FCmpInst::FCMP_UGE: return "uge"; 3950 case FCmpInst::FCMP_ULT: return "ult"; 3951 case FCmpInst::FCMP_ULE: return "ule"; 3952 case FCmpInst::FCMP_UNE: return "une"; 3953 case FCmpInst::FCMP_TRUE: return "true"; 3954 case ICmpInst::ICMP_EQ: return "eq"; 3955 case ICmpInst::ICMP_NE: return "ne"; 3956 case ICmpInst::ICMP_SGT: return "sgt"; 3957 case ICmpInst::ICMP_SGE: return "sge"; 3958 case ICmpInst::ICMP_SLT: return "slt"; 3959 case ICmpInst::ICMP_SLE: return "sle"; 3960 case ICmpInst::ICMP_UGT: return "ugt"; 3961 case ICmpInst::ICMP_UGE: return "uge"; 3962 case ICmpInst::ICMP_ULT: return "ult"; 3963 case ICmpInst::ICMP_ULE: return "ule"; 3964 } 3965 } 3966 3967 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { 3968 switch (pred) { 3969 default: llvm_unreachable("Unknown icmp predicate!"); 3970 case ICMP_EQ: case ICMP_NE: 3971 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 3972 return pred; 3973 case ICMP_UGT: return ICMP_SGT; 3974 case ICMP_ULT: return ICMP_SLT; 3975 case ICMP_UGE: return ICMP_SGE; 3976 case ICMP_ULE: return ICMP_SLE; 3977 } 3978 } 3979 3980 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { 3981 switch (pred) { 3982 default: llvm_unreachable("Unknown icmp predicate!"); 3983 case ICMP_EQ: case ICMP_NE: 3984 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: 3985 return pred; 3986 case ICMP_SGT: return ICMP_UGT; 3987 case ICMP_SLT: return ICMP_ULT; 3988 case ICMP_SGE: return ICMP_UGE; 3989 case ICMP_SLE: return ICMP_ULE; 3990 } 3991 } 3992 3993 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { 3994 switch (pred) { 3995 default: llvm_unreachable("Unknown cmp predicate!"); 3996 case ICMP_EQ: case ICMP_NE: 3997 return pred; 3998 case ICMP_SGT: return ICMP_SLT; 3999 case ICMP_SLT: return ICMP_SGT; 4000 case ICMP_SGE: return ICMP_SLE; 4001 case ICMP_SLE: return ICMP_SGE; 4002 case ICMP_UGT: return ICMP_ULT; 4003 case ICMP_ULT: return ICMP_UGT; 4004 case ICMP_UGE: return ICMP_ULE; 4005 case ICMP_ULE: return ICMP_UGE; 4006 4007 case FCMP_FALSE: case FCMP_TRUE: 4008 case FCMP_OEQ: case FCMP_ONE: 4009 case FCMP_UEQ: case FCMP_UNE: 4010 case FCMP_ORD: case FCMP_UNO: 4011 return pred; 4012 case FCMP_OGT: return FCMP_OLT; 4013 case FCMP_OLT: return FCMP_OGT; 4014 case FCMP_OGE: return FCMP_OLE; 4015 case FCMP_OLE: return FCMP_OGE; 4016 case FCMP_UGT: return FCMP_ULT; 4017 case FCMP_ULT: return FCMP_UGT; 4018 case FCMP_UGE: return FCMP_ULE; 4019 case FCMP_ULE: return FCMP_UGE; 4020 } 4021 } 4022 4023 bool CmpInst::isNonStrictPredicate(Predicate pred) { 4024 switch (pred) { 4025 case ICMP_SGE: 4026 case ICMP_SLE: 4027 case ICMP_UGE: 4028 case ICMP_ULE: 4029 case FCMP_OGE: 4030 case FCMP_OLE: 4031 case FCMP_UGE: 4032 case FCMP_ULE: 4033 return true; 4034 default: 4035 return false; 4036 } 4037 } 4038 4039 bool CmpInst::isStrictPredicate(Predicate pred) { 4040 switch (pred) { 4041 case ICMP_SGT: 4042 case ICMP_SLT: 4043 case ICMP_UGT: 4044 case ICMP_ULT: 4045 case FCMP_OGT: 4046 case FCMP_OLT: 4047 case FCMP_UGT: 4048 case FCMP_ULT: 4049 return true; 4050 default: 4051 return false; 4052 } 4053 } 4054 4055 CmpInst::Predicate CmpInst::getStrictPredicate(Predicate pred) { 4056 switch (pred) { 4057 case ICMP_SGE: 4058 return ICMP_SGT; 4059 case ICMP_SLE: 4060 return ICMP_SLT; 4061 case ICMP_UGE: 4062 return ICMP_UGT; 4063 case ICMP_ULE: 4064 return ICMP_ULT; 4065 case FCMP_OGE: 4066 return FCMP_OGT; 4067 case FCMP_OLE: 4068 return FCMP_OLT; 4069 case FCMP_UGE: 4070 return FCMP_UGT; 4071 case FCMP_ULE: 4072 return FCMP_ULT; 4073 default: 4074 return pred; 4075 } 4076 } 4077 4078 CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { 4079 switch (pred) { 4080 case ICMP_SGT: 4081 return ICMP_SGE; 4082 case ICMP_SLT: 4083 return ICMP_SLE; 4084 case ICMP_UGT: 4085 return ICMP_UGE; 4086 case ICMP_ULT: 4087 return ICMP_ULE; 4088 case FCMP_OGT: 4089 return FCMP_OGE; 4090 case FCMP_OLT: 4091 return FCMP_OLE; 4092 case FCMP_UGT: 4093 return FCMP_UGE; 4094 case FCMP_ULT: 4095 return FCMP_ULE; 4096 default: 4097 return pred; 4098 } 4099 } 4100 4101 CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { 4102 assert(CmpInst::isRelational(pred) && "Call only with relational predicate!"); 4103 4104 if (isStrictPredicate(pred)) 4105 return getNonStrictPredicate(pred); 4106 if (isNonStrictPredicate(pred)) 4107 return getStrictPredicate(pred); 4108 4109 llvm_unreachable("Unknown predicate!"); 4110 } 4111 4112 CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { 4113 assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!"); 4114 4115 switch (pred) { 4116 default: 4117 llvm_unreachable("Unknown predicate!"); 4118 case CmpInst::ICMP_ULT: 4119 return CmpInst::ICMP_SLT; 4120 case CmpInst::ICMP_ULE: 4121 return CmpInst::ICMP_SLE; 4122 case CmpInst::ICMP_UGT: 4123 return CmpInst::ICMP_SGT; 4124 case CmpInst::ICMP_UGE: 4125 return CmpInst::ICMP_SGE; 4126 } 4127 } 4128 4129 CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) { 4130 assert(CmpInst::isSigned(pred) && "Call only with signed predicates!"); 4131 4132 switch (pred) { 4133 default: 4134 llvm_unreachable("Unknown predicate!"); 4135 case CmpInst::ICMP_SLT: 4136 return CmpInst::ICMP_ULT; 4137 case CmpInst::ICMP_SLE: 4138 return CmpInst::ICMP_ULE; 4139 case CmpInst::ICMP_SGT: 4140 return CmpInst::ICMP_UGT; 4141 case CmpInst::ICMP_SGE: 4142 return CmpInst::ICMP_UGE; 4143 } 4144 } 4145 4146 bool CmpInst::isUnsigned(Predicate predicate) { 4147 switch (predicate) { 4148 default: return false; 4149 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 4150 case ICmpInst::ICMP_UGE: return true; 4151 } 4152 } 4153 4154 bool CmpInst::isSigned(Predicate predicate) { 4155 switch (predicate) { 4156 default: return false; 4157 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 4158 case ICmpInst::ICMP_SGE: return true; 4159 } 4160 } 4161 4162 bool ICmpInst::compare(const APInt &LHS, const APInt &RHS, 4163 ICmpInst::Predicate Pred) { 4164 assert(ICmpInst::isIntPredicate(Pred) && "Only for integer predicates!"); 4165 switch (Pred) { 4166 case ICmpInst::Predicate::ICMP_EQ: 4167 return LHS.eq(RHS); 4168 case ICmpInst::Predicate::ICMP_NE: 4169 return LHS.ne(RHS); 4170 case ICmpInst::Predicate::ICMP_UGT: 4171 return LHS.ugt(RHS); 4172 case ICmpInst::Predicate::ICMP_UGE: 4173 return LHS.uge(RHS); 4174 case ICmpInst::Predicate::ICMP_ULT: 4175 return LHS.ult(RHS); 4176 case ICmpInst::Predicate::ICMP_ULE: 4177 return LHS.ule(RHS); 4178 case ICmpInst::Predicate::ICMP_SGT: 4179 return LHS.sgt(RHS); 4180 case ICmpInst::Predicate::ICMP_SGE: 4181 return LHS.sge(RHS); 4182 case ICmpInst::Predicate::ICMP_SLT: 4183 return LHS.slt(RHS); 4184 case ICmpInst::Predicate::ICMP_SLE: 4185 return LHS.sle(RHS); 4186 default: 4187 llvm_unreachable("Unexpected non-integer predicate."); 4188 }; 4189 } 4190 4191 bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS, 4192 FCmpInst::Predicate Pred) { 4193 APFloat::cmpResult R = LHS.compare(RHS); 4194 switch (Pred) { 4195 default: 4196 llvm_unreachable("Invalid FCmp Predicate"); 4197 case FCmpInst::FCMP_FALSE: 4198 return false; 4199 case FCmpInst::FCMP_TRUE: 4200 return true; 4201 case FCmpInst::FCMP_UNO: 4202 return R == APFloat::cmpUnordered; 4203 case FCmpInst::FCMP_ORD: 4204 return R != APFloat::cmpUnordered; 4205 case FCmpInst::FCMP_UEQ: 4206 return R == APFloat::cmpUnordered || R == APFloat::cmpEqual; 4207 case FCmpInst::FCMP_OEQ: 4208 return R == APFloat::cmpEqual; 4209 case FCmpInst::FCMP_UNE: 4210 return R != APFloat::cmpEqual; 4211 case FCmpInst::FCMP_ONE: 4212 return R == APFloat::cmpLessThan || R == APFloat::cmpGreaterThan; 4213 case FCmpInst::FCMP_ULT: 4214 return R == APFloat::cmpUnordered || R == APFloat::cmpLessThan; 4215 case FCmpInst::FCMP_OLT: 4216 return R == APFloat::cmpLessThan; 4217 case FCmpInst::FCMP_UGT: 4218 return R == APFloat::cmpUnordered || R == APFloat::cmpGreaterThan; 4219 case FCmpInst::FCMP_OGT: 4220 return R == APFloat::cmpGreaterThan; 4221 case FCmpInst::FCMP_ULE: 4222 return R != APFloat::cmpGreaterThan; 4223 case FCmpInst::FCMP_OLE: 4224 return R == APFloat::cmpLessThan || R == APFloat::cmpEqual; 4225 case FCmpInst::FCMP_UGE: 4226 return R != APFloat::cmpLessThan; 4227 case FCmpInst::FCMP_OGE: 4228 return R == APFloat::cmpGreaterThan || R == APFloat::cmpEqual; 4229 } 4230 } 4231 4232 CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) { 4233 assert(CmpInst::isRelational(pred) && 4234 "Call only with non-equality predicates!"); 4235 4236 if (isSigned(pred)) 4237 return getUnsignedPredicate(pred); 4238 if (isUnsigned(pred)) 4239 return getSignedPredicate(pred); 4240 4241 llvm_unreachable("Unknown predicate!"); 4242 } 4243 4244 bool CmpInst::isOrdered(Predicate predicate) { 4245 switch (predicate) { 4246 default: return false; 4247 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 4248 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 4249 case FCmpInst::FCMP_ORD: return true; 4250 } 4251 } 4252 4253 bool CmpInst::isUnordered(Predicate predicate) { 4254 switch (predicate) { 4255 default: return false; 4256 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 4257 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 4258 case FCmpInst::FCMP_UNO: return true; 4259 } 4260 } 4261 4262 bool CmpInst::isTrueWhenEqual(Predicate predicate) { 4263 switch(predicate) { 4264 default: return false; 4265 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: 4266 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; 4267 } 4268 } 4269 4270 bool CmpInst::isFalseWhenEqual(Predicate predicate) { 4271 switch(predicate) { 4272 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: 4273 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; 4274 default: return false; 4275 } 4276 } 4277 4278 bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { 4279 // If the predicates match, then we know the first condition implies the 4280 // second is true. 4281 if (Pred1 == Pred2) 4282 return true; 4283 4284 switch (Pred1) { 4285 default: 4286 break; 4287 case ICMP_EQ: 4288 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. 4289 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || 4290 Pred2 == ICMP_SLE; 4291 case ICMP_UGT: // A >u B implies A != B and A >=u B are true. 4292 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; 4293 case ICMP_ULT: // A <u B implies A != B and A <=u B are true. 4294 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; 4295 case ICMP_SGT: // A >s B implies A != B and A >=s B are true. 4296 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; 4297 case ICMP_SLT: // A <s B implies A != B and A <=s B are true. 4298 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; 4299 } 4300 return false; 4301 } 4302 4303 bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { 4304 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); 4305 } 4306 4307 //===----------------------------------------------------------------------===// 4308 // SwitchInst Implementation 4309 //===----------------------------------------------------------------------===// 4310 4311 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { 4312 assert(Value && Default && NumReserved); 4313 ReservedSpace = NumReserved; 4314 setNumHungOffUseOperands(2); 4315 allocHungoffUses(ReservedSpace); 4316 4317 Op<0>() = Value; 4318 Op<1>() = Default; 4319 } 4320 4321 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 4322 /// switch on and a default destination. The number of additional cases can 4323 /// be specified here to make memory allocation more efficient. This 4324 /// constructor can also autoinsert before another instruction. 4325 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 4326 Instruction *InsertBefore) 4327 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, 4328 nullptr, 0, InsertBefore) { 4329 init(Value, Default, 2+NumCases*2); 4330 } 4331 4332 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 4333 /// switch on and a default destination. The number of additional cases can 4334 /// be specified here to make memory allocation more efficient. This 4335 /// constructor also autoinserts at the end of the specified BasicBlock. 4336 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 4337 BasicBlock *InsertAtEnd) 4338 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, 4339 nullptr, 0, InsertAtEnd) { 4340 init(Value, Default, 2+NumCases*2); 4341 } 4342 4343 SwitchInst::SwitchInst(const SwitchInst &SI) 4344 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { 4345 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); 4346 setNumHungOffUseOperands(SI.getNumOperands()); 4347 Use *OL = getOperandList(); 4348 const Use *InOL = SI.getOperandList(); 4349 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { 4350 OL[i] = InOL[i]; 4351 OL[i+1] = InOL[i+1]; 4352 } 4353 SubclassOptionalData = SI.SubclassOptionalData; 4354 } 4355 4356 /// addCase - Add an entry to the switch instruction... 4357 /// 4358 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { 4359 unsigned NewCaseIdx = getNumCases(); 4360 unsigned OpNo = getNumOperands(); 4361 if (OpNo+2 > ReservedSpace) 4362 growOperands(); // Get more space! 4363 // Initialize some new operands. 4364 assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); 4365 setNumHungOffUseOperands(OpNo+2); 4366 CaseHandle Case(this, NewCaseIdx); 4367 Case.setValue(OnVal); 4368 Case.setSuccessor(Dest); 4369 } 4370 4371 /// removeCase - This method removes the specified case and its successor 4372 /// from the switch instruction. 4373 SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { 4374 unsigned idx = I->getCaseIndex(); 4375 4376 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); 4377 4378 unsigned NumOps = getNumOperands(); 4379 Use *OL = getOperandList(); 4380 4381 // Overwrite this case with the end of the list. 4382 if (2 + (idx + 1) * 2 != NumOps) { 4383 OL[2 + idx * 2] = OL[NumOps - 2]; 4384 OL[2 + idx * 2 + 1] = OL[NumOps - 1]; 4385 } 4386 4387 // Nuke the last value. 4388 OL[NumOps-2].set(nullptr); 4389 OL[NumOps-2+1].set(nullptr); 4390 setNumHungOffUseOperands(NumOps-2); 4391 4392 return CaseIt(this, idx); 4393 } 4394 4395 /// growOperands - grow operands - This grows the operand list in response 4396 /// to a push_back style of operation. This grows the number of ops by 3 times. 4397 /// 4398 void SwitchInst::growOperands() { 4399 unsigned e = getNumOperands(); 4400 unsigned NumOps = e*3; 4401 4402 ReservedSpace = NumOps; 4403 growHungoffUses(ReservedSpace); 4404 } 4405 4406 MDNode * 4407 SwitchInstProfUpdateWrapper::getProfBranchWeightsMD(const SwitchInst &SI) { 4408 if (MDNode *ProfileData = SI.getMetadata(LLVMContext::MD_prof)) 4409 if (auto *MDName = dyn_cast<MDString>(ProfileData->getOperand(0))) 4410 if (MDName->getString() == "branch_weights") 4411 return ProfileData; 4412 return nullptr; 4413 } 4414 4415 MDNode *SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() { 4416 assert(Changed && "called only if metadata has changed"); 4417 4418 if (!Weights) 4419 return nullptr; 4420 4421 assert(SI.getNumSuccessors() == Weights->size() && 4422 "num of prof branch_weights must accord with num of successors"); 4423 4424 bool AllZeroes = 4425 all_of(Weights.getValue(), [](uint32_t W) { return W == 0; }); 4426 4427 if (AllZeroes || Weights.getValue().size() < 2) 4428 return nullptr; 4429 4430 return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights); 4431 } 4432 4433 void SwitchInstProfUpdateWrapper::init() { 4434 MDNode *ProfileData = getProfBranchWeightsMD(SI); 4435 if (!ProfileData) 4436 return; 4437 4438 if (ProfileData->getNumOperands() != SI.getNumSuccessors() + 1) { 4439 llvm_unreachable("number of prof branch_weights metadata operands does " 4440 "not correspond to number of succesors"); 4441 } 4442 4443 SmallVector<uint32_t, 8> Weights; 4444 for (unsigned CI = 1, CE = SI.getNumSuccessors(); CI <= CE; ++CI) { 4445 ConstantInt *C = mdconst::extract<ConstantInt>(ProfileData->getOperand(CI)); 4446 uint32_t CW = C->getValue().getZExtValue(); 4447 Weights.push_back(CW); 4448 } 4449 this->Weights = std::move(Weights); 4450 } 4451 4452 SwitchInst::CaseIt 4453 SwitchInstProfUpdateWrapper::removeCase(SwitchInst::CaseIt I) { 4454 if (Weights) { 4455 assert(SI.getNumSuccessors() == Weights->size() && 4456 "num of prof branch_weights must accord with num of successors"); 4457 Changed = true; 4458 // Copy the last case to the place of the removed one and shrink. 4459 // This is tightly coupled with the way SwitchInst::removeCase() removes 4460 // the cases in SwitchInst::removeCase(CaseIt). 4461 Weights.getValue()[I->getCaseIndex() + 1] = Weights.getValue().back(); 4462 Weights.getValue().pop_back(); 4463 } 4464 return SI.removeCase(I); 4465 } 4466 4467 void SwitchInstProfUpdateWrapper::addCase( 4468 ConstantInt *OnVal, BasicBlock *Dest, 4469 SwitchInstProfUpdateWrapper::CaseWeightOpt W) { 4470 SI.addCase(OnVal, Dest); 4471 4472 if (!Weights && W && *W) { 4473 Changed = true; 4474 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); 4475 Weights.getValue()[SI.getNumSuccessors() - 1] = *W; 4476 } else if (Weights) { 4477 Changed = true; 4478 Weights.getValue().push_back(W.getValueOr(0)); 4479 } 4480 if (Weights) 4481 assert(SI.getNumSuccessors() == Weights->size() && 4482 "num of prof branch_weights must accord with num of successors"); 4483 } 4484 4485 SymbolTableList<Instruction>::iterator 4486 SwitchInstProfUpdateWrapper::eraseFromParent() { 4487 // Instruction is erased. Mark as unchanged to not touch it in the destructor. 4488 Changed = false; 4489 if (Weights) 4490 Weights->resize(0); 4491 return SI.eraseFromParent(); 4492 } 4493 4494 SwitchInstProfUpdateWrapper::CaseWeightOpt 4495 SwitchInstProfUpdateWrapper::getSuccessorWeight(unsigned idx) { 4496 if (!Weights) 4497 return None; 4498 return Weights.getValue()[idx]; 4499 } 4500 4501 void SwitchInstProfUpdateWrapper::setSuccessorWeight( 4502 unsigned idx, SwitchInstProfUpdateWrapper::CaseWeightOpt W) { 4503 if (!W) 4504 return; 4505 4506 if (!Weights && *W) 4507 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); 4508 4509 if (Weights) { 4510 auto &OldW = Weights.getValue()[idx]; 4511 if (*W != OldW) { 4512 Changed = true; 4513 OldW = *W; 4514 } 4515 } 4516 } 4517 4518 SwitchInstProfUpdateWrapper::CaseWeightOpt 4519 SwitchInstProfUpdateWrapper::getSuccessorWeight(const SwitchInst &SI, 4520 unsigned idx) { 4521 if (MDNode *ProfileData = getProfBranchWeightsMD(SI)) 4522 if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1) 4523 return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1)) 4524 ->getValue() 4525 .getZExtValue(); 4526 4527 return None; 4528 } 4529 4530 //===----------------------------------------------------------------------===// 4531 // IndirectBrInst Implementation 4532 //===----------------------------------------------------------------------===// 4533 4534 void IndirectBrInst::init(Value *Address, unsigned NumDests) { 4535 assert(Address && Address->getType()->isPointerTy() && 4536 "Address of indirectbr must be a pointer"); 4537 ReservedSpace = 1+NumDests; 4538 setNumHungOffUseOperands(1); 4539 allocHungoffUses(ReservedSpace); 4540 4541 Op<0>() = Address; 4542 } 4543 4544 4545 /// growOperands - grow operands - This grows the operand list in response 4546 /// to a push_back style of operation. This grows the number of ops by 2 times. 4547 /// 4548 void IndirectBrInst::growOperands() { 4549 unsigned e = getNumOperands(); 4550 unsigned NumOps = e*2; 4551 4552 ReservedSpace = NumOps; 4553 growHungoffUses(ReservedSpace); 4554 } 4555 4556 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, 4557 Instruction *InsertBefore) 4558 : Instruction(Type::getVoidTy(Address->getContext()), 4559 Instruction::IndirectBr, nullptr, 0, InsertBefore) { 4560 init(Address, NumCases); 4561 } 4562 4563 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, 4564 BasicBlock *InsertAtEnd) 4565 : Instruction(Type::getVoidTy(Address->getContext()), 4566 Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { 4567 init(Address, NumCases); 4568 } 4569 4570 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) 4571 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, 4572 nullptr, IBI.getNumOperands()) { 4573 allocHungoffUses(IBI.getNumOperands()); 4574 Use *OL = getOperandList(); 4575 const Use *InOL = IBI.getOperandList(); 4576 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) 4577 OL[i] = InOL[i]; 4578 SubclassOptionalData = IBI.SubclassOptionalData; 4579 } 4580 4581 /// addDestination - Add a destination. 4582 /// 4583 void IndirectBrInst::addDestination(BasicBlock *DestBB) { 4584 unsigned OpNo = getNumOperands(); 4585 if (OpNo+1 > ReservedSpace) 4586 growOperands(); // Get more space! 4587 // Initialize some new operands. 4588 assert(OpNo < ReservedSpace && "Growing didn't work!"); 4589 setNumHungOffUseOperands(OpNo+1); 4590 getOperandList()[OpNo] = DestBB; 4591 } 4592 4593 /// removeDestination - This method removes the specified successor from the 4594 /// indirectbr instruction. 4595 void IndirectBrInst::removeDestination(unsigned idx) { 4596 assert(idx < getNumOperands()-1 && "Successor index out of range!"); 4597 4598 unsigned NumOps = getNumOperands(); 4599 Use *OL = getOperandList(); 4600 4601 // Replace this value with the last one. 4602 OL[idx+1] = OL[NumOps-1]; 4603 4604 // Nuke the last value. 4605 OL[NumOps-1].set(nullptr); 4606 setNumHungOffUseOperands(NumOps-1); 4607 } 4608 4609 //===----------------------------------------------------------------------===// 4610 // FreezeInst Implementation 4611 //===----------------------------------------------------------------------===// 4612 4613 FreezeInst::FreezeInst(Value *S, 4614 const Twine &Name, Instruction *InsertBefore) 4615 : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) { 4616 setName(Name); 4617 } 4618 4619 FreezeInst::FreezeInst(Value *S, 4620 const Twine &Name, BasicBlock *InsertAtEnd) 4621 : UnaryInstruction(S->getType(), Freeze, S, InsertAtEnd) { 4622 setName(Name); 4623 } 4624 4625 //===----------------------------------------------------------------------===// 4626 // cloneImpl() implementations 4627 //===----------------------------------------------------------------------===// 4628 4629 // Define these methods here so vtables don't get emitted into every translation 4630 // unit that uses these classes. 4631 4632 GetElementPtrInst *GetElementPtrInst::cloneImpl() const { 4633 return new (getNumOperands()) GetElementPtrInst(*this); 4634 } 4635 4636 UnaryOperator *UnaryOperator::cloneImpl() const { 4637 return Create(getOpcode(), Op<0>()); 4638 } 4639 4640 BinaryOperator *BinaryOperator::cloneImpl() const { 4641 return Create(getOpcode(), Op<0>(), Op<1>()); 4642 } 4643 4644 FCmpInst *FCmpInst::cloneImpl() const { 4645 return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); 4646 } 4647 4648 ICmpInst *ICmpInst::cloneImpl() const { 4649 return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); 4650 } 4651 4652 ExtractValueInst *ExtractValueInst::cloneImpl() const { 4653 return new ExtractValueInst(*this); 4654 } 4655 4656 InsertValueInst *InsertValueInst::cloneImpl() const { 4657 return new InsertValueInst(*this); 4658 } 4659 4660 AllocaInst *AllocaInst::cloneImpl() const { 4661 AllocaInst *Result = 4662 new AllocaInst(getAllocatedType(), getType()->getAddressSpace(), 4663 getOperand(0), getAlign()); 4664 Result->setUsedWithInAlloca(isUsedWithInAlloca()); 4665 Result->setSwiftError(isSwiftError()); 4666 return Result; 4667 } 4668 4669 LoadInst *LoadInst::cloneImpl() const { 4670 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), 4671 getAlign(), getOrdering(), getSyncScopeID()); 4672 } 4673 4674 StoreInst *StoreInst::cloneImpl() const { 4675 return new StoreInst(getOperand(0), getOperand(1), isVolatile(), getAlign(), 4676 getOrdering(), getSyncScopeID()); 4677 } 4678 4679 AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { 4680 AtomicCmpXchgInst *Result = new AtomicCmpXchgInst( 4681 getOperand(0), getOperand(1), getOperand(2), getAlign(), 4682 getSuccessOrdering(), getFailureOrdering(), getSyncScopeID()); 4683 Result->setVolatile(isVolatile()); 4684 Result->setWeak(isWeak()); 4685 return Result; 4686 } 4687 4688 AtomicRMWInst *AtomicRMWInst::cloneImpl() const { 4689 AtomicRMWInst *Result = 4690 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), 4691 getAlign(), getOrdering(), getSyncScopeID()); 4692 Result->setVolatile(isVolatile()); 4693 return Result; 4694 } 4695 4696 FenceInst *FenceInst::cloneImpl() const { 4697 return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); 4698 } 4699 4700 TruncInst *TruncInst::cloneImpl() const { 4701 return new TruncInst(getOperand(0), getType()); 4702 } 4703 4704 ZExtInst *ZExtInst::cloneImpl() const { 4705 return new ZExtInst(getOperand(0), getType()); 4706 } 4707 4708 SExtInst *SExtInst::cloneImpl() const { 4709 return new SExtInst(getOperand(0), getType()); 4710 } 4711 4712 FPTruncInst *FPTruncInst::cloneImpl() const { 4713 return new FPTruncInst(getOperand(0), getType()); 4714 } 4715 4716 FPExtInst *FPExtInst::cloneImpl() const { 4717 return new FPExtInst(getOperand(0), getType()); 4718 } 4719 4720 UIToFPInst *UIToFPInst::cloneImpl() const { 4721 return new UIToFPInst(getOperand(0), getType()); 4722 } 4723 4724 SIToFPInst *SIToFPInst::cloneImpl() const { 4725 return new SIToFPInst(getOperand(0), getType()); 4726 } 4727 4728 FPToUIInst *FPToUIInst::cloneImpl() const { 4729 return new FPToUIInst(getOperand(0), getType()); 4730 } 4731 4732 FPToSIInst *FPToSIInst::cloneImpl() const { 4733 return new FPToSIInst(getOperand(0), getType()); 4734 } 4735 4736 PtrToIntInst *PtrToIntInst::cloneImpl() const { 4737 return new PtrToIntInst(getOperand(0), getType()); 4738 } 4739 4740 IntToPtrInst *IntToPtrInst::cloneImpl() const { 4741 return new IntToPtrInst(getOperand(0), getType()); 4742 } 4743 4744 BitCastInst *BitCastInst::cloneImpl() const { 4745 return new BitCastInst(getOperand(0), getType()); 4746 } 4747 4748 AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { 4749 return new AddrSpaceCastInst(getOperand(0), getType()); 4750 } 4751 4752 CallInst *CallInst::cloneImpl() const { 4753 if (hasOperandBundles()) { 4754 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4755 return new(getNumOperands(), DescriptorBytes) CallInst(*this); 4756 } 4757 return new(getNumOperands()) CallInst(*this); 4758 } 4759 4760 SelectInst *SelectInst::cloneImpl() const { 4761 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); 4762 } 4763 4764 VAArgInst *VAArgInst::cloneImpl() const { 4765 return new VAArgInst(getOperand(0), getType()); 4766 } 4767 4768 ExtractElementInst *ExtractElementInst::cloneImpl() const { 4769 return ExtractElementInst::Create(getOperand(0), getOperand(1)); 4770 } 4771 4772 InsertElementInst *InsertElementInst::cloneImpl() const { 4773 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); 4774 } 4775 4776 ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { 4777 return new ShuffleVectorInst(getOperand(0), getOperand(1), getShuffleMask()); 4778 } 4779 4780 PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } 4781 4782 LandingPadInst *LandingPadInst::cloneImpl() const { 4783 return new LandingPadInst(*this); 4784 } 4785 4786 ReturnInst *ReturnInst::cloneImpl() const { 4787 return new(getNumOperands()) ReturnInst(*this); 4788 } 4789 4790 BranchInst *BranchInst::cloneImpl() const { 4791 return new(getNumOperands()) BranchInst(*this); 4792 } 4793 4794 SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } 4795 4796 IndirectBrInst *IndirectBrInst::cloneImpl() const { 4797 return new IndirectBrInst(*this); 4798 } 4799 4800 InvokeInst *InvokeInst::cloneImpl() const { 4801 if (hasOperandBundles()) { 4802 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4803 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); 4804 } 4805 return new(getNumOperands()) InvokeInst(*this); 4806 } 4807 4808 CallBrInst *CallBrInst::cloneImpl() const { 4809 if (hasOperandBundles()) { 4810 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4811 return new (getNumOperands(), DescriptorBytes) CallBrInst(*this); 4812 } 4813 return new (getNumOperands()) CallBrInst(*this); 4814 } 4815 4816 ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } 4817 4818 CleanupReturnInst *CleanupReturnInst::cloneImpl() const { 4819 return new (getNumOperands()) CleanupReturnInst(*this); 4820 } 4821 4822 CatchReturnInst *CatchReturnInst::cloneImpl() const { 4823 return new (getNumOperands()) CatchReturnInst(*this); 4824 } 4825 4826 CatchSwitchInst *CatchSwitchInst::cloneImpl() const { 4827 return new CatchSwitchInst(*this); 4828 } 4829 4830 FuncletPadInst *FuncletPadInst::cloneImpl() const { 4831 return new (getNumOperands()) FuncletPadInst(*this); 4832 } 4833 4834 UnreachableInst *UnreachableInst::cloneImpl() const { 4835 LLVMContext &Context = getContext(); 4836 return new UnreachableInst(Context); 4837 } 4838 4839 FreezeInst *FreezeInst::cloneImpl() const { 4840 return new FreezeInst(getOperand(0)); 4841 } 4842