blob: 5e0f42e063bf870be97f675ff04fde1e0d3a17d4 [file] [log] [blame]
Jim Laskey0cf8ed62006-03-23 18:05:12 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers -----*- C++ -*-===//
2//
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
11// functions with the isa/dyncast family of functions. In particular, this
12// allows you to do things like:
13//
14// if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(Inst))
15// ... SPI->getFileName() ... SPI->getDirectory() ...
16//
17// All intrinsic function calls are instances of the call instruction, so these
18// are all subclasses of the CallInst class. Note that none of these classes
19// has state or virtual methods, which is an important part of this gross/neat
20// hack working.
21//
22// In some cases, arguments to intrinsics need to be generic and are defined as
23// type pointer to empty struct { }*. To access the real item of interest the
24// cast instruction needs to be stripped away.
25//
26//===----------------------------------------------------------------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +000027
28#include "llvm/IntrinsicInst.h"
Jim Laskey0cf8ed62006-03-23 18:05:12 +000029#include "llvm/Constants.h"
30#include "llvm/GlobalVariable.h"
Evan Chengda3db112008-06-30 07:31:25 +000031#include "llvm/Analysis/ValueTracking.h"
Jim Laskeyc56315c2007-01-26 21:22:28 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey0cf8ed62006-03-23 18:05:12 +000033using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
37///
38
39static Value *CastOperand(Value *C) {
40 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Reid Spencer6c38f0b2006-11-27 01:05:10 +000041 if (CE->isCast())
Jim Laskey0cf8ed62006-03-23 18:05:12 +000042 return CE->getOperand(0);
43 return NULL;
44}
45
46Value *DbgInfoIntrinsic::StripCast(Value *C) {
47 if (Value *CO = CastOperand(C)) {
Jim Laskey70928882006-03-26 22:46:27 +000048 C = StripCast(CO);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000049 } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
50 if (GV->hasInitializer())
51 if (Value *CO = CastOperand(GV->getInitializer()))
Jim Laskey70928882006-03-26 22:46:27 +000052 C = StripCast(CO);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000053 }
Jim Laskey70928882006-03-26 22:46:27 +000054 return dyn_cast<GlobalVariable>(C);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000055}
56
57//===----------------------------------------------------------------------===//
58/// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
59///
60
Evan Chengda3db112008-06-30 07:31:25 +000061Value *DbgStopPointInst::getFileName() const {
Jim Laskey90cd68a2006-06-19 12:54:15 +000062 // Once the operand indices are verified, update this assert
John Criswell8dd90a72008-12-19 19:56:36 +000063 assert(LLVMDebugVersion == (7 << 16) && "Verify operand indices");
Chris Lattner9b493022009-12-31 01:22:29 +000064 return getContext()->getOperand(3);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000065}
66
Evan Chengda3db112008-06-30 07:31:25 +000067Value *DbgStopPointInst::getDirectory() const {
Jim Laskey90cd68a2006-06-19 12:54:15 +000068 // Once the operand indices are verified, update this assert
John Criswell8dd90a72008-12-19 19:56:36 +000069 assert(LLVMDebugVersion == (7 << 16) && "Verify operand indices");
Chris Lattner9b493022009-12-31 01:22:29 +000070 return getContext()->getOperand(4);
Jim Laskey0cf8ed62006-03-23 18:05:12 +000071}
Chris Lattnercc8c8142009-12-31 01:32:41 +000072
73//===----------------------------------------------------------------------===//
74/// DbgValueInst - This represents the llvm.dbg.value instruction.
75///
76
77Value *DbgValueInst::getValue() const {
78 return cast<MDNode>(getOperand(1))->getOperand(0);
79}