LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Instruction class for the VMCore library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Instructions.h"
00015 #include "llvm/Function.h"
00016 #include "llvm/SymbolTable.h"
00017 #include "llvm/Type.h"
00018 #include "llvm/Support/LeakDetector.h"
00019 using namespace llvm;
00020 
00021 void Instruction::init() {
00022   // Make sure that we get added to a basicblock
00023   LeakDetector::addGarbageObject(this);
00024 }
00025 
00026 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
00027                          Instruction *InsertBefore)
00028   : User(ty, Value::InstructionVal + it, Name), Parent(0) {
00029   init();
00030 
00031   // If requested, insert this instruction into a basic block...
00032   if (InsertBefore) {
00033     assert(InsertBefore->getParent() &&
00034            "Instruction to insert before is not in a basic block!");
00035     InsertBefore->getParent()->getInstList().insert(InsertBefore, this);
00036   }
00037 }
00038 
00039 Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name,
00040                          BasicBlock *InsertAtEnd)
00041   : User(ty, Value::InstructionVal + it, Name), Parent(0) {
00042   init();
00043 
00044   // append this instruction into the basic block
00045   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
00046   InsertAtEnd->getInstList().push_back(this);
00047 }
00048 
00049 void Instruction::setOpcode(unsigned opc) {
00050   setValueType(Value::InstructionVal + opc);
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 // Specialize setName to take care of symbol table majik
00064 void Instruction::setName(const std::string &name, SymbolTable *ST) {
00065   BasicBlock *P = 0; Function *PP = 0;
00066   assert((ST == 0 || !getParent() || !getParent()->getParent() || 
00067     ST == &getParent()->getParent()->getSymbolTable()) &&
00068    "Invalid symtab argument!");
00069   if ((P = getParent()) && (PP = P->getParent()) && hasName())
00070     PP->getSymbolTable().remove(this);
00071   Value::setName(name);
00072   if (PP && hasName()) PP->getSymbolTable().insert(this);
00073 }
00074 
00075 void Instruction::removeFromParent() {
00076   getParent()->getInstList().remove(this);
00077 }
00078 
00079 void Instruction::eraseFromParent() {
00080   getParent()->getInstList().erase(this);
00081 }
00082 
00083 const char *Instruction::getOpcodeName(unsigned OpCode) {
00084   switch (OpCode) {
00085   // Terminators
00086   case Ret:    return "ret";
00087   case Br:     return "br";
00088   case Switch: return "switch";
00089   case Invoke: return "invoke";
00090   case Unwind: return "unwind";
00091   case Unreachable: return "unreachable";
00092     
00093   // Standard binary operators...
00094   case Add: return "add";
00095   case Sub: return "sub";
00096   case Mul: return "mul";
00097   case Div: return "div";
00098   case Rem: return "rem";
00099 
00100   // Logical operators...
00101   case And: return "and";
00102   case Or : return "or";
00103   case Xor: return "xor";
00104 
00105   // SetCC operators...
00106   case SetLE:  return "setle";
00107   case SetGE:  return "setge";
00108   case SetLT:  return "setlt";
00109   case SetGT:  return "setgt";
00110   case SetEQ:  return "seteq";
00111   case SetNE:  return "setne";
00112     
00113   // Memory instructions...
00114   case Malloc:        return "malloc";
00115   case Free:          return "free";
00116   case Alloca:        return "alloca";
00117   case Load:          return "load";
00118   case Store:         return "store";
00119   case GetElementPtr: return "getelementptr";
00120     
00121   // Other instructions...
00122   case PHI:     return "phi";
00123   case Cast:    return "cast";
00124   case Select:  return "select";
00125   case Call:    return "call";
00126   case Shl:     return "shl";
00127   case Shr:     return "shr";
00128   case VANext:  return "vanext";
00129   case VAArg:   return "vaarg";
00130 
00131   default: return "<Invalid operator> ";
00132   }
00133   
00134   return 0;
00135 }
00136 
00137 /// isIdenticalTo - Return true if the specified instruction is exactly
00138 /// identical to the current one.  This means that all operands match and any
00139 /// extra information (e.g. load is volatile) agree.
00140 bool Instruction::isIdenticalTo(Instruction *I) const {
00141   if (getOpcode() != I->getOpcode() ||
00142       getNumOperands() != I->getNumOperands() ||
00143       getType() != I->getType())
00144     return false;
00145 
00146   // We have two instructions of identical opcode and #operands.  Check to see
00147   // if all operands are the same.
00148   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00149     if (getOperand(i) != I->getOperand(i))
00150       return false;
00151 
00152   // Check special state that is a part of some instructions.
00153   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00154     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
00155   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00156     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
00157   if (const VANextInst *VAN = dyn_cast<VANextInst>(this))
00158     return VAN->getArgType() == cast<VANextInst>(I)->getArgType();
00159   return true;
00160 }
00161 
00162 
00163 /// isAssociative - Return true if the instruction is associative:
00164 ///
00165 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
00166 ///
00167 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
00168 /// applied to floating point types.
00169 ///
00170 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
00171   if (Opcode == Add || Opcode == Mul ||
00172       Opcode == And || Opcode == Or || Opcode == Xor) {
00173     // Floating point operations do not associate!
00174     return !Ty->isFloatingPoint();
00175   }
00176   return 0;
00177 }
00178 
00179 /// isCommutative - Return true if the instruction is commutative:
00180 ///
00181 ///   Commutative operators satisfy: (x op y) === (y op x)
00182 ///
00183 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00184 /// applied to any type.
00185 ///
00186 bool Instruction::isCommutative(unsigned op) {
00187   switch (op) {
00188   case Add:
00189   case Mul:
00190   case And: 
00191   case Or:
00192   case Xor:
00193   case SetEQ:
00194   case SetNE:
00195     return true;
00196   default:
00197     return false;
00198   }
00199 }
00200 
00201 /// isRelational - Return true if the instruction is a Set* instruction:
00202 ///
00203 bool Instruction::isRelational(unsigned op) {
00204   switch (op) {
00205   case SetEQ:
00206   case SetNE:
00207   case SetLT:
00208   case SetGT:
00209   case SetLE:
00210   case SetGE:
00211     return true;
00212   }
00213   return false;
00214 }
00215 
00216 
00217 
00218 /// isTrappingInstruction - Return true if the instruction may trap.
00219 ///
00220 bool Instruction::isTrapping(unsigned op) {
00221   switch(op) {
00222   case Div:
00223   case Rem:
00224   case Load:
00225   case Store:
00226   case Call:
00227   case Invoke:
00228     return true;
00229   default:
00230     return false;
00231   }
00232 }