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