1249259Sdim//===-- Instructions.cpp - Implement the LLVM instructions ----------------===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file implements all of the non-inline methods for the LLVM instruction 11249259Sdim// classes. 12249259Sdim// 13249259Sdim//===----------------------------------------------------------------------===// 14249259Sdim 15249259Sdim#include "llvm/IR/Instructions.h" 16249259Sdim#include "LLVMContextImpl.h" 17249259Sdim#include "llvm/IR/Constants.h" 18249259Sdim#include "llvm/IR/DataLayout.h" 19249259Sdim#include "llvm/IR/DerivedTypes.h" 20249259Sdim#include "llvm/IR/Function.h" 21249259Sdim#include "llvm/IR/Module.h" 22249259Sdim#include "llvm/IR/Operator.h" 23249259Sdim#include "llvm/Support/CallSite.h" 24249259Sdim#include "llvm/Support/ConstantRange.h" 25249259Sdim#include "llvm/Support/ErrorHandling.h" 26249259Sdim#include "llvm/Support/MathExtras.h" 27249259Sdimusing namespace llvm; 28249259Sdim 29249259Sdim//===----------------------------------------------------------------------===// 30249259Sdim// CallSite Class 31249259Sdim//===----------------------------------------------------------------------===// 32249259Sdim 33249259SdimUser::op_iterator CallSite::getCallee() const { 34249259Sdim Instruction *II(getInstruction()); 35249259Sdim return isCall() 36249259Sdim ? cast<CallInst>(II)->op_end() - 1 // Skip Callee 37249259Sdim : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee 38249259Sdim} 39249259Sdim 40249259Sdim//===----------------------------------------------------------------------===// 41249259Sdim// TerminatorInst Class 42249259Sdim//===----------------------------------------------------------------------===// 43249259Sdim 44249259Sdim// Out of line virtual method, so the vtable, etc has a home. 45249259SdimTerminatorInst::~TerminatorInst() { 46249259Sdim} 47249259Sdim 48249259Sdim//===----------------------------------------------------------------------===// 49249259Sdim// UnaryInstruction Class 50249259Sdim//===----------------------------------------------------------------------===// 51249259Sdim 52249259Sdim// Out of line virtual method, so the vtable, etc has a home. 53249259SdimUnaryInstruction::~UnaryInstruction() { 54249259Sdim} 55249259Sdim 56249259Sdim//===----------------------------------------------------------------------===// 57249259Sdim// SelectInst Class 58249259Sdim//===----------------------------------------------------------------------===// 59249259Sdim 60249259Sdim/// areInvalidOperands - Return a string if the specified operands are invalid 61249259Sdim/// for a select operation, otherwise return null. 62249259Sdimconst char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { 63249259Sdim if (Op1->getType() != Op2->getType()) 64249259Sdim return "both values to select must have same type"; 65249259Sdim 66249259Sdim if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { 67249259Sdim // Vector select. 68249259Sdim if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) 69249259Sdim return "vector select condition element type must be i1"; 70249259Sdim VectorType *ET = dyn_cast<VectorType>(Op1->getType()); 71249259Sdim if (ET == 0) 72249259Sdim return "selected values for vector select must be vectors"; 73249259Sdim if (ET->getNumElements() != VT->getNumElements()) 74249259Sdim return "vector select requires selected vectors to have " 75249259Sdim "the same vector length as select condition"; 76249259Sdim } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { 77249259Sdim return "select condition must be i1 or <n x i1>"; 78249259Sdim } 79249259Sdim return 0; 80249259Sdim} 81249259Sdim 82249259Sdim 83249259Sdim//===----------------------------------------------------------------------===// 84249259Sdim// PHINode Class 85249259Sdim//===----------------------------------------------------------------------===// 86249259Sdim 87249259SdimPHINode::PHINode(const PHINode &PN) 88249259Sdim : Instruction(PN.getType(), Instruction::PHI, 89249259Sdim allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), 90249259Sdim ReservedSpace(PN.getNumOperands()) { 91249259Sdim std::copy(PN.op_begin(), PN.op_end(), op_begin()); 92249259Sdim std::copy(PN.block_begin(), PN.block_end(), block_begin()); 93249259Sdim SubclassOptionalData = PN.SubclassOptionalData; 94249259Sdim} 95249259Sdim 96249259SdimPHINode::~PHINode() { 97249259Sdim dropHungoffUses(); 98249259Sdim} 99249259Sdim 100249259SdimUse *PHINode::allocHungoffUses(unsigned N) const { 101249259Sdim // Allocate the array of Uses of the incoming values, followed by a pointer 102249259Sdim // (with bottom bit set) to the User, followed by the array of pointers to 103249259Sdim // the incoming basic blocks. 104249259Sdim size_t size = N * sizeof(Use) + sizeof(Use::UserRef) 105249259Sdim + N * sizeof(BasicBlock*); 106249259Sdim Use *Begin = static_cast<Use*>(::operator new(size)); 107249259Sdim Use *End = Begin + N; 108249259Sdim (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1); 109249259Sdim return Use::initTags(Begin, End); 110249259Sdim} 111249259Sdim 112249259Sdim// removeIncomingValue - Remove an incoming value. This is useful if a 113249259Sdim// predecessor basic block is deleted. 114249259SdimValue *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { 115249259Sdim Value *Removed = getIncomingValue(Idx); 116249259Sdim 117249259Sdim // Move everything after this operand down. 118249259Sdim // 119249259Sdim // FIXME: we could just swap with the end of the list, then erase. However, 120249259Sdim // clients might not expect this to happen. The code as it is thrashes the 121249259Sdim // use/def lists, which is kinda lame. 122249259Sdim std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); 123249259Sdim std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); 124249259Sdim 125249259Sdim // Nuke the last value. 126249259Sdim Op<-1>().set(0); 127249259Sdim --NumOperands; 128249259Sdim 129249259Sdim // If the PHI node is dead, because it has zero entries, nuke it now. 130249259Sdim if (getNumOperands() == 0 && DeletePHIIfEmpty) { 131249259Sdim // If anyone is using this PHI, make them use a dummy value instead... 132249259Sdim replaceAllUsesWith(UndefValue::get(getType())); 133249259Sdim eraseFromParent(); 134249259Sdim } 135249259Sdim return Removed; 136249259Sdim} 137249259Sdim 138249259Sdim/// growOperands - grow operands - This grows the operand list in response 139249259Sdim/// to a push_back style of operation. This grows the number of ops by 1.5 140249259Sdim/// times. 141249259Sdim/// 142249259Sdimvoid PHINode::growOperands() { 143249259Sdim unsigned e = getNumOperands(); 144249259Sdim unsigned NumOps = e + e / 2; 145249259Sdim if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. 146249259Sdim 147249259Sdim Use *OldOps = op_begin(); 148249259Sdim BasicBlock **OldBlocks = block_begin(); 149249259Sdim 150249259Sdim ReservedSpace = NumOps; 151249259Sdim OperandList = allocHungoffUses(ReservedSpace); 152249259Sdim 153249259Sdim std::copy(OldOps, OldOps + e, op_begin()); 154249259Sdim std::copy(OldBlocks, OldBlocks + e, block_begin()); 155249259Sdim 156249259Sdim Use::zap(OldOps, OldOps + e, true); 157249259Sdim} 158249259Sdim 159249259Sdim/// hasConstantValue - If the specified PHI node always merges together the same 160249259Sdim/// value, return the value, otherwise return null. 161249259SdimValue *PHINode::hasConstantValue() const { 162249259Sdim // Exploit the fact that phi nodes always have at least one entry. 163249259Sdim Value *ConstantValue = getIncomingValue(0); 164249259Sdim for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) 165249259Sdim if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { 166249259Sdim if (ConstantValue != this) 167249259Sdim return 0; // Incoming values not all the same. 168249259Sdim // The case where the first value is this PHI. 169249259Sdim ConstantValue = getIncomingValue(i); 170249259Sdim } 171249259Sdim if (ConstantValue == this) 172249259Sdim return UndefValue::get(getType()); 173249259Sdim return ConstantValue; 174249259Sdim} 175249259Sdim 176249259Sdim//===----------------------------------------------------------------------===// 177249259Sdim// LandingPadInst Implementation 178249259Sdim//===----------------------------------------------------------------------===// 179249259Sdim 180249259SdimLandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, 181249259Sdim unsigned NumReservedValues, const Twine &NameStr, 182249259Sdim Instruction *InsertBefore) 183249259Sdim : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) { 184249259Sdim init(PersonalityFn, 1 + NumReservedValues, NameStr); 185249259Sdim} 186249259Sdim 187249259SdimLandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, 188249259Sdim unsigned NumReservedValues, const Twine &NameStr, 189249259Sdim BasicBlock *InsertAtEnd) 190249259Sdim : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) { 191249259Sdim init(PersonalityFn, 1 + NumReservedValues, NameStr); 192249259Sdim} 193249259Sdim 194249259SdimLandingPadInst::LandingPadInst(const LandingPadInst &LP) 195249259Sdim : Instruction(LP.getType(), Instruction::LandingPad, 196249259Sdim allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()), 197249259Sdim ReservedSpace(LP.getNumOperands()) { 198249259Sdim Use *OL = OperandList, *InOL = LP.OperandList; 199249259Sdim for (unsigned I = 0, E = ReservedSpace; I != E; ++I) 200249259Sdim OL[I] = InOL[I]; 201249259Sdim 202249259Sdim setCleanup(LP.isCleanup()); 203249259Sdim} 204249259Sdim 205249259SdimLandingPadInst::~LandingPadInst() { 206249259Sdim dropHungoffUses(); 207249259Sdim} 208249259Sdim 209249259SdimLandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, 210249259Sdim unsigned NumReservedClauses, 211249259Sdim const Twine &NameStr, 212249259Sdim Instruction *InsertBefore) { 213249259Sdim return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, 214249259Sdim InsertBefore); 215249259Sdim} 216249259Sdim 217249259SdimLandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, 218249259Sdim unsigned NumReservedClauses, 219249259Sdim const Twine &NameStr, 220249259Sdim BasicBlock *InsertAtEnd) { 221249259Sdim return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, 222249259Sdim InsertAtEnd); 223249259Sdim} 224249259Sdim 225249259Sdimvoid LandingPadInst::init(Value *PersFn, unsigned NumReservedValues, 226249259Sdim const Twine &NameStr) { 227249259Sdim ReservedSpace = NumReservedValues; 228249259Sdim NumOperands = 1; 229249259Sdim OperandList = allocHungoffUses(ReservedSpace); 230249259Sdim OperandList[0] = PersFn; 231249259Sdim setName(NameStr); 232249259Sdim setCleanup(false); 233249259Sdim} 234249259Sdim 235249259Sdim/// growOperands - grow operands - This grows the operand list in response to a 236249259Sdim/// push_back style of operation. This grows the number of ops by 2 times. 237249259Sdimvoid LandingPadInst::growOperands(unsigned Size) { 238249259Sdim unsigned e = getNumOperands(); 239249259Sdim if (ReservedSpace >= e + Size) return; 240249259Sdim ReservedSpace = (e + Size / 2) * 2; 241249259Sdim 242249259Sdim Use *NewOps = allocHungoffUses(ReservedSpace); 243249259Sdim Use *OldOps = OperandList; 244249259Sdim for (unsigned i = 0; i != e; ++i) 245249259Sdim NewOps[i] = OldOps[i]; 246249259Sdim 247249259Sdim OperandList = NewOps; 248249259Sdim Use::zap(OldOps, OldOps + e, true); 249249259Sdim} 250249259Sdim 251249259Sdimvoid LandingPadInst::addClause(Value *Val) { 252249259Sdim unsigned OpNo = getNumOperands(); 253249259Sdim growOperands(1); 254249259Sdim assert(OpNo < ReservedSpace && "Growing didn't work!"); 255249259Sdim ++NumOperands; 256249259Sdim OperandList[OpNo] = Val; 257249259Sdim} 258249259Sdim 259249259Sdim//===----------------------------------------------------------------------===// 260249259Sdim// CallInst Implementation 261249259Sdim//===----------------------------------------------------------------------===// 262249259Sdim 263249259SdimCallInst::~CallInst() { 264249259Sdim} 265249259Sdim 266249259Sdimvoid CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) { 267249259Sdim assert(NumOperands == Args.size() + 1 && "NumOperands not set up?"); 268249259Sdim Op<-1>() = Func; 269249259Sdim 270249259Sdim#ifndef NDEBUG 271249259Sdim FunctionType *FTy = 272249259Sdim cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 273249259Sdim 274249259Sdim assert((Args.size() == FTy->getNumParams() || 275249259Sdim (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 276249259Sdim "Calling a function with bad signature!"); 277249259Sdim 278249259Sdim for (unsigned i = 0; i != Args.size(); ++i) 279249259Sdim assert((i >= FTy->getNumParams() || 280249259Sdim FTy->getParamType(i) == Args[i]->getType()) && 281249259Sdim "Calling a function with a bad signature!"); 282249259Sdim#endif 283249259Sdim 284249259Sdim std::copy(Args.begin(), Args.end(), op_begin()); 285249259Sdim setName(NameStr); 286249259Sdim} 287249259Sdim 288249259Sdimvoid CallInst::init(Value *Func, const Twine &NameStr) { 289249259Sdim assert(NumOperands == 1 && "NumOperands not set up?"); 290249259Sdim Op<-1>() = Func; 291249259Sdim 292249259Sdim#ifndef NDEBUG 293249259Sdim FunctionType *FTy = 294249259Sdim cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 295249259Sdim 296249259Sdim assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); 297249259Sdim#endif 298249259Sdim 299249259Sdim setName(NameStr); 300249259Sdim} 301249259Sdim 302249259SdimCallInst::CallInst(Value *Func, const Twine &Name, 303249259Sdim Instruction *InsertBefore) 304249259Sdim : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 305249259Sdim ->getElementType())->getReturnType(), 306249259Sdim Instruction::Call, 307249259Sdim OperandTraits<CallInst>::op_end(this) - 1, 308249259Sdim 1, InsertBefore) { 309249259Sdim init(Func, Name); 310249259Sdim} 311249259Sdim 312249259SdimCallInst::CallInst(Value *Func, const Twine &Name, 313249259Sdim BasicBlock *InsertAtEnd) 314249259Sdim : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 315249259Sdim ->getElementType())->getReturnType(), 316249259Sdim Instruction::Call, 317249259Sdim OperandTraits<CallInst>::op_end(this) - 1, 318249259Sdim 1, InsertAtEnd) { 319249259Sdim init(Func, Name); 320249259Sdim} 321249259Sdim 322249259SdimCallInst::CallInst(const CallInst &CI) 323249259Sdim : Instruction(CI.getType(), Instruction::Call, 324249259Sdim OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), 325249259Sdim CI.getNumOperands()) { 326249259Sdim setAttributes(CI.getAttributes()); 327249259Sdim setTailCall(CI.isTailCall()); 328249259Sdim setCallingConv(CI.getCallingConv()); 329249259Sdim 330249259Sdim std::copy(CI.op_begin(), CI.op_end(), op_begin()); 331249259Sdim SubclassOptionalData = CI.SubclassOptionalData; 332249259Sdim} 333249259Sdim 334249259Sdimvoid CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) { 335249259Sdim AttributeSet PAL = getAttributes(); 336249259Sdim PAL = PAL.addAttribute(getContext(), i, attr); 337249259Sdim setAttributes(PAL); 338249259Sdim} 339249259Sdim 340249259Sdimvoid CallInst::removeAttribute(unsigned i, Attribute attr) { 341249259Sdim AttributeSet PAL = getAttributes(); 342249259Sdim AttrBuilder B(attr); 343249259Sdim LLVMContext &Context = getContext(); 344249259Sdim PAL = PAL.removeAttributes(Context, i, 345249259Sdim AttributeSet::get(Context, i, B)); 346249259Sdim setAttributes(PAL); 347249259Sdim} 348249259Sdim 349263508Sdimbool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const { 350249259Sdim if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) 351249259Sdim return true; 352249259Sdim if (const Function *F = getCalledFunction()) 353249259Sdim return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); 354249259Sdim return false; 355249259Sdim} 356249259Sdim 357249259Sdimbool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { 358249259Sdim if (AttributeList.hasAttribute(i, A)) 359249259Sdim return true; 360249259Sdim if (const Function *F = getCalledFunction()) 361249259Sdim return F->getAttributes().hasAttribute(i, A); 362249259Sdim return false; 363249259Sdim} 364249259Sdim 365249259Sdim/// IsConstantOne - Return true only if val is constant int 1 366249259Sdimstatic bool IsConstantOne(Value *val) { 367249259Sdim assert(val && "IsConstantOne does not work with NULL val"); 368249259Sdim return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); 369249259Sdim} 370249259Sdim 371249259Sdimstatic Instruction *createMalloc(Instruction *InsertBefore, 372249259Sdim BasicBlock *InsertAtEnd, Type *IntPtrTy, 373249259Sdim Type *AllocTy, Value *AllocSize, 374249259Sdim Value *ArraySize, Function *MallocF, 375249259Sdim const Twine &Name) { 376249259Sdim assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && 377249259Sdim "createMalloc needs either InsertBefore or InsertAtEnd"); 378249259Sdim 379249259Sdim // malloc(type) becomes: 380249259Sdim // bitcast (i8* malloc(typeSize)) to type* 381249259Sdim // malloc(type, arraySize) becomes: 382249259Sdim // bitcast (i8 *malloc(typeSize*arraySize)) to type* 383249259Sdim if (!ArraySize) 384249259Sdim ArraySize = ConstantInt::get(IntPtrTy, 1); 385249259Sdim else if (ArraySize->getType() != IntPtrTy) { 386249259Sdim if (InsertBefore) 387249259Sdim ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, 388249259Sdim "", InsertBefore); 389249259Sdim else 390249259Sdim ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, 391249259Sdim "", InsertAtEnd); 392249259Sdim } 393249259Sdim 394249259Sdim if (!IsConstantOne(ArraySize)) { 395249259Sdim if (IsConstantOne(AllocSize)) { 396249259Sdim AllocSize = ArraySize; // Operand * 1 = Operand 397249259Sdim } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { 398249259Sdim Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, 399249259Sdim false /*ZExt*/); 400249259Sdim // Malloc arg is constant product of type size and array size 401249259Sdim AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); 402249259Sdim } else { 403249259Sdim // Multiply type size by the array size... 404249259Sdim if (InsertBefore) 405249259Sdim AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, 406249259Sdim "mallocsize", InsertBefore); 407249259Sdim else 408249259Sdim AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, 409249259Sdim "mallocsize", InsertAtEnd); 410249259Sdim } 411249259Sdim } 412249259Sdim 413249259Sdim assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); 414249259Sdim // Create the call to Malloc. 415249259Sdim BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; 416249259Sdim Module* M = BB->getParent()->getParent(); 417249259Sdim Type *BPTy = Type::getInt8PtrTy(BB->getContext()); 418249259Sdim Value *MallocFunc = MallocF; 419249259Sdim if (!MallocFunc) 420249259Sdim // prototype malloc as "void *malloc(size_t)" 421249259Sdim MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); 422249259Sdim PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); 423249259Sdim CallInst *MCall = NULL; 424249259Sdim Instruction *Result = NULL; 425249259Sdim if (InsertBefore) { 426249259Sdim MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); 427249259Sdim Result = MCall; 428249259Sdim if (Result->getType() != AllocPtrType) 429249259Sdim // Create a cast instruction to convert to the right type... 430249259Sdim Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); 431249259Sdim } else { 432249259Sdim MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); 433249259Sdim Result = MCall; 434249259Sdim if (Result->getType() != AllocPtrType) { 435249259Sdim InsertAtEnd->getInstList().push_back(MCall); 436249259Sdim // Create a cast instruction to convert to the right type... 437249259Sdim Result = new BitCastInst(MCall, AllocPtrType, Name); 438249259Sdim } 439249259Sdim } 440249259Sdim MCall->setTailCall(); 441249259Sdim if (Function *F = dyn_cast<Function>(MallocFunc)) { 442249259Sdim MCall->setCallingConv(F->getCallingConv()); 443249259Sdim if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); 444249259Sdim } 445249259Sdim assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); 446249259Sdim 447249259Sdim return Result; 448249259Sdim} 449249259Sdim 450249259Sdim/// CreateMalloc - Generate the IR for a call to malloc: 451249259Sdim/// 1. Compute the malloc call's argument as the specified type's size, 452249259Sdim/// possibly multiplied by the array size if the array size is not 453249259Sdim/// constant 1. 454249259Sdim/// 2. Call malloc with that argument. 455249259Sdim/// 3. Bitcast the result of the malloc call to the specified type. 456249259SdimInstruction *CallInst::CreateMalloc(Instruction *InsertBefore, 457249259Sdim Type *IntPtrTy, Type *AllocTy, 458249259Sdim Value *AllocSize, Value *ArraySize, 459249259Sdim Function * MallocF, 460249259Sdim const Twine &Name) { 461249259Sdim return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize, 462249259Sdim ArraySize, MallocF, Name); 463249259Sdim} 464249259Sdim 465249259Sdim/// CreateMalloc - Generate the IR for a call to malloc: 466249259Sdim/// 1. Compute the malloc call's argument as the specified type's size, 467249259Sdim/// possibly multiplied by the array size if the array size is not 468249259Sdim/// constant 1. 469249259Sdim/// 2. Call malloc with that argument. 470249259Sdim/// 3. Bitcast the result of the malloc call to the specified type. 471249259Sdim/// Note: This function does not add the bitcast to the basic block, that is the 472249259Sdim/// responsibility of the caller. 473249259SdimInstruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, 474249259Sdim Type *IntPtrTy, Type *AllocTy, 475249259Sdim Value *AllocSize, Value *ArraySize, 476249259Sdim Function *MallocF, const Twine &Name) { 477249259Sdim return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, 478249259Sdim ArraySize, MallocF, Name); 479249259Sdim} 480249259Sdim 481249259Sdimstatic Instruction* createFree(Value* Source, Instruction *InsertBefore, 482249259Sdim BasicBlock *InsertAtEnd) { 483249259Sdim assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && 484249259Sdim "createFree needs either InsertBefore or InsertAtEnd"); 485249259Sdim assert(Source->getType()->isPointerTy() && 486249259Sdim "Can not free something of nonpointer type!"); 487249259Sdim 488249259Sdim BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; 489249259Sdim Module* M = BB->getParent()->getParent(); 490249259Sdim 491249259Sdim Type *VoidTy = Type::getVoidTy(M->getContext()); 492249259Sdim Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); 493249259Sdim // prototype free as "void free(void*)" 494249259Sdim Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); 495249259Sdim CallInst* Result = NULL; 496249259Sdim Value *PtrCast = Source; 497249259Sdim if (InsertBefore) { 498249259Sdim if (Source->getType() != IntPtrTy) 499249259Sdim PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); 500249259Sdim Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); 501249259Sdim } else { 502249259Sdim if (Source->getType() != IntPtrTy) 503249259Sdim PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); 504249259Sdim Result = CallInst::Create(FreeFunc, PtrCast, ""); 505249259Sdim } 506249259Sdim Result->setTailCall(); 507249259Sdim if (Function *F = dyn_cast<Function>(FreeFunc)) 508249259Sdim Result->setCallingConv(F->getCallingConv()); 509249259Sdim 510249259Sdim return Result; 511249259Sdim} 512249259Sdim 513249259Sdim/// CreateFree - Generate the IR for a call to the builtin free function. 514249259SdimInstruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { 515249259Sdim return createFree(Source, InsertBefore, NULL); 516249259Sdim} 517249259Sdim 518249259Sdim/// CreateFree - Generate the IR for a call to the builtin free function. 519249259Sdim/// Note: This function does not add the call to the basic block, that is the 520249259Sdim/// responsibility of the caller. 521249259SdimInstruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { 522249259Sdim Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd); 523249259Sdim assert(FreeCall && "CreateFree did not create a CallInst"); 524249259Sdim return FreeCall; 525249259Sdim} 526249259Sdim 527249259Sdim//===----------------------------------------------------------------------===// 528249259Sdim// InvokeInst Implementation 529249259Sdim//===----------------------------------------------------------------------===// 530249259Sdim 531249259Sdimvoid InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, 532249259Sdim ArrayRef<Value *> Args, const Twine &NameStr) { 533249259Sdim assert(NumOperands == 3 + Args.size() && "NumOperands not set up?"); 534249259Sdim Op<-3>() = Fn; 535249259Sdim Op<-2>() = IfNormal; 536249259Sdim Op<-1>() = IfException; 537249259Sdim 538249259Sdim#ifndef NDEBUG 539249259Sdim FunctionType *FTy = 540249259Sdim cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); 541249259Sdim 542249259Sdim assert(((Args.size() == FTy->getNumParams()) || 543249259Sdim (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 544249259Sdim "Invoking a function with bad signature"); 545249259Sdim 546249259Sdim for (unsigned i = 0, e = Args.size(); i != e; i++) 547249259Sdim assert((i >= FTy->getNumParams() || 548249259Sdim FTy->getParamType(i) == Args[i]->getType()) && 549249259Sdim "Invoking a function with a bad signature!"); 550249259Sdim#endif 551249259Sdim 552249259Sdim std::copy(Args.begin(), Args.end(), op_begin()); 553249259Sdim setName(NameStr); 554249259Sdim} 555249259Sdim 556249259SdimInvokeInst::InvokeInst(const InvokeInst &II) 557249259Sdim : TerminatorInst(II.getType(), Instruction::Invoke, 558249259Sdim OperandTraits<InvokeInst>::op_end(this) 559249259Sdim - II.getNumOperands(), 560249259Sdim II.getNumOperands()) { 561249259Sdim setAttributes(II.getAttributes()); 562249259Sdim setCallingConv(II.getCallingConv()); 563249259Sdim std::copy(II.op_begin(), II.op_end(), op_begin()); 564249259Sdim SubclassOptionalData = II.SubclassOptionalData; 565249259Sdim} 566249259Sdim 567249259SdimBasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { 568249259Sdim return getSuccessor(idx); 569249259Sdim} 570249259Sdimunsigned InvokeInst::getNumSuccessorsV() const { 571249259Sdim return getNumSuccessors(); 572249259Sdim} 573249259Sdimvoid InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { 574249259Sdim return setSuccessor(idx, B); 575249259Sdim} 576249259Sdim 577263508Sdimbool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const { 578249259Sdim if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) 579249259Sdim return true; 580249259Sdim if (const Function *F = getCalledFunction()) 581249259Sdim return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); 582249259Sdim return false; 583249259Sdim} 584249259Sdim 585249259Sdimbool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { 586249259Sdim if (AttributeList.hasAttribute(i, A)) 587249259Sdim return true; 588249259Sdim if (const Function *F = getCalledFunction()) 589249259Sdim return F->getAttributes().hasAttribute(i, A); 590249259Sdim return false; 591249259Sdim} 592249259Sdim 593249259Sdimvoid InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) { 594249259Sdim AttributeSet PAL = getAttributes(); 595249259Sdim PAL = PAL.addAttribute(getContext(), i, attr); 596249259Sdim setAttributes(PAL); 597249259Sdim} 598249259Sdim 599249259Sdimvoid InvokeInst::removeAttribute(unsigned i, Attribute attr) { 600249259Sdim AttributeSet PAL = getAttributes(); 601249259Sdim AttrBuilder B(attr); 602249259Sdim PAL = PAL.removeAttributes(getContext(), i, 603249259Sdim AttributeSet::get(getContext(), i, B)); 604249259Sdim setAttributes(PAL); 605249259Sdim} 606249259Sdim 607249259SdimLandingPadInst *InvokeInst::getLandingPadInst() const { 608249259Sdim return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); 609249259Sdim} 610249259Sdim 611249259Sdim//===----------------------------------------------------------------------===// 612249259Sdim// ReturnInst Implementation 613249259Sdim//===----------------------------------------------------------------------===// 614249259Sdim 615249259SdimReturnInst::ReturnInst(const ReturnInst &RI) 616249259Sdim : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, 617249259Sdim OperandTraits<ReturnInst>::op_end(this) - 618249259Sdim RI.getNumOperands(), 619249259Sdim RI.getNumOperands()) { 620249259Sdim if (RI.getNumOperands()) 621249259Sdim Op<0>() = RI.Op<0>(); 622249259Sdim SubclassOptionalData = RI.SubclassOptionalData; 623249259Sdim} 624249259Sdim 625249259SdimReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) 626249259Sdim : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, 627249259Sdim OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 628249259Sdim InsertBefore) { 629249259Sdim if (retVal) 630249259Sdim Op<0>() = retVal; 631249259Sdim} 632249259SdimReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) 633249259Sdim : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, 634249259Sdim OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 635249259Sdim InsertAtEnd) { 636249259Sdim if (retVal) 637249259Sdim Op<0>() = retVal; 638249259Sdim} 639249259SdimReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 640249259Sdim : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, 641249259Sdim OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { 642249259Sdim} 643249259Sdim 644249259Sdimunsigned ReturnInst::getNumSuccessorsV() const { 645249259Sdim return getNumSuccessors(); 646249259Sdim} 647249259Sdim 648249259Sdim/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to 649249259Sdim/// emit the vtable for the class in this translation unit. 650249259Sdimvoid ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 651249259Sdim llvm_unreachable("ReturnInst has no successors!"); 652249259Sdim} 653249259Sdim 654249259SdimBasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { 655249259Sdim llvm_unreachable("ReturnInst has no successors!"); 656249259Sdim} 657249259Sdim 658249259SdimReturnInst::~ReturnInst() { 659249259Sdim} 660249259Sdim 661249259Sdim//===----------------------------------------------------------------------===// 662249259Sdim// ResumeInst Implementation 663249259Sdim//===----------------------------------------------------------------------===// 664249259Sdim 665249259SdimResumeInst::ResumeInst(const ResumeInst &RI) 666249259Sdim : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, 667249259Sdim OperandTraits<ResumeInst>::op_begin(this), 1) { 668249259Sdim Op<0>() = RI.Op<0>(); 669249259Sdim} 670249259Sdim 671249259SdimResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) 672249259Sdim : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 673249259Sdim OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { 674249259Sdim Op<0>() = Exn; 675249259Sdim} 676249259Sdim 677249259SdimResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) 678249259Sdim : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 679249259Sdim OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { 680249259Sdim Op<0>() = Exn; 681249259Sdim} 682249259Sdim 683249259Sdimunsigned ResumeInst::getNumSuccessorsV() const { 684249259Sdim return getNumSuccessors(); 685249259Sdim} 686249259Sdim 687249259Sdimvoid ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 688249259Sdim llvm_unreachable("ResumeInst has no successors!"); 689249259Sdim} 690249259Sdim 691249259SdimBasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { 692249259Sdim llvm_unreachable("ResumeInst has no successors!"); 693249259Sdim} 694249259Sdim 695249259Sdim//===----------------------------------------------------------------------===// 696249259Sdim// UnreachableInst Implementation 697249259Sdim//===----------------------------------------------------------------------===// 698249259Sdim 699249259SdimUnreachableInst::UnreachableInst(LLVMContext &Context, 700249259Sdim Instruction *InsertBefore) 701249259Sdim : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, 702249259Sdim 0, 0, InsertBefore) { 703249259Sdim} 704249259SdimUnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) 705249259Sdim : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, 706249259Sdim 0, 0, InsertAtEnd) { 707249259Sdim} 708249259Sdim 709249259Sdimunsigned UnreachableInst::getNumSuccessorsV() const { 710249259Sdim return getNumSuccessors(); 711249259Sdim} 712249259Sdim 713249259Sdimvoid UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 714249259Sdim llvm_unreachable("UnreachableInst has no successors!"); 715249259Sdim} 716249259Sdim 717249259SdimBasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { 718249259Sdim llvm_unreachable("UnreachableInst has no successors!"); 719249259Sdim} 720249259Sdim 721249259Sdim//===----------------------------------------------------------------------===// 722249259Sdim// BranchInst Implementation 723249259Sdim//===----------------------------------------------------------------------===// 724249259Sdim 725249259Sdimvoid BranchInst::AssertOK() { 726249259Sdim if (isConditional()) 727249259Sdim assert(getCondition()->getType()->isIntegerTy(1) && 728249259Sdim "May only branch on boolean predicates!"); 729249259Sdim} 730249259Sdim 731249259SdimBranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) 732249259Sdim : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 733249259Sdim OperandTraits<BranchInst>::op_end(this) - 1, 734249259Sdim 1, InsertBefore) { 735249259Sdim assert(IfTrue != 0 && "Branch destination may not be null!"); 736249259Sdim Op<-1>() = IfTrue; 737249259Sdim} 738249259SdimBranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 739249259Sdim Instruction *InsertBefore) 740249259Sdim : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 741249259Sdim OperandTraits<BranchInst>::op_end(this) - 3, 742249259Sdim 3, InsertBefore) { 743249259Sdim Op<-1>() = IfTrue; 744249259Sdim Op<-2>() = IfFalse; 745249259Sdim Op<-3>() = Cond; 746249259Sdim#ifndef NDEBUG 747249259Sdim AssertOK(); 748249259Sdim#endif 749249259Sdim} 750249259Sdim 751249259SdimBranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) 752249259Sdim : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 753249259Sdim OperandTraits<BranchInst>::op_end(this) - 1, 754249259Sdim 1, InsertAtEnd) { 755249259Sdim assert(IfTrue != 0 && "Branch destination may not be null!"); 756249259Sdim Op<-1>() = IfTrue; 757249259Sdim} 758249259Sdim 759249259SdimBranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 760249259Sdim BasicBlock *InsertAtEnd) 761249259Sdim : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 762249259Sdim OperandTraits<BranchInst>::op_end(this) - 3, 763249259Sdim 3, InsertAtEnd) { 764249259Sdim Op<-1>() = IfTrue; 765249259Sdim Op<-2>() = IfFalse; 766249259Sdim Op<-3>() = Cond; 767249259Sdim#ifndef NDEBUG 768249259Sdim AssertOK(); 769249259Sdim#endif 770249259Sdim} 771249259Sdim 772249259Sdim 773249259SdimBranchInst::BranchInst(const BranchInst &BI) : 774249259Sdim TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, 775249259Sdim OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), 776249259Sdim BI.getNumOperands()) { 777249259Sdim Op<-1>() = BI.Op<-1>(); 778249259Sdim if (BI.getNumOperands() != 1) { 779249259Sdim assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); 780249259Sdim Op<-3>() = BI.Op<-3>(); 781249259Sdim Op<-2>() = BI.Op<-2>(); 782249259Sdim } 783249259Sdim SubclassOptionalData = BI.SubclassOptionalData; 784249259Sdim} 785249259Sdim 786249259Sdimvoid BranchInst::swapSuccessors() { 787249259Sdim assert(isConditional() && 788249259Sdim "Cannot swap successors of an unconditional branch"); 789249259Sdim Op<-1>().swap(Op<-2>()); 790249259Sdim 791249259Sdim // Update profile metadata if present and it matches our structural 792249259Sdim // expectations. 793249259Sdim MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); 794249259Sdim if (!ProfileData || ProfileData->getNumOperands() != 3) 795249259Sdim return; 796249259Sdim 797249259Sdim // The first operand is the name. Fetch them backwards and build a new one. 798249259Sdim Value *Ops[] = { 799249259Sdim ProfileData->getOperand(0), 800249259Sdim ProfileData->getOperand(2), 801249259Sdim ProfileData->getOperand(1) 802249259Sdim }; 803249259Sdim setMetadata(LLVMContext::MD_prof, 804249259Sdim MDNode::get(ProfileData->getContext(), Ops)); 805249259Sdim} 806249259Sdim 807249259SdimBasicBlock *BranchInst::getSuccessorV(unsigned idx) const { 808249259Sdim return getSuccessor(idx); 809249259Sdim} 810249259Sdimunsigned BranchInst::getNumSuccessorsV() const { 811249259Sdim return getNumSuccessors(); 812249259Sdim} 813249259Sdimvoid BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { 814249259Sdim setSuccessor(idx, B); 815249259Sdim} 816249259Sdim 817249259Sdim 818249259Sdim//===----------------------------------------------------------------------===// 819249259Sdim// AllocaInst Implementation 820249259Sdim//===----------------------------------------------------------------------===// 821249259Sdim 822249259Sdimstatic Value *getAISize(LLVMContext &Context, Value *Amt) { 823249259Sdim if (!Amt) 824249259Sdim Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); 825249259Sdim else { 826249259Sdim assert(!isa<BasicBlock>(Amt) && 827249259Sdim "Passed basic block into allocation size parameter! Use other ctor"); 828249259Sdim assert(Amt->getType()->isIntegerTy() && 829249259Sdim "Allocation array size is not an integer!"); 830249259Sdim } 831249259Sdim return Amt; 832249259Sdim} 833249259Sdim 834249259SdimAllocaInst::AllocaInst(Type *Ty, Value *ArraySize, 835249259Sdim const Twine &Name, Instruction *InsertBefore) 836249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 837249259Sdim getAISize(Ty->getContext(), ArraySize), InsertBefore) { 838249259Sdim setAlignment(0); 839249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 840249259Sdim setName(Name); 841249259Sdim} 842249259Sdim 843249259SdimAllocaInst::AllocaInst(Type *Ty, Value *ArraySize, 844249259Sdim const Twine &Name, BasicBlock *InsertAtEnd) 845249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 846249259Sdim getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { 847249259Sdim setAlignment(0); 848249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 849249259Sdim setName(Name); 850249259Sdim} 851249259Sdim 852249259SdimAllocaInst::AllocaInst(Type *Ty, const Twine &Name, 853249259Sdim Instruction *InsertBefore) 854249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 855249259Sdim getAISize(Ty->getContext(), 0), InsertBefore) { 856249259Sdim setAlignment(0); 857249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 858249259Sdim setName(Name); 859249259Sdim} 860249259Sdim 861249259SdimAllocaInst::AllocaInst(Type *Ty, const Twine &Name, 862249259Sdim BasicBlock *InsertAtEnd) 863249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 864249259Sdim getAISize(Ty->getContext(), 0), InsertAtEnd) { 865249259Sdim setAlignment(0); 866249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 867249259Sdim setName(Name); 868249259Sdim} 869249259Sdim 870249259SdimAllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 871249259Sdim const Twine &Name, Instruction *InsertBefore) 872249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 873249259Sdim getAISize(Ty->getContext(), ArraySize), InsertBefore) { 874249259Sdim setAlignment(Align); 875249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 876249259Sdim setName(Name); 877249259Sdim} 878249259Sdim 879249259SdimAllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, 880249259Sdim const Twine &Name, BasicBlock *InsertAtEnd) 881249259Sdim : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, 882249259Sdim getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { 883249259Sdim setAlignment(Align); 884249259Sdim assert(!Ty->isVoidTy() && "Cannot allocate void!"); 885249259Sdim setName(Name); 886249259Sdim} 887249259Sdim 888249259Sdim// Out of line virtual method, so the vtable, etc has a home. 889249259SdimAllocaInst::~AllocaInst() { 890249259Sdim} 891249259Sdim 892249259Sdimvoid AllocaInst::setAlignment(unsigned Align) { 893249259Sdim assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 894249259Sdim assert(Align <= MaximumAlignment && 895249259Sdim "Alignment is greater than MaximumAlignment!"); 896249259Sdim setInstructionSubclassData(Log2_32(Align) + 1); 897249259Sdim assert(getAlignment() == Align && "Alignment representation error!"); 898249259Sdim} 899249259Sdim 900249259Sdimbool AllocaInst::isArrayAllocation() const { 901249259Sdim if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) 902249259Sdim return !CI->isOne(); 903249259Sdim return true; 904249259Sdim} 905249259Sdim 906249259SdimType *AllocaInst::getAllocatedType() const { 907249259Sdim return getType()->getElementType(); 908249259Sdim} 909249259Sdim 910249259Sdim/// isStaticAlloca - Return true if this alloca is in the entry block of the 911249259Sdim/// function and is a constant size. If so, the code generator will fold it 912249259Sdim/// into the prolog/epilog code, so it is basically free. 913249259Sdimbool AllocaInst::isStaticAlloca() const { 914249259Sdim // Must be constant size. 915249259Sdim if (!isa<ConstantInt>(getArraySize())) return false; 916249259Sdim 917249259Sdim // Must be in the entry block. 918249259Sdim const BasicBlock *Parent = getParent(); 919249259Sdim return Parent == &Parent->getParent()->front(); 920249259Sdim} 921249259Sdim 922249259Sdim//===----------------------------------------------------------------------===// 923249259Sdim// LoadInst Implementation 924249259Sdim//===----------------------------------------------------------------------===// 925249259Sdim 926249259Sdimvoid LoadInst::AssertOK() { 927249259Sdim assert(getOperand(0)->getType()->isPointerTy() && 928249259Sdim "Ptr must have pointer type."); 929249259Sdim assert(!(isAtomic() && getAlignment() == 0) && 930249259Sdim "Alignment required for atomic load"); 931249259Sdim} 932249259Sdim 933249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) 934249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 935249259Sdim Load, Ptr, InsertBef) { 936249259Sdim setVolatile(false); 937249259Sdim setAlignment(0); 938249259Sdim setAtomic(NotAtomic); 939249259Sdim AssertOK(); 940249259Sdim setName(Name); 941249259Sdim} 942249259Sdim 943249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) 944249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 945249259Sdim Load, Ptr, InsertAE) { 946249259Sdim setVolatile(false); 947249259Sdim setAlignment(0); 948249259Sdim setAtomic(NotAtomic); 949249259Sdim AssertOK(); 950249259Sdim setName(Name); 951249259Sdim} 952249259Sdim 953249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 954249259Sdim Instruction *InsertBef) 955249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 956249259Sdim Load, Ptr, InsertBef) { 957249259Sdim setVolatile(isVolatile); 958249259Sdim setAlignment(0); 959249259Sdim setAtomic(NotAtomic); 960249259Sdim AssertOK(); 961249259Sdim setName(Name); 962249259Sdim} 963249259Sdim 964249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 965249259Sdim BasicBlock *InsertAE) 966249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 967249259Sdim Load, Ptr, InsertAE) { 968249259Sdim setVolatile(isVolatile); 969249259Sdim setAlignment(0); 970249259Sdim setAtomic(NotAtomic); 971249259Sdim AssertOK(); 972249259Sdim setName(Name); 973249259Sdim} 974249259Sdim 975249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 976249259Sdim unsigned Align, Instruction *InsertBef) 977249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 978249259Sdim Load, Ptr, InsertBef) { 979249259Sdim setVolatile(isVolatile); 980249259Sdim setAlignment(Align); 981249259Sdim setAtomic(NotAtomic); 982249259Sdim AssertOK(); 983249259Sdim setName(Name); 984249259Sdim} 985249259Sdim 986249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 987249259Sdim unsigned Align, BasicBlock *InsertAE) 988249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 989249259Sdim Load, Ptr, InsertAE) { 990249259Sdim setVolatile(isVolatile); 991249259Sdim setAlignment(Align); 992249259Sdim setAtomic(NotAtomic); 993249259Sdim AssertOK(); 994249259Sdim setName(Name); 995249259Sdim} 996249259Sdim 997249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 998249259Sdim unsigned Align, AtomicOrdering Order, 999249259Sdim SynchronizationScope SynchScope, 1000249259Sdim Instruction *InsertBef) 1001249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1002249259Sdim Load, Ptr, InsertBef) { 1003249259Sdim setVolatile(isVolatile); 1004249259Sdim setAlignment(Align); 1005249259Sdim setAtomic(Order, SynchScope); 1006249259Sdim AssertOK(); 1007249259Sdim setName(Name); 1008249259Sdim} 1009249259Sdim 1010249259SdimLoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, 1011249259Sdim unsigned Align, AtomicOrdering Order, 1012249259Sdim SynchronizationScope SynchScope, 1013249259Sdim BasicBlock *InsertAE) 1014249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1015249259Sdim Load, Ptr, InsertAE) { 1016249259Sdim setVolatile(isVolatile); 1017249259Sdim setAlignment(Align); 1018249259Sdim setAtomic(Order, SynchScope); 1019249259Sdim AssertOK(); 1020249259Sdim setName(Name); 1021249259Sdim} 1022249259Sdim 1023249259SdimLoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) 1024249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1025249259Sdim Load, Ptr, InsertBef) { 1026249259Sdim setVolatile(false); 1027249259Sdim setAlignment(0); 1028249259Sdim setAtomic(NotAtomic); 1029249259Sdim AssertOK(); 1030249259Sdim if (Name && Name[0]) setName(Name); 1031249259Sdim} 1032249259Sdim 1033249259SdimLoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) 1034249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1035249259Sdim Load, Ptr, InsertAE) { 1036249259Sdim setVolatile(false); 1037249259Sdim setAlignment(0); 1038249259Sdim setAtomic(NotAtomic); 1039249259Sdim AssertOK(); 1040249259Sdim if (Name && Name[0]) setName(Name); 1041249259Sdim} 1042249259Sdim 1043249259SdimLoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, 1044249259Sdim Instruction *InsertBef) 1045249259Sdim: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1046249259Sdim Load, Ptr, InsertBef) { 1047249259Sdim setVolatile(isVolatile); 1048249259Sdim setAlignment(0); 1049249259Sdim setAtomic(NotAtomic); 1050249259Sdim AssertOK(); 1051249259Sdim if (Name && Name[0]) setName(Name); 1052249259Sdim} 1053249259Sdim 1054249259SdimLoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, 1055249259Sdim BasicBlock *InsertAE) 1056249259Sdim : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 1057249259Sdim Load, Ptr, InsertAE) { 1058249259Sdim setVolatile(isVolatile); 1059249259Sdim setAlignment(0); 1060249259Sdim setAtomic(NotAtomic); 1061249259Sdim AssertOK(); 1062249259Sdim if (Name && Name[0]) setName(Name); 1063249259Sdim} 1064249259Sdim 1065249259Sdimvoid LoadInst::setAlignment(unsigned Align) { 1066249259Sdim assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 1067249259Sdim assert(Align <= MaximumAlignment && 1068249259Sdim "Alignment is greater than MaximumAlignment!"); 1069249259Sdim setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | 1070249259Sdim ((Log2_32(Align)+1)<<1)); 1071249259Sdim assert(getAlignment() == Align && "Alignment representation error!"); 1072249259Sdim} 1073249259Sdim 1074249259Sdim//===----------------------------------------------------------------------===// 1075249259Sdim// StoreInst Implementation 1076249259Sdim//===----------------------------------------------------------------------===// 1077249259Sdim 1078249259Sdimvoid StoreInst::AssertOK() { 1079249259Sdim assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); 1080249259Sdim assert(getOperand(1)->getType()->isPointerTy() && 1081249259Sdim "Ptr must have pointer type!"); 1082249259Sdim assert(getOperand(0)->getType() == 1083249259Sdim cast<PointerType>(getOperand(1)->getType())->getElementType() 1084249259Sdim && "Ptr must be a pointer to Val type!"); 1085249259Sdim assert(!(isAtomic() && getAlignment() == 0) && 1086249259Sdim "Alignment required for atomic load"); 1087249259Sdim} 1088249259Sdim 1089249259Sdim 1090249259SdimStoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) 1091249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1092249259Sdim OperandTraits<StoreInst>::op_begin(this), 1093249259Sdim OperandTraits<StoreInst>::operands(this), 1094249259Sdim InsertBefore) { 1095249259Sdim Op<0>() = val; 1096249259Sdim Op<1>() = addr; 1097249259Sdim setVolatile(false); 1098249259Sdim setAlignment(0); 1099249259Sdim setAtomic(NotAtomic); 1100249259Sdim AssertOK(); 1101249259Sdim} 1102249259Sdim 1103249259SdimStoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) 1104249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1105249259Sdim OperandTraits<StoreInst>::op_begin(this), 1106249259Sdim OperandTraits<StoreInst>::operands(this), 1107249259Sdim InsertAtEnd) { 1108249259Sdim Op<0>() = val; 1109249259Sdim Op<1>() = addr; 1110249259Sdim setVolatile(false); 1111249259Sdim setAlignment(0); 1112249259Sdim setAtomic(NotAtomic); 1113249259Sdim AssertOK(); 1114249259Sdim} 1115249259Sdim 1116249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1117249259Sdim Instruction *InsertBefore) 1118249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1119249259Sdim OperandTraits<StoreInst>::op_begin(this), 1120249259Sdim OperandTraits<StoreInst>::operands(this), 1121249259Sdim InsertBefore) { 1122249259Sdim Op<0>() = val; 1123249259Sdim Op<1>() = addr; 1124249259Sdim setVolatile(isVolatile); 1125249259Sdim setAlignment(0); 1126249259Sdim setAtomic(NotAtomic); 1127249259Sdim AssertOK(); 1128249259Sdim} 1129249259Sdim 1130249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1131249259Sdim unsigned Align, Instruction *InsertBefore) 1132249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1133249259Sdim OperandTraits<StoreInst>::op_begin(this), 1134249259Sdim OperandTraits<StoreInst>::operands(this), 1135249259Sdim InsertBefore) { 1136249259Sdim Op<0>() = val; 1137249259Sdim Op<1>() = addr; 1138249259Sdim setVolatile(isVolatile); 1139249259Sdim setAlignment(Align); 1140249259Sdim setAtomic(NotAtomic); 1141249259Sdim AssertOK(); 1142249259Sdim} 1143249259Sdim 1144249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1145249259Sdim unsigned Align, AtomicOrdering Order, 1146249259Sdim SynchronizationScope SynchScope, 1147249259Sdim Instruction *InsertBefore) 1148249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1149249259Sdim OperandTraits<StoreInst>::op_begin(this), 1150249259Sdim OperandTraits<StoreInst>::operands(this), 1151249259Sdim InsertBefore) { 1152249259Sdim Op<0>() = val; 1153249259Sdim Op<1>() = addr; 1154249259Sdim setVolatile(isVolatile); 1155249259Sdim setAlignment(Align); 1156249259Sdim setAtomic(Order, SynchScope); 1157249259Sdim AssertOK(); 1158249259Sdim} 1159249259Sdim 1160249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1161249259Sdim BasicBlock *InsertAtEnd) 1162249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1163249259Sdim OperandTraits<StoreInst>::op_begin(this), 1164249259Sdim OperandTraits<StoreInst>::operands(this), 1165249259Sdim InsertAtEnd) { 1166249259Sdim Op<0>() = val; 1167249259Sdim Op<1>() = addr; 1168249259Sdim setVolatile(isVolatile); 1169249259Sdim setAlignment(0); 1170249259Sdim setAtomic(NotAtomic); 1171249259Sdim AssertOK(); 1172249259Sdim} 1173249259Sdim 1174249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1175249259Sdim unsigned Align, BasicBlock *InsertAtEnd) 1176249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1177249259Sdim OperandTraits<StoreInst>::op_begin(this), 1178249259Sdim OperandTraits<StoreInst>::operands(this), 1179249259Sdim InsertAtEnd) { 1180249259Sdim Op<0>() = val; 1181249259Sdim Op<1>() = addr; 1182249259Sdim setVolatile(isVolatile); 1183249259Sdim setAlignment(Align); 1184249259Sdim setAtomic(NotAtomic); 1185249259Sdim AssertOK(); 1186249259Sdim} 1187249259Sdim 1188249259SdimStoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1189249259Sdim unsigned Align, AtomicOrdering Order, 1190249259Sdim SynchronizationScope SynchScope, 1191249259Sdim BasicBlock *InsertAtEnd) 1192249259Sdim : Instruction(Type::getVoidTy(val->getContext()), Store, 1193249259Sdim OperandTraits<StoreInst>::op_begin(this), 1194249259Sdim OperandTraits<StoreInst>::operands(this), 1195249259Sdim InsertAtEnd) { 1196249259Sdim Op<0>() = val; 1197249259Sdim Op<1>() = addr; 1198249259Sdim setVolatile(isVolatile); 1199249259Sdim setAlignment(Align); 1200249259Sdim setAtomic(Order, SynchScope); 1201249259Sdim AssertOK(); 1202249259Sdim} 1203249259Sdim 1204249259Sdimvoid StoreInst::setAlignment(unsigned Align) { 1205249259Sdim assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 1206249259Sdim assert(Align <= MaximumAlignment && 1207249259Sdim "Alignment is greater than MaximumAlignment!"); 1208249259Sdim setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | 1209249259Sdim ((Log2_32(Align)+1) << 1)); 1210249259Sdim assert(getAlignment() == Align && "Alignment representation error!"); 1211249259Sdim} 1212249259Sdim 1213249259Sdim//===----------------------------------------------------------------------===// 1214249259Sdim// AtomicCmpXchgInst Implementation 1215249259Sdim//===----------------------------------------------------------------------===// 1216249259Sdim 1217249259Sdimvoid AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, 1218249259Sdim AtomicOrdering Ordering, 1219249259Sdim SynchronizationScope SynchScope) { 1220249259Sdim Op<0>() = Ptr; 1221249259Sdim Op<1>() = Cmp; 1222249259Sdim Op<2>() = NewVal; 1223249259Sdim setOrdering(Ordering); 1224249259Sdim setSynchScope(SynchScope); 1225249259Sdim 1226249259Sdim assert(getOperand(0) && getOperand(1) && getOperand(2) && 1227249259Sdim "All operands must be non-null!"); 1228249259Sdim assert(getOperand(0)->getType()->isPointerTy() && 1229249259Sdim "Ptr must have pointer type!"); 1230249259Sdim assert(getOperand(1)->getType() == 1231249259Sdim cast<PointerType>(getOperand(0)->getType())->getElementType() 1232249259Sdim && "Ptr must be a pointer to Cmp type!"); 1233249259Sdim assert(getOperand(2)->getType() == 1234249259Sdim cast<PointerType>(getOperand(0)->getType())->getElementType() 1235249259Sdim && "Ptr must be a pointer to NewVal type!"); 1236249259Sdim assert(Ordering != NotAtomic && 1237249259Sdim "AtomicCmpXchg instructions must be atomic!"); 1238249259Sdim} 1239249259Sdim 1240249259SdimAtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1241249259Sdim AtomicOrdering Ordering, 1242249259Sdim SynchronizationScope SynchScope, 1243249259Sdim Instruction *InsertBefore) 1244249259Sdim : Instruction(Cmp->getType(), AtomicCmpXchg, 1245249259Sdim OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1246249259Sdim OperandTraits<AtomicCmpXchgInst>::operands(this), 1247249259Sdim InsertBefore) { 1248249259Sdim Init(Ptr, Cmp, NewVal, Ordering, SynchScope); 1249249259Sdim} 1250249259Sdim 1251249259SdimAtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1252249259Sdim AtomicOrdering Ordering, 1253249259Sdim SynchronizationScope SynchScope, 1254249259Sdim BasicBlock *InsertAtEnd) 1255249259Sdim : Instruction(Cmp->getType(), AtomicCmpXchg, 1256249259Sdim OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1257249259Sdim OperandTraits<AtomicCmpXchgInst>::operands(this), 1258249259Sdim InsertAtEnd) { 1259249259Sdim Init(Ptr, Cmp, NewVal, Ordering, SynchScope); 1260249259Sdim} 1261249259Sdim 1262249259Sdim//===----------------------------------------------------------------------===// 1263249259Sdim// AtomicRMWInst Implementation 1264249259Sdim//===----------------------------------------------------------------------===// 1265249259Sdim 1266249259Sdimvoid AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, 1267249259Sdim AtomicOrdering Ordering, 1268249259Sdim SynchronizationScope SynchScope) { 1269249259Sdim Op<0>() = Ptr; 1270249259Sdim Op<1>() = Val; 1271249259Sdim setOperation(Operation); 1272249259Sdim setOrdering(Ordering); 1273249259Sdim setSynchScope(SynchScope); 1274249259Sdim 1275249259Sdim assert(getOperand(0) && getOperand(1) && 1276249259Sdim "All operands must be non-null!"); 1277249259Sdim assert(getOperand(0)->getType()->isPointerTy() && 1278249259Sdim "Ptr must have pointer type!"); 1279249259Sdim assert(getOperand(1)->getType() == 1280249259Sdim cast<PointerType>(getOperand(0)->getType())->getElementType() 1281249259Sdim && "Ptr must be a pointer to Val type!"); 1282249259Sdim assert(Ordering != NotAtomic && 1283249259Sdim "AtomicRMW instructions must be atomic!"); 1284249259Sdim} 1285249259Sdim 1286249259SdimAtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1287249259Sdim AtomicOrdering Ordering, 1288249259Sdim SynchronizationScope SynchScope, 1289249259Sdim Instruction *InsertBefore) 1290249259Sdim : Instruction(Val->getType(), AtomicRMW, 1291249259Sdim OperandTraits<AtomicRMWInst>::op_begin(this), 1292249259Sdim OperandTraits<AtomicRMWInst>::operands(this), 1293249259Sdim InsertBefore) { 1294249259Sdim Init(Operation, Ptr, Val, Ordering, SynchScope); 1295249259Sdim} 1296249259Sdim 1297249259SdimAtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1298249259Sdim AtomicOrdering Ordering, 1299249259Sdim SynchronizationScope SynchScope, 1300249259Sdim BasicBlock *InsertAtEnd) 1301249259Sdim : Instruction(Val->getType(), AtomicRMW, 1302249259Sdim OperandTraits<AtomicRMWInst>::op_begin(this), 1303249259Sdim OperandTraits<AtomicRMWInst>::operands(this), 1304249259Sdim InsertAtEnd) { 1305249259Sdim Init(Operation, Ptr, Val, Ordering, SynchScope); 1306249259Sdim} 1307249259Sdim 1308249259Sdim//===----------------------------------------------------------------------===// 1309249259Sdim// FenceInst Implementation 1310249259Sdim//===----------------------------------------------------------------------===// 1311249259Sdim 1312249259SdimFenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1313249259Sdim SynchronizationScope SynchScope, 1314249259Sdim Instruction *InsertBefore) 1315249259Sdim : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) { 1316249259Sdim setOrdering(Ordering); 1317249259Sdim setSynchScope(SynchScope); 1318249259Sdim} 1319249259Sdim 1320249259SdimFenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1321249259Sdim SynchronizationScope SynchScope, 1322249259Sdim BasicBlock *InsertAtEnd) 1323249259Sdim : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) { 1324249259Sdim setOrdering(Ordering); 1325249259Sdim setSynchScope(SynchScope); 1326249259Sdim} 1327249259Sdim 1328249259Sdim//===----------------------------------------------------------------------===// 1329249259Sdim// GetElementPtrInst Implementation 1330249259Sdim//===----------------------------------------------------------------------===// 1331249259Sdim 1332249259Sdimvoid GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, 1333249259Sdim const Twine &Name) { 1334249259Sdim assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?"); 1335249259Sdim OperandList[0] = Ptr; 1336249259Sdim std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); 1337249259Sdim setName(Name); 1338249259Sdim} 1339249259Sdim 1340249259SdimGetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) 1341249259Sdim : Instruction(GEPI.getType(), GetElementPtr, 1342249259Sdim OperandTraits<GetElementPtrInst>::op_end(this) 1343249259Sdim - GEPI.getNumOperands(), 1344249259Sdim GEPI.getNumOperands()) { 1345249259Sdim std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); 1346249259Sdim SubclassOptionalData = GEPI.SubclassOptionalData; 1347249259Sdim} 1348249259Sdim 1349249259Sdim/// getIndexedType - Returns the type of the element that would be accessed with 1350249259Sdim/// a gep instruction with the specified parameters. 1351249259Sdim/// 1352249259Sdim/// The Idxs pointer should point to a continuous piece of memory containing the 1353249259Sdim/// indices, either as Value* or uint64_t. 1354249259Sdim/// 1355249259Sdim/// A null type is returned if the indices are invalid for the specified 1356249259Sdim/// pointer type. 1357249259Sdim/// 1358249259Sdimtemplate <typename IndexTy> 1359249259Sdimstatic Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { 1360249259Sdim PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType()); 1361249259Sdim if (!PTy) return 0; // Type isn't a pointer type! 1362249259Sdim Type *Agg = PTy->getElementType(); 1363249259Sdim 1364249259Sdim // Handle the special case of the empty set index set, which is always valid. 1365249259Sdim if (IdxList.empty()) 1366249259Sdim return Agg; 1367249259Sdim 1368249259Sdim // If there is at least one index, the top level type must be sized, otherwise 1369249259Sdim // it cannot be 'stepped over'. 1370249259Sdim if (!Agg->isSized()) 1371249259Sdim return 0; 1372249259Sdim 1373249259Sdim unsigned CurIdx = 1; 1374249259Sdim for (; CurIdx != IdxList.size(); ++CurIdx) { 1375249259Sdim CompositeType *CT = dyn_cast<CompositeType>(Agg); 1376249259Sdim if (!CT || CT->isPointerTy()) return 0; 1377249259Sdim IndexTy Index = IdxList[CurIdx]; 1378249259Sdim if (!CT->indexValid(Index)) return 0; 1379249259Sdim Agg = CT->getTypeAtIndex(Index); 1380249259Sdim } 1381249259Sdim return CurIdx == IdxList.size() ? Agg : 0; 1382249259Sdim} 1383249259Sdim 1384249259SdimType *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) { 1385249259Sdim return getIndexedTypeInternal(Ptr, IdxList); 1386249259Sdim} 1387249259Sdim 1388249259SdimType *GetElementPtrInst::getIndexedType(Type *Ptr, 1389249259Sdim ArrayRef<Constant *> IdxList) { 1390249259Sdim return getIndexedTypeInternal(Ptr, IdxList); 1391249259Sdim} 1392249259Sdim 1393249259SdimType *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) { 1394249259Sdim return getIndexedTypeInternal(Ptr, IdxList); 1395249259Sdim} 1396249259Sdim 1397249259Sdim/// hasAllZeroIndices - Return true if all of the indices of this GEP are 1398249259Sdim/// zeros. If so, the result pointer and the first operand have the same 1399249259Sdim/// value, just potentially different types. 1400249259Sdimbool GetElementPtrInst::hasAllZeroIndices() const { 1401249259Sdim for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1402249259Sdim if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { 1403249259Sdim if (!CI->isZero()) return false; 1404249259Sdim } else { 1405249259Sdim return false; 1406249259Sdim } 1407249259Sdim } 1408249259Sdim return true; 1409249259Sdim} 1410249259Sdim 1411249259Sdim/// hasAllConstantIndices - Return true if all of the indices of this GEP are 1412249259Sdim/// constant integers. If so, the result pointer and the first operand have 1413249259Sdim/// a constant offset between them. 1414249259Sdimbool GetElementPtrInst::hasAllConstantIndices() const { 1415249259Sdim for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1416249259Sdim if (!isa<ConstantInt>(getOperand(i))) 1417249259Sdim return false; 1418249259Sdim } 1419249259Sdim return true; 1420249259Sdim} 1421249259Sdim 1422249259Sdimvoid GetElementPtrInst::setIsInBounds(bool B) { 1423249259Sdim cast<GEPOperator>(this)->setIsInBounds(B); 1424249259Sdim} 1425249259Sdim 1426249259Sdimbool GetElementPtrInst::isInBounds() const { 1427249259Sdim return cast<GEPOperator>(this)->isInBounds(); 1428249259Sdim} 1429249259Sdim 1430249259Sdimbool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, 1431249259Sdim APInt &Offset) const { 1432249259Sdim // Delegate to the generic GEPOperator implementation. 1433249259Sdim return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); 1434249259Sdim} 1435249259Sdim 1436249259Sdim//===----------------------------------------------------------------------===// 1437249259Sdim// ExtractElementInst Implementation 1438249259Sdim//===----------------------------------------------------------------------===// 1439249259Sdim 1440249259SdimExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1441249259Sdim const Twine &Name, 1442249259Sdim Instruction *InsertBef) 1443249259Sdim : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1444249259Sdim ExtractElement, 1445249259Sdim OperandTraits<ExtractElementInst>::op_begin(this), 1446249259Sdim 2, InsertBef) { 1447249259Sdim assert(isValidOperands(Val, Index) && 1448249259Sdim "Invalid extractelement instruction operands!"); 1449249259Sdim Op<0>() = Val; 1450249259Sdim Op<1>() = Index; 1451249259Sdim setName(Name); 1452249259Sdim} 1453249259Sdim 1454249259SdimExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1455249259Sdim const Twine &Name, 1456249259Sdim BasicBlock *InsertAE) 1457249259Sdim : Instruction(cast<VectorType>(Val->getType())->getElementType(), 1458249259Sdim ExtractElement, 1459249259Sdim OperandTraits<ExtractElementInst>::op_begin(this), 1460249259Sdim 2, InsertAE) { 1461249259Sdim assert(isValidOperands(Val, Index) && 1462249259Sdim "Invalid extractelement instruction operands!"); 1463249259Sdim 1464249259Sdim Op<0>() = Val; 1465249259Sdim Op<1>() = Index; 1466249259Sdim setName(Name); 1467249259Sdim} 1468249259Sdim 1469249259Sdim 1470249259Sdimbool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { 1471249259Sdim if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32)) 1472249259Sdim return false; 1473249259Sdim return true; 1474249259Sdim} 1475249259Sdim 1476249259Sdim 1477249259Sdim//===----------------------------------------------------------------------===// 1478249259Sdim// InsertElementInst Implementation 1479249259Sdim//===----------------------------------------------------------------------===// 1480249259Sdim 1481249259SdimInsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1482249259Sdim const Twine &Name, 1483249259Sdim Instruction *InsertBef) 1484249259Sdim : Instruction(Vec->getType(), InsertElement, 1485249259Sdim OperandTraits<InsertElementInst>::op_begin(this), 1486249259Sdim 3, InsertBef) { 1487249259Sdim assert(isValidOperands(Vec, Elt, Index) && 1488249259Sdim "Invalid insertelement instruction operands!"); 1489249259Sdim Op<0>() = Vec; 1490249259Sdim Op<1>() = Elt; 1491249259Sdim Op<2>() = Index; 1492249259Sdim setName(Name); 1493249259Sdim} 1494249259Sdim 1495249259SdimInsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1496249259Sdim const Twine &Name, 1497249259Sdim BasicBlock *InsertAE) 1498249259Sdim : Instruction(Vec->getType(), InsertElement, 1499249259Sdim OperandTraits<InsertElementInst>::op_begin(this), 1500249259Sdim 3, InsertAE) { 1501249259Sdim assert(isValidOperands(Vec, Elt, Index) && 1502249259Sdim "Invalid insertelement instruction operands!"); 1503249259Sdim 1504249259Sdim Op<0>() = Vec; 1505249259Sdim Op<1>() = Elt; 1506249259Sdim Op<2>() = Index; 1507249259Sdim setName(Name); 1508249259Sdim} 1509249259Sdim 1510249259Sdimbool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 1511249259Sdim const Value *Index) { 1512249259Sdim if (!Vec->getType()->isVectorTy()) 1513249259Sdim return false; // First operand of insertelement must be vector type. 1514249259Sdim 1515249259Sdim if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) 1516249259Sdim return false;// Second operand of insertelement must be vector element type. 1517249259Sdim 1518249259Sdim if (!Index->getType()->isIntegerTy(32)) 1519249259Sdim return false; // Third operand of insertelement must be i32. 1520249259Sdim return true; 1521249259Sdim} 1522249259Sdim 1523249259Sdim 1524249259Sdim//===----------------------------------------------------------------------===// 1525249259Sdim// ShuffleVectorInst Implementation 1526249259Sdim//===----------------------------------------------------------------------===// 1527249259Sdim 1528249259SdimShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1529249259Sdim const Twine &Name, 1530249259Sdim Instruction *InsertBefore) 1531249259Sdim: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1532249259Sdim cast<VectorType>(Mask->getType())->getNumElements()), 1533249259Sdim ShuffleVector, 1534249259Sdim OperandTraits<ShuffleVectorInst>::op_begin(this), 1535249259Sdim OperandTraits<ShuffleVectorInst>::operands(this), 1536249259Sdim InsertBefore) { 1537249259Sdim assert(isValidOperands(V1, V2, Mask) && 1538249259Sdim "Invalid shuffle vector instruction operands!"); 1539249259Sdim Op<0>() = V1; 1540249259Sdim Op<1>() = V2; 1541249259Sdim Op<2>() = Mask; 1542249259Sdim setName(Name); 1543249259Sdim} 1544249259Sdim 1545249259SdimShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1546249259Sdim const Twine &Name, 1547249259Sdim BasicBlock *InsertAtEnd) 1548249259Sdim: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1549249259Sdim cast<VectorType>(Mask->getType())->getNumElements()), 1550249259Sdim ShuffleVector, 1551249259Sdim OperandTraits<ShuffleVectorInst>::op_begin(this), 1552249259Sdim OperandTraits<ShuffleVectorInst>::operands(this), 1553249259Sdim InsertAtEnd) { 1554249259Sdim assert(isValidOperands(V1, V2, Mask) && 1555249259Sdim "Invalid shuffle vector instruction operands!"); 1556249259Sdim 1557249259Sdim Op<0>() = V1; 1558249259Sdim Op<1>() = V2; 1559249259Sdim Op<2>() = Mask; 1560249259Sdim setName(Name); 1561249259Sdim} 1562249259Sdim 1563249259Sdimbool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 1564249259Sdim const Value *Mask) { 1565249259Sdim // V1 and V2 must be vectors of the same type. 1566249259Sdim if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) 1567249259Sdim return false; 1568249259Sdim 1569249259Sdim // Mask must be vector of i32. 1570249259Sdim VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); 1571249259Sdim if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32)) 1572249259Sdim return false; 1573249259Sdim 1574249259Sdim // Check to see if Mask is valid. 1575249259Sdim if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) 1576249259Sdim return true; 1577249259Sdim 1578249259Sdim if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { 1579249259Sdim unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); 1580249259Sdim for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { 1581249259Sdim if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { 1582249259Sdim if (CI->uge(V1Size*2)) 1583249259Sdim return false; 1584249259Sdim } else if (!isa<UndefValue>(MV->getOperand(i))) { 1585249259Sdim return false; 1586249259Sdim } 1587249259Sdim } 1588249259Sdim return true; 1589249259Sdim } 1590249259Sdim 1591249259Sdim if (const ConstantDataSequential *CDS = 1592249259Sdim dyn_cast<ConstantDataSequential>(Mask)) { 1593249259Sdim unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); 1594249259Sdim for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) 1595249259Sdim if (CDS->getElementAsInteger(i) >= V1Size*2) 1596249259Sdim return false; 1597249259Sdim return true; 1598249259Sdim } 1599249259Sdim 1600249259Sdim // The bitcode reader can create a place holder for a forward reference 1601249259Sdim // used as the shuffle mask. When this occurs, the shuffle mask will 1602249259Sdim // fall into this case and fail. To avoid this error, do this bit of 1603249259Sdim // ugliness to allow such a mask pass. 1604249259Sdim if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask)) 1605249259Sdim if (CE->getOpcode() == Instruction::UserOp1) 1606249259Sdim return true; 1607249259Sdim 1608249259Sdim return false; 1609249259Sdim} 1610249259Sdim 1611249259Sdim/// getMaskValue - Return the index from the shuffle mask for the specified 1612249259Sdim/// output result. This is either -1 if the element is undef or a number less 1613249259Sdim/// than 2*numelements. 1614249259Sdimint ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { 1615249259Sdim assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); 1616249259Sdim if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask)) 1617249259Sdim return CDS->getElementAsInteger(i); 1618249259Sdim Constant *C = Mask->getAggregateElement(i); 1619249259Sdim if (isa<UndefValue>(C)) 1620249259Sdim return -1; 1621249259Sdim return cast<ConstantInt>(C)->getZExtValue(); 1622249259Sdim} 1623249259Sdim 1624249259Sdim/// getShuffleMask - Return the full mask for this instruction, where each 1625249259Sdim/// element is the element number and undef's are returned as -1. 1626249259Sdimvoid ShuffleVectorInst::getShuffleMask(Constant *Mask, 1627249259Sdim SmallVectorImpl<int> &Result) { 1628249259Sdim unsigned NumElts = Mask->getType()->getVectorNumElements(); 1629249259Sdim 1630249259Sdim if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) { 1631249259Sdim for (unsigned i = 0; i != NumElts; ++i) 1632249259Sdim Result.push_back(CDS->getElementAsInteger(i)); 1633249259Sdim return; 1634249259Sdim } 1635249259Sdim for (unsigned i = 0; i != NumElts; ++i) { 1636249259Sdim Constant *C = Mask->getAggregateElement(i); 1637249259Sdim Result.push_back(isa<UndefValue>(C) ? -1 : 1638249259Sdim cast<ConstantInt>(C)->getZExtValue()); 1639249259Sdim } 1640249259Sdim} 1641249259Sdim 1642249259Sdim 1643249259Sdim//===----------------------------------------------------------------------===// 1644249259Sdim// InsertValueInst Class 1645249259Sdim//===----------------------------------------------------------------------===// 1646249259Sdim 1647249259Sdimvoid InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 1648249259Sdim const Twine &Name) { 1649249259Sdim assert(NumOperands == 2 && "NumOperands not initialized?"); 1650249259Sdim 1651249259Sdim // There's no fundamental reason why we require at least one index 1652249259Sdim // (other than weirdness with &*IdxBegin being invalid; see 1653249259Sdim // getelementptr's init routine for example). But there's no 1654249259Sdim // present need to support it. 1655249259Sdim assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); 1656249259Sdim 1657249259Sdim assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == 1658249259Sdim Val->getType() && "Inserted value must match indexed type!"); 1659249259Sdim Op<0>() = Agg; 1660249259Sdim Op<1>() = Val; 1661249259Sdim 1662249259Sdim Indices.append(Idxs.begin(), Idxs.end()); 1663249259Sdim setName(Name); 1664249259Sdim} 1665249259Sdim 1666249259SdimInsertValueInst::InsertValueInst(const InsertValueInst &IVI) 1667249259Sdim : Instruction(IVI.getType(), InsertValue, 1668249259Sdim OperandTraits<InsertValueInst>::op_begin(this), 2), 1669249259Sdim Indices(IVI.Indices) { 1670249259Sdim Op<0>() = IVI.getOperand(0); 1671249259Sdim Op<1>() = IVI.getOperand(1); 1672249259Sdim SubclassOptionalData = IVI.SubclassOptionalData; 1673249259Sdim} 1674249259Sdim 1675249259Sdim//===----------------------------------------------------------------------===// 1676249259Sdim// ExtractValueInst Class 1677249259Sdim//===----------------------------------------------------------------------===// 1678249259Sdim 1679249259Sdimvoid ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { 1680249259Sdim assert(NumOperands == 1 && "NumOperands not initialized?"); 1681249259Sdim 1682249259Sdim // There's no fundamental reason why we require at least one index. 1683249259Sdim // But there's no present need to support it. 1684249259Sdim assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); 1685249259Sdim 1686249259Sdim Indices.append(Idxs.begin(), Idxs.end()); 1687249259Sdim setName(Name); 1688249259Sdim} 1689249259Sdim 1690249259SdimExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) 1691249259Sdim : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), 1692249259Sdim Indices(EVI.Indices) { 1693249259Sdim SubclassOptionalData = EVI.SubclassOptionalData; 1694249259Sdim} 1695249259Sdim 1696249259Sdim// getIndexedType - Returns the type of the element that would be extracted 1697249259Sdim// with an extractvalue instruction with the specified parameters. 1698249259Sdim// 1699249259Sdim// A null type is returned if the indices are invalid for the specified 1700249259Sdim// pointer type. 1701249259Sdim// 1702249259SdimType *ExtractValueInst::getIndexedType(Type *Agg, 1703249259Sdim ArrayRef<unsigned> Idxs) { 1704249259Sdim for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) { 1705249259Sdim unsigned Index = Idxs[CurIdx]; 1706249259Sdim // We can't use CompositeType::indexValid(Index) here. 1707249259Sdim // indexValid() always returns true for arrays because getelementptr allows 1708249259Sdim // out-of-bounds indices. Since we don't allow those for extractvalue and 1709249259Sdim // insertvalue we need to check array indexing manually. 1710249259Sdim // Since the only other types we can index into are struct types it's just 1711249259Sdim // as easy to check those manually as well. 1712249259Sdim if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { 1713249259Sdim if (Index >= AT->getNumElements()) 1714249259Sdim return 0; 1715249259Sdim } else if (StructType *ST = dyn_cast<StructType>(Agg)) { 1716249259Sdim if (Index >= ST->getNumElements()) 1717249259Sdim return 0; 1718249259Sdim } else { 1719249259Sdim // Not a valid type to index into. 1720249259Sdim return 0; 1721249259Sdim } 1722249259Sdim 1723249259Sdim Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); 1724249259Sdim } 1725249259Sdim return const_cast<Type*>(Agg); 1726249259Sdim} 1727249259Sdim 1728249259Sdim//===----------------------------------------------------------------------===// 1729249259Sdim// BinaryOperator Class 1730249259Sdim//===----------------------------------------------------------------------===// 1731249259Sdim 1732249259SdimBinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 1733249259Sdim Type *Ty, const Twine &Name, 1734249259Sdim Instruction *InsertBefore) 1735249259Sdim : Instruction(Ty, iType, 1736249259Sdim OperandTraits<BinaryOperator>::op_begin(this), 1737249259Sdim OperandTraits<BinaryOperator>::operands(this), 1738249259Sdim InsertBefore) { 1739249259Sdim Op<0>() = S1; 1740249259Sdim Op<1>() = S2; 1741249259Sdim init(iType); 1742249259Sdim setName(Name); 1743249259Sdim} 1744249259Sdim 1745249259SdimBinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 1746249259Sdim Type *Ty, const Twine &Name, 1747249259Sdim BasicBlock *InsertAtEnd) 1748249259Sdim : Instruction(Ty, iType, 1749249259Sdim OperandTraits<BinaryOperator>::op_begin(this), 1750249259Sdim OperandTraits<BinaryOperator>::operands(this), 1751249259Sdim InsertAtEnd) { 1752249259Sdim Op<0>() = S1; 1753249259Sdim Op<1>() = S2; 1754249259Sdim init(iType); 1755249259Sdim setName(Name); 1756249259Sdim} 1757249259Sdim 1758249259Sdim 1759249259Sdimvoid BinaryOperator::init(BinaryOps iType) { 1760249259Sdim Value *LHS = getOperand(0), *RHS = getOperand(1); 1761249259Sdim (void)LHS; (void)RHS; // Silence warnings. 1762249259Sdim assert(LHS->getType() == RHS->getType() && 1763249259Sdim "Binary operator operand types must match!"); 1764249259Sdim#ifndef NDEBUG 1765249259Sdim switch (iType) { 1766249259Sdim case Add: case Sub: 1767249259Sdim case Mul: 1768249259Sdim assert(getType() == LHS->getType() && 1769249259Sdim "Arithmetic operation should return same type as operands!"); 1770249259Sdim assert(getType()->isIntOrIntVectorTy() && 1771249259Sdim "Tried to create an integer operation on a non-integer type!"); 1772249259Sdim break; 1773249259Sdim case FAdd: case FSub: 1774249259Sdim case FMul: 1775249259Sdim assert(getType() == LHS->getType() && 1776249259Sdim "Arithmetic operation should return same type as operands!"); 1777249259Sdim assert(getType()->isFPOrFPVectorTy() && 1778249259Sdim "Tried to create a floating-point operation on a " 1779249259Sdim "non-floating-point type!"); 1780249259Sdim break; 1781249259Sdim case UDiv: 1782249259Sdim case SDiv: 1783249259Sdim assert(getType() == LHS->getType() && 1784249259Sdim "Arithmetic operation should return same type as operands!"); 1785249259Sdim assert((getType()->isIntegerTy() || (getType()->isVectorTy() && 1786249259Sdim cast<VectorType>(getType())->getElementType()->isIntegerTy())) && 1787249259Sdim "Incorrect operand type (not integer) for S/UDIV"); 1788249259Sdim break; 1789249259Sdim case FDiv: 1790249259Sdim assert(getType() == LHS->getType() && 1791249259Sdim "Arithmetic operation should return same type as operands!"); 1792249259Sdim assert(getType()->isFPOrFPVectorTy() && 1793249259Sdim "Incorrect operand type (not floating point) for FDIV"); 1794249259Sdim break; 1795249259Sdim case URem: 1796249259Sdim case SRem: 1797249259Sdim assert(getType() == LHS->getType() && 1798249259Sdim "Arithmetic operation should return same type as operands!"); 1799249259Sdim assert((getType()->isIntegerTy() || (getType()->isVectorTy() && 1800249259Sdim cast<VectorType>(getType())->getElementType()->isIntegerTy())) && 1801249259Sdim "Incorrect operand type (not integer) for S/UREM"); 1802249259Sdim break; 1803249259Sdim case FRem: 1804249259Sdim assert(getType() == LHS->getType() && 1805249259Sdim "Arithmetic operation should return same type as operands!"); 1806249259Sdim assert(getType()->isFPOrFPVectorTy() && 1807249259Sdim "Incorrect operand type (not floating point) for FREM"); 1808249259Sdim break; 1809249259Sdim case Shl: 1810249259Sdim case LShr: 1811249259Sdim case AShr: 1812249259Sdim assert(getType() == LHS->getType() && 1813249259Sdim "Shift operation should return same type as operands!"); 1814249259Sdim assert((getType()->isIntegerTy() || 1815249259Sdim (getType()->isVectorTy() && 1816249259Sdim cast<VectorType>(getType())->getElementType()->isIntegerTy())) && 1817249259Sdim "Tried to create a shift operation on a non-integral type!"); 1818249259Sdim break; 1819249259Sdim case And: case Or: 1820249259Sdim case Xor: 1821249259Sdim assert(getType() == LHS->getType() && 1822249259Sdim "Logical operation should return same type as operands!"); 1823249259Sdim assert((getType()->isIntegerTy() || 1824249259Sdim (getType()->isVectorTy() && 1825249259Sdim cast<VectorType>(getType())->getElementType()->isIntegerTy())) && 1826249259Sdim "Tried to create a logical operation on a non-integral type!"); 1827249259Sdim break; 1828249259Sdim default: 1829249259Sdim break; 1830249259Sdim } 1831249259Sdim#endif 1832249259Sdim} 1833249259Sdim 1834249259SdimBinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 1835249259Sdim const Twine &Name, 1836249259Sdim Instruction *InsertBefore) { 1837249259Sdim assert(S1->getType() == S2->getType() && 1838249259Sdim "Cannot create binary operator with two operands of differing type!"); 1839249259Sdim return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); 1840249259Sdim} 1841249259Sdim 1842249259SdimBinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 1843249259Sdim const Twine &Name, 1844249259Sdim BasicBlock *InsertAtEnd) { 1845249259Sdim BinaryOperator *Res = Create(Op, S1, S2, Name); 1846249259Sdim InsertAtEnd->getInstList().push_back(Res); 1847249259Sdim return Res; 1848249259Sdim} 1849249259Sdim 1850249259SdimBinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 1851249259Sdim Instruction *InsertBefore) { 1852249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1853249259Sdim return new BinaryOperator(Instruction::Sub, 1854249259Sdim zero, Op, 1855249259Sdim Op->getType(), Name, InsertBefore); 1856249259Sdim} 1857249259Sdim 1858249259SdimBinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 1859249259Sdim BasicBlock *InsertAtEnd) { 1860249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1861249259Sdim return new BinaryOperator(Instruction::Sub, 1862249259Sdim zero, Op, 1863249259Sdim Op->getType(), Name, InsertAtEnd); 1864249259Sdim} 1865249259Sdim 1866249259SdimBinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 1867249259Sdim Instruction *InsertBefore) { 1868249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1869249259Sdim return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); 1870249259Sdim} 1871249259Sdim 1872249259SdimBinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 1873249259Sdim BasicBlock *InsertAtEnd) { 1874249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1875249259Sdim return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); 1876249259Sdim} 1877249259Sdim 1878249259SdimBinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 1879249259Sdim Instruction *InsertBefore) { 1880249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1881249259Sdim return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); 1882249259Sdim} 1883249259Sdim 1884249259SdimBinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, 1885249259Sdim BasicBlock *InsertAtEnd) { 1886249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1887249259Sdim return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); 1888249259Sdim} 1889249259Sdim 1890249259SdimBinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, 1891249259Sdim Instruction *InsertBefore) { 1892249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1893249259Sdim return new BinaryOperator(Instruction::FSub, zero, Op, 1894249259Sdim Op->getType(), Name, InsertBefore); 1895249259Sdim} 1896249259Sdim 1897249259SdimBinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, 1898249259Sdim BasicBlock *InsertAtEnd) { 1899249259Sdim Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); 1900249259Sdim return new BinaryOperator(Instruction::FSub, zero, Op, 1901249259Sdim Op->getType(), Name, InsertAtEnd); 1902249259Sdim} 1903249259Sdim 1904249259SdimBinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 1905249259Sdim Instruction *InsertBefore) { 1906249259Sdim Constant *C = Constant::getAllOnesValue(Op->getType()); 1907249259Sdim return new BinaryOperator(Instruction::Xor, Op, C, 1908249259Sdim Op->getType(), Name, InsertBefore); 1909249259Sdim} 1910249259Sdim 1911249259SdimBinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 1912249259Sdim BasicBlock *InsertAtEnd) { 1913249259Sdim Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); 1914249259Sdim return new BinaryOperator(Instruction::Xor, Op, AllOnes, 1915249259Sdim Op->getType(), Name, InsertAtEnd); 1916249259Sdim} 1917249259Sdim 1918249259Sdim 1919249259Sdim// isConstantAllOnes - Helper function for several functions below 1920249259Sdimstatic inline bool isConstantAllOnes(const Value *V) { 1921249259Sdim if (const Constant *C = dyn_cast<Constant>(V)) 1922249259Sdim return C->isAllOnesValue(); 1923249259Sdim return false; 1924249259Sdim} 1925249259Sdim 1926249259Sdimbool BinaryOperator::isNeg(const Value *V) { 1927249259Sdim if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) 1928249259Sdim if (Bop->getOpcode() == Instruction::Sub) 1929249259Sdim if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) 1930249259Sdim return C->isNegativeZeroValue(); 1931249259Sdim return false; 1932249259Sdim} 1933249259Sdim 1934249259Sdimbool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { 1935249259Sdim if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) 1936249259Sdim if (Bop->getOpcode() == Instruction::FSub) 1937249259Sdim if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) { 1938249259Sdim if (!IgnoreZeroSign) 1939249259Sdim IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros(); 1940249259Sdim return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); 1941249259Sdim } 1942249259Sdim return false; 1943249259Sdim} 1944249259Sdim 1945249259Sdimbool BinaryOperator::isNot(const Value *V) { 1946249259Sdim if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) 1947249259Sdim return (Bop->getOpcode() == Instruction::Xor && 1948249259Sdim (isConstantAllOnes(Bop->getOperand(1)) || 1949249259Sdim isConstantAllOnes(Bop->getOperand(0)))); 1950249259Sdim return false; 1951249259Sdim} 1952249259Sdim 1953249259SdimValue *BinaryOperator::getNegArgument(Value *BinOp) { 1954249259Sdim return cast<BinaryOperator>(BinOp)->getOperand(1); 1955249259Sdim} 1956249259Sdim 1957249259Sdimconst Value *BinaryOperator::getNegArgument(const Value *BinOp) { 1958249259Sdim return getNegArgument(const_cast<Value*>(BinOp)); 1959249259Sdim} 1960249259Sdim 1961249259SdimValue *BinaryOperator::getFNegArgument(Value *BinOp) { 1962249259Sdim return cast<BinaryOperator>(BinOp)->getOperand(1); 1963249259Sdim} 1964249259Sdim 1965249259Sdimconst Value *BinaryOperator::getFNegArgument(const Value *BinOp) { 1966249259Sdim return getFNegArgument(const_cast<Value*>(BinOp)); 1967249259Sdim} 1968249259Sdim 1969249259SdimValue *BinaryOperator::getNotArgument(Value *BinOp) { 1970249259Sdim assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); 1971249259Sdim BinaryOperator *BO = cast<BinaryOperator>(BinOp); 1972249259Sdim Value *Op0 = BO->getOperand(0); 1973249259Sdim Value *Op1 = BO->getOperand(1); 1974249259Sdim if (isConstantAllOnes(Op0)) return Op1; 1975249259Sdim 1976249259Sdim assert(isConstantAllOnes(Op1)); 1977249259Sdim return Op0; 1978249259Sdim} 1979249259Sdim 1980249259Sdimconst Value *BinaryOperator::getNotArgument(const Value *BinOp) { 1981249259Sdim return getNotArgument(const_cast<Value*>(BinOp)); 1982249259Sdim} 1983249259Sdim 1984249259Sdim 1985249259Sdim// swapOperands - Exchange the two operands to this instruction. This 1986249259Sdim// instruction is safe to use on any binary instruction and does not 1987249259Sdim// modify the semantics of the instruction. If the instruction is 1988249259Sdim// order dependent (SetLT f.e.) the opcode is changed. 1989249259Sdim// 1990249259Sdimbool BinaryOperator::swapOperands() { 1991249259Sdim if (!isCommutative()) 1992249259Sdim return true; // Can't commute operands 1993249259Sdim Op<0>().swap(Op<1>()); 1994249259Sdim return false; 1995249259Sdim} 1996249259Sdim 1997249259Sdimvoid BinaryOperator::setHasNoUnsignedWrap(bool b) { 1998249259Sdim cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); 1999249259Sdim} 2000249259Sdim 2001249259Sdimvoid BinaryOperator::setHasNoSignedWrap(bool b) { 2002249259Sdim cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); 2003249259Sdim} 2004249259Sdim 2005249259Sdimvoid BinaryOperator::setIsExact(bool b) { 2006249259Sdim cast<PossiblyExactOperator>(this)->setIsExact(b); 2007249259Sdim} 2008249259Sdim 2009249259Sdimbool BinaryOperator::hasNoUnsignedWrap() const { 2010249259Sdim return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); 2011249259Sdim} 2012249259Sdim 2013249259Sdimbool BinaryOperator::hasNoSignedWrap() const { 2014249259Sdim return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); 2015249259Sdim} 2016249259Sdim 2017249259Sdimbool BinaryOperator::isExact() const { 2018249259Sdim return cast<PossiblyExactOperator>(this)->isExact(); 2019249259Sdim} 2020249259Sdim 2021249259Sdim//===----------------------------------------------------------------------===// 2022249259Sdim// FPMathOperator Class 2023249259Sdim//===----------------------------------------------------------------------===// 2024249259Sdim 2025249259Sdim/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs. 2026249259Sdim/// An accuracy of 0.0 means that the operation should be performed with the 2027249259Sdim/// default precision. 2028249259Sdimfloat FPMathOperator::getFPAccuracy() const { 2029249259Sdim const MDNode *MD = 2030249259Sdim cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); 2031249259Sdim if (!MD) 2032249259Sdim return 0.0; 2033249259Sdim ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0)); 2034249259Sdim return Accuracy->getValueAPF().convertToFloat(); 2035249259Sdim} 2036249259Sdim 2037249259Sdim 2038249259Sdim//===----------------------------------------------------------------------===// 2039249259Sdim// CastInst Class 2040249259Sdim//===----------------------------------------------------------------------===// 2041249259Sdim 2042249259Sdimvoid CastInst::anchor() {} 2043249259Sdim 2044249259Sdim// Just determine if this cast only deals with integral->integral conversion. 2045249259Sdimbool CastInst::isIntegerCast() const { 2046249259Sdim switch (getOpcode()) { 2047249259Sdim default: return false; 2048249259Sdim case Instruction::ZExt: 2049249259Sdim case Instruction::SExt: 2050249259Sdim case Instruction::Trunc: 2051249259Sdim return true; 2052249259Sdim case Instruction::BitCast: 2053249259Sdim return getOperand(0)->getType()->isIntegerTy() && 2054249259Sdim getType()->isIntegerTy(); 2055249259Sdim } 2056249259Sdim} 2057249259Sdim 2058249259Sdimbool CastInst::isLosslessCast() const { 2059249259Sdim // Only BitCast can be lossless, exit fast if we're not BitCast 2060249259Sdim if (getOpcode() != Instruction::BitCast) 2061249259Sdim return false; 2062249259Sdim 2063249259Sdim // Identity cast is always lossless 2064249259Sdim Type* SrcTy = getOperand(0)->getType(); 2065249259Sdim Type* DstTy = getType(); 2066249259Sdim if (SrcTy == DstTy) 2067249259Sdim return true; 2068249259Sdim 2069249259Sdim // Pointer to pointer is always lossless. 2070249259Sdim if (SrcTy->isPointerTy()) 2071249259Sdim return DstTy->isPointerTy(); 2072249259Sdim return false; // Other types have no identity values 2073249259Sdim} 2074249259Sdim 2075249259Sdim/// This function determines if the CastInst does not require any bits to be 2076249259Sdim/// changed in order to effect the cast. Essentially, it identifies cases where 2077249259Sdim/// no code gen is necessary for the cast, hence the name no-op cast. For 2078249259Sdim/// example, the following are all no-op casts: 2079249259Sdim/// # bitcast i32* %x to i8* 2080249259Sdim/// # bitcast <2 x i32> %x to <4 x i16> 2081249259Sdim/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only 2082249259Sdim/// @brief Determine if the described cast is a no-op. 2083249259Sdimbool CastInst::isNoopCast(Instruction::CastOps Opcode, 2084249259Sdim Type *SrcTy, 2085249259Sdim Type *DestTy, 2086249259Sdim Type *IntPtrTy) { 2087249259Sdim switch (Opcode) { 2088249259Sdim default: llvm_unreachable("Invalid CastOp"); 2089249259Sdim case Instruction::Trunc: 2090249259Sdim case Instruction::ZExt: 2091249259Sdim case Instruction::SExt: 2092249259Sdim case Instruction::FPTrunc: 2093249259Sdim case Instruction::FPExt: 2094249259Sdim case Instruction::UIToFP: 2095249259Sdim case Instruction::SIToFP: 2096249259Sdim case Instruction::FPToUI: 2097249259Sdim case Instruction::FPToSI: 2098263508Sdim case Instruction::AddrSpaceCast: 2099263508Sdim // TODO: Target informations may give a more accurate answer here. 2100263508Sdim return false; 2101249259Sdim case Instruction::BitCast: 2102249259Sdim return true; // BitCast never modifies bits. 2103249259Sdim case Instruction::PtrToInt: 2104249259Sdim return IntPtrTy->getScalarSizeInBits() == 2105249259Sdim DestTy->getScalarSizeInBits(); 2106249259Sdim case Instruction::IntToPtr: 2107249259Sdim return IntPtrTy->getScalarSizeInBits() == 2108249259Sdim SrcTy->getScalarSizeInBits(); 2109249259Sdim } 2110249259Sdim} 2111249259Sdim 2112249259Sdim/// @brief Determine if a cast is a no-op. 2113249259Sdimbool CastInst::isNoopCast(Type *IntPtrTy) const { 2114249259Sdim return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); 2115249259Sdim} 2116249259Sdim 2117249259Sdim/// This function determines if a pair of casts can be eliminated and what 2118249259Sdim/// opcode should be used in the elimination. This assumes that there are two 2119249259Sdim/// instructions like this: 2120249259Sdim/// * %F = firstOpcode SrcTy %x to MidTy 2121249259Sdim/// * %S = secondOpcode MidTy %F to DstTy 2122249259Sdim/// The function returns a resultOpcode so these two casts can be replaced with: 2123249259Sdim/// * %Replacement = resultOpcode %SrcTy %x to DstTy 2124249259Sdim/// If no such cast is permited, the function returns 0. 2125249259Sdimunsigned CastInst::isEliminableCastPair( 2126249259Sdim Instruction::CastOps firstOp, Instruction::CastOps secondOp, 2127249259Sdim Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, 2128249259Sdim Type *DstIntPtrTy) { 2129249259Sdim // Define the 144 possibilities for these two cast instructions. The values 2130249259Sdim // in this matrix determine what to do in a given situation and select the 2131249259Sdim // case in the switch below. The rows correspond to firstOp, the columns 2132249259Sdim // correspond to secondOp. In looking at the table below, keep in mind 2133249259Sdim // the following cast properties: 2134249259Sdim // 2135249259Sdim // Size Compare Source Destination 2136249259Sdim // Operator Src ? Size Type Sign Type Sign 2137249259Sdim // -------- ------------ ------------------- --------------------- 2138249259Sdim // TRUNC > Integer Any Integral Any 2139249259Sdim // ZEXT < Integral Unsigned Integer Any 2140249259Sdim // SEXT < Integral Signed Integer Any 2141249259Sdim // FPTOUI n/a FloatPt n/a Integral Unsigned 2142263508Sdim // FPTOSI n/a FloatPt n/a Integral Signed 2143263508Sdim // UITOFP n/a Integral Unsigned FloatPt n/a 2144263508Sdim // SITOFP n/a Integral Signed FloatPt n/a 2145263508Sdim // FPTRUNC > FloatPt n/a FloatPt n/a 2146263508Sdim // FPEXT < FloatPt n/a FloatPt n/a 2147249259Sdim // PTRTOINT n/a Pointer n/a Integral Unsigned 2148249259Sdim // INTTOPTR n/a Integral Unsigned Pointer n/a 2149263508Sdim // BITCAST = FirstClass n/a FirstClass n/a 2150263508Sdim // ADDRSPCST n/a Pointer n/a Pointer n/a 2151249259Sdim // 2152249259Sdim // NOTE: some transforms are safe, but we consider them to be non-profitable. 2153249259Sdim // For example, we could merge "fptoui double to i32" + "zext i32 to i64", 2154249259Sdim // into "fptoui double to i64", but this loses information about the range 2155263508Sdim // of the produced value (we no longer know the top-part is all zeros). 2156249259Sdim // Further this conversion is often much more expensive for typical hardware, 2157263508Sdim // and causes issues when building libgcc. We disallow fptosi+sext for the 2158249259Sdim // same reason. 2159263508Sdim const unsigned numCastOps = 2160249259Sdim Instruction::CastOpsEnd - Instruction::CastOpsBegin; 2161249259Sdim static const uint8_t CastResults[numCastOps][numCastOps] = { 2162263508Sdim // T F F U S F F P I B A -+ 2163263508Sdim // R Z S P P I I T P 2 N T S | 2164263508Sdim // U E E 2 2 2 2 R E I T C C +- secondOp 2165263508Sdim // N X X U S F F N X N 2 V V | 2166263508Sdim // C T T I I P P C T T P T T -+ 2167263508Sdim { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ 2168263508Sdim { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3, 0}, // ZExt | 2169263508Sdim { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | 2170263508Sdim { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | 2171263508Sdim { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | 2172263508Sdim { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp 2173263508Sdim { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | 2174263508Sdim { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4, 0}, // FPTrunc | 2175263508Sdim { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt | 2176263508Sdim { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | 2177263508Sdim { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | 2178263508Sdim { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | 2179263508Sdim { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ 2180249259Sdim }; 2181263508Sdim 2182249259Sdim // If either of the casts are a bitcast from scalar to vector, disallow the 2183249259Sdim // merging. However, bitcast of A->B->A are allowed. 2184249259Sdim bool isFirstBitcast = (firstOp == Instruction::BitCast); 2185249259Sdim bool isSecondBitcast = (secondOp == Instruction::BitCast); 2186249259Sdim bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast); 2187249259Sdim 2188249259Sdim // Check if any of the bitcasts convert scalars<->vectors. 2189249259Sdim if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || 2190249259Sdim (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) 2191249259Sdim // Unless we are bitcasing to the original type, disallow optimizations. 2192249259Sdim if (!chainedBitcast) return 0; 2193249259Sdim 2194249259Sdim int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] 2195249259Sdim [secondOp-Instruction::CastOpsBegin]; 2196249259Sdim switch (ElimCase) { 2197249259Sdim case 0: 2198263508Sdim // Categorically disallowed. 2199249259Sdim return 0; 2200249259Sdim case 1: 2201263508Sdim // Allowed, use first cast's opcode. 2202249259Sdim return firstOp; 2203249259Sdim case 2: 2204263508Sdim // Allowed, use second cast's opcode. 2205249259Sdim return secondOp; 2206249259Sdim case 3: 2207263508Sdim // No-op cast in second op implies firstOp as long as the DestTy 2208249259Sdim // is integer and we are not converting between a vector and a 2209249259Sdim // non vector type. 2210249259Sdim if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) 2211249259Sdim return firstOp; 2212249259Sdim return 0; 2213249259Sdim case 4: 2214263508Sdim // No-op cast in second op implies firstOp as long as the DestTy 2215249259Sdim // is floating point. 2216249259Sdim if (DstTy->isFloatingPointTy()) 2217249259Sdim return firstOp; 2218249259Sdim return 0; 2219249259Sdim case 5: 2220263508Sdim // No-op cast in first op implies secondOp as long as the SrcTy 2221249259Sdim // is an integer. 2222249259Sdim if (SrcTy->isIntegerTy()) 2223249259Sdim return secondOp; 2224249259Sdim return 0; 2225249259Sdim case 6: 2226263508Sdim // No-op cast in first op implies secondOp as long as the SrcTy 2227249259Sdim // is a floating point. 2228249259Sdim if (SrcTy->isFloatingPointTy()) 2229249259Sdim return secondOp; 2230249259Sdim return 0; 2231263508Sdim case 7: { 2232263508Sdim // Cannot simplify if address spaces are different! 2233263508Sdim if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 2234263508Sdim return 0; 2235263508Sdim 2236263508Sdim unsigned MidSize = MidTy->getScalarSizeInBits(); 2237263508Sdim // We can still fold this without knowing the actual sizes as long we 2238263508Sdim // know that the intermediate pointer is the largest possible 2239263508Sdim // pointer size. 2240263508Sdim // FIXME: Is this always true? 2241263508Sdim if (MidSize == 64) 2242263508Sdim return Instruction::BitCast; 2243263508Sdim 2244263508Sdim // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. 2245249259Sdim if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) 2246249259Sdim return 0; 2247249259Sdim unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); 2248249259Sdim if (MidSize >= PtrSize) 2249249259Sdim return Instruction::BitCast; 2250249259Sdim return 0; 2251249259Sdim } 2252249259Sdim case 8: { 2253249259Sdim // ext, trunc -> bitcast, if the SrcTy and DstTy are same size 2254249259Sdim // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) 2255249259Sdim // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) 2256249259Sdim unsigned SrcSize = SrcTy->getScalarSizeInBits(); 2257249259Sdim unsigned DstSize = DstTy->getScalarSizeInBits(); 2258249259Sdim if (SrcSize == DstSize) 2259249259Sdim return Instruction::BitCast; 2260249259Sdim else if (SrcSize < DstSize) 2261249259Sdim return firstOp; 2262249259Sdim return secondOp; 2263249259Sdim } 2264263508Sdim case 9: 2265263508Sdim // zext, sext -> zext, because sext can't sign extend after zext 2266249259Sdim return Instruction::ZExt; 2267249259Sdim case 10: 2268249259Sdim // fpext followed by ftrunc is allowed if the bit size returned to is 2269249259Sdim // the same as the original, in which case its just a bitcast 2270249259Sdim if (SrcTy == DstTy) 2271249259Sdim return Instruction::BitCast; 2272249259Sdim return 0; // If the types are not the same we can't eliminate it. 2273263508Sdim case 11: { 2274249259Sdim // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize 2275249259Sdim if (!MidIntPtrTy) 2276249259Sdim return 0; 2277249259Sdim unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); 2278249259Sdim unsigned SrcSize = SrcTy->getScalarSizeInBits(); 2279249259Sdim unsigned DstSize = DstTy->getScalarSizeInBits(); 2280249259Sdim if (SrcSize <= PtrSize && SrcSize == DstSize) 2281249259Sdim return Instruction::BitCast; 2282249259Sdim return 0; 2283249259Sdim } 2284263508Sdim case 12: { 2285263508Sdim // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS 2286263508Sdim // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS 2287263508Sdim if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 2288263508Sdim return Instruction::AddrSpaceCast; 2289263508Sdim return Instruction::BitCast; 2290263508Sdim } 2291263508Sdim case 13: 2292263508Sdim // FIXME: this state can be merged with (1), but the following assert 2293263508Sdim // is useful to check the correcteness of the sequence due to semantic 2294263508Sdim // change of bitcast. 2295263508Sdim assert( 2296263508Sdim SrcTy->isPtrOrPtrVectorTy() && 2297263508Sdim MidTy->isPtrOrPtrVectorTy() && 2298263508Sdim DstTy->isPtrOrPtrVectorTy() && 2299263508Sdim SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && 2300263508Sdim MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 2301263508Sdim "Illegal addrspacecast, bitcast sequence!"); 2302263508Sdim // Allowed, use first cast's opcode 2303263508Sdim return firstOp; 2304263508Sdim case 14: 2305263508Sdim // FIXME: this state can be merged with (2), but the following assert 2306263508Sdim // is useful to check the correcteness of the sequence due to semantic 2307263508Sdim // change of bitcast. 2308263508Sdim assert( 2309263508Sdim SrcTy->isPtrOrPtrVectorTy() && 2310263508Sdim MidTy->isPtrOrPtrVectorTy() && 2311263508Sdim DstTy->isPtrOrPtrVectorTy() && 2312263508Sdim SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && 2313263508Sdim MidTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace() && 2314263508Sdim "Illegal bitcast, addrspacecast sequence!"); 2315263508Sdim // Allowed, use second cast's opcode 2316263508Sdim return secondOp; 2317263508Sdim case 15: 2318263508Sdim // FIXME: this state can be merged with (1), but the following assert 2319263508Sdim // is useful to check the correcteness of the sequence due to semantic 2320263508Sdim // change of bitcast. 2321263508Sdim assert( 2322263508Sdim SrcTy->isIntOrIntVectorTy() && 2323263508Sdim MidTy->isPtrOrPtrVectorTy() && 2324263508Sdim DstTy->isPtrOrPtrVectorTy() && 2325263508Sdim MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 2326263508Sdim "Illegal inttoptr, bitcast sequence!"); 2327263508Sdim // Allowed, use first cast's opcode 2328263508Sdim return firstOp; 2329263508Sdim case 16: 2330263508Sdim // FIXME: this state can be merged with (2), but the following assert 2331263508Sdim // is useful to check the correcteness of the sequence due to semantic 2332263508Sdim // change of bitcast. 2333263508Sdim assert( 2334263508Sdim SrcTy->isPtrOrPtrVectorTy() && 2335263508Sdim MidTy->isPtrOrPtrVectorTy() && 2336263508Sdim DstTy->isIntOrIntVectorTy() && 2337263508Sdim SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && 2338263508Sdim "Illegal bitcast, ptrtoint sequence!"); 2339263508Sdim // Allowed, use second cast's opcode 2340263508Sdim return secondOp; 2341249259Sdim case 99: 2342263508Sdim // Cast combination can't happen (error in input). This is for all cases 2343249259Sdim // where the MidTy is not the same for the two cast instructions. 2344249259Sdim llvm_unreachable("Invalid Cast Combination"); 2345249259Sdim default: 2346249259Sdim llvm_unreachable("Error in CastResults table!!!"); 2347249259Sdim } 2348249259Sdim} 2349249259Sdim 2350249259SdimCastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 2351249259Sdim const Twine &Name, Instruction *InsertBefore) { 2352249259Sdim assert(castIsValid(op, S, Ty) && "Invalid cast!"); 2353249259Sdim // Construct and return the appropriate CastInst subclass 2354249259Sdim switch (op) { 2355263508Sdim case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); 2356263508Sdim case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); 2357263508Sdim case SExt: return new SExtInst (S, Ty, Name, InsertBefore); 2358263508Sdim case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); 2359263508Sdim case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); 2360263508Sdim case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); 2361263508Sdim case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); 2362263508Sdim case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); 2363263508Sdim case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); 2364263508Sdim case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); 2365263508Sdim case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); 2366263508Sdim case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); 2367263508Sdim case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); 2368263508Sdim default: llvm_unreachable("Invalid opcode provided"); 2369249259Sdim } 2370249259Sdim} 2371249259Sdim 2372249259SdimCastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 2373249259Sdim const Twine &Name, BasicBlock *InsertAtEnd) { 2374249259Sdim assert(castIsValid(op, S, Ty) && "Invalid cast!"); 2375249259Sdim // Construct and return the appropriate CastInst subclass 2376249259Sdim switch (op) { 2377263508Sdim case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); 2378263508Sdim case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); 2379263508Sdim case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); 2380263508Sdim case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); 2381263508Sdim case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); 2382263508Sdim case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); 2383263508Sdim case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); 2384263508Sdim case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); 2385263508Sdim case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); 2386263508Sdim case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); 2387263508Sdim case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); 2388263508Sdim case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); 2389263508Sdim case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); 2390263508Sdim default: llvm_unreachable("Invalid opcode provided"); 2391249259Sdim } 2392249259Sdim} 2393249259Sdim 2394249259SdimCastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 2395249259Sdim const Twine &Name, 2396249259Sdim Instruction *InsertBefore) { 2397249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2398249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2399249259Sdim return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); 2400249259Sdim} 2401249259Sdim 2402249259SdimCastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, 2403249259Sdim const Twine &Name, 2404249259Sdim BasicBlock *InsertAtEnd) { 2405249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2406249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 2407249259Sdim return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); 2408249259Sdim} 2409249259Sdim 2410249259SdimCastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 2411249259Sdim const Twine &Name, 2412249259Sdim Instruction *InsertBefore) { 2413249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2414249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2415249259Sdim return Create(Instruction::SExt, S, Ty, Name, InsertBefore); 2416249259Sdim} 2417249259Sdim 2418249259SdimCastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, 2419249259Sdim const Twine &Name, 2420249259Sdim BasicBlock *InsertAtEnd) { 2421249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2422249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 2423249259Sdim return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); 2424249259Sdim} 2425249259Sdim 2426249259SdimCastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 2427249259Sdim const Twine &Name, 2428249259Sdim Instruction *InsertBefore) { 2429249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2430249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2431249259Sdim return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); 2432249259Sdim} 2433249259Sdim 2434249259SdimCastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, 2435249259Sdim const Twine &Name, 2436249259Sdim BasicBlock *InsertAtEnd) { 2437249259Sdim if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2438249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 2439249259Sdim return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); 2440249259Sdim} 2441249259Sdim 2442249259SdimCastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 2443249259Sdim const Twine &Name, 2444249259Sdim BasicBlock *InsertAtEnd) { 2445263508Sdim assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 2446263508Sdim assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 2447249259Sdim "Invalid cast"); 2448263508Sdim assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 2449263508Sdim assert((!Ty->isVectorTy() || 2450263508Sdim Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && 2451263508Sdim "Invalid cast"); 2452249259Sdim 2453263508Sdim if (Ty->isIntOrIntVectorTy()) 2454249259Sdim return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); 2455263508Sdim 2456263508Sdim Type *STy = S->getType(); 2457263508Sdim if (STy->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 2458263508Sdim return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); 2459263508Sdim 2460249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); 2461249259Sdim} 2462249259Sdim 2463249259Sdim/// @brief Create a BitCast or a PtrToInt cast instruction 2464263508SdimCastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, 2465263508Sdim const Twine &Name, 2466249259Sdim Instruction *InsertBefore) { 2467249259Sdim assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 2468249259Sdim assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 2469249259Sdim "Invalid cast"); 2470263508Sdim assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 2471263508Sdim assert((!Ty->isVectorTy() || 2472263508Sdim Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && 2473263508Sdim "Invalid cast"); 2474249259Sdim 2475249259Sdim if (Ty->isIntOrIntVectorTy()) 2476249259Sdim return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 2477263508Sdim 2478263508Sdim Type *STy = S->getType(); 2479263508Sdim if (STy->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 2480263508Sdim return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); 2481263508Sdim 2482249259Sdim return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2483249259Sdim} 2484249259Sdim 2485249259SdimCastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 2486249259Sdim bool isSigned, const Twine &Name, 2487249259Sdim Instruction *InsertBefore) { 2488249259Sdim assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 2489249259Sdim "Invalid integer cast"); 2490249259Sdim unsigned SrcBits = C->getType()->getScalarSizeInBits(); 2491249259Sdim unsigned DstBits = Ty->getScalarSizeInBits(); 2492249259Sdim Instruction::CastOps opcode = 2493249259Sdim (SrcBits == DstBits ? Instruction::BitCast : 2494249259Sdim (SrcBits > DstBits ? Instruction::Trunc : 2495249259Sdim (isSigned ? Instruction::SExt : Instruction::ZExt))); 2496249259Sdim return Create(opcode, C, Ty, Name, InsertBefore); 2497249259Sdim} 2498249259Sdim 2499249259SdimCastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, 2500249259Sdim bool isSigned, const Twine &Name, 2501249259Sdim BasicBlock *InsertAtEnd) { 2502249259Sdim assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 2503249259Sdim "Invalid cast"); 2504249259Sdim unsigned SrcBits = C->getType()->getScalarSizeInBits(); 2505249259Sdim unsigned DstBits = Ty->getScalarSizeInBits(); 2506249259Sdim Instruction::CastOps opcode = 2507249259Sdim (SrcBits == DstBits ? Instruction::BitCast : 2508249259Sdim (SrcBits > DstBits ? Instruction::Trunc : 2509249259Sdim (isSigned ? Instruction::SExt : Instruction::ZExt))); 2510249259Sdim return Create(opcode, C, Ty, Name, InsertAtEnd); 2511249259Sdim} 2512249259Sdim 2513249259SdimCastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 2514249259Sdim const Twine &Name, 2515249259Sdim Instruction *InsertBefore) { 2516249259Sdim assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 2517249259Sdim "Invalid cast"); 2518249259Sdim unsigned SrcBits = C->getType()->getScalarSizeInBits(); 2519249259Sdim unsigned DstBits = Ty->getScalarSizeInBits(); 2520249259Sdim Instruction::CastOps opcode = 2521249259Sdim (SrcBits == DstBits ? Instruction::BitCast : 2522249259Sdim (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 2523249259Sdim return Create(opcode, C, Ty, Name, InsertBefore); 2524249259Sdim} 2525249259Sdim 2526249259SdimCastInst *CastInst::CreateFPCast(Value *C, Type *Ty, 2527249259Sdim const Twine &Name, 2528249259Sdim BasicBlock *InsertAtEnd) { 2529249259Sdim assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 2530249259Sdim "Invalid cast"); 2531249259Sdim unsigned SrcBits = C->getType()->getScalarSizeInBits(); 2532249259Sdim unsigned DstBits = Ty->getScalarSizeInBits(); 2533249259Sdim Instruction::CastOps opcode = 2534249259Sdim (SrcBits == DstBits ? Instruction::BitCast : 2535249259Sdim (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 2536249259Sdim return Create(opcode, C, Ty, Name, InsertAtEnd); 2537249259Sdim} 2538249259Sdim 2539249259Sdim// Check whether it is valid to call getCastOpcode for these types. 2540249259Sdim// This routine must be kept in sync with getCastOpcode. 2541249259Sdimbool CastInst::isCastable(Type *SrcTy, Type *DestTy) { 2542249259Sdim if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) 2543249259Sdim return false; 2544249259Sdim 2545249259Sdim if (SrcTy == DestTy) 2546249259Sdim return true; 2547249259Sdim 2548249259Sdim if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) 2549249259Sdim if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) 2550249259Sdim if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { 2551249259Sdim // An element by element cast. Valid if casting the elements is valid. 2552249259Sdim SrcTy = SrcVecTy->getElementType(); 2553249259Sdim DestTy = DestVecTy->getElementType(); 2554249259Sdim } 2555249259Sdim 2556249259Sdim // Get the bit sizes, we'll need these 2557249259Sdim unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 2558249259Sdim unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 2559249259Sdim 2560249259Sdim // Run through the possibilities ... 2561249259Sdim if (DestTy->isIntegerTy()) { // Casting to integral 2562249259Sdim if (SrcTy->isIntegerTy()) { // Casting from integral 2563249259Sdim return true; 2564249259Sdim } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 2565249259Sdim return true; 2566249259Sdim } else if (SrcTy->isVectorTy()) { // Casting from vector 2567249259Sdim return DestBits == SrcBits; 2568249259Sdim } else { // Casting from something else 2569249259Sdim return SrcTy->isPointerTy(); 2570249259Sdim } 2571249259Sdim } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt 2572249259Sdim if (SrcTy->isIntegerTy()) { // Casting from integral 2573249259Sdim return true; 2574249259Sdim } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 2575249259Sdim return true; 2576249259Sdim } else if (SrcTy->isVectorTy()) { // Casting from vector 2577249259Sdim return DestBits == SrcBits; 2578249259Sdim } else { // Casting from something else 2579249259Sdim return false; 2580249259Sdim } 2581249259Sdim } else if (DestTy->isVectorTy()) { // Casting to vector 2582249259Sdim return DestBits == SrcBits; 2583249259Sdim } else if (DestTy->isPointerTy()) { // Casting to pointer 2584249259Sdim if (SrcTy->isPointerTy()) { // Casting from pointer 2585249259Sdim return true; 2586249259Sdim } else if (SrcTy->isIntegerTy()) { // Casting from integral 2587249259Sdim return true; 2588249259Sdim } else { // Casting from something else 2589249259Sdim return false; 2590249259Sdim } 2591249259Sdim } else if (DestTy->isX86_MMXTy()) { 2592249259Sdim if (SrcTy->isVectorTy()) { 2593249259Sdim return DestBits == SrcBits; // 64-bit vector to MMX 2594249259Sdim } else { 2595249259Sdim return false; 2596249259Sdim } 2597249259Sdim } else { // Casting to something else 2598249259Sdim return false; 2599249259Sdim } 2600249259Sdim} 2601249259Sdim 2602263508Sdimbool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { 2603263508Sdim if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) 2604263508Sdim return false; 2605263508Sdim 2606263508Sdim if (SrcTy == DestTy) 2607263508Sdim return true; 2608263508Sdim 2609263508Sdim if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { 2610263508Sdim if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { 2611263508Sdim if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { 2612263508Sdim // An element by element cast. Valid if casting the elements is valid. 2613263508Sdim SrcTy = SrcVecTy->getElementType(); 2614263508Sdim DestTy = DestVecTy->getElementType(); 2615263508Sdim } 2616263508Sdim } 2617263508Sdim } 2618263508Sdim 2619263508Sdim if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { 2620263508Sdim if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { 2621263508Sdim return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); 2622263508Sdim } 2623263508Sdim } 2624263508Sdim 2625263508Sdim unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 2626263508Sdim unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 2627263508Sdim 2628263508Sdim // Could still have vectors of pointers if the number of elements doesn't 2629263508Sdim // match 2630263508Sdim if (SrcBits == 0 || DestBits == 0) 2631263508Sdim return false; 2632263508Sdim 2633263508Sdim if (SrcBits != DestBits) 2634263508Sdim return false; 2635263508Sdim 2636263508Sdim if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) 2637263508Sdim return false; 2638263508Sdim 2639263508Sdim return true; 2640263508Sdim} 2641263508Sdim 2642263508Sdim// Provide a way to get a "cast" where the cast opcode is inferred from the 2643263508Sdim// types and size of the operand. This, basically, is a parallel of the 2644249259Sdim// logic in the castIsValid function below. This axiom should hold: 2645249259Sdim// castIsValid( getCastOpcode(Val, Ty), Val, Ty) 2646249259Sdim// should not assert in castIsValid. In other words, this produces a "correct" 2647249259Sdim// casting opcode for the arguments passed to it. 2648249259Sdim// This routine must be kept in sync with isCastable. 2649249259SdimInstruction::CastOps 2650249259SdimCastInst::getCastOpcode( 2651249259Sdim const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { 2652249259Sdim Type *SrcTy = Src->getType(); 2653249259Sdim 2654249259Sdim assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && 2655249259Sdim "Only first class types are castable!"); 2656249259Sdim 2657249259Sdim if (SrcTy == DestTy) 2658249259Sdim return BitCast; 2659249259Sdim 2660263508Sdim // FIXME: Check address space sizes here 2661249259Sdim if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) 2662249259Sdim if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) 2663249259Sdim if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { 2664249259Sdim // An element by element cast. Find the appropriate opcode based on the 2665249259Sdim // element types. 2666249259Sdim SrcTy = SrcVecTy->getElementType(); 2667249259Sdim DestTy = DestVecTy->getElementType(); 2668249259Sdim } 2669249259Sdim 2670249259Sdim // Get the bit sizes, we'll need these 2671249259Sdim unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 2672249259Sdim unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 2673249259Sdim 2674249259Sdim // Run through the possibilities ... 2675249259Sdim if (DestTy->isIntegerTy()) { // Casting to integral 2676249259Sdim if (SrcTy->isIntegerTy()) { // Casting from integral 2677249259Sdim if (DestBits < SrcBits) 2678249259Sdim return Trunc; // int -> smaller int 2679249259Sdim else if (DestBits > SrcBits) { // its an extension 2680249259Sdim if (SrcIsSigned) 2681249259Sdim return SExt; // signed -> SEXT 2682249259Sdim else 2683249259Sdim return ZExt; // unsigned -> ZEXT 2684249259Sdim } else { 2685249259Sdim return BitCast; // Same size, No-op cast 2686249259Sdim } 2687249259Sdim } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 2688249259Sdim if (DestIsSigned) 2689249259Sdim return FPToSI; // FP -> sint 2690249259Sdim else 2691249259Sdim return FPToUI; // FP -> uint 2692249259Sdim } else if (SrcTy->isVectorTy()) { 2693249259Sdim assert(DestBits == SrcBits && 2694249259Sdim "Casting vector to integer of different width"); 2695249259Sdim return BitCast; // Same size, no-op cast 2696249259Sdim } else { 2697249259Sdim assert(SrcTy->isPointerTy() && 2698249259Sdim "Casting from a value that is not first-class type"); 2699249259Sdim return PtrToInt; // ptr -> int 2700249259Sdim } 2701249259Sdim } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt 2702249259Sdim if (SrcTy->isIntegerTy()) { // Casting from integral 2703249259Sdim if (SrcIsSigned) 2704249259Sdim return SIToFP; // sint -> FP 2705249259Sdim else 2706249259Sdim return UIToFP; // uint -> FP 2707249259Sdim } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 2708249259Sdim if (DestBits < SrcBits) { 2709249259Sdim return FPTrunc; // FP -> smaller FP 2710249259Sdim } else if (DestBits > SrcBits) { 2711249259Sdim return FPExt; // FP -> larger FP 2712249259Sdim } else { 2713249259Sdim return BitCast; // same size, no-op cast 2714249259Sdim } 2715249259Sdim } else if (SrcTy->isVectorTy()) { 2716249259Sdim assert(DestBits == SrcBits && 2717249259Sdim "Casting vector to floating point of different width"); 2718249259Sdim return BitCast; // same size, no-op cast 2719249259Sdim } 2720249259Sdim llvm_unreachable("Casting pointer or non-first class to float"); 2721249259Sdim } else if (DestTy->isVectorTy()) { 2722249259Sdim assert(DestBits == SrcBits && 2723249259Sdim "Illegal cast to vector (wrong type or size)"); 2724249259Sdim return BitCast; 2725249259Sdim } else if (DestTy->isPointerTy()) { 2726249259Sdim if (SrcTy->isPointerTy()) { 2727263508Sdim if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) 2728263508Sdim return AddrSpaceCast; 2729249259Sdim return BitCast; // ptr -> ptr 2730249259Sdim } else if (SrcTy->isIntegerTy()) { 2731249259Sdim return IntToPtr; // int -> ptr 2732249259Sdim } 2733249259Sdim llvm_unreachable("Casting pointer to other than pointer or int"); 2734249259Sdim } else if (DestTy->isX86_MMXTy()) { 2735249259Sdim if (SrcTy->isVectorTy()) { 2736249259Sdim assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); 2737249259Sdim return BitCast; // 64-bit vector to MMX 2738249259Sdim } 2739249259Sdim llvm_unreachable("Illegal cast to X86_MMX"); 2740249259Sdim } 2741249259Sdim llvm_unreachable("Casting to type that is not first-class"); 2742249259Sdim} 2743249259Sdim 2744249259Sdim//===----------------------------------------------------------------------===// 2745249259Sdim// CastInst SubClass Constructors 2746249259Sdim//===----------------------------------------------------------------------===// 2747249259Sdim 2748249259Sdim/// Check that the construction parameters for a CastInst are correct. This 2749249259Sdim/// could be broken out into the separate constructors but it is useful to have 2750249259Sdim/// it in one place and to eliminate the redundant code for getting the sizes 2751249259Sdim/// of the types involved. 2752249259Sdimbool 2753249259SdimCastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { 2754249259Sdim 2755249259Sdim // Check for type sanity on the arguments 2756249259Sdim Type *SrcTy = S->getType(); 2757249259Sdim 2758249259Sdim // If this is a cast to the same type then it's trivially true. 2759249259Sdim if (SrcTy == DstTy) 2760249259Sdim return true; 2761249259Sdim 2762249259Sdim if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || 2763249259Sdim SrcTy->isAggregateType() || DstTy->isAggregateType()) 2764249259Sdim return false; 2765249259Sdim 2766249259Sdim // Get the size of the types in bits, we'll need this later 2767249259Sdim unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 2768249259Sdim unsigned DstBitSize = DstTy->getScalarSizeInBits(); 2769249259Sdim 2770249259Sdim // If these are vector types, get the lengths of the vectors (using zero for 2771249259Sdim // scalar types means that checking that vector lengths match also checks that 2772249259Sdim // scalars are not being converted to vectors or vectors to scalars). 2773249259Sdim unsigned SrcLength = SrcTy->isVectorTy() ? 2774249259Sdim cast<VectorType>(SrcTy)->getNumElements() : 0; 2775249259Sdim unsigned DstLength = DstTy->isVectorTy() ? 2776249259Sdim cast<VectorType>(DstTy)->getNumElements() : 0; 2777249259Sdim 2778249259Sdim // Switch on the opcode provided 2779249259Sdim switch (op) { 2780249259Sdim default: return false; // This is an input error 2781249259Sdim case Instruction::Trunc: 2782249259Sdim return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 2783249259Sdim SrcLength == DstLength && SrcBitSize > DstBitSize; 2784249259Sdim case Instruction::ZExt: 2785249259Sdim return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 2786249259Sdim SrcLength == DstLength && SrcBitSize < DstBitSize; 2787249259Sdim case Instruction::SExt: 2788249259Sdim return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 2789249259Sdim SrcLength == DstLength && SrcBitSize < DstBitSize; 2790249259Sdim case Instruction::FPTrunc: 2791249259Sdim return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 2792249259Sdim SrcLength == DstLength && SrcBitSize > DstBitSize; 2793249259Sdim case Instruction::FPExt: 2794249259Sdim return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 2795249259Sdim SrcLength == DstLength && SrcBitSize < DstBitSize; 2796249259Sdim case Instruction::UIToFP: 2797249259Sdim case Instruction::SIToFP: 2798249259Sdim return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && 2799249259Sdim SrcLength == DstLength; 2800249259Sdim case Instruction::FPToUI: 2801249259Sdim case Instruction::FPToSI: 2802249259Sdim return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && 2803249259Sdim SrcLength == DstLength; 2804249259Sdim case Instruction::PtrToInt: 2805249259Sdim if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) 2806249259Sdim return false; 2807249259Sdim if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) 2808249259Sdim if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) 2809249259Sdim return false; 2810249259Sdim return SrcTy->getScalarType()->isPointerTy() && 2811249259Sdim DstTy->getScalarType()->isIntegerTy(); 2812249259Sdim case Instruction::IntToPtr: 2813249259Sdim if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) 2814249259Sdim return false; 2815249259Sdim if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) 2816249259Sdim if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) 2817249259Sdim return false; 2818249259Sdim return SrcTy->getScalarType()->isIntegerTy() && 2819249259Sdim DstTy->getScalarType()->isPointerTy(); 2820249259Sdim case Instruction::BitCast: 2821249259Sdim // BitCast implies a no-op cast of type only. No bits change. 2822249259Sdim // However, you can't cast pointers to anything but pointers. 2823263508Sdim if (SrcTy->isPtrOrPtrVectorTy() != DstTy->isPtrOrPtrVectorTy()) 2824249259Sdim return false; 2825249259Sdim 2826263508Sdim // For non pointer cases, the cast is okay if the source and destination bit 2827263508Sdim // widths are identical. 2828263508Sdim if (!SrcTy->isPtrOrPtrVectorTy()) 2829263508Sdim return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); 2830263508Sdim 2831263508Sdim // If both are pointers then the address spaces must match and vector of 2832263508Sdim // pointers must have the same number of elements. 2833263508Sdim return SrcTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 2834263508Sdim SrcTy->isVectorTy() == DstTy->isVectorTy() && 2835263508Sdim (!SrcTy->isVectorTy() || 2836263508Sdim SrcTy->getVectorNumElements() == SrcTy->getVectorNumElements()); 2837263508Sdim 2838263508Sdim case Instruction::AddrSpaceCast: 2839263508Sdim return SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() && 2840263508Sdim SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace() && 2841263508Sdim SrcTy->isVectorTy() == DstTy->isVectorTy() && 2842263508Sdim (!SrcTy->isVectorTy() || 2843263508Sdim SrcTy->getVectorNumElements() == SrcTy->getVectorNumElements()); 2844249259Sdim } 2845249259Sdim} 2846249259Sdim 2847249259SdimTruncInst::TruncInst( 2848249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2849249259Sdim) : CastInst(Ty, Trunc, S, Name, InsertBefore) { 2850249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 2851249259Sdim} 2852249259Sdim 2853249259SdimTruncInst::TruncInst( 2854249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2855249259Sdim) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { 2856249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 2857249259Sdim} 2858249259Sdim 2859249259SdimZExtInst::ZExtInst( 2860249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2861249259Sdim) : CastInst(Ty, ZExt, S, Name, InsertBefore) { 2862249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 2863249259Sdim} 2864249259Sdim 2865249259SdimZExtInst::ZExtInst( 2866249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2867249259Sdim) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { 2868249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 2869249259Sdim} 2870249259SdimSExtInst::SExtInst( 2871249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2872249259Sdim) : CastInst(Ty, SExt, S, Name, InsertBefore) { 2873249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 2874249259Sdim} 2875249259Sdim 2876249259SdimSExtInst::SExtInst( 2877249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2878249259Sdim) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { 2879249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 2880249259Sdim} 2881249259Sdim 2882249259SdimFPTruncInst::FPTruncInst( 2883249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2884249259Sdim) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 2885249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 2886249259Sdim} 2887249259Sdim 2888249259SdimFPTruncInst::FPTruncInst( 2889249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2890249259Sdim) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { 2891249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 2892249259Sdim} 2893249259Sdim 2894249259SdimFPExtInst::FPExtInst( 2895249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2896249259Sdim) : CastInst(Ty, FPExt, S, Name, InsertBefore) { 2897249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 2898249259Sdim} 2899249259Sdim 2900249259SdimFPExtInst::FPExtInst( 2901249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2902249259Sdim) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { 2903249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 2904249259Sdim} 2905249259Sdim 2906249259SdimUIToFPInst::UIToFPInst( 2907249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2908249259Sdim) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 2909249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 2910249259Sdim} 2911249259Sdim 2912249259SdimUIToFPInst::UIToFPInst( 2913249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2914249259Sdim) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { 2915249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 2916249259Sdim} 2917249259Sdim 2918249259SdimSIToFPInst::SIToFPInst( 2919249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2920249259Sdim) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 2921249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 2922249259Sdim} 2923249259Sdim 2924249259SdimSIToFPInst::SIToFPInst( 2925249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2926249259Sdim) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { 2927249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 2928249259Sdim} 2929249259Sdim 2930249259SdimFPToUIInst::FPToUIInst( 2931249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2932249259Sdim) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 2933249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 2934249259Sdim} 2935249259Sdim 2936249259SdimFPToUIInst::FPToUIInst( 2937249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2938249259Sdim) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { 2939249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 2940249259Sdim} 2941249259Sdim 2942249259SdimFPToSIInst::FPToSIInst( 2943249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2944249259Sdim) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 2945249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 2946249259Sdim} 2947249259Sdim 2948249259SdimFPToSIInst::FPToSIInst( 2949249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2950249259Sdim) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { 2951249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 2952249259Sdim} 2953249259Sdim 2954249259SdimPtrToIntInst::PtrToIntInst( 2955249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2956249259Sdim) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 2957249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 2958249259Sdim} 2959249259Sdim 2960249259SdimPtrToIntInst::PtrToIntInst( 2961249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2962249259Sdim) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { 2963249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 2964249259Sdim} 2965249259Sdim 2966249259SdimIntToPtrInst::IntToPtrInst( 2967249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2968249259Sdim) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 2969249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 2970249259Sdim} 2971249259Sdim 2972249259SdimIntToPtrInst::IntToPtrInst( 2973249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2974249259Sdim) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { 2975249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 2976249259Sdim} 2977249259Sdim 2978249259SdimBitCastInst::BitCastInst( 2979249259Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2980249259Sdim) : CastInst(Ty, BitCast, S, Name, InsertBefore) { 2981249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 2982249259Sdim} 2983249259Sdim 2984249259SdimBitCastInst::BitCastInst( 2985249259Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2986249259Sdim) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { 2987249259Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 2988249259Sdim} 2989249259Sdim 2990263508SdimAddrSpaceCastInst::AddrSpaceCastInst( 2991263508Sdim Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore 2992263508Sdim) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { 2993263508Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 2994263508Sdim} 2995263508Sdim 2996263508SdimAddrSpaceCastInst::AddrSpaceCastInst( 2997263508Sdim Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd 2998263508Sdim) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { 2999263508Sdim assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3000263508Sdim} 3001263508Sdim 3002249259Sdim//===----------------------------------------------------------------------===// 3003249259Sdim// CmpInst Classes 3004249259Sdim//===----------------------------------------------------------------------===// 3005249259Sdim 3006249259Sdimvoid CmpInst::anchor() {} 3007249259Sdim 3008249259SdimCmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, 3009249259Sdim Value *LHS, Value *RHS, const Twine &Name, 3010249259Sdim Instruction *InsertBefore) 3011249259Sdim : Instruction(ty, op, 3012249259Sdim OperandTraits<CmpInst>::op_begin(this), 3013249259Sdim OperandTraits<CmpInst>::operands(this), 3014249259Sdim InsertBefore) { 3015249259Sdim Op<0>() = LHS; 3016249259Sdim Op<1>() = RHS; 3017249259Sdim setPredicate((Predicate)predicate); 3018249259Sdim setName(Name); 3019249259Sdim} 3020249259Sdim 3021249259SdimCmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, 3022249259Sdim Value *LHS, Value *RHS, const Twine &Name, 3023249259Sdim BasicBlock *InsertAtEnd) 3024249259Sdim : Instruction(ty, op, 3025249259Sdim OperandTraits<CmpInst>::op_begin(this), 3026249259Sdim OperandTraits<CmpInst>::operands(this), 3027249259Sdim InsertAtEnd) { 3028249259Sdim Op<0>() = LHS; 3029249259Sdim Op<1>() = RHS; 3030249259Sdim setPredicate((Predicate)predicate); 3031249259Sdim setName(Name); 3032249259Sdim} 3033249259Sdim 3034249259SdimCmpInst * 3035249259SdimCmpInst::Create(OtherOps Op, unsigned short predicate, 3036249259Sdim Value *S1, Value *S2, 3037249259Sdim const Twine &Name, Instruction *InsertBefore) { 3038249259Sdim if (Op == Instruction::ICmp) { 3039249259Sdim if (InsertBefore) 3040249259Sdim return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), 3041249259Sdim S1, S2, Name); 3042249259Sdim else 3043249259Sdim return new ICmpInst(CmpInst::Predicate(predicate), 3044249259Sdim S1, S2, Name); 3045249259Sdim } 3046249259Sdim 3047249259Sdim if (InsertBefore) 3048249259Sdim return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), 3049249259Sdim S1, S2, Name); 3050249259Sdim else 3051249259Sdim return new FCmpInst(CmpInst::Predicate(predicate), 3052249259Sdim S1, S2, Name); 3053249259Sdim} 3054249259Sdim 3055249259SdimCmpInst * 3056249259SdimCmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, 3057249259Sdim const Twine &Name, BasicBlock *InsertAtEnd) { 3058249259Sdim if (Op == Instruction::ICmp) { 3059249259Sdim return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3060249259Sdim S1, S2, Name); 3061249259Sdim } 3062249259Sdim return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), 3063249259Sdim S1, S2, Name); 3064249259Sdim} 3065249259Sdim 3066249259Sdimvoid CmpInst::swapOperands() { 3067249259Sdim if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3068249259Sdim IC->swapOperands(); 3069249259Sdim else 3070249259Sdim cast<FCmpInst>(this)->swapOperands(); 3071249259Sdim} 3072249259Sdim 3073249259Sdimbool CmpInst::isCommutative() const { 3074249259Sdim if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3075249259Sdim return IC->isCommutative(); 3076249259Sdim return cast<FCmpInst>(this)->isCommutative(); 3077249259Sdim} 3078249259Sdim 3079249259Sdimbool CmpInst::isEquality() const { 3080249259Sdim if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3081249259Sdim return IC->isEquality(); 3082249259Sdim return cast<FCmpInst>(this)->isEquality(); 3083249259Sdim} 3084249259Sdim 3085249259Sdim 3086249259SdimCmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { 3087249259Sdim switch (pred) { 3088249259Sdim default: llvm_unreachable("Unknown cmp predicate!"); 3089249259Sdim case ICMP_EQ: return ICMP_NE; 3090249259Sdim case ICMP_NE: return ICMP_EQ; 3091249259Sdim case ICMP_UGT: return ICMP_ULE; 3092249259Sdim case ICMP_ULT: return ICMP_UGE; 3093249259Sdim case ICMP_UGE: return ICMP_ULT; 3094249259Sdim case ICMP_ULE: return ICMP_UGT; 3095249259Sdim case ICMP_SGT: return ICMP_SLE; 3096249259Sdim case ICMP_SLT: return ICMP_SGE; 3097249259Sdim case ICMP_SGE: return ICMP_SLT; 3098249259Sdim case ICMP_SLE: return ICMP_SGT; 3099249259Sdim 3100249259Sdim case FCMP_OEQ: return FCMP_UNE; 3101249259Sdim case FCMP_ONE: return FCMP_UEQ; 3102249259Sdim case FCMP_OGT: return FCMP_ULE; 3103249259Sdim case FCMP_OLT: return FCMP_UGE; 3104249259Sdim case FCMP_OGE: return FCMP_ULT; 3105249259Sdim case FCMP_OLE: return FCMP_UGT; 3106249259Sdim case FCMP_UEQ: return FCMP_ONE; 3107249259Sdim case FCMP_UNE: return FCMP_OEQ; 3108249259Sdim case FCMP_UGT: return FCMP_OLE; 3109249259Sdim case FCMP_ULT: return FCMP_OGE; 3110249259Sdim case FCMP_UGE: return FCMP_OLT; 3111249259Sdim case FCMP_ULE: return FCMP_OGT; 3112249259Sdim case FCMP_ORD: return FCMP_UNO; 3113249259Sdim case FCMP_UNO: return FCMP_ORD; 3114249259Sdim case FCMP_TRUE: return FCMP_FALSE; 3115249259Sdim case FCMP_FALSE: return FCMP_TRUE; 3116249259Sdim } 3117249259Sdim} 3118249259Sdim 3119249259SdimICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { 3120249259Sdim switch (pred) { 3121249259Sdim default: llvm_unreachable("Unknown icmp predicate!"); 3122249259Sdim case ICMP_EQ: case ICMP_NE: 3123249259Sdim case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 3124249259Sdim return pred; 3125249259Sdim case ICMP_UGT: return ICMP_SGT; 3126249259Sdim case ICMP_ULT: return ICMP_SLT; 3127249259Sdim case ICMP_UGE: return ICMP_SGE; 3128249259Sdim case ICMP_ULE: return ICMP_SLE; 3129249259Sdim } 3130249259Sdim} 3131249259Sdim 3132249259SdimICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { 3133249259Sdim switch (pred) { 3134249259Sdim default: llvm_unreachable("Unknown icmp predicate!"); 3135249259Sdim case ICMP_EQ: case ICMP_NE: 3136249259Sdim case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: 3137249259Sdim return pred; 3138249259Sdim case ICMP_SGT: return ICMP_UGT; 3139249259Sdim case ICMP_SLT: return ICMP_ULT; 3140249259Sdim case ICMP_SGE: return ICMP_UGE; 3141249259Sdim case ICMP_SLE: return ICMP_ULE; 3142249259Sdim } 3143249259Sdim} 3144249259Sdim 3145249259Sdim/// Initialize a set of values that all satisfy the condition with C. 3146249259Sdim/// 3147249259SdimConstantRange 3148249259SdimICmpInst::makeConstantRange(Predicate pred, const APInt &C) { 3149249259Sdim APInt Lower(C); 3150249259Sdim APInt Upper(C); 3151249259Sdim uint32_t BitWidth = C.getBitWidth(); 3152249259Sdim switch (pred) { 3153249259Sdim default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); 3154249259Sdim case ICmpInst::ICMP_EQ: ++Upper; break; 3155249259Sdim case ICmpInst::ICMP_NE: ++Lower; break; 3156249259Sdim case ICmpInst::ICMP_ULT: 3157249259Sdim Lower = APInt::getMinValue(BitWidth); 3158249259Sdim // Check for an empty-set condition. 3159249259Sdim if (Lower == Upper) 3160249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/false); 3161249259Sdim break; 3162249259Sdim case ICmpInst::ICMP_SLT: 3163249259Sdim Lower = APInt::getSignedMinValue(BitWidth); 3164249259Sdim // Check for an empty-set condition. 3165249259Sdim if (Lower == Upper) 3166249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/false); 3167249259Sdim break; 3168249259Sdim case ICmpInst::ICMP_UGT: 3169249259Sdim ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) 3170249259Sdim // Check for an empty-set condition. 3171249259Sdim if (Lower == Upper) 3172249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/false); 3173249259Sdim break; 3174249259Sdim case ICmpInst::ICMP_SGT: 3175249259Sdim ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) 3176249259Sdim // Check for an empty-set condition. 3177249259Sdim if (Lower == Upper) 3178249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/false); 3179249259Sdim break; 3180249259Sdim case ICmpInst::ICMP_ULE: 3181249259Sdim Lower = APInt::getMinValue(BitWidth); ++Upper; 3182249259Sdim // Check for a full-set condition. 3183249259Sdim if (Lower == Upper) 3184249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/true); 3185249259Sdim break; 3186249259Sdim case ICmpInst::ICMP_SLE: 3187249259Sdim Lower = APInt::getSignedMinValue(BitWidth); ++Upper; 3188249259Sdim // Check for a full-set condition. 3189249259Sdim if (Lower == Upper) 3190249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/true); 3191249259Sdim break; 3192249259Sdim case ICmpInst::ICMP_UGE: 3193249259Sdim Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) 3194249259Sdim // Check for a full-set condition. 3195249259Sdim if (Lower == Upper) 3196249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/true); 3197249259Sdim break; 3198249259Sdim case ICmpInst::ICMP_SGE: 3199249259Sdim Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) 3200249259Sdim // Check for a full-set condition. 3201249259Sdim if (Lower == Upper) 3202249259Sdim return ConstantRange(BitWidth, /*isFullSet=*/true); 3203249259Sdim break; 3204249259Sdim } 3205249259Sdim return ConstantRange(Lower, Upper); 3206249259Sdim} 3207249259Sdim 3208249259SdimCmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { 3209249259Sdim switch (pred) { 3210249259Sdim default: llvm_unreachable("Unknown cmp predicate!"); 3211249259Sdim case ICMP_EQ: case ICMP_NE: 3212249259Sdim return pred; 3213249259Sdim case ICMP_SGT: return ICMP_SLT; 3214249259Sdim case ICMP_SLT: return ICMP_SGT; 3215249259Sdim case ICMP_SGE: return ICMP_SLE; 3216249259Sdim case ICMP_SLE: return ICMP_SGE; 3217249259Sdim case ICMP_UGT: return ICMP_ULT; 3218249259Sdim case ICMP_ULT: return ICMP_UGT; 3219249259Sdim case ICMP_UGE: return ICMP_ULE; 3220249259Sdim case ICMP_ULE: return ICMP_UGE; 3221249259Sdim 3222249259Sdim case FCMP_FALSE: case FCMP_TRUE: 3223249259Sdim case FCMP_OEQ: case FCMP_ONE: 3224249259Sdim case FCMP_UEQ: case FCMP_UNE: 3225249259Sdim case FCMP_ORD: case FCMP_UNO: 3226249259Sdim return pred; 3227249259Sdim case FCMP_OGT: return FCMP_OLT; 3228249259Sdim case FCMP_OLT: return FCMP_OGT; 3229249259Sdim case FCMP_OGE: return FCMP_OLE; 3230249259Sdim case FCMP_OLE: return FCMP_OGE; 3231249259Sdim case FCMP_UGT: return FCMP_ULT; 3232249259Sdim case FCMP_ULT: return FCMP_UGT; 3233249259Sdim case FCMP_UGE: return FCMP_ULE; 3234249259Sdim case FCMP_ULE: return FCMP_UGE; 3235249259Sdim } 3236249259Sdim} 3237249259Sdim 3238249259Sdimbool CmpInst::isUnsigned(unsigned short predicate) { 3239249259Sdim switch (predicate) { 3240249259Sdim default: return false; 3241249259Sdim case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 3242249259Sdim case ICmpInst::ICMP_UGE: return true; 3243249259Sdim } 3244249259Sdim} 3245249259Sdim 3246249259Sdimbool CmpInst::isSigned(unsigned short predicate) { 3247249259Sdim switch (predicate) { 3248249259Sdim default: return false; 3249249259Sdim case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 3250249259Sdim case ICmpInst::ICMP_SGE: return true; 3251249259Sdim } 3252249259Sdim} 3253249259Sdim 3254249259Sdimbool CmpInst::isOrdered(unsigned short predicate) { 3255249259Sdim switch (predicate) { 3256249259Sdim default: return false; 3257249259Sdim case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 3258249259Sdim case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 3259249259Sdim case FCmpInst::FCMP_ORD: return true; 3260249259Sdim } 3261249259Sdim} 3262249259Sdim 3263249259Sdimbool CmpInst::isUnordered(unsigned short predicate) { 3264249259Sdim switch (predicate) { 3265249259Sdim default: return false; 3266249259Sdim case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 3267249259Sdim case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 3268249259Sdim case FCmpInst::FCMP_UNO: return true; 3269249259Sdim } 3270249259Sdim} 3271249259Sdim 3272249259Sdimbool CmpInst::isTrueWhenEqual(unsigned short predicate) { 3273249259Sdim switch(predicate) { 3274249259Sdim default: return false; 3275249259Sdim case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: 3276249259Sdim case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; 3277249259Sdim } 3278249259Sdim} 3279249259Sdim 3280249259Sdimbool CmpInst::isFalseWhenEqual(unsigned short predicate) { 3281249259Sdim switch(predicate) { 3282249259Sdim case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: 3283249259Sdim case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; 3284249259Sdim default: return false; 3285249259Sdim } 3286249259Sdim} 3287249259Sdim 3288249259Sdim 3289249259Sdim//===----------------------------------------------------------------------===// 3290249259Sdim// SwitchInst Implementation 3291249259Sdim//===----------------------------------------------------------------------===// 3292249259Sdim 3293249259Sdimvoid SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { 3294249259Sdim assert(Value && Default && NumReserved); 3295249259Sdim ReservedSpace = NumReserved; 3296249259Sdim NumOperands = 2; 3297249259Sdim OperandList = allocHungoffUses(ReservedSpace); 3298249259Sdim 3299249259Sdim OperandList[0] = Value; 3300249259Sdim OperandList[1] = Default; 3301249259Sdim} 3302249259Sdim 3303249259Sdim/// SwitchInst ctor - Create a new switch instruction, specifying a value to 3304249259Sdim/// switch on and a default destination. The number of additional cases can 3305249259Sdim/// be specified here to make memory allocation more efficient. This 3306249259Sdim/// constructor can also autoinsert before another instruction. 3307249259SdimSwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 3308249259Sdim Instruction *InsertBefore) 3309249259Sdim : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, 3310249259Sdim 0, 0, InsertBefore) { 3311249259Sdim init(Value, Default, 2+NumCases*2); 3312249259Sdim} 3313249259Sdim 3314249259Sdim/// SwitchInst ctor - Create a new switch instruction, specifying a value to 3315249259Sdim/// switch on and a default destination. The number of additional cases can 3316249259Sdim/// be specified here to make memory allocation more efficient. This 3317249259Sdim/// constructor also autoinserts at the end of the specified BasicBlock. 3318249259SdimSwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 3319249259Sdim BasicBlock *InsertAtEnd) 3320249259Sdim : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, 3321249259Sdim 0, 0, InsertAtEnd) { 3322249259Sdim init(Value, Default, 2+NumCases*2); 3323249259Sdim} 3324249259Sdim 3325249259SdimSwitchInst::SwitchInst(const SwitchInst &SI) 3326249259Sdim : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) { 3327249259Sdim init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); 3328249259Sdim NumOperands = SI.getNumOperands(); 3329249259Sdim Use *OL = OperandList, *InOL = SI.OperandList; 3330249259Sdim for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { 3331249259Sdim OL[i] = InOL[i]; 3332249259Sdim OL[i+1] = InOL[i+1]; 3333249259Sdim } 3334249259Sdim SubclassOptionalData = SI.SubclassOptionalData; 3335249259Sdim} 3336249259Sdim 3337249259SdimSwitchInst::~SwitchInst() { 3338249259Sdim dropHungoffUses(); 3339249259Sdim} 3340249259Sdim 3341249259Sdim 3342249259Sdim/// addCase - Add an entry to the switch instruction... 3343249259Sdim/// 3344249259Sdimvoid SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { 3345249259Sdim unsigned NewCaseIdx = getNumCases(); 3346249259Sdim unsigned OpNo = NumOperands; 3347249259Sdim if (OpNo+2 > ReservedSpace) 3348249259Sdim growOperands(); // Get more space! 3349249259Sdim // Initialize some new operands. 3350249259Sdim assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); 3351249259Sdim NumOperands = OpNo+2; 3352263508Sdim CaseIt Case(this, NewCaseIdx); 3353263508Sdim Case.setValue(OnVal); 3354249259Sdim Case.setSuccessor(Dest); 3355249259Sdim} 3356249259Sdim 3357249259Sdim/// removeCase - This method removes the specified case and its successor 3358249259Sdim/// from the switch instruction. 3359263508Sdimvoid SwitchInst::removeCase(CaseIt i) { 3360249259Sdim unsigned idx = i.getCaseIndex(); 3361249259Sdim 3362249259Sdim assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); 3363249259Sdim 3364249259Sdim unsigned NumOps = getNumOperands(); 3365249259Sdim Use *OL = OperandList; 3366249259Sdim 3367249259Sdim // Overwrite this case with the end of the list. 3368249259Sdim if (2 + (idx + 1) * 2 != NumOps) { 3369249259Sdim OL[2 + idx * 2] = OL[NumOps - 2]; 3370249259Sdim OL[2 + idx * 2 + 1] = OL[NumOps - 1]; 3371249259Sdim } 3372249259Sdim 3373249259Sdim // Nuke the last value. 3374249259Sdim OL[NumOps-2].set(0); 3375249259Sdim OL[NumOps-2+1].set(0); 3376249259Sdim NumOperands = NumOps-2; 3377249259Sdim} 3378249259Sdim 3379249259Sdim/// growOperands - grow operands - This grows the operand list in response 3380249259Sdim/// to a push_back style of operation. This grows the number of ops by 3 times. 3381249259Sdim/// 3382249259Sdimvoid SwitchInst::growOperands() { 3383249259Sdim unsigned e = getNumOperands(); 3384249259Sdim unsigned NumOps = e*3; 3385249259Sdim 3386249259Sdim ReservedSpace = NumOps; 3387249259Sdim Use *NewOps = allocHungoffUses(NumOps); 3388249259Sdim Use *OldOps = OperandList; 3389249259Sdim for (unsigned i = 0; i != e; ++i) { 3390249259Sdim NewOps[i] = OldOps[i]; 3391249259Sdim } 3392249259Sdim OperandList = NewOps; 3393249259Sdim Use::zap(OldOps, OldOps + e, true); 3394249259Sdim} 3395249259Sdim 3396249259Sdim 3397249259SdimBasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { 3398249259Sdim return getSuccessor(idx); 3399249259Sdim} 3400249259Sdimunsigned SwitchInst::getNumSuccessorsV() const { 3401249259Sdim return getNumSuccessors(); 3402249259Sdim} 3403249259Sdimvoid SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { 3404249259Sdim setSuccessor(idx, B); 3405249259Sdim} 3406249259Sdim 3407249259Sdim//===----------------------------------------------------------------------===// 3408249259Sdim// IndirectBrInst Implementation 3409249259Sdim//===----------------------------------------------------------------------===// 3410249259Sdim 3411249259Sdimvoid IndirectBrInst::init(Value *Address, unsigned NumDests) { 3412249259Sdim assert(Address && Address->getType()->isPointerTy() && 3413249259Sdim "Address of indirectbr must be a pointer"); 3414249259Sdim ReservedSpace = 1+NumDests; 3415249259Sdim NumOperands = 1; 3416249259Sdim OperandList = allocHungoffUses(ReservedSpace); 3417249259Sdim 3418249259Sdim OperandList[0] = Address; 3419249259Sdim} 3420249259Sdim 3421249259Sdim 3422249259Sdim/// growOperands - grow operands - This grows the operand list in response 3423249259Sdim/// to a push_back style of operation. This grows the number of ops by 2 times. 3424249259Sdim/// 3425249259Sdimvoid IndirectBrInst::growOperands() { 3426249259Sdim unsigned e = getNumOperands(); 3427249259Sdim unsigned NumOps = e*2; 3428249259Sdim 3429249259Sdim ReservedSpace = NumOps; 3430249259Sdim Use *NewOps = allocHungoffUses(NumOps); 3431249259Sdim Use *OldOps = OperandList; 3432249259Sdim for (unsigned i = 0; i != e; ++i) 3433249259Sdim NewOps[i] = OldOps[i]; 3434249259Sdim OperandList = NewOps; 3435249259Sdim Use::zap(OldOps, OldOps + e, true); 3436249259Sdim} 3437249259Sdim 3438249259SdimIndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, 3439249259Sdim Instruction *InsertBefore) 3440249259Sdim: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, 3441249259Sdim 0, 0, InsertBefore) { 3442249259Sdim init(Address, NumCases); 3443249259Sdim} 3444249259Sdim 3445249259SdimIndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, 3446249259Sdim BasicBlock *InsertAtEnd) 3447249259Sdim: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, 3448249259Sdim 0, 0, InsertAtEnd) { 3449249259Sdim init(Address, NumCases); 3450249259Sdim} 3451249259Sdim 3452249259SdimIndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) 3453249259Sdim : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, 3454249259Sdim allocHungoffUses(IBI.getNumOperands()), 3455249259Sdim IBI.getNumOperands()) { 3456249259Sdim Use *OL = OperandList, *InOL = IBI.OperandList; 3457249259Sdim for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) 3458249259Sdim OL[i] = InOL[i]; 3459249259Sdim SubclassOptionalData = IBI.SubclassOptionalData; 3460249259Sdim} 3461249259Sdim 3462249259SdimIndirectBrInst::~IndirectBrInst() { 3463249259Sdim dropHungoffUses(); 3464249259Sdim} 3465249259Sdim 3466249259Sdim/// addDestination - Add a destination. 3467249259Sdim/// 3468249259Sdimvoid IndirectBrInst::addDestination(BasicBlock *DestBB) { 3469249259Sdim unsigned OpNo = NumOperands; 3470249259Sdim if (OpNo+1 > ReservedSpace) 3471249259Sdim growOperands(); // Get more space! 3472249259Sdim // Initialize some new operands. 3473249259Sdim assert(OpNo < ReservedSpace && "Growing didn't work!"); 3474249259Sdim NumOperands = OpNo+1; 3475249259Sdim OperandList[OpNo] = DestBB; 3476249259Sdim} 3477249259Sdim 3478249259Sdim/// removeDestination - This method removes the specified successor from the 3479249259Sdim/// indirectbr instruction. 3480249259Sdimvoid IndirectBrInst::removeDestination(unsigned idx) { 3481249259Sdim assert(idx < getNumOperands()-1 && "Successor index out of range!"); 3482249259Sdim 3483249259Sdim unsigned NumOps = getNumOperands(); 3484249259Sdim Use *OL = OperandList; 3485249259Sdim 3486249259Sdim // Replace this value with the last one. 3487249259Sdim OL[idx+1] = OL[NumOps-1]; 3488249259Sdim 3489249259Sdim // Nuke the last value. 3490249259Sdim OL[NumOps-1].set(0); 3491249259Sdim NumOperands = NumOps-1; 3492249259Sdim} 3493249259Sdim 3494249259SdimBasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { 3495249259Sdim return getSuccessor(idx); 3496249259Sdim} 3497249259Sdimunsigned IndirectBrInst::getNumSuccessorsV() const { 3498249259Sdim return getNumSuccessors(); 3499249259Sdim} 3500249259Sdimvoid IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { 3501249259Sdim setSuccessor(idx, B); 3502249259Sdim} 3503249259Sdim 3504249259Sdim//===----------------------------------------------------------------------===// 3505249259Sdim// clone_impl() implementations 3506249259Sdim//===----------------------------------------------------------------------===// 3507249259Sdim 3508249259Sdim// Define these methods here so vtables don't get emitted into every translation 3509249259Sdim// unit that uses these classes. 3510249259Sdim 3511249259SdimGetElementPtrInst *GetElementPtrInst::clone_impl() const { 3512249259Sdim return new (getNumOperands()) GetElementPtrInst(*this); 3513249259Sdim} 3514249259Sdim 3515249259SdimBinaryOperator *BinaryOperator::clone_impl() const { 3516249259Sdim return Create(getOpcode(), Op<0>(), Op<1>()); 3517249259Sdim} 3518249259Sdim 3519249259SdimFCmpInst* FCmpInst::clone_impl() const { 3520249259Sdim return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); 3521249259Sdim} 3522249259Sdim 3523249259SdimICmpInst* ICmpInst::clone_impl() const { 3524249259Sdim return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); 3525249259Sdim} 3526249259Sdim 3527249259SdimExtractValueInst *ExtractValueInst::clone_impl() const { 3528249259Sdim return new ExtractValueInst(*this); 3529249259Sdim} 3530249259Sdim 3531249259SdimInsertValueInst *InsertValueInst::clone_impl() const { 3532249259Sdim return new InsertValueInst(*this); 3533249259Sdim} 3534249259Sdim 3535249259SdimAllocaInst *AllocaInst::clone_impl() const { 3536249259Sdim return new AllocaInst(getAllocatedType(), 3537249259Sdim (Value*)getOperand(0), 3538249259Sdim getAlignment()); 3539249259Sdim} 3540249259Sdim 3541249259SdimLoadInst *LoadInst::clone_impl() const { 3542249259Sdim return new LoadInst(getOperand(0), Twine(), isVolatile(), 3543249259Sdim getAlignment(), getOrdering(), getSynchScope()); 3544249259Sdim} 3545249259Sdim 3546249259SdimStoreInst *StoreInst::clone_impl() const { 3547249259Sdim return new StoreInst(getOperand(0), getOperand(1), isVolatile(), 3548249259Sdim getAlignment(), getOrdering(), getSynchScope()); 3549249259Sdim 3550249259Sdim} 3551249259Sdim 3552249259SdimAtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const { 3553249259Sdim AtomicCmpXchgInst *Result = 3554249259Sdim new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), 3555249259Sdim getOrdering(), getSynchScope()); 3556249259Sdim Result->setVolatile(isVolatile()); 3557249259Sdim return Result; 3558249259Sdim} 3559249259Sdim 3560249259SdimAtomicRMWInst *AtomicRMWInst::clone_impl() const { 3561249259Sdim AtomicRMWInst *Result = 3562249259Sdim new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), 3563249259Sdim getOrdering(), getSynchScope()); 3564249259Sdim Result->setVolatile(isVolatile()); 3565249259Sdim return Result; 3566249259Sdim} 3567249259Sdim 3568249259SdimFenceInst *FenceInst::clone_impl() const { 3569249259Sdim return new FenceInst(getContext(), getOrdering(), getSynchScope()); 3570249259Sdim} 3571249259Sdim 3572249259SdimTruncInst *TruncInst::clone_impl() const { 3573249259Sdim return new TruncInst(getOperand(0), getType()); 3574249259Sdim} 3575249259Sdim 3576249259SdimZExtInst *ZExtInst::clone_impl() const { 3577249259Sdim return new ZExtInst(getOperand(0), getType()); 3578249259Sdim} 3579249259Sdim 3580249259SdimSExtInst *SExtInst::clone_impl() const { 3581249259Sdim return new SExtInst(getOperand(0), getType()); 3582249259Sdim} 3583249259Sdim 3584249259SdimFPTruncInst *FPTruncInst::clone_impl() const { 3585249259Sdim return new FPTruncInst(getOperand(0), getType()); 3586249259Sdim} 3587249259Sdim 3588249259SdimFPExtInst *FPExtInst::clone_impl() const { 3589249259Sdim return new FPExtInst(getOperand(0), getType()); 3590249259Sdim} 3591249259Sdim 3592249259SdimUIToFPInst *UIToFPInst::clone_impl() const { 3593249259Sdim return new UIToFPInst(getOperand(0), getType()); 3594249259Sdim} 3595249259Sdim 3596249259SdimSIToFPInst *SIToFPInst::clone_impl() const { 3597249259Sdim return new SIToFPInst(getOperand(0), getType()); 3598249259Sdim} 3599249259Sdim 3600249259SdimFPToUIInst *FPToUIInst::clone_impl() const { 3601249259Sdim return new FPToUIInst(getOperand(0), getType()); 3602249259Sdim} 3603249259Sdim 3604249259SdimFPToSIInst *FPToSIInst::clone_impl() const { 3605249259Sdim return new FPToSIInst(getOperand(0), getType()); 3606249259Sdim} 3607249259Sdim 3608249259SdimPtrToIntInst *PtrToIntInst::clone_impl() const { 3609249259Sdim return new PtrToIntInst(getOperand(0), getType()); 3610249259Sdim} 3611249259Sdim 3612249259SdimIntToPtrInst *IntToPtrInst::clone_impl() const { 3613249259Sdim return new IntToPtrInst(getOperand(0), getType()); 3614249259Sdim} 3615249259Sdim 3616249259SdimBitCastInst *BitCastInst::clone_impl() const { 3617249259Sdim return new BitCastInst(getOperand(0), getType()); 3618249259Sdim} 3619249259Sdim 3620263508SdimAddrSpaceCastInst *AddrSpaceCastInst::clone_impl() const { 3621263508Sdim return new AddrSpaceCastInst(getOperand(0), getType()); 3622263508Sdim} 3623263508Sdim 3624249259SdimCallInst *CallInst::clone_impl() const { 3625249259Sdim return new(getNumOperands()) CallInst(*this); 3626249259Sdim} 3627249259Sdim 3628249259SdimSelectInst *SelectInst::clone_impl() const { 3629249259Sdim return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); 3630249259Sdim} 3631249259Sdim 3632249259SdimVAArgInst *VAArgInst::clone_impl() const { 3633249259Sdim return new VAArgInst(getOperand(0), getType()); 3634249259Sdim} 3635249259Sdim 3636249259SdimExtractElementInst *ExtractElementInst::clone_impl() const { 3637249259Sdim return ExtractElementInst::Create(getOperand(0), getOperand(1)); 3638249259Sdim} 3639249259Sdim 3640249259SdimInsertElementInst *InsertElementInst::clone_impl() const { 3641249259Sdim return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); 3642249259Sdim} 3643249259Sdim 3644249259SdimShuffleVectorInst *ShuffleVectorInst::clone_impl() const { 3645249259Sdim return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); 3646249259Sdim} 3647249259Sdim 3648249259SdimPHINode *PHINode::clone_impl() const { 3649249259Sdim return new PHINode(*this); 3650249259Sdim} 3651249259Sdim 3652249259SdimLandingPadInst *LandingPadInst::clone_impl() const { 3653249259Sdim return new LandingPadInst(*this); 3654249259Sdim} 3655249259Sdim 3656249259SdimReturnInst *ReturnInst::clone_impl() const { 3657249259Sdim return new(getNumOperands()) ReturnInst(*this); 3658249259Sdim} 3659249259Sdim 3660249259SdimBranchInst *BranchInst::clone_impl() const { 3661249259Sdim return new(getNumOperands()) BranchInst(*this); 3662249259Sdim} 3663249259Sdim 3664249259SdimSwitchInst *SwitchInst::clone_impl() const { 3665249259Sdim return new SwitchInst(*this); 3666249259Sdim} 3667249259Sdim 3668249259SdimIndirectBrInst *IndirectBrInst::clone_impl() const { 3669249259Sdim return new IndirectBrInst(*this); 3670249259Sdim} 3671249259Sdim 3672249259Sdim 3673249259SdimInvokeInst *InvokeInst::clone_impl() const { 3674249259Sdim return new(getNumOperands()) InvokeInst(*this); 3675249259Sdim} 3676249259Sdim 3677249259SdimResumeInst *ResumeInst::clone_impl() const { 3678249259Sdim return new(1) ResumeInst(*this); 3679249259Sdim} 3680249259Sdim 3681249259SdimUnreachableInst *UnreachableInst::clone_impl() const { 3682249259Sdim LLVMContext &Context = getContext(); 3683249259Sdim return new UnreachableInst(Context); 3684249259Sdim} 3685