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 is distributed under the University of Illinois Open Source
00006 // 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 #include "llvm/Constants.h"
00030 #include "llvm/GlobalVariable.h"
00031 #include "llvm/Analysis/ValueTracking.h"
00032 #include "llvm/CodeGen/MachineModuleInfo.h"
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->isCast())
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 Value *DbgStopPointInst::getFileName() const {
00062   // Once the operand indices are verified, update this assert
00063   assert(LLVMDebugVersion == (7 << 16) && "Verify operand indices");
00064   GlobalVariable *GV = cast<GlobalVariable>(getContext());
00065   if (!GV->hasInitializer()) return NULL;
00066   ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
00067   return CS->getOperand(3);
00068 }
00069 
00070 Value *DbgStopPointInst::getDirectory() const {
00071   // Once the operand indices are verified, update this assert
00072   assert(LLVMDebugVersion == (7 << 16) && "Verify operand indices");
00073   GlobalVariable *GV = cast<GlobalVariable>(getContext());
00074   if (!GV->hasInitializer()) return NULL;
00075   ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer());
00076   return CS->getOperand(4);
00077 }