blob: 84addcc0aa9bbe6a37924b11e71eef940b00e3ff [file] [log] [blame]
Jim Laskey4556ce52006-03-23 18:05:12 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Jim Laskey98a69792006-03-24 10:00:56 +00005// This file was developed by James M. Laskey and is distributed under
Jim Laskey4556ce52006-03-23 18:05:12 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Jim Laskey98a69792006-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 Laskey4556ce52006-03-23 18:05:12 +000027
28#include "llvm/IntrinsicInst.h"
Jim Laskey4556ce52006-03-23 18:05:12 +000029#include "llvm/Constants.h"
30#include "llvm/GlobalVariable.h"
Jim Laskeyca0dc562006-06-19 12:54:15 +000031#include "llvm/CodeGen/MachineDebugInfo.h"
Jim Laskey4556ce52006-03-23 18:05:12 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
36///
37
38static Value *CastOperand(Value *C) {
39 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
Reid Spencer3da59db2006-11-27 01:05:10 +000040 if (CE->isCast())
Jim Laskey4556ce52006-03-23 18:05:12 +000041 return CE->getOperand(0);
42 return NULL;
43}
44
45Value *DbgInfoIntrinsic::StripCast(Value *C) {
46 if (Value *CO = CastOperand(C)) {
Jim Laskeyfbcf23c2006-03-26 22:46:27 +000047 C = StripCast(CO);
Jim Laskey4556ce52006-03-23 18:05:12 +000048 } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
49 if (GV->hasInitializer())
50 if (Value *CO = CastOperand(GV->getInitializer()))
Jim Laskeyfbcf23c2006-03-26 22:46:27 +000051 C = StripCast(CO);
Jim Laskey4556ce52006-03-23 18:05:12 +000052 }
Jim Laskeyfbcf23c2006-03-26 22:46:27 +000053 return dyn_cast<GlobalVariable>(C);
Jim Laskey4556ce52006-03-23 18:05:12 +000054}
55
56//===----------------------------------------------------------------------===//
57/// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
58///
59
60std::string DbgStopPointInst::getFileName() const {
Jim Laskeyca0dc562006-06-19 12:54:15 +000061 // Once the operand indices are verified, update this assert
Jim Laskeye2a78f22006-07-11 15:58:09 +000062 assert(LLVMDebugVersion == (5 << 16) && "Verify operand indices");
Jim Laskey4556ce52006-03-23 18:05:12 +000063 GlobalVariable *GV = cast<GlobalVariable>(getContext());
Chris Lattnerdf20b962006-10-04 23:06:26 +000064 if (!GV->hasInitializer()) return "";
Jim Laskey4556ce52006-03-23 18:05:12 +000065 ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyf06ef2c2006-06-16 23:36:12 +000066 return CS->getOperand(3)->getStringValue();
Jim Laskey4556ce52006-03-23 18:05:12 +000067}
68
69std::string DbgStopPointInst::getDirectory() const {
Jim Laskeyca0dc562006-06-19 12:54:15 +000070 // Once the operand indices are verified, update this assert
Jim Laskeye2a78f22006-07-11 15:58:09 +000071 assert(LLVMDebugVersion == (5 << 16) && "Verify operand indices");
Jim Laskey4556ce52006-03-23 18:05:12 +000072 GlobalVariable *GV = cast<GlobalVariable>(getContext());
Chris Lattnerdf20b962006-10-04 23:06:26 +000073 if (!GV->hasInitializer()) return "";
Jim Laskey4556ce52006-03-23 18:05:12 +000074 ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
Jim Laskeyf06ef2c2006-06-16 23:36:12 +000075 return CS->getOperand(4)->getStringValue();
Jim Laskey4556ce52006-03-23 18:05:12 +000076}
77
78//===----------------------------------------------------------------------===//
Reid Spencer4f1bd9e2006-06-07 22:00:26 +000079/// Ensure that users of IntrinsicInst.h will link with this module.
80DEFINING_FILE_FOR(IntrinsicInst)