blob: b9b5a29091dff3e72514c2befcb5beb9d00f3ed2 [file] [log] [blame]
Nick Lewyckyea08c702014-02-26 03:10:45 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey0cf8ed62006-03-23 18:05:12 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey864e4442006-03-24 10:00:56 +00009//
10// This file implements methods that make it really easy to deal with intrinsic
Devang Patelbe94f232010-01-05 01:10:40 +000011// functions.
Jim Laskey864e4442006-03-24 10:00:56 +000012//
13// All intrinsic function calls are instances of the call instruction, so these
14// are all subclasses of the CallInst class. Note that none of these classes
15// has state or virtual methods, which is an important part of this gross/neat
16// hack working.
17//
18// In some cases, arguments to intrinsics need to be generic and are defined as
19// type pointer to empty struct { }*. To access the real item of interest the
20// cast instruction needs to be stripped away.
21//
22//===----------------------------------------------------------------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +000023
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/GlobalVariable.h"
27#include "llvm/IR/Metadata.h"
Jim Laskey0cf8ed62006-03-23 18:05:12 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
32///
33
34static Value *CastOperand(Value *C) {
35 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Reid Spencer6c38f0b2006-11-27 01:05:10 +000036 if (CE->isCast())
Jim Laskey0cf8ed62006-03-23 18:05:12 +000037 return CE->getOperand(0);
Craig Topperc6207612014-04-09 06:08:46 +000038 return nullptr;
Jim Laskey0cf8ed62006-03-23 18:05:12 +000039}
40
41Value *DbgInfoIntrinsic::StripCast(Value *C) {
42 if (Value *CO = CastOperand(C)) {
Jim Laskey70928882006-03-26 22:46:27 +000043 C = StripCast(CO);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000044 } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
45 if (GV->hasInitializer())
46 if (Value *CO = CastOperand(GV->getInitializer()))
Jim Laskey70928882006-03-26 22:46:27 +000047 C = StripCast(CO);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000048 }
Jim Laskey70928882006-03-26 22:46:27 +000049 return dyn_cast<GlobalVariable>(C);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000050}
51
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000052static Value *getValueImpl(Value *Op) {
53 auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
54 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
55 return V->getValue();
56
57 // When the value goes to null, it gets replaced by an empty MDNode.
58 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
59 return nullptr;
60}
61
Jim Laskey0cf8ed62006-03-23 18:05:12 +000062//===----------------------------------------------------------------------===//
Victor Hernandezb324e662010-01-15 19:04:09 +000063/// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
64///
65
66Value *DbgDeclareInst::getAddress() const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000067 if (!getArgOperand(0))
Craig Topperc6207612014-04-09 06:08:46 +000068 return nullptr;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000069
70 return getValueImpl(getArgOperand(0));
Victor Hernandezb324e662010-01-15 19:04:09 +000071}
72
73//===----------------------------------------------------------------------===//
Chris Lattnercc8c8142009-12-31 01:32:41 +000074/// DbgValueInst - This represents the llvm.dbg.value instruction.
75///
76
Victor Hernandez9ce5b512010-01-11 07:45:19 +000077const Value *DbgValueInst::getValue() const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000078 return const_cast<DbgValueInst *>(this)->getValue();
Victor Hernandez9ce5b512010-01-11 07:45:19 +000079}
80
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000081Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); }