LLVM API Documentation
00001 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements all of the non-inline methods for the LLVM instruction 00011 // classes. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Constants.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/Function.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Support/CallSite.h" 00020 #include "llvm/Support/ConstantRange.h" 00021 #include "llvm/Support/MathExtras.h" 00022 using namespace llvm; 00023 00024 //===----------------------------------------------------------------------===// 00025 // CallSite Class 00026 //===----------------------------------------------------------------------===// 00027 00028 CallSite::CallSite(Instruction *C) { 00029 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!"); 00030 I = C; 00031 } 00032 unsigned CallSite::getCallingConv() const { 00033 if (CallInst *CI = dyn_cast<CallInst>(I)) 00034 return CI->getCallingConv(); 00035 else 00036 return cast<InvokeInst>(I)->getCallingConv(); 00037 } 00038 void CallSite::setCallingConv(unsigned CC) { 00039 if (CallInst *CI = dyn_cast<CallInst>(I)) 00040 CI->setCallingConv(CC); 00041 else 00042 cast<InvokeInst>(I)->setCallingConv(CC); 00043 } 00044 const PAListPtr &CallSite::getParamAttrs() const { 00045 if (CallInst *CI = dyn_cast<CallInst>(I)) 00046 return CI->getParamAttrs(); 00047 else 00048 return cast<InvokeInst>(I)->getParamAttrs(); 00049 } 00050 void CallSite::setParamAttrs(const PAListPtr &PAL) { 00051 if (CallInst *CI = dyn_cast<CallInst>(I)) 00052 CI->setParamAttrs(PAL); 00053 else 00054 cast<InvokeInst>(I)->setParamAttrs(PAL); 00055 } 00056 bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const { 00057 if (CallInst *CI = dyn_cast<CallInst>(I)) 00058 return CI->paramHasAttr(i, attr); 00059 else 00060 return cast<InvokeInst>(I)->paramHasAttr(i, attr); 00061 } 00062 uint16_t CallSite::getParamAlignment(uint16_t i) const { 00063 if (CallInst *CI = dyn_cast<CallInst>(I)) 00064 return CI->getParamAlignment(i); 00065 else 00066 return cast<InvokeInst>(I)->getParamAlignment(i); 00067 } 00068 00069 bool CallSite::doesNotAccessMemory() const { 00070 if (CallInst *CI = dyn_cast<CallInst>(I)) 00071 return CI->doesNotAccessMemory(); 00072 else 00073 return cast<InvokeInst>(I)->doesNotAccessMemory(); 00074 } 00075 void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) { 00076 if (CallInst *CI = dyn_cast<CallInst>(I)) 00077 CI->setDoesNotAccessMemory(doesNotAccessMemory); 00078 else 00079 cast<InvokeInst>(I)->setDoesNotAccessMemory(doesNotAccessMemory); 00080 } 00081 bool CallSite::onlyReadsMemory() const { 00082 if (CallInst *CI = dyn_cast<CallInst>(I)) 00083 return CI->onlyReadsMemory(); 00084 else 00085 return cast<InvokeInst>(I)->onlyReadsMemory(); 00086 } 00087 void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) { 00088 if (CallInst *CI = dyn_cast<CallInst>(I)) 00089 CI->setOnlyReadsMemory(onlyReadsMemory); 00090 else 00091 cast<InvokeInst>(I)->setOnlyReadsMemory(onlyReadsMemory); 00092 } 00093 bool CallSite::doesNotReturn() const { 00094 if (CallInst *CI = dyn_cast<CallInst>(I)) 00095 return CI->doesNotReturn(); 00096 else 00097 return cast<InvokeInst>(I)->doesNotReturn(); 00098 } 00099 void CallSite::setDoesNotReturn(bool doesNotReturn) { 00100 if (CallInst *CI = dyn_cast<CallInst>(I)) 00101 CI->setDoesNotReturn(doesNotReturn); 00102 else 00103 cast<InvokeInst>(I)->setDoesNotReturn(doesNotReturn); 00104 } 00105 bool CallSite::doesNotThrow() const { 00106 if (CallInst *CI = dyn_cast<CallInst>(I)) 00107 return CI->doesNotThrow(); 00108 else 00109 return cast<InvokeInst>(I)->doesNotThrow(); 00110 } 00111 void CallSite::setDoesNotThrow(bool doesNotThrow) { 00112 if (CallInst *CI = dyn_cast<CallInst>(I)) 00113 CI->setDoesNotThrow(doesNotThrow); 00114 else 00115 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow); 00116 } 00117 00118 bool CallSite::hasArgument(const Value *Arg) const { 00119 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI) 00120 if (AI->get() == Arg) 00121 return true; 00122 return false; 00123 } 00124 00125 //===----------------------------------------------------------------------===// 00126 // TerminatorInst Class 00127 //===----------------------------------------------------------------------===// 00128 00129 // Out of line virtual method, so the vtable, etc has a home. 00130 TerminatorInst::~TerminatorInst() { 00131 } 00132 00133 //===----------------------------------------------------------------------===// 00134 // UnaryInstruction Class 00135 //===----------------------------------------------------------------------===// 00136 00137 // Out of line virtual method, so the vtable, etc has a home. 00138 UnaryInstruction::~UnaryInstruction() { 00139 } 00140 00141 //===----------------------------------------------------------------------===// 00142 // PHINode Class 00143 //===----------------------------------------------------------------------===// 00144 00145 PHINode::PHINode(const PHINode &PN) 00146 : Instruction(PN.getType(), Instruction::PHI, 00147 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), 00148 ReservedSpace(PN.getNumOperands()) { 00149 Use *OL = OperandList; 00150 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { 00151 OL[i] = PN.getOperand(i); 00152 OL[i+1] = PN.getOperand(i+1); 00153 } 00154 } 00155 00156 PHINode::~PHINode() { 00157 if (OperandList) 00158 dropHungoffUses(OperandList); 00159 } 00160 00161 // removeIncomingValue - Remove an incoming value. This is useful if a 00162 // predecessor basic block is deleted. 00163 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { 00164 unsigned NumOps = getNumOperands(); 00165 Use *OL = OperandList; 00166 assert(Idx*2 < NumOps && "BB not in PHI node!"); 00167 Value *Removed = OL[Idx*2]; 00168 00169 // Move everything after this operand down. 00170 // 00171 // FIXME: we could just swap with the end of the list, then erase. However, 00172 // client might not expect this to happen. The code as it is thrashes the 00173 // use/def lists, which is kinda lame. 00174 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { 00175 OL[i-2] = OL[i]; 00176 OL[i-2+1] = OL[i+1]; 00177 } 00178 00179 // Nuke the last value. 00180 OL[NumOps-2].set(0); 00181 OL[NumOps-2+1].set(0); 00182 NumOperands = NumOps-2; 00183 00184 // If the PHI node is dead, because it has zero entries, nuke it now. 00185 if (NumOps == 2 && DeletePHIIfEmpty) { 00186 // If anyone is using this PHI, make them use a dummy value instead... 00187 replaceAllUsesWith(UndefValue::get(getType())); 00188 eraseFromParent(); 00189 } 00190 return Removed; 00191 } 00192 00193 /// resizeOperands - resize operands - This adjusts the length of the operands 00194 /// list according to the following behavior: 00195 /// 1. If NumOps == 0, grow the operand list in response to a push_back style 00196 /// of operation. This grows the number of ops by 1.5 times. 00197 /// 2. If NumOps > NumOperands, reserve space for NumOps operands. 00198 /// 3. If NumOps == NumOperands, trim the reserved space. 00199 /// 00200 void PHINode::resizeOperands(unsigned NumOps) { 00201 unsigned e = getNumOperands(); 00202 if (NumOps == 0) { 00203 NumOps = e*3/2; 00204 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. 00205 } else if (NumOps*2 > NumOperands) { 00206 // No resize needed. 00207 if (ReservedSpace >= NumOps) return; 00208 } else if (NumOps == NumOperands) { 00209 if (ReservedSpace == NumOps) return; 00210 } else { 00211 return; 00212 } 00213 00214 ReservedSpace = NumOps; 00215 Use *OldOps = OperandList; 00216 Use *NewOps = allocHungoffUses(NumOps); 00217 std::copy(OldOps, OldOps + e, NewOps); 00218 OperandList = NewOps; 00219 if (OldOps) Use::zap(OldOps, OldOps + e, true); 00220 } 00221 00222 /// hasConstantValue - If the specified PHI node always merges together the same 00223 /// value, return the value, otherwise return null. 00224 /// 00225 Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { 00226 // If the PHI node only has one incoming value, eliminate the PHI node... 00227 if (getNumIncomingValues() == 1) { 00228 if (getIncomingValue(0) != this) // not X = phi X 00229 return getIncomingValue(0); 00230 else 00231 return UndefValue::get(getType()); // Self cycle is dead. 00232 } 00233 00234 // Otherwise if all of the incoming values are the same for the PHI, replace 00235 // the PHI node with the incoming value. 00236 // 00237 Value *InVal = 0; 00238 bool HasUndefInput = false; 00239 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) 00240 if (isa<UndefValue>(getIncomingValue(i))) { 00241 HasUndefInput = true; 00242 } else if (getIncomingValue(i) != this) { // Not the PHI node itself... 00243 if (InVal && getIncomingValue(i) != InVal) 00244 return 0; // Not the same, bail out. 00245 else 00246 InVal = getIncomingValue(i); 00247 } 00248 00249 // The only case that could cause InVal to be null is if we have a PHI node 00250 // that only has entries for itself. In this case, there is no entry into the 00251 // loop, so kill the PHI. 00252 // 00253 if (InVal == 0) InVal = UndefValue::get(getType()); 00254 00255 // If we have a PHI node like phi(X, undef, X), where X is defined by some 00256 // instruction, we cannot always return X as the result of the PHI node. Only 00257 // do this if X is not an instruction (thus it must dominate the PHI block), 00258 // or if the client is prepared to deal with this possibility. 00259 if (HasUndefInput && !AllowNonDominatingInstruction) 00260 if (Instruction *IV = dyn_cast<Instruction>(InVal)) 00261 // If it's in the entry block, it dominates everything. 00262 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || 00263 isa<InvokeInst>(IV)) 00264 return 0; // Cannot guarantee that InVal dominates this PHINode. 00265 00266 // All of the incoming values are the same, return the value now. 00267 return InVal; 00268 } 00269 00270 00271 //===----------------------------------------------------------------------===// 00272 // CallInst Implementation 00273 //===----------------------------------------------------------------------===// 00274 00275 CallInst::~CallInst() { 00276 } 00277 00278 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { 00279 assert(NumOperands == NumParams+1 && "NumOperands not set up?"); 00280 Use *OL = OperandList; 00281 OL[0] = Func; 00282 00283 const FunctionType *FTy = 00284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00285 FTy = FTy; // silence warning. 00286 00287 assert((NumParams == FTy->getNumParams() || 00288 (FTy->isVarArg() && NumParams > FTy->getNumParams())) && 00289 "Calling a function with bad signature!"); 00290 for (unsigned i = 0; i != NumParams; ++i) { 00291 assert((i >= FTy->getNumParams() || 00292 FTy->getParamType(i) == Params[i]->getType()) && 00293 "Calling a function with a bad signature!"); 00294 OL[i+1] = Params[i]; 00295 } 00296 } 00297 00298 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { 00299 assert(NumOperands == 3 && "NumOperands not set up?"); 00300 Use *OL = OperandList; 00301 OL[0] = Func; 00302 OL[1] = Actual1; 00303 OL[2] = Actual2; 00304 00305 const FunctionType *FTy = 00306 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00307 FTy = FTy; // silence warning. 00308 00309 assert((FTy->getNumParams() == 2 || 00310 (FTy->isVarArg() && FTy->getNumParams() < 2)) && 00311 "Calling a function with bad signature"); 00312 assert((0 >= FTy->getNumParams() || 00313 FTy->getParamType(0) == Actual1->getType()) && 00314 "Calling a function with a bad signature!"); 00315 assert((1 >= FTy->getNumParams() || 00316 FTy->getParamType(1) == Actual2->getType()) && 00317 "Calling a function with a bad signature!"); 00318 } 00319 00320 void CallInst::init(Value *Func, Value *Actual) { 00321 assert(NumOperands == 2 && "NumOperands not set up?"); 00322 Use *OL = OperandList; 00323 OL[0] = Func; 00324 OL[1] = Actual; 00325 00326 const FunctionType *FTy = 00327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00328 FTy = FTy; // silence warning. 00329 00330 assert((FTy->getNumParams() == 1 || 00331 (FTy->isVarArg() && FTy->getNumParams() == 0)) && 00332 "Calling a function with bad signature"); 00333 assert((0 == FTy->getNumParams() || 00334 FTy->getParamType(0) == Actual->getType()) && 00335 "Calling a function with a bad signature!"); 00336 } 00337 00338 void CallInst::init(Value *Func) { 00339 assert(NumOperands == 1 && "NumOperands not set up?"); 00340 Use *OL = OperandList; 00341 OL[0] = Func; 00342 00343 const FunctionType *FTy = 00344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00345 FTy = FTy; // silence warning. 00346 00347 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); 00348 } 00349 00350 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, 00351 Instruction *InsertBefore) 00352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00353 ->getElementType())->getReturnType(), 00354 Instruction::Call, 00355 OperandTraits<CallInst>::op_end(this) - 2, 00356 2, InsertBefore) { 00357 init(Func, Actual); 00358 setName(Name); 00359 } 00360 00361 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, 00362 BasicBlock *InsertAtEnd) 00363 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00364 ->getElementType())->getReturnType(), 00365 Instruction::Call, 00366 OperandTraits<CallInst>::op_end(this) - 2, 00367 2, InsertAtEnd) { 00368 init(Func, Actual); 00369 setName(Name); 00370 } 00371 CallInst::CallInst(Value *Func, const std::string &Name, 00372 Instruction *InsertBefore) 00373 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00374 ->getElementType())->getReturnType(), 00375 Instruction::Call, 00376 OperandTraits<CallInst>::op_end(this) - 1, 00377 1, InsertBefore) { 00378 init(Func); 00379 setName(Name); 00380 } 00381 00382 CallInst::CallInst(Value *Func, const std::string &Name, 00383 BasicBlock *InsertAtEnd) 00384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00385 ->getElementType())->getReturnType(), 00386 Instruction::Call, 00387 OperandTraits<CallInst>::op_end(this) - 1, 00388 1, InsertAtEnd) { 00389 init(Func); 00390 setName(Name); 00391 } 00392 00393 CallInst::CallInst(const CallInst &CI) 00394 : Instruction(CI.getType(), Instruction::Call, 00395 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), 00396 CI.getNumOperands()) { 00397 setParamAttrs(CI.getParamAttrs()); 00398 SubclassData = CI.SubclassData; 00399 Use *OL = OperandList; 00400 Use *InOL = CI.OperandList; 00401 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) 00402 OL[i] = InOL[i]; 00403 } 00404 00405 void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) { 00406 PAListPtr PAL = getParamAttrs(); 00407 PAL = PAL.addAttr(i, attr); 00408 setParamAttrs(PAL); 00409 } 00410 00411 void CallInst::removeParamAttr(unsigned i, ParameterAttributes attr) { 00412 PAListPtr PAL = getParamAttrs(); 00413 PAL = PAL.removeAttr(i, attr); 00414 setParamAttrs(PAL); 00415 } 00416 00417 bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { 00418 if (ParamAttrs.paramHasAttr(i, attr)) 00419 return true; 00420 if (const Function *F = getCalledFunction()) 00421 return F->paramHasAttr(i, attr); 00422 return false; 00423 } 00424 00425 00426 //===----------------------------------------------------------------------===// 00427 // InvokeInst Implementation 00428 //===----------------------------------------------------------------------===// 00429 00430 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, 00431 Value* const *Args, unsigned NumArgs) { 00432 assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); 00433 Use *OL = OperandList; 00434 OL[0] = Fn; 00435 OL[1] = IfNormal; 00436 OL[2] = IfException; 00437 const FunctionType *FTy = 00438 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); 00439 FTy = FTy; // silence warning. 00440 00441 assert(((NumArgs == FTy->getNumParams()) || 00442 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) && 00443 "Calling a function with bad signature"); 00444 00445 for (unsigned i = 0, e = NumArgs; i != e; i++) { 00446 assert((i >= FTy->getNumParams() || 00447 FTy->getParamType(i) == Args[i]->getType()) && 00448 "Invoking a function with a bad signature!"); 00449 00450 OL[i+3] = Args[i]; 00451 } 00452 } 00453 00454 InvokeInst::InvokeInst(const InvokeInst &II) 00455 : TerminatorInst(II.getType(), Instruction::Invoke, 00456 OperandTraits<InvokeInst>::op_end(this) 00457 - II.getNumOperands(), 00458 II.getNumOperands()) { 00459 setParamAttrs(II.getParamAttrs()); 00460 SubclassData = II.SubclassData; 00461 Use *OL = OperandList, *InOL = II.OperandList; 00462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) 00463 OL[i] = InOL[i]; 00464 } 00465 00466 BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { 00467 return getSuccessor(idx); 00468 } 00469 unsigned InvokeInst::getNumSuccessorsV() const { 00470 return getNumSuccessors(); 00471 } 00472 void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { 00473 return setSuccessor(idx, B); 00474 } 00475 00476 bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { 00477 if (ParamAttrs.paramHasAttr(i, attr)) 00478 return true; 00479 if (const Function *F = getCalledFunction()) 00480 return F->paramHasAttr(i, attr); 00481 return false; 00482 } 00483 00484 void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) { 00485 PAListPtr PAL = getParamAttrs(); 00486 PAL = PAL.addAttr(i, attr); 00487 setParamAttrs(PAL); 00488 } 00489 00490 void InvokeInst::removeParamAttr(unsigned i, ParameterAttributes attr) { 00491 PAListPtr PAL = getParamAttrs(); 00492 PAL = PAL.removeAttr(i, attr); 00493 setParamAttrs(PAL); 00494 } 00495 00496 00497 //===----------------------------------------------------------------------===// 00498 // ReturnInst Implementation 00499 //===----------------------------------------------------------------------===// 00500 00501 ReturnInst::ReturnInst(const ReturnInst &RI) 00502 : TerminatorInst(Type::VoidTy, Instruction::Ret, 00503 OperandTraits<ReturnInst>::op_end(this) - 00504 RI.getNumOperands(), 00505 RI.getNumOperands()) { 00506 if (RI.getNumOperands()) 00507 Op<0>() = RI.Op<0>(); 00508 } 00509 00510 ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) 00511 : TerminatorInst(Type::VoidTy, Instruction::Ret, 00512 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 00513 InsertBefore) { 00514 if (retVal) 00515 Op<0>() = retVal; 00516 } 00517 ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) 00518 : TerminatorInst(Type::VoidTy, Instruction::Ret, 00519 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 00520 InsertAtEnd) { 00521 if (retVal) 00522 Op<0>() = retVal; 00523 } 00524 ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) 00525 : TerminatorInst(Type::VoidTy, Instruction::Ret, 00526 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { 00527 } 00528 00529 unsigned ReturnInst::getNumSuccessorsV() const { 00530 return getNumSuccessors(); 00531 } 00532 00533 /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to 00534 /// emit the vtable for the class in this translation unit. 00535 void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 00536 assert(0 && "ReturnInst has no successors!"); 00537 } 00538 00539 BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { 00540 assert(0 && "ReturnInst has no successors!"); 00541 abort(); 00542 return 0; 00543 } 00544 00545 ReturnInst::~ReturnInst() { 00546 } 00547 00548 //===----------------------------------------------------------------------===// 00549 // UnwindInst Implementation 00550 //===----------------------------------------------------------------------===// 00551 00552 UnwindInst::UnwindInst(Instruction *InsertBefore) 00553 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) { 00554 } 00555 UnwindInst::UnwindInst(BasicBlock *InsertAtEnd) 00556 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) { 00557 } 00558 00559 00560 unsigned UnwindInst::getNumSuccessorsV() const { 00561 return getNumSuccessors(); 00562 } 00563 00564 void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 00565 assert(0 && "UnwindInst has no successors!"); 00566 } 00567 00568 BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { 00569 assert(0 && "UnwindInst has no successors!"); 00570 abort(); 00571 return 0; 00572 } 00573 00574 //===----------------------------------------------------------------------===// 00575 // UnreachableInst Implementation 00576 //===----------------------------------------------------------------------===// 00577 00578 UnreachableInst::UnreachableInst(Instruction *InsertBefore) 00579 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) { 00580 } 00581 UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd) 00582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) { 00583 } 00584 00585 unsigned UnreachableInst::getNumSuccessorsV() const { 00586 return getNumSuccessors(); 00587 } 00588 00589 void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { 00590 assert(0 && "UnwindInst has no successors!"); 00591 } 00592 00593 BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { 00594 assert(0 && "UnwindInst has no successors!"); 00595 abort(); 00596 return 0; 00597 } 00598 00599 //===----------------------------------------------------------------------===// 00600 // BranchInst Implementation 00601 //===----------------------------------------------------------------------===// 00602 00603 void BranchInst::AssertOK() { 00604 if (isConditional()) 00605 assert(getCondition()->getType() == Type::Int1Ty && 00606 "May only branch on boolean predicates!"); 00607 } 00608 00609 BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) 00610 : TerminatorInst(Type::VoidTy, Instruction::Br, 00611 OperandTraits<BranchInst>::op_end(this) - 1, 00612 1, InsertBefore) { 00613 assert(IfTrue != 0 && "Branch destination may not be null!"); 00614 Op<0>() = IfTrue; 00615 } 00616 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 00617 Instruction *InsertBefore) 00618 : TerminatorInst(Type::VoidTy, Instruction::Br, 00619 OperandTraits<BranchInst>::op_end(this) - 3, 00620 3, InsertBefore) { 00621 Op<0>() = IfTrue; 00622 Op<1>() = IfFalse; 00623 Op<2>() = Cond; 00624 #ifndef NDEBUG 00625 AssertOK(); 00626 #endif 00627 } 00628 00629 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) 00630 : TerminatorInst(Type::VoidTy, Instruction::Br, 00631 OperandTraits<BranchInst>::op_end(this) - 1, 00632 1, InsertAtEnd) { 00633 assert(IfTrue != 0 && "Branch destination may not be null!"); 00634 Op<0>() = IfTrue; 00635 } 00636 00637 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 00638 BasicBlock *InsertAtEnd) 00639 : TerminatorInst(Type::VoidTy, Instruction::Br, 00640 OperandTraits<BranchInst>::op_end(this) - 3, 00641 3, InsertAtEnd) { 00642 Op<0>() = IfTrue; 00643 Op<1>() = IfFalse; 00644 Op<2>() = Cond; 00645 #ifndef NDEBUG 00646 AssertOK(); 00647 #endif 00648 } 00649 00650 00651 BranchInst::BranchInst(const BranchInst &BI) : 00652 TerminatorInst(Type::VoidTy, Instruction::Br, 00653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), 00654 BI.getNumOperands()) { 00655 OperandList[0] = BI.getOperand(0); 00656 if (BI.getNumOperands() != 1) { 00657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); 00658 OperandList[1] = BI.getOperand(1); 00659 OperandList[2] = BI.getOperand(2); 00660 } 00661 } 00662 00663 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { 00664 return getSuccessor(idx); 00665 } 00666 unsigned BranchInst::getNumSuccessorsV() const { 00667 return getNumSuccessors(); 00668 } 00669 void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { 00670 setSuccessor(idx, B); 00671 } 00672 00673 00674 //===----------------------------------------------------------------------===// 00675 // AllocationInst Implementation 00676 //===----------------------------------------------------------------------===// 00677 00678 static Value *getAISize(Value *Amt) { 00679 if (!Amt) 00680 Amt = ConstantInt::get(Type::Int32Ty, 1); 00681 else { 00682 assert(!isa<BasicBlock>(Amt) && 00683 "Passed basic block into allocation size parameter! Use other ctor"); 00684 assert(Amt->getType() == Type::Int32Ty && 00685 "Malloc/Allocation array size is not a 32-bit integer!"); 00686 } 00687 return Amt; 00688 } 00689 00690 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 00691 unsigned Align, const std::string &Name, 00692 Instruction *InsertBefore) 00693 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), 00694 InsertBefore) { 00695 setAlignment(Align); 00696 assert(Ty != Type::VoidTy && "Cannot allocate void!"); 00697 setName(Name); 00698 } 00699 00700 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 00701 unsigned Align, const std::string &Name, 00702 BasicBlock *InsertAtEnd) 00703 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), 00704 InsertAtEnd) { 00705 setAlignment(Align); 00706 assert(Ty != Type::VoidTy && "Cannot allocate void!"); 00707 setName(Name); 00708 } 00709 00710 // Out of line virtual method, so the vtable, etc has a home. 00711 AllocationInst::~AllocationInst() { 00712 } 00713 00714 void AllocationInst::setAlignment(unsigned Align) { 00715 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 00716 SubclassData = Log2_32(Align) + 1; 00717 assert(getAlignment() == Align && "Alignment representation error!"); 00718 } 00719 00720 bool AllocationInst::isArrayAllocation() const { 00721 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) 00722 return CI->getZExtValue() != 1; 00723 return true; 00724 } 00725 00726 const Type *AllocationInst::getAllocatedType() const { 00727 return getType()->getElementType(); 00728 } 00729 00730 AllocaInst::AllocaInst(const AllocaInst &AI) 00731 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), 00732 Instruction::Alloca, AI.getAlignment()) { 00733 } 00734 00735 MallocInst::MallocInst(const MallocInst &MI) 00736 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), 00737 Instruction::Malloc, MI.getAlignment()) { 00738 } 00739 00740 //===----------------------------------------------------------------------===// 00741 // FreeInst Implementation 00742 //===----------------------------------------------------------------------===// 00743 00744 void FreeInst::AssertOK() { 00745 assert(isa<PointerType>(getOperand(0)->getType()) && 00746 "Can not free something of nonpointer type!"); 00747 } 00748 00749 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) 00750 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) { 00751 AssertOK(); 00752 } 00753 00754 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) 00755 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) { 00756 AssertOK(); 00757 } 00758 00759 00760 //===----------------------------------------------------------------------===// 00761 // LoadInst Implementation 00762 //===----------------------------------------------------------------------===// 00763 00764 void LoadInst::AssertOK() { 00765 assert(isa<PointerType>(getOperand(0)->getType()) && 00766 "Ptr must have pointer type."); 00767 } 00768 00769 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) 00770 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00771 Load, Ptr, InsertBef) { 00772 setVolatile(false); 00773 setAlignment(0); 00774 AssertOK(); 00775 setName(Name); 00776 } 00777 00778 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) 00779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00780 Load, Ptr, InsertAE) { 00781 setVolatile(false); 00782 setAlignment(0); 00783 AssertOK(); 00784 setName(Name); 00785 } 00786 00787 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00788 Instruction *InsertBef) 00789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00790 Load, Ptr, InsertBef) { 00791 setVolatile(isVolatile); 00792 setAlignment(0); 00793 AssertOK(); 00794 setName(Name); 00795 } 00796 00797 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00798 unsigned Align, Instruction *InsertBef) 00799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00800 Load, Ptr, InsertBef) { 00801 setVolatile(isVolatile); 00802 setAlignment(Align); 00803 AssertOK(); 00804 setName(Name); 00805 } 00806 00807 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00808 unsigned Align, BasicBlock *InsertAE) 00809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00810 Load, Ptr, InsertAE) { 00811 setVolatile(isVolatile); 00812 setAlignment(Align); 00813 AssertOK(); 00814 setName(Name); 00815 } 00816 00817 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00818 BasicBlock *InsertAE) 00819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00820 Load, Ptr, InsertAE) { 00821 setVolatile(isVolatile); 00822 setAlignment(0); 00823 AssertOK(); 00824 setName(Name); 00825 } 00826 00827 00828 00829 LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) 00830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00831 Load, Ptr, InsertBef) { 00832 setVolatile(false); 00833 setAlignment(0); 00834 AssertOK(); 00835 if (Name && Name[0]) setName(Name); 00836 } 00837 00838 LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) 00839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00840 Load, Ptr, InsertAE) { 00841 setVolatile(false); 00842 setAlignment(0); 00843 AssertOK(); 00844 if (Name && Name[0]) setName(Name); 00845 } 00846 00847 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, 00848 Instruction *InsertBef) 00849 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00850 Load, Ptr, InsertBef) { 00851 setVolatile(isVolatile); 00852 setAlignment(0); 00853 AssertOK(); 00854 if (Name && Name[0]) setName(Name); 00855 } 00856 00857 LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, 00858 BasicBlock *InsertAE) 00859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), 00860 Load, Ptr, InsertAE) { 00861 setVolatile(isVolatile); 00862 setAlignment(0); 00863 AssertOK(); 00864 if (Name && Name[0]) setName(Name); 00865 } 00866 00867 void LoadInst::setAlignment(unsigned Align) { 00868 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 00869 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); 00870 } 00871 00872 //===----------------------------------------------------------------------===// 00873 // StoreInst Implementation 00874 //===----------------------------------------------------------------------===// 00875 00876 void StoreInst::AssertOK() { 00877 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); 00878 assert(isa<PointerType>(getOperand(1)->getType()) && 00879 "Ptr must have pointer type!"); 00880 assert(getOperand(0)->getType() == 00881 cast<PointerType>(getOperand(1)->getType())->getElementType() 00882 && "Ptr must be a pointer to Val type!"); 00883 } 00884 00885 00886 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) 00887 : Instruction(Type::VoidTy, Store, 00888 OperandTraits<StoreInst>::op_begin(this), 00889 OperandTraits<StoreInst>::operands(this), 00890 InsertBefore) { 00891 Op<0>() = val; 00892 Op<1>() = addr; 00893 setVolatile(false); 00894 setAlignment(0); 00895 AssertOK(); 00896 } 00897 00898 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) 00899 : Instruction(Type::VoidTy, Store, 00900 OperandTraits<StoreInst>::op_begin(this), 00901 OperandTraits<StoreInst>::operands(this), 00902 InsertAtEnd) { 00903 Op<0>() = val; 00904 Op<1>() = addr; 00905 setVolatile(false); 00906 setAlignment(0); 00907 AssertOK(); 00908 } 00909 00910 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 00911 Instruction *InsertBefore) 00912 : Instruction(Type::VoidTy, Store, 00913 OperandTraits<StoreInst>::op_begin(this), 00914 OperandTraits<StoreInst>::operands(this), 00915 InsertBefore) { 00916 Op<0>() = val; 00917 Op<1>() = addr; 00918 setVolatile(isVolatile); 00919 setAlignment(0); 00920 AssertOK(); 00921 } 00922 00923 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 00924 unsigned Align, Instruction *InsertBefore) 00925 : Instruction(Type::VoidTy, Store, 00926 OperandTraits<StoreInst>::op_begin(this), 00927 OperandTraits<StoreInst>::operands(this), 00928 InsertBefore) { 00929 Op<0>() = val; 00930 Op<1>() = addr; 00931 setVolatile(isVolatile); 00932 setAlignment(Align); 00933 AssertOK(); 00934 } 00935 00936 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 00937 unsigned Align, BasicBlock *InsertAtEnd) 00938 : Instruction(Type::VoidTy, Store, 00939 OperandTraits<StoreInst>::op_begin(this), 00940 OperandTraits<StoreInst>::operands(this), 00941 InsertAtEnd) { 00942 Op<0>() = val; 00943 Op<1>() = addr; 00944 setVolatile(isVolatile); 00945 setAlignment(Align); 00946 AssertOK(); 00947 } 00948 00949 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 00950 BasicBlock *InsertAtEnd) 00951 : Instruction(Type::VoidTy, Store, 00952 OperandTraits<StoreInst>::op_begin(this), 00953 OperandTraits<StoreInst>::operands(this), 00954 InsertAtEnd) { 00955 Op<0>() = val; 00956 Op<1>() = addr; 00957 setVolatile(isVolatile); 00958 setAlignment(0); 00959 AssertOK(); 00960 } 00961 00962 void StoreInst::setAlignment(unsigned Align) { 00963 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 00964 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); 00965 } 00966 00967 //===----------------------------------------------------------------------===// 00968 // GetElementPtrInst Implementation 00969 //===----------------------------------------------------------------------===// 00970 00971 static unsigned retrieveAddrSpace(const Value *Val) { 00972 return cast<PointerType>(Val->getType())->getAddressSpace(); 00973 } 00974 00975 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, 00976 const std::string &Name) { 00977 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); 00978 Use *OL = OperandList; 00979 OL[0] = Ptr; 00980 00981 for (unsigned i = 0; i != NumIdx; ++i) 00982 OL[i+1] = Idx[i]; 00983 00984 setName(Name); 00985 } 00986 00987 void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) { 00988 assert(NumOperands == 2 && "NumOperands not initialized?"); 00989 Use *OL = OperandList; 00990 OL[0] = Ptr; 00991 OL[1] = Idx; 00992 00993 setName(Name); 00994 } 00995 00996 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) 00997 : Instruction(GEPI.getType(), GetElementPtr, 00998 OperandTraits<GetElementPtrInst>::op_end(this) 00999 - GEPI.getNumOperands(), 01000 GEPI.getNumOperands()) { 01001 Use *OL = OperandList; 01002 Use *GEPIOL = GEPI.OperandList; 01003 for (unsigned i = 0, E = NumOperands; i != E; ++i) 01004 OL[i] = GEPIOL[i]; 01005 } 01006 01007 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, 01008 const std::string &Name, Instruction *InBe) 01009 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), 01010 retrieveAddrSpace(Ptr)), 01011 GetElementPtr, 01012 OperandTraits<GetElementPtrInst>::op_end(this) - 2, 01013 2, InBe) { 01014 init(Ptr, Idx, Name); 01015 } 01016 01017 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, 01018 const std::string &Name, BasicBlock *IAE) 01019 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), 01020 retrieveAddrSpace(Ptr)), 01021 GetElementPtr, 01022 OperandTraits<GetElementPtrInst>::op_end(this) - 2, 01023 2, IAE) { 01024 init(Ptr, Idx, Name); 01025 } 01026 01027 // getIndexedType - Returns the type of the element that would be loaded with 01028 // a load instruction with the specified parameters. 01029 // 01030 // The Idxs pointer should point to a continuous piece of memory containing the 01031 // indices, either as Value* or uint64_t. 01032 // 01033 // A null type is returned if the indices are invalid for the specified 01034 // pointer type. 01035 // 01036 template <typename IndexTy> 01037 static const Type* getIndexedTypeInternal(const Type *Ptr, 01038 IndexTy const *Idxs, 01039 unsigned NumIdx) { 01040 const PointerType *PTy = dyn_cast<PointerType>(Ptr); 01041 if (!PTy) return 0; // Type isn't a pointer type! 01042 const Type *Agg = PTy->getElementType(); 01043 01044 // Handle the special case of the empty set index set... 01045 if (NumIdx == 0) 01046 return Agg; 01047 01048 unsigned CurIdx = 1; 01049 for (; CurIdx != NumIdx; ++CurIdx) { 01050 const CompositeType *CT = dyn_cast<CompositeType>(Agg); 01051 if (!CT || isa<PointerType>(CT)) return 0; 01052 IndexTy Index = Idxs[CurIdx]; 01053 if (!CT->indexValid(Index)) return 0; 01054 Agg = CT->getTypeAtIndex(Index); 01055 01056 // If the new type forwards to another type, then it is in the middle 01057 // of being refined to another type (and hence, may have dropped all 01058 // references to what it was using before). So, use the new forwarded 01059 // type. 01060 if (const Type *Ty = Agg->getForwardedType()) 01061 Agg = Ty; 01062 } 01063 return CurIdx == NumIdx ? Agg : 0; 01064 } 01065 01066 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 01067 Value* const *Idxs, 01068 unsigned NumIdx) { 01069 return getIndexedTypeInternal(Ptr, Idxs, NumIdx); 01070 } 01071 01072 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 01073 uint64_t const *Idxs, 01074 unsigned NumIdx) { 01075 return getIndexedTypeInternal(Ptr, Idxs, NumIdx); 01076 } 01077 01078 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { 01079 const PointerType *PTy = dyn_cast<PointerType>(Ptr); 01080 if (!PTy) return 0; // Type isn't a pointer type! 01081 01082 // Check the pointer index. 01083 if (!PTy->indexValid(Idx)) return 0; 01084 01085 return PTy->getElementType(); 01086 } 01087 01088 01089 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 01090 /// zeros. If so, the result pointer and the first operand have the same 01091 /// value, just potentially different types. 01092 bool GetElementPtrInst::hasAllZeroIndices() const { 01093 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 01094 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { 01095 if (!CI->isZero()) return false; 01096 } else { 01097 return false; 01098 } 01099 } 01100 return true; 01101 } 01102 01103 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 01104 /// constant integers. If so, the result pointer and the first operand have 01105 /// a constant offset between them. 01106 bool GetElementPtrInst::hasAllConstantIndices() const { 01107 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 01108 if (!isa<ConstantInt>(getOperand(i))) 01109 return false; 01110 } 01111 return true; 01112 } 01113 01114 01115 //===----------------------------------------------------------------------===// 01116 // ExtractElementInst Implementation 01117 //===----------------------------------------------------------------------===// 01118 01119 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 01120 const std::string &Name, 01121 Instruction *InsertBef) 01122 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 01123 ExtractElement, 01124 OperandTraits<ExtractElementInst>::op_begin(this), 01125 2, InsertBef) { 01126 assert(isValidOperands(Val, Index) && 01127 "Invalid extractelement instruction operands!"); 01128 Op<0>() = Val; 01129 Op<1>() = Index; 01130 setName(Name); 01131 } 01132 01133 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, 01134 const std::string &Name, 01135 Instruction *InsertBef) 01136 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 01137 ExtractElement, 01138 OperandTraits<ExtractElementInst>::op_begin(this), 01139 2, InsertBef) { 01140 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); 01141 assert(isValidOperands(Val, Index) && 01142 "Invalid extractelement instruction operands!"); 01143 Op<0>() = Val; 01144 Op<1>() = Index; 01145 setName(Name); 01146 } 01147 01148 01149 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 01150 const std::string &Name, 01151 BasicBlock *InsertAE) 01152 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 01153 ExtractElement, 01154 OperandTraits<ExtractElementInst>::op_begin(this), 01155 2, InsertAE) { 01156 assert(isValidOperands(Val, Index) && 01157 "Invalid extractelement instruction operands!"); 01158 01159 Op<0>() = Val; 01160 Op<1>() = Index; 01161 setName(Name); 01162 } 01163 01164 ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, 01165 const std::string &Name, 01166 BasicBlock *InsertAE) 01167 : Instruction(cast<VectorType>(Val->getType())->getElementType(), 01168 ExtractElement, 01169 OperandTraits<ExtractElementInst>::op_begin(this), 01170 2, InsertAE) { 01171 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); 01172 assert(isValidOperands(Val, Index) && 01173 "Invalid extractelement instruction operands!"); 01174 01175 Op<0>() = Val; 01176 Op<1>() = Index; 01177 setName(Name); 01178 } 01179 01180 01181 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { 01182 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty) 01183 return false; 01184 return true; 01185 } 01186 01187 01188 //===----------------------------------------------------------------------===// 01189 // InsertElementInst Implementation 01190 //===----------------------------------------------------------------------===// 01191 01192 InsertElementInst::InsertElementInst(const InsertElementInst &IE) 01193 : Instruction(IE.getType(), InsertElement, 01194 OperandTraits<InsertElementInst>::op_begin(this), 3) { 01195 Op<0>() = IE.Op<0>(); 01196 Op<1>() = IE.Op<1>(); 01197 Op<2>() = IE.Op<2>(); 01198 } 01199 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 01200 const std::string &Name, 01201 Instruction *InsertBef) 01202 : Instruction(Vec->getType(), InsertElement, 01203 OperandTraits<InsertElementInst>::op_begin(this), 01204 3, InsertBef) { 01205 assert(isValidOperands(Vec, Elt, Index) && 01206 "Invalid insertelement instruction operands!"); 01207 Op<0>() = Vec; 01208 Op<1>() = Elt; 01209 Op<2>() = Index; 01210 setName(Name); 01211 } 01212 01213 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, 01214 const std::string &Name, 01215 Instruction *InsertBef) 01216 : Instruction(Vec->getType(), InsertElement, 01217 OperandTraits<InsertElementInst>::op_begin(this), 01218 3, InsertBef) { 01219 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); 01220 assert(isValidOperands(Vec, Elt, Index) && 01221 "Invalid insertelement instruction operands!"); 01222 Op<0>() = Vec; 01223 Op<1>() = Elt; 01224 Op<2>() = Index; 01225 setName(Name); 01226 } 01227 01228 01229 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 01230 const std::string &Name, 01231 BasicBlock *InsertAE) 01232 : Instruction(Vec->getType(), InsertElement, 01233 OperandTraits<InsertElementInst>::op_begin(this), 01234 3, InsertAE) { 01235 assert(isValidOperands(Vec, Elt, Index) && 01236 "Invalid insertelement instruction operands!"); 01237 01238 Op<0>() = Vec; 01239 Op<1>() = Elt; 01240 Op<2>() = Index; 01241 setName(Name); 01242 } 01243 01244 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, 01245 const std::string &Name, 01246 BasicBlock *InsertAE) 01247 : Instruction(Vec->getType(), InsertElement, 01248 OperandTraits<InsertElementInst>::op_begin(this), 01249 3, InsertAE) { 01250 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); 01251 assert(isValidOperands(Vec, Elt, Index) && 01252 "Invalid insertelement instruction operands!"); 01253 01254 Op<0>() = Vec; 01255 Op<1>() = Elt; 01256 Op<2>() = Index; 01257 setName(Name); 01258 } 01259 01260 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 01261 const Value *Index) { 01262 if (!isa<VectorType>(Vec->getType())) 01263 return false; // First operand of insertelement must be vector type. 01264 01265 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) 01266 return false;// Second operand of insertelement must be vector element type. 01267 01268 if (Index->getType() != Type::Int32Ty) 01269 return false; // Third operand of insertelement must be uint. 01270 return true; 01271 } 01272 01273 01274 //===----------------------------------------------------------------------===// 01275 // ShuffleVectorInst Implementation 01276 //===----------------------------------------------------------------------===// 01277 01278 ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) 01279 : Instruction(SV.getType(), ShuffleVector, 01280 OperandTraits<ShuffleVectorInst>::op_begin(this), 01281 OperandTraits<ShuffleVectorInst>::operands(this)) { 01282 Op<0>() = SV.Op<0>(); 01283 Op<1>() = SV.Op<1>(); 01284 Op<2>() = SV.Op<2>(); 01285 } 01286 01287 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 01288 const std::string &Name, 01289 Instruction *InsertBefore) 01290 : Instruction(V1->getType(), ShuffleVector, 01291 OperandTraits<ShuffleVectorInst>::op_begin(this), 01292 OperandTraits<ShuffleVectorInst>::operands(this), 01293 InsertBefore) { 01294 assert(isValidOperands(V1, V2, Mask) && 01295 "Invalid shuffle vector instruction operands!"); 01296 Op<0>() = V1; 01297 Op<1>() = V2; 01298 Op<2>() = Mask; 01299 setName(Name); 01300 } 01301 01302 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 01303