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