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(PoisonValue::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 BlockAddress * 964 CallBrInst::getBlockAddressForIndirectDest(unsigned DestNo) const { 965 return BlockAddress::get(const_cast<Function *>(getFunction()), 966 getIndirectDest(DestNo)); 967 } 968 969 CallBrInst::CallBrInst(const CallBrInst &CBI) 970 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, 971 OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), 972 CBI.getNumOperands()) { 973 setCallingConv(CBI.getCallingConv()); 974 std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); 975 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), 976 bundle_op_info_begin()); 977 SubclassOptionalData = CBI.SubclassOptionalData; 978 NumIndirectDests = CBI.NumIndirectDests; 979 } 980 981 CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, 982 Instruction *InsertPt) { 983 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); 984 985 auto *NewCBI = CallBrInst::Create( 986 CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(), 987 CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt); 988 NewCBI->setCallingConv(CBI->getCallingConv()); 989 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; 990 NewCBI->setAttributes(CBI->getAttributes()); 991 NewCBI->setDebugLoc(CBI->getDebugLoc()); 992 NewCBI->NumIndirectDests = CBI->NumIndirectDests; 993 return NewCBI; 994 } 995 996 //===----------------------------------------------------------------------===// 997 // ReturnInst Implementation 998 //===----------------------------------------------------------------------===// 999 1000 ReturnInst::ReturnInst(const ReturnInst &RI) 1001 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, 1002 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), 1003 RI.getNumOperands()) { 1004 if (RI.getNumOperands()) 1005 Op<0>() = RI.Op<0>(); 1006 SubclassOptionalData = RI.SubclassOptionalData; 1007 } 1008 1009 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) 1010 : Instruction(Type::getVoidTy(C), Instruction::Ret, 1011 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 1012 InsertBefore) { 1013 if (retVal) 1014 Op<0>() = retVal; 1015 } 1016 1017 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) 1018 : Instruction(Type::getVoidTy(C), Instruction::Ret, 1019 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 1020 InsertAtEnd) { 1021 if (retVal) 1022 Op<0>() = retVal; 1023 } 1024 1025 ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 1026 : Instruction(Type::getVoidTy(Context), Instruction::Ret, 1027 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} 1028 1029 //===----------------------------------------------------------------------===// 1030 // ResumeInst Implementation 1031 //===----------------------------------------------------------------------===// 1032 1033 ResumeInst::ResumeInst(const ResumeInst &RI) 1034 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, 1035 OperandTraits<ResumeInst>::op_begin(this), 1) { 1036 Op<0>() = RI.Op<0>(); 1037 } 1038 1039 ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) 1040 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 1041 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { 1042 Op<0>() = Exn; 1043 } 1044 1045 ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) 1046 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 1047 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { 1048 Op<0>() = Exn; 1049 } 1050 1051 //===----------------------------------------------------------------------===// 1052 // CleanupReturnInst Implementation 1053 //===----------------------------------------------------------------------===// 1054 1055 CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) 1056 : Instruction(CRI.getType(), Instruction::CleanupRet, 1057 OperandTraits<CleanupReturnInst>::op_end(this) - 1058 CRI.getNumOperands(), 1059 CRI.getNumOperands()) { 1060 setSubclassData<Instruction::OpaqueField>( 1061 CRI.getSubclassData<Instruction::OpaqueField>()); 1062 Op<0>() = CRI.Op<0>(); 1063 if (CRI.hasUnwindDest()) 1064 Op<1>() = CRI.Op<1>(); 1065 } 1066 1067 void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { 1068 if (UnwindBB) 1069 setSubclassData<UnwindDestField>(true); 1070 1071 Op<0>() = CleanupPad; 1072 if (UnwindBB) 1073 Op<1>() = UnwindBB; 1074 } 1075 1076 CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, 1077 unsigned Values, Instruction *InsertBefore) 1078 : Instruction(Type::getVoidTy(CleanupPad->getContext()), 1079 Instruction::CleanupRet, 1080 OperandTraits<CleanupReturnInst>::op_end(this) - Values, 1081 Values, InsertBefore) { 1082 init(CleanupPad, UnwindBB); 1083 } 1084 1085 CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, 1086 unsigned Values, BasicBlock *InsertAtEnd) 1087 : Instruction(Type::getVoidTy(CleanupPad->getContext()), 1088 Instruction::CleanupRet, 1089 OperandTraits<CleanupReturnInst>::op_end(this) - Values, 1090 Values, InsertAtEnd) { 1091 init(CleanupPad, UnwindBB); 1092 } 1093 1094 //===----------------------------------------------------------------------===// 1095 // CatchReturnInst Implementation 1096 //===----------------------------------------------------------------------===// 1097 void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { 1098 Op<0>() = CatchPad; 1099 Op<1>() = BB; 1100 } 1101 1102 CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) 1103 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, 1104 OperandTraits<CatchReturnInst>::op_begin(this), 2) { 1105 Op<0>() = CRI.Op<0>(); 1106 Op<1>() = CRI.Op<1>(); 1107 } 1108 1109 CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, 1110 Instruction *InsertBefore) 1111 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, 1112 OperandTraits<CatchReturnInst>::op_begin(this), 2, 1113 InsertBefore) { 1114 init(CatchPad, BB); 1115 } 1116 1117 CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, 1118 BasicBlock *InsertAtEnd) 1119 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, 1120 OperandTraits<CatchReturnInst>::op_begin(this), 2, 1121 InsertAtEnd) { 1122 init(CatchPad, BB); 1123 } 1124 1125 //===----------------------------------------------------------------------===// 1126 // CatchSwitchInst Implementation 1127 //===----------------------------------------------------------------------===// 1128 1129 CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 1130 unsigned NumReservedValues, 1131 const Twine &NameStr, 1132 Instruction *InsertBefore) 1133 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, 1134 InsertBefore) { 1135 if (UnwindDest) 1136 ++NumReservedValues; 1137 init(ParentPad, UnwindDest, NumReservedValues + 1); 1138 setName(NameStr); 1139 } 1140 1141 CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 1142 unsigned NumReservedValues, 1143 const Twine &NameStr, BasicBlock *InsertAtEnd) 1144 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, 1145 InsertAtEnd) { 1146 if (UnwindDest) 1147 ++NumReservedValues; 1148 init(ParentPad, UnwindDest, NumReservedValues + 1); 1149 setName(NameStr); 1150 } 1151 1152 CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) 1153 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, 1154 CSI.getNumOperands()) { 1155 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); 1156 setNumHungOffUseOperands(ReservedSpace); 1157 Use *OL = getOperandList(); 1158 const Use *InOL = CSI.getOperandList(); 1159 for (unsigned I = 1, E = ReservedSpace; I != E; ++I) 1160 OL[I] = InOL[I]; 1161 } 1162 1163 void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, 1164 unsigned NumReservedValues) { 1165 assert(ParentPad && NumReservedValues); 1166 1167 ReservedSpace = NumReservedValues; 1168 setNumHungOffUseOperands(UnwindDest ? 2 : 1); 1169 allocHungoffUses(ReservedSpace); 1170 1171 Op<0>() = ParentPad; 1172 if (UnwindDest) { 1173 setSubclassData<UnwindDestField>(true); 1174 setUnwindDest(UnwindDest); 1175 } 1176 } 1177 1178 /// growOperands - grow operands - This grows the operand list in response to a 1179 /// push_back style of operation. This grows the number of ops by 2 times. 1180 void CatchSwitchInst::growOperands(unsigned Size) { 1181 unsigned NumOperands = getNumOperands(); 1182 assert(NumOperands >= 1); 1183 if (ReservedSpace >= NumOperands + Size) 1184 return; 1185 ReservedSpace = (NumOperands + Size / 2) * 2; 1186 growHungoffUses(ReservedSpace); 1187 } 1188 1189 void CatchSwitchInst::addHandler(BasicBlock *Handler) { 1190 unsigned OpNo = getNumOperands(); 1191 growOperands(1); 1192 assert(OpNo < ReservedSpace && "Growing didn't work!"); 1193 setNumHungOffUseOperands(getNumOperands() + 1); 1194 getOperandList()[OpNo] = Handler; 1195 } 1196 1197 void CatchSwitchInst::removeHandler(handler_iterator HI) { 1198 // Move all subsequent handlers up one. 1199 Use *EndDst = op_end() - 1; 1200 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) 1201 *CurDst = *(CurDst + 1); 1202 // Null out the last handler use. 1203 *EndDst = nullptr; 1204 1205 setNumHungOffUseOperands(getNumOperands() - 1); 1206 } 1207 1208 //===----------------------------------------------------------------------===// 1209 // FuncletPadInst Implementation 1210 //===----------------------------------------------------------------------===// 1211 void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, 1212 const Twine &NameStr) { 1213 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); 1214 llvm::copy(Args, op_begin()); 1215 setParentPad(ParentPad); 1216 setName(NameStr); 1217 } 1218 1219 FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) 1220 : Instruction(FPI.getType(), FPI.getOpcode(), 1221 OperandTraits<FuncletPadInst>::op_end(this) - 1222 FPI.getNumOperands(), 1223 FPI.getNumOperands()) { 1224 std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); 1225 setParentPad(FPI.getParentPad()); 1226 } 1227 1228 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, 1229 ArrayRef<Value *> Args, unsigned Values, 1230 const Twine &NameStr, Instruction *InsertBefore) 1231 : Instruction(ParentPad->getType(), Op, 1232 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, 1233 InsertBefore) { 1234 init(ParentPad, Args, NameStr); 1235 } 1236 1237 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, 1238 ArrayRef<Value *> Args, unsigned Values, 1239 const Twine &NameStr, BasicBlock *InsertAtEnd) 1240 : Instruction(ParentPad->getType(), Op, 1241 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, 1242 InsertAtEnd) { 1243 init(ParentPad, Args, NameStr); 1244 } 1245 1246 //===----------------------------------------------------------------------===// 1247 // UnreachableInst Implementation 1248 //===----------------------------------------------------------------------===// 1249 1250 UnreachableInst::UnreachableInst(LLVMContext &Context, 1251 Instruction *InsertBefore) 1252 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, 1253 0, InsertBefore) {} 1254 UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 1255 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, 1256 0, InsertAtEnd) {} 1257 1258 //===----------------------------------------------------------------------===// 1259 // BranchInst Implementation 1260 //===----------------------------------------------------------------------===// 1261 1262 void BranchInst::AssertOK() { 1263 if (isConditional()) 1264 assert(getCondition()->getType()->isIntegerTy(1) && 1265 "May only branch on boolean predicates!"); 1266 } 1267 1268 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) 1269 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1270 OperandTraits<BranchInst>::op_end(this) - 1, 1, 1271 InsertBefore) { 1272 assert(IfTrue && "Branch destination may not be null!"); 1273 Op<-1>() = IfTrue; 1274 } 1275 1276 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1277 Instruction *InsertBefore) 1278 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1279 OperandTraits<BranchInst>::op_end(this) - 3, 3, 1280 InsertBefore) { 1281 // Assign in order of operand index to make use-list order predictable. 1282 Op<-3>() = Cond; 1283 Op<-2>() = IfFalse; 1284 Op<-1>() = IfTrue; 1285 #ifndef NDEBUG 1286 AssertOK(); 1287 #endif 1288 } 1289 1290 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) 1291 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1292 OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { 1293 assert(IfTrue && "Branch destination may not be null!"); 1294 Op<-1>() = IfTrue; 1295 } 1296 1297 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1298 BasicBlock *InsertAtEnd) 1299 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1300 OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { 1301 // Assign in order of operand index to make use-list order predictable. 1302 Op<-3>() = Cond; 1303 Op<-2>() = IfFalse; 1304 Op<-1>() = IfTrue; 1305 #ifndef NDEBUG 1306 AssertOK(); 1307 #endif 1308 } 1309 1310 BranchInst::BranchInst(const BranchInst &BI) 1311 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, 1312 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), 1313 BI.getNumOperands()) { 1314 // Assign in order of operand index to make use-list order predictable. 1315 if (BI.getNumOperands() != 1) { 1316 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); 1317 Op<-3>() = BI.Op<-3>(); 1318 Op<-2>() = BI.Op<-2>(); 1319 } 1320 Op<-1>() = BI.Op<-1>(); 1321 SubclassOptionalData = BI.SubclassOptionalData; 1322 } 1323 1324 void BranchInst::swapSuccessors() { 1325 assert(isConditional() && 1326 "Cannot swap successors of an unconditional branch"); 1327 Op<-1>().swap(Op<-2>()); 1328 1329 // Update profile metadata if present and it matches our structural 1330 // expectations. 1331 swapProfMetadata(); 1332 } 1333 1334 //===----------------------------------------------------------------------===// 1335 // AllocaInst Implementation 1336 //===----------------------------------------------------------------------===// 1337 1338 static Value *getAISize(LLVMContext &Context, Value *Amt) { 1339 if (!Amt) 1340 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); 1341 else { 1342 assert(!isa<BasicBlock>(Amt) && 1343 "Passed basic block into allocation size parameter! Use other ctor"); 1344 assert(Amt->getType()->isIntegerTy() && 1345 "Allocation array size is not an integer!"); 1346 } 1347 return Amt; 1348 } 1349 1350 static Align computeAllocaDefaultAlign(Type *Ty, BasicBlock *BB) { 1351 assert(BB && "Insertion BB cannot be null when alignment not provided!"); 1352 assert(BB->getParent() && 1353 "BB must be in a Function when alignment not provided!"); 1354 const DataLayout &DL = BB->getModule()->getDataLayout(); 1355 return DL.getPrefTypeAlign(Ty); 1356 } 1357 1358 static Align computeAllocaDefaultAlign(Type *Ty, Instruction *I) { 1359 assert(I && "Insertion position cannot be null when alignment not provided!"); 1360 return computeAllocaDefaultAlign(Ty, I->getParent()); 1361 } 1362 1363 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, 1364 Instruction *InsertBefore) 1365 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} 1366 1367 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, 1368 BasicBlock *InsertAtEnd) 1369 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} 1370 1371 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1372 const Twine &Name, Instruction *InsertBefore) 1373 : AllocaInst(Ty, AddrSpace, ArraySize, 1374 computeAllocaDefaultAlign(Ty, InsertBefore), Name, 1375 InsertBefore) {} 1376 1377 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1378 const Twine &Name, BasicBlock *InsertAtEnd) 1379 : AllocaInst(Ty, AddrSpace, ArraySize, 1380 computeAllocaDefaultAlign(Ty, InsertAtEnd), Name, 1381 InsertAtEnd) {} 1382 1383 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1384 Align Align, const Twine &Name, 1385 Instruction *InsertBefore) 1386 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, 1387 getAISize(Ty->getContext(), ArraySize), InsertBefore), 1388 AllocatedType(Ty) { 1389 setAlignment(Align); 1390 assert(!Ty->isVoidTy() && "Cannot allocate void!"); 1391 setName(Name); 1392 } 1393 1394 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1395 Align Align, const Twine &Name, BasicBlock *InsertAtEnd) 1396 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, 1397 getAISize(Ty->getContext(), ArraySize), InsertAtEnd), 1398 AllocatedType(Ty) { 1399 setAlignment(Align); 1400 assert(!Ty->isVoidTy() && "Cannot allocate void!"); 1401 setName(Name); 1402 } 1403 1404 1405 bool AllocaInst::isArrayAllocation() const { 1406 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) 1407 return !CI->isOne(); 1408 return true; 1409 } 1410 1411 /// isStaticAlloca - Return true if this alloca is in the entry block of the 1412 /// function and is a constant size. If so, the code generator will fold it 1413 /// into the prolog/epilog code, so it is basically free. 1414 bool AllocaInst::isStaticAlloca() const { 1415 // Must be constant size. 1416 if (!isa<ConstantInt>(getArraySize())) return false; 1417 1418 // Must be in the entry block. 1419 const BasicBlock *Parent = getParent(); 1420 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); 1421 } 1422 1423 //===----------------------------------------------------------------------===// 1424 // LoadInst Implementation 1425 //===----------------------------------------------------------------------===// 1426 1427 void LoadInst::AssertOK() { 1428 assert(getOperand(0)->getType()->isPointerTy() && 1429 "Ptr must have pointer type."); 1430 } 1431 1432 static Align computeLoadStoreDefaultAlign(Type *Ty, BasicBlock *BB) { 1433 assert(BB && "Insertion BB cannot be null when alignment not provided!"); 1434 assert(BB->getParent() && 1435 "BB must be in a Function when alignment not provided!"); 1436 const DataLayout &DL = BB->getModule()->getDataLayout(); 1437 return DL.getABITypeAlign(Ty); 1438 } 1439 1440 static Align computeLoadStoreDefaultAlign(Type *Ty, Instruction *I) { 1441 assert(I && "Insertion position cannot be null when alignment not provided!"); 1442 return computeLoadStoreDefaultAlign(Ty, I->getParent()); 1443 } 1444 1445 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, 1446 Instruction *InsertBef) 1447 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} 1448 1449 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, 1450 BasicBlock *InsertAE) 1451 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} 1452 1453 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1454 Instruction *InsertBef) 1455 : LoadInst(Ty, Ptr, Name, isVolatile, 1456 computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {} 1457 1458 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1459 BasicBlock *InsertAE) 1460 : LoadInst(Ty, Ptr, Name, isVolatile, 1461 computeLoadStoreDefaultAlign(Ty, InsertAE), InsertAE) {} 1462 1463 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1464 Align Align, Instruction *InsertBef) 1465 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, 1466 SyncScope::System, InsertBef) {} 1467 1468 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1469 Align Align, BasicBlock *InsertAE) 1470 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, 1471 SyncScope::System, InsertAE) {} 1472 1473 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1474 Align Align, AtomicOrdering Order, SyncScope::ID SSID, 1475 Instruction *InsertBef) 1476 : UnaryInstruction(Ty, Load, Ptr, InsertBef) { 1477 assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty)); 1478 setVolatile(isVolatile); 1479 setAlignment(Align); 1480 setAtomic(Order, SSID); 1481 AssertOK(); 1482 setName(Name); 1483 } 1484 1485 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1486 Align Align, AtomicOrdering Order, SyncScope::ID SSID, 1487 BasicBlock *InsertAE) 1488 : UnaryInstruction(Ty, Load, Ptr, InsertAE) { 1489 assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty)); 1490 setVolatile(isVolatile); 1491 setAlignment(Align); 1492 setAtomic(Order, SSID); 1493 AssertOK(); 1494 setName(Name); 1495 } 1496 1497 //===----------------------------------------------------------------------===// 1498 // StoreInst Implementation 1499 //===----------------------------------------------------------------------===// 1500 1501 void StoreInst::AssertOK() { 1502 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); 1503 assert(getOperand(1)->getType()->isPointerTy() && 1504 "Ptr must have pointer type!"); 1505 assert(cast<PointerType>(getOperand(1)->getType()) 1506 ->isOpaqueOrPointeeTypeMatches(getOperand(0)->getType()) && 1507 "Ptr must be a pointer to Val type!"); 1508 } 1509 1510 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) 1511 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} 1512 1513 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) 1514 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} 1515 1516 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1517 Instruction *InsertBefore) 1518 : StoreInst(val, addr, isVolatile, 1519 computeLoadStoreDefaultAlign(val->getType(), InsertBefore), 1520 InsertBefore) {} 1521 1522 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1523 BasicBlock *InsertAtEnd) 1524 : StoreInst(val, addr, isVolatile, 1525 computeLoadStoreDefaultAlign(val->getType(), InsertAtEnd), 1526 InsertAtEnd) {} 1527 1528 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1529 Instruction *InsertBefore) 1530 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, 1531 SyncScope::System, InsertBefore) {} 1532 1533 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1534 BasicBlock *InsertAtEnd) 1535 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, 1536 SyncScope::System, InsertAtEnd) {} 1537 1538 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1539 AtomicOrdering Order, SyncScope::ID SSID, 1540 Instruction *InsertBefore) 1541 : Instruction(Type::getVoidTy(val->getContext()), Store, 1542 OperandTraits<StoreInst>::op_begin(this), 1543 OperandTraits<StoreInst>::operands(this), InsertBefore) { 1544 Op<0>() = val; 1545 Op<1>() = addr; 1546 setVolatile(isVolatile); 1547 setAlignment(Align); 1548 setAtomic(Order, SSID); 1549 AssertOK(); 1550 } 1551 1552 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1553 AtomicOrdering Order, SyncScope::ID SSID, 1554 BasicBlock *InsertAtEnd) 1555 : Instruction(Type::getVoidTy(val->getContext()), Store, 1556 OperandTraits<StoreInst>::op_begin(this), 1557 OperandTraits<StoreInst>::operands(this), InsertAtEnd) { 1558 Op<0>() = val; 1559 Op<1>() = addr; 1560 setVolatile(isVolatile); 1561 setAlignment(Align); 1562 setAtomic(Order, SSID); 1563 AssertOK(); 1564 } 1565 1566 1567 //===----------------------------------------------------------------------===// 1568 // AtomicCmpXchgInst Implementation 1569 //===----------------------------------------------------------------------===// 1570 1571 void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, 1572 Align Alignment, AtomicOrdering SuccessOrdering, 1573 AtomicOrdering FailureOrdering, 1574 SyncScope::ID SSID) { 1575 Op<0>() = Ptr; 1576 Op<1>() = Cmp; 1577 Op<2>() = NewVal; 1578 setSuccessOrdering(SuccessOrdering); 1579 setFailureOrdering(FailureOrdering); 1580 setSyncScopeID(SSID); 1581 setAlignment(Alignment); 1582 1583 assert(getOperand(0) && getOperand(1) && getOperand(2) && 1584 "All operands must be non-null!"); 1585 assert(getOperand(0)->getType()->isPointerTy() && 1586 "Ptr must have pointer type!"); 1587 assert(cast<PointerType>(getOperand(0)->getType()) 1588 ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) && 1589 "Ptr must be a pointer to Cmp type!"); 1590 assert(cast<PointerType>(getOperand(0)->getType()) 1591 ->isOpaqueOrPointeeTypeMatches(getOperand(2)->getType()) && 1592 "Ptr must be a pointer to NewVal type!"); 1593 assert(getOperand(1)->getType() == getOperand(2)->getType() && 1594 "Cmp type and NewVal type must be same!"); 1595 } 1596 1597 AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1598 Align Alignment, 1599 AtomicOrdering SuccessOrdering, 1600 AtomicOrdering FailureOrdering, 1601 SyncScope::ID SSID, 1602 Instruction *InsertBefore) 1603 : Instruction( 1604 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), 1605 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1606 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { 1607 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); 1608 } 1609 1610 AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1611 Align Alignment, 1612 AtomicOrdering SuccessOrdering, 1613 AtomicOrdering FailureOrdering, 1614 SyncScope::ID SSID, 1615 BasicBlock *InsertAtEnd) 1616 : Instruction( 1617 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), 1618 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1619 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { 1620 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); 1621 } 1622 1623 //===----------------------------------------------------------------------===// 1624 // AtomicRMWInst Implementation 1625 //===----------------------------------------------------------------------===// 1626 1627 void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, 1628 Align Alignment, AtomicOrdering Ordering, 1629 SyncScope::ID SSID) { 1630 Op<0>() = Ptr; 1631 Op<1>() = Val; 1632 setOperation(Operation); 1633 setOrdering(Ordering); 1634 setSyncScopeID(SSID); 1635 setAlignment(Alignment); 1636 1637 assert(getOperand(0) && getOperand(1) && 1638 "All operands must be non-null!"); 1639 assert(getOperand(0)->getType()->isPointerTy() && 1640 "Ptr must have pointer type!"); 1641 assert(cast<PointerType>(getOperand(0)->getType()) 1642 ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) && 1643 "Ptr must be a pointer to Val type!"); 1644 assert(Ordering != AtomicOrdering::NotAtomic && 1645 "AtomicRMW instructions must be atomic!"); 1646 } 1647 1648 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1649 Align Alignment, AtomicOrdering Ordering, 1650 SyncScope::ID SSID, Instruction *InsertBefore) 1651 : Instruction(Val->getType(), AtomicRMW, 1652 OperandTraits<AtomicRMWInst>::op_begin(this), 1653 OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) { 1654 Init(Operation, Ptr, Val, Alignment, Ordering, SSID); 1655 } 1656 1657 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1658 Align Alignment, AtomicOrdering Ordering, 1659 SyncScope::ID SSID, BasicBlock *InsertAtEnd) 1660 : Instruction(Val->getType(), AtomicRMW, 1661 OperandTraits<AtomicRMWInst>::op_begin(this), 1662 OperandTraits<AtomicRMWInst>::operands(this), InsertAtEnd) { 1663 Init(Operation, Ptr, Val, Alignment, Ordering, SSID); 1664 } 1665 1666 StringRef AtomicRMWInst::getOperationName(BinOp Op) { 1667 switch (Op) { 1668 case AtomicRMWInst::Xchg: 1669 return "xchg"; 1670 case AtomicRMWInst::Add: 1671 return "add"; 1672 case AtomicRMWInst::Sub: 1673 return "sub"; 1674 case AtomicRMWInst::And: 1675 return "and"; 1676 case AtomicRMWInst::Nand: 1677 return "nand"; 1678 case AtomicRMWInst::Or: 1679 return "or"; 1680 case AtomicRMWInst::Xor: 1681 return "xor"; 1682 case AtomicRMWInst::Max: 1683 return "max"; 1684 case AtomicRMWInst::Min: 1685 return "min"; 1686 case AtomicRMWInst::UMax: 1687 return "umax"; 1688 case AtomicRMWInst::UMin: 1689 return "umin"; 1690 case AtomicRMWInst::FAdd: 1691 return "fadd"; 1692 case AtomicRMWInst::FSub: 1693 return "fsub"; 1694 case AtomicRMWInst::FMax: 1695 return "fmax"; 1696 case AtomicRMWInst::FMin: 1697 return "fmin"; 1698 case AtomicRMWInst::BAD_BINOP: 1699 return "<invalid operation>"; 1700 } 1701 1702 llvm_unreachable("invalid atomicrmw operation"); 1703 } 1704 1705 //===----------------------------------------------------------------------===// 1706 // FenceInst Implementation 1707 //===----------------------------------------------------------------------===// 1708 1709 FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1710 SyncScope::ID SSID, 1711 Instruction *InsertBefore) 1712 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { 1713 setOrdering(Ordering); 1714 setSyncScopeID(SSID); 1715 } 1716 1717 FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1718 SyncScope::ID SSID, 1719 BasicBlock *InsertAtEnd) 1720 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { 1721 setOrdering(Ordering); 1722 setSyncScopeID(SSID); 1723 } 1724 1725 //===----------------------------------------------------------------------===// 1726 // GetElementPtrInst Implementation 1727 //===----------------------------------------------------------------------===// 1728 1729 void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, 1730 const Twine &Name) { 1731 assert(getNumOperands() == 1 + IdxList.size() && 1732 "NumOperands not initialized?"); 1733 Op<0>() = Ptr; 1734 llvm::copy(IdxList, op_begin() + 1); 1735 setName(Name); 1736 } 1737 1738 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) 1739 : Instruction(GEPI.getType(), GetElementPtr, 1740 OperandTraits<GetElementPtrInst>::op_end(this) - 1741 GEPI.getNumOperands(), 1742 GEPI.getNumOperands()), 1743 SourceElementType(GEPI.SourceElementType), 1744 ResultElementType(GEPI.ResultElementType) { 1745 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); 1746 SubclassOptionalData = GEPI.SubclassOptionalData; 1747 } 1748 1749 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) { 1750 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1751 if (!Struct->indexValid(Idx)) 1752 return nullptr; 1753 return Struct->getTypeAtIndex(Idx); 1754 } 1755 if (!Idx->getType()->isIntOrIntVectorTy()) 1756 return nullptr; 1757 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1758 return Array->getElementType(); 1759 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1760 return Vector->getElementType(); 1761 return nullptr; 1762 } 1763 1764 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) { 1765 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1766 if (Idx >= Struct->getNumElements()) 1767 return nullptr; 1768 return Struct->getElementType(Idx); 1769 } 1770 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1771 return Array->getElementType(); 1772 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1773 return Vector->getElementType(); 1774 return nullptr; 1775 } 1776 1777 template <typename IndexTy> 1778 static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) { 1779 if (IdxList.empty()) 1780 return Ty; 1781 for (IndexTy V : IdxList.slice(1)) { 1782 Ty = GetElementPtrInst::getTypeAtIndex(Ty, V); 1783 if (!Ty) 1784 return Ty; 1785 } 1786 return Ty; 1787 } 1788 1789 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { 1790 return getIndexedTypeInternal(Ty, IdxList); 1791 } 1792 1793 Type *GetElementPtrInst::getIndexedType(Type *Ty, 1794 ArrayRef<Constant *> IdxList) { 1795 return getIndexedTypeInternal(Ty, IdxList); 1796 } 1797 1798 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { 1799 return getIndexedTypeInternal(Ty, IdxList); 1800 } 1801 1802 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 1803 /// zeros. If so, the result pointer and the first operand have the same 1804 /// value, just potentially different types. 1805 bool GetElementPtrInst::hasAllZeroIndices() const { 1806 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1807 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { 1808 if (!CI->isZero()) return false; 1809 } else { 1810 return false; 1811 } 1812 } 1813 return true; 1814 } 1815 1816 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 1817 /// constant integers. If so, the result pointer and the first operand have 1818 /// a constant offset between them. 1819 bool GetElementPtrInst::hasAllConstantIndices() const { 1820 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1821 if (!isa<ConstantInt>(getOperand(i))) 1822 return false; 1823 } 1824 return true; 1825 } 1826 1827 void GetElementPtrInst::setIsInBounds(bool B) { 1828 cast<GEPOperator>(this)->setIsInBounds(B); 1829 } 1830 1831 bool GetElementPtrInst::isInBounds() const { 1832 return cast<GEPOperator>(this)->isInBounds(); 1833 } 1834 1835 bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, 1836 APInt &Offset) const { 1837 // Delegate to the generic GEPOperator implementation. 1838 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); 1839 } 1840 1841 bool GetElementPtrInst::collectOffset( 1842 const DataLayout &DL, unsigned BitWidth, 1843 MapVector<Value *, APInt> &VariableOffsets, 1844 APInt &ConstantOffset) const { 1845 // Delegate to the generic GEPOperator implementation. 1846 return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets, 1847 ConstantOffset); 1848 } 1849 1850 //===----------------------------------------------------------------------===// 1851 // ExtractElementInst Implementation 1852 //===----------------------------------------------------------------------===// 1853 1854 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1855 const Twine &Name, 1856 Instruction *InsertBef) 1857 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1858 ExtractElement, 1859 OperandTraits<ExtractElementInst>::op_begin(this), 1860 2, InsertBef) { 1861 assert(isValidOperands(Val, Index) && 1862 "Invalid extractelement instruction operands!"); 1863 Op<0>() = Val; 1864 Op<1>() = Index; 1865 setName(Name); 1866 } 1867 1868 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1869 const Twine &Name, 1870 BasicBlock *InsertAE) 1871 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1872 ExtractElement, 1873 OperandTraits<ExtractElementInst>::op_begin(this), 1874 2, InsertAE) { 1875 assert(isValidOperands(Val, Index) && 1876 "Invalid extractelement instruction operands!"); 1877 1878 Op<0>() = Val; 1879 Op<1>() = Index; 1880 setName(Name); 1881 } 1882 1883 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { 1884 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) 1885 return false; 1886 return true; 1887 } 1888 1889 //===----------------------------------------------------------------------===// 1890 // InsertElementInst Implementation 1891 //===----------------------------------------------------------------------===// 1892 1893 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1894 const Twine &Name, 1895 Instruction *InsertBef) 1896 : Instruction(Vec->getType(), InsertElement, 1897 OperandTraits<InsertElementInst>::op_begin(this), 1898 3, InsertBef) { 1899 assert(isValidOperands(Vec, Elt, Index) && 1900 "Invalid insertelement instruction operands!"); 1901 Op<0>() = Vec; 1902 Op<1>() = Elt; 1903 Op<2>() = Index; 1904 setName(Name); 1905 } 1906 1907 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1908 const Twine &Name, 1909 BasicBlock *InsertAE) 1910 : Instruction(Vec->getType(), InsertElement, 1911 OperandTraits<InsertElementInst>::op_begin(this), 1912 3, InsertAE) { 1913 assert(isValidOperands(Vec, Elt, Index) && 1914 "Invalid insertelement instruction operands!"); 1915 1916 Op<0>() = Vec; 1917 Op<1>() = Elt; 1918 Op<2>() = Index; 1919 setName(Name); 1920 } 1921 1922 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 1923 const Value *Index) { 1924 if (!Vec->getType()->isVectorTy()) 1925 return false; // First operand of insertelement must be vector type. 1926 1927 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) 1928 return false;// Second operand of insertelement must be vector element type. 1929 1930 if (!Index->getType()->isIntegerTy()) 1931 return false; // Third operand of insertelement must be i32. 1932 return true; 1933 } 1934 1935 //===----------------------------------------------------------------------===// 1936 // ShuffleVectorInst Implementation 1937 //===----------------------------------------------------------------------===// 1938 1939 static Value *createPlaceholderForShuffleVector(Value *V) { 1940 assert(V && "Cannot create placeholder of nullptr V"); 1941 return PoisonValue::get(V->getType()); 1942 } 1943 1944 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *Mask, const Twine &Name, 1945 Instruction *InsertBefore) 1946 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1947 InsertBefore) {} 1948 1949 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *Mask, const Twine &Name, 1950 BasicBlock *InsertAtEnd) 1951 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1952 InsertAtEnd) {} 1953 1954 ShuffleVectorInst::ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, 1955 const Twine &Name, 1956 Instruction *InsertBefore) 1957 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1958 InsertBefore) {} 1959 1960 ShuffleVectorInst::ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, 1961 const Twine &Name, BasicBlock *InsertAtEnd) 1962 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1963 InsertAtEnd) {} 1964 1965 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1966 const Twine &Name, 1967 Instruction *InsertBefore) 1968 : Instruction( 1969 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1970 cast<VectorType>(Mask->getType())->getElementCount()), 1971 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1972 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 1973 assert(isValidOperands(V1, V2, Mask) && 1974 "Invalid shuffle vector instruction operands!"); 1975 1976 Op<0>() = V1; 1977 Op<1>() = V2; 1978 SmallVector<int, 16> MaskArr; 1979 getShuffleMask(cast<Constant>(Mask), MaskArr); 1980 setShuffleMask(MaskArr); 1981 setName(Name); 1982 } 1983 1984 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1985 const Twine &Name, BasicBlock *InsertAtEnd) 1986 : Instruction( 1987 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1988 cast<VectorType>(Mask->getType())->getElementCount()), 1989 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1990 OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { 1991 assert(isValidOperands(V1, V2, Mask) && 1992 "Invalid shuffle vector instruction operands!"); 1993 1994 Op<0>() = V1; 1995 Op<1>() = V2; 1996 SmallVector<int, 16> MaskArr; 1997 getShuffleMask(cast<Constant>(Mask), MaskArr); 1998 setShuffleMask(MaskArr); 1999 setName(Name); 2000 } 2001 2002 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, 2003 const Twine &Name, 2004 Instruction *InsertBefore) 2005 : Instruction( 2006 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 2007 Mask.size(), isa<ScalableVectorType>(V1->getType())), 2008 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 2009 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 2010 assert(isValidOperands(V1, V2, Mask) && 2011 "Invalid shuffle vector instruction operands!"); 2012 Op<0>() = V1; 2013 Op<1>() = V2; 2014 setShuffleMask(Mask); 2015 setName(Name); 2016 } 2017 2018 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, 2019 const Twine &Name, BasicBlock *InsertAtEnd) 2020 : Instruction( 2021 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 2022 Mask.size(), isa<ScalableVectorType>(V1->getType())), 2023 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 2024 OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { 2025 assert(isValidOperands(V1, V2, Mask) && 2026 "Invalid shuffle vector instruction operands!"); 2027 2028 Op<0>() = V1; 2029 Op<1>() = V2; 2030 setShuffleMask(Mask); 2031 setName(Name); 2032 } 2033 2034 void ShuffleVectorInst::commute() { 2035 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2036 int NumMaskElts = ShuffleMask.size(); 2037 SmallVector<int, 16> NewMask(NumMaskElts); 2038 for (int i = 0; i != NumMaskElts; ++i) { 2039 int MaskElt = getMaskValue(i); 2040 if (MaskElt == UndefMaskElem) { 2041 NewMask[i] = UndefMaskElem; 2042 continue; 2043 } 2044 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask"); 2045 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; 2046 NewMask[i] = MaskElt; 2047 } 2048 setShuffleMask(NewMask); 2049 Op<0>().swap(Op<1>()); 2050 } 2051 2052 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 2053 ArrayRef<int> Mask) { 2054 // V1 and V2 must be vectors of the same type. 2055 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) 2056 return false; 2057 2058 // Make sure the mask elements make sense. 2059 int V1Size = 2060 cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue(); 2061 for (int Elem : Mask) 2062 if (Elem != UndefMaskElem && Elem >= V1Size * 2) 2063 return false; 2064 2065 if (isa<ScalableVectorType>(V1->getType())) 2066 if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !is_splat(Mask)) 2067 return false; 2068 2069 return true; 2070 } 2071 2072 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 2073 const Value *Mask) { 2074 // V1 and V2 must be vectors of the same type. 2075 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) 2076 return false; 2077 2078 // Mask must be vector of i32, and must be the same kind of vector as the 2079 // input vectors 2080 auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); 2081 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) || 2082 isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType())) 2083 return false; 2084 2085 // Check to see if Mask is valid. 2086 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) 2087 return true; 2088 2089 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { 2090 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 2091 for (Value *Op : MV->operands()) { 2092 if (auto *CI = dyn_cast<ConstantInt>(Op)) { 2093 if (CI->uge(V1Size*2)) 2094 return false; 2095 } else if (!isa<UndefValue>(Op)) { 2096 return false; 2097 } 2098 } 2099 return true; 2100 } 2101 2102 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 2103 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 2104 for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements(); 2105 i != e; ++i) 2106 if (CDS->getElementAsInteger(i) >= V1Size*2) 2107 return false; 2108 return true; 2109 } 2110 2111 return false; 2112 } 2113 2114 void ShuffleVectorInst::getShuffleMask(const Constant *Mask, 2115 SmallVectorImpl<int> &Result) { 2116 ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount(); 2117 2118 if (isa<ConstantAggregateZero>(Mask)) { 2119 Result.resize(EC.getKnownMinValue(), 0); 2120 return; 2121 } 2122 2123 Result.reserve(EC.getKnownMinValue()); 2124 2125 if (EC.isScalable()) { 2126 assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) && 2127 "Scalable vector shuffle mask must be undef or zeroinitializer"); 2128 int MaskVal = isa<UndefValue>(Mask) ? -1 : 0; 2129 for (unsigned I = 0; I < EC.getKnownMinValue(); ++I) 2130 Result.emplace_back(MaskVal); 2131 return; 2132 } 2133 2134 unsigned NumElts = EC.getKnownMinValue(); 2135 2136 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 2137 for (unsigned i = 0; i != NumElts; ++i) 2138 Result.push_back(CDS->getElementAsInteger(i)); 2139 return; 2140 } 2141 for (unsigned i = 0; i != NumElts; ++i) { 2142 Constant *C = Mask->getAggregateElement(i); 2143 Result.push_back(isa<UndefValue>(C) ? -1 : 2144 cast<ConstantInt>(C)->getZExtValue()); 2145 } 2146 } 2147 2148 void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) { 2149 ShuffleMask.assign(Mask.begin(), Mask.end()); 2150 ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType()); 2151 } 2152 2153 Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask, 2154 Type *ResultTy) { 2155 Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext()); 2156 if (isa<ScalableVectorType>(ResultTy)) { 2157 assert(is_splat(Mask) && "Unexpected shuffle"); 2158 Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true); 2159 if (Mask[0] == 0) 2160 return Constant::getNullValue(VecTy); 2161 return UndefValue::get(VecTy); 2162 } 2163 SmallVector<Constant *, 16> MaskConst; 2164 for (int Elem : Mask) { 2165 if (Elem == UndefMaskElem) 2166 MaskConst.push_back(UndefValue::get(Int32Ty)); 2167 else 2168 MaskConst.push_back(ConstantInt::get(Int32Ty, Elem)); 2169 } 2170 return ConstantVector::get(MaskConst); 2171 } 2172 2173 static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 2174 assert(!Mask.empty() && "Shuffle mask must contain elements"); 2175 bool UsesLHS = false; 2176 bool UsesRHS = false; 2177 for (int I : Mask) { 2178 if (I == -1) 2179 continue; 2180 assert(I >= 0 && I < (NumOpElts * 2) && 2181 "Out-of-bounds shuffle mask element"); 2182 UsesLHS |= (I < NumOpElts); 2183 UsesRHS |= (I >= NumOpElts); 2184 if (UsesLHS && UsesRHS) 2185 return false; 2186 } 2187 // Allow for degenerate case: completely undef mask means neither source is used. 2188 return UsesLHS || UsesRHS; 2189 } 2190 2191 bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { 2192 // We don't have vector operand size information, so assume operands are the 2193 // same size as the mask. 2194 return isSingleSourceMaskImpl(Mask, Mask.size()); 2195 } 2196 2197 static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 2198 if (!isSingleSourceMaskImpl(Mask, NumOpElts)) 2199 return false; 2200 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { 2201 if (Mask[i] == -1) 2202 continue; 2203 if (Mask[i] != i && Mask[i] != (NumOpElts + i)) 2204 return false; 2205 } 2206 return true; 2207 } 2208 2209 bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { 2210 // We don't have vector operand size information, so assume operands are the 2211 // same size as the mask. 2212 return isIdentityMaskImpl(Mask, Mask.size()); 2213 } 2214 2215 bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { 2216 if (!isSingleSourceMask(Mask)) 2217 return false; 2218 2219 // The number of elements in the mask must be at least 2. 2220 int NumElts = Mask.size(); 2221 if (NumElts < 2) 2222 return false; 2223 2224 for (int i = 0; i < NumElts; ++i) { 2225 if (Mask[i] == -1) 2226 continue; 2227 if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) 2228 return false; 2229 } 2230 return true; 2231 } 2232 2233 bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { 2234 if (!isSingleSourceMask(Mask)) 2235 return false; 2236 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { 2237 if (Mask[i] == -1) 2238 continue; 2239 if (Mask[i] != 0 && Mask[i] != NumElts) 2240 return false; 2241 } 2242 return true; 2243 } 2244 2245 bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { 2246 // Select is differentiated from identity. It requires using both sources. 2247 if (isSingleSourceMask(Mask)) 2248 return false; 2249 for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { 2250 if (Mask[i] == -1) 2251 continue; 2252 if (Mask[i] != i && Mask[i] != (NumElts + i)) 2253 return false; 2254 } 2255 return true; 2256 } 2257 2258 bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { 2259 // Example masks that will return true: 2260 // v1 = <a, b, c, d> 2261 // v2 = <e, f, g, h> 2262 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> 2263 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> 2264 2265 // 1. The number of elements in the mask must be a power-of-2 and at least 2. 2266 int NumElts = Mask.size(); 2267 if (NumElts < 2 || !isPowerOf2_32(NumElts)) 2268 return false; 2269 2270 // 2. The first element of the mask must be either a 0 or a 1. 2271 if (Mask[0] != 0 && Mask[0] != 1) 2272 return false; 2273 2274 // 3. The difference between the first 2 elements must be equal to the 2275 // number of elements in the mask. 2276 if ((Mask[1] - Mask[0]) != NumElts) 2277 return false; 2278 2279 // 4. The difference between consecutive even-numbered and odd-numbered 2280 // elements must be equal to 2. 2281 for (int i = 2; i < NumElts; ++i) { 2282 int MaskEltVal = Mask[i]; 2283 if (MaskEltVal == -1) 2284 return false; 2285 int MaskEltPrevVal = Mask[i - 2]; 2286 if (MaskEltVal - MaskEltPrevVal != 2) 2287 return false; 2288 } 2289 return true; 2290 } 2291 2292 bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, 2293 int NumSrcElts, int &Index) { 2294 // Must extract from a single source. 2295 if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) 2296 return false; 2297 2298 // Must be smaller (else this is an Identity shuffle). 2299 if (NumSrcElts <= (int)Mask.size()) 2300 return false; 2301 2302 // Find start of extraction, accounting that we may start with an UNDEF. 2303 int SubIndex = -1; 2304 for (int i = 0, e = Mask.size(); i != e; ++i) { 2305 int M = Mask[i]; 2306 if (M < 0) 2307 continue; 2308 int Offset = (M % NumSrcElts) - i; 2309 if (0 <= SubIndex && SubIndex != Offset) 2310 return false; 2311 SubIndex = Offset; 2312 } 2313 2314 if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) { 2315 Index = SubIndex; 2316 return true; 2317 } 2318 return false; 2319 } 2320 2321 bool ShuffleVectorInst::isInsertSubvectorMask(ArrayRef<int> Mask, 2322 int NumSrcElts, int &NumSubElts, 2323 int &Index) { 2324 int NumMaskElts = Mask.size(); 2325 2326 // Don't try to match if we're shuffling to a smaller size. 2327 if (NumMaskElts < NumSrcElts) 2328 return false; 2329 2330 // TODO: We don't recognize self-insertion/widening. 2331 if (isSingleSourceMaskImpl(Mask, NumSrcElts)) 2332 return false; 2333 2334 // Determine which mask elements are attributed to which source. 2335 APInt UndefElts = APInt::getZero(NumMaskElts); 2336 APInt Src0Elts = APInt::getZero(NumMaskElts); 2337 APInt Src1Elts = APInt::getZero(NumMaskElts); 2338 bool Src0Identity = true; 2339 bool Src1Identity = true; 2340 2341 for (int i = 0; i != NumMaskElts; ++i) { 2342 int M = Mask[i]; 2343 if (M < 0) { 2344 UndefElts.setBit(i); 2345 continue; 2346 } 2347 if (M < NumSrcElts) { 2348 Src0Elts.setBit(i); 2349 Src0Identity &= (M == i); 2350 continue; 2351 } 2352 Src1Elts.setBit(i); 2353 Src1Identity &= (M == (i + NumSrcElts)); 2354 } 2355 assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() && 2356 "unknown shuffle elements"); 2357 assert(!Src0Elts.isZero() && !Src1Elts.isZero() && 2358 "2-source shuffle not found"); 2359 2360 // Determine lo/hi span ranges. 2361 // TODO: How should we handle undefs at the start of subvector insertions? 2362 int Src0Lo = Src0Elts.countTrailingZeros(); 2363 int Src1Lo = Src1Elts.countTrailingZeros(); 2364 int Src0Hi = NumMaskElts - Src0Elts.countLeadingZeros(); 2365 int Src1Hi = NumMaskElts - Src1Elts.countLeadingZeros(); 2366 2367 // If src0 is in place, see if the src1 elements is inplace within its own 2368 // span. 2369 if (Src0Identity) { 2370 int NumSub1Elts = Src1Hi - Src1Lo; 2371 ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts); 2372 if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) { 2373 NumSubElts = NumSub1Elts; 2374 Index = Src1Lo; 2375 return true; 2376 } 2377 } 2378 2379 // If src1 is in place, see if the src0 elements is inplace within its own 2380 // span. 2381 if (Src1Identity) { 2382 int NumSub0Elts = Src0Hi - Src0Lo; 2383 ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts); 2384 if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) { 2385 NumSubElts = NumSub0Elts; 2386 Index = Src0Lo; 2387 return true; 2388 } 2389 } 2390 2391 return false; 2392 } 2393 2394 bool ShuffleVectorInst::isIdentityWithPadding() const { 2395 if (isa<UndefValue>(Op<2>())) 2396 return false; 2397 2398 // FIXME: Not currently possible to express a shuffle mask for a scalable 2399 // vector for this case. 2400 if (isa<ScalableVectorType>(getType())) 2401 return false; 2402 2403 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2404 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2405 if (NumMaskElts <= NumOpElts) 2406 return false; 2407 2408 // The first part of the mask must choose elements from exactly 1 source op. 2409 ArrayRef<int> Mask = getShuffleMask(); 2410 if (!isIdentityMaskImpl(Mask, NumOpElts)) 2411 return false; 2412 2413 // All extending must be with undef elements. 2414 for (int i = NumOpElts; i < NumMaskElts; ++i) 2415 if (Mask[i] != -1) 2416 return false; 2417 2418 return true; 2419 } 2420 2421 bool ShuffleVectorInst::isIdentityWithExtract() const { 2422 if (isa<UndefValue>(Op<2>())) 2423 return false; 2424 2425 // FIXME: Not currently possible to express a shuffle mask for a scalable 2426 // vector for this case. 2427 if (isa<ScalableVectorType>(getType())) 2428 return false; 2429 2430 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2431 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2432 if (NumMaskElts >= NumOpElts) 2433 return false; 2434 2435 return isIdentityMaskImpl(getShuffleMask(), NumOpElts); 2436 } 2437 2438 bool ShuffleVectorInst::isConcat() const { 2439 // Vector concatenation is differentiated from identity with padding. 2440 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) || 2441 isa<UndefValue>(Op<2>())) 2442 return false; 2443 2444 // FIXME: Not currently possible to express a shuffle mask for a scalable 2445 // vector for this case. 2446 if (isa<ScalableVectorType>(getType())) 2447 return false; 2448 2449 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2450 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2451 if (NumMaskElts != NumOpElts * 2) 2452 return false; 2453 2454 // Use the mask length rather than the operands' vector lengths here. We 2455 // already know that the shuffle returns a vector twice as long as the inputs, 2456 // and neither of the inputs are undef vectors. If the mask picks consecutive 2457 // elements from both inputs, then this is a concatenation of the inputs. 2458 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); 2459 } 2460 2461 static bool isReplicationMaskWithParams(ArrayRef<int> Mask, 2462 int ReplicationFactor, int VF) { 2463 assert(Mask.size() == (unsigned)ReplicationFactor * VF && 2464 "Unexpected mask size."); 2465 2466 for (int CurrElt : seq(0, VF)) { 2467 ArrayRef<int> CurrSubMask = Mask.take_front(ReplicationFactor); 2468 assert(CurrSubMask.size() == (unsigned)ReplicationFactor && 2469 "Run out of mask?"); 2470 Mask = Mask.drop_front(ReplicationFactor); 2471 if (!all_of(CurrSubMask, [CurrElt](int MaskElt) { 2472 return MaskElt == UndefMaskElem || MaskElt == CurrElt; 2473 })) 2474 return false; 2475 } 2476 assert(Mask.empty() && "Did not consume the whole mask?"); 2477 2478 return true; 2479 } 2480 2481 bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask, 2482 int &ReplicationFactor, int &VF) { 2483 // undef-less case is trivial. 2484 if (none_of(Mask, [](int MaskElt) { return MaskElt == UndefMaskElem; })) { 2485 ReplicationFactor = 2486 Mask.take_while([](int MaskElt) { return MaskElt == 0; }).size(); 2487 if (ReplicationFactor == 0 || Mask.size() % ReplicationFactor != 0) 2488 return false; 2489 VF = Mask.size() / ReplicationFactor; 2490 return isReplicationMaskWithParams(Mask, ReplicationFactor, VF); 2491 } 2492 2493 // However, if the mask contains undef's, we have to enumerate possible tuples 2494 // and pick one. There are bounds on replication factor: [1, mask size] 2495 // (where RF=1 is an identity shuffle, RF=mask size is a broadcast shuffle) 2496 // Additionally, mask size is a replication factor multiplied by vector size, 2497 // which further significantly reduces the search space. 2498 2499 // Before doing that, let's perform basic correctness checking first. 2500 int Largest = -1; 2501 for (int MaskElt : Mask) { 2502 if (MaskElt == UndefMaskElem) 2503 continue; 2504 // Elements must be in non-decreasing order. 2505 if (MaskElt < Largest) 2506 return false; 2507 Largest = std::max(Largest, MaskElt); 2508 } 2509 2510 // Prefer larger replication factor if all else equal. 2511 for (int PossibleReplicationFactor : 2512 reverse(seq_inclusive<unsigned>(1, Mask.size()))) { 2513 if (Mask.size() % PossibleReplicationFactor != 0) 2514 continue; 2515 int PossibleVF = Mask.size() / PossibleReplicationFactor; 2516 if (!isReplicationMaskWithParams(Mask, PossibleReplicationFactor, 2517 PossibleVF)) 2518 continue; 2519 ReplicationFactor = PossibleReplicationFactor; 2520 VF = PossibleVF; 2521 return true; 2522 } 2523 2524 return false; 2525 } 2526 2527 bool ShuffleVectorInst::isReplicationMask(int &ReplicationFactor, 2528 int &VF) const { 2529 // Not possible to express a shuffle mask for a scalable vector for this 2530 // case. 2531 if (isa<ScalableVectorType>(getType())) 2532 return false; 2533 2534 VF = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2535 if (ShuffleMask.size() % VF != 0) 2536 return false; 2537 ReplicationFactor = ShuffleMask.size() / VF; 2538 2539 return isReplicationMaskWithParams(ShuffleMask, ReplicationFactor, VF); 2540 } 2541 2542 //===----------------------------------------------------------------------===// 2543 // InsertValueInst Class 2544 //===----------------------------------------------------------------------===// 2545 2546 void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 2547 const Twine &Name) { 2548 assert(getNumOperands() == 2 && "NumOperands not initialized?"); 2549 2550 // There's no fundamental reason why we require at least one index 2551 // (other than weirdness with &*IdxBegin being invalid; see 2552 // getelementptr's init routine for example). But there's no 2553 // present need to support it. 2554 assert(!Idxs.empty() && "InsertValueInst must have at least one index"); 2555 2556 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == 2557 Val->getType() && "Inserted value must match indexed type!"); 2558 Op<0>() = Agg; 2559 Op<1>() = Val; 2560 2561 Indices.append(Idxs.begin(), Idxs.end()); 2562 setName(Name); 2563 } 2564 2565 InsertValueInst::InsertValueInst(const InsertValueInst &IVI) 2566 : Instruction(IVI.getType(), InsertValue, 2567 OperandTraits<InsertValueInst>::op_begin(this), 2), 2568 Indices(IVI.Indices) { 2569 Op<0>() = IVI.getOperand(0); 2570 Op<1>() = IVI.getOperand(1); 2571 SubclassOptionalData = IVI.SubclassOptionalData; 2572 } 2573 2574 //===----------------------------------------------------------------------===// 2575 // ExtractValueInst Class 2576 //===----------------------------------------------------------------------===// 2577 2578 void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { 2579 assert(getNumOperands() == 1 && "NumOperands not initialized?"); 2580 2581 // There's no fundamental reason why we require at least one index. 2582 // But there's no present need to support it. 2583 assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); 2584 2585 Indices.append(Idxs.begin(), Idxs.end()); 2586 setName(Name); 2587 } 2588 2589 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) 2590 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), 2591 Indices(EVI.Indices) { 2592 SubclassOptionalData = EVI.SubclassOptionalData; 2593 } 2594 2595 // getIndexedType - Returns the type of the element that would be extracted 2596 // with an extractvalue instruction with the specified parameters. 2597 // 2598 // A null type is returned if the indices are invalid for the specified 2599 // pointer type. 2600 // 2601 Type *ExtractValueInst::getIndexedType(Type *Agg, 2602 ArrayRef<unsigned> Idxs) { 2603 for (unsigned Index : Idxs) { 2604 // We can't use CompositeType::indexValid(Index) here. 2605 // indexValid() always returns true for arrays because getelementptr allows 2606 // out-of-bounds indices. Since we don't allow those for extractvalue and 2607 // insertvalue we need to check array indexing manually. 2608 // Since the only other types we can index into are struct types it's just 2609 // as easy to check those manually as well. 2610 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { 2611 if (Index >= AT->getNumElements()) 2612 return nullptr; 2613 Agg = AT->getElementType(); 2614 } else if (StructType *ST = dyn_cast<StructType>(Agg)) { 2615 if (Index >= ST->getNumElements()) 2616 return nullptr; 2617 Agg = ST->getElementType(Index); 2618 } else { 2619 // Not a valid type to index into. 2620 return nullptr; 2621 } 2622 } 2623 return const_cast<Type*>(Agg); 2624 } 2625 2626 //===----------------------------------------------------------------------===// 2627 // UnaryOperator Class 2628 //===----------------------------------------------------------------------===// 2629 2630 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, 2631 Type *Ty, const Twine &Name, 2632 Instruction *InsertBefore) 2633 : UnaryInstruction(Ty, iType, S, InsertBefore) { 2634 Op<0>() = S; 2635 setName(Name); 2636 AssertOK(); 2637 } 2638 2639 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, 2640 Type *Ty, const Twine &Name, 2641 BasicBlock *InsertAtEnd) 2642 : UnaryInstruction(Ty, iType, S, InsertAtEnd) { 2643 Op<0>() = S; 2644 setName(Name); 2645 AssertOK(); 2646 } 2647 2648 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, 2649 const Twine &Name, 2650 Instruction *InsertBefore) { 2651 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); 2652 } 2653 2654 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, 2655 const Twine &Name, 2656 BasicBlock *InsertAtEnd) { 2657 UnaryOperator *Res = Create(Op, S, Name); 2658 InsertAtEnd->getInstList().push_back(Res); 2659 return Res; 2660 } 2661 2662 void UnaryOperator::AssertOK() { 2663 Value *LHS = getOperand(0); 2664 (void)LHS; // Silence warnings. 2665 #ifndef NDEBUG 2666 switch (getOpcode()) { 2667 case FNeg: 2668 assert(getType() == LHS->getType() && 2669 "Unary operation should return same type as operand!"); 2670 assert(getType()->isFPOrFPVectorTy() && 2671 "Tried to create a floating-point operation on a " 2672 "non-floating-point type!"); 2673 break; 2674 default: llvm_unreachable("Invalid opcode provided"); 2675 } 2676 #endif 2677 } 2678 2679 //===----------------------------------------------------------------------===// 2680 // BinaryOperator Class 2681 //===----------------------------------------------------------------------===// 2682 2683 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 2684 Type *Ty, const Twine &Name, 2685 Instruction *InsertBefore) 2686 : Instruction(Ty, iType, 2687 OperandTraits<BinaryOperator>::op_begin(this), 2688 OperandTraits<BinaryOperator>::operands(this), 2689 InsertBefore) { 2690 Op<0>() = S1; 2691 Op<1>() = S2; 2692 setName(Name); 2693 AssertOK(); 2694 } 2695 2696 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 2697 Type *Ty, const Twine &Name, 2698 BasicBlock *InsertAtEnd) 2699 : Instruction(Ty, iType, 2700 OperandTraits<BinaryOperator>::op_begin(this), 2701 OperandTraits<BinaryOperator>::operands(this), 2702 InsertAtEnd) { 2703 Op<0>() = S1; 2704 Op<1>() = S2; 2705 setName(Name); 2706 AssertOK(); 2707 } 2708 2709 void BinaryOperator::AssertOK() { 2710 Value *LHS = getOperand(0), *RHS = getOperand(1); 2711 (void)LHS; (void)RHS; // Silence warnings. 2712 assert(LHS->getType() == RHS->getType() && 2713 "Binary operator operand types must match!"); 2714 #ifndef NDEBUG 2715 switch (getOpcode()) { 2716 case Add: case Sub: 2717 case Mul: 2718 assert(getType() == LHS->getType() && 2719 "Arithmetic operation should return same type as operands!"); 2720 assert(getType()->isIntOrIntVectorTy() && 2721 "Tried to create an integer operation on a non-integer type!"); 2722 break; 2723 case FAdd: case FSub: 2724 case FMul: 2725 assert(getType() == LHS->getType() && 2726 "Arithmetic operation should return same type as operands!"); 2727 assert(getType()->isFPOrFPVectorTy() && 2728 "Tried to create a floating-point operation on a " 2729 "non-floating-point type!"); 2730 break; 2731 case UDiv: 2732 case SDiv: 2733 assert(getType() == LHS->getType() && 2734 "Arithmetic operation should return same type as operands!"); 2735 assert(getType()->isIntOrIntVectorTy() && 2736 "Incorrect operand type (not integer) for S/UDIV"); 2737 break; 2738 case FDiv: 2739 assert(getType() == LHS->getType() && 2740 "Arithmetic operation should return same type as operands!"); 2741 assert(getType()->isFPOrFPVectorTy() && 2742 "Incorrect operand type (not floating point) for FDIV"); 2743 break; 2744 case URem: 2745 case SRem: 2746 assert(getType() == LHS->getType() && 2747 "Arithmetic operation should return same type as operands!"); 2748 assert(getType()->isIntOrIntVectorTy() && 2749 "Incorrect operand type (not integer) for S/UREM"); 2750 break; 2751 case FRem: 2752 assert(getType() == LHS->getType() && 2753 "Arithmetic operation should return same type as operands!"); 2754 assert(getType()->isFPOrFPVectorTy() && 2755 "Incorrect operand type (not floating point) for FREM"); 2756 break; 2757 case Shl: 2758 case LShr: 2759 case AShr: 2760 assert(getType() == LHS->getType() && 2761 "Shift operation should return same type as operands!"); 2762 assert(getType()->isIntOrIntVectorTy() && 2763 "Tried to create a shift operation on a non-integral type!"); 2764 break; 2765 case And: case Or: 2766 case Xor: 2767 assert(getType() == LHS->getType() && 2768 "Logical operation should return same type as operands!"); 2769 assert(getType()->isIntOrIntVectorTy() && 2770 "Tried to create a logical operation on a non-integral type!"); 2771 break; 2772 default: llvm_unreachable("Invalid opcode provided"); 2773 } 2774 #endif 2775 } 2776 2777 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 2778 const Twine &Name, 2779 Instruction *InsertBefore) { 2780 assert(S1->getType() == S2->getType() && 2781 "Cannot create binary operator with two operands of differing type!"); 2782 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); 2783 } 2784 2785 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 2786 const Twine &Name, 2787 BasicBlock *InsertAtEnd) { 2788 BinaryOperator *Res = Create(Op, S1, S2, Name); 2789 InsertAtEnd->getInstList().push_back(Res); 2790 return Res; 2791 } 2792 2793 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 2794 Instruction *InsertBefore) { 2795 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2796 return new BinaryOperator(Instruction::Sub, 2797 zero, Op, 2798 Op->getType(), Name, InsertBefore); 2799 } 2800 2801 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 2802 BasicBlock *InsertAtEnd) { 2803 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2804 return new BinaryOperator(Instruction::Sub, 2805 zero, Op, 2806 Op->getType(), Name, InsertAtEnd); 2807 } 2808 2809 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 2810 Instruction *InsertBefore) { 2811 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2812 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); 2813 } 2814 2815 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 2816 BasicBlock *InsertAtEnd) { 2817 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2818 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); 2819 } 2820 2821 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 2822 Instruction *InsertBefore) { 2823 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2824 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); 2825 } 2826 2827 BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 2828 BasicBlock *InsertAtEnd) { 2829 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 2830 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); 2831 } 2832 2833 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 2834 Instruction *InsertBefore) { 2835 Constant *C = Constant::getAllOnesValue(Op->getType()); 2836 return new BinaryOperator(Instruction::Xor, Op, C, 2837 Op->getType(), Name, InsertBefore); 2838 } 2839 2840 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 2841 BasicBlock *InsertAtEnd) { 2842 Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); 2843 return new BinaryOperator(Instruction::Xor, Op, AllOnes, 2844 Op->getType(), Name, InsertAtEnd); 2845 } 2846 2847 // Exchange the two operands to this instruction. This instruction is safe to 2848 // use on any binary instruction and does not modify the semantics of the 2849 // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode 2850 // is changed. 2851 bool BinaryOperator::swapOperands() { 2852 if (!isCommutative()) 2853 return true; // Can't commute operands 2854 Op<0>().swap(Op<1>()); 2855 return false; 2856 } 2857 2858 //===----------------------------------------------------------------------===// 2859 // FPMathOperator Class 2860 //===----------------------------------------------------------------------===// 2861 2862 float FPMathOperator::getFPAccuracy() const { 2863 const MDNode *MD = 2864 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); 2865 if (!MD) 2866 return 0.0; 2867 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); 2868 return Accuracy->getValueAPF().convertToFloat(); 2869 } 2870 2871 //===----------------------------------------------------------------------===// 2872 // CastInst Class 2873 //===----------------------------------------------------------------------===// 2874 2875 // Just determine if this cast only deals with integral->integral conversion. 2876 bool CastInst::isIntegerCast() const { 2877 switch (getOpcode()) { 2878 default: return false; 2879 case Instruction::ZExt: 2880 case Instruction::SExt: 2881 case Instruction::Trunc: 2882 return true; 2883 case Instruction::BitCast: 2884 return getOperand(0)->getType()->isIntegerTy() && 2885 getType()->isIntegerTy(); 2886 } 2887 } 2888 2889 bool CastInst::isLosslessCast() const { 2890 // Only BitCast can be lossless, exit fast if we're not BitCast 2891 if (getOpcode() != Instruction::BitCast) 2892 return false; 2893 2894 // Identity cast is always lossless 2895 Type *SrcTy = getOperand(0)->getType(); 2896 Type *DstTy = getType(); 2897 if (SrcTy == DstTy) 2898 return true; 2899 2900 // Pointer to pointer is always lossless. 2901 if (SrcTy->isPointerTy()) 2902 return DstTy->isPointerTy(); 2903 return false; // Other types have no identity values 2904 } 2905 2906 /// This function determines if the CastInst does not require any bits to be 2907 /// changed in order to effect the cast. Essentially, it identifies cases where 2908 /// no code gen is necessary for the cast, hence the name no-op cast. For 2909 /// example, the following are all no-op casts: 2910 /// # bitcast i32* %x to i8* 2911 /// # bitcast <2 x i32> %x to <4 x i16> 2912 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only 2913 /// Determine if the described cast is a no-op. 2914 bool CastInst::isNoopCast(Instruction::CastOps Opcode, 2915 Type *SrcTy, 2916 Type *DestTy, 2917 const DataLayout &DL) { 2918 assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition"); 2919 switch (Opcode) { 2920 default: llvm_unreachable("Invalid CastOp"); 2921 case Instruction::Trunc: 2922 case Instruction::ZExt: 2923 case Instruction::SExt: 2924 case Instruction::FPTrunc: 2925 case Instruction::FPExt: 2926 case Instruction::UIToFP: 2927 case Instruction::SIToFP: 2928 case Instruction::FPToUI: 2929 case Instruction::FPToSI: 2930 case Instruction::AddrSpaceCast: 2931 // TODO: Target informations may give a more accurate answer here. 2932 return false; 2933 case Instruction::BitCast: 2934 return true; // BitCast never modifies bits. 2935 case Instruction::PtrToInt: 2936 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == 2937 DestTy->getScalarSizeInBits(); 2938 case Instruction::IntToPtr: 2939 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == 2940 SrcTy->getScalarSizeInBits(); 2941 } 2942 } 2943 2944 bool CastInst::isNoopCast(const DataLayout &DL) const { 2945 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); 2946 } 2947 2948 /// This function determines if a pair of casts can be eliminated and what 2949 /// opcode should be used in the elimination. This assumes that there are two 2950 /// instructions like this: 2951 /// * %F = firstOpcode SrcTy %x to MidTy 2952 /// * %S = secondOpcode MidTy %F to DstTy 2953 /// The function returns a resultOpcode so these two casts can be replaced with: 2954 /// * %Replacement = resultOpcode %SrcTy %x to DstTy 2955 /// If no such cast is permitted, the function returns 0. 2956 unsigned CastInst::isEliminableCastPair( 2957 Instruction::CastOps firstOp, Instruction::CastOps secondOp, 2958 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, 2959 Type *DstIntPtrTy) { 2960 // Define the 144 possibilities for these two cast instructions. The values 2961 // in this matrix determine what to do in a given situation and select the 2962 // case in the switch below. The rows correspond to firstOp, the columns 2963 // correspond to secondOp. In looking at the table below, keep in mind 2964 // the following cast properties: 2965 // 2966 // Size Compare Source Destination 2967 // Operator Src ? Size Type Sign Type Sign 2968 // -------- ------------ ------------------- --------------------- 2969 // TRUNC > Integer Any Integral Any 2970 // ZEXT < Integral Unsigned Integer Any 2971 // SEXT < Integral Signed Integer Any 2972 // FPTOUI n/a FloatPt n/a Integral Unsigned 2973 // FPTOSI n/a FloatPt n/a Integral Signed 2974 // UITOFP n/a Integral Unsigned FloatPt n/a 2975 // SITOFP n/a Integral Signed FloatPt n/a 2976 // FPTRUNC > FloatPt n/a FloatPt n/a 2977 // FPEXT < FloatPt n/a FloatPt n/a 2978 // PTRTOINT n/a Pointer n/a Integral Unsigned 2979 // INTTOPTR n/a Integral Unsigned Pointer n/a 2980 // BITCAST = FirstClass n/a FirstClass n/a 2981 // ADDRSPCST n/a Pointer n/a Pointer n/a 2982 // 2983 // NOTE: some transforms are safe, but we consider them to be non-profitable. 2984 // For example, we could merge "fptoui double to i32" + "zext i32 to i64", 2985 // into "fptoui double to i64", but this loses information about the range 2986 // of the produced value (we no longer know the top-part is all zeros). 2987 // Further this conversion is often much more expensive for typical hardware, 2988 // and causes issues when building libgcc. We disallow fptosi+sext for the 2989 // same reason. 2990 const unsigned numCastOps = 2991 Instruction::CastOpsEnd - Instruction::CastOpsBegin; 2992 static const uint8_t CastResults[numCastOps][numCastOps] = { 2993 // T F F U S F F P I B A -+ 2994 // R Z S P P I I T P 2 N T S | 2995 // U E E 2 2 2 2 R E I T C C +- secondOp 2996 // N X X U S F F N X N 2 V V | 2997 // C T T I I P P C T T P T T -+ 2998 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ 2999 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | 3000 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | 3001 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | 3002 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | 3003 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp 3004 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | 3005 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | 3006 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | 3007 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | 3008 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | 3009 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | 3010 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ 3011 }; 3012 3013 // TODO: This logic could be encoded into the table above and handled in the 3014 // switch below. 3015 // If either of the casts are a bitcast from scalar to vector, disallow the 3016 // merging. However, any pair of bitcasts are allowed. 3017 bool IsFirstBitcast = (firstOp == Instruction::BitCast); 3018 bool IsSecondBitcast = (secondOp == Instruction::BitCast); 3019 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; 3020 3021 // Check if any of the casts convert scalars <-> vectors. 3022 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || 3023 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) 3024 if (!AreBothBitcasts) 3025 return 0; 3026 3027 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] 3028 [secondOp-Instruction::CastOpsBegin]; 3029 switch (ElimCase) { 3030 case 0: 3031 // Categorically disallowed. 3032 return 0; 3033 case 1: 3034 // Allowed, use first cast's opcode. 3035 return firstOp; 3036 case 2: 3037 // Allowed, use second cast's opcode. 3038 return secondOp; 3039 case 3: 3040 // No-op cast in second op implies firstOp as long as the DestTy 3041 // is integer and we are not converting between a vector and a 3042 // non-vector type. 3043 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) 3044 return firstOp; 3045 return 0; 3046 case 4: 3047 // No-op cast in second op implies firstOp as long as the DestTy 3048 // is floating point. 3049 if (DstTy->isFloatingPointTy()) 3050 return firstOp; 3051 return 0; 3052 case 5: 3053 // No-op cast in first op implies secondOp as long as the SrcTy 3054 // is an integer. 3055 if (SrcTy->isIntegerTy()) 3056 return secondOp; 3057 return 0; 3058 case 6: 3059 // No-op cast in first op implies secondOp as long as the SrcTy 3060 // is a floating point. 3061 if (SrcTy->isFloatingPointTy()) 3062 return secondOp; 3063 return 0; 3064 case 7: { 3065 // Disable inttoptr/ptrtoint optimization if enabled. 3066 if (DisableI2pP2iOpt) 3067 return 0; 3068 3069 // Cannot simplify if address spaces are different! 3070 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 3071 return 0; 3072 3073 unsigned MidSize = MidTy->getScalarSizeInBits(); 3074 // We can still fold this without knowing the actual sizes as long we 3075 // know that the intermediate pointer is the largest possible 3076 // pointer size. 3077 // FIXME: Is this always true? 3078 if (MidSize == 64) 3079 return Instruction::BitCast; 3080 3081 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. 3082 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) 3083 return 0; 3084 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); 3085 if (MidSize >= PtrSize) 3086 return Instruction::BitCast; 3087 return 0; 3088 } 3089 case 8: { 3090 // ext, trunc -> bitcast, if the SrcTy and DstTy are the same 3091 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) 3092 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) 3093 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 3094 unsigned DstSize = DstTy->getScalarSizeInBits(); 3095 if (SrcTy == DstTy) 3096 return Instruction::BitCast; 3097 if (SrcSize < DstSize) 3098 return firstOp; 3099 if (SrcSize > DstSize) 3100 return secondOp; 3101 return 0; 3102 } 3103 case 9: 3104 // zext, sext -> zext, because sext can't sign extend after zext 3105 return Instruction::ZExt; 3106 case 11: { 3107 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize 3108 if (!MidIntPtrTy) 3109 return 0; 3110 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); 3111 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 3112 unsigned DstSize = DstTy->getScalarSizeInBits(); 3113 if (SrcSize <= PtrSize && SrcSize == DstSize) 3114 return Instruction::BitCast; 3115 return 0; 3116 } 3117 case 12: 3118 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS 3119 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS 3120 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 3121 return Instruction::AddrSpaceCast; 3122 return Instruction::BitCast; 3123 case 13: 3124 // FIXME: this state can be merged with (1), but the following assert 3125 // is useful to check the correcteness of the sequence due to semantic 3126 // change of bitcast. 3127 assert( 3128 SrcTy->isPtrOrPtrVectorTy() && 3129 MidTy->isPtrOrPtrVectorTy() && 3130 DstTy->isPtrOrPtrVectorTy() && 3131 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && 3132 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 3133 "Illegal addrspacecast, bitcast sequence!"); 3134 // Allowed, use first cast's opcode 3135 return firstOp; 3136 case 14: { 3137 // bitcast, addrspacecast -> addrspacecast if the element type of 3138 // bitcast's source is the same as that of addrspacecast's destination. 3139 PointerType *SrcPtrTy = cast<PointerType>(SrcTy->getScalarType()); 3140 PointerType *DstPtrTy = cast<PointerType>(DstTy->getScalarType()); 3141 if (SrcPtrTy->hasSameElementTypeAs(DstPtrTy)) 3142 return Instruction::AddrSpaceCast; 3143 return 0; 3144 } 3145 case 15: 3146 // FIXME: this state can be merged with (1), but the following assert 3147 // is useful to check the correcteness of the sequence due to semantic 3148 // change of bitcast. 3149 assert( 3150 SrcTy->isIntOrIntVectorTy() && 3151 MidTy->isPtrOrPtrVectorTy() && 3152 DstTy->isPtrOrPtrVectorTy() && 3153 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 3154 "Illegal inttoptr, bitcast sequence!"); 3155 // Allowed, use first cast's opcode 3156 return firstOp; 3157 case 16: 3158 // FIXME: this state can be merged with (2), but the following assert 3159 // is useful to check the correcteness of the sequence due to semantic 3160 // change of bitcast. 3161 assert( 3162 SrcTy->isPtrOrPtrVectorTy() && 3163 MidTy->isPtrOrPtrVectorTy() && 3164 DstTy->isIntOrIntVectorTy() && 3165 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && 3166 "Illegal bitcast, ptrtoint sequence!"); 3167 // Allowed, use second cast's opcode 3168 return secondOp; 3169 case 17: 3170 // (sitofp (zext x)) -> (uitofp x) 3171 return Instruction::UIToFP; 3172 case 99: 3173 // Cast combination can't happen (error in input). This is for all cases 3174 // where the MidTy is not the same for the two cast instructions. 3175 llvm_unreachable("Invalid Cast Combination"); 3176 default: 3177 llvm_unreachable("Error in CastResults table!!!"); 3178 } 3179 } 3180 3181 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 3182 const Twine &Name, Instruction *InsertBefore) { 3183 assert(castIsValid(op, S, Ty) && "Invalid cast!"); 3184 // Construct and return the appropriate CastInst subclass 3185 switch (op) { 3186 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); 3187 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); 3188 case SExt: return new SExtInst (S, Ty, Name, InsertBefore); 3189 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); 3190 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); 3191 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); 3192 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); 3193 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); 3194 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); 3195 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); 3196 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); 3197 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); 3198 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); 3199 default: llvm_unreachable("Invalid opcode provided"); 3200 } 3201 } 3202 3203 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 3204 const Twine &Name, BasicBlock *InsertAtEnd) { 3205 assert(castIsValid(op, S, Ty) && "Invalid cast!"); 3206 // Construct and return the appropriate CastInst subclass 3207 switch (op) { 3208 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); 3209 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); 3210 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); 3211 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); 3212 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); 3213 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); 3214 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); 3215 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); 3216 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); 3217 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); 3218 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); 3219 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); 3220 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); 3221 default: llvm_unreachable("Invalid opcode provided"); 3222 } 3223 } 3224 3225 CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 3226 const Twine &Name, 3227 Instruction *InsertBefore) { 3228 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3229 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3230 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); 3231 } 3232 3233 CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 3234 const Twine &Name, 3235 BasicBlock *InsertAtEnd) { 3236 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3237 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3238 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); 3239 } 3240 3241 CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 3242 const Twine &Name, 3243 Instruction *InsertBefore) { 3244 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3245 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3246 return Create(Instruction::SExt, S, Ty, Name, InsertBefore); 3247 } 3248 3249 CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 3250 const Twine &Name, 3251 BasicBlock *InsertAtEnd) { 3252 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3253 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3254 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); 3255 } 3256 3257 CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 3258 const Twine &Name, 3259 Instruction *InsertBefore) { 3260 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3261 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3262 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); 3263 } 3264 3265 CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 3266 const Twine &Name, 3267 BasicBlock *InsertAtEnd) { 3268 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3269 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3270 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); 3271 } 3272 3273 CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 3274 const Twine &Name, 3275 BasicBlock *InsertAtEnd) { 3276 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3277 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 3278 "Invalid cast"); 3279 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 3280 assert((!Ty->isVectorTy() || 3281 cast<VectorType>(Ty)->getElementCount() == 3282 cast<VectorType>(S->getType())->getElementCount()) && 3283 "Invalid cast"); 3284 3285 if (Ty->isIntOrIntVectorTy()) 3286 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); 3287 3288 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); 3289 } 3290 3291 /// Create a BitCast or a PtrToInt cast instruction 3292 CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 3293 const Twine &Name, 3294 Instruction *InsertBefore) { 3295 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3296 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 3297 "Invalid cast"); 3298 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 3299 assert((!Ty->isVectorTy() || 3300 cast<VectorType>(Ty)->getElementCount() == 3301 cast<VectorType>(S->getType())->getElementCount()) && 3302 "Invalid cast"); 3303 3304 if (Ty->isIntOrIntVectorTy()) 3305 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3306 3307 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); 3308 } 3309 3310 CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( 3311 Value *S, Type *Ty, 3312 const Twine &Name, 3313 BasicBlock *InsertAtEnd) { 3314 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3315 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); 3316 3317 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 3318 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); 3319 3320 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 3321 } 3322 3323 CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( 3324 Value *S, Type *Ty, 3325 const Twine &Name, 3326 Instruction *InsertBefore) { 3327 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3328 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); 3329 3330 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 3331 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); 3332 3333 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3334 } 3335 3336 CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, 3337 const Twine &Name, 3338 Instruction *InsertBefore) { 3339 if (S->getType()->isPointerTy() && Ty->isIntegerTy()) 3340 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3341 if (S->getType()->isIntegerTy() && Ty->isPointerTy()) 3342 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); 3343 3344 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3345 } 3346 3347 CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 3348 bool isSigned, const Twine &Name, 3349 Instruction *InsertBefore) { 3350 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 3351 "Invalid integer cast"); 3352 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3353 unsigned DstBits = Ty->getScalarSizeInBits(); 3354 Instruction::CastOps opcode = 3355 (SrcBits == DstBits ? Instruction::BitCast : 3356 (SrcBits > DstBits ? Instruction::Trunc : 3357 (isSigned ? Instruction::SExt : Instruction::ZExt))); 3358 return Create(opcode, C, Ty, Name, InsertBefore); 3359 } 3360 3361 CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 3362 bool isSigned, const Twine &Name, 3363 BasicBlock *InsertAtEnd) { 3364 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 3365 "Invalid cast"); 3366 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3367 unsigned DstBits = Ty->getScalarSizeInBits(); 3368 Instruction::CastOps opcode = 3369 (SrcBits == DstBits ? Instruction::BitCast : 3370 (SrcBits > DstBits ? Instruction::Trunc : 3371 (isSigned ? Instruction::SExt : Instruction::ZExt))); 3372 return Create(opcode, C, Ty, Name, InsertAtEnd); 3373 } 3374 3375 CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 3376 const Twine &Name, 3377 Instruction *InsertBefore) { 3378 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 3379 "Invalid cast"); 3380 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3381 unsigned DstBits = Ty->getScalarSizeInBits(); 3382 Instruction::CastOps opcode = 3383 (SrcBits == DstBits ? Instruction::BitCast : 3384 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 3385 return Create(opcode, C, Ty, Name, InsertBefore); 3386 } 3387 3388 CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 3389 const Twine &Name, 3390 BasicBlock *InsertAtEnd) { 3391 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 3392 "Invalid cast"); 3393 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3394 unsigned DstBits = Ty->getScalarSizeInBits(); 3395 Instruction::CastOps opcode = 3396 (SrcBits == DstBits ? Instruction::BitCast : 3397 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 3398 return Create(opcode, C, Ty, Name, InsertAtEnd); 3399 } 3400 3401 bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { 3402 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) 3403 return false; 3404 3405 if (SrcTy == DestTy) 3406 return true; 3407 3408 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { 3409 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { 3410 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3411 // An element by element cast. Valid if casting the elements is valid. 3412 SrcTy = SrcVecTy->getElementType(); 3413 DestTy = DestVecTy->getElementType(); 3414 } 3415 } 3416 } 3417 3418 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { 3419 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { 3420 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); 3421 } 3422 } 3423 3424 TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3425 TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3426 3427 // Could still have vectors of pointers if the number of elements doesn't 3428 // match 3429 if (SrcBits.getKnownMinSize() == 0 || DestBits.getKnownMinSize() == 0) 3430 return false; 3431 3432 if (SrcBits != DestBits) 3433 return false; 3434 3435 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) 3436 return false; 3437 3438 return true; 3439 } 3440 3441 bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, 3442 const DataLayout &DL) { 3443 // ptrtoint and inttoptr are not allowed on non-integral pointers 3444 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) 3445 if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) 3446 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3447 !DL.isNonIntegralPointerType(PtrTy)); 3448 if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) 3449 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) 3450 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3451 !DL.isNonIntegralPointerType(PtrTy)); 3452 3453 return isBitCastable(SrcTy, DestTy); 3454 } 3455 3456 // Provide a way to get a "cast" where the cast opcode is inferred from the 3457 // types and size of the operand. This, basically, is a parallel of the 3458 // logic in the castIsValid function below. This axiom should hold: 3459 // castIsValid( getCastOpcode(Val, Ty), Val, Ty) 3460 // should not assert in castIsValid. In other words, this produces a "correct" 3461 // casting opcode for the arguments passed to it. 3462 Instruction::CastOps 3463 CastInst::getCastOpcode( 3464 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { 3465 Type *SrcTy = Src->getType(); 3466 3467 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && 3468 "Only first class types are castable!"); 3469 3470 if (SrcTy == DestTy) 3471 return BitCast; 3472 3473 // FIXME: Check address space sizes here 3474 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) 3475 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) 3476 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3477 // An element by element cast. Find the appropriate opcode based on the 3478 // element types. 3479 SrcTy = SrcVecTy->getElementType(); 3480 DestTy = DestVecTy->getElementType(); 3481 } 3482 3483 // Get the bit sizes, we'll need these 3484 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3485 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3486 3487 // Run through the possibilities ... 3488 if (DestTy->isIntegerTy()) { // Casting to integral 3489 if (SrcTy->isIntegerTy()) { // Casting from integral 3490 if (DestBits < SrcBits) 3491 return Trunc; // int -> smaller int 3492 else if (DestBits > SrcBits) { // its an extension 3493 if (SrcIsSigned) 3494 return SExt; // signed -> SEXT 3495 else 3496 return ZExt; // unsigned -> ZEXT 3497 } else { 3498 return BitCast; // Same size, No-op cast 3499 } 3500 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3501 if (DestIsSigned) 3502 return FPToSI; // FP -> sint 3503 else 3504 return FPToUI; // FP -> uint 3505 } else if (SrcTy->isVectorTy()) { 3506 assert(DestBits == SrcBits && 3507 "Casting vector to integer of different width"); 3508 return BitCast; // Same size, no-op cast 3509 } else { 3510 assert(SrcTy->isPointerTy() && 3511 "Casting from a value that is not first-class type"); 3512 return PtrToInt; // ptr -> int 3513 } 3514 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt 3515 if (SrcTy->isIntegerTy()) { // Casting from integral 3516 if (SrcIsSigned) 3517 return SIToFP; // sint -> FP 3518 else 3519 return UIToFP; // uint -> FP 3520 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3521 if (DestBits < SrcBits) { 3522 return FPTrunc; // FP -> smaller FP 3523 } else if (DestBits > SrcBits) { 3524 return FPExt; // FP -> larger FP 3525 } else { 3526 return BitCast; // same size, no-op cast 3527 } 3528 } else if (SrcTy->isVectorTy()) { 3529 assert(DestBits == SrcBits && 3530 "Casting vector to floating point of different width"); 3531 return BitCast; // same size, no-op cast 3532 } 3533 llvm_unreachable("Casting pointer or non-first class to float"); 3534 } else if (DestTy->isVectorTy()) { 3535 assert(DestBits == SrcBits && 3536 "Illegal cast to vector (wrong type or size)"); 3537 return BitCast; 3538 } else if (DestTy->isPointerTy()) { 3539 if (SrcTy->isPointerTy()) { 3540 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) 3541 return AddrSpaceCast; 3542 return BitCast; // ptr -> ptr 3543 } else if (SrcTy->isIntegerTy()) { 3544 return IntToPtr; // int -> ptr 3545 } 3546 llvm_unreachable("Casting pointer to other than pointer or int"); 3547 } else if (DestTy->isX86_MMXTy()) { 3548 if (SrcTy->isVectorTy()) { 3549 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); 3550 return BitCast; // 64-bit vector to MMX 3551 } 3552 llvm_unreachable("Illegal cast to X86_MMX"); 3553 } 3554 llvm_unreachable("Casting to type that is not first-class"); 3555 } 3556 3557 //===----------------------------------------------------------------------===// 3558 // CastInst SubClass Constructors 3559 //===----------------------------------------------------------------------===// 3560 3561 /// Check that the construction parameters for a CastInst are correct. This 3562 /// could be broken out into the separate constructors but it is useful to have 3563 /// it in one place and to eliminate the redundant code for getting the sizes 3564 /// of the types involved. 3565 bool 3566 CastInst::castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy) { 3567 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || 3568 SrcTy->isAggregateType() || DstTy->isAggregateType()) 3569 return false; 3570 3571 // Get the size of the types in bits, and whether we are dealing 3572 // with vector types, we'll need this later. 3573 bool SrcIsVec = isa<VectorType>(SrcTy); 3574 bool DstIsVec = isa<VectorType>(DstTy); 3575 unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits(); 3576 unsigned DstScalarBitSize = DstTy->getScalarSizeInBits(); 3577 3578 // If these are vector types, get the lengths of the vectors (using zero for 3579 // scalar types means that checking that vector lengths match also checks that 3580 // scalars are not being converted to vectors or vectors to scalars). 3581 ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount() 3582 : ElementCount::getFixed(0); 3583 ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount() 3584 : ElementCount::getFixed(0); 3585 3586 // Switch on the opcode provided 3587 switch (op) { 3588 default: return false; // This is an input error 3589 case Instruction::Trunc: 3590 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3591 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3592 case Instruction::ZExt: 3593 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3594 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3595 case Instruction::SExt: 3596 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3597 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3598 case Instruction::FPTrunc: 3599 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3600 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3601 case Instruction::FPExt: 3602 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3603 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3604 case Instruction::UIToFP: 3605 case Instruction::SIToFP: 3606 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && 3607 SrcEC == DstEC; 3608 case Instruction::FPToUI: 3609 case Instruction::FPToSI: 3610 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && 3611 SrcEC == DstEC; 3612 case Instruction::PtrToInt: 3613 if (SrcEC != DstEC) 3614 return false; 3615 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); 3616 case Instruction::IntToPtr: 3617 if (SrcEC != DstEC) 3618 return false; 3619 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); 3620 case Instruction::BitCast: { 3621 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3622 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3623 3624 // BitCast implies a no-op cast of type only. No bits change. 3625 // However, you can't cast pointers to anything but pointers. 3626 if (!SrcPtrTy != !DstPtrTy) 3627 return false; 3628 3629 // For non-pointer cases, the cast is okay if the source and destination bit 3630 // widths are identical. 3631 if (!SrcPtrTy) 3632 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); 3633 3634 // If both are pointers then the address spaces must match. 3635 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) 3636 return false; 3637 3638 // A vector of pointers must have the same number of elements. 3639 if (SrcIsVec && DstIsVec) 3640 return SrcEC == DstEC; 3641 if (SrcIsVec) 3642 return SrcEC == ElementCount::getFixed(1); 3643 if (DstIsVec) 3644 return DstEC == ElementCount::getFixed(1); 3645 3646 return true; 3647 } 3648 case Instruction::AddrSpaceCast: { 3649 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3650 if (!SrcPtrTy) 3651 return false; 3652 3653 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3654 if (!DstPtrTy) 3655 return false; 3656 3657 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) 3658 return false; 3659 3660 return SrcEC == DstEC; 3661 } 3662 } 3663 } 3664 3665 TruncInst::TruncInst( 3666 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3667 ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { 3668 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 3669 } 3670 3671 TruncInst::TruncInst( 3672 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3673 ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { 3674 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 3675 } 3676 3677 ZExtInst::ZExtInst( 3678 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3679 ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { 3680 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 3681 } 3682 3683 ZExtInst::ZExtInst( 3684 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3685 ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { 3686 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 3687 } 3688 SExtInst::SExtInst( 3689 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3690 ) : CastInst(Ty, SExt, S, Name, InsertBefore) { 3691 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 3692 } 3693 3694 SExtInst::SExtInst( 3695 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3696 ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { 3697 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 3698 } 3699 3700 FPTruncInst::FPTruncInst( 3701 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3702 ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 3703 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 3704 } 3705 3706 FPTruncInst::FPTruncInst( 3707 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3708 ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { 3709 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 3710 } 3711 3712 FPExtInst::FPExtInst( 3713 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3714 ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { 3715 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 3716 } 3717 3718 FPExtInst::FPExtInst( 3719 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3720 ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { 3721 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 3722 } 3723 3724 UIToFPInst::UIToFPInst( 3725 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3726 ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 3727 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 3728 } 3729 3730 UIToFPInst::UIToFPInst( 3731 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3732 ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { 3733 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 3734 } 3735 3736 SIToFPInst::SIToFPInst( 3737 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3738 ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 3739 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 3740 } 3741 3742 SIToFPInst::SIToFPInst( 3743 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3744 ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { 3745 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 3746 } 3747 3748 FPToUIInst::FPToUIInst( 3749 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3750 ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 3751 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 3752 } 3753 3754 FPToUIInst::FPToUIInst( 3755 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3756 ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { 3757 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 3758 } 3759 3760 FPToSIInst::FPToSIInst( 3761 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3762 ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 3763 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 3764 } 3765 3766 FPToSIInst::FPToSIInst( 3767 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3768 ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { 3769 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 3770 } 3771 3772 PtrToIntInst::PtrToIntInst( 3773 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3774 ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 3775 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 3776 } 3777 3778 PtrToIntInst::PtrToIntInst( 3779 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3780 ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { 3781 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 3782 } 3783 3784 IntToPtrInst::IntToPtrInst( 3785 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3786 ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 3787 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 3788 } 3789 3790 IntToPtrInst::IntToPtrInst( 3791 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3792 ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { 3793 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 3794 } 3795 3796 BitCastInst::BitCastInst( 3797 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3798 ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { 3799 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 3800 } 3801 3802 BitCastInst::BitCastInst( 3803 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3804 ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { 3805 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 3806 } 3807 3808 AddrSpaceCastInst::AddrSpaceCastInst( 3809 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 3810 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { 3811 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3812 } 3813 3814 AddrSpaceCastInst::AddrSpaceCastInst( 3815 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 3816 ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { 3817 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3818 } 3819 3820 //===----------------------------------------------------------------------===// 3821 // CmpInst Classes 3822 //===----------------------------------------------------------------------===// 3823 3824 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, 3825 Value *RHS, const Twine &Name, Instruction *InsertBefore, 3826 Instruction *FlagsSource) 3827 : Instruction(ty, op, 3828 OperandTraits<CmpInst>::op_begin(this), 3829 OperandTraits<CmpInst>::operands(this), 3830 InsertBefore) { 3831 Op<0>() = LHS; 3832 Op<1>() = RHS; 3833 setPredicate((Predicate)predicate); 3834 setName(Name); 3835 if (FlagsSource) 3836 copyIRFlags(FlagsSource); 3837 } 3838 3839 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, 3840 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) 3841 : Instruction(ty, op, 3842 OperandTraits<CmpInst>::op_begin(this), 3843 OperandTraits<CmpInst>::operands(this), 3844 InsertAtEnd) { 3845 Op<0>() = LHS; 3846 Op<1>() = RHS; 3847 setPredicate((Predicate)predicate); 3848 setName(Name); 3849 } 3850 3851 CmpInst * 3852 CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, 3853 const Twine &Name, Instruction *InsertBefore) { 3854 if (Op == Instruction::ICmp) { 3855 if (InsertBefore) 3856 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), 3857 S1, S2, Name); 3858 else 3859 return new ICmpInst(CmpInst::Predicate(predicate), 3860 S1, S2, Name); 3861 } 3862 3863 if (InsertBefore) 3864 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), 3865 S1, S2, Name); 3866 else 3867 return new FCmpInst(CmpInst::Predicate(predicate), 3868 S1, S2, Name); 3869 } 3870 3871 CmpInst * 3872 CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, 3873 const Twine &Name, BasicBlock *InsertAtEnd) { 3874 if (Op == Instruction::ICmp) { 3875 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3876 S1, S2, Name); 3877 } 3878 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3879 S1, S2, Name); 3880 } 3881 3882 void CmpInst::swapOperands() { 3883 if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3884 IC->swapOperands(); 3885 else 3886 cast<FCmpInst>(this)->swapOperands(); 3887 } 3888 3889 bool CmpInst::isCommutative() const { 3890 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3891 return IC->isCommutative(); 3892 return cast<FCmpInst>(this)->isCommutative(); 3893 } 3894 3895 bool CmpInst::isEquality(Predicate P) { 3896 if (ICmpInst::isIntPredicate(P)) 3897 return ICmpInst::isEquality(P); 3898 if (FCmpInst::isFPPredicate(P)) 3899 return FCmpInst::isEquality(P); 3900 llvm_unreachable("Unsupported predicate kind"); 3901 } 3902 3903 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { 3904 switch (pred) { 3905 default: llvm_unreachable("Unknown cmp predicate!"); 3906 case ICMP_EQ: return ICMP_NE; 3907 case ICMP_NE: return ICMP_EQ; 3908 case ICMP_UGT: return ICMP_ULE; 3909 case ICMP_ULT: return ICMP_UGE; 3910 case ICMP_UGE: return ICMP_ULT; 3911 case ICMP_ULE: return ICMP_UGT; 3912 case ICMP_SGT: return ICMP_SLE; 3913 case ICMP_SLT: return ICMP_SGE; 3914 case ICMP_SGE: return ICMP_SLT; 3915 case ICMP_SLE: return ICMP_SGT; 3916 3917 case FCMP_OEQ: return FCMP_UNE; 3918 case FCMP_ONE: return FCMP_UEQ; 3919 case FCMP_OGT: return FCMP_ULE; 3920 case FCMP_OLT: return FCMP_UGE; 3921 case FCMP_OGE: return FCMP_ULT; 3922 case FCMP_OLE: return FCMP_UGT; 3923 case FCMP_UEQ: return FCMP_ONE; 3924 case FCMP_UNE: return FCMP_OEQ; 3925 case FCMP_UGT: return FCMP_OLE; 3926 case FCMP_ULT: return FCMP_OGE; 3927 case FCMP_UGE: return FCMP_OLT; 3928 case FCMP_ULE: return FCMP_OGT; 3929 case FCMP_ORD: return FCMP_UNO; 3930 case FCMP_UNO: return FCMP_ORD; 3931 case FCMP_TRUE: return FCMP_FALSE; 3932 case FCMP_FALSE: return FCMP_TRUE; 3933 } 3934 } 3935 3936 StringRef CmpInst::getPredicateName(Predicate Pred) { 3937 switch (Pred) { 3938 default: return "unknown"; 3939 case FCmpInst::FCMP_FALSE: return "false"; 3940 case FCmpInst::FCMP_OEQ: return "oeq"; 3941 case FCmpInst::FCMP_OGT: return "ogt"; 3942 case FCmpInst::FCMP_OGE: return "oge"; 3943 case FCmpInst::FCMP_OLT: return "olt"; 3944 case FCmpInst::FCMP_OLE: return "ole"; 3945 case FCmpInst::FCMP_ONE: return "one"; 3946 case FCmpInst::FCMP_ORD: return "ord"; 3947 case FCmpInst::FCMP_UNO: return "uno"; 3948 case FCmpInst::FCMP_UEQ: return "ueq"; 3949 case FCmpInst::FCMP_UGT: return "ugt"; 3950 case FCmpInst::FCMP_UGE: return "uge"; 3951 case FCmpInst::FCMP_ULT: return "ult"; 3952 case FCmpInst::FCMP_ULE: return "ule"; 3953 case FCmpInst::FCMP_UNE: return "une"; 3954 case FCmpInst::FCMP_TRUE: return "true"; 3955 case ICmpInst::ICMP_EQ: return "eq"; 3956 case ICmpInst::ICMP_NE: return "ne"; 3957 case ICmpInst::ICMP_SGT: return "sgt"; 3958 case ICmpInst::ICMP_SGE: return "sge"; 3959 case ICmpInst::ICMP_SLT: return "slt"; 3960 case ICmpInst::ICMP_SLE: return "sle"; 3961 case ICmpInst::ICMP_UGT: return "ugt"; 3962 case ICmpInst::ICMP_UGE: return "uge"; 3963 case ICmpInst::ICMP_ULT: return "ult"; 3964 case ICmpInst::ICMP_ULE: return "ule"; 3965 } 3966 } 3967 3968 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { 3969 switch (pred) { 3970 default: llvm_unreachable("Unknown icmp predicate!"); 3971 case ICMP_EQ: case ICMP_NE: 3972 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 3973 return pred; 3974 case ICMP_UGT: return ICMP_SGT; 3975 case ICMP_ULT: return ICMP_SLT; 3976 case ICMP_UGE: return ICMP_SGE; 3977 case ICMP_ULE: return ICMP_SLE; 3978 } 3979 } 3980 3981 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { 3982 switch (pred) { 3983 default: llvm_unreachable("Unknown icmp predicate!"); 3984 case ICMP_EQ: case ICMP_NE: 3985 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: 3986 return pred; 3987 case ICMP_SGT: return ICMP_UGT; 3988 case ICMP_SLT: return ICMP_ULT; 3989 case ICMP_SGE: return ICMP_UGE; 3990 case ICMP_SLE: return ICMP_ULE; 3991 } 3992 } 3993 3994 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { 3995 switch (pred) { 3996 default: llvm_unreachable("Unknown cmp predicate!"); 3997 case ICMP_EQ: case ICMP_NE: 3998 return pred; 3999 case ICMP_SGT: return ICMP_SLT; 4000 case ICMP_SLT: return ICMP_SGT; 4001 case ICMP_SGE: return ICMP_SLE; 4002 case ICMP_SLE: return ICMP_SGE; 4003 case ICMP_UGT: return ICMP_ULT; 4004 case ICMP_ULT: return ICMP_UGT; 4005 case ICMP_UGE: return ICMP_ULE; 4006 case ICMP_ULE: return ICMP_UGE; 4007 4008 case FCMP_FALSE: case FCMP_TRUE: 4009 case FCMP_OEQ: case FCMP_ONE: 4010 case FCMP_UEQ: case FCMP_UNE: 4011 case FCMP_ORD: case FCMP_UNO: 4012 return pred; 4013 case FCMP_OGT: return FCMP_OLT; 4014 case FCMP_OLT: return FCMP_OGT; 4015 case FCMP_OGE: return FCMP_OLE; 4016 case FCMP_OLE: return FCMP_OGE; 4017 case FCMP_UGT: return FCMP_ULT; 4018 case FCMP_ULT: return FCMP_UGT; 4019 case FCMP_UGE: return FCMP_ULE; 4020 case FCMP_ULE: return FCMP_UGE; 4021 } 4022 } 4023 4024 bool CmpInst::isNonStrictPredicate(Predicate pred) { 4025 switch (pred) { 4026 case ICMP_SGE: 4027 case ICMP_SLE: 4028 case ICMP_UGE: 4029 case ICMP_ULE: 4030 case FCMP_OGE: 4031 case FCMP_OLE: 4032 case FCMP_UGE: 4033 case FCMP_ULE: 4034 return true; 4035 default: 4036 return false; 4037 } 4038 } 4039 4040 bool CmpInst::isStrictPredicate(Predicate pred) { 4041 switch (pred) { 4042 case ICMP_SGT: 4043 case ICMP_SLT: 4044 case ICMP_UGT: 4045 case ICMP_ULT: 4046 case FCMP_OGT: 4047 case FCMP_OLT: 4048 case FCMP_UGT: 4049 case FCMP_ULT: 4050 return true; 4051 default: 4052 return false; 4053 } 4054 } 4055 4056 CmpInst::Predicate CmpInst::getStrictPredicate(Predicate pred) { 4057 switch (pred) { 4058 case ICMP_SGE: 4059 return ICMP_SGT; 4060 case ICMP_SLE: 4061 return ICMP_SLT; 4062 case ICMP_UGE: 4063 return ICMP_UGT; 4064 case ICMP_ULE: 4065 return ICMP_ULT; 4066 case FCMP_OGE: 4067 return FCMP_OGT; 4068 case FCMP_OLE: 4069 return FCMP_OLT; 4070 case FCMP_UGE: 4071 return FCMP_UGT; 4072 case FCMP_ULE: 4073 return FCMP_ULT; 4074 default: 4075 return pred; 4076 } 4077 } 4078 4079 CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { 4080 switch (pred) { 4081 case ICMP_SGT: 4082 return ICMP_SGE; 4083 case ICMP_SLT: 4084 return ICMP_SLE; 4085 case ICMP_UGT: 4086 return ICMP_UGE; 4087 case ICMP_ULT: 4088 return ICMP_ULE; 4089 case FCMP_OGT: 4090 return FCMP_OGE; 4091 case FCMP_OLT: 4092 return FCMP_OLE; 4093 case FCMP_UGT: 4094 return FCMP_UGE; 4095 case FCMP_ULT: 4096 return FCMP_ULE; 4097 default: 4098 return pred; 4099 } 4100 } 4101 4102 CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { 4103 assert(CmpInst::isRelational(pred) && "Call only with relational predicate!"); 4104 4105 if (isStrictPredicate(pred)) 4106 return getNonStrictPredicate(pred); 4107 if (isNonStrictPredicate(pred)) 4108 return getStrictPredicate(pred); 4109 4110 llvm_unreachable("Unknown predicate!"); 4111 } 4112 4113 CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { 4114 assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!"); 4115 4116 switch (pred) { 4117 default: 4118 llvm_unreachable("Unknown predicate!"); 4119 case CmpInst::ICMP_ULT: 4120 return CmpInst::ICMP_SLT; 4121 case CmpInst::ICMP_ULE: 4122 return CmpInst::ICMP_SLE; 4123 case CmpInst::ICMP_UGT: 4124 return CmpInst::ICMP_SGT; 4125 case CmpInst::ICMP_UGE: 4126 return CmpInst::ICMP_SGE; 4127 } 4128 } 4129 4130 CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) { 4131 assert(CmpInst::isSigned(pred) && "Call only with signed predicates!"); 4132 4133 switch (pred) { 4134 default: 4135 llvm_unreachable("Unknown predicate!"); 4136 case CmpInst::ICMP_SLT: 4137 return CmpInst::ICMP_ULT; 4138 case CmpInst::ICMP_SLE: 4139 return CmpInst::ICMP_ULE; 4140 case CmpInst::ICMP_SGT: 4141 return CmpInst::ICMP_UGT; 4142 case CmpInst::ICMP_SGE: 4143 return CmpInst::ICMP_UGE; 4144 } 4145 } 4146 4147 bool CmpInst::isUnsigned(Predicate predicate) { 4148 switch (predicate) { 4149 default: return false; 4150 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 4151 case ICmpInst::ICMP_UGE: return true; 4152 } 4153 } 4154 4155 bool CmpInst::isSigned(Predicate predicate) { 4156 switch (predicate) { 4157 default: return false; 4158 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 4159 case ICmpInst::ICMP_SGE: return true; 4160 } 4161 } 4162 4163 bool ICmpInst::compare(const APInt &LHS, const APInt &RHS, 4164 ICmpInst::Predicate Pred) { 4165 assert(ICmpInst::isIntPredicate(Pred) && "Only for integer predicates!"); 4166 switch (Pred) { 4167 case ICmpInst::Predicate::ICMP_EQ: 4168 return LHS.eq(RHS); 4169 case ICmpInst::Predicate::ICMP_NE: 4170 return LHS.ne(RHS); 4171 case ICmpInst::Predicate::ICMP_UGT: 4172 return LHS.ugt(RHS); 4173 case ICmpInst::Predicate::ICMP_UGE: 4174 return LHS.uge(RHS); 4175 case ICmpInst::Predicate::ICMP_ULT: 4176 return LHS.ult(RHS); 4177 case ICmpInst::Predicate::ICMP_ULE: 4178 return LHS.ule(RHS); 4179 case ICmpInst::Predicate::ICMP_SGT: 4180 return LHS.sgt(RHS); 4181 case ICmpInst::Predicate::ICMP_SGE: 4182 return LHS.sge(RHS); 4183 case ICmpInst::Predicate::ICMP_SLT: 4184 return LHS.slt(RHS); 4185 case ICmpInst::Predicate::ICMP_SLE: 4186 return LHS.sle(RHS); 4187 default: 4188 llvm_unreachable("Unexpected non-integer predicate."); 4189 }; 4190 } 4191 4192 bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS, 4193 FCmpInst::Predicate Pred) { 4194 APFloat::cmpResult R = LHS.compare(RHS); 4195 switch (Pred) { 4196 default: 4197 llvm_unreachable("Invalid FCmp Predicate"); 4198 case FCmpInst::FCMP_FALSE: 4199 return false; 4200 case FCmpInst::FCMP_TRUE: 4201 return true; 4202 case FCmpInst::FCMP_UNO: 4203 return R == APFloat::cmpUnordered; 4204 case FCmpInst::FCMP_ORD: 4205 return R != APFloat::cmpUnordered; 4206 case FCmpInst::FCMP_UEQ: 4207 return R == APFloat::cmpUnordered || R == APFloat::cmpEqual; 4208 case FCmpInst::FCMP_OEQ: 4209 return R == APFloat::cmpEqual; 4210 case FCmpInst::FCMP_UNE: 4211 return R != APFloat::cmpEqual; 4212 case FCmpInst::FCMP_ONE: 4213 return R == APFloat::cmpLessThan || R == APFloat::cmpGreaterThan; 4214 case FCmpInst::FCMP_ULT: 4215 return R == APFloat::cmpUnordered || R == APFloat::cmpLessThan; 4216 case FCmpInst::FCMP_OLT: 4217 return R == APFloat::cmpLessThan; 4218 case FCmpInst::FCMP_UGT: 4219 return R == APFloat::cmpUnordered || R == APFloat::cmpGreaterThan; 4220 case FCmpInst::FCMP_OGT: 4221 return R == APFloat::cmpGreaterThan; 4222 case FCmpInst::FCMP_ULE: 4223 return R != APFloat::cmpGreaterThan; 4224 case FCmpInst::FCMP_OLE: 4225 return R == APFloat::cmpLessThan || R == APFloat::cmpEqual; 4226 case FCmpInst::FCMP_UGE: 4227 return R != APFloat::cmpLessThan; 4228 case FCmpInst::FCMP_OGE: 4229 return R == APFloat::cmpGreaterThan || R == APFloat::cmpEqual; 4230 } 4231 } 4232 4233 CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) { 4234 assert(CmpInst::isRelational(pred) && 4235 "Call only with non-equality predicates!"); 4236 4237 if (isSigned(pred)) 4238 return getUnsignedPredicate(pred); 4239 if (isUnsigned(pred)) 4240 return getSignedPredicate(pred); 4241 4242 llvm_unreachable("Unknown predicate!"); 4243 } 4244 4245 bool CmpInst::isOrdered(Predicate predicate) { 4246 switch (predicate) { 4247 default: return false; 4248 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 4249 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 4250 case FCmpInst::FCMP_ORD: return true; 4251 } 4252 } 4253 4254 bool CmpInst::isUnordered(Predicate predicate) { 4255 switch (predicate) { 4256 default: return false; 4257 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 4258 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 4259 case FCmpInst::FCMP_UNO: return true; 4260 } 4261 } 4262 4263 bool CmpInst::isTrueWhenEqual(Predicate predicate) { 4264 switch(predicate) { 4265 default: return false; 4266 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: 4267 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; 4268 } 4269 } 4270 4271 bool CmpInst::isFalseWhenEqual(Predicate predicate) { 4272 switch(predicate) { 4273 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: 4274 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; 4275 default: return false; 4276 } 4277 } 4278 4279 bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { 4280 // If the predicates match, then we know the first condition implies the 4281 // second is true. 4282 if (Pred1 == Pred2) 4283 return true; 4284 4285 switch (Pred1) { 4286 default: 4287 break; 4288 case ICMP_EQ: 4289 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. 4290 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || 4291 Pred2 == ICMP_SLE; 4292 case ICMP_UGT: // A >u B implies A != B and A >=u B are true. 4293 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; 4294 case ICMP_ULT: // A <u B implies A != B and A <=u B are true. 4295 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; 4296 case ICMP_SGT: // A >s B implies A != B and A >=s B are true. 4297 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; 4298 case ICMP_SLT: // A <s B implies A != B and A <=s B are true. 4299 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; 4300 } 4301 return false; 4302 } 4303 4304 bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { 4305 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); 4306 } 4307 4308 //===----------------------------------------------------------------------===// 4309 // SwitchInst Implementation 4310 //===----------------------------------------------------------------------===// 4311 4312 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { 4313 assert(Value && Default && NumReserved); 4314 ReservedSpace = NumReserved; 4315 setNumHungOffUseOperands(2); 4316 allocHungoffUses(ReservedSpace); 4317 4318 Op<0>() = Value; 4319 Op<1>() = Default; 4320 } 4321 4322 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 4323 /// switch on and a default destination. The number of additional cases can 4324 /// be specified here to make memory allocation more efficient. This 4325 /// constructor can also autoinsert before another instruction. 4326 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 4327 Instruction *InsertBefore) 4328 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, 4329 nullptr, 0, InsertBefore) { 4330 init(Value, Default, 2+NumCases*2); 4331 } 4332 4333 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 4334 /// switch on and a default destination. The number of additional cases can 4335 /// be specified here to make memory allocation more efficient. This 4336 /// constructor also autoinserts at the end of the specified BasicBlock. 4337 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 4338 BasicBlock *InsertAtEnd) 4339 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, 4340 nullptr, 0, InsertAtEnd) { 4341 init(Value, Default, 2+NumCases*2); 4342 } 4343 4344 SwitchInst::SwitchInst(const SwitchInst &SI) 4345 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { 4346 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); 4347 setNumHungOffUseOperands(SI.getNumOperands()); 4348 Use *OL = getOperandList(); 4349 const Use *InOL = SI.getOperandList(); 4350 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { 4351 OL[i] = InOL[i]; 4352 OL[i+1] = InOL[i+1]; 4353 } 4354 SubclassOptionalData = SI.SubclassOptionalData; 4355 } 4356 4357 /// addCase - Add an entry to the switch instruction... 4358 /// 4359 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { 4360 unsigned NewCaseIdx = getNumCases(); 4361 unsigned OpNo = getNumOperands(); 4362 if (OpNo+2 > ReservedSpace) 4363 growOperands(); // Get more space! 4364 // Initialize some new operands. 4365 assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); 4366 setNumHungOffUseOperands(OpNo+2); 4367 CaseHandle Case(this, NewCaseIdx); 4368 Case.setValue(OnVal); 4369 Case.setSuccessor(Dest); 4370 } 4371 4372 /// removeCase - This method removes the specified case and its successor 4373 /// from the switch instruction. 4374 SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { 4375 unsigned idx = I->getCaseIndex(); 4376 4377 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); 4378 4379 unsigned NumOps = getNumOperands(); 4380 Use *OL = getOperandList(); 4381 4382 // Overwrite this case with the end of the list. 4383 if (2 + (idx + 1) * 2 != NumOps) { 4384 OL[2 + idx * 2] = OL[NumOps - 2]; 4385 OL[2 + idx * 2 + 1] = OL[NumOps - 1]; 4386 } 4387 4388 // Nuke the last value. 4389 OL[NumOps-2].set(nullptr); 4390 OL[NumOps-2+1].set(nullptr); 4391 setNumHungOffUseOperands(NumOps-2); 4392 4393 return CaseIt(this, idx); 4394 } 4395 4396 /// growOperands - grow operands - This grows the operand list in response 4397 /// to a push_back style of operation. This grows the number of ops by 3 times. 4398 /// 4399 void SwitchInst::growOperands() { 4400 unsigned e = getNumOperands(); 4401 unsigned NumOps = e*3; 4402 4403 ReservedSpace = NumOps; 4404 growHungoffUses(ReservedSpace); 4405 } 4406 4407 MDNode * 4408 SwitchInstProfUpdateWrapper::getProfBranchWeightsMD(const SwitchInst &SI) { 4409 if (MDNode *ProfileData = SI.getMetadata(LLVMContext::MD_prof)) 4410 if (auto *MDName = dyn_cast<MDString>(ProfileData->getOperand(0))) 4411 if (MDName->getString() == "branch_weights") 4412 return ProfileData; 4413 return nullptr; 4414 } 4415 4416 MDNode *SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() { 4417 assert(Changed && "called only if metadata has changed"); 4418 4419 if (!Weights) 4420 return nullptr; 4421 4422 assert(SI.getNumSuccessors() == Weights->size() && 4423 "num of prof branch_weights must accord with num of successors"); 4424 4425 bool AllZeroes = all_of(Weights.value(), [](uint32_t W) { return W == 0; }); 4426 4427 if (AllZeroes || Weights.value().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.value()[I->getCaseIndex() + 1] = Weights.value().back(); 4462 Weights.value().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.value()[SI.getNumSuccessors() - 1] = *W; 4476 } else if (Weights) { 4477 Changed = true; 4478 Weights.value().push_back(W.value_or(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)[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)[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