blob: 240250662aecaf68985ad7b77e6d0a5cc2c77e28 [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"
Xinliang David Lia754c472016-09-20 19:07:22 +000028#include "llvm/IR/Module.h"
Reid Klecknerc2752da2016-01-26 22:33:19 +000029#include "llvm/Support/raw_ostream.h"
Jim Laskey0cf8ed62006-03-23 18:05:12 +000030using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
34///
35
Duncan P. N. Exon Smith40b44e12016-03-29 18:56:03 +000036Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
37 Value *Op = getArgOperand(0);
38 if (AllowNullOp && !Op)
39 return nullptr;
40
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000041 auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
42 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
43 return V->getValue();
44
45 // When the value goes to null, it gets replaced by an empty MDNode.
46 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
47 return nullptr;
48}
49
Reid Klecknerc2752da2016-01-26 22:33:19 +000050int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
51 StringRef Name) {
52 assert(Name.startswith("llvm."));
53
54 // Do successive binary searches of the dotted name components. For
55 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
56 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
57 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
58 // size 1. During the search, we can skip the prefix that we already know is
59 // identical. By using strncmp we consider names with differing suffixes to
60 // be part of the equal range.
61 size_t CmpStart = 0;
62 size_t CmpEnd = 4; // Skip the "llvm" component.
63 const char *const *Low = NameTable.begin();
64 const char *const *High = NameTable.end();
65 const char *const *LastLow = Low;
66 while (CmpEnd < Name.size() && High - Low > 0) {
67 CmpStart = CmpEnd;
68 CmpEnd = Name.find('.', CmpStart + 1);
69 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
70 auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
71 return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
72 };
73 LastLow = Low;
74 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
75 }
76 if (High - Low > 0)
77 LastLow = Low;
78
79 if (LastLow == NameTable.end())
80 return -1;
81 StringRef NameFound = *LastLow;
82 if (Name == NameFound ||
83 (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
84 return LastLow - NameTable.begin();
85 return -1;
86}
Xinliang David Lia754c472016-09-20 19:07:22 +000087
88Value *InstrProfIncrementInst::getStep() const {
89 if (InstrProfIncrementInstStep::classof(this)) {
90 return const_cast<Value *>(getArgOperand(4));
91 }
92 const Module *M = getModule();
93 LLVMContext &Context = M->getContext();
94 return ConstantInt::get(Type::getInt64Ty(Context), 1);
95}