LLVM API Documentation

Instruction.cpp

Go to the documentation of this file.
00001 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Instruction class for the VMCore library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Type.h"
00015 #include "llvm/Instructions.h"
00016 #include "llvm/Function.h"
00017 #include "llvm/Support/CallSite.h"
00018 #include "llvm/Support/LeakDetector.h"
00019 using namespace llvm;
00020 
00021 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
00022                          Instruction *InsertBefore)
00023   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
00024   // Make sure that we get added to a basicblock
00025   LeakDetector::addGarbageObject(this);
00026 
00027   // If requested, insert this instruction into a basic block...
00028   if (InsertBefore) {
00029     assert(InsertBefore->getParent() &&
00030            "Instruction to insert before is not in a basic block!");
00031     InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
00032   }
00033 }
00034 
00035 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
00036                          BasicBlock *InsertAtEnd)
00037   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
00038   // Make sure that we get added to a basicblock
00039   LeakDetector::addGarbageObject(this);
00040 
00041   // append this instruction into the basic block
00042   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
00043   InsertAtEnd->getInstList().push_back(this);
00044 }
00045 
00046 
00047 // Out of line virtual method, so the vtable, etc has a home.
00048 Instruction::~Instruction() {
00049   assert(Parent == 0 && "Instruction still linked in the program!");
00050 }
00051 
00052 
00053 void Instruction::setParent(BasicBlock *P) {
00054   if (getParent()) {
00055     if (!P) LeakDetector::addGarbageObject(this);
00056   } else {
00057     if (P) LeakDetector::removeGarbageObject(this);
00058   }
00059 
00060   Parent = P;
00061 }
00062 
00063 void Instruction::removeFromParent() {
00064   getParent()->getInstList().remove(this);
00065 }
00066 
00067 void Instruction::eraseFromParent() {
00068   getParent()->getInstList().erase(this);
00069 }
00070 
00071 /// insertBefore - Insert an unlinked instructions into a basic block
00072 /// immediately before the specified instruction.
00073 void Instruction::insertBefore(Instruction *InsertPos) {
00074   InsertPos->getParent()->getInstList().insert(InsertPos, this);
00075 }
00076 
00077 /// insertAfter - Insert an unlinked instructions into a basic block
00078 /// immediately after the specified instruction.
00079 void Instruction::insertAfter(Instruction *InsertPos) {
00080   InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
00081 }
00082 
00083 /// moveBefore - Unlink this instruction from its current basic block and
00084 /// insert it into the basic block that MovePos lives in, right before
00085 /// MovePos.
00086 void Instruction::moveBefore(Instruction *MovePos) {
00087   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
00088                                              this);
00089 }
00090 
00091 
00092 const char *Instruction::getOpcodeName(unsigned OpCode) {
00093   switch (OpCode) {
00094   // Terminators
00095   case Ret:    return "ret";
00096   case Br:     return "br";
00097   case Switch: return "switch";
00098   case Invoke: return "invoke";
00099   case Unwind: return "unwind";
00100   case Unreachable: return "unreachable";
00101 
00102   // Standard binary operators...
00103   case Add: return "add";
00104   case Sub: return "sub";
00105   case Mul: return "mul";
00106   case UDiv: return "udiv";
00107   case SDiv: return "sdiv";
00108   case FDiv: return "fdiv";
00109   case URem: return "urem";
00110   case SRem: return "srem";
00111   case FRem: return "frem";
00112 
00113   // Logical operators...
00114   case And: return "and";
00115   case Or : return "or";
00116   case Xor: return "xor";
00117 
00118   // Memory instructions...
00119   case Malloc:        return "malloc";
00120   case Free:          return "free";
00121   case Alloca:        return "alloca";
00122   case Load:          return "load";
00123   case Store:         return "store";
00124   case GetElementPtr: return "getelementptr";
00125 
00126   // Convert instructions...
00127   case Trunc:     return "trunc";
00128   case ZExt:      return "zext";
00129   case SExt:      return "sext";
00130   case FPTrunc:   return "fptrunc";
00131   case FPExt:     return "fpext";
00132   case FPToUI:    return "fptoui";
00133   case FPToSI:    return "fptosi";
00134   case UIToFP:    return "uitofp";
00135   case SIToFP:    return "sitofp";
00136   case IntToPtr:  return "inttoptr";
00137   case PtrToInt:  return "ptrtoint";
00138   case BitCast:   return "bitcast";
00139 
00140   // Other instructions...
00141   case ICmp:           return "icmp";
00142   case FCmp:           return "fcmp";
00143   case VICmp:          return "vicmp";
00144   case VFCmp:          return "vfcmp";
00145   case PHI:            return "phi";
00146   case Select:         return "select";
00147   case Call:           return "call";
00148   case Shl:            return "shl";
00149   case LShr:           return "lshr";
00150   case AShr:           return "ashr";
00151   case VAArg:          return "va_arg";
00152   case ExtractElement: return "extractelement";
00153   case InsertElement:  return "insertelement";
00154   case ShuffleVector:  return "shufflevector";
00155   case ExtractValue:   return "extractvalue";
00156   case InsertValue:    return "insertvalue";
00157 
00158   default: return "<Invalid operator> ";
00159   }
00160 
00161   return 0;
00162 }
00163 
00164 /// isIdenticalTo - Return true if the specified instruction is exactly
00165 /// identical to the current one.  This means that all operands match and any
00166 /// extra information (e.g. load is volatile) agree.
00167 bool Instruction::isIdenticalTo(const Instruction *I) const {
00168   if (getOpcode() != I->getOpcode() ||
00169       getNumOperands() != I->getNumOperands() ||
00170       getType() != I->getType())
00171     return false;
00172 
00173   // We have two instructions of identical opcode and #operands.  Check to see
00174   // if all operands are the same.
00175   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00176     if (getOperand(i) != I->getOperand(i))
00177       return false;
00178 
00179   // Check special state that is a part of some instructions.
00180   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00181     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
00182            LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
00183   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00184     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
00185            SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
00186   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
00187     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
00188   if (const CallInst *CI = dyn_cast<CallInst>(this))
00189     return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
00190            CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
00191            CI->getAttributes().getRawPointer() ==
00192              cast<CallInst>(I)->getAttributes().getRawPointer();
00193   if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
00194     return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
00195            CI->getAttributes().getRawPointer() ==
00196              cast<InvokeInst>(I)->getAttributes().getRawPointer();
00197   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
00198     if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
00199       return false;
00200     for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
00201       if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
00202         return false;
00203     return true;
00204   }
00205   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
00206     if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
00207       return false;
00208     for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
00209       if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
00210         return false;
00211     return true;
00212   }
00213 
00214   return true;
00215 }
00216 
00217 // isSameOperationAs
00218 bool Instruction::isSameOperationAs(const Instruction *I) const {
00219   if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
00220       getNumOperands() != I->getNumOperands())
00221     return false;
00222 
00223   // We have two instructions of identical opcode and #operands.  Check to see
00224   // if all operands are the same type
00225   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00226     if (getOperand(i)->getType() != I->getOperand(i)->getType())
00227       return false;
00228 
00229   // Check special state that is a part of some instructions.
00230   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00231     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
00232            LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
00233   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00234     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
00235            SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
00236   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
00237     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
00238   if (const CallInst *CI = dyn_cast<CallInst>(this))
00239     return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
00240            CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
00241            CI->getAttributes().getRawPointer() ==
00242              cast<CallInst>(I)->getAttributes().getRawPointer();
00243   if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
00244     return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
00245            CI->getAttributes().getRawPointer() ==
00246              cast<InvokeInst>(I)->getAttributes().getRawPointer();
00247   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
00248     if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
00249       return false;
00250     for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
00251       if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
00252         return false;
00253     return true;
00254   }
00255   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
00256     if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
00257       return false;
00258     for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
00259       if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
00260         return false;
00261     return true;
00262   }
00263 
00264   return true;
00265 }
00266 
00267 /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
00268 /// specified block.  Note that PHI nodes are considered to evaluate their
00269 /// operands in the corresponding predecessor block.
00270 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
00271   for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
00272     // PHI nodes uses values in the corresponding predecessor block.  For other
00273     // instructions, just check to see whether the parent of the use matches up.
00274     const PHINode *PN = dyn_cast<PHINode>(*UI);
00275     if (PN == 0) {
00276       if (cast<Instruction>(*UI)->getParent() != BB)
00277         return true;
00278       continue;
00279     }
00280     
00281     if (PN->getIncomingBlock(UI) != BB)
00282       return true;
00283   }
00284   return false;    
00285 }
00286 
00287 /// mayReadFromMemory - Return true if this instruction may read memory.
00288 ///
00289 bool Instruction::mayReadFromMemory() const {
00290   switch (getOpcode()) {
00291   default: return false;
00292   case Instruction::Free:
00293   case Instruction::VAArg:
00294   case Instruction::Load:
00295     return true;
00296   case Instruction::Call:
00297     return !cast<CallInst>(this)->doesNotAccessMemory();
00298   case Instruction::Invoke:
00299     return !cast<InvokeInst>(this)->doesNotAccessMemory();
00300   case Instruction::Store:
00301     return cast<StoreInst>(this)->isVolatile();
00302   }
00303 }
00304 
00305 /// mayWriteToMemory - Return true if this instruction may modify memory.
00306 ///
00307 bool Instruction::mayWriteToMemory() const {
00308   switch (getOpcode()) {
00309   default: return false;
00310   case Instruction::Free:
00311   case Instruction::Store:
00312   case Instruction::VAArg:
00313     return true;
00314   case Instruction::Call:
00315     return !cast<CallInst>(this)->onlyReadsMemory();
00316   case Instruction::Invoke:
00317     return !cast<InvokeInst>(this)->onlyReadsMemory();
00318   case Instruction::Load:
00319     return cast<LoadInst>(this)->isVolatile();
00320   }
00321 }
00322 
00323 /// isAssociative - Return true if the instruction is associative:
00324 ///
00325 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
00326 ///
00327 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
00328 /// applied to floating point types.
00329 ///
00330 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
00331   if (Opcode == And || Opcode == Or || Opcode == Xor)
00332     return true;
00333 
00334   // Add/Mul reassociate unless they are FP or FP vectors.
00335   if (Opcode == Add || Opcode == Mul)
00336     return !Ty->isFPOrFPVector();
00337   return 0;
00338 }
00339 
00340 /// isCommutative - Return true if the instruction is commutative:
00341 ///
00342 ///   Commutative operators satisfy: (x op y) === (y op x)
00343 ///
00344 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00345 /// applied to any type.
00346 ///
00347 bool Instruction::isCommutative(unsigned op) {
00348   switch (op) {
00349   case Add:
00350   case Mul:
00351   case And:
00352   case Or:
00353   case Xor:
00354     return true;
00355   default:
00356     return false;
00357   }
00358 }
00359 
00360 /// isTrapping - Return true if the instruction may trap.
00361 ///
00362 bool Instruction::isTrapping(unsigned op) {
00363   switch(op) {
00364   case UDiv:
00365   case SDiv:
00366   case FDiv:
00367   case URem:
00368   case SRem:
00369   case FRem:
00370   case Load:
00371   case Store:
00372   case Call:
00373   case Invoke:
00374   case VAArg:
00375     return true;
00376   default:
00377     return false;
00378   }
00379 }