1276479Sdim//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file implements methods that make it really easy to deal with intrinsic 11249259Sdim// functions. 12249259Sdim// 13249259Sdim// All intrinsic function calls are instances of the call instruction, so these 14249259Sdim// are all subclasses of the CallInst class. Note that none of these classes 15249259Sdim// has state or virtual methods, which is an important part of this gross/neat 16249259Sdim// hack working. 17249259Sdim// 18249259Sdim// In some cases, arguments to intrinsics need to be generic and are defined as 19249259Sdim// type pointer to empty struct { }*. To access the real item of interest the 20249259Sdim// cast instruction needs to be stripped away. 21249259Sdim// 22249259Sdim//===----------------------------------------------------------------------===// 23249259Sdim 24249259Sdim#include "llvm/IR/IntrinsicInst.h" 25249259Sdim#include "llvm/IR/Constants.h" 26249259Sdim#include "llvm/IR/GlobalVariable.h" 27249259Sdim#include "llvm/IR/Metadata.h" 28249259Sdimusing namespace llvm; 29249259Sdim 30249259Sdim//===----------------------------------------------------------------------===// 31249259Sdim/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics 32249259Sdim/// 33249259Sdim 34249259Sdimstatic Value *CastOperand(Value *C) { 35249259Sdim if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 36249259Sdim if (CE->isCast()) 37249259Sdim return CE->getOperand(0); 38276479Sdim return nullptr; 39249259Sdim} 40249259Sdim 41249259SdimValue *DbgInfoIntrinsic::StripCast(Value *C) { 42249259Sdim if (Value *CO = CastOperand(C)) { 43249259Sdim C = StripCast(CO); 44249259Sdim } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { 45249259Sdim if (GV->hasInitializer()) 46249259Sdim if (Value *CO = CastOperand(GV->getInitializer())) 47249259Sdim C = StripCast(CO); 48249259Sdim } 49249259Sdim return dyn_cast<GlobalVariable>(C); 50249259Sdim} 51249259Sdim 52280031Sdimstatic Value *getValueImpl(Value *Op) { 53280031Sdim auto *MD = cast<MetadataAsValue>(Op)->getMetadata(); 54280031Sdim if (auto *V = dyn_cast<ValueAsMetadata>(MD)) 55280031Sdim return V->getValue(); 56280031Sdim 57280031Sdim // When the value goes to null, it gets replaced by an empty MDNode. 58280031Sdim assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode"); 59280031Sdim return nullptr; 60280031Sdim} 61280031Sdim 62249259Sdim//===----------------------------------------------------------------------===// 63249259Sdim/// DbgDeclareInst - This represents the llvm.dbg.declare instruction. 64249259Sdim/// 65249259Sdim 66249259SdimValue *DbgDeclareInst::getAddress() const { 67280031Sdim if (!getArgOperand(0)) 68276479Sdim return nullptr; 69280031Sdim 70280031Sdim return getValueImpl(getArgOperand(0)); 71249259Sdim} 72249259Sdim 73249259Sdim//===----------------------------------------------------------------------===// 74249259Sdim/// DbgValueInst - This represents the llvm.dbg.value instruction. 75249259Sdim/// 76249259Sdim 77249259Sdimconst Value *DbgValueInst::getValue() const { 78280031Sdim return const_cast<DbgValueInst *>(this)->getValue(); 79249259Sdim} 80249259Sdim 81280031SdimValue *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); } 82