LLVM API Documentation
00001 //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LLVM instructions... 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/BasicBlock.h" 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 using namespace llvm; 00021 00022 //===----------------------------------------------------------------------===// 00023 // CallInst Implementation 00024 //===----------------------------------------------------------------------===// 00025 00026 void CallInst::init(Value *Func, const std::vector<Value*> &Params) 00027 { 00028 Operands.reserve(1+Params.size()); 00029 Operands.push_back(Use(Func, this)); 00030 00031 const FunctionType *FTy = 00032 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00033 00034 assert((Params.size() == FTy->getNumParams() || 00035 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && 00036 "Calling a function with bad signature"); 00037 for (unsigned i = 0; i != Params.size(); i++) 00038 Operands.push_back(Use(Params[i], this)); 00039 } 00040 00041 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) 00042 { 00043 Operands.reserve(3); 00044 Operands.push_back(Use(Func, this)); 00045 00046 const FunctionType *MTy = 00047 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00048 00049 assert((MTy->getNumParams() == 2 || 00050 (MTy->isVarArg() && MTy->getNumParams() == 0)) && 00051 "Calling a function with bad signature"); 00052 Operands.push_back(Use(Actual1, this)); 00053 Operands.push_back(Use(Actual2, this)); 00054 } 00055 00056 void CallInst::init(Value *Func, Value *Actual) 00057 { 00058 Operands.reserve(2); 00059 Operands.push_back(Use(Func, this)); 00060 00061 const FunctionType *MTy = 00062 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00063 00064 assert((MTy->getNumParams() == 1 || 00065 (MTy->isVarArg() && MTy->getNumParams() == 0)) && 00066 "Calling a function with bad signature"); 00067 Operands.push_back(Use(Actual, this)); 00068 } 00069 00070 void CallInst::init(Value *Func) 00071 { 00072 Operands.reserve(1); 00073 Operands.push_back(Use(Func, this)); 00074 00075 const FunctionType *MTy = 00076 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); 00077 00078 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); 00079 } 00080 00081 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, 00082 const std::string &Name, Instruction *InsertBefore) 00083 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00084 ->getElementType())->getReturnType(), 00085 Instruction::Call, Name, InsertBefore) { 00086 init(Func, Params); 00087 } 00088 00089 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, 00090 const std::string &Name, BasicBlock *InsertAtEnd) 00091 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00092 ->getElementType())->getReturnType(), 00093 Instruction::Call, Name, InsertAtEnd) { 00094 init(Func, Params); 00095 } 00096 00097 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, 00098 const std::string &Name, Instruction *InsertBefore) 00099 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00100 ->getElementType())->getReturnType(), 00101 Instruction::Call, Name, InsertBefore) { 00102 init(Func, Actual1, Actual2); 00103 } 00104 00105 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, 00106 const std::string &Name, BasicBlock *InsertAtEnd) 00107 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00108 ->getElementType())->getReturnType(), 00109 Instruction::Call, Name, InsertAtEnd) { 00110 init(Func, Actual1, Actual2); 00111 } 00112 00113 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, 00114 Instruction *InsertBefore) 00115 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00116 ->getElementType())->getReturnType(), 00117 Instruction::Call, Name, InsertBefore) { 00118 init(Func, Actual); 00119 } 00120 00121 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, 00122 BasicBlock *InsertAtEnd) 00123 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00124 ->getElementType())->getReturnType(), 00125 Instruction::Call, Name, InsertAtEnd) { 00126 init(Func, Actual); 00127 } 00128 00129 CallInst::CallInst(Value *Func, const std::string &Name, 00130 Instruction *InsertBefore) 00131 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00132 ->getElementType())->getReturnType(), 00133 Instruction::Call, Name, InsertBefore) { 00134 init(Func); 00135 } 00136 00137 CallInst::CallInst(Value *Func, const std::string &Name, 00138 BasicBlock *InsertAtEnd) 00139 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) 00140 ->getElementType())->getReturnType(), 00141 Instruction::Call, Name, InsertAtEnd) { 00142 init(Func); 00143 } 00144 00145 CallInst::CallInst(const CallInst &CI) 00146 : Instruction(CI.getType(), Instruction::Call) { 00147 Operands.reserve(CI.Operands.size()); 00148 for (unsigned i = 0; i < CI.Operands.size(); ++i) 00149 Operands.push_back(Use(CI.Operands[i], this)); 00150 } 00151 00152 00153 //===----------------------------------------------------------------------===// 00154 // InvokeInst Implementation 00155 //===----------------------------------------------------------------------===// 00156 00157 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, 00158 const std::vector<Value*> &Params) 00159 { 00160 Operands.reserve(3+Params.size()); 00161 Operands.push_back(Use(Fn, this)); 00162 Operands.push_back(Use((Value*)IfNormal, this)); 00163 Operands.push_back(Use((Value*)IfException, this)); 00164 const FunctionType *MTy = 00165 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); 00166 00167 assert((Params.size() == MTy->getNumParams()) || 00168 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) && 00169 "Calling a function with bad signature"); 00170 00171 for (unsigned i = 0; i < Params.size(); i++) 00172 Operands.push_back(Use(Params[i], this)); 00173 } 00174 00175 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, 00176 BasicBlock *IfException, 00177 const std::vector<Value*> &Params, 00178 const std::string &Name, Instruction *InsertBefore) 00179 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) 00180 ->getElementType())->getReturnType(), 00181 Instruction::Invoke, Name, InsertBefore) { 00182 init(Fn, IfNormal, IfException, Params); 00183 } 00184 00185 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, 00186 BasicBlock *IfException, 00187 const std::vector<Value*> &Params, 00188 const std::string &Name, BasicBlock *InsertAtEnd) 00189 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) 00190 ->getElementType())->getReturnType(), 00191 Instruction::Invoke, Name, InsertAtEnd) { 00192 init(Fn, IfNormal, IfException, Params); 00193 } 00194 00195 InvokeInst::InvokeInst(const InvokeInst &CI) 00196 : TerminatorInst(CI.getType(), Instruction::Invoke) { 00197 Operands.reserve(CI.Operands.size()); 00198 for (unsigned i = 0; i < CI.Operands.size(); ++i) 00199 Operands.push_back(Use(CI.Operands[i], this)); 00200 } 00201 00202 //===----------------------------------------------------------------------===// 00203 // ReturnInst Implementation 00204 //===----------------------------------------------------------------------===// 00205 00206 void ReturnInst::init(Value* RetVal) { 00207 if (RetVal && RetVal->getType() != Type::VoidTy) { 00208 assert(!isa<BasicBlock>(RetVal) && 00209 "Cannot return basic block. Probably using the incorrect ctor"); 00210 Operands.reserve(1); 00211 Operands.push_back(Use(RetVal, this)); 00212 } 00213 } 00214 00215 // Out-of-line ReturnInst method, put here so the C++ compiler can choose to 00216 // emit the vtable for the class in this translation unit. 00217 void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) { 00218 assert(0 && "ReturnInst has no successors!"); 00219 } 00220 00221 //===----------------------------------------------------------------------===// 00222 // UnwindInst Implementation 00223 //===----------------------------------------------------------------------===// 00224 00225 // Likewise for UnwindInst 00226 void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) { 00227 assert(0 && "UnwindInst has no successors!"); 00228 } 00229 00230 //===----------------------------------------------------------------------===// 00231 // UnreachableInst Implementation 00232 //===----------------------------------------------------------------------===// 00233 00234 void UnreachableInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) { 00235 assert(0 && "UnreachableInst has no successors!"); 00236 } 00237 00238 //===----------------------------------------------------------------------===// 00239 // BranchInst Implementation 00240 //===----------------------------------------------------------------------===// 00241 00242 void BranchInst::init(BasicBlock *IfTrue) 00243 { 00244 assert(IfTrue != 0 && "Branch destination may not be null!"); 00245 Operands.reserve(1); 00246 Operands.push_back(Use(IfTrue, this)); 00247 } 00248 00249 void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond) 00250 { 00251 assert(IfTrue && IfFalse && Cond && 00252 "Branch destinations and condition may not be null!"); 00253 assert(Cond && Cond->getType() == Type::BoolTy && 00254 "May only branch on boolean predicates!"); 00255 Operands.reserve(3); 00256 Operands.push_back(Use(IfTrue, this)); 00257 Operands.push_back(Use(IfFalse, this)); 00258 Operands.push_back(Use(Cond, this)); 00259 } 00260 00261 BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) { 00262 Operands.reserve(BI.Operands.size()); 00263 Operands.push_back(Use(BI.Operands[0], this)); 00264 if (BI.Operands.size() != 1) { 00265 assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!"); 00266 Operands.push_back(Use(BI.Operands[1], this)); 00267 Operands.push_back(Use(BI.Operands[2], this)); 00268 } 00269 } 00270 00271 //===----------------------------------------------------------------------===// 00272 // AllocationInst Implementation 00273 //===----------------------------------------------------------------------===// 00274 00275 void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) { 00276 assert(Ty != Type::VoidTy && "Cannot allocate void elements!"); 00277 // ArraySize defaults to 1. 00278 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1); 00279 00280 Operands.reserve(1); 00281 assert(ArraySize->getType() == Type::UIntTy && 00282 "Malloc/Allocation array size != UIntTy!"); 00283 00284 Operands.push_back(Use(ArraySize, this)); 00285 } 00286 00287 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 00288 const std::string &Name, 00289 Instruction *InsertBefore) 00290 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) { 00291 init(Ty, ArraySize, iTy); 00292 } 00293 00294 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 00295 const std::string &Name, 00296 BasicBlock *InsertAtEnd) 00297 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) { 00298 init(Ty, ArraySize, iTy); 00299 } 00300 00301 bool AllocationInst::isArrayAllocation() const { 00302 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1); 00303 } 00304 00305 const Type *AllocationInst::getAllocatedType() const { 00306 return getType()->getElementType(); 00307 } 00308 00309 AllocaInst::AllocaInst(const AllocaInst &AI) 00310 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), 00311 Instruction::Alloca) { 00312 } 00313 00314 MallocInst::MallocInst(const MallocInst &MI) 00315 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), 00316 Instruction::Malloc) { 00317 } 00318 00319 //===----------------------------------------------------------------------===// 00320 // FreeInst Implementation 00321 //===----------------------------------------------------------------------===// 00322 00323 void FreeInst::init(Value *Ptr) 00324 { 00325 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!"); 00326 Operands.reserve(1); 00327 Operands.push_back(Use(Ptr, this)); 00328 } 00329 00330 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) 00331 : Instruction(Type::VoidTy, Free, "", InsertBefore) { 00332 init(Ptr); 00333 } 00334 00335 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) 00336 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) { 00337 init(Ptr); 00338 } 00339 00340 00341 //===----------------------------------------------------------------------===// 00342 // LoadInst Implementation 00343 //===----------------------------------------------------------------------===// 00344 00345 void LoadInst::init(Value *Ptr) { 00346 assert(Ptr && isa<PointerType>(Ptr->getType()) && 00347 "Ptr must have pointer type."); 00348 Operands.reserve(1); 00349 Operands.push_back(Use(Ptr, this)); 00350 } 00351 00352 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) 00353 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), 00354 Load, Name, InsertBef), Volatile(false) { 00355 init(Ptr); 00356 } 00357 00358 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) 00359 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), 00360 Load, Name, InsertAE), Volatile(false) { 00361 init(Ptr); 00362 } 00363 00364 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00365 Instruction *InsertBef) 00366 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), 00367 Load, Name, InsertBef), Volatile(isVolatile) { 00368 init(Ptr); 00369 } 00370 00371 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 00372 BasicBlock *InsertAE) 00373 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), 00374 Load, Name, InsertAE), Volatile(isVolatile) { 00375 init(Ptr); 00376 } 00377 00378 00379 //===----------------------------------------------------------------------===// 00380 // StoreInst Implementation 00381 //===----------------------------------------------------------------------===// 00382 00383 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore) 00384 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) { 00385 init(Val, Ptr); 00386 } 00387 00388 StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd) 00389 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) { 00390 init(Val, Ptr); 00391 } 00392 00393 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, 00394 Instruction *InsertBefore) 00395 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) { 00396 init(Val, Ptr); 00397 } 00398 00399 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, 00400 BasicBlock *InsertAtEnd) 00401 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) { 00402 init(Val, Ptr); 00403 } 00404 00405 void StoreInst::init(Value *Val, Value *Ptr) { 00406 assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!"); 00407 assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType() 00408 && "Ptr must be a pointer to Val type!"); 00409 00410 Operands.reserve(2); 00411 Operands.push_back(Use(Val, this)); 00412 Operands.push_back(Use(Ptr, this)); 00413 } 00414 00415 //===----------------------------------------------------------------------===// 00416 // GetElementPtrInst Implementation 00417 //===----------------------------------------------------------------------===// 00418 00419 // checkType - Simple wrapper function to give a better assertion failure 00420 // message on bad indexes for a gep instruction. 00421 // 00422 static inline const Type *checkType(const Type *Ty) { 00423 assert(Ty && "Invalid indices for type!"); 00424 return Ty; 00425 } 00426 00427 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) 00428 { 00429 Operands.reserve(1+Idx.size()); 00430 Operands.push_back(Use(Ptr, this)); 00431 00432 for (unsigned i = 0, E = Idx.size(); i != E; ++i) 00433 Operands.push_back(Use(Idx[i], this)); 00434 } 00435 00436 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { 00437 Operands.reserve(3); 00438 Operands.push_back(Use(Ptr, this)); 00439 Operands.push_back(Use(Idx0, this)); 00440 Operands.push_back(Use(Idx1, this)); 00441 } 00442 00443 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, 00444 const std::string &Name, Instruction *InBe) 00445 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), 00446 Idx, true))), 00447 GetElementPtr, Name, InBe) { 00448 init(Ptr, Idx); 00449 } 00450 00451 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, 00452 const std::string &Name, BasicBlock *IAE) 00453 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), 00454 Idx, true))), 00455 GetElementPtr, Name, IAE) { 00456 init(Ptr, Idx); 00457 } 00458 00459 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, 00460 const std::string &Name, Instruction *InBe) 00461 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), 00462 Idx0, Idx1, true))), 00463 GetElementPtr, Name, InBe) { 00464 init(Ptr, Idx0, Idx1); 00465 } 00466 00467 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, 00468 const std::string &Name, BasicBlock *IAE) 00469 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), 00470 Idx0, Idx1, true))), 00471 GetElementPtr, Name, IAE) { 00472 init(Ptr, Idx0, Idx1); 00473 } 00474 00475 // getIndexedType - Returns the type of the element that would be loaded with 00476 // a load instruction with the specified parameters. 00477 // 00478 // A null type is returned if the indices are invalid for the specified 00479 // pointer type. 00480 // 00481 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 00482 const std::vector<Value*> &Idx, 00483 bool AllowCompositeLeaf) { 00484 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! 00485 00486 // Handle the special case of the empty set index set... 00487 if (Idx.empty()) 00488 if (AllowCompositeLeaf || 00489 cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) 00490 return cast<PointerType>(Ptr)->getElementType(); 00491 else 00492 return 0; 00493 00494 unsigned CurIdx = 0; 00495 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { 00496 if (Idx.size() == CurIdx) { 00497 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; 00498 return 0; // Can't load a whole structure or array!?!? 00499 } 00500 00501 Value *Index = Idx[CurIdx++]; 00502 if (isa<PointerType>(CT) && CurIdx != 1) 00503 return 0; // Can only index into pointer types at the first index! 00504 if (!CT->indexValid(Index)) return 0; 00505 Ptr = CT->getTypeAtIndex(Index); 00506 00507 // If the new type forwards to another type, then it is in the middle 00508 // of being refined to another type (and hence, may have dropped all 00509 // references to what it was using before). So, use the new forwarded 00510 // type. 00511 if (const Type * Ty = Ptr->getForwardedType()) { 00512 Ptr = Ty; 00513 } 00514 } 00515 return CurIdx == Idx.size() ? Ptr : 0; 00516 } 00517 00518 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 00519 Value *Idx0, Value *Idx1, 00520 bool AllowCompositeLeaf) { 00521 const PointerType *PTy = dyn_cast<PointerType>(Ptr); 00522 if (!PTy) return 0; // Type isn't a pointer type! 00523 00524 // Check the pointer index. 00525 if (!PTy->indexValid(Idx0)) return 0; 00526 00527 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); 00528 if (!CT || !CT->indexValid(Idx1)) return 0; 00529 00530 const Type *ElTy = CT->getTypeAtIndex(Idx1); 00531 if (AllowCompositeLeaf || ElTy->isFirstClassType()) 00532 return ElTy; 00533 return 0; 00534 } 00535 00536 //===----------------------------------------------------------------------===// 00537 // BinaryOperator Class 00538 //===----------------------------------------------------------------------===// 00539 00540 void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2) 00541 { 00542 Operands.reserve(2); 00543 Operands.push_back(Use(S1, this)); 00544 Operands.push_back(Use(S2, this)); 00545 assert(S1 && S2 && S1->getType() == S2->getType()); 00546 00547 #ifndef NDEBUG 00548 switch (iType) { 00549 case Add: case Sub: 00550 case Mul: case Div: 00551 case Rem: 00552 assert(getType() == S1->getType() && 00553 "Arithmetic operation should return same type as operands!"); 00554 assert((getType()->isInteger() || 00555 getType()->isFloatingPoint() || 00556 isa<PackedType>(getType()) ) && 00557 "Tried to create an arithmetic operation on a non-arithmetic type!"); 00558 break; 00559 case And: case Or: 00560 case Xor: 00561 assert(getType() == S1->getType() && 00562 "Logical operation should return same type as operands!"); 00563 assert(getType()->isIntegral() && 00564 "Tried to create an logical operation on a non-integral type!"); 00565 break; 00566 case SetLT: case SetGT: case SetLE: 00567 case SetGE: case SetEQ: case SetNE: 00568 assert(getType() == Type::BoolTy && "Setcc must return bool!"); 00569 default: 00570 break; 00571 } 00572 #endif 00573 } 00574 00575 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, 00576 const std::string &Name, 00577 Instruction *InsertBefore) { 00578 assert(S1->getType() == S2->getType() && 00579 "Cannot create binary operator with two operands of differing type!"); 00580 switch (Op) { 00581 // Binary comparison operators... 00582 case SetLT: case SetGT: case SetLE: 00583 case SetGE: case SetEQ: case SetNE: 00584 return new SetCondInst(Op, S1, S2, Name, InsertBefore); 00585 00586 default: 00587 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); 00588 } 00589 } 00590 00591 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, 00592 const std::string &Name, 00593 BasicBlock *InsertAtEnd) { 00594 BinaryOperator *Res = create(Op, S1, S2, Name); 00595 InsertAtEnd->getInstList().push_back(Res); 00596 return Res; 00597 } 00598 00599 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, 00600 Instruction *InsertBefore) { 00601 if (!Op->getType()->isFloatingPoint()) 00602 return new BinaryOperator(Instruction::Sub, 00603 Constant::getNullValue(Op->getType()), Op, 00604 Op->getType(), Name, InsertBefore); 00605 else 00606 return new BinaryOperator(Instruction::Sub, 00607 ConstantFP::get(Op->getType(), -0.0), Op, 00608 Op->getType(), Name, InsertBefore); 00609 } 00610 00611 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, 00612 BasicBlock *InsertAtEnd) { 00613 if (!Op->getType()->isFloatingPoint()) 00614 return new BinaryOperator(Instruction::Sub, 00615 Constant::getNullValue(Op->getType()), Op, 00616 Op->getType(), Name, InsertAtEnd); 00617 else 00618 return new BinaryOperator(Instruction::Sub, 00619 ConstantFP::get(Op->getType(), -0.0), Op, 00620 Op->getType(), Name, InsertAtEnd); 00621 } 00622 00623 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, 00624 Instruction *InsertBefore) { 00625 return new BinaryOperator(Instruction::Xor, Op, 00626 ConstantIntegral::getAllOnesValue(Op->getType()), 00627 Op->getType(), Name, InsertBefore); 00628 } 00629 00630 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, 00631 BasicBlock *InsertAtEnd) { 00632 return new BinaryOperator(Instruction::Xor, Op, 00633 ConstantIntegral::getAllOnesValue(Op->getType()), 00634 Op->getType(), Name, InsertAtEnd); 00635 } 00636 00637 00638 // isConstantAllOnes - Helper function for several functions below 00639 static inline bool isConstantAllOnes(const Value *V) { 00640 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue(); 00641 } 00642 00643 bool BinaryOperator::isNeg(const Value *V) { 00644 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) 00645 if (Bop->getOpcode() == Instruction::Sub) 00646 if (!V->getType()->isFloatingPoint()) 00647 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); 00648 else 00649 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); 00650 return false; 00651 } 00652 00653 bool BinaryOperator::isNot(const Value *V) { 00654 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) 00655 return (Bop->getOpcode() == Instruction::Xor && 00656 (isConstantAllOnes(Bop->getOperand(1)) || 00657 isConstantAllOnes(Bop->getOperand(0)))); 00658 return false; 00659 } 00660 00661 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) { 00662 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!"); 00663 return Bop->getOperand(1); 00664 } 00665 00666 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) { 00667 return getNegArgument((BinaryOperator*)Bop); 00668 } 00669 00670 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) { 00671 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!"); 00672 Value *Op0 = Bop->getOperand(0); 00673 Value *Op1 = Bop->getOperand(1); 00674 if (isConstantAllOnes(Op0)) return Op1; 00675 00676 assert(isConstantAllOnes(Op1)); 00677 return Op0; 00678 } 00679 00680 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) { 00681 return getNotArgument((BinaryOperator*)Bop); 00682 } 00683 00684 00685 // swapOperands - Exchange the two operands to this instruction. This 00686 // instruction is safe to use on any binary instruction and does not 00687 // modify the semantics of the instruction. If the instruction is 00688 // order dependent (SetLT f.e.) the opcode is changed. 00689 // 00690 bool BinaryOperator::swapOperands() { 00691 if (isCommutative()) 00692 ; // If the instruction is commutative, it is safe to swap the operands 00693 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) 00694 /// FIXME: SetCC instructions shouldn't all have different opcodes. 00695 setOpcode(SCI->getSwappedCondition()); 00696 else 00697 return true; // Can't commute operands 00698 00699 std::swap(Operands[0], Operands[1]); 00700 return false; 00701 } 00702 00703 00704 //===----------------------------------------------------------------------===// 00705 // SetCondInst Class 00706 //===----------------------------------------------------------------------===// 00707 00708 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, 00709 const std::string &Name, Instruction *InsertBefore) 00710 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) { 00711 00712 // Make sure it's a valid type... getInverseCondition will assert out if not. 00713 assert(getInverseCondition(Opcode)); 00714 } 00715 00716 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, 00717 const std::string &Name, BasicBlock *InsertAtEnd) 00718 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) { 00719 00720 // Make sure it's a valid type... getInverseCondition will assert out if not. 00721 assert(getInverseCondition(Opcode)); 00722 } 00723 00724 // getInverseCondition - Return the inverse of the current condition opcode. 00725 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc... 00726 // 00727 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) { 00728 switch (Opcode) { 00729 default: 00730 assert(0 && "Unknown setcc opcode!"); 00731 case SetEQ: return SetNE; 00732 case SetNE: return SetEQ; 00733 case SetGT: return SetLE; 00734 case SetLT: return SetGE; 00735 case SetGE: return SetLT; 00736 case SetLE: return SetGT; 00737 } 00738 } 00739 00740 // getSwappedCondition - Return the condition opcode that would be the result 00741 // of exchanging the two operands of the setcc instruction without changing 00742 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. 00743 // 00744 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) { 00745 switch (Opcode) { 00746 default: assert(0 && "Unknown setcc instruction!"); 00747 case SetEQ: case SetNE: return Opcode; 00748 case SetGT: return SetLT; 00749 case SetLT: return SetGT; 00750 case SetGE: return SetLE; 00751 case SetLE: return SetGE; 00752 } 00753 } 00754 00755 //===----------------------------------------------------------------------===// 00756 // SwitchInst Implementation 00757 //===----------------------------------------------------------------------===// 00758 00759 void SwitchInst::init(Value *Value, BasicBlock *Default) 00760 { 00761 assert(Value && Default); 00762 Operands.push_back(Use(Value, this)); 00763 Operands.push_back(Use(Default, this)); 00764 } 00765 00766 SwitchInst::SwitchInst(const SwitchInst &SI) 00767 : TerminatorInst(Instruction::Switch) { 00768 Operands.reserve(SI.Operands.size()); 00769 00770 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) { 00771 Operands.push_back(Use(SI.Operands[i], this)); 00772 Operands.push_back(Use(SI.Operands[i+1], this)); 00773 } 00774 } 00775 00776 /// addCase - Add an entry to the switch instruction... 00777 /// 00778 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) { 00779 Operands.push_back(Use((Value*)OnVal, this)); 00780 Operands.push_back(Use((Value*)Dest, this)); 00781 } 00782 00783 /// removeCase - This method removes the specified successor from the switch 00784 /// instruction. Note that this cannot be used to remove the default 00785 /// destination (successor #0). 00786 /// 00787 void SwitchInst::removeCase(unsigned idx) { 00788 assert(idx != 0 && "Cannot remove the default case!"); 00789 assert(idx*2 < Operands.size() && "Successor index out of range!!!"); 00790 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2); 00791 } 00792 00793 00794 // Define these methods here so vtables don't get emitted into every translation 00795 // unit that uses these classes. 00796 00797 GetElementPtrInst *GetElementPtrInst::clone() const { 00798 return new GetElementPtrInst(*this); 00799 } 00800 00801 BinaryOperator *BinaryOperator::clone() const { 00802 return create(getOpcode(), Operands[0], Operands[1]); 00803 } 00804 00805 MallocInst *MallocInst::clone() const { return new MallocInst(*this); } 00806 AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } 00807 FreeInst *FreeInst::clone() const { return new FreeInst(Operands[0]); } 00808 LoadInst *LoadInst::clone() const { return new LoadInst(*this); } 00809 StoreInst *StoreInst::clone() const { return new StoreInst(*this); } 00810 CastInst *CastInst::clone() const { return new CastInst(*this); } 00811 CallInst *CallInst::clone() const { return new CallInst(*this); } 00812 ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } 00813 SelectInst *SelectInst::clone() const { return new SelectInst(*this); } 00814 VANextInst *VANextInst::clone() const { return new VANextInst(*this); } 00815 VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } 00816 PHINode *PHINode::clone() const { return new PHINode(*this); } 00817 ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } 00818 BranchInst *BranchInst::clone() const { return new BranchInst(*this); } 00819 SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } 00820 InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } 00821 UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } 00822 UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}