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 void Instruction::setOpcode(unsigned opc) {
00047   setValueType(Value::InstructionVal + opc);
00048 }
00049 
00050 void Instruction::setParent(BasicBlock *P) {
00051   if (getParent()) {
00052     if (!P) LeakDetector::addGarbageObject(this);
00053   } else {
00054     if (P) LeakDetector::removeGarbageObject(this);
00055   }
00056 
00057   Parent = P;
00058 }
00059 
00060 void Instruction::removeFromParent() {
00061   getParent()->getInstList().remove(this);
00062 }
00063 
00064 void Instruction::eraseFromParent() {
00065   getParent()->getInstList().erase(this);
00066 }
00067 
00068 /// moveBefore - Unlink this instruction from its current basic block and
00069 /// insert it into the basic block that MovePos lives in, right before
00070 /// MovePos.
00071 void Instruction::moveBefore(Instruction *MovePos) {
00072   MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
00073                                              this);
00074 }
00075 
00076 
00077 const char *Instruction::getOpcodeName(unsigned OpCode) {
00078   switch (OpCode) {
00079   // Terminators
00080   case Ret:    return "ret";
00081   case Br:     return "br";
00082   case Switch: return "switch";
00083   case Invoke: return "invoke";
00084   case Unwind: return "unwind";
00085   case Unreachable: return "unreachable";
00086 
00087   // Standard binary operators...
00088   case Add: return "add";
00089   case Sub: return "sub";
00090   case Mul: return "mul";
00091   case Div: return "div";
00092   case Rem: return "rem";
00093 
00094   // Logical operators...
00095   case And: return "and";
00096   case Or : return "or";
00097   case Xor: return "xor";
00098 
00099   // SetCC operators...
00100   case SetLE:  return "setle";
00101   case SetGE:  return "setge";
00102   case SetLT:  return "setlt";
00103   case SetGT:  return "setgt";
00104   case SetEQ:  return "seteq";
00105   case SetNE:  return "setne";
00106 
00107   // Memory instructions...
00108   case Malloc:        return "malloc";
00109   case Free:          return "free";
00110   case Alloca:        return "alloca";
00111   case Load:          return "load";
00112   case Store:         return "store";
00113   case GetElementPtr: return "getelementptr";
00114 
00115   // Other instructions...
00116   case PHI:     return "phi";
00117   case Cast:    return "cast";
00118   case Select:  return "select";
00119   case Call:    return "call";
00120   case Shl:     return "shl";
00121   case Shr:     return "shr";
00122   case VAArg:   return "va_arg";
00123   case ExtractElement: return "extractelement";
00124   case InsertElement: return "insertelement";
00125   case ShuffleVector: return "shufflevector";
00126 
00127   default: return "<Invalid operator> ";
00128   }
00129 
00130   return 0;
00131 }
00132 
00133 /// isIdenticalTo - Return true if the specified instruction is exactly
00134 /// identical to the current one.  This means that all operands match and any
00135 /// extra information (e.g. load is volatile) agree.
00136 bool Instruction::isIdenticalTo(Instruction *I) const {
00137   if (getOpcode() != I->getOpcode() ||
00138       getNumOperands() != I->getNumOperands() ||
00139       getType() != I->getType())
00140     return false;
00141 
00142   // We have two instructions of identical opcode and #operands.  Check to see
00143   // if all operands are the same.
00144   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
00145     if (getOperand(i) != I->getOperand(i))
00146       return false;
00147 
00148   // Check special state that is a part of some instructions.
00149   if (const LoadInst *LI = dyn_cast<LoadInst>(this))
00150     return LI->isVolatile() == cast<LoadInst>(I)->isVolatile();
00151   if (const StoreInst *SI = dyn_cast<StoreInst>(this))
00152     return SI->isVolatile() == cast<StoreInst>(I)->isVolatile();
00153   if (const CallInst *CI = dyn_cast<CallInst>(this))
00154     return CI->isTailCall() == cast<CallInst>(I)->isTailCall();
00155   return true;
00156 }
00157 
00158 
00159 /// isAssociative - Return true if the instruction is associative:
00160 ///
00161 ///   Associative operators satisfy:  x op (y op z) === (x op y) op z)
00162 ///
00163 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
00164 /// applied to floating point types.
00165 ///
00166 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
00167   if (Opcode == Add || Opcode == Mul ||
00168       Opcode == And || Opcode == Or || Opcode == Xor) {
00169     // Floating point operations do not associate!
00170     return !Ty->isFloatingPoint();
00171   }
00172   return 0;
00173 }
00174 
00175 /// isCommutative - Return true if the instruction is commutative:
00176 ///
00177 ///   Commutative operators satisfy: (x op y) === (y op x)
00178 ///
00179 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
00180 /// applied to any type.
00181 ///
00182 bool Instruction::isCommutative(unsigned op) {
00183   switch (op) {
00184   case Add:
00185   case Mul:
00186   case And:
00187   case Or:
00188   case Xor:
00189   case SetEQ:
00190   case SetNE:
00191     return true;
00192   default:
00193     return false;
00194   }
00195 }
00196 
00197 /// isRelational - Return true if the instruction is a Set* instruction:
00198 ///
00199 bool Instruction::isRelational(unsigned op) {
00200   switch (op) {
00201   case SetEQ:
00202   case SetNE:
00203   case SetLT:
00204   case SetGT:
00205   case SetLE:
00206   case SetGE:
00207     return true;
00208   }
00209   return false;
00210 }
00211 
00212 
00213 
00214 /// isTrappingInstruction - Return true if the instruction may trap.
00215 ///
00216 bool Instruction::isTrapping(unsigned op) {
00217   switch(op) {
00218   case Div:
00219   case Rem:
00220   case Load:
00221   case Store:
00222   case Call:
00223   case Invoke:
00224     return true;
00225   default:
00226     return false;
00227   }
00228 }