IntrinsicInst.cpp revision 276479
1178481Sjb//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
2178481Sjb//
3178481Sjb//                     The LLVM Compiler Infrastructure
4178481Sjb//
5178481Sjb// This file is distributed under the University of Illinois Open Source
6178481Sjb// License. See LICENSE.TXT for details.
7178481Sjb//
8178481Sjb//===----------------------------------------------------------------------===//
9178481Sjb//
10178481Sjb// This file implements methods that make it really easy to deal with intrinsic
11178481Sjb// functions.
12178481Sjb//
13178481Sjb// All intrinsic function calls are instances of the call instruction, so these
14178481Sjb// are all subclasses of the CallInst class.  Note that none of these classes
15178481Sjb// has state or virtual methods, which is an important part of this gross/neat
16178481Sjb// hack working.
17178481Sjb//
18178481Sjb// In some cases, arguments to intrinsics need to be generic and are defined as
19178481Sjb// type pointer to empty struct { }*.  To access the real item of interest the
20178481Sjb// cast instruction needs to be stripped away.
21178481Sjb//
22178481Sjb//===----------------------------------------------------------------------===//
23178481Sjb
24178481Sjb#include "llvm/IR/IntrinsicInst.h"
25178481Sjb#include "llvm/IR/Constants.h"
26178481Sjb#include "llvm/IR/GlobalVariable.h"
27178481Sjb#include "llvm/IR/Metadata.h"
28178481Sjbusing namespace llvm;
29178481Sjb
30178481Sjb//===----------------------------------------------------------------------===//
31178481Sjb/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
32178481Sjb///
33178481Sjb
34178481Sjbstatic Value *CastOperand(Value *C) {
35178481Sjb  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
36178481Sjb    if (CE->isCast())
37178481Sjb      return CE->getOperand(0);
38178481Sjb  return nullptr;
39178481Sjb}
40178481Sjb
41178481SjbValue *DbgInfoIntrinsic::StripCast(Value *C) {
42178481Sjb  if (Value *CO = CastOperand(C)) {
43178481Sjb    C = StripCast(CO);
44178481Sjb  } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
45178481Sjb    if (GV->hasInitializer())
46178544Sjb      if (Value *CO = CastOperand(GV->getInitializer()))
47178481Sjb        C = StripCast(CO);
48178481Sjb  }
49178481Sjb  return dyn_cast<GlobalVariable>(C);
50178481Sjb}
51178481Sjb
52178481Sjb//===----------------------------------------------------------------------===//
53178481Sjb/// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
54178481Sjb///
55178481Sjb
56178481SjbValue *DbgDeclareInst::getAddress() const {
57178481Sjb  if (MDNode* MD = cast_or_null<MDNode>(getArgOperand(0)))
58178481Sjb    return MD->getOperand(0);
59178481Sjb  else
60178481Sjb    return nullptr;
61178481Sjb}
62178481Sjb
63178481Sjb//===----------------------------------------------------------------------===//
64178481Sjb/// DbgValueInst - This represents the llvm.dbg.value instruction.
65178481Sjb///
66178481Sjb
67178481Sjbconst Value *DbgValueInst::getValue() const {
68178481Sjb  return cast<MDNode>(getArgOperand(0))->getOperand(0);
69178481Sjb}
70178481Sjb
71178481SjbValue *DbgValueInst::getValue() {
72178481Sjb  return cast<MDNode>(getArgOperand(0))->getOperand(0);
73178481Sjb}
74178481Sjb