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