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 /// moveBefore - Unlink this instruction from its current basic block and
00078 /// insert it into the basic block that MovePos lives in, right before
00079 /// MovePos.
00080 void Instruction::moveBefore(Instruction *MovePos) {
00081   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
00082                                              this);
00083 }
00084 
00085 
00086 const char *Instruction::getOpcodeName(unsigned OpCode) {
00087   switch (OpCode) {
00088   // Terminators
00089   case Ret:    return "ret";
00090   case Br:     return "br";
00091   case Switch: return "switch";
00092   case Invoke: return "invoke";
00093   case Unwind: return "unwind";
00094   case Unreachable: return "unreachable";
00095 
00096   // Standard binary operators...
00097   case Add: return "add";
00098   case Sub: return "sub";
00099   case Mul: return "mul";
00100   case UDiv: return "udiv";
00101   case SDiv: return "sdiv";
00102   case FDiv: return "fdiv";
00103   case URem: return "urem";
00104   case SRem: return "srem";
00105   case FRem: return "frem";
00106 
00107   // Logical operators...
00108   case And: return "and";
00109   case Or : return "or";
00110   case Xor: return "xor";
00111 
00112   // Memory instructions...
00113   case Malloc:        return "malloc";
00114   case Free:          return "free";
00115   case Alloca:        return "alloca";
00116   case Load:          return "load";
00117   case Store:         return "store";
00118   case GetElementPtr: return "getelementptr";
00119 
00120   // Convert instructions...
00121   case Trunc:     return "trunc";
00122   case ZExt:      return "zext";
00123   case SExt:      return "sext";
00124   case FPTrunc:   return "fptrunc";
00125   case FPExt:     return "fpext";
00126   case FPToUI:    return "fptoui";
00127   case FPToSI:    return "fptosi";
00128   case UIToFP:    return "uitofp";
00129   case SIToFP:    return "sitofp";
00130   case IntToPtr:  return "inttoptr";
00131   case PtrToInt:  return "ptrtoint";
00132   case BitCast:   return "bitcast";
00133 
00134   // Other instructions...
00135   case ICmp:           return "icmp";
00136   case FCmp:           return "fcmp";
00137   case VICmp:          return "vicmp";
00138   case VFCmp:          return "vfcmp";
00139   case PHI:            return "phi";
00140   case Select:         return "select";
00141   case Call:           return "call";
00142   case Shl:            return "shl";
00143   case LShr:           return "lshr";
00144   case AShr:           return "ashr";
00145   case VAArg:          return "va_arg";
00146   case ExtractElement: return "extractelement";
00147   case InsertElement:  return "insertelement";
00148   case ShuffleVector:  return "shufflevector";
00149   case ExtractValue:   return "extractvalue";
00150   case InsertValue:    return "insertvalue";
00151 
00152   default: return "<Invalid operator> ";
00153   }
00154 
00155   return 0;
00156 }
00157 
00158 /// isIdenticalTo - Return true if the specified instruction is exactly
00159 /// identical to the current one.  This means that all operands match and any
00160 /// extra information (e.g. load is volatile) agree.
00161 bool Instruction::isIdenticalTo(const Instruction *I) const {
00162   if (getOpcode() != I->getOpcode() ||
00163       getNumOperands() != I->getNumOperands() ||
00164       getType() != I->getType())
00165     return false;
00166 
00167   // We have two instructions of identical opcode and #operands.  Check to see
00168   // if all operands are the same.
00169   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00170     if (getOperand(i) != I->getOperand(i))
00171       return false;
00172 
00173   // Check special state that is a part of some instructions.
00174   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00175     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
00176            LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
00177   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00178     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
00179            SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
00180   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
00181     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
00182   if (const CallInst *CI = dyn_cast<CallInst>(this))
00183     return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
00184            CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
00185            CI->getAttributes().getRawPointer() ==
00186              cast<CallInst>(I)->getAttributes().getRawPointer();
00187   if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
00188     return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
00189            CI->getAttributes().getRawPointer() ==
00190              cast<InvokeInst>(I)->getAttributes().getRawPointer();
00191   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
00192     if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
00193       return false;
00194     for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
00195       if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
00196         return false;
00197     return true;
00198   }
00199   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
00200     if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
00201       return false;
00202     for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
00203       if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
00204         return false;
00205     return true;
00206   }
00207 
00208   return true;
00209 }
00210 
00211 // isSameOperationAs
00212 bool Instruction::isSameOperationAs(const Instruction *I) const {
00213   if (getOpcode() != I->getOpcode() || getType() != I->getType() ||
00214       getNumOperands() != I->getNumOperands())
00215     return false;
00216 
00217   // We have two instructions of identical opcode and #operands.  Check to see
00218   // if all operands are the same type
00219   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00220     if (getOperand(i)->getType() != I->getOperand(i)->getType())
00221       return false;
00222 
00223   // Check special state that is a part of some instructions.
00224   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00225     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile() &&
00226            LI->getAlignment() == cast<LoadInst>(I)->getAlignment();
00227   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00228     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile() &&
00229            SI->getAlignment() == cast<StoreInst>(I)->getAlignment();
00230   if (const CmpInst *CI = dyn_cast<CmpInst>(this))
00231     return CI->getPredicate() == cast<CmpInst>(I)->getPredicate();
00232   if (const CallInst *CI = dyn_cast<CallInst>(this))
00233     return CI->isTailCall() == cast<CallInst>(I)->isTailCall() &&
00234            CI->getCallingConv() == cast<CallInst>(I)->getCallingConv() &&
00235            CI->getAttributes().getRawPointer() ==
00236              cast<CallInst>(I)->getAttributes().getRawPointer();
00237   if (const InvokeInst *CI = dyn_cast<InvokeInst>(this))
00238     return CI->getCallingConv() == cast<InvokeInst>(I)->getCallingConv() &&
00239            CI->getAttributes().getRawPointer() ==
00240              cast<InvokeInst>(I)->getAttributes().getRawPointer();
00241   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(this)) {
00242     if (IVI->getNumIndices() != cast<InsertValueInst>(I)->getNumIndices())
00243       return false;
00244     for (unsigned i = 0, e = IVI->getNumIndices(); i != e; ++i)
00245       if (IVI->idx_begin()[i] != cast<InsertValueInst>(I)->idx_begin()[i])
00246         return false;
00247     return true;
00248   }
00249   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(this)) {
00250     if (EVI->getNumIndices() != cast<ExtractValueInst>(I)->getNumIndices())
00251       return false;
00252     for (unsigned i = 0, e = EVI->getNumIndices(); i != e; ++i)
00253       if (EVI->idx_begin()[i] != cast<ExtractValueInst>(I)->idx_begin()[i])
00254         return false;
00255     return true;
00256   }
00257 
00258   return true;
00259 }
00260 
00261 /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
00262 /// specified block.  Note that PHI nodes are considered to evaluate their
00263 /// operands in the corresponding predecessor block.
00264 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
00265   for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
00266     // PHI nodes uses values in the corresponding predecessor block.  For other
00267     // instructions, just check to see whether the parent of the use matches up.
00268     const PHINode *PN = dyn_cast<PHINode>(*UI);
00269     if (PN == 0) {
00270       if (cast<Instruction>(*UI)->getParent() != BB)
00271         return true;
00272       continue;
00273     }
00274     
00275     unsigned UseOperand = UI.getOperandNo();
00276     if (PN->getIncomingBlock(UseOperand/2) != BB)
00277       return true;
00278   }
00279   return false;    
00280 }
00281 
00282 /// mayReadFromMemory - Return true if this instruction may read memory.
00283 ///
00284 bool Instruction::mayReadFromMemory() const {
00285   switch (getOpcode()) {
00286   default: return false;
00287   case Instruction::Free:
00288   case Instruction::VAArg:
00289   case Instruction::Load:
00290     return true;
00291   case Instruction::Call:
00292     return !cast<CallInst>(this)->doesNotAccessMemory();
00293   case Instruction::Invoke:
00294     return !cast<InvokeInst>(this)->doesNotAccessMemory();
00295   case Instruction::Store:
00296     return cast<StoreInst>(this)->isVolatile();
00297   }
00298 }
00299 
00300 /// mayWriteToMemory - Return true if this instruction may modify memory.
00301 ///
00302 bool Instruction::mayWriteToMemory() const {
00303   switch (getOpcode()) {
00304   default: return false;
00305   case Instruction::Free:
00306   case Instruction::Store:
00307   case Instruction::VAArg:
00308     return true;
00309   case Instruction::Call:
00310     return !cast<CallInst>(this)->onlyReadsMemory();
00311   case Instruction::Invoke:
00312     return !cast<InvokeInst>(this)->onlyReadsMemory();
00313   case Instruction::Load:
00314     return cast<LoadInst>(this)->isVolatile();
00315   }
00316 }
00317 
00318 /// isAssociative - Return true if the instruction is associative:
00319 ///
00320 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
00321 ///
00322 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
00323 /// applied to floating point types.
00324 ///
00325 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
00326   if (Opcode == And || Opcode == Or || Opcode == Xor)
00327     return true;
00328 
00329   // Add/Mul reassociate unless they are FP or FP vectors.
00330   if (Opcode == Add || Opcode == Mul)
00331     return !Ty->isFPOrFPVector();
00332   return 0;
00333 }
00334 
00335 /// isCommutative - Return true if the instruction is commutative:
00336 ///
00337 ///   Commutative operators satisfy: (x op y) === (y op x)
00338 ///
00339 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00340 /// applied to any type.
00341 ///
00342 bool Instruction::isCommutative(unsigned op) {
00343   switch (op) {
00344   case Add:
00345   case Mul:
00346   case And:
00347   case Or:
00348   case Xor:
00349     return true;
00350   default:
00351     return false;
00352   }
00353 }
00354 
00355 /// isTrapping - Return true if the instruction may trap.
00356 ///
00357 bool Instruction::isTrapping(unsigned op) {
00358   switch(op) {
00359   case UDiv:
00360   case SDiv:
00361   case FDiv:
00362   case URem:
00363   case SRem:
00364   case FRem:
00365   case Load:
00366   case Store:
00367   case Call:
00368   case Invoke:
00369   case VAArg:
00370     return true;
00371   default:
00372     return false;
00373   }
00374 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.