LLVM API Documentation

IntrinsicInst.cpp

Go to the documentation of this file.
00001 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by James M. Laskey and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements methods that make it really easy to deal with intrinsic
00011 // functions with the isa/dyncast family of functions.  In particular, this
00012 // allows you to do things like:
00013 //
00014 //     if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(Inst))
00015 //        ... SPI->getFileName() ... SPI->getDirectory() ...
00016 //
00017 // All intrinsic function calls are instances of the call instruction, so these
00018 // are all subclasses of the CallInst class.  Note that none of these classes
00019 // has state or virtual methods, which is an important part of this gross/neat
00020 // hack working.
00021 // 
00022 // In some cases, arguments to intrinsics need to be generic and are defined as
00023 // type pointer to empty struct { }*.  To access the real item of interest the
00024 // cast instruction needs to be stripped away. 
00025 //
00026 //===----------------------------------------------------------------------===//
00027 
00028 #include "llvm/IntrinsicInst.h"
00029 
00030 #include "llvm/Constants.h"
00031 #include "llvm/GlobalVariable.h"
00032 
00033 using namespace llvm;
00034 
00035 //===----------------------------------------------------------------------===//
00036 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
00037 ///
00038 
00039 static Value *CastOperand(Value *C) {
00040   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
00041     if (CE->getOpcode() == Instruction::Cast)
00042       return CE->getOperand(0);
00043   return NULL;
00044 }
00045 
00046 Value *DbgInfoIntrinsic::StripCast(Value *C) {
00047   if (Value *CO = CastOperand(C)) {
00048     C = StripCast(CO);
00049   } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
00050     if (GV->hasInitializer())
00051       if (Value *CO = CastOperand(GV->getInitializer()))
00052         C = StripCast(CO);
00053   }
00054   return dyn_cast<GlobalVariable>(C);
00055 }
00056 
00057 //===----------------------------------------------------------------------===//
00058 /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
00059 ///
00060 
00061 std::string DbgStopPointInst::getFileName() const {
00062   GlobalVariable *GV = cast<GlobalVariable>(getContext());
00063   ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
00064   return CS->getOperand(4)->getStringValue();
00065 }
00066 
00067 std::string DbgStopPointInst::getDirectory() const {
00068   GlobalVariable *GV = cast<GlobalVariable>(getContext());
00069   ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
00070   return CS->getOperand(5)->getStringValue();
00071 }
00072 
00073 //===----------------------------------------------------------------------===//