Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM instructions... |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/BasicBlock.h" |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Instructions.h" |
| 19 | #include "llvm/Support/CallSite.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // CallInst Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | void CallInst::init(Value *Func, const std::vector<Value*> &Params) |
| 27 | { |
| 28 | Operands.reserve(1+Params.size()); |
| 29 | Operands.push_back(Use(Func, this)); |
| 30 | |
| 31 | const FunctionType *FTy = |
| 32 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 33 | |
| 34 | assert((Params.size() == FTy->getNumParams() || |
| 35 | (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && |
| 36 | "Calling a function with bad signature"); |
| 37 | for (unsigned i = 0; i != Params.size(); i++) |
| 38 | Operands.push_back(Use(Params[i], this)); |
| 39 | } |
| 40 | |
| 41 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) |
| 42 | { |
| 43 | Operands.reserve(3); |
| 44 | Operands.push_back(Use(Func, this)); |
| 45 | |
| 46 | const FunctionType *MTy = |
| 47 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 48 | |
| 49 | assert((MTy->getNumParams() == 2 || |
| 50 | (MTy->isVarArg() && MTy->getNumParams() == 0)) && |
| 51 | "Calling a function with bad signature"); |
| 52 | Operands.push_back(Use(Actual1, this)); |
| 53 | Operands.push_back(Use(Actual2, this)); |
| 54 | } |
| 55 | |
| 56 | void CallInst::init(Value *Func, Value *Actual) |
| 57 | { |
| 58 | Operands.reserve(2); |
| 59 | Operands.push_back(Use(Func, this)); |
| 60 | |
| 61 | const FunctionType *MTy = |
| 62 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 63 | |
| 64 | assert((MTy->getNumParams() == 1 || |
| 65 | (MTy->isVarArg() && MTy->getNumParams() == 0)) && |
| 66 | "Calling a function with bad signature"); |
| 67 | Operands.push_back(Use(Actual, this)); |
| 68 | } |
| 69 | |
| 70 | void CallInst::init(Value *Func) |
| 71 | { |
| 72 | Operands.reserve(1); |
| 73 | Operands.push_back(Use(Func, this)); |
| 74 | |
| 75 | const FunctionType *MTy = |
| 76 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 77 | |
| 78 | assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); |
| 79 | } |
| 80 | |
| 81 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 82 | const std::string &Name, Instruction *InsertBefore) |
| 83 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 84 | ->getElementType())->getReturnType(), |
| 85 | Instruction::Call, Name, InsertBefore) { |
| 86 | init(Func, Params); |
| 87 | } |
| 88 | |
| 89 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 90 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 91 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 92 | ->getElementType())->getReturnType(), |
| 93 | Instruction::Call, Name, InsertAtEnd) { |
| 94 | init(Func, Params); |
| 95 | } |
| 96 | |
| 97 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 98 | const std::string &Name, Instruction *InsertBefore) |
| 99 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 100 | ->getElementType())->getReturnType(), |
| 101 | Instruction::Call, Name, InsertBefore) { |
| 102 | init(Func, Actual1, Actual2); |
| 103 | } |
| 104 | |
| 105 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 106 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 107 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 108 | ->getElementType())->getReturnType(), |
| 109 | Instruction::Call, Name, InsertAtEnd) { |
| 110 | init(Func, Actual1, Actual2); |
| 111 | } |
| 112 | |
| 113 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 114 | Instruction *InsertBefore) |
| 115 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 116 | ->getElementType())->getReturnType(), |
| 117 | Instruction::Call, Name, InsertBefore) { |
| 118 | init(Func, Actual); |
| 119 | } |
| 120 | |
| 121 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 122 | BasicBlock *InsertAtEnd) |
| 123 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 124 | ->getElementType())->getReturnType(), |
| 125 | Instruction::Call, Name, InsertAtEnd) { |
| 126 | init(Func, Actual); |
| 127 | } |
| 128 | |
| 129 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 130 | Instruction *InsertBefore) |
| 131 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 132 | ->getElementType())->getReturnType(), |
| 133 | Instruction::Call, Name, InsertBefore) { |
| 134 | init(Func); |
| 135 | } |
| 136 | |
| 137 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 138 | BasicBlock *InsertAtEnd) |
| 139 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 140 | ->getElementType())->getReturnType(), |
| 141 | Instruction::Call, Name, InsertAtEnd) { |
| 142 | init(Func); |
| 143 | } |
| 144 | |
| 145 | CallInst::CallInst(const CallInst &CI) |
| 146 | : Instruction(CI.getType(), Instruction::Call) { |
| 147 | Operands.reserve(CI.Operands.size()); |
| 148 | for (unsigned i = 0; i < CI.Operands.size(); ++i) |
| 149 | Operands.push_back(Use(CI.Operands[i], this)); |
| 150 | } |
| 151 | |
| 152 | const Function *CallInst::getCalledFunction() const { |
| 153 | if (const Function *F = dyn_cast<Function>(Operands[0])) |
| 154 | return F; |
| 155 | return 0; |
| 156 | } |
| 157 | Function *CallInst::getCalledFunction() { |
| 158 | if (Function *F = dyn_cast<Function>(Operands[0])) |
| 159 | return F; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | // InvokeInst Implementation |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
| 169 | const std::vector<Value*> &Params) |
| 170 | { |
| 171 | Operands.reserve(3+Params.size()); |
| 172 | Operands.push_back(Use(Fn, this)); |
| 173 | Operands.push_back(Use((Value*)IfNormal, this)); |
| 174 | Operands.push_back(Use((Value*)IfException, this)); |
| 175 | const FunctionType *MTy = |
| 176 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
| 177 | |
| 178 | assert((Params.size() == MTy->getNumParams()) || |
| 179 | (MTy->isVarArg() && Params.size() > MTy->getNumParams()) && |
| 180 | "Calling a function with bad signature"); |
| 181 | |
| 182 | for (unsigned i = 0; i < Params.size(); i++) |
| 183 | Operands.push_back(Use(Params[i], this)); |
| 184 | } |
| 185 | |
| 186 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 187 | BasicBlock *IfException, |
| 188 | const std::vector<Value*> &Params, |
| 189 | const std::string &Name, Instruction *InsertBefore) |
| 190 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 191 | ->getElementType())->getReturnType(), |
| 192 | Instruction::Invoke, Name, InsertBefore) { |
| 193 | init(Fn, IfNormal, IfException, Params); |
| 194 | } |
| 195 | |
| 196 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 197 | BasicBlock *IfException, |
| 198 | const std::vector<Value*> &Params, |
| 199 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 200 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 201 | ->getElementType())->getReturnType(), |
| 202 | Instruction::Invoke, Name, InsertAtEnd) { |
| 203 | init(Fn, IfNormal, IfException, Params); |
| 204 | } |
| 205 | |
| 206 | InvokeInst::InvokeInst(const InvokeInst &CI) |
| 207 | : TerminatorInst(CI.getType(), Instruction::Invoke) { |
| 208 | Operands.reserve(CI.Operands.size()); |
| 209 | for (unsigned i = 0; i < CI.Operands.size(); ++i) |
| 210 | Operands.push_back(Use(CI.Operands[i], this)); |
| 211 | } |
| 212 | |
| 213 | const Function *InvokeInst::getCalledFunction() const { |
| 214 | if (const Function *F = dyn_cast<Function>(Operands[0])) |
| 215 | return F; |
| 216 | return 0; |
| 217 | } |
| 218 | Function *InvokeInst::getCalledFunction() { |
| 219 | if (Function *F = dyn_cast<Function>(Operands[0])) |
| 220 | return F; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | // FIXME: Is this supposed to be here? |
| 225 | Function *CallSite::getCalledFunction() const { |
| 226 | Value *Callee = getCalledValue(); |
| 227 | if (Function *F = dyn_cast<Function>(Callee)) |
| 228 | return F; |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | // ReturnInst Implementation |
| 234 | //===----------------------------------------------------------------------===// |
| 235 | |
| 236 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 237 | // emit the vtable for the class in this translation unit. |
| 238 | void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
| 239 | assert(0 && "ReturnInst has no successors!"); |
| 240 | } |
| 241 | |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | // UnwindInst Implementation |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
| 246 | // Likewise for UnwindInst |
| 247 | void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
| 248 | assert(0 && "UnwindInst has no successors!"); |
| 249 | } |
| 250 | |
| 251 | //===----------------------------------------------------------------------===// |
| 252 | // BranchInst Implementation |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
| 255 | void BranchInst::init(BasicBlock *IfTrue) |
| 256 | { |
| 257 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 258 | Operands.reserve(1); |
| 259 | Operands.push_back(Use(IfTrue, this)); |
| 260 | } |
| 261 | |
| 262 | void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond) |
| 263 | { |
| 264 | assert(IfTrue && IfFalse && Cond && |
| 265 | "Branch destinations and condition may not be null!"); |
| 266 | assert(Cond && Cond->getType() == Type::BoolTy && |
| 267 | "May only branch on boolean predicates!"); |
| 268 | Operands.reserve(3); |
| 269 | Operands.push_back(Use(IfTrue, this)); |
| 270 | Operands.push_back(Use(IfFalse, this)); |
| 271 | Operands.push_back(Use(Cond, this)); |
| 272 | } |
| 273 | |
| 274 | BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) { |
| 275 | Operands.reserve(BI.Operands.size()); |
| 276 | Operands.push_back(Use(BI.Operands[0], this)); |
| 277 | if (BI.Operands.size() != 1) { |
| 278 | assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!"); |
| 279 | Operands.push_back(Use(BI.Operands[1], this)); |
| 280 | Operands.push_back(Use(BI.Operands[2], this)); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | // AllocationInst Implementation |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
| 288 | void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) { |
| 289 | assert(Ty != Type::VoidTy && "Cannot allocate void elements!"); |
| 290 | // ArraySize defaults to 1. |
| 291 | if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1); |
| 292 | |
| 293 | Operands.reserve(1); |
| 294 | assert(ArraySize->getType() == Type::UIntTy && |
| 295 | "Malloc/Allocation array size != UIntTy!"); |
| 296 | |
| 297 | Operands.push_back(Use(ArraySize, this)); |
| 298 | } |
| 299 | |
| 300 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
| 301 | const std::string &Name, |
| 302 | Instruction *InsertBefore) |
| 303 | : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) { |
| 304 | init(Ty, ArraySize, iTy); |
| 305 | } |
| 306 | |
| 307 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
| 308 | const std::string &Name, |
| 309 | BasicBlock *InsertAtEnd) |
| 310 | : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) { |
| 311 | init(Ty, ArraySize, iTy); |
| 312 | } |
| 313 | |
| 314 | bool AllocationInst::isArrayAllocation() const { |
| 315 | return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1); |
| 316 | } |
| 317 | |
| 318 | const Type *AllocationInst::getAllocatedType() const { |
| 319 | return getType()->getElementType(); |
| 320 | } |
| 321 | |
| 322 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 323 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
| 324 | Instruction::Alloca) { |
| 325 | } |
| 326 | |
| 327 | MallocInst::MallocInst(const MallocInst &MI) |
| 328 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
| 329 | Instruction::Malloc) { |
| 330 | } |
| 331 | |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // FreeInst Implementation |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
| 336 | void FreeInst::init(Value *Ptr) |
| 337 | { |
| 338 | assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!"); |
| 339 | Operands.reserve(1); |
| 340 | Operands.push_back(Use(Ptr, this)); |
| 341 | } |
| 342 | |
| 343 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
| 344 | : Instruction(Type::VoidTy, Free, "", InsertBefore) { |
| 345 | init(Ptr); |
| 346 | } |
| 347 | |
| 348 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
| 349 | : Instruction(Type::VoidTy, Free, "", InsertAtEnd) { |
| 350 | init(Ptr); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | // LoadInst Implementation |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | void LoadInst::init(Value *Ptr) { |
| 359 | assert(Ptr && isa<PointerType>(Ptr->getType()) && |
| 360 | "Ptr must have pointer type."); |
| 361 | Operands.reserve(1); |
| 362 | Operands.push_back(Use(Ptr, this)); |
| 363 | } |
| 364 | |
| 365 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
| 366 | : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 367 | Load, Name, InsertBef), Volatile(false) { |
| 368 | init(Ptr); |
| 369 | } |
| 370 | |
| 371 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
| 372 | : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 373 | Load, Name, InsertAE), Volatile(false) { |
| 374 | init(Ptr); |
| 375 | } |
| 376 | |
| 377 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 378 | Instruction *InsertBef) |
| 379 | : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 380 | Load, Name, InsertBef), Volatile(isVolatile) { |
| 381 | init(Ptr); |
| 382 | } |
| 383 | |
| 384 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 385 | BasicBlock *InsertAE) |
| 386 | : Instruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 387 | Load, Name, InsertAE), Volatile(isVolatile) { |
| 388 | init(Ptr); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | // StoreInst Implementation |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore) |
| 397 | : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) { |
| 398 | init(Val, Ptr); |
| 399 | } |
| 400 | |
| 401 | StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd) |
| 402 | : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) { |
| 403 | init(Val, Ptr); |
| 404 | } |
| 405 | |
| 406 | StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
| 407 | Instruction *InsertBefore) |
| 408 | : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) { |
| 409 | init(Val, Ptr); |
| 410 | } |
| 411 | |
| 412 | StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, |
| 413 | BasicBlock *InsertAtEnd) |
| 414 | : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) { |
| 415 | init(Val, Ptr); |
| 416 | } |
| 417 | |
| 418 | void StoreInst::init(Value *Val, Value *Ptr) { |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 419 | assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!"); |
| 420 | assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType() |
| 421 | && "Ptr must be a pointer to Val type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 422 | |
| 423 | Operands.reserve(2); |
| 424 | Operands.push_back(Use(Val, this)); |
| 425 | Operands.push_back(Use(Ptr, this)); |
| 426 | } |
| 427 | |
| 428 | //===----------------------------------------------------------------------===// |
| 429 | // GetElementPtrInst Implementation |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
| 432 | // checkType - Simple wrapper function to give a better assertion failure |
| 433 | // message on bad indexes for a gep instruction. |
| 434 | // |
| 435 | static inline const Type *checkType(const Type *Ty) { |
| 436 | assert(Ty && "Invalid indices for type!"); |
| 437 | return Ty; |
| 438 | } |
| 439 | |
| 440 | void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) |
| 441 | { |
| 442 | Operands.reserve(1+Idx.size()); |
| 443 | Operands.push_back(Use(Ptr, this)); |
| 444 | |
| 445 | for (unsigned i = 0, E = Idx.size(); i != E; ++i) |
| 446 | Operands.push_back(Use(Idx[i], this)); |
| 447 | } |
| 448 | |
| 449 | void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { |
| 450 | Operands.reserve(3); |
| 451 | Operands.push_back(Use(Ptr, this)); |
| 452 | Operands.push_back(Use(Idx0, this)); |
| 453 | Operands.push_back(Use(Idx1, this)); |
| 454 | } |
| 455 | |
| 456 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
| 457 | const std::string &Name, Instruction *InBe) |
| 458 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 459 | Idx, true))), |
| 460 | GetElementPtr, Name, InBe) { |
| 461 | init(Ptr, Idx); |
| 462 | } |
| 463 | |
| 464 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
| 465 | const std::string &Name, BasicBlock *IAE) |
| 466 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 467 | Idx, true))), |
| 468 | GetElementPtr, Name, IAE) { |
| 469 | init(Ptr, Idx); |
| 470 | } |
| 471 | |
| 472 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 473 | const std::string &Name, Instruction *InBe) |
| 474 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 475 | Idx0, Idx1, true))), |
| 476 | GetElementPtr, Name, InBe) { |
| 477 | init(Ptr, Idx0, Idx1); |
| 478 | } |
| 479 | |
| 480 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 481 | const std::string &Name, BasicBlock *IAE) |
| 482 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 483 | Idx0, Idx1, true))), |
| 484 | GetElementPtr, Name, IAE) { |
| 485 | init(Ptr, Idx0, Idx1); |
| 486 | } |
| 487 | |
| 488 | // getIndexedType - Returns the type of the element that would be loaded with |
| 489 | // a load instruction with the specified parameters. |
| 490 | // |
| 491 | // A null type is returned if the indices are invalid for the specified |
| 492 | // pointer type. |
| 493 | // |
| 494 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 495 | const std::vector<Value*> &Idx, |
| 496 | bool AllowCompositeLeaf) { |
| 497 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 498 | |
| 499 | // Handle the special case of the empty set index set... |
| 500 | if (Idx.empty()) |
| 501 | if (AllowCompositeLeaf || |
| 502 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 503 | return cast<PointerType>(Ptr)->getElementType(); |
| 504 | else |
| 505 | return 0; |
| 506 | |
| 507 | unsigned CurIdx = 0; |
| 508 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| 509 | if (Idx.size() == CurIdx) { |
| 510 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 511 | return 0; // Can't load a whole structure or array!?!? |
| 512 | } |
| 513 | |
| 514 | Value *Index = Idx[CurIdx++]; |
| 515 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 516 | return 0; // Can only index into pointer types at the first index! |
| 517 | if (!CT->indexValid(Index)) return 0; |
| 518 | Ptr = CT->getTypeAtIndex(Index); |
| 519 | |
| 520 | // If the new type forwards to another type, then it is in the middle |
| 521 | // of being refined to another type (and hence, may have dropped all |
| 522 | // references to what it was using before). So, use the new forwarded |
| 523 | // type. |
| 524 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 525 | Ptr = Ty; |
| 526 | } |
| 527 | } |
| 528 | return CurIdx == Idx.size() ? Ptr : 0; |
| 529 | } |
| 530 | |
| 531 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 532 | Value *Idx0, Value *Idx1, |
| 533 | bool AllowCompositeLeaf) { |
| 534 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 535 | if (!PTy) return 0; // Type isn't a pointer type! |
| 536 | |
| 537 | // Check the pointer index. |
| 538 | if (!PTy->indexValid(Idx0)) return 0; |
| 539 | |
| 540 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 541 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 542 | |
| 543 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 544 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 545 | return ElTy; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | //===----------------------------------------------------------------------===// |
| 550 | // BinaryOperator Class |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | |
| 553 | void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2) |
| 554 | { |
| 555 | Operands.reserve(2); |
| 556 | Operands.push_back(Use(S1, this)); |
| 557 | Operands.push_back(Use(S2, this)); |
| 558 | assert(S1 && S2 && S1->getType() == S2->getType()); |
| 559 | |
| 560 | #ifndef NDEBUG |
| 561 | switch (iType) { |
| 562 | case Add: case Sub: |
| 563 | case Mul: case Div: |
| 564 | case Rem: |
| 565 | assert(getType() == S1->getType() && |
| 566 | "Arithmetic operation should return same type as operands!"); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 567 | assert((getType()->isInteger() || |
| 568 | getType()->isFloatingPoint() || |
| 569 | isa<PackedType>(getType()) ) && |
| 570 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 571 | break; |
| 572 | case And: case Or: |
| 573 | case Xor: |
| 574 | assert(getType() == S1->getType() && |
| 575 | "Logical operation should return same type as operands!"); |
| 576 | assert(getType()->isIntegral() && |
| 577 | "Tried to create an logical operation on a non-integral type!"); |
| 578 | break; |
| 579 | case SetLT: case SetGT: case SetLE: |
| 580 | case SetGE: case SetEQ: case SetNE: |
| 581 | assert(getType() == Type::BoolTy && "Setcc must return bool!"); |
| 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | #endif |
| 586 | } |
| 587 | |
| 588 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
| 589 | const std::string &Name, |
| 590 | Instruction *InsertBefore) { |
| 591 | assert(S1->getType() == S2->getType() && |
| 592 | "Cannot create binary operator with two operands of differing type!"); |
| 593 | switch (Op) { |
| 594 | // Binary comparison operators... |
| 595 | case SetLT: case SetGT: case SetLE: |
| 596 | case SetGE: case SetEQ: case SetNE: |
| 597 | return new SetCondInst(Op, S1, S2, Name, InsertBefore); |
| 598 | |
| 599 | default: |
| 600 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
| 605 | const std::string &Name, |
| 606 | BasicBlock *InsertAtEnd) { |
| 607 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 608 | InsertAtEnd->getInstList().push_back(Res); |
| 609 | return Res; |
| 610 | } |
| 611 | |
| 612 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 613 | Instruction *InsertBefore) { |
| 614 | if (!Op->getType()->isFloatingPoint()) |
| 615 | return new BinaryOperator(Instruction::Sub, |
| 616 | Constant::getNullValue(Op->getType()), Op, |
| 617 | Op->getType(), Name, InsertBefore); |
| 618 | else |
| 619 | return new BinaryOperator(Instruction::Sub, |
| 620 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 621 | Op->getType(), Name, InsertBefore); |
| 622 | } |
| 623 | |
| 624 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 625 | BasicBlock *InsertAtEnd) { |
| 626 | if (!Op->getType()->isFloatingPoint()) |
| 627 | return new BinaryOperator(Instruction::Sub, |
| 628 | Constant::getNullValue(Op->getType()), Op, |
| 629 | Op->getType(), Name, InsertAtEnd); |
| 630 | else |
| 631 | return new BinaryOperator(Instruction::Sub, |
| 632 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 633 | Op->getType(), Name, InsertAtEnd); |
| 634 | } |
| 635 | |
| 636 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 637 | Instruction *InsertBefore) { |
| 638 | return new BinaryOperator(Instruction::Xor, Op, |
| 639 | ConstantIntegral::getAllOnesValue(Op->getType()), |
| 640 | Op->getType(), Name, InsertBefore); |
| 641 | } |
| 642 | |
| 643 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 644 | BasicBlock *InsertAtEnd) { |
| 645 | return new BinaryOperator(Instruction::Xor, Op, |
| 646 | ConstantIntegral::getAllOnesValue(Op->getType()), |
| 647 | Op->getType(), Name, InsertAtEnd); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | // isConstantAllOnes - Helper function for several functions below |
| 652 | static inline bool isConstantAllOnes(const Value *V) { |
| 653 | return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue(); |
| 654 | } |
| 655 | |
| 656 | bool BinaryOperator::isNeg(const Value *V) { |
| 657 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 658 | if (Bop->getOpcode() == Instruction::Sub) |
| 659 | if (!V->getType()->isFloatingPoint()) |
| 660 | return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); |
| 661 | else |
| 662 | return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | bool BinaryOperator::isNot(const Value *V) { |
| 667 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 668 | return (Bop->getOpcode() == Instruction::Xor && |
| 669 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 670 | isConstantAllOnes(Bop->getOperand(0)))); |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) { |
| 675 | assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!"); |
| 676 | return Bop->getOperand(1); |
| 677 | } |
| 678 | |
| 679 | const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) { |
| 680 | return getNegArgument((BinaryOperator*)Bop); |
| 681 | } |
| 682 | |
| 683 | Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) { |
| 684 | assert(isNot(Bop) && "getNotArgument on non-'not' instruction!"); |
| 685 | Value *Op0 = Bop->getOperand(0); |
| 686 | Value *Op1 = Bop->getOperand(1); |
| 687 | if (isConstantAllOnes(Op0)) return Op1; |
| 688 | |
| 689 | assert(isConstantAllOnes(Op1)); |
| 690 | return Op0; |
| 691 | } |
| 692 | |
| 693 | const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) { |
| 694 | return getNotArgument((BinaryOperator*)Bop); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | // swapOperands - Exchange the two operands to this instruction. This |
| 699 | // instruction is safe to use on any binary instruction and does not |
| 700 | // modify the semantics of the instruction. If the instruction is |
| 701 | // order dependent (SetLT f.e.) the opcode is changed. |
| 702 | // |
| 703 | bool BinaryOperator::swapOperands() { |
| 704 | if (isCommutative()) |
| 705 | ; // If the instruction is commutative, it is safe to swap the operands |
| 706 | else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) |
| 707 | /// FIXME: SetCC instructions shouldn't all have different opcodes. |
| 708 | setOpcode(SCI->getSwappedCondition()); |
| 709 | else |
| 710 | return true; // Can't commute operands |
| 711 | |
| 712 | std::swap(Operands[0], Operands[1]); |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | // SetCondInst Class |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
| 721 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
| 722 | const std::string &Name, Instruction *InsertBefore) |
| 723 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) { |
| 724 | |
| 725 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 726 | assert(getInverseCondition(Opcode)); |
| 727 | } |
| 728 | |
| 729 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
| 730 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 731 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) { |
| 732 | |
| 733 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 734 | assert(getInverseCondition(Opcode)); |
| 735 | } |
| 736 | |
| 737 | // getInverseCondition - Return the inverse of the current condition opcode. |
| 738 | // For example seteq -> setne, setgt -> setle, setlt -> setge, etc... |
| 739 | // |
| 740 | Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) { |
| 741 | switch (Opcode) { |
| 742 | default: |
| 743 | assert(0 && "Unknown setcc opcode!"); |
| 744 | case SetEQ: return SetNE; |
| 745 | case SetNE: return SetEQ; |
| 746 | case SetGT: return SetLE; |
| 747 | case SetLT: return SetGE; |
| 748 | case SetGE: return SetLT; |
| 749 | case SetLE: return SetGT; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // getSwappedCondition - Return the condition opcode that would be the result |
| 754 | // of exchanging the two operands of the setcc instruction without changing |
| 755 | // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. |
| 756 | // |
| 757 | Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) { |
| 758 | switch (Opcode) { |
| 759 | default: assert(0 && "Unknown setcc instruction!"); |
| 760 | case SetEQ: case SetNE: return Opcode; |
| 761 | case SetGT: return SetLT; |
| 762 | case SetLT: return SetGT; |
| 763 | case SetGE: return SetLE; |
| 764 | case SetLE: return SetGE; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | //===----------------------------------------------------------------------===// |
| 769 | // SwitchInst Implementation |
| 770 | //===----------------------------------------------------------------------===// |
| 771 | |
| 772 | void SwitchInst::init(Value *Value, BasicBlock *Default) |
| 773 | { |
| 774 | assert(Value && Default); |
| 775 | Operands.push_back(Use(Value, this)); |
| 776 | Operands.push_back(Use(Default, this)); |
| 777 | } |
| 778 | |
| 779 | SwitchInst::SwitchInst(const SwitchInst &SI) |
| 780 | : TerminatorInst(Instruction::Switch) { |
| 781 | Operands.reserve(SI.Operands.size()); |
| 782 | |
| 783 | for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) { |
| 784 | Operands.push_back(Use(SI.Operands[i], this)); |
| 785 | Operands.push_back(Use(SI.Operands[i+1], this)); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /// addCase - Add an entry to the switch instruction... |
| 790 | /// |
| 791 | void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) { |
| 792 | Operands.push_back(Use((Value*)OnVal, this)); |
| 793 | Operands.push_back(Use((Value*)Dest, this)); |
| 794 | } |
| 795 | |
| 796 | /// removeCase - This method removes the specified successor from the switch |
| 797 | /// instruction. Note that this cannot be used to remove the default |
| 798 | /// destination (successor #0). |
| 799 | /// |
| 800 | void SwitchInst::removeCase(unsigned idx) { |
| 801 | assert(idx != 0 && "Cannot remove the default case!"); |
| 802 | assert(idx*2 < Operands.size() && "Successor index out of range!!!"); |
| 803 | Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2); |
| 804 | } |