]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/IntrinsicInst.cpp
Merge ^/head r318380 through r318559.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / IntrinsicInst.cpp
1 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements methods that make it really easy to deal with intrinsic
11 // functions.
12 //
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 //===----------------------------------------------------------------------===//
23
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm;
32
33 //===----------------------------------------------------------------------===//
34 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
35 ///
36
37 Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
38   Value *Op = getArgOperand(0);
39   if (AllowNullOp && !Op)
40     return nullptr;
41
42   auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
43   if (auto *V = dyn_cast<ValueAsMetadata>(MD))
44     return V->getValue();
45
46   // When the value goes to null, it gets replaced by an empty MDNode.
47   assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
48   return nullptr;
49 }
50
51 int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
52                                                StringRef Name) {
53   assert(Name.startswith("llvm."));
54
55   // Do successive binary searches of the dotted name components. For
56   // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
57   // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
58   // "llvm.gc.experimental.statepoint", and then we will stop as the range is
59   // size 1. During the search, we can skip the prefix that we already know is
60   // identical. By using strncmp we consider names with differing suffixes to
61   // be part of the equal range.
62   size_t CmpStart = 0;
63   size_t CmpEnd = 4; // Skip the "llvm" component.
64   const char *const *Low = NameTable.begin();
65   const char *const *High = NameTable.end();
66   const char *const *LastLow = Low;
67   while (CmpEnd < Name.size() && High - Low > 0) {
68     CmpStart = CmpEnd;
69     CmpEnd = Name.find('.', CmpStart + 1);
70     CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
71     auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
72       return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
73     };
74     LastLow = Low;
75     std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
76   }
77   if (High - Low > 0)
78     LastLow = Low;
79
80   if (LastLow == NameTable.end())
81     return -1;
82   StringRef NameFound = *LastLow;
83   if (Name == NameFound ||
84       (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
85     return LastLow - NameTable.begin();
86   return -1;
87 }
88
89 Value *InstrProfIncrementInst::getStep() const {
90   if (InstrProfIncrementInstStep::classof(this)) {
91     return const_cast<Value *>(getArgOperand(4));
92   }
93   const Module *M = getModule();
94   LLVMContext &Context = M->getContext();
95   return ConstantInt::get(Type::getInt64Ty(Context), 1);
96 }
97
98 ConstrainedFPIntrinsic::RoundingMode
99 ConstrainedFPIntrinsic::getRoundingMode() const {
100   Metadata *MD = dyn_cast<MetadataAsValue>(getOperand(2))->getMetadata();
101   if (!MD || !isa<MDString>(MD))
102     return rmInvalid;
103   StringRef RoundingArg = cast<MDString>(MD)->getString();
104
105   // For dynamic rounding mode, we use round to nearest but we will set the
106   // 'exact' SDNodeFlag so that the value will not be rounded.
107   return StringSwitch<RoundingMode>(RoundingArg)
108     .Case("round.dynamic",    rmDynamic)
109     .Case("round.tonearest",  rmToNearest)
110     .Case("round.downward",   rmDownward)
111     .Case("round.upward",     rmUpward)
112     .Case("round.towardzero", rmTowardZero)
113     .Default(rmInvalid);
114 }
115
116 ConstrainedFPIntrinsic::ExceptionBehavior
117 ConstrainedFPIntrinsic::getExceptionBehavior() const {
118   Metadata *MD = dyn_cast<MetadataAsValue>(getOperand(3))->getMetadata();
119   if (!MD || !isa<MDString>(MD))
120     return ebInvalid;
121   StringRef ExceptionArg = cast<MDString>(MD)->getString();
122   return StringSwitch<ExceptionBehavior>(ExceptionArg)
123     .Case("fpexcept.ignore",  ebIgnore)
124     .Case("fpexcept.maytrap", ebMayTrap)
125     .Case("fpexcept.strict",  ebStrict)
126     .Default(ebInvalid);
127 }