LLVM API Documentation

IntrinsicInst.cpp
Go to the documentation of this file.
00001 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
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 methods that make it really easy to deal with intrinsic
00011 // functions.
00012 //
00013 // All intrinsic function calls are instances of the call instruction, so these
00014 // are all subclasses of the CallInst class.  Note that none of these classes
00015 // has state or virtual methods, which is an important part of this gross/neat
00016 // hack working.
00017 // 
00018 // In some cases, arguments to intrinsics need to be generic and are defined as
00019 // type pointer to empty struct { }*.  To access the real item of interest the
00020 // cast instruction needs to be stripped away. 
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #include "llvm/IR/IntrinsicInst.h"
00025 #include "llvm/IR/Constants.h"
00026 #include "llvm/IR/GlobalVariable.h"
00027 #include "llvm/IR/Metadata.h"
00028 using namespace llvm;
00029 
00030 //===----------------------------------------------------------------------===//
00031 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
00032 ///
00033 
00034 static Value *CastOperand(Value *C) {
00035   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
00036     if (CE->isCast())
00037       return CE->getOperand(0);
00038   return nullptr;
00039 }
00040 
00041 Value *DbgInfoIntrinsic::StripCast(Value *C) {
00042   if (Value *CO = CastOperand(C)) {
00043     C = StripCast(CO);
00044   } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
00045     if (GV->hasInitializer())
00046       if (Value *CO = CastOperand(GV->getInitializer()))
00047         C = StripCast(CO);
00048   }
00049   return dyn_cast<GlobalVariable>(C);
00050 }
00051 
00052 static Value *getValueImpl(Value *Op) {
00053   auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
00054   if (auto *V = dyn_cast<ValueAsMetadata>(MD))
00055     return V->getValue();
00056 
00057   // When the value goes to null, it gets replaced by an empty MDNode.
00058   assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
00059   return nullptr;
00060 }
00061 
00062 //===----------------------------------------------------------------------===//
00063 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
00064 ///
00065 
00066 Value *DbgDeclareInst::getAddress() const {
00067   if (!getArgOperand(0))
00068     return nullptr;
00069 
00070   return getValueImpl(getArgOperand(0));
00071 }
00072 
00073 //===----------------------------------------------------------------------===//
00074 /// DbgValueInst - This represents the llvm.dbg.value instruction.
00075 ///
00076 
00077 const Value *DbgValueInst::getValue() const {
00078   return const_cast<DbgValueInst *>(this)->getValue();
00079 }
00080 
00081 Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); }