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 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 Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
00022                          const std::string &Name, Instruction *InsertBefore)
00023   : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), 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                          const std::string &Name, BasicBlock *InsertAtEnd)
00037   : User(ty, Value::InstructionVal + it, Ops, NumOps, Name), 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 // Out of line virtual method, so the vtable, etc has a home.
00047 Instruction::~Instruction() {
00048   assert(Parent == 0 && "Instruction still linked in the program!");
00049 }
00050 
00051 
00052 void Instruction::setOpcode(unsigned opc) {
00053   setValueType(Value::InstructionVal + opc);
00054 }
00055 
00056 void Instruction::setParent(BasicBlock *P) {
00057   if (getParent()) {
00058     if (!P) LeakDetector::addGarbageObject(this);
00059   } else {
00060     if (P) LeakDetector::removeGarbageObject(this);
00061   }
00062 
00063   Parent = P;
00064 }
00065 
00066 void Instruction::removeFromParent() {
00067   getParent()->getInstList().remove(this);
00068 }
00069 
00070 void Instruction::eraseFromParent() {
00071   getParent()->getInstList().erase(this);
00072 }
00073 
00074 /// moveBefore - Unlink this instruction from its current basic block and
00075 /// insert it into the basic block that MovePos lives in, right before
00076 /// MovePos.
00077 void Instruction::moveBefore(Instruction *MovePos) {
00078   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
00079                                              this);
00080 }
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 VAArg:   return "va_arg";
00129   case ExtractElement: return "extractelement";
00130   case InsertElement: return "insertelement";
00131   case ShuffleVector: return "shufflevector";
00132 
00133   default: return "<Invalid operator> ";
00134   }
00135 
00136   return 0;
00137 }
00138 
00139 /// isIdenticalTo - Return true if the specified instruction is exactly
00140 /// identical to the current one.  This means that all operands match and any
00141 /// extra information (e.g. load is volatile) agree.
00142 bool Instruction::isIdenticalTo(Instruction *I) const {
00143   if (getOpcode() != I->getOpcode() ||
00144       getNumOperands() != I->getNumOperands() ||
00145       getType() != I->getType())
00146     return false;
00147 
00148   // We have two instructions of identical opcode and #operands.  Check to see
00149   // if all operands are the same.
00150   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00151     if (getOperand(i) != I->getOperand(i))
00152       return false;
00153 
00154   // Check special state that is a part of some instructions.
00155   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00156     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
00157   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00158     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
00159   if (const CallInst *CI = dyn_cast<CallInst>(this))
00160     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
00161   return true;
00162 }
00163 
00164 
00165 /// isAssociative - Return true if the instruction is associative:
00166 ///
00167 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
00168 ///
00169 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
00170 /// applied to floating point types.
00171 ///
00172 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
00173   if (Opcode == Add || Opcode == Mul ||
00174       Opcode == And || Opcode == Or || Opcode == Xor) {
00175     // Floating point operations do not associate!
00176     return !Ty->isFloatingPoint();
00177   }
00178   return 0;
00179 }
00180 
00181 /// isCommutative - Return true if the instruction is commutative:
00182 ///
00183 ///   Commutative operators satisfy: (x op y) === (y op x)
00184 ///
00185 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00186 /// applied to any type.
00187 ///
00188 bool Instruction::isCommutative(unsigned op) {
00189   switch (op) {
00190   case Add:
00191   case Mul:
00192   case And:
00193   case Or:
00194   case Xor:
00195   case SetEQ:
00196   case SetNE:
00197     return true;
00198   default:
00199     return false;
00200   }
00201 }
00202 
00203 /// isRelational - Return true if the instruction is a Set* instruction:
00204 ///
00205 bool Instruction::isRelational(unsigned op) {
00206   switch (op) {
00207   case SetEQ:
00208   case SetNE:
00209   case SetLT:
00210   case SetGT:
00211   case SetLE:
00212   case SetGE:
00213     return true;
00214   }
00215   return false;
00216 }
00217 
00218 
00219 
00220 /// isTrappingInstruction - Return true if the instruction may trap.
00221 ///
00222 bool Instruction::isTrapping(unsigned op) {
00223   switch(op) {
00224   case Div:
00225   case Rem:
00226   case Load:
00227   case Store:
00228   case Call:
00229   case Invoke:
00230     return true;
00231   default:
00232     return false;
00233   }
00234 }