1 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements methods that make it really easy to deal with intrinsic
10 // functions.
11 //
12 // All intrinsic function calls are instances of the call instruction, so these
13 // are all subclasses of the CallInst class.  Note that none of these classes
14 // has state or virtual methods, which is an important part of this gross/neat
15 // hack working.
16 //
17 // In some cases, arguments to intrinsics need to be generic and are defined as
18 // type pointer to empty struct { }*.  To access the real item of interest the
19 // cast instruction needs to be stripped away.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DebugInfoMetadata.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/PatternMatch.h"
32 #include "llvm/IR/Statepoint.h"
33 
34 #include "llvm/Support/raw_ostream.h"
35 using namespace llvm;
36 
37 //===----------------------------------------------------------------------===//
38 /// DbgVariableIntrinsic - This is the common base class for debug info
39 /// intrinsics for variables.
40 ///
41 
42 iterator_range<DbgVariableIntrinsic::location_op_iterator>
43 DbgVariableIntrinsic::location_ops() const {
44   auto *MD = getRawLocation();
45   assert(MD && "First operand of DbgVariableIntrinsic should be non-null.");
46 
47   // If operand is ValueAsMetadata, return a range over just that operand.
48   if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
49     return {location_op_iterator(VAM), location_op_iterator(VAM + 1)};
50   }
51   // If operand is DIArgList, return a range over its args.
52   if (auto *AL = dyn_cast<DIArgList>(MD))
53     return {location_op_iterator(AL->args_begin()),
54             location_op_iterator(AL->args_end())};
55   // Operand must be an empty metadata tuple, so return empty iterator.
56   return {location_op_iterator(static_cast<ValueAsMetadata *>(nullptr)),
57           location_op_iterator(static_cast<ValueAsMetadata *>(nullptr))};
58 }
59 
60 Value *DbgVariableIntrinsic::getVariableLocationOp(unsigned OpIdx) const {
61   auto *MD = getRawLocation();
62   assert(MD && "First operand of DbgVariableIntrinsic should be non-null.");
63   if (auto *AL = dyn_cast<DIArgList>(MD))
64     return AL->getArgs()[OpIdx]->getValue();
65   if (isa<MDNode>(MD))
66     return nullptr;
67   assert(
68       isa<ValueAsMetadata>(MD) &&
69       "Attempted to get location operand from DbgVariableIntrinsic with none.");
70   auto *V = cast<ValueAsMetadata>(MD);
71   assert(OpIdx == 0 && "Operand Index must be 0 for a debug intrinsic with a "
72                        "single location operand.");
73   return V->getValue();
74 }
75 
76 static ValueAsMetadata *getAsMetadata(Value *V) {
77   return isa<MetadataAsValue>(V) ? dyn_cast<ValueAsMetadata>(
78                                        cast<MetadataAsValue>(V)->getMetadata())
79                                  : ValueAsMetadata::get(V);
80 }
81 
82 void DbgVariableIntrinsic::replaceVariableLocationOp(Value *OldValue,
83                                                      Value *NewValue) {
84   assert(NewValue && "Values must be non-null");
85   auto Locations = location_ops();
86   auto OldIt = find(Locations, OldValue);
87   assert(OldIt != Locations.end() && "OldValue must be a current location");
88   if (!hasArgList()) {
89     Value *NewOperand = isa<MetadataAsValue>(NewValue)
90                             ? NewValue
91                             : MetadataAsValue::get(
92                                   getContext(), ValueAsMetadata::get(NewValue));
93     return setArgOperand(0, NewOperand);
94   }
95   SmallVector<ValueAsMetadata *, 4> MDs;
96   ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
97   for (auto *VMD : Locations)
98     MDs.push_back(VMD == *OldIt ? NewOperand : getAsMetadata(VMD));
99   setArgOperand(
100       0, MetadataAsValue::get(getContext(), DIArgList::get(getContext(), MDs)));
101 }
102 void DbgVariableIntrinsic::replaceVariableLocationOp(unsigned OpIdx,
103                                                      Value *NewValue) {
104   assert(OpIdx < getNumVariableLocationOps() && "Invalid Operand Index");
105   if (!hasArgList()) {
106     Value *NewOperand = isa<MetadataAsValue>(NewValue)
107                             ? NewValue
108                             : MetadataAsValue::get(
109                                   getContext(), ValueAsMetadata::get(NewValue));
110     return setArgOperand(0, NewOperand);
111   }
112   SmallVector<ValueAsMetadata *, 4> MDs;
113   ValueAsMetadata *NewOperand = getAsMetadata(NewValue);
114   for (unsigned Idx = 0; Idx < getNumVariableLocationOps(); ++Idx)
115     MDs.push_back(Idx == OpIdx ? NewOperand
116                                : getAsMetadata(getVariableLocationOp(Idx)));
117   setArgOperand(
118       0, MetadataAsValue::get(getContext(), DIArgList::get(getContext(), MDs)));
119 }
120 
121 Optional<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
122   if (auto Fragment = getExpression()->getFragmentInfo())
123     return Fragment->SizeInBits;
124   return getVariable()->getSizeInBits();
125 }
126 
127 int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
128                                                StringRef Name) {
129   assert(Name.startswith("llvm."));
130 
131   // Do successive binary searches of the dotted name components. For
132   // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
133   // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
134   // "llvm.gc.experimental.statepoint", and then we will stop as the range is
135   // size 1. During the search, we can skip the prefix that we already know is
136   // identical. By using strncmp we consider names with differing suffixes to
137   // be part of the equal range.
138   size_t CmpEnd = 4; // Skip the "llvm" component.
139   const char *const *Low = NameTable.begin();
140   const char *const *High = NameTable.end();
141   const char *const *LastLow = Low;
142   while (CmpEnd < Name.size() && High - Low > 0) {
143     size_t CmpStart = CmpEnd;
144     CmpEnd = Name.find('.', CmpStart + 1);
145     CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
146     auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
147       return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
148     };
149     LastLow = Low;
150     std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
151   }
152   if (High - Low > 0)
153     LastLow = Low;
154 
155   if (LastLow == NameTable.end())
156     return -1;
157   StringRef NameFound = *LastLow;
158   if (Name == NameFound ||
159       (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
160     return LastLow - NameTable.begin();
161   return -1;
162 }
163 
164 Value *InstrProfIncrementInst::getStep() const {
165   if (InstrProfIncrementInstStep::classof(this)) {
166     return const_cast<Value *>(getArgOperand(4));
167   }
168   const Module *M = getModule();
169   LLVMContext &Context = M->getContext();
170   return ConstantInt::get(Type::getInt64Ty(Context), 1);
171 }
172 
173 Optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
174   unsigned NumOperands = getNumArgOperands();
175   Metadata *MD =
176       cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
177   if (!MD || !isa<MDString>(MD))
178     return None;
179   return StrToRoundingMode(cast<MDString>(MD)->getString());
180 }
181 
182 Optional<fp::ExceptionBehavior>
183 ConstrainedFPIntrinsic::getExceptionBehavior() const {
184   unsigned NumOperands = getNumArgOperands();
185   Metadata *MD =
186       cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
187   if (!MD || !isa<MDString>(MD))
188     return None;
189   return StrToExceptionBehavior(cast<MDString>(MD)->getString());
190 }
191 
192 FCmpInst::Predicate ConstrainedFPCmpIntrinsic::getPredicate() const {
193   Metadata *MD = cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
194   if (!MD || !isa<MDString>(MD))
195     return FCmpInst::BAD_FCMP_PREDICATE;
196   return StringSwitch<FCmpInst::Predicate>(cast<MDString>(MD)->getString())
197       .Case("oeq", FCmpInst::FCMP_OEQ)
198       .Case("ogt", FCmpInst::FCMP_OGT)
199       .Case("oge", FCmpInst::FCMP_OGE)
200       .Case("olt", FCmpInst::FCMP_OLT)
201       .Case("ole", FCmpInst::FCMP_OLE)
202       .Case("one", FCmpInst::FCMP_ONE)
203       .Case("ord", FCmpInst::FCMP_ORD)
204       .Case("uno", FCmpInst::FCMP_UNO)
205       .Case("ueq", FCmpInst::FCMP_UEQ)
206       .Case("ugt", FCmpInst::FCMP_UGT)
207       .Case("uge", FCmpInst::FCMP_UGE)
208       .Case("ult", FCmpInst::FCMP_ULT)
209       .Case("ule", FCmpInst::FCMP_ULE)
210       .Case("une", FCmpInst::FCMP_UNE)
211       .Default(FCmpInst::BAD_FCMP_PREDICATE);
212 }
213 
214 bool ConstrainedFPIntrinsic::isUnaryOp() const {
215   switch (getIntrinsicID()) {
216   default:
217     return false;
218 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
219   case Intrinsic::INTRINSIC:                                                   \
220     return NARG == 1;
221 #include "llvm/IR/ConstrainedOps.def"
222   }
223 }
224 
225 bool ConstrainedFPIntrinsic::isTernaryOp() const {
226   switch (getIntrinsicID()) {
227   default:
228     return false;
229 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
230   case Intrinsic::INTRINSIC:                                                   \
231     return NARG == 3;
232 #include "llvm/IR/ConstrainedOps.def"
233   }
234 }
235 
236 bool ConstrainedFPIntrinsic::classof(const IntrinsicInst *I) {
237   switch (I->getIntrinsicID()) {
238 #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC)                        \
239   case Intrinsic::INTRINSIC:
240 #include "llvm/IR/ConstrainedOps.def"
241     return true;
242   default:
243     return false;
244   }
245 }
246 
247 ElementCount VPIntrinsic::getStaticVectorLength() const {
248   auto GetVectorLengthOfType = [](const Type *T) -> ElementCount {
249     auto VT = cast<VectorType>(T);
250     auto ElemCount = VT->getElementCount();
251     return ElemCount;
252   };
253 
254   auto VPMask = getMaskParam();
255   return GetVectorLengthOfType(VPMask->getType());
256 }
257 
258 Value *VPIntrinsic::getMaskParam() const {
259   auto maskPos = GetMaskParamPos(getIntrinsicID());
260   if (maskPos)
261     return getArgOperand(maskPos.getValue());
262   return nullptr;
263 }
264 
265 void VPIntrinsic::setMaskParam(Value *NewMask) {
266   auto MaskPos = GetMaskParamPos(getIntrinsicID());
267   setArgOperand(*MaskPos, NewMask);
268 }
269 
270 Value *VPIntrinsic::getVectorLengthParam() const {
271   auto vlenPos = GetVectorLengthParamPos(getIntrinsicID());
272   if (vlenPos)
273     return getArgOperand(vlenPos.getValue());
274   return nullptr;
275 }
276 
277 void VPIntrinsic::setVectorLengthParam(Value *NewEVL) {
278   auto EVLPos = GetVectorLengthParamPos(getIntrinsicID());
279   setArgOperand(*EVLPos, NewEVL);
280 }
281 
282 Optional<int> VPIntrinsic::GetMaskParamPos(Intrinsic::ID IntrinsicID) {
283   switch (IntrinsicID) {
284   default:
285     return None;
286 
287 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS)                    \
288   case Intrinsic::VPID:                                                        \
289     return MASKPOS;
290 #include "llvm/IR/VPIntrinsics.def"
291   }
292 }
293 
294 Optional<int> VPIntrinsic::GetVectorLengthParamPos(Intrinsic::ID IntrinsicID) {
295   switch (IntrinsicID) {
296   default:
297     return None;
298 
299 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS)                    \
300   case Intrinsic::VPID:                                                        \
301     return VLENPOS;
302 #include "llvm/IR/VPIntrinsics.def"
303   }
304 }
305 
306 bool VPIntrinsic::IsVPIntrinsic(Intrinsic::ID ID) {
307   switch (ID) {
308   default:
309     return false;
310 
311 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, MASKPOS, VLENPOS)                    \
312   case Intrinsic::VPID:                                                        \
313     break;
314 #include "llvm/IR/VPIntrinsics.def"
315   }
316   return true;
317 }
318 
319 // Equivalent non-predicated opcode
320 unsigned VPIntrinsic::GetFunctionalOpcodeForVP(Intrinsic::ID ID) {
321   unsigned FunctionalOC = Instruction::Call;
322   switch (ID) {
323   default:
324     break;
325 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
326 #define HANDLE_VP_TO_OPC(OPC) FunctionalOC = Instruction::OPC;
327 #define END_REGISTER_VP_INTRINSIC(...) break;
328 #include "llvm/IR/VPIntrinsics.def"
329   }
330 
331   return FunctionalOC;
332 }
333 
334 Intrinsic::ID VPIntrinsic::GetForOpcode(unsigned IROPC) {
335   switch (IROPC) {
336   default:
337     return Intrinsic::not_intrinsic;
338 
339 #define HANDLE_VP_TO_OPC(OPC) case Instruction::OPC:
340 #define END_REGISTER_VP_INTRINSIC(VPID) return Intrinsic::VPID;
341 #include "llvm/IR/VPIntrinsics.def"
342   }
343 }
344 
345 bool VPIntrinsic::canIgnoreVectorLengthParam() const {
346   using namespace PatternMatch;
347 
348   ElementCount EC = getStaticVectorLength();
349 
350   // No vlen param - no lanes masked-off by it.
351   auto *VLParam = getVectorLengthParam();
352   if (!VLParam)
353     return true;
354 
355   // Note that the VP intrinsic causes undefined behavior if the Explicit Vector
356   // Length parameter is strictly greater-than the number of vector elements of
357   // the operation. This function returns true when this is detected statically
358   // in the IR.
359 
360   // Check whether "W == vscale * EC.getKnownMinValue()"
361   if (EC.isScalable()) {
362     // Undig the DL
363     auto ParMod = this->getModule();
364     if (!ParMod)
365       return false;
366     const auto &DL = ParMod->getDataLayout();
367 
368     // Compare vscale patterns
369     uint64_t VScaleFactor;
370     if (match(VLParam, m_c_Mul(m_ConstantInt(VScaleFactor), m_VScale(DL))))
371       return VScaleFactor >= EC.getKnownMinValue();
372     return (EC.getKnownMinValue() == 1) && match(VLParam, m_VScale(DL));
373   }
374 
375   // standard SIMD operation
376   auto VLConst = dyn_cast<ConstantInt>(VLParam);
377   if (!VLConst)
378     return false;
379 
380   uint64_t VLNum = VLConst->getZExtValue();
381   if (VLNum >= EC.getKnownMinValue())
382     return true;
383 
384   return false;
385 }
386 
387 Instruction::BinaryOps BinaryOpIntrinsic::getBinaryOp() const {
388   switch (getIntrinsicID()) {
389   case Intrinsic::uadd_with_overflow:
390   case Intrinsic::sadd_with_overflow:
391   case Intrinsic::uadd_sat:
392   case Intrinsic::sadd_sat:
393     return Instruction::Add;
394   case Intrinsic::usub_with_overflow:
395   case Intrinsic::ssub_with_overflow:
396   case Intrinsic::usub_sat:
397   case Intrinsic::ssub_sat:
398     return Instruction::Sub;
399   case Intrinsic::umul_with_overflow:
400   case Intrinsic::smul_with_overflow:
401     return Instruction::Mul;
402   default:
403     llvm_unreachable("Invalid intrinsic");
404   }
405 }
406 
407 bool BinaryOpIntrinsic::isSigned() const {
408   switch (getIntrinsicID()) {
409   case Intrinsic::sadd_with_overflow:
410   case Intrinsic::ssub_with_overflow:
411   case Intrinsic::smul_with_overflow:
412   case Intrinsic::sadd_sat:
413   case Intrinsic::ssub_sat:
414     return true;
415   default:
416     return false;
417   }
418 }
419 
420 unsigned BinaryOpIntrinsic::getNoWrapKind() const {
421   if (isSigned())
422     return OverflowingBinaryOperator::NoSignedWrap;
423   else
424     return OverflowingBinaryOperator::NoUnsignedWrap;
425 }
426 
427 const GCStatepointInst *GCProjectionInst::getStatepoint() const {
428   const Value *Token = getArgOperand(0);
429 
430   // This takes care both of relocates for call statepoints and relocates
431   // on normal path of invoke statepoint.
432   if (!isa<LandingPadInst>(Token))
433     return cast<GCStatepointInst>(Token);
434 
435   // This relocate is on exceptional path of an invoke statepoint
436   const BasicBlock *InvokeBB =
437     cast<Instruction>(Token)->getParent()->getUniquePredecessor();
438 
439   assert(InvokeBB && "safepoints should have unique landingpads");
440   assert(InvokeBB->getTerminator() &&
441          "safepoint block should be well formed");
442 
443   return cast<GCStatepointInst>(InvokeBB->getTerminator());
444 }
445 
446 Value *GCRelocateInst::getBasePtr() const {
447   if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
448     return *(Opt->Inputs.begin() + getBasePtrIndex());
449   return *(getStatepoint()->arg_begin() + getBasePtrIndex());
450 }
451 
452 Value *GCRelocateInst::getDerivedPtr() const {
453   if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
454     return *(Opt->Inputs.begin() + getDerivedPtrIndex());
455   return *(getStatepoint()->arg_begin() + getDerivedPtrIndex());
456 }
457