1249259Sdim//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers -----*- C++ -*-===//
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);
38249259Sdim  return NULL;
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
52249259Sdim//===----------------------------------------------------------------------===//
53249259Sdim/// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
54249259Sdim///
55249259Sdim
56249259SdimValue *DbgDeclareInst::getAddress() const {
57249259Sdim  if (MDNode* MD = cast_or_null<MDNode>(getArgOperand(0)))
58249259Sdim    return MD->getOperand(0);
59249259Sdim  else
60249259Sdim    return NULL;
61249259Sdim}
62249259Sdim
63249259Sdim//===----------------------------------------------------------------------===//
64249259Sdim/// DbgValueInst - This represents the llvm.dbg.value instruction.
65249259Sdim///
66249259Sdim
67249259Sdimconst Value *DbgValueInst::getValue() const {
68249259Sdim  return cast<MDNode>(getArgOperand(0))->getOperand(0);
69249259Sdim}
70249259Sdim
71249259SdimValue *DbgValueInst::getValue() {
72249259Sdim  return cast<MDNode>(getArgOperand(0))->getOperand(0);
73249259Sdim}
74