File: | llvm/lib/IR/Instructions.cpp |
Warning: | line 440, column 3 Returning null reference |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- Instructions.cpp - Implement the LLVM instructions -----------------===// | |||
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 all of the non-inline methods for the LLVM instruction | |||
10 | // classes. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "llvm/IR/Instructions.h" | |||
15 | #include "LLVMContextImpl.h" | |||
16 | #include "llvm/ADT/None.h" | |||
17 | #include "llvm/ADT/SmallVector.h" | |||
18 | #include "llvm/ADT/Twine.h" | |||
19 | #include "llvm/IR/Attributes.h" | |||
20 | #include "llvm/IR/BasicBlock.h" | |||
21 | #include "llvm/IR/Constant.h" | |||
22 | #include "llvm/IR/Constants.h" | |||
23 | #include "llvm/IR/DataLayout.h" | |||
24 | #include "llvm/IR/DerivedTypes.h" | |||
25 | #include "llvm/IR/Function.h" | |||
26 | #include "llvm/IR/InstrTypes.h" | |||
27 | #include "llvm/IR/Instruction.h" | |||
28 | #include "llvm/IR/Intrinsics.h" | |||
29 | #include "llvm/IR/LLVMContext.h" | |||
30 | #include "llvm/IR/MDBuilder.h" | |||
31 | #include "llvm/IR/Metadata.h" | |||
32 | #include "llvm/IR/Module.h" | |||
33 | #include "llvm/IR/Operator.h" | |||
34 | #include "llvm/IR/Type.h" | |||
35 | #include "llvm/IR/Value.h" | |||
36 | #include "llvm/Support/AtomicOrdering.h" | |||
37 | #include "llvm/Support/Casting.h" | |||
38 | #include "llvm/Support/ErrorHandling.h" | |||
39 | #include "llvm/Support/MathExtras.h" | |||
40 | #include "llvm/Support/TypeSize.h" | |||
41 | #include <algorithm> | |||
42 | #include <cassert> | |||
43 | #include <cstdint> | |||
44 | #include <vector> | |||
45 | ||||
46 | using namespace llvm; | |||
47 | ||||
48 | static cl::opt<bool> DisableI2pP2iOpt( | |||
49 | "disable-i2p-p2i-opt", cl::init(false), | |||
50 | cl::desc("Disables inttoptr/ptrtoint roundtrip optimization")); | |||
51 | ||||
52 | //===----------------------------------------------------------------------===// | |||
53 | // AllocaInst Class | |||
54 | //===----------------------------------------------------------------------===// | |||
55 | ||||
56 | Optional<TypeSize> | |||
57 | AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { | |||
58 | TypeSize Size = DL.getTypeAllocSizeInBits(getAllocatedType()); | |||
59 | if (isArrayAllocation()) { | |||
60 | auto *C = dyn_cast<ConstantInt>(getArraySize()); | |||
61 | if (!C) | |||
62 | return None; | |||
63 | assert(!Size.isScalable() && "Array elements cannot have a scalable size")(static_cast<void> (0)); | |||
64 | Size *= C->getZExtValue(); | |||
65 | } | |||
66 | return Size; | |||
67 | } | |||
68 | ||||
69 | //===----------------------------------------------------------------------===// | |||
70 | // SelectInst Class | |||
71 | //===----------------------------------------------------------------------===// | |||
72 | ||||
73 | /// areInvalidOperands - Return a string if the specified operands are invalid | |||
74 | /// for a select operation, otherwise return null. | |||
75 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { | |||
76 | if (Op1->getType() != Op2->getType()) | |||
77 | return "both values to select must have same type"; | |||
78 | ||||
79 | if (Op1->getType()->isTokenTy()) | |||
80 | return "select values cannot have token type"; | |||
81 | ||||
82 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { | |||
83 | // Vector select. | |||
84 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) | |||
85 | return "vector select condition element type must be i1"; | |||
86 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); | |||
87 | if (!ET) | |||
88 | return "selected values for vector select must be vectors"; | |||
89 | if (ET->getElementCount() != VT->getElementCount()) | |||
90 | return "vector select requires selected vectors to have " | |||
91 | "the same vector length as select condition"; | |||
92 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { | |||
93 | return "select condition must be i1 or <n x i1>"; | |||
94 | } | |||
95 | return nullptr; | |||
96 | } | |||
97 | ||||
98 | //===----------------------------------------------------------------------===// | |||
99 | // PHINode Class | |||
100 | //===----------------------------------------------------------------------===// | |||
101 | ||||
102 | PHINode::PHINode(const PHINode &PN) | |||
103 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), | |||
104 | ReservedSpace(PN.getNumOperands()) { | |||
105 | allocHungoffUses(PN.getNumOperands()); | |||
106 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); | |||
107 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); | |||
108 | SubclassOptionalData = PN.SubclassOptionalData; | |||
109 | } | |||
110 | ||||
111 | // removeIncomingValue - Remove an incoming value. This is useful if a | |||
112 | // predecessor basic block is deleted. | |||
113 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { | |||
114 | Value *Removed = getIncomingValue(Idx); | |||
115 | ||||
116 | // Move everything after this operand down. | |||
117 | // | |||
118 | // FIXME: we could just swap with the end of the list, then erase. However, | |||
119 | // clients might not expect this to happen. The code as it is thrashes the | |||
120 | // use/def lists, which is kinda lame. | |||
121 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); | |||
122 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); | |||
123 | ||||
124 | // Nuke the last value. | |||
125 | Op<-1>().set(nullptr); | |||
126 | setNumHungOffUseOperands(getNumOperands() - 1); | |||
127 | ||||
128 | // If the PHI node is dead, because it has zero entries, nuke it now. | |||
129 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { | |||
130 | // If anyone is using this PHI, make them use a dummy value instead... | |||
131 | replaceAllUsesWith(UndefValue::get(getType())); | |||
132 | eraseFromParent(); | |||
133 | } | |||
134 | return Removed; | |||
135 | } | |||
136 | ||||
137 | /// growOperands - grow operands - This grows the operand list in response | |||
138 | /// to a push_back style of operation. This grows the number of ops by 1.5 | |||
139 | /// times. | |||
140 | /// | |||
141 | void PHINode::growOperands() { | |||
142 | unsigned e = getNumOperands(); | |||
143 | unsigned NumOps = e + e / 2; | |||
144 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. | |||
145 | ||||
146 | ReservedSpace = NumOps; | |||
147 | growHungoffUses(ReservedSpace, /* IsPhi */ true); | |||
148 | } | |||
149 | ||||
150 | /// hasConstantValue - If the specified PHI node always merges together the same | |||
151 | /// value, return the value, otherwise return null. | |||
152 | Value *PHINode::hasConstantValue() const { | |||
153 | // Exploit the fact that phi nodes always have at least one entry. | |||
154 | Value *ConstantValue = getIncomingValue(0); | |||
155 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) | |||
156 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { | |||
157 | if (ConstantValue != this) | |||
158 | return nullptr; // Incoming values not all the same. | |||
159 | // The case where the first value is this PHI. | |||
160 | ConstantValue = getIncomingValue(i); | |||
161 | } | |||
162 | if (ConstantValue == this) | |||
163 | return UndefValue::get(getType()); | |||
164 | return ConstantValue; | |||
165 | } | |||
166 | ||||
167 | /// hasConstantOrUndefValue - Whether the specified PHI node always merges | |||
168 | /// together the same value, assuming that undefs result in the same value as | |||
169 | /// non-undefs. | |||
170 | /// Unlike \ref hasConstantValue, this does not return a value because the | |||
171 | /// unique non-undef incoming value need not dominate the PHI node. | |||
172 | bool PHINode::hasConstantOrUndefValue() const { | |||
173 | Value *ConstantValue = nullptr; | |||
174 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { | |||
175 | Value *Incoming = getIncomingValue(i); | |||
176 | if (Incoming != this && !isa<UndefValue>(Incoming)) { | |||
177 | if (ConstantValue && ConstantValue != Incoming) | |||
178 | return false; | |||
179 | ConstantValue = Incoming; | |||
180 | } | |||
181 | } | |||
182 | return true; | |||
183 | } | |||
184 | ||||
185 | //===----------------------------------------------------------------------===// | |||
186 | // LandingPadInst Implementation | |||
187 | //===----------------------------------------------------------------------===// | |||
188 | ||||
189 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
190 | const Twine &NameStr, Instruction *InsertBefore) | |||
191 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { | |||
192 | init(NumReservedValues, NameStr); | |||
193 | } | |||
194 | ||||
195 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
196 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
197 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { | |||
198 | init(NumReservedValues, NameStr); | |||
199 | } | |||
200 | ||||
201 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) | |||
202 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, | |||
203 | LP.getNumOperands()), | |||
204 | ReservedSpace(LP.getNumOperands()) { | |||
205 | allocHungoffUses(LP.getNumOperands()); | |||
206 | Use *OL = getOperandList(); | |||
207 | const Use *InOL = LP.getOperandList(); | |||
208 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) | |||
209 | OL[I] = InOL[I]; | |||
210 | ||||
211 | setCleanup(LP.isCleanup()); | |||
212 | } | |||
213 | ||||
214 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, | |||
215 | const Twine &NameStr, | |||
216 | Instruction *InsertBefore) { | |||
217 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); | |||
218 | } | |||
219 | ||||
220 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, | |||
221 | const Twine &NameStr, | |||
222 | BasicBlock *InsertAtEnd) { | |||
223 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); | |||
224 | } | |||
225 | ||||
226 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { | |||
227 | ReservedSpace = NumReservedValues; | |||
228 | setNumHungOffUseOperands(0); | |||
229 | allocHungoffUses(ReservedSpace); | |||
230 | setName(NameStr); | |||
231 | setCleanup(false); | |||
232 | } | |||
233 | ||||
234 | /// growOperands - grow operands - This grows the operand list in response to a | |||
235 | /// push_back style of operation. This grows the number of ops by 2 times. | |||
236 | void LandingPadInst::growOperands(unsigned Size) { | |||
237 | unsigned e = getNumOperands(); | |||
238 | if (ReservedSpace >= e + Size) return; | |||
239 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; | |||
240 | growHungoffUses(ReservedSpace); | |||
241 | } | |||
242 | ||||
243 | void LandingPadInst::addClause(Constant *Val) { | |||
244 | unsigned OpNo = getNumOperands(); | |||
245 | growOperands(1); | |||
246 | assert(OpNo < ReservedSpace && "Growing didn't work!")(static_cast<void> (0)); | |||
247 | setNumHungOffUseOperands(getNumOperands() + 1); | |||
248 | getOperandList()[OpNo] = Val; | |||
249 | } | |||
250 | ||||
251 | //===----------------------------------------------------------------------===// | |||
252 | // CallBase Implementation | |||
253 | //===----------------------------------------------------------------------===// | |||
254 | ||||
255 | CallBase *CallBase::Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, | |||
256 | Instruction *InsertPt) { | |||
257 | switch (CB->getOpcode()) { | |||
258 | case Instruction::Call: | |||
259 | return CallInst::Create(cast<CallInst>(CB), Bundles, InsertPt); | |||
260 | case Instruction::Invoke: | |||
261 | return InvokeInst::Create(cast<InvokeInst>(CB), Bundles, InsertPt); | |||
262 | case Instruction::CallBr: | |||
263 | return CallBrInst::Create(cast<CallBrInst>(CB), Bundles, InsertPt); | |||
264 | default: | |||
265 | llvm_unreachable("Unknown CallBase sub-class!")__builtin_unreachable(); | |||
266 | } | |||
267 | } | |||
268 | ||||
269 | CallBase *CallBase::Create(CallBase *CI, OperandBundleDef OpB, | |||
270 | Instruction *InsertPt) { | |||
271 | SmallVector<OperandBundleDef, 2> OpDefs; | |||
272 | for (unsigned i = 0, e = CI->getNumOperandBundles(); i < e; ++i) { | |||
273 | auto ChildOB = CI->getOperandBundleAt(i); | |||
274 | if (ChildOB.getTagName() != OpB.getTag()) | |||
275 | OpDefs.emplace_back(ChildOB); | |||
276 | } | |||
277 | OpDefs.emplace_back(OpB); | |||
278 | return CallBase::Create(CI, OpDefs, InsertPt); | |||
279 | } | |||
280 | ||||
281 | ||||
282 | Function *CallBase::getCaller() { return getParent()->getParent(); } | |||
283 | ||||
284 | unsigned CallBase::getNumSubclassExtraOperandsDynamic() const { | |||
285 | assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!")(static_cast<void> (0)); | |||
286 | return cast<CallBrInst>(this)->getNumIndirectDests() + 1; | |||
287 | } | |||
288 | ||||
289 | bool CallBase::isIndirectCall() const { | |||
290 | const Value *V = getCalledOperand(); | |||
291 | if (isa<Function>(V) || isa<Constant>(V)) | |||
292 | return false; | |||
293 | return !isInlineAsm(); | |||
294 | } | |||
295 | ||||
296 | /// Tests if this call site must be tail call optimized. Only a CallInst can | |||
297 | /// be tail call optimized. | |||
298 | bool CallBase::isMustTailCall() const { | |||
299 | if (auto *CI = dyn_cast<CallInst>(this)) | |||
300 | return CI->isMustTailCall(); | |||
301 | return false; | |||
302 | } | |||
303 | ||||
304 | /// Tests if this call site is marked as a tail call. | |||
305 | bool CallBase::isTailCall() const { | |||
306 | if (auto *CI = dyn_cast<CallInst>(this)) | |||
307 | return CI->isTailCall(); | |||
308 | return false; | |||
309 | } | |||
310 | ||||
311 | Intrinsic::ID CallBase::getIntrinsicID() const { | |||
312 | if (auto *F = getCalledFunction()) | |||
313 | return F->getIntrinsicID(); | |||
314 | return Intrinsic::not_intrinsic; | |||
315 | } | |||
316 | ||||
317 | bool CallBase::isReturnNonNull() const { | |||
318 | if (hasRetAttr(Attribute::NonNull)) | |||
319 | return true; | |||
320 | ||||
321 | if (getRetDereferenceableBytes() > 0 && | |||
322 | !NullPointerIsDefined(getCaller(), getType()->getPointerAddressSpace())) | |||
323 | return true; | |||
324 | ||||
325 | return false; | |||
326 | } | |||
327 | ||||
328 | Value *CallBase::getReturnedArgOperand() const { | |||
329 | unsigned Index; | |||
330 | ||||
331 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) | |||
332 | return getArgOperand(Index - AttributeList::FirstArgIndex); | |||
333 | if (const Function *F = getCalledFunction()) | |||
334 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && | |||
335 | Index) | |||
336 | return getArgOperand(Index - AttributeList::FirstArgIndex); | |||
337 | ||||
338 | return nullptr; | |||
339 | } | |||
340 | ||||
341 | /// Determine whether the argument or parameter has the given attribute. | |||
342 | bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { | |||
343 | assert(ArgNo < getNumArgOperands() && "Param index out of bounds!")(static_cast<void> (0)); | |||
344 | ||||
345 | if (Attrs.hasParamAttr(ArgNo, Kind)) | |||
346 | return true; | |||
347 | if (const Function *F = getCalledFunction()) | |||
348 | return F->getAttributes().hasParamAttr(ArgNo, Kind); | |||
349 | return false; | |||
350 | } | |||
351 | ||||
352 | bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { | |||
353 | if (const Function *F = getCalledFunction()) | |||
354 | return F->getAttributes().hasFnAttr(Kind); | |||
355 | return false; | |||
356 | } | |||
357 | ||||
358 | bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { | |||
359 | if (const Function *F = getCalledFunction()) | |||
360 | return F->getAttributes().hasFnAttr(Kind); | |||
361 | return false; | |||
362 | } | |||
363 | ||||
364 | void CallBase::getOperandBundlesAsDefs( | |||
365 | SmallVectorImpl<OperandBundleDef> &Defs) const { | |||
366 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) | |||
367 | Defs.emplace_back(getOperandBundleAt(i)); | |||
368 | } | |||
369 | ||||
370 | CallBase::op_iterator | |||
371 | CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, | |||
372 | const unsigned BeginIndex) { | |||
373 | auto It = op_begin() + BeginIndex; | |||
374 | for (auto &B : Bundles) | |||
375 | It = std::copy(B.input_begin(), B.input_end(), It); | |||
376 | ||||
377 | auto *ContextImpl = getContext().pImpl; | |||
378 | auto BI = Bundles.begin(); | |||
379 | unsigned CurrentIndex = BeginIndex; | |||
380 | ||||
381 | for (auto &BOI : bundle_op_infos()) { | |||
382 | assert(BI != Bundles.end() && "Incorrect allocation?")(static_cast<void> (0)); | |||
383 | ||||
384 | BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); | |||
385 | BOI.Begin = CurrentIndex; | |||
386 | BOI.End = CurrentIndex + BI->input_size(); | |||
387 | CurrentIndex = BOI.End; | |||
388 | BI++; | |||
389 | } | |||
390 | ||||
391 | assert(BI == Bundles.end() && "Incorrect allocation?")(static_cast<void> (0)); | |||
392 | ||||
393 | return It; | |||
394 | } | |||
395 | ||||
396 | CallBase::BundleOpInfo &CallBase::getBundleOpInfoForOperand(unsigned OpIdx) { | |||
397 | /// When there isn't many bundles, we do a simple linear search. | |||
398 | /// Else fallback to a binary-search that use the fact that bundles usually | |||
399 | /// have similar number of argument to get faster convergence. | |||
400 | if (bundle_op_info_end() - bundle_op_info_begin() < 8) { | |||
| ||||
401 | for (auto &BOI : bundle_op_infos()) | |||
402 | if (BOI.Begin <= OpIdx && OpIdx < BOI.End) | |||
403 | return BOI; | |||
404 | ||||
405 | llvm_unreachable("Did not find operand bundle for operand!")__builtin_unreachable(); | |||
406 | } | |||
407 | ||||
408 | assert(OpIdx >= arg_size() && "the Idx is not in the operand bundles")(static_cast<void> (0)); | |||
409 | assert(bundle_op_info_end() - bundle_op_info_begin() > 0 &&(static_cast<void> (0)) | |||
410 | OpIdx < std::prev(bundle_op_info_end())->End &&(static_cast<void> (0)) | |||
411 | "The Idx isn't in the operand bundle")(static_cast<void> (0)); | |||
412 | ||||
413 | /// We need a decimal number below and to prevent using floating point numbers | |||
414 | /// we use an intergal value multiplied by this constant. | |||
415 | constexpr unsigned NumberScaling = 1024; | |||
416 | ||||
417 | bundle_op_iterator Begin = bundle_op_info_begin(); | |||
418 | bundle_op_iterator End = bundle_op_info_end(); | |||
419 | bundle_op_iterator Current = Begin; | |||
420 | ||||
421 | while (Begin != End) { | |||
422 | unsigned ScaledOperandPerBundle = | |||
423 | NumberScaling * (std::prev(End)->End - Begin->Begin) / (End - Begin); | |||
424 | Current = Begin + (((OpIdx - Begin->Begin) * NumberScaling) / | |||
425 | ScaledOperandPerBundle); | |||
426 | if (Current >= End) | |||
427 | Current = std::prev(End); | |||
428 | assert(Current < End && Current >= Begin &&(static_cast<void> (0)) | |||
429 | "the operand bundle doesn't cover every value in the range")(static_cast<void> (0)); | |||
430 | if (OpIdx >= Current->Begin && OpIdx < Current->End) | |||
431 | break; | |||
432 | if (OpIdx >= Current->End) | |||
433 | Begin = Current + 1; | |||
434 | else | |||
435 | End = Current; | |||
436 | } | |||
437 | ||||
438 | assert(OpIdx >= Current->Begin && OpIdx < Current->End &&(static_cast<void> (0)) | |||
439 | "the operand bundle doesn't cover every value in the range")(static_cast<void> (0)); | |||
440 | return *Current; | |||
| ||||
441 | } | |||
442 | ||||
443 | CallBase *CallBase::addOperandBundle(CallBase *CB, uint32_t ID, | |||
444 | OperandBundleDef OB, | |||
445 | Instruction *InsertPt) { | |||
446 | if (CB->getOperandBundle(ID)) | |||
447 | return CB; | |||
448 | ||||
449 | SmallVector<OperandBundleDef, 1> Bundles; | |||
450 | CB->getOperandBundlesAsDefs(Bundles); | |||
451 | Bundles.push_back(OB); | |||
452 | return Create(CB, Bundles, InsertPt); | |||
453 | } | |||
454 | ||||
455 | CallBase *CallBase::removeOperandBundle(CallBase *CB, uint32_t ID, | |||
456 | Instruction *InsertPt) { | |||
457 | SmallVector<OperandBundleDef, 1> Bundles; | |||
458 | bool CreateNew = false; | |||
459 | ||||
460 | for (unsigned I = 0, E = CB->getNumOperandBundles(); I != E; ++I) { | |||
461 | auto Bundle = CB->getOperandBundleAt(I); | |||
462 | if (Bundle.getTagID() == ID) { | |||
463 | CreateNew = true; | |||
464 | continue; | |||
465 | } | |||
466 | Bundles.emplace_back(Bundle); | |||
467 | } | |||
468 | ||||
469 | return CreateNew ? Create(CB, Bundles, InsertPt) : CB; | |||
470 | } | |||
471 | ||||
472 | bool CallBase::hasReadingOperandBundles() const { | |||
473 | // Implementation note: this is a conservative implementation of operand | |||
474 | // bundle semantics, where *any* non-assume operand bundle forces a callsite | |||
475 | // to be at least readonly. | |||
476 | return hasOperandBundles() && getIntrinsicID() != Intrinsic::assume; | |||
477 | } | |||
478 | ||||
479 | //===----------------------------------------------------------------------===// | |||
480 | // CallInst Implementation | |||
481 | //===----------------------------------------------------------------------===// | |||
482 | ||||
483 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, | |||
484 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { | |||
485 | this->FTy = FTy; | |||
486 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&(static_cast<void> (0)) | |||
487 | "NumOperands not set up?")(static_cast<void> (0)); | |||
488 | ||||
489 | #ifndef NDEBUG1 | |||
490 | assert((Args.size() == FTy->getNumParams() ||(static_cast<void> (0)) | |||
491 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&(static_cast<void> (0)) | |||
492 | "Calling a function with bad signature!")(static_cast<void> (0)); | |||
493 | ||||
494 | for (unsigned i = 0; i != Args.size(); ++i) | |||
495 | assert((i >= FTy->getNumParams() ||(static_cast<void> (0)) | |||
496 | FTy->getParamType(i) == Args[i]->getType()) &&(static_cast<void> (0)) | |||
497 | "Calling a function with a bad signature!")(static_cast<void> (0)); | |||
498 | #endif | |||
499 | ||||
500 | // Set operands in order of their index to match use-list-order | |||
501 | // prediction. | |||
502 | llvm::copy(Args, op_begin()); | |||
503 | setCalledOperand(Func); | |||
504 | ||||
505 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
506 | (void)It; | |||
507 | assert(It + 1 == op_end() && "Should add up!")(static_cast<void> (0)); | |||
508 | ||||
509 | setName(NameStr); | |||
510 | } | |||
511 | ||||
512 | void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { | |||
513 | this->FTy = FTy; | |||
514 | assert(getNumOperands() == 1 && "NumOperands not set up?")(static_cast<void> (0)); | |||
515 | setCalledOperand(Func); | |||
516 | ||||
517 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature")(static_cast<void> (0)); | |||
518 | ||||
519 | setName(NameStr); | |||
520 | } | |||
521 | ||||
522 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, | |||
523 | Instruction *InsertBefore) | |||
524 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
525 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { | |||
526 | init(Ty, Func, Name); | |||
527 | } | |||
528 | ||||
529 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, | |||
530 | BasicBlock *InsertAtEnd) | |||
531 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
532 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { | |||
533 | init(Ty, Func, Name); | |||
534 | } | |||
535 | ||||
536 | CallInst::CallInst(const CallInst &CI) | |||
537 | : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, | |||
538 | OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), | |||
539 | CI.getNumOperands()) { | |||
540 | setTailCallKind(CI.getTailCallKind()); | |||
541 | setCallingConv(CI.getCallingConv()); | |||
542 | ||||
543 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); | |||
544 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), | |||
545 | bundle_op_info_begin()); | |||
546 | SubclassOptionalData = CI.SubclassOptionalData; | |||
547 | } | |||
548 | ||||
549 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, | |||
550 | Instruction *InsertPt) { | |||
551 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); | |||
552 | ||||
553 | auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledOperand(), | |||
554 | Args, OpB, CI->getName(), InsertPt); | |||
555 | NewCI->setTailCallKind(CI->getTailCallKind()); | |||
556 | NewCI->setCallingConv(CI->getCallingConv()); | |||
557 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; | |||
558 | NewCI->setAttributes(CI->getAttributes()); | |||
559 | NewCI->setDebugLoc(CI->getDebugLoc()); | |||
560 | return NewCI; | |||
561 | } | |||
562 | ||||
563 | // Update profile weight for call instruction by scaling it using the ratio | |||
564 | // of S/T. The meaning of "branch_weights" meta data for call instruction is | |||
565 | // transfered to represent call count. | |||
566 | void CallInst::updateProfWeight(uint64_t S, uint64_t T) { | |||
567 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); | |||
568 | if (ProfileData == nullptr) | |||
569 | return; | |||
570 | ||||
571 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); | |||
572 | if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && | |||
573 | !ProfDataName->getString().equals("VP"))) | |||
574 | return; | |||
575 | ||||
576 | if (T == 0) { | |||
577 | LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in "do { } while (false) | |||
578 | "div by 0. Ignoring. Likely the function "do { } while (false) | |||
579 | << getParent()->getParent()->getName()do { } while (false) | |||
580 | << " has 0 entry count, and contains call instructions "do { } while (false) | |||
581 | "with non-zero prof info.")do { } while (false); | |||
582 | return; | |||
583 | } | |||
584 | ||||
585 | MDBuilder MDB(getContext()); | |||
586 | SmallVector<Metadata *, 3> Vals; | |||
587 | Vals.push_back(ProfileData->getOperand(0)); | |||
588 | APInt APS(128, S), APT(128, T); | |||
589 | if (ProfDataName->getString().equals("branch_weights") && | |||
590 | ProfileData->getNumOperands() > 0) { | |||
591 | // Using APInt::div may be expensive, but most cases should fit 64 bits. | |||
592 | APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)) | |||
593 | ->getValue() | |||
594 | .getZExtValue()); | |||
595 | Val *= APS; | |||
596 | Vals.push_back(MDB.createConstant( | |||
597 | ConstantInt::get(Type::getInt32Ty(getContext()), | |||
598 | Val.udiv(APT).getLimitedValue(UINT32_MAX(4294967295U))))); | |||
599 | } else if (ProfDataName->getString().equals("VP")) | |||
600 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { | |||
601 | // The first value is the key of the value profile, which will not change. | |||
602 | Vals.push_back(ProfileData->getOperand(i)); | |||
603 | uint64_t Count = | |||
604 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) | |||
605 | ->getValue() | |||
606 | .getZExtValue(); | |||
607 | // Don't scale the magic number. | |||
608 | if (Count == NOMORE_ICP_MAGICNUM) { | |||
609 | Vals.push_back(ProfileData->getOperand(i + 1)); | |||
610 | continue; | |||
611 | } | |||
612 | // Using APInt::div may be expensive, but most cases should fit 64 bits. | |||
613 | APInt Val(128, Count); | |||
614 | Val *= APS; | |||
615 | Vals.push_back(MDB.createConstant( | |||
616 | ConstantInt::get(Type::getInt64Ty(getContext()), | |||
617 | Val.udiv(APT).getLimitedValue()))); | |||
618 | } | |||
619 | setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); | |||
620 | } | |||
621 | ||||
622 | /// IsConstantOne - Return true only if val is constant int 1 | |||
623 | static bool IsConstantOne(Value *val) { | |||
624 | assert(val && "IsConstantOne does not work with nullptr val")(static_cast<void> (0)); | |||
625 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); | |||
626 | return CVal && CVal->isOne(); | |||
627 | } | |||
628 | ||||
629 | static Instruction *createMalloc(Instruction *InsertBefore, | |||
630 | BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
631 | Type *AllocTy, Value *AllocSize, | |||
632 | Value *ArraySize, | |||
633 | ArrayRef<OperandBundleDef> OpB, | |||
634 | Function *MallocF, const Twine &Name) { | |||
635 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&(static_cast<void> (0)) | |||
636 | "createMalloc needs either InsertBefore or InsertAtEnd")(static_cast<void> (0)); | |||
637 | ||||
638 | // malloc(type) becomes: | |||
639 | // bitcast (i8* malloc(typeSize)) to type* | |||
640 | // malloc(type, arraySize) becomes: | |||
641 | // bitcast (i8* malloc(typeSize*arraySize)) to type* | |||
642 | if (!ArraySize) | |||
643 | ArraySize = ConstantInt::get(IntPtrTy, 1); | |||
644 | else if (ArraySize->getType() != IntPtrTy) { | |||
645 | if (InsertBefore) | |||
646 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, | |||
647 | "", InsertBefore); | |||
648 | else | |||
649 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, | |||
650 | "", InsertAtEnd); | |||
651 | } | |||
652 | ||||
653 | if (!IsConstantOne(ArraySize)) { | |||
654 | if (IsConstantOne(AllocSize)) { | |||
655 | AllocSize = ArraySize; // Operand * 1 = Operand | |||
656 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { | |||
657 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, | |||
658 | false /*ZExt*/); | |||
659 | // Malloc arg is constant product of type size and array size | |||
660 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); | |||
661 | } else { | |||
662 | // Multiply type size by the array size... | |||
663 | if (InsertBefore) | |||
664 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, | |||
665 | "mallocsize", InsertBefore); | |||
666 | else | |||
667 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, | |||
668 | "mallocsize", InsertAtEnd); | |||
669 | } | |||
670 | } | |||
671 | ||||
672 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size")(static_cast<void> (0)); | |||
673 | // Create the call to Malloc. | |||
674 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; | |||
675 | Module *M = BB->getParent()->getParent(); | |||
676 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); | |||
677 | FunctionCallee MallocFunc = MallocF; | |||
678 | if (!MallocFunc) | |||
679 | // prototype malloc as "void *malloc(size_t)" | |||
680 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); | |||
681 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); | |||
682 | CallInst *MCall = nullptr; | |||
683 | Instruction *Result = nullptr; | |||
684 | if (InsertBefore) { | |||
685 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", | |||
686 | InsertBefore); | |||
687 | Result = MCall; | |||
688 | if (Result->getType() != AllocPtrType) | |||
689 | // Create a cast instruction to convert to the right type... | |||
690 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); | |||
691 | } else { | |||
692 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); | |||
693 | Result = MCall; | |||
694 | if (Result->getType() != AllocPtrType) { | |||
695 | InsertAtEnd->getInstList().push_back(MCall); | |||
696 | // Create a cast instruction to convert to the right type... | |||
697 | Result = new BitCastInst(MCall, AllocPtrType, Name); | |||
698 | } | |||
699 | } | |||
700 | MCall->setTailCall(); | |||
701 | if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) { | |||
702 | MCall->setCallingConv(F->getCallingConv()); | |||
703 | if (!F->returnDoesNotAlias()) | |||
704 | F->setReturnDoesNotAlias(); | |||
705 | } | |||
706 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type")(static_cast<void> (0)); | |||
707 | ||||
708 | return Result; | |||
709 | } | |||
710 | ||||
711 | /// CreateMalloc - Generate the IR for a call to malloc: | |||
712 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
713 | /// possibly multiplied by the array size if the array size is not | |||
714 | /// constant 1. | |||
715 | /// 2. Call malloc with that argument. | |||
716 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
717 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, | |||
718 | Type *IntPtrTy, Type *AllocTy, | |||
719 | Value *AllocSize, Value *ArraySize, | |||
720 | Function *MallocF, | |||
721 | const Twine &Name) { | |||
722 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, | |||
723 | ArraySize, None, MallocF, Name); | |||
724 | } | |||
725 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, | |||
726 | Type *IntPtrTy, Type *AllocTy, | |||
727 | Value *AllocSize, Value *ArraySize, | |||
728 | ArrayRef<OperandBundleDef> OpB, | |||
729 | Function *MallocF, | |||
730 | const Twine &Name) { | |||
731 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, | |||
732 | ArraySize, OpB, MallocF, Name); | |||
733 | } | |||
734 | ||||
735 | /// CreateMalloc - Generate the IR for a call to malloc: | |||
736 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
737 | /// possibly multiplied by the array size if the array size is not | |||
738 | /// constant 1. | |||
739 | /// 2. Call malloc with that argument. | |||
740 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
741 | /// Note: This function does not add the bitcast to the basic block, that is the | |||
742 | /// responsibility of the caller. | |||
743 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, | |||
744 | Type *IntPtrTy, Type *AllocTy, | |||
745 | Value *AllocSize, Value *ArraySize, | |||
746 | Function *MallocF, const Twine &Name) { | |||
747 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, | |||
748 | ArraySize, None, MallocF, Name); | |||
749 | } | |||
750 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, | |||
751 | Type *IntPtrTy, Type *AllocTy, | |||
752 | Value *AllocSize, Value *ArraySize, | |||
753 | ArrayRef<OperandBundleDef> OpB, | |||
754 | Function *MallocF, const Twine &Name) { | |||
755 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, | |||
756 | ArraySize, OpB, MallocF, Name); | |||
757 | } | |||
758 | ||||
759 | static Instruction *createFree(Value *Source, | |||
760 | ArrayRef<OperandBundleDef> Bundles, | |||
761 | Instruction *InsertBefore, | |||
762 | BasicBlock *InsertAtEnd) { | |||
763 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&(static_cast<void> (0)) | |||
764 | "createFree needs either InsertBefore or InsertAtEnd")(static_cast<void> (0)); | |||
765 | assert(Source->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
766 | "Can not free something of nonpointer type!")(static_cast<void> (0)); | |||
767 | ||||
768 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; | |||
769 | Module *M = BB->getParent()->getParent(); | |||
770 | ||||
771 | Type *VoidTy = Type::getVoidTy(M->getContext()); | |||
772 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); | |||
773 | // prototype free as "void free(void*)" | |||
774 | FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); | |||
775 | CallInst *Result = nullptr; | |||
776 | Value *PtrCast = Source; | |||
777 | if (InsertBefore) { | |||
778 | if (Source->getType() != IntPtrTy) | |||
779 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); | |||
780 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); | |||
781 | } else { | |||
782 | if (Source->getType() != IntPtrTy) | |||
783 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); | |||
784 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); | |||
785 | } | |||
786 | Result->setTailCall(); | |||
787 | if (Function *F = dyn_cast<Function>(FreeFunc.getCallee())) | |||
788 | Result->setCallingConv(F->getCallingConv()); | |||
789 | ||||
790 | return Result; | |||
791 | } | |||
792 | ||||
793 | /// CreateFree - Generate the IR for a call to the builtin free function. | |||
794 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { | |||
795 | return createFree(Source, None, InsertBefore, nullptr); | |||
796 | } | |||
797 | Instruction *CallInst::CreateFree(Value *Source, | |||
798 | ArrayRef<OperandBundleDef> Bundles, | |||
799 | Instruction *InsertBefore) { | |||
800 | return createFree(Source, Bundles, InsertBefore, nullptr); | |||
801 | } | |||
802 | ||||
803 | /// CreateFree - Generate the IR for a call to the builtin free function. | |||
804 | /// Note: This function does not add the call to the basic block, that is the | |||
805 | /// responsibility of the caller. | |||
806 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { | |||
807 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); | |||
808 | assert(FreeCall && "CreateFree did not create a CallInst")(static_cast<void> (0)); | |||
809 | return FreeCall; | |||
810 | } | |||
811 | Instruction *CallInst::CreateFree(Value *Source, | |||
812 | ArrayRef<OperandBundleDef> Bundles, | |||
813 | BasicBlock *InsertAtEnd) { | |||
814 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); | |||
815 | assert(FreeCall && "CreateFree did not create a CallInst")(static_cast<void> (0)); | |||
816 | return FreeCall; | |||
817 | } | |||
818 | ||||
819 | //===----------------------------------------------------------------------===// | |||
820 | // InvokeInst Implementation | |||
821 | //===----------------------------------------------------------------------===// | |||
822 | ||||
823 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, | |||
824 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
825 | ArrayRef<OperandBundleDef> Bundles, | |||
826 | const Twine &NameStr) { | |||
827 | this->FTy = FTy; | |||
828 | ||||
829 | assert((int)getNumOperands() ==(static_cast<void> (0)) | |||
830 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&(static_cast<void> (0)) | |||
831 | "NumOperands not set up?")(static_cast<void> (0)); | |||
832 | ||||
833 | #ifndef NDEBUG1 | |||
834 | assert(((Args.size() == FTy->getNumParams()) ||(static_cast<void> (0)) | |||
835 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&(static_cast<void> (0)) | |||
836 | "Invoking a function with bad signature")(static_cast<void> (0)); | |||
837 | ||||
838 | for (unsigned i = 0, e = Args.size(); i != e; i++) | |||
839 | assert((i >= FTy->getNumParams() ||(static_cast<void> (0)) | |||
840 | FTy->getParamType(i) == Args[i]->getType()) &&(static_cast<void> (0)) | |||
841 | "Invoking a function with a bad signature!")(static_cast<void> (0)); | |||
842 | #endif | |||
843 | ||||
844 | // Set operands in order of their index to match use-list-order | |||
845 | // prediction. | |||
846 | llvm::copy(Args, op_begin()); | |||
847 | setNormalDest(IfNormal); | |||
848 | setUnwindDest(IfException); | |||
849 | setCalledOperand(Fn); | |||
850 | ||||
851 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
852 | (void)It; | |||
853 | assert(It + 3 == op_end() && "Should add up!")(static_cast<void> (0)); | |||
854 | ||||
855 | setName(NameStr); | |||
856 | } | |||
857 | ||||
858 | InvokeInst::InvokeInst(const InvokeInst &II) | |||
859 | : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, | |||
860 | OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), | |||
861 | II.getNumOperands()) { | |||
862 | setCallingConv(II.getCallingConv()); | |||
863 | std::copy(II.op_begin(), II.op_end(), op_begin()); | |||
864 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), | |||
865 | bundle_op_info_begin()); | |||
866 | SubclassOptionalData = II.SubclassOptionalData; | |||
867 | } | |||
868 | ||||
869 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, | |||
870 | Instruction *InsertPt) { | |||
871 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); | |||
872 | ||||
873 | auto *NewII = InvokeInst::Create( | |||
874 | II->getFunctionType(), II->getCalledOperand(), II->getNormalDest(), | |||
875 | II->getUnwindDest(), Args, OpB, II->getName(), InsertPt); | |||
876 | NewII->setCallingConv(II->getCallingConv()); | |||
877 | NewII->SubclassOptionalData = II->SubclassOptionalData; | |||
878 | NewII->setAttributes(II->getAttributes()); | |||
879 | NewII->setDebugLoc(II->getDebugLoc()); | |||
880 | return NewII; | |||
881 | } | |||
882 | ||||
883 | LandingPadInst *InvokeInst::getLandingPadInst() const { | |||
884 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); | |||
885 | } | |||
886 | ||||
887 | //===----------------------------------------------------------------------===// | |||
888 | // CallBrInst Implementation | |||
889 | //===----------------------------------------------------------------------===// | |||
890 | ||||
891 | void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough, | |||
892 | ArrayRef<BasicBlock *> IndirectDests, | |||
893 | ArrayRef<Value *> Args, | |||
894 | ArrayRef<OperandBundleDef> Bundles, | |||
895 | const Twine &NameStr) { | |||
896 | this->FTy = FTy; | |||
897 | ||||
898 | assert((int)getNumOperands() ==(static_cast<void> (0)) | |||
899 | ComputeNumOperands(Args.size(), IndirectDests.size(),(static_cast<void> (0)) | |||
900 | CountBundleInputs(Bundles)) &&(static_cast<void> (0)) | |||
901 | "NumOperands not set up?")(static_cast<void> (0)); | |||
902 | ||||
903 | #ifndef NDEBUG1 | |||
904 | assert(((Args.size() == FTy->getNumParams()) ||(static_cast<void> (0)) | |||
905 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&(static_cast<void> (0)) | |||
906 | "Calling a function with bad signature")(static_cast<void> (0)); | |||
907 | ||||
908 | for (unsigned i = 0, e = Args.size(); i != e; i++) | |||
909 | assert((i >= FTy->getNumParams() ||(static_cast<void> (0)) | |||
910 | FTy->getParamType(i) == Args[i]->getType()) &&(static_cast<void> (0)) | |||
911 | "Calling a function with a bad signature!")(static_cast<void> (0)); | |||
912 | #endif | |||
913 | ||||
914 | // Set operands in order of their index to match use-list-order | |||
915 | // prediction. | |||
916 | std::copy(Args.begin(), Args.end(), op_begin()); | |||
917 | NumIndirectDests = IndirectDests.size(); | |||
918 | setDefaultDest(Fallthrough); | |||
919 | for (unsigned i = 0; i != NumIndirectDests; ++i) | |||
920 | setIndirectDest(i, IndirectDests[i]); | |||
921 | setCalledOperand(Fn); | |||
922 | ||||
923 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
924 | (void)It; | |||
925 | assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!")(static_cast<void> (0)); | |||
926 | ||||
927 | setName(NameStr); | |||
928 | } | |||
929 | ||||
930 | void CallBrInst::updateArgBlockAddresses(unsigned i, BasicBlock *B) { | |||
931 | assert(getNumIndirectDests() > i && "IndirectDest # out of range for callbr")(static_cast<void> (0)); | |||
932 | if (BasicBlock *OldBB = getIndirectDest(i)) { | |||
933 | BlockAddress *Old = BlockAddress::get(OldBB); | |||
934 | BlockAddress *New = BlockAddress::get(B); | |||
935 | for (unsigned ArgNo = 0, e = getNumArgOperands(); ArgNo != e; ++ArgNo) | |||
936 | if (dyn_cast<BlockAddress>(getArgOperand(ArgNo)) == Old) | |||
937 | setArgOperand(ArgNo, New); | |||
938 | } | |||
939 | } | |||
940 | ||||
941 | CallBrInst::CallBrInst(const CallBrInst &CBI) | |||
942 | : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, | |||
943 | OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), | |||
944 | CBI.getNumOperands()) { | |||
945 | setCallingConv(CBI.getCallingConv()); | |||
946 | std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); | |||
947 | std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), | |||
948 | bundle_op_info_begin()); | |||
949 | SubclassOptionalData = CBI.SubclassOptionalData; | |||
950 | NumIndirectDests = CBI.NumIndirectDests; | |||
951 | } | |||
952 | ||||
953 | CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, | |||
954 | Instruction *InsertPt) { | |||
955 | std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); | |||
956 | ||||
957 | auto *NewCBI = CallBrInst::Create( | |||
958 | CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(), | |||
959 | CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt); | |||
960 | NewCBI->setCallingConv(CBI->getCallingConv()); | |||
961 | NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; | |||
962 | NewCBI->setAttributes(CBI->getAttributes()); | |||
963 | NewCBI->setDebugLoc(CBI->getDebugLoc()); | |||
964 | NewCBI->NumIndirectDests = CBI->NumIndirectDests; | |||
965 | return NewCBI; | |||
966 | } | |||
967 | ||||
968 | //===----------------------------------------------------------------------===// | |||
969 | // ReturnInst Implementation | |||
970 | //===----------------------------------------------------------------------===// | |||
971 | ||||
972 | ReturnInst::ReturnInst(const ReturnInst &RI) | |||
973 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, | |||
974 | OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), | |||
975 | RI.getNumOperands()) { | |||
976 | if (RI.getNumOperands()) | |||
977 | Op<0>() = RI.Op<0>(); | |||
978 | SubclassOptionalData = RI.SubclassOptionalData; | |||
979 | } | |||
980 | ||||
981 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) | |||
982 | : Instruction(Type::getVoidTy(C), Instruction::Ret, | |||
983 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, | |||
984 | InsertBefore) { | |||
985 | if (retVal) | |||
986 | Op<0>() = retVal; | |||
987 | } | |||
988 | ||||
989 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) | |||
990 | : Instruction(Type::getVoidTy(C), Instruction::Ret, | |||
991 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, | |||
992 | InsertAtEnd) { | |||
993 | if (retVal) | |||
994 | Op<0>() = retVal; | |||
995 | } | |||
996 | ||||
997 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) | |||
998 | : Instruction(Type::getVoidTy(Context), Instruction::Ret, | |||
999 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} | |||
1000 | ||||
1001 | //===----------------------------------------------------------------------===// | |||
1002 | // ResumeInst Implementation | |||
1003 | //===----------------------------------------------------------------------===// | |||
1004 | ||||
1005 | ResumeInst::ResumeInst(const ResumeInst &RI) | |||
1006 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, | |||
1007 | OperandTraits<ResumeInst>::op_begin(this), 1) { | |||
1008 | Op<0>() = RI.Op<0>(); | |||
1009 | } | |||
1010 | ||||
1011 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) | |||
1012 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, | |||
1013 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { | |||
1014 | Op<0>() = Exn; | |||
1015 | } | |||
1016 | ||||
1017 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) | |||
1018 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, | |||
1019 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { | |||
1020 | Op<0>() = Exn; | |||
1021 | } | |||
1022 | ||||
1023 | //===----------------------------------------------------------------------===// | |||
1024 | // CleanupReturnInst Implementation | |||
1025 | //===----------------------------------------------------------------------===// | |||
1026 | ||||
1027 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) | |||
1028 | : Instruction(CRI.getType(), Instruction::CleanupRet, | |||
1029 | OperandTraits<CleanupReturnInst>::op_end(this) - | |||
1030 | CRI.getNumOperands(), | |||
1031 | CRI.getNumOperands()) { | |||
1032 | setSubclassData<Instruction::OpaqueField>( | |||
1033 | CRI.getSubclassData<Instruction::OpaqueField>()); | |||
1034 | Op<0>() = CRI.Op<0>(); | |||
1035 | if (CRI.hasUnwindDest()) | |||
1036 | Op<1>() = CRI.Op<1>(); | |||
1037 | } | |||
1038 | ||||
1039 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { | |||
1040 | if (UnwindBB) | |||
1041 | setSubclassData<UnwindDestField>(true); | |||
1042 | ||||
1043 | Op<0>() = CleanupPad; | |||
1044 | if (UnwindBB) | |||
1045 | Op<1>() = UnwindBB; | |||
1046 | } | |||
1047 | ||||
1048 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, | |||
1049 | unsigned Values, Instruction *InsertBefore) | |||
1050 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), | |||
1051 | Instruction::CleanupRet, | |||
1052 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, | |||
1053 | Values, InsertBefore) { | |||
1054 | init(CleanupPad, UnwindBB); | |||
1055 | } | |||
1056 | ||||
1057 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, | |||
1058 | unsigned Values, BasicBlock *InsertAtEnd) | |||
1059 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), | |||
1060 | Instruction::CleanupRet, | |||
1061 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, | |||
1062 | Values, InsertAtEnd) { | |||
1063 | init(CleanupPad, UnwindBB); | |||
1064 | } | |||
1065 | ||||
1066 | //===----------------------------------------------------------------------===// | |||
1067 | // CatchReturnInst Implementation | |||
1068 | //===----------------------------------------------------------------------===// | |||
1069 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { | |||
1070 | Op<0>() = CatchPad; | |||
1071 | Op<1>() = BB; | |||
1072 | } | |||
1073 | ||||
1074 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) | |||
1075 | : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, | |||
1076 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { | |||
1077 | Op<0>() = CRI.Op<0>(); | |||
1078 | Op<1>() = CRI.Op<1>(); | |||
1079 | } | |||
1080 | ||||
1081 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, | |||
1082 | Instruction *InsertBefore) | |||
1083 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, | |||
1084 | OperandTraits<CatchReturnInst>::op_begin(this), 2, | |||
1085 | InsertBefore) { | |||
1086 | init(CatchPad, BB); | |||
1087 | } | |||
1088 | ||||
1089 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, | |||
1090 | BasicBlock *InsertAtEnd) | |||
1091 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, | |||
1092 | OperandTraits<CatchReturnInst>::op_begin(this), 2, | |||
1093 | InsertAtEnd) { | |||
1094 | init(CatchPad, BB); | |||
1095 | } | |||
1096 | ||||
1097 | //===----------------------------------------------------------------------===// | |||
1098 | // CatchSwitchInst Implementation | |||
1099 | //===----------------------------------------------------------------------===// | |||
1100 | ||||
1101 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
1102 | unsigned NumReservedValues, | |||
1103 | const Twine &NameStr, | |||
1104 | Instruction *InsertBefore) | |||
1105 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, | |||
1106 | InsertBefore) { | |||
1107 | if (UnwindDest) | |||
1108 | ++NumReservedValues; | |||
1109 | init(ParentPad, UnwindDest, NumReservedValues + 1); | |||
1110 | setName(NameStr); | |||
1111 | } | |||
1112 | ||||
1113 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
1114 | unsigned NumReservedValues, | |||
1115 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
1116 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, | |||
1117 | InsertAtEnd) { | |||
1118 | if (UnwindDest) | |||
1119 | ++NumReservedValues; | |||
1120 | init(ParentPad, UnwindDest, NumReservedValues + 1); | |||
1121 | setName(NameStr); | |||
1122 | } | |||
1123 | ||||
1124 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) | |||
1125 | : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, | |||
1126 | CSI.getNumOperands()) { | |||
1127 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); | |||
1128 | setNumHungOffUseOperands(ReservedSpace); | |||
1129 | Use *OL = getOperandList(); | |||
1130 | const Use *InOL = CSI.getOperandList(); | |||
1131 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) | |||
1132 | OL[I] = InOL[I]; | |||
1133 | } | |||
1134 | ||||
1135 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, | |||
1136 | unsigned NumReservedValues) { | |||
1137 | assert(ParentPad && NumReservedValues)(static_cast<void> (0)); | |||
1138 | ||||
1139 | ReservedSpace = NumReservedValues; | |||
1140 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); | |||
1141 | allocHungoffUses(ReservedSpace); | |||
1142 | ||||
1143 | Op<0>() = ParentPad; | |||
1144 | if (UnwindDest) { | |||
1145 | setSubclassData<UnwindDestField>(true); | |||
1146 | setUnwindDest(UnwindDest); | |||
1147 | } | |||
1148 | } | |||
1149 | ||||
1150 | /// growOperands - grow operands - This grows the operand list in response to a | |||
1151 | /// push_back style of operation. This grows the number of ops by 2 times. | |||
1152 | void CatchSwitchInst::growOperands(unsigned Size) { | |||
1153 | unsigned NumOperands = getNumOperands(); | |||
1154 | assert(NumOperands >= 1)(static_cast<void> (0)); | |||
1155 | if (ReservedSpace >= NumOperands + Size) | |||
1156 | return; | |||
1157 | ReservedSpace = (NumOperands + Size / 2) * 2; | |||
1158 | growHungoffUses(ReservedSpace); | |||
1159 | } | |||
1160 | ||||
1161 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { | |||
1162 | unsigned OpNo = getNumOperands(); | |||
1163 | growOperands(1); | |||
1164 | assert(OpNo < ReservedSpace && "Growing didn't work!")(static_cast<void> (0)); | |||
1165 | setNumHungOffUseOperands(getNumOperands() + 1); | |||
1166 | getOperandList()[OpNo] = Handler; | |||
1167 | } | |||
1168 | ||||
1169 | void CatchSwitchInst::removeHandler(handler_iterator HI) { | |||
1170 | // Move all subsequent handlers up one. | |||
1171 | Use *EndDst = op_end() - 1; | |||
1172 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) | |||
1173 | *CurDst = *(CurDst + 1); | |||
1174 | // Null out the last handler use. | |||
1175 | *EndDst = nullptr; | |||
1176 | ||||
1177 | setNumHungOffUseOperands(getNumOperands() - 1); | |||
1178 | } | |||
1179 | ||||
1180 | //===----------------------------------------------------------------------===// | |||
1181 | // FuncletPadInst Implementation | |||
1182 | //===----------------------------------------------------------------------===// | |||
1183 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, | |||
1184 | const Twine &NameStr) { | |||
1185 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?")(static_cast<void> (0)); | |||
1186 | llvm::copy(Args, op_begin()); | |||
1187 | setParentPad(ParentPad); | |||
1188 | setName(NameStr); | |||
1189 | } | |||
1190 | ||||
1191 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) | |||
1192 | : Instruction(FPI.getType(), FPI.getOpcode(), | |||
1193 | OperandTraits<FuncletPadInst>::op_end(this) - | |||
1194 | FPI.getNumOperands(), | |||
1195 | FPI.getNumOperands()) { | |||
1196 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); | |||
1197 | setParentPad(FPI.getParentPad()); | |||
1198 | } | |||
1199 | ||||
1200 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, | |||
1201 | ArrayRef<Value *> Args, unsigned Values, | |||
1202 | const Twine &NameStr, Instruction *InsertBefore) | |||
1203 | : Instruction(ParentPad->getType(), Op, | |||
1204 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, | |||
1205 | InsertBefore) { | |||
1206 | init(ParentPad, Args, NameStr); | |||
1207 | } | |||
1208 | ||||
1209 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, | |||
1210 | ArrayRef<Value *> Args, unsigned Values, | |||
1211 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
1212 | : Instruction(ParentPad->getType(), Op, | |||
1213 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, | |||
1214 | InsertAtEnd) { | |||
1215 | init(ParentPad, Args, NameStr); | |||
1216 | } | |||
1217 | ||||
1218 | //===----------------------------------------------------------------------===// | |||
1219 | // UnreachableInst Implementation | |||
1220 | //===----------------------------------------------------------------------===// | |||
1221 | ||||
1222 | UnreachableInst::UnreachableInst(LLVMContext &Context, | |||
1223 | Instruction *InsertBefore) | |||
1224 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, | |||
1225 | 0, InsertBefore) {} | |||
1226 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) | |||
1227 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, | |||
1228 | 0, InsertAtEnd) {} | |||
1229 | ||||
1230 | //===----------------------------------------------------------------------===// | |||
1231 | // BranchInst Implementation | |||
1232 | //===----------------------------------------------------------------------===// | |||
1233 | ||||
1234 | void BranchInst::AssertOK() { | |||
1235 | if (isConditional()) | |||
1236 | assert(getCondition()->getType()->isIntegerTy(1) &&(static_cast<void> (0)) | |||
1237 | "May only branch on boolean predicates!")(static_cast<void> (0)); | |||
1238 | } | |||
1239 | ||||
1240 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) | |||
1241 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1242 | OperandTraits<BranchInst>::op_end(this) - 1, 1, | |||
1243 | InsertBefore) { | |||
1244 | assert(IfTrue && "Branch destination may not be null!")(static_cast<void> (0)); | |||
1245 | Op<-1>() = IfTrue; | |||
1246 | } | |||
1247 | ||||
1248 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
1249 | Instruction *InsertBefore) | |||
1250 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1251 | OperandTraits<BranchInst>::op_end(this) - 3, 3, | |||
1252 | InsertBefore) { | |||
1253 | // Assign in order of operand index to make use-list order predictable. | |||
1254 | Op<-3>() = Cond; | |||
1255 | Op<-2>() = IfFalse; | |||
1256 | Op<-1>() = IfTrue; | |||
1257 | #ifndef NDEBUG1 | |||
1258 | AssertOK(); | |||
1259 | #endif | |||
1260 | } | |||
1261 | ||||
1262 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) | |||
1263 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1264 | OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { | |||
1265 | assert(IfTrue && "Branch destination may not be null!")(static_cast<void> (0)); | |||
1266 | Op<-1>() = IfTrue; | |||
1267 | } | |||
1268 | ||||
1269 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
1270 | BasicBlock *InsertAtEnd) | |||
1271 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1272 | OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { | |||
1273 | // Assign in order of operand index to make use-list order predictable. | |||
1274 | Op<-3>() = Cond; | |||
1275 | Op<-2>() = IfFalse; | |||
1276 | Op<-1>() = IfTrue; | |||
1277 | #ifndef NDEBUG1 | |||
1278 | AssertOK(); | |||
1279 | #endif | |||
1280 | } | |||
1281 | ||||
1282 | BranchInst::BranchInst(const BranchInst &BI) | |||
1283 | : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, | |||
1284 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), | |||
1285 | BI.getNumOperands()) { | |||
1286 | // Assign in order of operand index to make use-list order predictable. | |||
1287 | if (BI.getNumOperands() != 1) { | |||
1288 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!")(static_cast<void> (0)); | |||
1289 | Op<-3>() = BI.Op<-3>(); | |||
1290 | Op<-2>() = BI.Op<-2>(); | |||
1291 | } | |||
1292 | Op<-1>() = BI.Op<-1>(); | |||
1293 | SubclassOptionalData = BI.SubclassOptionalData; | |||
1294 | } | |||
1295 | ||||
1296 | void BranchInst::swapSuccessors() { | |||
1297 | assert(isConditional() &&(static_cast<void> (0)) | |||
1298 | "Cannot swap successors of an unconditional branch")(static_cast<void> (0)); | |||
1299 | Op<-1>().swap(Op<-2>()); | |||
1300 | ||||
1301 | // Update profile metadata if present and it matches our structural | |||
1302 | // expectations. | |||
1303 | swapProfMetadata(); | |||
1304 | } | |||
1305 | ||||
1306 | //===----------------------------------------------------------------------===// | |||
1307 | // AllocaInst Implementation | |||
1308 | //===----------------------------------------------------------------------===// | |||
1309 | ||||
1310 | static Value *getAISize(LLVMContext &Context, Value *Amt) { | |||
1311 | if (!Amt) | |||
1312 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); | |||
1313 | else { | |||
1314 | assert(!isa<BasicBlock>(Amt) &&(static_cast<void> (0)) | |||
1315 | "Passed basic block into allocation size parameter! Use other ctor")(static_cast<void> (0)); | |||
1316 | assert(Amt->getType()->isIntegerTy() &&(static_cast<void> (0)) | |||
1317 | "Allocation array size is not an integer!")(static_cast<void> (0)); | |||
1318 | } | |||
1319 | return Amt; | |||
1320 | } | |||
1321 | ||||
1322 | static Align computeAllocaDefaultAlign(Type *Ty, BasicBlock *BB) { | |||
1323 | assert(BB && "Insertion BB cannot be null when alignment not provided!")(static_cast<void> (0)); | |||
1324 | assert(BB->getParent() &&(static_cast<void> (0)) | |||
1325 | "BB must be in a Function when alignment not provided!")(static_cast<void> (0)); | |||
1326 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1327 | return DL.getPrefTypeAlign(Ty); | |||
1328 | } | |||
1329 | ||||
1330 | static Align computeAllocaDefaultAlign(Type *Ty, Instruction *I) { | |||
1331 | assert(I && "Insertion position cannot be null when alignment not provided!")(static_cast<void> (0)); | |||
1332 | return computeAllocaDefaultAlign(Ty, I->getParent()); | |||
1333 | } | |||
1334 | ||||
1335 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, | |||
1336 | Instruction *InsertBefore) | |||
1337 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} | |||
1338 | ||||
1339 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, | |||
1340 | BasicBlock *InsertAtEnd) | |||
1341 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} | |||
1342 | ||||
1343 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1344 | const Twine &Name, Instruction *InsertBefore) | |||
1345 | : AllocaInst(Ty, AddrSpace, ArraySize, | |||
1346 | computeAllocaDefaultAlign(Ty, InsertBefore), Name, | |||
1347 | InsertBefore) {} | |||
1348 | ||||
1349 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1350 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1351 | : AllocaInst(Ty, AddrSpace, ArraySize, | |||
1352 | computeAllocaDefaultAlign(Ty, InsertAtEnd), Name, | |||
1353 | InsertAtEnd) {} | |||
1354 | ||||
1355 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1356 | Align Align, const Twine &Name, | |||
1357 | Instruction *InsertBefore) | |||
1358 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, | |||
1359 | getAISize(Ty->getContext(), ArraySize), InsertBefore), | |||
1360 | AllocatedType(Ty) { | |||
1361 | setAlignment(Align); | |||
1362 | assert(!Ty->isVoidTy() && "Cannot allocate void!")(static_cast<void> (0)); | |||
1363 | setName(Name); | |||
1364 | } | |||
1365 | ||||
1366 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1367 | Align Align, const Twine &Name, BasicBlock *InsertAtEnd) | |||
1368 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, | |||
1369 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), | |||
1370 | AllocatedType(Ty) { | |||
1371 | setAlignment(Align); | |||
1372 | assert(!Ty->isVoidTy() && "Cannot allocate void!")(static_cast<void> (0)); | |||
1373 | setName(Name); | |||
1374 | } | |||
1375 | ||||
1376 | ||||
1377 | bool AllocaInst::isArrayAllocation() const { | |||
1378 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) | |||
1379 | return !CI->isOne(); | |||
1380 | return true; | |||
1381 | } | |||
1382 | ||||
1383 | /// isStaticAlloca - Return true if this alloca is in the entry block of the | |||
1384 | /// function and is a constant size. If so, the code generator will fold it | |||
1385 | /// into the prolog/epilog code, so it is basically free. | |||
1386 | bool AllocaInst::isStaticAlloca() const { | |||
1387 | // Must be constant size. | |||
1388 | if (!isa<ConstantInt>(getArraySize())) return false; | |||
1389 | ||||
1390 | // Must be in the entry block. | |||
1391 | const BasicBlock *Parent = getParent(); | |||
1392 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); | |||
1393 | } | |||
1394 | ||||
1395 | //===----------------------------------------------------------------------===// | |||
1396 | // LoadInst Implementation | |||
1397 | //===----------------------------------------------------------------------===// | |||
1398 | ||||
1399 | void LoadInst::AssertOK() { | |||
1400 | assert(getOperand(0)->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
1401 | "Ptr must have pointer type.")(static_cast<void> (0)); | |||
1402 | assert(!(isAtomic() && getAlignment() == 0) &&(static_cast<void> (0)) | |||
1403 | "Alignment required for atomic load")(static_cast<void> (0)); | |||
1404 | } | |||
1405 | ||||
1406 | static Align computeLoadStoreDefaultAlign(Type *Ty, BasicBlock *BB) { | |||
1407 | assert(BB && "Insertion BB cannot be null when alignment not provided!")(static_cast<void> (0)); | |||
1408 | assert(BB->getParent() &&(static_cast<void> (0)) | |||
1409 | "BB must be in a Function when alignment not provided!")(static_cast<void> (0)); | |||
1410 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1411 | return DL.getABITypeAlign(Ty); | |||
1412 | } | |||
1413 | ||||
1414 | static Align computeLoadStoreDefaultAlign(Type *Ty, Instruction *I) { | |||
1415 | assert(I && "Insertion position cannot be null when alignment not provided!")(static_cast<void> (0)); | |||
1416 | return computeLoadStoreDefaultAlign(Ty, I->getParent()); | |||
1417 | } | |||
1418 | ||||
1419 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, | |||
1420 | Instruction *InsertBef) | |||
1421 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} | |||
1422 | ||||
1423 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, | |||
1424 | BasicBlock *InsertAE) | |||
1425 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} | |||
1426 | ||||
1427 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1428 | Instruction *InsertBef) | |||
1429 | : LoadInst(Ty, Ptr, Name, isVolatile, | |||
1430 | computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {} | |||
1431 | ||||
1432 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1433 | BasicBlock *InsertAE) | |||
1434 | : LoadInst(Ty, Ptr, Name, isVolatile, | |||
1435 | computeLoadStoreDefaultAlign(Ty, InsertAE), InsertAE) {} | |||
1436 | ||||
1437 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1438 | Align Align, Instruction *InsertBef) | |||
1439 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1440 | SyncScope::System, InsertBef) {} | |||
1441 | ||||
1442 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1443 | Align Align, BasicBlock *InsertAE) | |||
1444 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1445 | SyncScope::System, InsertAE) {} | |||
1446 | ||||
1447 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1448 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
1449 | Instruction *InsertBef) | |||
1450 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { | |||
1451 | assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty))(static_cast<void> (0)); | |||
1452 | setVolatile(isVolatile); | |||
1453 | setAlignment(Align); | |||
1454 | setAtomic(Order, SSID); | |||
1455 | AssertOK(); | |||
1456 | setName(Name); | |||
1457 | } | |||
1458 | ||||
1459 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1460 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
1461 | BasicBlock *InsertAE) | |||
1462 | : UnaryInstruction(Ty, Load, Ptr, InsertAE) { | |||
1463 | assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty))(static_cast<void> (0)); | |||
1464 | setVolatile(isVolatile); | |||
1465 | setAlignment(Align); | |||
1466 | setAtomic(Order, SSID); | |||
1467 | AssertOK(); | |||
1468 | setName(Name); | |||
1469 | } | |||
1470 | ||||
1471 | //===----------------------------------------------------------------------===// | |||
1472 | // StoreInst Implementation | |||
1473 | //===----------------------------------------------------------------------===// | |||
1474 | ||||
1475 | void StoreInst::AssertOK() { | |||
1476 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!")(static_cast<void> (0)); | |||
1477 | assert(getOperand(1)->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
1478 | "Ptr must have pointer type!")(static_cast<void> (0)); | |||
1479 | assert(cast<PointerType>(getOperand(1)->getType())(static_cast<void> (0)) | |||
1480 | ->isOpaqueOrPointeeTypeMatches(getOperand(0)->getType()) &&(static_cast<void> (0)) | |||
1481 | "Ptr must be a pointer to Val type!")(static_cast<void> (0)); | |||
1482 | assert(!(isAtomic() && getAlignment() == 0) &&(static_cast<void> (0)) | |||
1483 | "Alignment required for atomic store")(static_cast<void> (0)); | |||
1484 | } | |||
1485 | ||||
1486 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) | |||
1487 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} | |||
1488 | ||||
1489 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) | |||
1490 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} | |||
1491 | ||||
1492 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, | |||
1493 | Instruction *InsertBefore) | |||
1494 | : StoreInst(val, addr, isVolatile, | |||
1495 | computeLoadStoreDefaultAlign(val->getType(), InsertBefore), | |||
1496 | InsertBefore) {} | |||
1497 | ||||
1498 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, | |||
1499 | BasicBlock *InsertAtEnd) | |||
1500 | : StoreInst(val, addr, isVolatile, | |||
1501 | computeLoadStoreDefaultAlign(val->getType(), InsertAtEnd), | |||
1502 | InsertAtEnd) {} | |||
1503 | ||||
1504 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1505 | Instruction *InsertBefore) | |||
1506 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1507 | SyncScope::System, InsertBefore) {} | |||
1508 | ||||
1509 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1510 | BasicBlock *InsertAtEnd) | |||
1511 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1512 | SyncScope::System, InsertAtEnd) {} | |||
1513 | ||||
1514 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1515 | AtomicOrdering Order, SyncScope::ID SSID, | |||
1516 | Instruction *InsertBefore) | |||
1517 | : Instruction(Type::getVoidTy(val->getContext()), Store, | |||
1518 | OperandTraits<StoreInst>::op_begin(this), | |||
1519 | OperandTraits<StoreInst>::operands(this), InsertBefore) { | |||
1520 | Op<0>() = val; | |||
1521 | Op<1>() = addr; | |||
1522 | setVolatile(isVolatile); | |||
1523 | setAlignment(Align); | |||
1524 | setAtomic(Order, SSID); | |||
1525 | AssertOK(); | |||
1526 | } | |||
1527 | ||||
1528 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1529 | AtomicOrdering Order, SyncScope::ID SSID, | |||
1530 | BasicBlock *InsertAtEnd) | |||
1531 | : Instruction(Type::getVoidTy(val->getContext()), Store, | |||
1532 | OperandTraits<StoreInst>::op_begin(this), | |||
1533 | OperandTraits<StoreInst>::operands(this), InsertAtEnd) { | |||
1534 | Op<0>() = val; | |||
1535 | Op<1>() = addr; | |||
1536 | setVolatile(isVolatile); | |||
1537 | setAlignment(Align); | |||
1538 | setAtomic(Order, SSID); | |||
1539 | AssertOK(); | |||
1540 | } | |||
1541 | ||||
1542 | ||||
1543 | //===----------------------------------------------------------------------===// | |||
1544 | // AtomicCmpXchgInst Implementation | |||
1545 | //===----------------------------------------------------------------------===// | |||
1546 | ||||
1547 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1548 | Align Alignment, AtomicOrdering SuccessOrdering, | |||
1549 | AtomicOrdering FailureOrdering, | |||
1550 | SyncScope::ID SSID) { | |||
1551 | Op<0>() = Ptr; | |||
1552 | Op<1>() = Cmp; | |||
1553 | Op<2>() = NewVal; | |||
1554 | setSuccessOrdering(SuccessOrdering); | |||
1555 | setFailureOrdering(FailureOrdering); | |||
1556 | setSyncScopeID(SSID); | |||
1557 | setAlignment(Alignment); | |||
1558 | ||||
1559 | assert(getOperand(0) && getOperand(1) && getOperand(2) &&(static_cast<void> (0)) | |||
1560 | "All operands must be non-null!")(static_cast<void> (0)); | |||
1561 | assert(getOperand(0)->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
1562 | "Ptr must have pointer type!")(static_cast<void> (0)); | |||
1563 | assert(cast<PointerType>(getOperand(0)->getType())(static_cast<void> (0)) | |||
1564 | ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&(static_cast<void> (0)) | |||
1565 | "Ptr must be a pointer to Cmp type!")(static_cast<void> (0)); | |||
1566 | assert(cast<PointerType>(getOperand(0)->getType())(static_cast<void> (0)) | |||
1567 | ->isOpaqueOrPointeeTypeMatches(getOperand(2)->getType()) &&(static_cast<void> (0)) | |||
1568 | "Ptr must be a pointer to NewVal type!")(static_cast<void> (0)); | |||
1569 | assert(getOperand(1)->getType() == getOperand(2)->getType() &&(static_cast<void> (0)) | |||
1570 | "Cmp type and NewVal type must be same!")(static_cast<void> (0)); | |||
1571 | } | |||
1572 | ||||
1573 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1574 | Align Alignment, | |||
1575 | AtomicOrdering SuccessOrdering, | |||
1576 | AtomicOrdering FailureOrdering, | |||
1577 | SyncScope::ID SSID, | |||
1578 | Instruction *InsertBefore) | |||
1579 | : Instruction( | |||
1580 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), | |||
1581 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), | |||
1582 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { | |||
1583 | Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); | |||
1584 | } | |||
1585 | ||||
1586 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1587 | Align Alignment, | |||
1588 | AtomicOrdering SuccessOrdering, | |||
1589 | AtomicOrdering FailureOrdering, | |||
1590 | SyncScope::ID SSID, | |||
1591 | BasicBlock *InsertAtEnd) | |||
1592 | : Instruction( | |||
1593 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), | |||
1594 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), | |||
1595 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { | |||
1596 | Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); | |||
1597 | } | |||
1598 | ||||
1599 | //===----------------------------------------------------------------------===// | |||
1600 | // AtomicRMWInst Implementation | |||
1601 | //===----------------------------------------------------------------------===// | |||
1602 | ||||
1603 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, | |||
1604 | Align Alignment, AtomicOrdering Ordering, | |||
1605 | SyncScope::ID SSID) { | |||
1606 | Op<0>() = Ptr; | |||
1607 | Op<1>() = Val; | |||
1608 | setOperation(Operation); | |||
1609 | setOrdering(Ordering); | |||
1610 | setSyncScopeID(SSID); | |||
1611 | setAlignment(Alignment); | |||
1612 | ||||
1613 | assert(getOperand(0) && getOperand(1) &&(static_cast<void> (0)) | |||
1614 | "All operands must be non-null!")(static_cast<void> (0)); | |||
1615 | assert(getOperand(0)->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
1616 | "Ptr must have pointer type!")(static_cast<void> (0)); | |||
1617 | assert(cast<PointerType>(getOperand(0)->getType())(static_cast<void> (0)) | |||
1618 | ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&(static_cast<void> (0)) | |||
1619 | "Ptr must be a pointer to Val type!")(static_cast<void> (0)); | |||
1620 | assert(Ordering != AtomicOrdering::NotAtomic &&(static_cast<void> (0)) | |||
1621 | "AtomicRMW instructions must be atomic!")(static_cast<void> (0)); | |||
1622 | } | |||
1623 | ||||
1624 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
1625 | Align Alignment, AtomicOrdering Ordering, | |||
1626 | SyncScope::ID SSID, Instruction *InsertBefore) | |||
1627 | : Instruction(Val->getType(), AtomicRMW, | |||
1628 | OperandTraits<AtomicRMWInst>::op_begin(this), | |||
1629 | OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) { | |||
1630 | Init(Operation, Ptr, Val, Alignment, Ordering, SSID); | |||
1631 | } | |||
1632 | ||||
1633 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
1634 | Align Alignment, AtomicOrdering Ordering, | |||
1635 | SyncScope::ID SSID, BasicBlock *InsertAtEnd) | |||
1636 | : Instruction(Val->getType(), AtomicRMW, | |||
1637 | OperandTraits<AtomicRMWInst>::op_begin(this), | |||
1638 | OperandTraits<AtomicRMWInst>::operands(this), InsertAtEnd) { | |||
1639 | Init(Operation, Ptr, Val, Alignment, Ordering, SSID); | |||
1640 | } | |||
1641 | ||||
1642 | StringRef AtomicRMWInst::getOperationName(BinOp Op) { | |||
1643 | switch (Op) { | |||
1644 | case AtomicRMWInst::Xchg: | |||
1645 | return "xchg"; | |||
1646 | case AtomicRMWInst::Add: | |||
1647 | return "add"; | |||
1648 | case AtomicRMWInst::Sub: | |||
1649 | return "sub"; | |||
1650 | case AtomicRMWInst::And: | |||
1651 | return "and"; | |||
1652 | case AtomicRMWInst::Nand: | |||
1653 | return "nand"; | |||
1654 | case AtomicRMWInst::Or: | |||
1655 | return "or"; | |||
1656 | case AtomicRMWInst::Xor: | |||
1657 | return "xor"; | |||
1658 | case AtomicRMWInst::Max: | |||
1659 | return "max"; | |||
1660 | case AtomicRMWInst::Min: | |||
1661 | return "min"; | |||
1662 | case AtomicRMWInst::UMax: | |||
1663 | return "umax"; | |||
1664 | case AtomicRMWInst::UMin: | |||
1665 | return "umin"; | |||
1666 | case AtomicRMWInst::FAdd: | |||
1667 | return "fadd"; | |||
1668 | case AtomicRMWInst::FSub: | |||
1669 | return "fsub"; | |||
1670 | case AtomicRMWInst::BAD_BINOP: | |||
1671 | return "<invalid operation>"; | |||
1672 | } | |||
1673 | ||||
1674 | llvm_unreachable("invalid atomicrmw operation")__builtin_unreachable(); | |||
1675 | } | |||
1676 | ||||
1677 | //===----------------------------------------------------------------------===// | |||
1678 | // FenceInst Implementation | |||
1679 | //===----------------------------------------------------------------------===// | |||
1680 | ||||
1681 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
1682 | SyncScope::ID SSID, | |||
1683 | Instruction *InsertBefore) | |||
1684 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { | |||
1685 | setOrdering(Ordering); | |||
1686 | setSyncScopeID(SSID); | |||
1687 | } | |||
1688 | ||||
1689 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
1690 | SyncScope::ID SSID, | |||
1691 | BasicBlock *InsertAtEnd) | |||
1692 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { | |||
1693 | setOrdering(Ordering); | |||
1694 | setSyncScopeID(SSID); | |||
1695 | } | |||
1696 | ||||
1697 | //===----------------------------------------------------------------------===// | |||
1698 | // GetElementPtrInst Implementation | |||
1699 | //===----------------------------------------------------------------------===// | |||
1700 | ||||
1701 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, | |||
1702 | const Twine &Name) { | |||
1703 | assert(getNumOperands() == 1 + IdxList.size() &&(static_cast<void> (0)) | |||
1704 | "NumOperands not initialized?")(static_cast<void> (0)); | |||
1705 | Op<0>() = Ptr; | |||
1706 | llvm::copy(IdxList, op_begin() + 1); | |||
1707 | setName(Name); | |||
1708 | } | |||
1709 | ||||
1710 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) | |||
1711 | : Instruction(GEPI.getType(), GetElementPtr, | |||
1712 | OperandTraits<GetElementPtrInst>::op_end(this) - | |||
1713 | GEPI.getNumOperands(), | |||
1714 | GEPI.getNumOperands()), | |||
1715 | SourceElementType(GEPI.SourceElementType), | |||
1716 | ResultElementType(GEPI.ResultElementType) { | |||
1717 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); | |||
1718 | SubclassOptionalData = GEPI.SubclassOptionalData; | |||
1719 | } | |||
1720 | ||||
1721 | Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) { | |||
1722 | if (auto *Struct = dyn_cast<StructType>(Ty)) { | |||
1723 | if (!Struct->indexValid(Idx)) | |||
1724 | return nullptr; | |||
1725 | return Struct->getTypeAtIndex(Idx); | |||
1726 | } | |||
1727 | if (!Idx->getType()->isIntOrIntVectorTy()) | |||
1728 | return nullptr; | |||
1729 | if (auto *Array = dyn_cast<ArrayType>(Ty)) | |||
1730 | return Array->getElementType(); | |||
1731 | if (auto *Vector = dyn_cast<VectorType>(Ty)) | |||
1732 | return Vector->getElementType(); | |||
1733 | return nullptr; | |||
1734 | } | |||
1735 | ||||
1736 | Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) { | |||
1737 | if (auto *Struct = dyn_cast<StructType>(Ty)) { | |||
1738 | if (Idx >= Struct->getNumElements()) | |||
1739 | return nullptr; | |||
1740 | return Struct->getElementType(Idx); | |||
1741 | } | |||
1742 | if (auto *Array = dyn_cast<ArrayType>(Ty)) | |||
1743 | return Array->getElementType(); | |||
1744 | if (auto *Vector = dyn_cast<VectorType>(Ty)) | |||
1745 | return Vector->getElementType(); | |||
1746 | return nullptr; | |||
1747 | } | |||
1748 | ||||
1749 | template <typename IndexTy> | |||
1750 | static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) { | |||
1751 | if (IdxList.empty()) | |||
1752 | return Ty; | |||
1753 | for (IndexTy V : IdxList.slice(1)) { | |||
1754 | Ty = GetElementPtrInst::getTypeAtIndex(Ty, V); | |||
1755 | if (!Ty) | |||
1756 | return Ty; | |||
1757 | } | |||
1758 | return Ty; | |||
1759 | } | |||
1760 | ||||
1761 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { | |||
1762 | return getIndexedTypeInternal(Ty, IdxList); | |||
1763 | } | |||
1764 | ||||
1765 | Type *GetElementPtrInst::getIndexedType(Type *Ty, | |||
1766 | ArrayRef<Constant *> IdxList) { | |||
1767 | return getIndexedTypeInternal(Ty, IdxList); | |||
1768 | } | |||
1769 | ||||
1770 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { | |||
1771 | return getIndexedTypeInternal(Ty, IdxList); | |||
1772 | } | |||
1773 | ||||
1774 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are | |||
1775 | /// zeros. If so, the result pointer and the first operand have the same | |||
1776 | /// value, just potentially different types. | |||
1777 | bool GetElementPtrInst::hasAllZeroIndices() const { | |||
1778 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { | |||
1779 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { | |||
1780 | if (!CI->isZero()) return false; | |||
1781 | } else { | |||
1782 | return false; | |||
1783 | } | |||
1784 | } | |||
1785 | return true; | |||
1786 | } | |||
1787 | ||||
1788 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are | |||
1789 | /// constant integers. If so, the result pointer and the first operand have | |||
1790 | /// a constant offset between them. | |||
1791 | bool GetElementPtrInst::hasAllConstantIndices() const { | |||
1792 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { | |||
1793 | if (!isa<ConstantInt>(getOperand(i))) | |||
1794 | return false; | |||
1795 | } | |||
1796 | return true; | |||
1797 | } | |||
1798 | ||||
1799 | void GetElementPtrInst::setIsInBounds(bool B) { | |||
1800 | cast<GEPOperator>(this)->setIsInBounds(B); | |||
1801 | } | |||
1802 | ||||
1803 | bool GetElementPtrInst::isInBounds() const { | |||
1804 | return cast<GEPOperator>(this)->isInBounds(); | |||
1805 | } | |||
1806 | ||||
1807 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, | |||
1808 | APInt &Offset) const { | |||
1809 | // Delegate to the generic GEPOperator implementation. | |||
1810 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); | |||
1811 | } | |||
1812 | ||||
1813 | bool GetElementPtrInst::collectOffset( | |||
1814 | const DataLayout &DL, unsigned BitWidth, | |||
1815 | MapVector<Value *, APInt> &VariableOffsets, | |||
1816 | APInt &ConstantOffset) const { | |||
1817 | // Delegate to the generic GEPOperator implementation. | |||
1818 | return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets, | |||
1819 | ConstantOffset); | |||
1820 | } | |||
1821 | ||||
1822 | //===----------------------------------------------------------------------===// | |||
1823 | // ExtractElementInst Implementation | |||
1824 | //===----------------------------------------------------------------------===// | |||
1825 | ||||
1826 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, | |||
1827 | const Twine &Name, | |||
1828 | Instruction *InsertBef) | |||
1829 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), | |||
1830 | ExtractElement, | |||
1831 | OperandTraits<ExtractElementInst>::op_begin(this), | |||
1832 | 2, InsertBef) { | |||
1833 | assert(isValidOperands(Val, Index) &&(static_cast<void> (0)) | |||
1834 | "Invalid extractelement instruction operands!")(static_cast<void> (0)); | |||
1835 | Op<0>() = Val; | |||
1836 | Op<1>() = Index; | |||
1837 | setName(Name); | |||
1838 | } | |||
1839 | ||||
1840 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, | |||
1841 | const Twine &Name, | |||
1842 | BasicBlock *InsertAE) | |||
1843 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), | |||
1844 | ExtractElement, | |||
1845 | OperandTraits<ExtractElementInst>::op_begin(this), | |||
1846 | 2, InsertAE) { | |||
1847 | assert(isValidOperands(Val, Index) &&(static_cast<void> (0)) | |||
1848 | "Invalid extractelement instruction operands!")(static_cast<void> (0)); | |||
1849 | ||||
1850 | Op<0>() = Val; | |||
1851 | Op<1>() = Index; | |||
1852 | setName(Name); | |||
1853 | } | |||
1854 | ||||
1855 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { | |||
1856 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) | |||
1857 | return false; | |||
1858 | return true; | |||
1859 | } | |||
1860 | ||||
1861 | //===----------------------------------------------------------------------===// | |||
1862 | // InsertElementInst Implementation | |||
1863 | //===----------------------------------------------------------------------===// | |||
1864 | ||||
1865 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, | |||
1866 | const Twine &Name, | |||
1867 | Instruction *InsertBef) | |||
1868 | : Instruction(Vec->getType(), InsertElement, | |||
1869 | OperandTraits<InsertElementInst>::op_begin(this), | |||
1870 | 3, InsertBef) { | |||
1871 | assert(isValidOperands(Vec, Elt, Index) &&(static_cast<void> (0)) | |||
1872 | "Invalid insertelement instruction operands!")(static_cast<void> (0)); | |||
1873 | Op<0>() = Vec; | |||
1874 | Op<1>() = Elt; | |||
1875 | Op<2>() = Index; | |||
1876 | setName(Name); | |||
1877 | } | |||
1878 | ||||
1879 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, | |||
1880 | const Twine &Name, | |||
1881 | BasicBlock *InsertAE) | |||
1882 | : Instruction(Vec->getType(), InsertElement, | |||
1883 | OperandTraits<InsertElementInst>::op_begin(this), | |||
1884 | 3, InsertAE) { | |||
1885 | assert(isValidOperands(Vec, Elt, Index) &&(static_cast<void> (0)) | |||
1886 | "Invalid insertelement instruction operands!")(static_cast<void> (0)); | |||
1887 | ||||
1888 | Op<0>() = Vec; | |||
1889 | Op<1>() = Elt; | |||
1890 | Op<2>() = Index; | |||
1891 | setName(Name); | |||
1892 | } | |||
1893 | ||||
1894 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, | |||
1895 | const Value *Index) { | |||
1896 | if (!Vec->getType()->isVectorTy()) | |||
1897 | return false; // First operand of insertelement must be vector type. | |||
1898 | ||||
1899 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) | |||
1900 | return false;// Second operand of insertelement must be vector element type. | |||
1901 | ||||
1902 | if (!Index->getType()->isIntegerTy()) | |||
1903 | return false; // Third operand of insertelement must be i32. | |||
1904 | return true; | |||
1905 | } | |||
1906 | ||||
1907 | //===----------------------------------------------------------------------===// | |||
1908 | // ShuffleVectorInst Implementation | |||
1909 | //===----------------------------------------------------------------------===// | |||
1910 | ||||
1911 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1912 | const Twine &Name, | |||
1913 | Instruction *InsertBefore) | |||
1914 | : Instruction( | |||
1915 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1916 | cast<VectorType>(Mask->getType())->getElementCount()), | |||
1917 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1918 | OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { | |||
1919 | assert(isValidOperands(V1, V2, Mask) &&(static_cast<void> (0)) | |||
1920 | "Invalid shuffle vector instruction operands!")(static_cast<void> (0)); | |||
1921 | ||||
1922 | Op<0>() = V1; | |||
1923 | Op<1>() = V2; | |||
1924 | SmallVector<int, 16> MaskArr; | |||
1925 | getShuffleMask(cast<Constant>(Mask), MaskArr); | |||
1926 | setShuffleMask(MaskArr); | |||
1927 | setName(Name); | |||
1928 | } | |||
1929 | ||||
1930 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1931 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1932 | : Instruction( | |||
1933 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1934 | cast<VectorType>(Mask->getType())->getElementCount()), | |||
1935 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1936 | OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { | |||
1937 | assert(isValidOperands(V1, V2, Mask) &&(static_cast<void> (0)) | |||
1938 | "Invalid shuffle vector instruction operands!")(static_cast<void> (0)); | |||
1939 | ||||
1940 | Op<0>() = V1; | |||
1941 | Op<1>() = V2; | |||
1942 | SmallVector<int, 16> MaskArr; | |||
1943 | getShuffleMask(cast<Constant>(Mask), MaskArr); | |||
1944 | setShuffleMask(MaskArr); | |||
1945 | setName(Name); | |||
1946 | } | |||
1947 | ||||
1948 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
1949 | const Twine &Name, | |||
1950 | Instruction *InsertBefore) | |||
1951 | : Instruction( | |||
1952 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1953 | Mask.size(), isa<ScalableVectorType>(V1->getType())), | |||
1954 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1955 | OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { | |||
1956 | assert(isValidOperands(V1, V2, Mask) &&(static_cast<void> (0)) | |||
1957 | "Invalid shuffle vector instruction operands!")(static_cast<void> (0)); | |||
1958 | Op<0>() = V1; | |||
1959 | Op<1>() = V2; | |||
1960 | setShuffleMask(Mask); | |||
1961 | setName(Name); | |||
1962 | } | |||
1963 | ||||
1964 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
1965 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1966 | : Instruction( | |||
1967 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1968 | Mask.size(), isa<ScalableVectorType>(V1->getType())), | |||
1969 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1970 | OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { | |||
1971 | assert(isValidOperands(V1, V2, Mask) &&(static_cast<void> (0)) | |||
1972 | "Invalid shuffle vector instruction operands!")(static_cast<void> (0)); | |||
1973 | ||||
1974 | Op<0>() = V1; | |||
1975 | Op<1>() = V2; | |||
1976 | setShuffleMask(Mask); | |||
1977 | setName(Name); | |||
1978 | } | |||
1979 | ||||
1980 | void ShuffleVectorInst::commute() { | |||
1981 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
1982 | int NumMaskElts = ShuffleMask.size(); | |||
1983 | SmallVector<int, 16> NewMask(NumMaskElts); | |||
1984 | for (int i = 0; i != NumMaskElts; ++i) { | |||
1985 | int MaskElt = getMaskValue(i); | |||
1986 | if (MaskElt == UndefMaskElem) { | |||
1987 | NewMask[i] = UndefMaskElem; | |||
1988 | continue; | |||
1989 | } | |||
1990 | assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask")(static_cast<void> (0)); | |||
1991 | MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; | |||
1992 | NewMask[i] = MaskElt; | |||
1993 | } | |||
1994 | setShuffleMask(NewMask); | |||
1995 | Op<0>().swap(Op<1>()); | |||
1996 | } | |||
1997 | ||||
1998 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, | |||
1999 | ArrayRef<int> Mask) { | |||
2000 | // V1 and V2 must be vectors of the same type. | |||
2001 | if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) | |||
2002 | return false; | |||
2003 | ||||
2004 | // Make sure the mask elements make sense. | |||
2005 | int V1Size = | |||
2006 | cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue(); | |||
2007 | for (int Elem : Mask) | |||
2008 | if (Elem != UndefMaskElem && Elem >= V1Size * 2) | |||
2009 | return false; | |||
2010 | ||||
2011 | if (isa<ScalableVectorType>(V1->getType())) | |||
2012 | if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !is_splat(Mask)) | |||
2013 | return false; | |||
2014 | ||||
2015 | return true; | |||
2016 | } | |||
2017 | ||||
2018 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, | |||
2019 | const Value *Mask) { | |||
2020 | // V1 and V2 must be vectors of the same type. | |||
2021 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) | |||
2022 | return false; | |||
2023 | ||||
2024 | // Mask must be vector of i32, and must be the same kind of vector as the | |||
2025 | // input vectors | |||
2026 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); | |||
2027 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) || | |||
2028 | isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType())) | |||
2029 | return false; | |||
2030 | ||||
2031 | // Check to see if Mask is valid. | |||
2032 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) | |||
2033 | return true; | |||
2034 | ||||
2035 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { | |||
2036 | unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); | |||
2037 | for (Value *Op : MV->operands()) { | |||
2038 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { | |||
2039 | if (CI->uge(V1Size*2)) | |||
2040 | return false; | |||
2041 | } else if (!isa<UndefValue>(Op)) { | |||
2042 | return false; | |||
2043 | } | |||
2044 | } | |||
2045 | return true; | |||
2046 | } | |||
2047 | ||||
2048 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { | |||
2049 | unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); | |||
2050 | for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements(); | |||
2051 | i != e; ++i) | |||
2052 | if (CDS->getElementAsInteger(i) >= V1Size*2) | |||
2053 | return false; | |||
2054 | return true; | |||
2055 | } | |||
2056 | ||||
2057 | return false; | |||
2058 | } | |||
2059 | ||||
2060 | void ShuffleVectorInst::getShuffleMask(const Constant *Mask, | |||
2061 | SmallVectorImpl<int> &Result) { | |||
2062 | ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount(); | |||
2063 | ||||
2064 | if (isa<ConstantAggregateZero>(Mask)) { | |||
2065 | Result.resize(EC.getKnownMinValue(), 0); | |||
2066 | return; | |||
2067 | } | |||
2068 | ||||
2069 | Result.reserve(EC.getKnownMinValue()); | |||
2070 | ||||
2071 | if (EC.isScalable()) { | |||
2072 | assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) &&(static_cast<void> (0)) | |||
2073 | "Scalable vector shuffle mask must be undef or zeroinitializer")(static_cast<void> (0)); | |||
2074 | int MaskVal = isa<UndefValue>(Mask) ? -1 : 0; | |||
2075 | for (unsigned I = 0; I < EC.getKnownMinValue(); ++I) | |||
2076 | Result.emplace_back(MaskVal); | |||
2077 | return; | |||
2078 | } | |||
2079 | ||||
2080 | unsigned NumElts = EC.getKnownMinValue(); | |||
2081 | ||||
2082 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { | |||
2083 | for (unsigned i = 0; i != NumElts; ++i) | |||
2084 | Result.push_back(CDS->getElementAsInteger(i)); | |||
2085 | return; | |||
2086 | } | |||
2087 | for (unsigned i = 0; i != NumElts; ++i) { | |||
2088 | Constant *C = Mask->getAggregateElement(i); | |||
2089 | Result.push_back(isa<UndefValue>(C) ? -1 : | |||
2090 | cast<ConstantInt>(C)->getZExtValue()); | |||
2091 | } | |||
2092 | } | |||
2093 | ||||
2094 | void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) { | |||
2095 | ShuffleMask.assign(Mask.begin(), Mask.end()); | |||
2096 | ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType()); | |||
2097 | } | |||
2098 | ||||
2099 | Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask, | |||
2100 | Type *ResultTy) { | |||
2101 | Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext()); | |||
2102 | if (isa<ScalableVectorType>(ResultTy)) { | |||
2103 | assert(is_splat(Mask) && "Unexpected shuffle")(static_cast<void> (0)); | |||
2104 | Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true); | |||
2105 | if (Mask[0] == 0) | |||
2106 | return Constant::getNullValue(VecTy); | |||
2107 | return UndefValue::get(VecTy); | |||
2108 | } | |||
2109 | SmallVector<Constant *, 16> MaskConst; | |||
2110 | for (int Elem : Mask) { | |||
2111 | if (Elem == UndefMaskElem) | |||
2112 | MaskConst.push_back(UndefValue::get(Int32Ty)); | |||
2113 | else | |||
2114 | MaskConst.push_back(ConstantInt::get(Int32Ty, Elem)); | |||
2115 | } | |||
2116 | return ConstantVector::get(MaskConst); | |||
2117 | } | |||
2118 | ||||
2119 | static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { | |||
2120 | assert(!Mask.empty() && "Shuffle mask must contain elements")(static_cast<void> (0)); | |||
2121 | bool UsesLHS = false; | |||
2122 | bool UsesRHS = false; | |||
2123 | for (int I : Mask) { | |||
2124 | if (I == -1) | |||
2125 | continue; | |||
2126 | assert(I >= 0 && I < (NumOpElts * 2) &&(static_cast<void> (0)) | |||
2127 | "Out-of-bounds shuffle mask element")(static_cast<void> (0)); | |||
2128 | UsesLHS |= (I < NumOpElts); | |||
2129 | UsesRHS |= (I >= NumOpElts); | |||
2130 | if (UsesLHS && UsesRHS) | |||
2131 | return false; | |||
2132 | } | |||
2133 | // Allow for degenerate case: completely undef mask means neither source is used. | |||
2134 | return UsesLHS || UsesRHS; | |||
2135 | } | |||
2136 | ||||
2137 | bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { | |||
2138 | // We don't have vector operand size information, so assume operands are the | |||
2139 | // same size as the mask. | |||
2140 | return isSingleSourceMaskImpl(Mask, Mask.size()); | |||
2141 | } | |||
2142 | ||||
2143 | static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { | |||
2144 | if (!isSingleSourceMaskImpl(Mask, NumOpElts)) | |||
2145 | return false; | |||
2146 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { | |||
2147 | if (Mask[i] == -1) | |||
2148 | continue; | |||
2149 | if (Mask[i] != i && Mask[i] != (NumOpElts + i)) | |||
2150 | return false; | |||
2151 | } | |||
2152 | return true; | |||
2153 | } | |||
2154 | ||||
2155 | bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { | |||
2156 | // We don't have vector operand size information, so assume operands are the | |||
2157 | // same size as the mask. | |||
2158 | return isIdentityMaskImpl(Mask, Mask.size()); | |||
2159 | } | |||
2160 | ||||
2161 | bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { | |||
2162 | if (!isSingleSourceMask(Mask)) | |||
2163 | return false; | |||
2164 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2165 | if (Mask[i] == -1) | |||
2166 | continue; | |||
2167 | if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) | |||
2168 | return false; | |||
2169 | } | |||
2170 | return true; | |||
2171 | } | |||
2172 | ||||
2173 | bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { | |||
2174 | if (!isSingleSourceMask(Mask)) | |||
2175 | return false; | |||
2176 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2177 | if (Mask[i] == -1) | |||
2178 | continue; | |||
2179 | if (Mask[i] != 0 && Mask[i] != NumElts) | |||
2180 | return false; | |||
2181 | } | |||
2182 | return true; | |||
2183 | } | |||
2184 | ||||
2185 | bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { | |||
2186 | // Select is differentiated from identity. It requires using both sources. | |||
2187 | if (isSingleSourceMask(Mask)) | |||
2188 | return false; | |||
2189 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2190 | if (Mask[i] == -1) | |||
2191 | continue; | |||
2192 | if (Mask[i] != i && Mask[i] != (NumElts + i)) | |||
2193 | return false; | |||
2194 | } | |||
2195 | return true; | |||
2196 | } | |||
2197 | ||||
2198 | bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { | |||
2199 | // Example masks that will return true: | |||
2200 | // v1 = <a, b, c, d> | |||
2201 | // v2 = <e, f, g, h> | |||
2202 | // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> | |||
2203 | // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> | |||
2204 | ||||
2205 | // 1. The number of elements in the mask must be a power-of-2 and at least 2. | |||
2206 | int NumElts = Mask.size(); | |||
2207 | if (NumElts < 2 || !isPowerOf2_32(NumElts)) | |||
2208 | return false; | |||
2209 | ||||
2210 | // 2. The first element of the mask must be either a 0 or a 1. | |||
2211 | if (Mask[0] != 0 && Mask[0] != 1) | |||
2212 | return false; | |||
2213 | ||||
2214 | // 3. The difference between the first 2 elements must be equal to the | |||
2215 | // number of elements in the mask. | |||
2216 | if ((Mask[1] - Mask[0]) != NumElts) | |||
2217 | return false; | |||
2218 | ||||
2219 | // 4. The difference between consecutive even-numbered and odd-numbered | |||
2220 | // elements must be equal to 2. | |||
2221 | for (int i = 2; i < NumElts; ++i) { | |||
2222 | int MaskEltVal = Mask[i]; | |||
2223 | if (MaskEltVal == -1) | |||
2224 | return false; | |||
2225 | int MaskEltPrevVal = Mask[i - 2]; | |||
2226 | if (MaskEltVal - MaskEltPrevVal != 2) | |||
2227 | return false; | |||
2228 | } | |||
2229 | return true; | |||
2230 | } | |||
2231 | ||||
2232 | bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, | |||
2233 | int NumSrcElts, int &Index) { | |||
2234 | // Must extract from a single source. | |||
2235 | if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) | |||
2236 | return false; | |||
2237 | ||||
2238 | // Must be smaller (else this is an Identity shuffle). | |||
2239 | if (NumSrcElts <= (int)Mask.size()) | |||
2240 | return false; | |||
2241 | ||||
2242 | // Find start of extraction, accounting that we may start with an UNDEF. | |||
2243 | int SubIndex = -1; | |||
2244 | for (int i = 0, e = Mask.size(); i != e; ++i) { | |||
2245 | int M = Mask[i]; | |||
2246 | if (M < 0) | |||
2247 | continue; | |||
2248 | int Offset = (M % NumSrcElts) - i; | |||
2249 | if (0 <= SubIndex && SubIndex != Offset) | |||
2250 | return false; | |||
2251 | SubIndex = Offset; | |||
2252 | } | |||
2253 | ||||
2254 | if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) { | |||
2255 | Index = SubIndex; | |||
2256 | return true; | |||
2257 | } | |||
2258 | return false; | |||
2259 | } | |||
2260 | ||||
2261 | bool ShuffleVectorInst::isInsertSubvectorMask(ArrayRef<int> Mask, | |||
2262 | int NumSrcElts, int &NumSubElts, | |||
2263 | int &Index) { | |||
2264 | int NumMaskElts = Mask.size(); | |||
2265 | ||||
2266 | // Don't try to match if we're shuffling to a smaller size. | |||
2267 | if (NumMaskElts < NumSrcElts) | |||
2268 | return false; | |||
2269 | ||||
2270 | // TODO: We don't recognize self-insertion/widening. | |||
2271 | if (isSingleSourceMaskImpl(Mask, NumSrcElts)) | |||
2272 | return false; | |||
2273 | ||||
2274 | // Determine which mask elements are attributed to which source. | |||
2275 | APInt UndefElts = APInt::getNullValue(NumMaskElts); | |||
2276 | APInt Src0Elts = APInt::getNullValue(NumMaskElts); | |||
2277 | APInt Src1Elts = APInt::getNullValue(NumMaskElts); | |||
2278 | bool Src0Identity = true; | |||
2279 | bool Src1Identity = true; | |||
2280 | ||||
2281 | for (int i = 0; i != NumMaskElts; ++i) { | |||
2282 | int M = Mask[i]; | |||
2283 | if (M < 0) { | |||
2284 | UndefElts.setBit(i); | |||
2285 | continue; | |||
2286 | } | |||
2287 | if (M < NumSrcElts) { | |||
2288 | Src0Elts.setBit(i); | |||
2289 | Src0Identity &= (M == i); | |||
2290 | continue; | |||
2291 | } | |||
2292 | Src1Elts.setBit(i); | |||
2293 | Src1Identity &= (M == (i + NumSrcElts)); | |||
2294 | continue; | |||
2295 | } | |||
2296 | assert((Src0Elts | Src1Elts | UndefElts).isAllOnesValue() &&(static_cast<void> (0)) | |||
2297 | "unknown shuffle elements")(static_cast<void> (0)); | |||
2298 | assert(!Src0Elts.isNullValue() && !Src1Elts.isNullValue() &&(static_cast<void> (0)) | |||
2299 | "2-source shuffle not found")(static_cast<void> (0)); | |||
2300 | ||||
2301 | // Determine lo/hi span ranges. | |||
2302 | // TODO: How should we handle undefs at the start of subvector insertions? | |||
2303 | int Src0Lo = Src0Elts.countTrailingZeros(); | |||
2304 | int Src1Lo = Src1Elts.countTrailingZeros(); | |||
2305 | int Src0Hi = NumMaskElts - Src0Elts.countLeadingZeros(); | |||
2306 | int Src1Hi = NumMaskElts - Src1Elts.countLeadingZeros(); | |||
2307 | ||||
2308 | // If src0 is in place, see if the src1 elements is inplace within its own | |||
2309 | // span. | |||
2310 | if (Src0Identity) { | |||
2311 | int NumSub1Elts = Src1Hi - Src1Lo; | |||
2312 | ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts); | |||
2313 | if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) { | |||
2314 | NumSubElts = NumSub1Elts; | |||
2315 | Index = Src1Lo; | |||
2316 | return true; | |||
2317 | } | |||
2318 | } | |||
2319 | ||||
2320 | // If src1 is in place, see if the src0 elements is inplace within its own | |||
2321 | // span. | |||
2322 | if (Src1Identity) { | |||
2323 | int NumSub0Elts = Src0Hi - Src0Lo; | |||
2324 | ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts); | |||
2325 | if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) { | |||
2326 | NumSubElts = NumSub0Elts; | |||
2327 | Index = Src0Lo; | |||
2328 | return true; | |||
2329 | } | |||
2330 | } | |||
2331 | ||||
2332 | return false; | |||
2333 | } | |||
2334 | ||||
2335 | bool ShuffleVectorInst::isIdentityWithPadding() const { | |||
2336 | if (isa<UndefValue>(Op<2>())) | |||
2337 | return false; | |||
2338 | ||||
2339 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2340 | // vector for this case. | |||
2341 | if (isa<ScalableVectorType>(getType())) | |||
2342 | return false; | |||
2343 | ||||
2344 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2345 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2346 | if (NumMaskElts <= NumOpElts) | |||
2347 | return false; | |||
2348 | ||||
2349 | // The first part of the mask must choose elements from exactly 1 source op. | |||
2350 | ArrayRef<int> Mask = getShuffleMask(); | |||
2351 | if (!isIdentityMaskImpl(Mask, NumOpElts)) | |||
2352 | return false; | |||
2353 | ||||
2354 | // All extending must be with undef elements. | |||
2355 | for (int i = NumOpElts; i < NumMaskElts; ++i) | |||
2356 | if (Mask[i] != -1) | |||
2357 | return false; | |||
2358 | ||||
2359 | return true; | |||
2360 | } | |||
2361 | ||||
2362 | bool ShuffleVectorInst::isIdentityWithExtract() const { | |||
2363 | if (isa<UndefValue>(Op<2>())) | |||
2364 | return false; | |||
2365 | ||||
2366 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2367 | // vector for this case. | |||
2368 | if (isa<ScalableVectorType>(getType())) | |||
2369 | return false; | |||
2370 | ||||
2371 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2372 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2373 | if (NumMaskElts >= NumOpElts) | |||
2374 | return false; | |||
2375 | ||||
2376 | return isIdentityMaskImpl(getShuffleMask(), NumOpElts); | |||
2377 | } | |||
2378 | ||||
2379 | bool ShuffleVectorInst::isConcat() const { | |||
2380 | // Vector concatenation is differentiated from identity with padding. | |||
2381 | if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) || | |||
2382 | isa<UndefValue>(Op<2>())) | |||
2383 | return false; | |||
2384 | ||||
2385 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2386 | // vector for this case. | |||
2387 | if (isa<ScalableVectorType>(getType())) | |||
2388 | return false; | |||
2389 | ||||
2390 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2391 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2392 | if (NumMaskElts != NumOpElts * 2) | |||
2393 | return false; | |||
2394 | ||||
2395 | // Use the mask length rather than the operands' vector lengths here. We | |||
2396 | // already know that the shuffle returns a vector twice as long as the inputs, | |||
2397 | // and neither of the inputs are undef vectors. If the mask picks consecutive | |||
2398 | // elements from both inputs, then this is a concatenation of the inputs. | |||
2399 | return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); | |||
2400 | } | |||
2401 | ||||
2402 | //===----------------------------------------------------------------------===// | |||
2403 | // InsertValueInst Class | |||
2404 | //===----------------------------------------------------------------------===// | |||
2405 | ||||
2406 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, | |||
2407 | const Twine &Name) { | |||
2408 | assert(getNumOperands() == 2 && "NumOperands not initialized?")(static_cast<void> (0)); | |||
2409 | ||||
2410 | // There's no fundamental reason why we require at least one index | |||
2411 | // (other than weirdness with &*IdxBegin being invalid; see | |||
2412 | // getelementptr's init routine for example). But there's no | |||
2413 | // present need to support it. | |||
2414 | assert(!Idxs.empty() && "InsertValueInst must have at least one index")(static_cast<void> (0)); | |||
2415 | ||||
2416 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==(static_cast<void> (0)) | |||
2417 | Val->getType() && "Inserted value must match indexed type!")(static_cast<void> (0)); | |||
2418 | Op<0>() = Agg; | |||
2419 | Op<1>() = Val; | |||
2420 | ||||
2421 | Indices.append(Idxs.begin(), Idxs.end()); | |||
2422 | setName(Name); | |||
2423 | } | |||
2424 | ||||
2425 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) | |||
2426 | : Instruction(IVI.getType(), InsertValue, | |||
2427 | OperandTraits<InsertValueInst>::op_begin(this), 2), | |||
2428 | Indices(IVI.Indices) { | |||
2429 | Op<0>() = IVI.getOperand(0); | |||
2430 | Op<1>() = IVI.getOperand(1); | |||
2431 | SubclassOptionalData = IVI.SubclassOptionalData; | |||
2432 | } | |||
2433 | ||||
2434 | //===----------------------------------------------------------------------===// | |||
2435 | // ExtractValueInst Class | |||
2436 | //===----------------------------------------------------------------------===// | |||
2437 | ||||
2438 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { | |||
2439 | assert(getNumOperands() == 1 && "NumOperands not initialized?")(static_cast<void> (0)); | |||
2440 | ||||
2441 | // There's no fundamental reason why we require at least one index. | |||
2442 | // But there's no present need to support it. | |||
2443 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index")(static_cast<void> (0)); | |||
2444 | ||||
2445 | Indices.append(Idxs.begin(), Idxs.end()); | |||
2446 | setName(Name); | |||
2447 | } | |||
2448 | ||||
2449 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) | |||
2450 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), | |||
2451 | Indices(EVI.Indices) { | |||
2452 | SubclassOptionalData = EVI.SubclassOptionalData; | |||
2453 | } | |||
2454 | ||||
2455 | // getIndexedType - Returns the type of the element that would be extracted | |||
2456 | // with an extractvalue instruction with the specified parameters. | |||
2457 | // | |||
2458 | // A null type is returned if the indices are invalid for the specified | |||
2459 | // pointer type. | |||
2460 | // | |||
2461 | Type *ExtractValueInst::getIndexedType(Type *Agg, | |||
2462 | ArrayRef<unsigned> Idxs) { | |||
2463 | for (unsigned Index : Idxs) { | |||
2464 | // We can't use CompositeType::indexValid(Index) here. | |||
2465 | // indexValid() always returns true for arrays because getelementptr allows | |||
2466 | // out-of-bounds indices. Since we don't allow those for extractvalue and | |||
2467 | // insertvalue we need to check array indexing manually. | |||
2468 | // Since the only other types we can index into are struct types it's just | |||
2469 | // as easy to check those manually as well. | |||
2470 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { | |||
2471 | if (Index >= AT->getNumElements()) | |||
2472 | return nullptr; | |||
2473 | Agg = AT->getElementType(); | |||
2474 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { | |||
2475 | if (Index >= ST->getNumElements()) | |||
2476 | return nullptr; | |||
2477 | Agg = ST->getElementType(Index); | |||
2478 | } else { | |||
2479 | // Not a valid type to index into. | |||
2480 | return nullptr; | |||
2481 | } | |||
2482 | } | |||
2483 | return const_cast<Type*>(Agg); | |||
2484 | } | |||
2485 | ||||
2486 | //===----------------------------------------------------------------------===// | |||
2487 | // UnaryOperator Class | |||
2488 | //===----------------------------------------------------------------------===// | |||
2489 | ||||
2490 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, | |||
2491 | Type *Ty, const Twine &Name, | |||
2492 | Instruction *InsertBefore) | |||
2493 | : UnaryInstruction(Ty, iType, S, InsertBefore) { | |||
2494 | Op<0>() = S; | |||
2495 | setName(Name); | |||
2496 | AssertOK(); | |||
2497 | } | |||
2498 | ||||
2499 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, | |||
2500 | Type *Ty, const Twine &Name, | |||
2501 | BasicBlock *InsertAtEnd) | |||
2502 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { | |||
2503 | Op<0>() = S; | |||
2504 | setName(Name); | |||
2505 | AssertOK(); | |||
2506 | } | |||
2507 | ||||
2508 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, | |||
2509 | const Twine &Name, | |||
2510 | Instruction *InsertBefore) { | |||
2511 | return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); | |||
2512 | } | |||
2513 | ||||
2514 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, | |||
2515 | const Twine &Name, | |||
2516 | BasicBlock *InsertAtEnd) { | |||
2517 | UnaryOperator *Res = Create(Op, S, Name); | |||
2518 | InsertAtEnd->getInstList().push_back(Res); | |||
2519 | return Res; | |||
2520 | } | |||
2521 | ||||
2522 | void UnaryOperator::AssertOK() { | |||
2523 | Value *LHS = getOperand(0); | |||
2524 | (void)LHS; // Silence warnings. | |||
2525 | #ifndef NDEBUG1 | |||
2526 | switch (getOpcode()) { | |||
2527 | case FNeg: | |||
2528 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2529 | "Unary operation should return same type as operand!")(static_cast<void> (0)); | |||
2530 | assert(getType()->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
2531 | "Tried to create a floating-point operation on a "(static_cast<void> (0)) | |||
2532 | "non-floating-point type!")(static_cast<void> (0)); | |||
2533 | break; | |||
2534 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
2535 | } | |||
2536 | #endif | |||
2537 | } | |||
2538 | ||||
2539 | //===----------------------------------------------------------------------===// | |||
2540 | // BinaryOperator Class | |||
2541 | //===----------------------------------------------------------------------===// | |||
2542 | ||||
2543 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, | |||
2544 | Type *Ty, const Twine &Name, | |||
2545 | Instruction *InsertBefore) | |||
2546 | : Instruction(Ty, iType, | |||
2547 | OperandTraits<BinaryOperator>::op_begin(this), | |||
2548 | OperandTraits<BinaryOperator>::operands(this), | |||
2549 | InsertBefore) { | |||
2550 | Op<0>() = S1; | |||
2551 | Op<1>() = S2; | |||
2552 | setName(Name); | |||
2553 | AssertOK(); | |||
2554 | } | |||
2555 | ||||
2556 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, | |||
2557 | Type *Ty, const Twine &Name, | |||
2558 | BasicBlock *InsertAtEnd) | |||
2559 | : Instruction(Ty, iType, | |||
2560 | OperandTraits<BinaryOperator>::op_begin(this), | |||
2561 | OperandTraits<BinaryOperator>::operands(this), | |||
2562 | InsertAtEnd) { | |||
2563 | Op<0>() = S1; | |||
2564 | Op<1>() = S2; | |||
2565 | setName(Name); | |||
2566 | AssertOK(); | |||
2567 | } | |||
2568 | ||||
2569 | void BinaryOperator::AssertOK() { | |||
2570 | Value *LHS = getOperand(0), *RHS = getOperand(1); | |||
2571 | (void)LHS; (void)RHS; // Silence warnings. | |||
2572 | assert(LHS->getType() == RHS->getType() &&(static_cast<void> (0)) | |||
2573 | "Binary operator operand types must match!")(static_cast<void> (0)); | |||
2574 | #ifndef NDEBUG1 | |||
2575 | switch (getOpcode()) { | |||
2576 | case Add: case Sub: | |||
2577 | case Mul: | |||
2578 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2579 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2580 | assert(getType()->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
2581 | "Tried to create an integer operation on a non-integer type!")(static_cast<void> (0)); | |||
2582 | break; | |||
2583 | case FAdd: case FSub: | |||
2584 | case FMul: | |||
2585 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2586 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2587 | assert(getType()->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
2588 | "Tried to create a floating-point operation on a "(static_cast<void> (0)) | |||
2589 | "non-floating-point type!")(static_cast<void> (0)); | |||
2590 | break; | |||
2591 | case UDiv: | |||
2592 | case SDiv: | |||
2593 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2594 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2595 | assert(getType()->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
2596 | "Incorrect operand type (not integer) for S/UDIV")(static_cast<void> (0)); | |||
2597 | break; | |||
2598 | case FDiv: | |||
2599 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2600 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2601 | assert(getType()->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
2602 | "Incorrect operand type (not floating point) for FDIV")(static_cast<void> (0)); | |||
2603 | break; | |||
2604 | case URem: | |||
2605 | case SRem: | |||
2606 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2607 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2608 | assert(getType()->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
2609 | "Incorrect operand type (not integer) for S/UREM")(static_cast<void> (0)); | |||
2610 | break; | |||
2611 | case FRem: | |||
2612 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2613 | "Arithmetic operation should return same type as operands!")(static_cast<void> (0)); | |||
2614 | assert(getType()->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
2615 | "Incorrect operand type (not floating point) for FREM")(static_cast<void> (0)); | |||
2616 | break; | |||
2617 | case Shl: | |||
2618 | case LShr: | |||
2619 | case AShr: | |||
2620 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2621 | "Shift operation should return same type as operands!")(static_cast<void> (0)); | |||
2622 | assert(getType()->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
2623 | "Tried to create a shift operation on a non-integral type!")(static_cast<void> (0)); | |||
2624 | break; | |||
2625 | case And: case Or: | |||
2626 | case Xor: | |||
2627 | assert(getType() == LHS->getType() &&(static_cast<void> (0)) | |||
2628 | "Logical operation should return same type as operands!")(static_cast<void> (0)); | |||
2629 | assert(getType()->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
2630 | "Tried to create a logical operation on a non-integral type!")(static_cast<void> (0)); | |||
2631 | break; | |||
2632 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
2633 | } | |||
2634 | #endif | |||
2635 | } | |||
2636 | ||||
2637 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, | |||
2638 | const Twine &Name, | |||
2639 | Instruction *InsertBefore) { | |||
2640 | assert(S1->getType() == S2->getType() &&(static_cast<void> (0)) | |||
2641 | "Cannot create binary operator with two operands of differing type!")(static_cast<void> (0)); | |||
2642 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); | |||
2643 | } | |||
2644 | ||||
2645 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, | |||
2646 | const Twine &Name, | |||
2647 | BasicBlock *InsertAtEnd) { | |||
2648 | BinaryOperator *Res = Create(Op, S1, S2, Name); | |||
2649 | InsertAtEnd->getInstList().push_back(Res); | |||
2650 | return Res; | |||
2651 | } | |||
2652 | ||||
2653 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, | |||
2654 | Instruction *InsertBefore) { | |||
2655 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2656 | return new BinaryOperator(Instruction::Sub, | |||
2657 | zero, Op, | |||
2658 | Op->getType(), Name, InsertBefore); | |||
2659 | } | |||
2660 | ||||
2661 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, | |||
2662 | BasicBlock *InsertAtEnd) { | |||
2663 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2664 | return new BinaryOperator(Instruction::Sub, | |||
2665 | zero, Op, | |||
2666 | Op->getType(), Name, InsertAtEnd); | |||
2667 | } | |||
2668 | ||||
2669 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, | |||
2670 | Instruction *InsertBefore) { | |||
2671 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2672 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); | |||
2673 | } | |||
2674 | ||||
2675 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, | |||
2676 | BasicBlock *InsertAtEnd) { | |||
2677 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2678 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); | |||
2679 | } | |||
2680 | ||||
2681 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, | |||
2682 | Instruction *InsertBefore) { | |||
2683 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2684 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); | |||
2685 | } | |||
2686 | ||||
2687 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, | |||
2688 | BasicBlock *InsertAtEnd) { | |||
2689 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2690 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); | |||
2691 | } | |||
2692 | ||||
2693 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, | |||
2694 | Instruction *InsertBefore) { | |||
2695 | Constant *C = Constant::getAllOnesValue(Op->getType()); | |||
2696 | return new BinaryOperator(Instruction::Xor, Op, C, | |||
2697 | Op->getType(), Name, InsertBefore); | |||
2698 | } | |||
2699 | ||||
2700 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, | |||
2701 | BasicBlock *InsertAtEnd) { | |||
2702 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); | |||
2703 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, | |||
2704 | Op->getType(), Name, InsertAtEnd); | |||
2705 | } | |||
2706 | ||||
2707 | // Exchange the two operands to this instruction. This instruction is safe to | |||
2708 | // use on any binary instruction and does not modify the semantics of the | |||
2709 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode | |||
2710 | // is changed. | |||
2711 | bool BinaryOperator::swapOperands() { | |||
2712 | if (!isCommutative()) | |||
2713 | return true; // Can't commute operands | |||
2714 | Op<0>().swap(Op<1>()); | |||
2715 | return false; | |||
2716 | } | |||
2717 | ||||
2718 | //===----------------------------------------------------------------------===// | |||
2719 | // FPMathOperator Class | |||
2720 | //===----------------------------------------------------------------------===// | |||
2721 | ||||
2722 | float FPMathOperator::getFPAccuracy() const { | |||
2723 | const MDNode *MD = | |||
2724 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); | |||
2725 | if (!MD) | |||
2726 | return 0.0; | |||
2727 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); | |||
2728 | return Accuracy->getValueAPF().convertToFloat(); | |||
2729 | } | |||
2730 | ||||
2731 | //===----------------------------------------------------------------------===// | |||
2732 | // CastInst Class | |||
2733 | //===----------------------------------------------------------------------===// | |||
2734 | ||||
2735 | // Just determine if this cast only deals with integral->integral conversion. | |||
2736 | bool CastInst::isIntegerCast() const { | |||
2737 | switch (getOpcode()) { | |||
2738 | default: return false; | |||
2739 | case Instruction::ZExt: | |||
2740 | case Instruction::SExt: | |||
2741 | case Instruction::Trunc: | |||
2742 | return true; | |||
2743 | case Instruction::BitCast: | |||
2744 | return getOperand(0)->getType()->isIntegerTy() && | |||
2745 | getType()->isIntegerTy(); | |||
2746 | } | |||
2747 | } | |||
2748 | ||||
2749 | bool CastInst::isLosslessCast() const { | |||
2750 | // Only BitCast can be lossless, exit fast if we're not BitCast | |||
2751 | if (getOpcode() != Instruction::BitCast) | |||
2752 | return false; | |||
2753 | ||||
2754 | // Identity cast is always lossless | |||
2755 | Type *SrcTy = getOperand(0)->getType(); | |||
2756 | Type *DstTy = getType(); | |||
2757 | if (SrcTy == DstTy) | |||
2758 | return true; | |||
2759 | ||||
2760 | // Pointer to pointer is always lossless. | |||
2761 | if (SrcTy->isPointerTy()) | |||
2762 | return DstTy->isPointerTy(); | |||
2763 | return false; // Other types have no identity values | |||
2764 | } | |||
2765 | ||||
2766 | /// This function determines if the CastInst does not require any bits to be | |||
2767 | /// changed in order to effect the cast. Essentially, it identifies cases where | |||
2768 | /// no code gen is necessary for the cast, hence the name no-op cast. For | |||
2769 | /// example, the following are all no-op casts: | |||
2770 | /// # bitcast i32* %x to i8* | |||
2771 | /// # bitcast <2 x i32> %x to <4 x i16> | |||
2772 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only | |||
2773 | /// Determine if the described cast is a no-op. | |||
2774 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, | |||
2775 | Type *SrcTy, | |||
2776 | Type *DestTy, | |||
2777 | const DataLayout &DL) { | |||
2778 | assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition")(static_cast<void> (0)); | |||
2779 | switch (Opcode) { | |||
2780 | default: llvm_unreachable("Invalid CastOp")__builtin_unreachable(); | |||
2781 | case Instruction::Trunc: | |||
2782 | case Instruction::ZExt: | |||
2783 | case Instruction::SExt: | |||
2784 | case Instruction::FPTrunc: | |||
2785 | case Instruction::FPExt: | |||
2786 | case Instruction::UIToFP: | |||
2787 | case Instruction::SIToFP: | |||
2788 | case Instruction::FPToUI: | |||
2789 | case Instruction::FPToSI: | |||
2790 | case Instruction::AddrSpaceCast: | |||
2791 | // TODO: Target informations may give a more accurate answer here. | |||
2792 | return false; | |||
2793 | case Instruction::BitCast: | |||
2794 | return true; // BitCast never modifies bits. | |||
2795 | case Instruction::PtrToInt: | |||
2796 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == | |||
2797 | DestTy->getScalarSizeInBits(); | |||
2798 | case Instruction::IntToPtr: | |||
2799 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == | |||
2800 | SrcTy->getScalarSizeInBits(); | |||
2801 | } | |||
2802 | } | |||
2803 | ||||
2804 | bool CastInst::isNoopCast(const DataLayout &DL) const { | |||
2805 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); | |||
2806 | } | |||
2807 | ||||
2808 | /// This function determines if a pair of casts can be eliminated and what | |||
2809 | /// opcode should be used in the elimination. This assumes that there are two | |||
2810 | /// instructions like this: | |||
2811 | /// * %F = firstOpcode SrcTy %x to MidTy | |||
2812 | /// * %S = secondOpcode MidTy %F to DstTy | |||
2813 | /// The function returns a resultOpcode so these two casts can be replaced with: | |||
2814 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy | |||
2815 | /// If no such cast is permitted, the function returns 0. | |||
2816 | unsigned CastInst::isEliminableCastPair( | |||
2817 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, | |||
2818 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, | |||
2819 | Type *DstIntPtrTy) { | |||
2820 | // Define the 144 possibilities for these two cast instructions. The values | |||
2821 | // in this matrix determine what to do in a given situation and select the | |||
2822 | // case in the switch below. The rows correspond to firstOp, the columns | |||
2823 | // correspond to secondOp. In looking at the table below, keep in mind | |||
2824 | // the following cast properties: | |||
2825 | // | |||
2826 | // Size Compare Source Destination | |||
2827 | // Operator Src ? Size Type Sign Type Sign | |||
2828 | // -------- ------------ ------------------- --------------------- | |||
2829 | // TRUNC > Integer Any Integral Any | |||
2830 | // ZEXT < Integral Unsigned Integer Any | |||
2831 | // SEXT < Integral Signed Integer Any | |||
2832 | // FPTOUI n/a FloatPt n/a Integral Unsigned | |||
2833 | // FPTOSI n/a FloatPt n/a Integral Signed | |||
2834 | // UITOFP n/a Integral Unsigned FloatPt n/a | |||
2835 | // SITOFP n/a Integral Signed FloatPt n/a | |||
2836 | // FPTRUNC > FloatPt n/a FloatPt n/a | |||
2837 | // FPEXT < FloatPt n/a FloatPt n/a | |||
2838 | // PTRTOINT n/a Pointer n/a Integral Unsigned | |||
2839 | // INTTOPTR n/a Integral Unsigned Pointer n/a | |||
2840 | // BITCAST = FirstClass n/a FirstClass n/a | |||
2841 | // ADDRSPCST n/a Pointer n/a Pointer n/a | |||
2842 | // | |||
2843 | // NOTE: some transforms are safe, but we consider them to be non-profitable. | |||
2844 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", | |||
2845 | // into "fptoui double to i64", but this loses information about the range | |||
2846 | // of the produced value (we no longer know the top-part is all zeros). | |||
2847 | // Further this conversion is often much more expensive for typical hardware, | |||
2848 | // and causes issues when building libgcc. We disallow fptosi+sext for the | |||
2849 | // same reason. | |||
2850 | const unsigned numCastOps = | |||
2851 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; | |||
2852 | static const uint8_t CastResults[numCastOps][numCastOps] = { | |||
2853 | // T F F U S F F P I B A -+ | |||
2854 | // R Z S P P I I T P 2 N T S | | |||
2855 | // U E E 2 2 2 2 R E I T C C +- secondOp | |||
2856 | // N X X U S F F N X N 2 V V | | |||
2857 | // C T T I I P P C T T P T T -+ | |||
2858 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ | |||
2859 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | | |||
2860 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | | |||
2861 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | | |||
2862 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | | |||
2863 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp | |||
2864 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | | |||
2865 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | | |||
2866 | { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | | |||
2867 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | | |||
2868 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | | |||
2869 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | | |||
2870 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ | |||
2871 | }; | |||
2872 | ||||
2873 | // TODO: This logic could be encoded into the table above and handled in the | |||
2874 | // switch below. | |||
2875 | // If either of the casts are a bitcast from scalar to vector, disallow the | |||
2876 | // merging. However, any pair of bitcasts are allowed. | |||
2877 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); | |||
2878 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); | |||
2879 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; | |||
2880 | ||||
2881 | // Check if any of the casts convert scalars <-> vectors. | |||
2882 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || | |||
2883 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) | |||
2884 | if (!AreBothBitcasts) | |||
2885 | return 0; | |||
2886 | ||||
2887 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] | |||
2888 | [secondOp-Instruction::CastOpsBegin]; | |||
2889 | switch (ElimCase) { | |||
2890 | case 0: | |||
2891 | // Categorically disallowed. | |||
2892 | return 0; | |||
2893 | case 1: | |||
2894 | // Allowed, use first cast's opcode. | |||
2895 | return firstOp; | |||
2896 | case 2: | |||
2897 | // Allowed, use second cast's opcode. | |||
2898 | return secondOp; | |||
2899 | case 3: | |||
2900 | // No-op cast in second op implies firstOp as long as the DestTy | |||
2901 | // is integer and we are not converting between a vector and a | |||
2902 | // non-vector type. | |||
2903 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) | |||
2904 | return firstOp; | |||
2905 | return 0; | |||
2906 | case 4: | |||
2907 | // No-op cast in second op implies firstOp as long as the DestTy | |||
2908 | // is floating point. | |||
2909 | if (DstTy->isFloatingPointTy()) | |||
2910 | return firstOp; | |||
2911 | return 0; | |||
2912 | case 5: | |||
2913 | // No-op cast in first op implies secondOp as long as the SrcTy | |||
2914 | // is an integer. | |||
2915 | if (SrcTy->isIntegerTy()) | |||
2916 | return secondOp; | |||
2917 | return 0; | |||
2918 | case 6: | |||
2919 | // No-op cast in first op implies secondOp as long as the SrcTy | |||
2920 | // is a floating point. | |||
2921 | if (SrcTy->isFloatingPointTy()) | |||
2922 | return secondOp; | |||
2923 | return 0; | |||
2924 | case 7: { | |||
2925 | // Disable inttoptr/ptrtoint optimization if enabled. | |||
2926 | if (DisableI2pP2iOpt) | |||
2927 | return 0; | |||
2928 | ||||
2929 | // Cannot simplify if address spaces are different! | |||
2930 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) | |||
2931 | return 0; | |||
2932 | ||||
2933 | unsigned MidSize = MidTy->getScalarSizeInBits(); | |||
2934 | // We can still fold this without knowing the actual sizes as long we | |||
2935 | // know that the intermediate pointer is the largest possible | |||
2936 | // pointer size. | |||
2937 | // FIXME: Is this always true? | |||
2938 | if (MidSize == 64) | |||
2939 | return Instruction::BitCast; | |||
2940 | ||||
2941 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. | |||
2942 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) | |||
2943 | return 0; | |||
2944 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); | |||
2945 | if (MidSize >= PtrSize) | |||
2946 | return Instruction::BitCast; | |||
2947 | return 0; | |||
2948 | } | |||
2949 | case 8: { | |||
2950 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size | |||
2951 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) | |||
2952 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) | |||
2953 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); | |||
2954 | unsigned DstSize = DstTy->getScalarSizeInBits(); | |||
2955 | if (SrcSize == DstSize) | |||
2956 | return Instruction::BitCast; | |||
2957 | else if (SrcSize < DstSize) | |||
2958 | return firstOp; | |||
2959 | return secondOp; | |||
2960 | } | |||
2961 | case 9: | |||
2962 | // zext, sext -> zext, because sext can't sign extend after zext | |||
2963 | return Instruction::ZExt; | |||
2964 | case 11: { | |||
2965 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize | |||
2966 | if (!MidIntPtrTy) | |||
2967 | return 0; | |||
2968 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); | |||
2969 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); | |||
2970 | unsigned DstSize = DstTy->getScalarSizeInBits(); | |||
2971 | if (SrcSize <= PtrSize && SrcSize == DstSize) | |||
2972 | return Instruction::BitCast; | |||
2973 | return 0; | |||
2974 | } | |||
2975 | case 12: | |||
2976 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS | |||
2977 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS | |||
2978 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) | |||
2979 | return Instruction::AddrSpaceCast; | |||
2980 | return Instruction::BitCast; | |||
2981 | case 13: | |||
2982 | // FIXME: this state can be merged with (1), but the following assert | |||
2983 | // is useful to check the correcteness of the sequence due to semantic | |||
2984 | // change of bitcast. | |||
2985 | assert((static_cast<void> (0)) | |||
2986 | SrcTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
2987 | MidTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
2988 | DstTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
2989 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&(static_cast<void> (0)) | |||
2990 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&(static_cast<void> (0)) | |||
2991 | "Illegal addrspacecast, bitcast sequence!")(static_cast<void> (0)); | |||
2992 | // Allowed, use first cast's opcode | |||
2993 | return firstOp; | |||
2994 | case 14: { | |||
2995 | // bitcast, addrspacecast -> addrspacecast if the element type of | |||
2996 | // bitcast's source is the same as that of addrspacecast's destination. | |||
2997 | PointerType *SrcPtrTy = cast<PointerType>(SrcTy->getScalarType()); | |||
2998 | PointerType *DstPtrTy = cast<PointerType>(DstTy->getScalarType()); | |||
2999 | if (SrcPtrTy->hasSameElementTypeAs(DstPtrTy)) | |||
3000 | return Instruction::AddrSpaceCast; | |||
3001 | return 0; | |||
3002 | } | |||
3003 | case 15: | |||
3004 | // FIXME: this state can be merged with (1), but the following assert | |||
3005 | // is useful to check the correcteness of the sequence due to semantic | |||
3006 | // change of bitcast. | |||
3007 | assert((static_cast<void> (0)) | |||
3008 | SrcTy->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
3009 | MidTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
3010 | DstTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
3011 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&(static_cast<void> (0)) | |||
3012 | "Illegal inttoptr, bitcast sequence!")(static_cast<void> (0)); | |||
3013 | // Allowed, use first cast's opcode | |||
3014 | return firstOp; | |||
3015 | case 16: | |||
3016 | // FIXME: this state can be merged with (2), but the following assert | |||
3017 | // is useful to check the correcteness of the sequence due to semantic | |||
3018 | // change of bitcast. | |||
3019 | assert((static_cast<void> (0)) | |||
3020 | SrcTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
3021 | MidTy->isPtrOrPtrVectorTy() &&(static_cast<void> (0)) | |||
3022 | DstTy->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
3023 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&(static_cast<void> (0)) | |||
3024 | "Illegal bitcast, ptrtoint sequence!")(static_cast<void> (0)); | |||
3025 | // Allowed, use second cast's opcode | |||
3026 | return secondOp; | |||
3027 | case 17: | |||
3028 | // (sitofp (zext x)) -> (uitofp x) | |||
3029 | return Instruction::UIToFP; | |||
3030 | case 99: | |||
3031 | // Cast combination can't happen (error in input). This is for all cases | |||
3032 | // where the MidTy is not the same for the two cast instructions. | |||
3033 | llvm_unreachable("Invalid Cast Combination")__builtin_unreachable(); | |||
3034 | default: | |||
3035 | llvm_unreachable("Error in CastResults table!!!")__builtin_unreachable(); | |||
3036 | } | |||
3037 | } | |||
3038 | ||||
3039 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, | |||
3040 | const Twine &Name, Instruction *InsertBefore) { | |||
3041 | assert(castIsValid(op, S, Ty) && "Invalid cast!")(static_cast<void> (0)); | |||
3042 | // Construct and return the appropriate CastInst subclass | |||
3043 | switch (op) { | |||
3044 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); | |||
3045 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); | |||
3046 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); | |||
3047 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); | |||
3048 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); | |||
3049 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); | |||
3050 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); | |||
3051 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); | |||
3052 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); | |||
3053 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); | |||
3054 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); | |||
3055 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); | |||
3056 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); | |||
3057 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
3058 | } | |||
3059 | } | |||
3060 | ||||
3061 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, | |||
3062 | const Twine &Name, BasicBlock *InsertAtEnd) { | |||
3063 | assert(castIsValid(op, S, Ty) && "Invalid cast!")(static_cast<void> (0)); | |||
3064 | // Construct and return the appropriate CastInst subclass | |||
3065 | switch (op) { | |||
3066 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); | |||
3067 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); | |||
3068 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); | |||
3069 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); | |||
3070 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); | |||
3071 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); | |||
3072 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); | |||
3073 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); | |||
3074 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); | |||
3075 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); | |||
3076 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); | |||
3077 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); | |||
3078 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); | |||
3079 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
3080 | } | |||
3081 | } | |||
3082 | ||||
3083 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, | |||
3084 | const Twine &Name, | |||
3085 | Instruction *InsertBefore) { | |||
3086 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3087 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3088 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); | |||
3089 | } | |||
3090 | ||||
3091 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, | |||
3092 | const Twine &Name, | |||
3093 | BasicBlock *InsertAtEnd) { | |||
3094 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3095 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3096 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); | |||
3097 | } | |||
3098 | ||||
3099 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, | |||
3100 | const Twine &Name, | |||
3101 | Instruction *InsertBefore) { | |||
3102 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3103 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3104 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); | |||
3105 | } | |||
3106 | ||||
3107 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, | |||
3108 | const Twine &Name, | |||
3109 | BasicBlock *InsertAtEnd) { | |||
3110 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3111 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3112 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); | |||
3113 | } | |||
3114 | ||||
3115 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, | |||
3116 | const Twine &Name, | |||
3117 | Instruction *InsertBefore) { | |||
3118 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3119 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3120 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); | |||
3121 | } | |||
3122 | ||||
3123 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, | |||
3124 | const Twine &Name, | |||
3125 | BasicBlock *InsertAtEnd) { | |||
3126 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3127 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3128 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); | |||
3129 | } | |||
3130 | ||||
3131 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, | |||
3132 | const Twine &Name, | |||
3133 | BasicBlock *InsertAtEnd) { | |||
3134 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3135 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&(static_cast<void> (0)) | |||
3136 | "Invalid cast")(static_cast<void> (0)); | |||
3137 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3138 | assert((!Ty->isVectorTy() ||(static_cast<void> (0)) | |||
3139 | cast<VectorType>(Ty)->getElementCount() ==(static_cast<void> (0)) | |||
3140 | cast<VectorType>(S->getType())->getElementCount()) &&(static_cast<void> (0)) | |||
3141 | "Invalid cast")(static_cast<void> (0)); | |||
3142 | ||||
3143 | if (Ty->isIntOrIntVectorTy()) | |||
3144 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); | |||
3145 | ||||
3146 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); | |||
3147 | } | |||
3148 | ||||
3149 | /// Create a BitCast or a PtrToInt cast instruction | |||
3150 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, | |||
3151 | const Twine &Name, | |||
3152 | Instruction *InsertBefore) { | |||
3153 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3154 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&(static_cast<void> (0)) | |||
3155 | "Invalid cast")(static_cast<void> (0)); | |||
3156 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3157 | assert((!Ty->isVectorTy() ||(static_cast<void> (0)) | |||
3158 | cast<VectorType>(Ty)->getElementCount() ==(static_cast<void> (0)) | |||
3159 | cast<VectorType>(S->getType())->getElementCount()) &&(static_cast<void> (0)) | |||
3160 | "Invalid cast")(static_cast<void> (0)); | |||
3161 | ||||
3162 | if (Ty->isIntOrIntVectorTy()) | |||
3163 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); | |||
3164 | ||||
3165 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); | |||
3166 | } | |||
3167 | ||||
3168 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( | |||
3169 | Value *S, Type *Ty, | |||
3170 | const Twine &Name, | |||
3171 | BasicBlock *InsertAtEnd) { | |||
3172 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3173 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3174 | ||||
3175 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) | |||
3176 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); | |||
3177 | ||||
3178 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3179 | } | |||
3180 | ||||
3181 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( | |||
3182 | Value *S, Type *Ty, | |||
3183 | const Twine &Name, | |||
3184 | Instruction *InsertBefore) { | |||
3185 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3186 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast")(static_cast<void> (0)); | |||
3187 | ||||
3188 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) | |||
3189 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); | |||
3190 | ||||
3191 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3192 | } | |||
3193 | ||||
3194 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, | |||
3195 | const Twine &Name, | |||
3196 | Instruction *InsertBefore) { | |||
3197 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) | |||
3198 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); | |||
3199 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) | |||
3200 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); | |||
3201 | ||||
3202 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3203 | } | |||
3204 | ||||
3205 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, | |||
3206 | bool isSigned, const Twine &Name, | |||
3207 | Instruction *InsertBefore) { | |||
3208 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
3209 | "Invalid integer cast")(static_cast<void> (0)); | |||
3210 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3211 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3212 | Instruction::CastOps opcode = | |||
3213 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3214 | (SrcBits > DstBits ? Instruction::Trunc : | |||
3215 | (isSigned ? Instruction::SExt : Instruction::ZExt))); | |||
3216 | return Create(opcode, C, Ty, Name, InsertBefore); | |||
3217 | } | |||
3218 | ||||
3219 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, | |||
3220 | bool isSigned, const Twine &Name, | |||
3221 | BasicBlock *InsertAtEnd) { | |||
3222 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&(static_cast<void> (0)) | |||
3223 | "Invalid cast")(static_cast<void> (0)); | |||
3224 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3225 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3226 | Instruction::CastOps opcode = | |||
3227 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3228 | (SrcBits > DstBits ? Instruction::Trunc : | |||
3229 | (isSigned ? Instruction::SExt : Instruction::ZExt))); | |||
3230 | return Create(opcode, C, Ty, Name, InsertAtEnd); | |||
3231 | } | |||
3232 | ||||
3233 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, | |||
3234 | const Twine &Name, | |||
3235 | Instruction *InsertBefore) { | |||
3236 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
3237 | "Invalid cast")(static_cast<void> (0)); | |||
3238 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3239 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3240 | Instruction::CastOps opcode = | |||
3241 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3242 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); | |||
3243 | return Create(opcode, C, Ty, Name, InsertBefore); | |||
3244 | } | |||
3245 | ||||
3246 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, | |||
3247 | const Twine &Name, | |||
3248 | BasicBlock *InsertAtEnd) { | |||
3249 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&(static_cast<void> (0)) | |||
3250 | "Invalid cast")(static_cast<void> (0)); | |||
3251 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3252 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3253 | Instruction::CastOps opcode = | |||
3254 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3255 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); | |||
3256 | return Create(opcode, C, Ty, Name, InsertAtEnd); | |||
3257 | } | |||
3258 | ||||
3259 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { | |||
3260 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) | |||
3261 | return false; | |||
3262 | ||||
3263 | if (SrcTy == DestTy) | |||
3264 | return true; | |||
3265 | ||||
3266 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { | |||
3267 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { | |||
3268 | if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { | |||
3269 | // An element by element cast. Valid if casting the elements is valid. | |||
3270 | SrcTy = SrcVecTy->getElementType(); | |||
3271 | DestTy = DestVecTy->getElementType(); | |||
3272 | } | |||
3273 | } | |||
3274 | } | |||
3275 | ||||
3276 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { | |||
3277 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { | |||
3278 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); | |||
3279 | } | |||
3280 | } | |||
3281 | ||||
3282 | TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3283 | TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3284 | ||||
3285 | // Could still have vectors of pointers if the number of elements doesn't | |||
3286 | // match | |||
3287 | if (SrcBits.getKnownMinSize() == 0 || DestBits.getKnownMinSize() == 0) | |||
3288 | return false; | |||
3289 | ||||
3290 | if (SrcBits != DestBits) | |||
3291 | return false; | |||
3292 | ||||
3293 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) | |||
3294 | return false; | |||
3295 | ||||
3296 | return true; | |||
3297 | } | |||
3298 | ||||
3299 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, | |||
3300 | const DataLayout &DL) { | |||
3301 | // ptrtoint and inttoptr are not allowed on non-integral pointers | |||
3302 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) | |||
3303 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) | |||
3304 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && | |||
3305 | !DL.isNonIntegralPointerType(PtrTy)); | |||
3306 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) | |||
3307 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) | |||
3308 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && | |||
3309 | !DL.isNonIntegralPointerType(PtrTy)); | |||
3310 | ||||
3311 | return isBitCastable(SrcTy, DestTy); | |||
3312 | } | |||
3313 | ||||
3314 | // Provide a way to get a "cast" where the cast opcode is inferred from the | |||
3315 | // types and size of the operand. This, basically, is a parallel of the | |||
3316 | // logic in the castIsValid function below. This axiom should hold: | |||
3317 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) | |||
3318 | // should not assert in castIsValid. In other words, this produces a "correct" | |||
3319 | // casting opcode for the arguments passed to it. | |||
3320 | Instruction::CastOps | |||
3321 | CastInst::getCastOpcode( | |||
3322 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { | |||
3323 | Type *SrcTy = Src->getType(); | |||
3324 | ||||
3325 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&(static_cast<void> (0)) | |||
3326 | "Only first class types are castable!")(static_cast<void> (0)); | |||
3327 | ||||
3328 | if (SrcTy == DestTy) | |||
3329 | return BitCast; | |||
3330 | ||||
3331 | // FIXME: Check address space sizes here | |||
3332 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) | |||
3333 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) | |||
3334 | if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { | |||
3335 | // An element by element cast. Find the appropriate opcode based on the | |||
3336 | // element types. | |||
3337 | SrcTy = SrcVecTy->getElementType(); | |||
3338 | DestTy = DestVecTy->getElementType(); | |||
3339 | } | |||
3340 | ||||
3341 | // Get the bit sizes, we'll need these | |||
3342 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3343 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3344 | ||||
3345 | // Run through the possibilities ... | |||
3346 | if (DestTy->isIntegerTy()) { // Casting to integral | |||
3347 | if (SrcTy->isIntegerTy()) { // Casting from integral | |||
3348 | if (DestBits < SrcBits) | |||
3349 | return Trunc; // int -> smaller int | |||
3350 | else if (DestBits > SrcBits) { // its an extension | |||
3351 | if (SrcIsSigned) | |||
3352 | return SExt; // signed -> SEXT | |||
3353 | else | |||
3354 | return ZExt; // unsigned -> ZEXT | |||
3355 | } else { | |||
3356 | return BitCast; // Same size, No-op cast | |||
3357 | } | |||
3358 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt | |||
3359 | if (DestIsSigned) | |||
3360 | return FPToSI; // FP -> sint | |||
3361 | else | |||
3362 | return FPToUI; // FP -> uint | |||
3363 | } else if (SrcTy->isVectorTy()) { | |||
3364 | assert(DestBits == SrcBits &&(static_cast<void> (0)) | |||
3365 | "Casting vector to integer of different width")(static_cast<void> (0)); | |||
3366 | return BitCast; // Same size, no-op cast | |||
3367 | } else { | |||
3368 | assert(SrcTy->isPointerTy() &&(static_cast<void> (0)) | |||
3369 | "Casting from a value that is not first-class type")(static_cast<void> (0)); | |||
3370 | return PtrToInt; // ptr -> int | |||
3371 | } | |||
3372 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt | |||
3373 | if (SrcTy->isIntegerTy()) { // Casting from integral | |||
3374 | if (SrcIsSigned) | |||
3375 | return SIToFP; // sint -> FP | |||
3376 | else | |||
3377 | return UIToFP; // uint -> FP | |||
3378 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt | |||
3379 | if (DestBits < SrcBits) { | |||
3380 | return FPTrunc; // FP -> smaller FP | |||
3381 | } else if (DestBits > SrcBits) { | |||
3382 | return FPExt; // FP -> larger FP | |||
3383 | } else { | |||
3384 | return BitCast; // same size, no-op cast | |||
3385 | } | |||
3386 | } else if (SrcTy->isVectorTy()) { | |||
3387 | assert(DestBits == SrcBits &&(static_cast<void> (0)) | |||
3388 | "Casting vector to floating point of different width")(static_cast<void> (0)); | |||
3389 | return BitCast; // same size, no-op cast | |||
3390 | } | |||
3391 | llvm_unreachable("Casting pointer or non-first class to float")__builtin_unreachable(); | |||
3392 | } else if (DestTy->isVectorTy()) { | |||
3393 | assert(DestBits == SrcBits &&(static_cast<void> (0)) | |||
3394 | "Illegal cast to vector (wrong type or size)")(static_cast<void> (0)); | |||
3395 | return BitCast; | |||
3396 | } else if (DestTy->isPointerTy()) { | |||
3397 | if (SrcTy->isPointerTy()) { | |||
3398 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) | |||
3399 | return AddrSpaceCast; | |||
3400 | return BitCast; // ptr -> ptr | |||
3401 | } else if (SrcTy->isIntegerTy()) { | |||
3402 | return IntToPtr; // int -> ptr | |||
3403 | } | |||
3404 | llvm_unreachable("Casting pointer to other than pointer or int")__builtin_unreachable(); | |||
3405 | } else if (DestTy->isX86_MMXTy()) { | |||
3406 | if (SrcTy->isVectorTy()) { | |||
3407 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX")(static_cast<void> (0)); | |||
3408 | return BitCast; // 64-bit vector to MMX | |||
3409 | } | |||
3410 | llvm_unreachable("Illegal cast to X86_MMX")__builtin_unreachable(); | |||
3411 | } | |||
3412 | llvm_unreachable("Casting to type that is not first-class")__builtin_unreachable(); | |||
3413 | } | |||
3414 | ||||
3415 | //===----------------------------------------------------------------------===// | |||
3416 | // CastInst SubClass Constructors | |||
3417 | //===----------------------------------------------------------------------===// | |||
3418 | ||||
3419 | /// Check that the construction parameters for a CastInst are correct. This | |||
3420 | /// could be broken out into the separate constructors but it is useful to have | |||
3421 | /// it in one place and to eliminate the redundant code for getting the sizes | |||
3422 | /// of the types involved. | |||
3423 | bool | |||
3424 | CastInst::castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy) { | |||
3425 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || | |||
3426 | SrcTy->isAggregateType() || DstTy->isAggregateType()) | |||
3427 | return false; | |||
3428 | ||||
3429 | // Get the size of the types in bits, and whether we are dealing | |||
3430 | // with vector types, we'll need this later. | |||
3431 | bool SrcIsVec = isa<VectorType>(SrcTy); | |||
3432 | bool DstIsVec = isa<VectorType>(DstTy); | |||
3433 | unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits(); | |||
3434 | unsigned DstScalarBitSize = DstTy->getScalarSizeInBits(); | |||
3435 | ||||
3436 | // If these are vector types, get the lengths of the vectors (using zero for | |||
3437 | // scalar types means that checking that vector lengths match also checks that | |||
3438 | // scalars are not being converted to vectors or vectors to scalars). | |||
3439 | ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount() | |||
3440 | : ElementCount::getFixed(0); | |||
3441 | ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount() | |||
3442 | : ElementCount::getFixed(0); | |||
3443 | ||||
3444 | // Switch on the opcode provided | |||
3445 | switch (op) { | |||
3446 | default: return false; // This is an input error | |||
3447 | case Instruction::Trunc: | |||
3448 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3449 | SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; | |||
3450 | case Instruction::ZExt: | |||
3451 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3452 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3453 | case Instruction::SExt: | |||
3454 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3455 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3456 | case Instruction::FPTrunc: | |||
3457 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3458 | SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; | |||
3459 | case Instruction::FPExt: | |||
3460 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3461 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3462 | case Instruction::UIToFP: | |||
3463 | case Instruction::SIToFP: | |||
3464 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3465 | SrcEC == DstEC; | |||
3466 | case Instruction::FPToUI: | |||
3467 | case Instruction::FPToSI: | |||
3468 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3469 | SrcEC == DstEC; | |||
3470 | case Instruction::PtrToInt: | |||
3471 | if (SrcEC != DstEC) | |||
3472 | return false; | |||
3473 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); | |||
3474 | case Instruction::IntToPtr: | |||
3475 | if (SrcEC != DstEC) | |||
3476 | return false; | |||
3477 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); | |||
3478 | case Instruction::BitCast: { | |||
3479 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); | |||
3480 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); | |||
3481 | ||||
3482 | // BitCast implies a no-op cast of type only. No bits change. | |||
3483 | // However, you can't cast pointers to anything but pointers. | |||
3484 | if (!SrcPtrTy != !DstPtrTy) | |||
3485 | return false; | |||
3486 | ||||
3487 | // For non-pointer cases, the cast is okay if the source and destination bit | |||
3488 | // widths are identical. | |||
3489 | if (!SrcPtrTy) | |||
3490 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); | |||
3491 | ||||
3492 | // If both are pointers then the address spaces must match. | |||
3493 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) | |||
3494 | return false; | |||
3495 | ||||
3496 | // A vector of pointers must have the same number of elements. | |||
3497 | if (SrcIsVec && DstIsVec) | |||
3498 | return SrcEC == DstEC; | |||
3499 | if (SrcIsVec) | |||
3500 | return SrcEC == ElementCount::getFixed(1); | |||
3501 | if (DstIsVec) | |||
3502 | return DstEC == ElementCount::getFixed(1); | |||
3503 | ||||
3504 | return true; | |||
3505 | } | |||
3506 | case Instruction::AddrSpaceCast: { | |||
3507 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); | |||
3508 | if (!SrcPtrTy) | |||
3509 | return false; | |||
3510 | ||||
3511 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); | |||
3512 | if (!DstPtrTy) | |||
3513 | return false; | |||
3514 | ||||
3515 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) | |||
3516 | return false; | |||
3517 | ||||
3518 | return SrcEC == DstEC; | |||
3519 | } | |||
3520 | } | |||
3521 | } | |||
3522 | ||||
3523 | TruncInst::TruncInst( | |||
3524 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3525 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { | |||
3526 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc")(static_cast<void> (0)); | |||
3527 | } | |||
3528 | ||||
3529 | TruncInst::TruncInst( | |||
3530 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3531 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { | |||
3532 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc")(static_cast<void> (0)); | |||
3533 | } | |||
3534 | ||||
3535 | ZExtInst::ZExtInst( | |||
3536 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3537 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { | |||
3538 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt")(static_cast<void> (0)); | |||
3539 | } | |||
3540 | ||||
3541 | ZExtInst::ZExtInst( | |||
3542 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3543 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { | |||
3544 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt")(static_cast<void> (0)); | |||
3545 | } | |||
3546 | SExtInst::SExtInst( | |||
3547 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3548 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { | |||
3549 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt")(static_cast<void> (0)); | |||
3550 | } | |||
3551 | ||||
3552 | SExtInst::SExtInst( | |||
3553 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3554 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { | |||
3555 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt")(static_cast<void> (0)); | |||
3556 | } | |||
3557 | ||||
3558 | FPTruncInst::FPTruncInst( | |||
3559 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3560 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { | |||
3561 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc")(static_cast<void> (0)); | |||
3562 | } | |||
3563 | ||||
3564 | FPTruncInst::FPTruncInst( | |||
3565 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3566 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { | |||
3567 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc")(static_cast<void> (0)); | |||
3568 | } | |||
3569 | ||||
3570 | FPExtInst::FPExtInst( | |||
3571 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3572 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { | |||
3573 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt")(static_cast<void> (0)); | |||
3574 | } | |||
3575 | ||||
3576 | FPExtInst::FPExtInst( | |||
3577 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3578 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { | |||
3579 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt")(static_cast<void> (0)); | |||
3580 | } | |||
3581 | ||||
3582 | UIToFPInst::UIToFPInst( | |||
3583 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3584 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { | |||
3585 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP")(static_cast<void> (0)); | |||
3586 | } | |||
3587 | ||||
3588 | UIToFPInst::UIToFPInst( | |||
3589 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3590 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { | |||
3591 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP")(static_cast<void> (0)); | |||
3592 | } | |||
3593 | ||||
3594 | SIToFPInst::SIToFPInst( | |||
3595 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3596 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { | |||
3597 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP")(static_cast<void> (0)); | |||
3598 | } | |||
3599 | ||||
3600 | SIToFPInst::SIToFPInst( | |||
3601 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3602 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { | |||
3603 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP")(static_cast<void> (0)); | |||
3604 | } | |||
3605 | ||||
3606 | FPToUIInst::FPToUIInst( | |||
3607 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3608 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { | |||
3609 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI")(static_cast<void> (0)); | |||
3610 | } | |||
3611 | ||||
3612 | FPToUIInst::FPToUIInst( | |||
3613 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3614 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { | |||
3615 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI")(static_cast<void> (0)); | |||
3616 | } | |||
3617 | ||||
3618 | FPToSIInst::FPToSIInst( | |||
3619 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3620 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { | |||
3621 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI")(static_cast<void> (0)); | |||
3622 | } | |||
3623 | ||||
3624 | FPToSIInst::FPToSIInst( | |||
3625 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3626 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { | |||
3627 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI")(static_cast<void> (0)); | |||
3628 | } | |||
3629 | ||||
3630 | PtrToIntInst::PtrToIntInst( | |||
3631 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3632 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { | |||
3633 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt")(static_cast<void> (0)); | |||
3634 | } | |||
3635 | ||||
3636 | PtrToIntInst::PtrToIntInst( | |||
3637 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3638 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { | |||
3639 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt")(static_cast<void> (0)); | |||
3640 | } | |||
3641 | ||||
3642 | IntToPtrInst::IntToPtrInst( | |||
3643 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3644 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { | |||
3645 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr")(static_cast<void> (0)); | |||
3646 | } | |||
3647 | ||||
3648 | IntToPtrInst::IntToPtrInst( | |||
3649 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3650 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { | |||
3651 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr")(static_cast<void> (0)); | |||
3652 | } | |||
3653 | ||||
3654 | BitCastInst::BitCastInst( | |||
3655 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3656 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { | |||
3657 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast")(static_cast<void> (0)); | |||
3658 | } | |||
3659 | ||||
3660 | BitCastInst::BitCastInst( | |||
3661 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3662 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { | |||
3663 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast")(static_cast<void> (0)); | |||
3664 | } | |||
3665 | ||||
3666 | AddrSpaceCastInst::AddrSpaceCastInst( | |||
3667 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3668 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { | |||
3669 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast")(static_cast<void> (0)); | |||
3670 | } | |||
3671 | ||||
3672 | AddrSpaceCastInst::AddrSpaceCastInst( | |||
3673 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3674 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { | |||
3675 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast")(static_cast<void> (0)); | |||
3676 | } | |||
3677 | ||||
3678 | //===----------------------------------------------------------------------===// | |||
3679 | // CmpInst Classes | |||
3680 | //===----------------------------------------------------------------------===// | |||
3681 | ||||
3682 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, | |||
3683 | Value *RHS, const Twine &Name, Instruction *InsertBefore, | |||
3684 | Instruction *FlagsSource) | |||
3685 | : Instruction(ty, op, | |||
3686 | OperandTraits<CmpInst>::op_begin(this), | |||
3687 | OperandTraits<CmpInst>::operands(this), | |||
3688 | InsertBefore) { | |||
3689 | Op<0>() = LHS; | |||
3690 | Op<1>() = RHS; | |||
3691 | setPredicate((Predicate)predicate); | |||
3692 | setName(Name); | |||
3693 | if (FlagsSource) | |||
3694 | copyIRFlags(FlagsSource); | |||
3695 | } | |||
3696 | ||||
3697 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, | |||
3698 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) | |||
3699 | : Instruction(ty, op, | |||
3700 | OperandTraits<CmpInst>::op_begin(this), | |||
3701 | OperandTraits<CmpInst>::operands(this), | |||
3702 | InsertAtEnd) { | |||
3703 | Op<0>() = LHS; | |||
3704 | Op<1>() = RHS; | |||
3705 | setPredicate((Predicate)predicate); | |||
3706 | setName(Name); | |||
3707 | } | |||
3708 | ||||
3709 | CmpInst * | |||
3710 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, | |||
3711 | const Twine &Name, Instruction *InsertBefore) { | |||
3712 | if (Op == Instruction::ICmp) { | |||
3713 | if (InsertBefore) | |||
3714 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), | |||
3715 | S1, S2, Name); | |||
3716 | else | |||
3717 | return new ICmpInst(CmpInst::Predicate(predicate), | |||
3718 | S1, S2, Name); | |||
3719 | } | |||
3720 | ||||
3721 | if (InsertBefore) | |||
3722 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), | |||
3723 | S1, S2, Name); | |||
3724 | else | |||
3725 | return new FCmpInst(CmpInst::Predicate(predicate), | |||
3726 | S1, S2, Name); | |||
3727 | } | |||
3728 | ||||
3729 | CmpInst * | |||
3730 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, | |||
3731 | const Twine &Name, BasicBlock *InsertAtEnd) { | |||
3732 | if (Op == Instruction::ICmp) { | |||
3733 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), | |||
3734 | S1, S2, Name); | |||
3735 | } | |||
3736 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), | |||
3737 | S1, S2, Name); | |||
3738 | } | |||
3739 | ||||
3740 | void CmpInst::swapOperands() { | |||
3741 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) | |||
3742 | IC->swapOperands(); | |||
3743 | else | |||
3744 | cast<FCmpInst>(this)->swapOperands(); | |||
3745 | } | |||
3746 | ||||
3747 | bool CmpInst::isCommutative() const { | |||
3748 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) | |||
3749 | return IC->isCommutative(); | |||
3750 | return cast<FCmpInst>(this)->isCommutative(); | |||
3751 | } | |||
3752 | ||||
3753 | bool CmpInst::isEquality(Predicate P) { | |||
3754 | if (ICmpInst::isIntPredicate(P)) | |||
3755 | return ICmpInst::isEquality(P); | |||
3756 | if (FCmpInst::isFPPredicate(P)) | |||
3757 | return FCmpInst::isEquality(P); | |||
3758 | llvm_unreachable("Unsupported predicate kind")__builtin_unreachable(); | |||
3759 | } | |||
3760 | ||||
3761 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { | |||
3762 | switch (pred) { | |||
3763 | default: llvm_unreachable("Unknown cmp predicate!")__builtin_unreachable(); | |||
3764 | case ICMP_EQ: return ICMP_NE; | |||
3765 | case ICMP_NE: return ICMP_EQ; | |||
3766 | case ICMP_UGT: return ICMP_ULE; | |||
3767 | case ICMP_ULT: return ICMP_UGE; | |||
3768 | case ICMP_UGE: return ICMP_ULT; | |||
3769 | case ICMP_ULE: return ICMP_UGT; | |||
3770 | case ICMP_SGT: return ICMP_SLE; | |||
3771 | case ICMP_SLT: return ICMP_SGE; | |||
3772 | case ICMP_SGE: return ICMP_SLT; | |||
3773 | case ICMP_SLE: return ICMP_SGT; | |||
3774 | ||||
3775 | case FCMP_OEQ: return FCMP_UNE; | |||
3776 | case FCMP_ONE: return FCMP_UEQ; | |||
3777 | case FCMP_OGT: return FCMP_ULE; | |||
3778 | case FCMP_OLT: return FCMP_UGE; | |||
3779 | case FCMP_OGE: return FCMP_ULT; | |||
3780 | case FCMP_OLE: return FCMP_UGT; | |||
3781 | case FCMP_UEQ: return FCMP_ONE; | |||
3782 | case FCMP_UNE: return FCMP_OEQ; | |||
3783 | case FCMP_UGT: return FCMP_OLE; | |||
3784 | case FCMP_ULT: return FCMP_OGE; | |||
3785 | case FCMP_UGE: return FCMP_OLT; | |||
3786 | case FCMP_ULE: return FCMP_OGT; | |||
3787 | case FCMP_ORD: return FCMP_UNO; | |||
3788 | case FCMP_UNO: return FCMP_ORD; | |||
3789 | case FCMP_TRUE: return FCMP_FALSE; | |||
3790 | case FCMP_FALSE: return FCMP_TRUE; | |||
3791 | } | |||
3792 | } | |||
3793 | ||||
3794 | StringRef CmpInst::getPredicateName(Predicate Pred) { | |||
3795 | switch (Pred) { | |||
3796 | default: return "unknown"; | |||
3797 | case FCmpInst::FCMP_FALSE: return "false"; | |||
3798 | case FCmpInst::FCMP_OEQ: return "oeq"; | |||
3799 | case FCmpInst::FCMP_OGT: return "ogt"; | |||
3800 | case FCmpInst::FCMP_OGE: return "oge"; | |||
3801 | case FCmpInst::FCMP_OLT: return "olt"; | |||
3802 | case FCmpInst::FCMP_OLE: return "ole"; | |||
3803 | case FCmpInst::FCMP_ONE: return "one"; | |||
3804 | case FCmpInst::FCMP_ORD: return "ord"; | |||
3805 | case FCmpInst::FCMP_UNO: return "uno"; | |||
3806 | case FCmpInst::FCMP_UEQ: return "ueq"; | |||
3807 | case FCmpInst::FCMP_UGT: return "ugt"; | |||
3808 | case FCmpInst::FCMP_UGE: return "uge"; | |||
3809 | case FCmpInst::FCMP_ULT: return "ult"; | |||
3810 | case FCmpInst::FCMP_ULE: return "ule"; | |||
3811 | case FCmpInst::FCMP_UNE: return "une"; | |||
3812 | case FCmpInst::FCMP_TRUE: return "true"; | |||
3813 | case ICmpInst::ICMP_EQ: return "eq"; | |||
3814 | case ICmpInst::ICMP_NE: return "ne"; | |||
3815 | case ICmpInst::ICMP_SGT: return "sgt"; | |||
3816 | case ICmpInst::ICMP_SGE: return "sge"; | |||
3817 | case ICmpInst::ICMP_SLT: return "slt"; | |||
3818 | case ICmpInst::ICMP_SLE: return "sle"; | |||
3819 | case ICmpInst::ICMP_UGT: return "ugt"; | |||
3820 | case ICmpInst::ICMP_UGE: return "uge"; | |||
3821 | case ICmpInst::ICMP_ULT: return "ult"; | |||
3822 | case ICmpInst::ICMP_ULE: return "ule"; | |||
3823 | } | |||
3824 | } | |||
3825 | ||||
3826 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { | |||
3827 | switch (pred) { | |||
3828 | default: llvm_unreachable("Unknown icmp predicate!")__builtin_unreachable(); | |||
3829 | case ICMP_EQ: case ICMP_NE: | |||
3830 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: | |||
3831 | return pred; | |||
3832 | case ICMP_UGT: return ICMP_SGT; | |||
3833 | case ICMP_ULT: return ICMP_SLT; | |||
3834 | case ICMP_UGE: return ICMP_SGE; | |||
3835 | case ICMP_ULE: return ICMP_SLE; | |||
3836 | } | |||
3837 | } | |||
3838 | ||||
3839 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { | |||
3840 | switch (pred) { | |||
3841 | default: llvm_unreachable("Unknown icmp predicate!")__builtin_unreachable(); | |||
3842 | case ICMP_EQ: case ICMP_NE: | |||
3843 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: | |||
3844 | return pred; | |||
3845 | case ICMP_SGT: return ICMP_UGT; | |||
3846 | case ICMP_SLT: return ICMP_ULT; | |||
3847 | case ICMP_SGE: return ICMP_UGE; | |||
3848 | case ICMP_SLE: return ICMP_ULE; | |||
3849 | } | |||
3850 | } | |||
3851 | ||||
3852 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { | |||
3853 | switch (pred) { | |||
3854 | default: llvm_unreachable("Unknown cmp predicate!")__builtin_unreachable(); | |||
3855 | case ICMP_EQ: case ICMP_NE: | |||
3856 | return pred; | |||
3857 | case ICMP_SGT: return ICMP_SLT; | |||
3858 | case ICMP_SLT: return ICMP_SGT; | |||
3859 | case ICMP_SGE: return ICMP_SLE; | |||
3860 | case ICMP_SLE: return ICMP_SGE; | |||
3861 | case ICMP_UGT: return ICMP_ULT; | |||
3862 | case ICMP_ULT: return ICMP_UGT; | |||
3863 | case ICMP_UGE: return ICMP_ULE; | |||
3864 | case ICMP_ULE: return ICMP_UGE; | |||
3865 | ||||
3866 | case FCMP_FALSE: case FCMP_TRUE: | |||
3867 | case FCMP_OEQ: case FCMP_ONE: | |||
3868 | case FCMP_UEQ: case FCMP_UNE: | |||
3869 | case FCMP_ORD: case FCMP_UNO: | |||
3870 | return pred; | |||
3871 | case FCMP_OGT: return FCMP_OLT; | |||
3872 | case FCMP_OLT: return FCMP_OGT; | |||
3873 | case FCMP_OGE: return FCMP_OLE; | |||
3874 | case FCMP_OLE: return FCMP_OGE; | |||
3875 | case FCMP_UGT: return FCMP_ULT; | |||
3876 | case FCMP_ULT: return FCMP_UGT; | |||
3877 | case FCMP_UGE: return FCMP_ULE; | |||
3878 | case FCMP_ULE: return FCMP_UGE; | |||
3879 | } | |||
3880 | } | |||
3881 | ||||
3882 | bool CmpInst::isNonStrictPredicate(Predicate pred) { | |||
3883 | switch (pred) { | |||
3884 | case ICMP_SGE: | |||
3885 | case ICMP_SLE: | |||
3886 | case ICMP_UGE: | |||
3887 | case ICMP_ULE: | |||
3888 | case FCMP_OGE: | |||
3889 | case FCMP_OLE: | |||
3890 | case FCMP_UGE: | |||
3891 | case FCMP_ULE: | |||
3892 | return true; | |||
3893 | default: | |||
3894 | return false; | |||
3895 | } | |||
3896 | } | |||
3897 | ||||
3898 | bool CmpInst::isStrictPredicate(Predicate pred) { | |||
3899 | switch (pred) { | |||
3900 | case ICMP_SGT: | |||
3901 | case ICMP_SLT: | |||
3902 | case ICMP_UGT: | |||
3903 | case ICMP_ULT: | |||
3904 | case FCMP_OGT: | |||
3905 | case FCMP_OLT: | |||
3906 | case FCMP_UGT: | |||
3907 | case FCMP_ULT: | |||
3908 | return true; | |||
3909 | default: | |||
3910 | return false; | |||
3911 | } | |||
3912 | } | |||
3913 | ||||
3914 | CmpInst::Predicate CmpInst::getStrictPredicate(Predicate pred) { | |||
3915 | switch (pred) { | |||
3916 | case ICMP_SGE: | |||
3917 | return ICMP_SGT; | |||
3918 | case ICMP_SLE: | |||
3919 | return ICMP_SLT; | |||
3920 | case ICMP_UGE: | |||
3921 | return ICMP_UGT; | |||
3922 | case ICMP_ULE: | |||
3923 | return ICMP_ULT; | |||
3924 | case FCMP_OGE: | |||
3925 | return FCMP_OGT; | |||
3926 | case FCMP_OLE: | |||
3927 | return FCMP_OLT; | |||
3928 | case FCMP_UGE: | |||
3929 | return FCMP_UGT; | |||
3930 | case FCMP_ULE: | |||
3931 | return FCMP_ULT; | |||
3932 | default: | |||
3933 | return pred; | |||
3934 | } | |||
3935 | } | |||
3936 | ||||
3937 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { | |||
3938 | switch (pred) { | |||
3939 | case ICMP_SGT: | |||
3940 | return ICMP_SGE; | |||
3941 | case ICMP_SLT: | |||
3942 | return ICMP_SLE; | |||
3943 | case ICMP_UGT: | |||
3944 | return ICMP_UGE; | |||
3945 | case ICMP_ULT: | |||
3946 | return ICMP_ULE; | |||
3947 | case FCMP_OGT: | |||
3948 | return FCMP_OGE; | |||
3949 | case FCMP_OLT: | |||
3950 | return FCMP_OLE; | |||
3951 | case FCMP_UGT: | |||
3952 | return FCMP_UGE; | |||
3953 | case FCMP_ULT: | |||
3954 | return FCMP_ULE; | |||
3955 | default: | |||
3956 | return pred; | |||
3957 | } | |||
3958 | } | |||
3959 | ||||
3960 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { | |||
3961 | assert(CmpInst::isRelational(pred) && "Call only with relational predicate!")(static_cast<void> (0)); | |||
3962 | ||||
3963 | if (isStrictPredicate(pred)) | |||
3964 | return getNonStrictPredicate(pred); | |||
3965 | if (isNonStrictPredicate(pred)) | |||
3966 | return getStrictPredicate(pred); | |||
3967 | ||||
3968 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3969 | } | |||
3970 | ||||
3971 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { | |||
3972 | assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!")(static_cast<void> (0)); | |||
3973 | ||||
3974 | switch (pred) { | |||
3975 | default: | |||
3976 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3977 | case CmpInst::ICMP_ULT: | |||
3978 | return CmpInst::ICMP_SLT; | |||
3979 | case CmpInst::ICMP_ULE: | |||
3980 | return CmpInst::ICMP_SLE; | |||
3981 | case CmpInst::ICMP_UGT: | |||
3982 | return CmpInst::ICMP_SGT; | |||
3983 | case CmpInst::ICMP_UGE: | |||
3984 | return CmpInst::ICMP_SGE; | |||
3985 | } | |||
3986 | } | |||
3987 | ||||
3988 | CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) { | |||
3989 | assert(CmpInst::isSigned(pred) && "Call only with signed predicates!")(static_cast<void> (0)); | |||
3990 | ||||
3991 | switch (pred) { | |||
3992 | default: | |||
3993 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3994 | case CmpInst::ICMP_SLT: | |||
3995 | return CmpInst::ICMP_ULT; | |||
3996 | case CmpInst::ICMP_SLE: | |||
3997 | return CmpInst::ICMP_ULE; | |||
3998 | case CmpInst::ICMP_SGT: | |||
3999 | return CmpInst::ICMP_UGT; | |||
4000 | case CmpInst::ICMP_SGE: | |||
4001 | return CmpInst::ICMP_UGE; | |||
4002 | } | |||
4003 | } | |||
4004 | ||||
4005 | bool CmpInst::isUnsigned(Predicate predicate) { | |||
4006 | switch (predicate) { | |||
4007 | default: return false; | |||
4008 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: | |||
4009 | case ICmpInst::ICMP_UGE: return true; | |||
4010 | } | |||
4011 | } | |||
4012 | ||||
4013 | bool CmpInst::isSigned(Predicate predicate) { | |||
4014 | switch (predicate) { | |||
4015 | default: return false; | |||
4016 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: | |||
4017 | case ICmpInst::ICMP_SGE: return true; | |||
4018 | } | |||
4019 | } | |||
4020 | ||||
4021 | CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) { | |||
4022 | assert(CmpInst::isRelational(pred) &&(static_cast<void> (0)) | |||
4023 | "Call only with non-equality predicates!")(static_cast<void> (0)); | |||
4024 | ||||
4025 | if (isSigned(pred)) | |||
4026 | return getUnsignedPredicate(pred); | |||
4027 | if (isUnsigned(pred)) | |||
4028 | return getSignedPredicate(pred); | |||
4029 | ||||
4030 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
4031 | } | |||
4032 | ||||
4033 | bool CmpInst::isOrdered(Predicate predicate) { | |||
4034 | switch (predicate) { | |||
4035 | default: return false; | |||
4036 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: | |||
4037 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: | |||
4038 | case FCmpInst::FCMP_ORD: return true; | |||
4039 | } | |||
4040 | } | |||
4041 | ||||
4042 | bool CmpInst::isUnordered(Predicate predicate) { | |||
4043 | switch (predicate) { | |||
4044 | default: return false; | |||
4045 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: | |||
4046 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: | |||
4047 | case FCmpInst::FCMP_UNO: return true; | |||
4048 | } | |||
4049 | } | |||
4050 | ||||
4051 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { | |||
4052 | switch(predicate) { | |||
4053 | default: return false; | |||
4054 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: | |||
4055 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; | |||
4056 | } | |||
4057 | } | |||
4058 | ||||
4059 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { | |||
4060 | switch(predicate) { | |||
4061 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: | |||
4062 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; | |||
4063 | default: return false; | |||
4064 | } | |||
4065 | } | |||
4066 | ||||
4067 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { | |||
4068 | // If the predicates match, then we know the first condition implies the | |||
4069 | // second is true. | |||
4070 | if (Pred1 == Pred2) | |||
4071 | return true; | |||
4072 | ||||
4073 | switch (Pred1) { | |||
4074 | default: | |||
4075 | break; | |||
4076 | case ICMP_EQ: | |||
4077 | // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. | |||
4078 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || | |||
4079 | Pred2 == ICMP_SLE; | |||
4080 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. | |||
4081 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; | |||
4082 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. | |||
4083 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; | |||
4084 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. | |||
4085 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; | |||
4086 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. | |||
4087 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; | |||
4088 | } | |||
4089 | return false; | |||
4090 | } | |||
4091 | ||||
4092 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { | |||
4093 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); | |||
4094 | } | |||
4095 | ||||
4096 | //===----------------------------------------------------------------------===// | |||
4097 | // SwitchInst Implementation | |||
4098 | //===----------------------------------------------------------------------===// | |||
4099 | ||||
4100 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { | |||
4101 | assert(Value && Default && NumReserved)(static_cast<void> (0)); | |||
4102 | ReservedSpace = NumReserved; | |||
4103 | setNumHungOffUseOperands(2); | |||
4104 | allocHungoffUses(ReservedSpace); | |||
4105 | ||||
4106 | Op<0>() = Value; | |||
4107 | Op<1>() = Default; | |||
4108 | } | |||
4109 | ||||
4110 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to | |||
4111 | /// switch on and a default destination. The number of additional cases can | |||
4112 | /// be specified here to make memory allocation more efficient. This | |||
4113 | /// constructor can also autoinsert before another instruction. | |||
4114 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
4115 | Instruction *InsertBefore) | |||
4116 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, | |||
4117 | nullptr, 0, InsertBefore) { | |||
4118 | init(Value, Default, 2+NumCases*2); | |||
4119 | } | |||
4120 | ||||
4121 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to | |||
4122 | /// switch on and a default destination. The number of additional cases can | |||
4123 | /// be specified here to make memory allocation more efficient. This | |||
4124 | /// constructor also autoinserts at the end of the specified BasicBlock. | |||
4125 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
4126 | BasicBlock *InsertAtEnd) | |||
4127 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, | |||
4128 | nullptr, 0, InsertAtEnd) { | |||
4129 | init(Value, Default, 2+NumCases*2); | |||
4130 | } | |||
4131 | ||||
4132 | SwitchInst::SwitchInst(const SwitchInst &SI) | |||
4133 | : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { | |||
4134 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); | |||
4135 | setNumHungOffUseOperands(SI.getNumOperands()); | |||
4136 | Use *OL = getOperandList(); | |||
4137 | const Use *InOL = SI.getOperandList(); | |||
4138 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { | |||
4139 | OL[i] = InOL[i]; | |||
4140 | OL[i+1] = InOL[i+1]; | |||
4141 | } | |||
4142 | SubclassOptionalData = SI.SubclassOptionalData; | |||
4143 | } | |||
4144 | ||||
4145 | /// addCase - Add an entry to the switch instruction... | |||
4146 | /// | |||
4147 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { | |||
4148 | unsigned NewCaseIdx = getNumCases(); | |||
4149 | unsigned OpNo = getNumOperands(); | |||
4150 | if (OpNo+2 > ReservedSpace) | |||
4151 | growOperands(); // Get more space! | |||
4152 | // Initialize some new operands. | |||
4153 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!")(static_cast<void> (0)); | |||
4154 | setNumHungOffUseOperands(OpNo+2); | |||
4155 | CaseHandle Case(this, NewCaseIdx); | |||
4156 | Case.setValue(OnVal); | |||
4157 | Case.setSuccessor(Dest); | |||
4158 | } | |||
4159 | ||||
4160 | /// removeCase - This method removes the specified case and its successor | |||
4161 | /// from the switch instruction. | |||
4162 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { | |||
4163 | unsigned idx = I->getCaseIndex(); | |||
4164 | ||||
4165 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!")(static_cast<void> (0)); | |||
4166 | ||||
4167 | unsigned NumOps = getNumOperands(); | |||
4168 | Use *OL = getOperandList(); | |||
4169 | ||||
4170 | // Overwrite this case with the end of the list. | |||
4171 | if (2 + (idx + 1) * 2 != NumOps) { | |||
4172 | OL[2 + idx * 2] = OL[NumOps - 2]; | |||
4173 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; | |||
4174 | } | |||
4175 | ||||
4176 | // Nuke the last value. | |||
4177 | OL[NumOps-2].set(nullptr); | |||
4178 | OL[NumOps-2+1].set(nullptr); | |||
4179 | setNumHungOffUseOperands(NumOps-2); | |||
4180 | ||||
4181 | return CaseIt(this, idx); | |||
4182 | } | |||
4183 | ||||
4184 | /// growOperands - grow operands - This grows the operand list in response | |||
4185 | /// to a push_back style of operation. This grows the number of ops by 3 times. | |||
4186 | /// | |||
4187 | void SwitchInst::growOperands() { | |||
4188 | unsigned e = getNumOperands(); | |||
4189 | unsigned NumOps = e*3; | |||
4190 | ||||
4191 | ReservedSpace = NumOps; | |||
4192 | growHungoffUses(ReservedSpace); | |||
4193 | } | |||
4194 | ||||
4195 | MDNode * | |||
4196 | SwitchInstProfUpdateWrapper::getProfBranchWeightsMD(const SwitchInst &SI) { | |||
4197 | if (MDNode *ProfileData = SI.getMetadata(LLVMContext::MD_prof)) | |||
4198 | if (auto *MDName = dyn_cast<MDString>(ProfileData->getOperand(0))) | |||
4199 | if (MDName->getString() == "branch_weights") | |||
4200 | return ProfileData; | |||
4201 | return nullptr; | |||
4202 | } | |||
4203 | ||||
4204 | MDNode *SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() { | |||
4205 | assert(Changed && "called only if metadata has changed")(static_cast<void> (0)); | |||
4206 | ||||
4207 | if (!Weights) | |||
4208 | return nullptr; | |||
4209 | ||||
4210 | assert(SI.getNumSuccessors() == Weights->size() &&(static_cast<void> (0)) | |||
4211 | "num of prof branch_weights must accord with num of successors")(static_cast<void> (0)); | |||
4212 | ||||
4213 | bool AllZeroes = | |||
4214 | all_of(Weights.getValue(), [](uint32_t W) { return W == 0; }); | |||
4215 | ||||
4216 | if (AllZeroes || Weights.getValue().size() < 2) | |||
4217 | return nullptr; | |||
4218 | ||||
4219 | return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights); | |||
4220 | } | |||
4221 | ||||
4222 | void SwitchInstProfUpdateWrapper::init() { | |||
4223 | MDNode *ProfileData = getProfBranchWeightsMD(SI); | |||
4224 | if (!ProfileData) | |||
4225 | return; | |||
4226 | ||||
4227 | if (ProfileData->getNumOperands() != SI.getNumSuccessors() + 1) { | |||
4228 | llvm_unreachable("number of prof branch_weights metadata operands does "__builtin_unreachable() | |||
4229 | "not correspond to number of succesors")__builtin_unreachable(); | |||
4230 | } | |||
4231 | ||||
4232 | SmallVector<uint32_t, 8> Weights; | |||
4233 | for (unsigned CI = 1, CE = SI.getNumSuccessors(); CI <= CE; ++CI) { | |||
4234 | ConstantInt *C = mdconst::extract<ConstantInt>(ProfileData->getOperand(CI)); | |||
4235 | uint32_t CW = C->getValue().getZExtValue(); | |||
4236 | Weights.push_back(CW); | |||
4237 | } | |||
4238 | this->Weights = std::move(Weights); | |||
4239 | } | |||
4240 | ||||
4241 | SwitchInst::CaseIt | |||
4242 | SwitchInstProfUpdateWrapper::removeCase(SwitchInst::CaseIt I) { | |||
4243 | if (Weights) { | |||
4244 | assert(SI.getNumSuccessors() == Weights->size() &&(static_cast<void> (0)) | |||
4245 | "num of prof branch_weights must accord with num of successors")(static_cast<void> (0)); | |||
4246 | Changed = true; | |||
4247 | // Copy the last case to the place of the removed one and shrink. | |||
4248 | // This is tightly coupled with the way SwitchInst::removeCase() removes | |||
4249 | // the cases in SwitchInst::removeCase(CaseIt). | |||
4250 | Weights.getValue()[I->getCaseIndex() + 1] = Weights.getValue().back(); | |||
4251 | Weights.getValue().pop_back(); | |||
4252 | } | |||
4253 | return SI.removeCase(I); | |||
4254 | } | |||
4255 | ||||
4256 | void SwitchInstProfUpdateWrapper::addCase( | |||
4257 | ConstantInt *OnVal, BasicBlock *Dest, | |||
4258 | SwitchInstProfUpdateWrapper::CaseWeightOpt W) { | |||
4259 | SI.addCase(OnVal, Dest); | |||
4260 | ||||
4261 | if (!Weights && W && *W) { | |||
4262 | Changed = true; | |||
4263 | Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); | |||
4264 | Weights.getValue()[SI.getNumSuccessors() - 1] = *W; | |||
4265 | } else if (Weights) { | |||
4266 | Changed = true; | |||
4267 | Weights.getValue().push_back(W ? *W : 0); | |||
4268 | } | |||
4269 | if (Weights) | |||
4270 | assert(SI.getNumSuccessors() == Weights->size() &&(static_cast<void> (0)) | |||
4271 | "num of prof branch_weights must accord with num of successors")(static_cast<void> (0)); | |||
4272 | } | |||
4273 | ||||
4274 | SymbolTableList<Instruction>::iterator | |||
4275 | SwitchInstProfUpdateWrapper::eraseFromParent() { | |||
4276 | // Instruction is erased. Mark as unchanged to not touch it in the destructor. | |||
4277 | Changed = false; | |||
4278 | if (Weights) | |||
4279 | Weights->resize(0); | |||
4280 | return SI.eraseFromParent(); | |||
4281 | } | |||
4282 | ||||
4283 | SwitchInstProfUpdateWrapper::CaseWeightOpt | |||
4284 | SwitchInstProfUpdateWrapper::getSuccessorWeight(unsigned idx) { | |||
4285 | if (!Weights) | |||
4286 | return None; | |||
4287 | return Weights.getValue()[idx]; | |||
4288 | } | |||
4289 | ||||
4290 | void SwitchInstProfUpdateWrapper::setSuccessorWeight( | |||
4291 | unsigned idx, SwitchInstProfUpdateWrapper::CaseWeightOpt W) { | |||
4292 | if (!W) | |||
4293 | return; | |||
4294 | ||||
4295 | if (!Weights && *W) | |||
4296 | Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); | |||
4297 | ||||
4298 | if (Weights) { | |||
4299 | auto &OldW = Weights.getValue()[idx]; | |||
4300 | if (*W != OldW) { | |||
4301 | Changed = true; | |||
4302 | OldW = *W; | |||
4303 | } | |||
4304 | } | |||
4305 | } | |||
4306 | ||||
4307 | SwitchInstProfUpdateWrapper::CaseWeightOpt | |||
4308 | SwitchInstProfUpdateWrapper::getSuccessorWeight(const SwitchInst &SI, | |||
4309 | unsigned idx) { | |||
4310 | if (MDNode *ProfileData = getProfBranchWeightsMD(SI)) | |||
4311 | if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1) | |||
4312 | return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1)) | |||
4313 | ->getValue() | |||
4314 | .getZExtValue(); | |||
4315 | ||||
4316 | return None; | |||
4317 | } | |||
4318 | ||||
4319 | //===----------------------------------------------------------------------===// | |||
4320 | // IndirectBrInst Implementation | |||
4321 | //===----------------------------------------------------------------------===// | |||
4322 | ||||
4323 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { | |||
4324 | assert(Address && Address->getType()->isPointerTy() &&(static_cast<void> (0)) | |||
4325 | "Address of indirectbr must be a pointer")(static_cast<void> (0)); | |||
4326 | ReservedSpace = 1+NumDests; | |||
4327 | setNumHungOffUseOperands(1); | |||
4328 | allocHungoffUses(ReservedSpace); | |||
4329 | ||||
4330 | Op<0>() = Address; | |||
4331 | } | |||
4332 | ||||
4333 | ||||
4334 | /// growOperands - grow operands - This grows the operand list in response | |||
4335 | /// to a push_back style of operation. This grows the number of ops by 2 times. | |||
4336 | /// | |||
4337 | void IndirectBrInst::growOperands() { | |||
4338 | unsigned e = getNumOperands(); | |||
4339 | unsigned NumOps = e*2; | |||
4340 | ||||
4341 | ReservedSpace = NumOps; | |||
4342 | growHungoffUses(ReservedSpace); | |||
4343 | } | |||
4344 | ||||
4345 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, | |||
4346 | Instruction *InsertBefore) | |||
4347 | : Instruction(Type::getVoidTy(Address->getContext()), | |||
4348 | Instruction::IndirectBr, nullptr, 0, InsertBefore) { | |||
4349 | init(Address, NumCases); | |||
4350 | } | |||
4351 | ||||
4352 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, | |||
4353 | BasicBlock *InsertAtEnd) | |||
4354 | : Instruction(Type::getVoidTy(Address->getContext()), | |||
4355 | Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { | |||
4356 | init(Address, NumCases); | |||
4357 | } | |||
4358 | ||||
4359 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) | |||
4360 | : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, | |||
4361 | nullptr, IBI.getNumOperands()) { | |||
4362 | allocHungoffUses(IBI.getNumOperands()); | |||
4363 | Use *OL = getOperandList(); | |||
4364 | const Use *InOL = IBI.getOperandList(); | |||
4365 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) | |||
4366 | OL[i] = InOL[i]; | |||
4367 | SubclassOptionalData = IBI.SubclassOptionalData; | |||
4368 | } | |||
4369 | ||||
4370 | /// addDestination - Add a destination. | |||
4371 | /// | |||
4372 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { | |||
4373 | unsigned OpNo = getNumOperands(); | |||
4374 | if (OpNo+1 > ReservedSpace) | |||
4375 | growOperands(); // Get more space! | |||
4376 | // Initialize some new operands. | |||
4377 | assert(OpNo < ReservedSpace && "Growing didn't work!")(static_cast<void> (0)); | |||
4378 | setNumHungOffUseOperands(OpNo+1); | |||
4379 | getOperandList()[OpNo] = DestBB; | |||
4380 | } | |||
4381 | ||||
4382 | /// removeDestination - This method removes the specified successor from the | |||
4383 | /// indirectbr instruction. | |||
4384 | void IndirectBrInst::removeDestination(unsigned idx) { | |||
4385 | assert(idx < getNumOperands()-1 && "Successor index out of range!")(static_cast<void> (0)); | |||
4386 | ||||
4387 | unsigned NumOps = getNumOperands(); | |||
4388 | Use *OL = getOperandList(); | |||
4389 | ||||
4390 | // Replace this value with the last one. | |||
4391 | OL[idx+1] = OL[NumOps-1]; | |||
4392 | ||||
4393 | // Nuke the last value. | |||
4394 | OL[NumOps-1].set(nullptr); | |||
4395 | setNumHungOffUseOperands(NumOps-1); | |||
4396 | } | |||
4397 | ||||
4398 | //===----------------------------------------------------------------------===// | |||
4399 | // FreezeInst Implementation | |||
4400 | //===----------------------------------------------------------------------===// | |||
4401 | ||||
4402 | FreezeInst::FreezeInst(Value *S, | |||
4403 | const Twine &Name, Instruction *InsertBefore) | |||
4404 | : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) { | |||
4405 | setName(Name); | |||
4406 | } | |||
4407 | ||||
4408 | FreezeInst::FreezeInst(Value *S, | |||
4409 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
4410 | : UnaryInstruction(S->getType(), Freeze, S, InsertAtEnd) { | |||
4411 | setName(Name); | |||
4412 | } | |||
4413 | ||||
4414 | //===----------------------------------------------------------------------===// | |||
4415 | // cloneImpl() implementations | |||
4416 | //===----------------------------------------------------------------------===// | |||
4417 | ||||
4418 | // Define these methods here so vtables don't get emitted into every translation | |||
4419 | // unit that uses these classes. | |||
4420 | ||||
4421 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { | |||
4422 | return new (getNumOperands()) GetElementPtrInst(*this); | |||
4423 | } | |||
4424 | ||||
4425 | UnaryOperator *UnaryOperator::cloneImpl() const { | |||
4426 | return Create(getOpcode(), Op<0>()); | |||
4427 | } | |||
4428 | ||||
4429 | BinaryOperator *BinaryOperator::cloneImpl() const { | |||
4430 | return Create(getOpcode(), Op<0>(), Op<1>()); | |||
4431 | } | |||
4432 | ||||
4433 | FCmpInst *FCmpInst::cloneImpl() const { | |||
4434 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); | |||
4435 | } | |||
4436 | ||||
4437 | ICmpInst *ICmpInst::cloneImpl() const { | |||
4438 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); | |||
4439 | } | |||
4440 | ||||
4441 | ExtractValueInst *ExtractValueInst::cloneImpl() const { | |||
4442 | return new ExtractValueInst(*this); | |||
4443 | } | |||
4444 | ||||
4445 | InsertValueInst *InsertValueInst::cloneImpl() const { | |||
4446 | return new InsertValueInst(*this); | |||
4447 | } | |||
4448 | ||||
4449 | AllocaInst *AllocaInst::cloneImpl() const { | |||
4450 | AllocaInst *Result = | |||
4451 | new AllocaInst(getAllocatedType(), getType()->getAddressSpace(), | |||
4452 | getOperand(0), getAlign()); | |||
4453 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); | |||
4454 | Result->setSwiftError(isSwiftError()); | |||
4455 | return Result; | |||
4456 | } | |||
4457 | ||||
4458 | LoadInst *LoadInst::cloneImpl() const { | |||
4459 | return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), | |||
4460 | getAlign(), getOrdering(), getSyncScopeID()); | |||
4461 | } | |||
4462 | ||||
4463 | StoreInst *StoreInst::cloneImpl() const { | |||
4464 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), getAlign(), | |||
4465 | getOrdering(), getSyncScopeID()); | |||
4466 | } | |||
4467 | ||||
4468 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { | |||
4469 | AtomicCmpXchgInst *Result = new AtomicCmpXchgInst( | |||
4470 | getOperand(0), getOperand(1), getOperand(2), getAlign(), | |||
4471 | getSuccessOrdering(), getFailureOrdering(), getSyncScopeID()); | |||
4472 | Result->setVolatile(isVolatile()); | |||
4473 | Result->setWeak(isWeak()); | |||
4474 | return Result; | |||
4475 | } | |||
4476 | ||||
4477 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { | |||
4478 | AtomicRMWInst *Result = | |||
4479 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), | |||
4480 | getAlign(), getOrdering(), getSyncScopeID()); | |||
4481 | Result->setVolatile(isVolatile()); | |||
4482 | return Result; | |||
4483 | } | |||
4484 | ||||
4485 | FenceInst *FenceInst::cloneImpl() const { | |||
4486 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); | |||
4487 | } | |||
4488 | ||||
4489 | TruncInst *TruncInst::cloneImpl() const { | |||
4490 | return new TruncInst(getOperand(0), getType()); | |||
4491 | } | |||
4492 | ||||
4493 | ZExtInst *ZExtInst::cloneImpl() const { | |||
4494 | return new ZExtInst(getOperand(0), getType()); | |||
4495 | } | |||
4496 | ||||
4497 | SExtInst *SExtInst::cloneImpl() const { | |||
4498 | return new SExtInst(getOperand(0), getType()); | |||
4499 | } | |||
4500 | ||||
4501 | FPTruncInst *FPTruncInst::cloneImpl() const { | |||
4502 | return new FPTruncInst(getOperand(0), getType()); | |||
4503 | } | |||
4504 | ||||
4505 | FPExtInst *FPExtInst::cloneImpl() const { | |||
4506 | return new FPExtInst(getOperand(0), getType()); | |||
4507 | } | |||
4508 | ||||
4509 | UIToFPInst *UIToFPInst::cloneImpl() const { | |||
4510 | return new UIToFPInst(getOperand(0), getType()); | |||
4511 | } | |||
4512 | ||||
4513 | SIToFPInst *SIToFPInst::cloneImpl() const { | |||
4514 | return new SIToFPInst(getOperand(0), getType()); | |||
4515 | } | |||
4516 | ||||
4517 | FPToUIInst *FPToUIInst::cloneImpl() const { | |||
4518 | return new FPToUIInst(getOperand(0), getType()); | |||
4519 | } | |||
4520 | ||||
4521 | FPToSIInst *FPToSIInst::cloneImpl() const { | |||
4522 | return new FPToSIInst(getOperand(0), getType()); | |||
4523 | } | |||
4524 | ||||
4525 | PtrToIntInst *PtrToIntInst::cloneImpl() const { | |||
4526 | return new PtrToIntInst(getOperand(0), getType()); | |||
4527 | } | |||
4528 | ||||
4529 | IntToPtrInst *IntToPtrInst::cloneImpl() const { | |||
4530 | return new IntToPtrInst(getOperand(0), getType()); | |||
4531 | } | |||
4532 | ||||
4533 | BitCastInst *BitCastInst::cloneImpl() const { | |||
4534 | return new BitCastInst(getOperand(0), getType()); | |||
4535 | } | |||
4536 | ||||
4537 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { | |||
4538 | return new AddrSpaceCastInst(getOperand(0), getType()); | |||
4539 | } | |||
4540 | ||||
4541 | CallInst *CallInst::cloneImpl() const { | |||
4542 | if (hasOperandBundles()) { | |||
4543 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4544 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); | |||
4545 | } | |||
4546 | return new(getNumOperands()) CallInst(*this); | |||
4547 | } | |||
4548 | ||||
4549 | SelectInst *SelectInst::cloneImpl() const { | |||
4550 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); | |||
4551 | } | |||
4552 | ||||
4553 | VAArgInst *VAArgInst::cloneImpl() const { | |||
4554 | return new VAArgInst(getOperand(0), getType()); | |||
4555 | } | |||
4556 | ||||
4557 | ExtractElementInst *ExtractElementInst::cloneImpl() const { | |||
4558 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); | |||
4559 | } | |||
4560 | ||||
4561 | InsertElementInst *InsertElementInst::cloneImpl() const { | |||
4562 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); | |||
4563 | } | |||
4564 | ||||
4565 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { | |||
4566 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getShuffleMask()); | |||
4567 | } | |||
4568 | ||||
4569 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } | |||
4570 | ||||
4571 | LandingPadInst *LandingPadInst::cloneImpl() const { | |||
4572 | return new LandingPadInst(*this); | |||
4573 | } | |||
4574 | ||||
4575 | ReturnInst *ReturnInst::cloneImpl() const { | |||
4576 | return new(getNumOperands()) ReturnInst(*this); | |||
4577 | } | |||
4578 | ||||
4579 | BranchInst *BranchInst::cloneImpl() const { | |||
4580 | return new(getNumOperands()) BranchInst(*this); | |||
4581 | } | |||
4582 | ||||
4583 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } | |||
4584 | ||||
4585 | IndirectBrInst *IndirectBrInst::cloneImpl() const { | |||
4586 | return new IndirectBrInst(*this); | |||
4587 | } | |||
4588 | ||||
4589 | InvokeInst *InvokeInst::cloneImpl() const { | |||
4590 | if (hasOperandBundles()) { | |||
4591 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4592 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); | |||
4593 | } | |||
4594 | return new(getNumOperands()) InvokeInst(*this); | |||
4595 | } | |||
4596 | ||||
4597 | CallBrInst *CallBrInst::cloneImpl() const { | |||
4598 | if (hasOperandBundles()) { | |||
4599 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4600 | return new (getNumOperands(), DescriptorBytes) CallBrInst(*this); | |||
4601 | } | |||
4602 | return new (getNumOperands()) CallBrInst(*this); | |||
4603 | } | |||
4604 | ||||
4605 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } | |||
4606 | ||||
4607 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { | |||
4608 | return new (getNumOperands()) CleanupReturnInst(*this); | |||
4609 | } | |||
4610 | ||||
4611 | CatchReturnInst *CatchReturnInst::cloneImpl() const { | |||
4612 | return new (getNumOperands()) CatchReturnInst(*this); | |||
4613 | } | |||
4614 | ||||
4615 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { | |||
4616 | return new CatchSwitchInst(*this); | |||
4617 | } | |||
4618 | ||||
4619 | FuncletPadInst *FuncletPadInst::cloneImpl() const { | |||
4620 | return new (getNumOperands()) FuncletPadInst(*this); | |||
4621 | } | |||
4622 | ||||
4623 | UnreachableInst *UnreachableInst::cloneImpl() const { | |||
4624 | LLVMContext &Context = getContext(); | |||
4625 | return new UnreachableInst(Context); | |||
4626 | } | |||
4627 | ||||
4628 | FreezeInst *FreezeInst::cloneImpl() const { | |||
4629 | return new FreezeInst(getOperand(0)); | |||
4630 | } |
1 | //===- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*-===// |
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 defines various meta classes of instructions that exist in the VM |
10 | // representation. Specific concrete subclasses of these may be found in the |
11 | // i*.h files... |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_IR_INSTRTYPES_H |
16 | #define LLVM_IR_INSTRTYPES_H |
17 | |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/None.h" |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/STLExtras.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringRef.h" |
24 | #include "llvm/ADT/Twine.h" |
25 | #include "llvm/ADT/iterator_range.h" |
26 | #include "llvm/IR/Attributes.h" |
27 | #include "llvm/IR/CallingConv.h" |
28 | #include "llvm/IR/Constants.h" |
29 | #include "llvm/IR/DerivedTypes.h" |
30 | #include "llvm/IR/Function.h" |
31 | #include "llvm/IR/Instruction.h" |
32 | #include "llvm/IR/LLVMContext.h" |
33 | #include "llvm/IR/OperandTraits.h" |
34 | #include "llvm/IR/Type.h" |
35 | #include "llvm/IR/User.h" |
36 | #include "llvm/IR/Value.h" |
37 | #include "llvm/Support/Casting.h" |
38 | #include "llvm/Support/ErrorHandling.h" |
39 | #include <algorithm> |
40 | #include <cassert> |
41 | #include <cstddef> |
42 | #include <cstdint> |
43 | #include <iterator> |
44 | #include <string> |
45 | #include <vector> |
46 | |
47 | namespace llvm { |
48 | |
49 | namespace Intrinsic { |
50 | typedef unsigned ID; |
51 | } |
52 | |
53 | //===----------------------------------------------------------------------===// |
54 | // UnaryInstruction Class |
55 | //===----------------------------------------------------------------------===// |
56 | |
57 | class UnaryInstruction : public Instruction { |
58 | protected: |
59 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, |
60 | Instruction *IB = nullptr) |
61 | : Instruction(Ty, iType, &Op<0>(), 1, IB) { |
62 | Op<0>() = V; |
63 | } |
64 | UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE) |
65 | : Instruction(Ty, iType, &Op<0>(), 1, IAE) { |
66 | Op<0>() = V; |
67 | } |
68 | |
69 | public: |
70 | // allocate space for exactly one operand |
71 | void *operator new(size_t S) { return User::operator new(S, 1); } |
72 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
73 | |
74 | /// Transparently provide more efficient getOperand methods. |
75 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
76 | |
77 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
78 | static bool classof(const Instruction *I) { |
79 | return I->isUnaryOp() || |
80 | I->getOpcode() == Instruction::Alloca || |
81 | I->getOpcode() == Instruction::Load || |
82 | I->getOpcode() == Instruction::VAArg || |
83 | I->getOpcode() == Instruction::ExtractValue || |
84 | (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd); |
85 | } |
86 | static bool classof(const Value *V) { |
87 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
88 | } |
89 | }; |
90 | |
91 | template <> |
92 | struct OperandTraits<UnaryInstruction> : |
93 | public FixedNumOperandTraits<UnaryInstruction, 1> { |
94 | }; |
95 | |
96 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)UnaryInstruction::op_iterator UnaryInstruction::op_begin() { return OperandTraits<UnaryInstruction>::op_begin(this); } UnaryInstruction ::const_op_iterator UnaryInstruction::op_begin() const { return OperandTraits<UnaryInstruction>::op_begin(const_cast< UnaryInstruction*>(this)); } UnaryInstruction::op_iterator UnaryInstruction::op_end() { return OperandTraits<UnaryInstruction >::op_end(this); } UnaryInstruction::const_op_iterator UnaryInstruction ::op_end() const { return OperandTraits<UnaryInstruction> ::op_end(const_cast<UnaryInstruction*>(this)); } Value * UnaryInstruction::getOperand(unsigned i_nocapture) const { (static_cast <void> (0)); return cast_or_null<Value>( OperandTraits <UnaryInstruction>::op_begin(const_cast<UnaryInstruction *>(this))[i_nocapture].get()); } void UnaryInstruction::setOperand (unsigned i_nocapture, Value *Val_nocapture) { (static_cast< void> (0)); OperandTraits<UnaryInstruction>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned UnaryInstruction ::getNumOperands() const { return OperandTraits<UnaryInstruction >::operands(this); } template <int Idx_nocapture> Use &UnaryInstruction::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & UnaryInstruction::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
97 | |
98 | //===----------------------------------------------------------------------===// |
99 | // UnaryOperator Class |
100 | //===----------------------------------------------------------------------===// |
101 | |
102 | class UnaryOperator : public UnaryInstruction { |
103 | void AssertOK(); |
104 | |
105 | protected: |
106 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
107 | const Twine &Name, Instruction *InsertBefore); |
108 | UnaryOperator(UnaryOps iType, Value *S, Type *Ty, |
109 | const Twine &Name, BasicBlock *InsertAtEnd); |
110 | |
111 | // Note: Instruction needs to be a friend here to call cloneImpl. |
112 | friend class Instruction; |
113 | |
114 | UnaryOperator *cloneImpl() const; |
115 | |
116 | public: |
117 | |
118 | /// Construct a unary instruction, given the opcode and an operand. |
119 | /// Optionally (if InstBefore is specified) insert the instruction |
120 | /// into a BasicBlock right before the specified instruction. The specified |
121 | /// Instruction is allowed to be a dereferenced end iterator. |
122 | /// |
123 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
124 | const Twine &Name = Twine(), |
125 | Instruction *InsertBefore = nullptr); |
126 | |
127 | /// Construct a unary instruction, given the opcode and an operand. |
128 | /// Also automatically insert this instruction to the end of the |
129 | /// BasicBlock specified. |
130 | /// |
131 | static UnaryOperator *Create(UnaryOps Op, Value *S, |
132 | const Twine &Name, |
133 | BasicBlock *InsertAtEnd); |
134 | |
135 | /// These methods just forward to Create, and are useful when you |
136 | /// statically know what type of instruction you're going to create. These |
137 | /// helpers just save some typing. |
138 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
139 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name = "") {\ |
140 | return Create(Instruction::OPC, V, Name);\ |
141 | } |
142 | #include "llvm/IR/Instruction.def" |
143 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
144 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
145 | BasicBlock *BB) {\ |
146 | return Create(Instruction::OPC, V, Name, BB);\ |
147 | } |
148 | #include "llvm/IR/Instruction.def" |
149 | #define HANDLE_UNARY_INST(N, OPC, CLASS) \ |
150 | static UnaryOperator *Create##OPC(Value *V, const Twine &Name, \ |
151 | Instruction *I) {\ |
152 | return Create(Instruction::OPC, V, Name, I);\ |
153 | } |
154 | #include "llvm/IR/Instruction.def" |
155 | |
156 | static UnaryOperator * |
157 | CreateWithCopiedFlags(UnaryOps Opc, Value *V, Instruction *CopyO, |
158 | const Twine &Name = "", |
159 | Instruction *InsertBefore = nullptr) { |
160 | UnaryOperator *UO = Create(Opc, V, Name, InsertBefore); |
161 | UO->copyIRFlags(CopyO); |
162 | return UO; |
163 | } |
164 | |
165 | static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource, |
166 | const Twine &Name = "", |
167 | Instruction *InsertBefore = nullptr) { |
168 | return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name, |
169 | InsertBefore); |
170 | } |
171 | |
172 | UnaryOps getOpcode() const { |
173 | return static_cast<UnaryOps>(Instruction::getOpcode()); |
174 | } |
175 | |
176 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
177 | static bool classof(const Instruction *I) { |
178 | return I->isUnaryOp(); |
179 | } |
180 | static bool classof(const Value *V) { |
181 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
182 | } |
183 | }; |
184 | |
185 | //===----------------------------------------------------------------------===// |
186 | // BinaryOperator Class |
187 | //===----------------------------------------------------------------------===// |
188 | |
189 | class BinaryOperator : public Instruction { |
190 | void AssertOK(); |
191 | |
192 | protected: |
193 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
194 | const Twine &Name, Instruction *InsertBefore); |
195 | BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, |
196 | const Twine &Name, BasicBlock *InsertAtEnd); |
197 | |
198 | // Note: Instruction needs to be a friend here to call cloneImpl. |
199 | friend class Instruction; |
200 | |
201 | BinaryOperator *cloneImpl() const; |
202 | |
203 | public: |
204 | // allocate space for exactly two operands |
205 | void *operator new(size_t S) { return User::operator new(S, 2); } |
206 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
207 | |
208 | /// Transparently provide more efficient getOperand methods. |
209 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
210 | |
211 | /// Construct a binary instruction, given the opcode and the two |
212 | /// operands. Optionally (if InstBefore is specified) insert the instruction |
213 | /// into a BasicBlock right before the specified instruction. The specified |
214 | /// Instruction is allowed to be a dereferenced end iterator. |
215 | /// |
216 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
217 | const Twine &Name = Twine(), |
218 | Instruction *InsertBefore = nullptr); |
219 | |
220 | /// Construct a binary instruction, given the opcode and the two |
221 | /// operands. Also automatically insert this instruction to the end of the |
222 | /// BasicBlock specified. |
223 | /// |
224 | static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2, |
225 | const Twine &Name, BasicBlock *InsertAtEnd); |
226 | |
227 | /// These methods just forward to Create, and are useful when you |
228 | /// statically know what type of instruction you're going to create. These |
229 | /// helpers just save some typing. |
230 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
231 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
232 | const Twine &Name = "") {\ |
233 | return Create(Instruction::OPC, V1, V2, Name);\ |
234 | } |
235 | #include "llvm/IR/Instruction.def" |
236 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
237 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
238 | const Twine &Name, BasicBlock *BB) {\ |
239 | return Create(Instruction::OPC, V1, V2, Name, BB);\ |
240 | } |
241 | #include "llvm/IR/Instruction.def" |
242 | #define HANDLE_BINARY_INST(N, OPC, CLASS) \ |
243 | static BinaryOperator *Create##OPC(Value *V1, Value *V2, \ |
244 | const Twine &Name, Instruction *I) {\ |
245 | return Create(Instruction::OPC, V1, V2, Name, I);\ |
246 | } |
247 | #include "llvm/IR/Instruction.def" |
248 | |
249 | static BinaryOperator * |
250 | CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Instruction *CopyO, |
251 | const Twine &Name = "", |
252 | Instruction *InsertBefore = nullptr) { |
253 | BinaryOperator *BO = Create(Opc, V1, V2, Name, InsertBefore); |
254 | BO->copyIRFlags(CopyO); |
255 | return BO; |
256 | } |
257 | |
258 | static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2, |
259 | Instruction *FMFSource, |
260 | const Twine &Name = "") { |
261 | return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name); |
262 | } |
263 | static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2, |
264 | Instruction *FMFSource, |
265 | const Twine &Name = "") { |
266 | return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name); |
267 | } |
268 | static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2, |
269 | Instruction *FMFSource, |
270 | const Twine &Name = "") { |
271 | return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name); |
272 | } |
273 | static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2, |
274 | Instruction *FMFSource, |
275 | const Twine &Name = "") { |
276 | return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name); |
277 | } |
278 | static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2, |
279 | Instruction *FMFSource, |
280 | const Twine &Name = "") { |
281 | return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name); |
282 | } |
283 | |
284 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
285 | const Twine &Name = "") { |
286 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
287 | BO->setHasNoSignedWrap(true); |
288 | return BO; |
289 | } |
290 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
291 | const Twine &Name, BasicBlock *BB) { |
292 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
293 | BO->setHasNoSignedWrap(true); |
294 | return BO; |
295 | } |
296 | static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2, |
297 | const Twine &Name, Instruction *I) { |
298 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
299 | BO->setHasNoSignedWrap(true); |
300 | return BO; |
301 | } |
302 | |
303 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
304 | const Twine &Name = "") { |
305 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
306 | BO->setHasNoUnsignedWrap(true); |
307 | return BO; |
308 | } |
309 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
310 | const Twine &Name, BasicBlock *BB) { |
311 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
312 | BO->setHasNoUnsignedWrap(true); |
313 | return BO; |
314 | } |
315 | static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2, |
316 | const Twine &Name, Instruction *I) { |
317 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
318 | BO->setHasNoUnsignedWrap(true); |
319 | return BO; |
320 | } |
321 | |
322 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
323 | const Twine &Name = "") { |
324 | BinaryOperator *BO = Create(Opc, V1, V2, Name); |
325 | BO->setIsExact(true); |
326 | return BO; |
327 | } |
328 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
329 | const Twine &Name, BasicBlock *BB) { |
330 | BinaryOperator *BO = Create(Opc, V1, V2, Name, BB); |
331 | BO->setIsExact(true); |
332 | return BO; |
333 | } |
334 | static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2, |
335 | const Twine &Name, Instruction *I) { |
336 | BinaryOperator *BO = Create(Opc, V1, V2, Name, I); |
337 | BO->setIsExact(true); |
338 | return BO; |
339 | } |
340 | |
341 | #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \ |
342 | static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \ |
343 | const Twine &Name = "") { \ |
344 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \ |
345 | } \ |
346 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
347 | Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \ |
348 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \ |
349 | } \ |
350 | static BinaryOperator *Create##NUWNSWEXACT##OPC( \ |
351 | Value *V1, Value *V2, const Twine &Name, Instruction *I) { \ |
352 | return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \ |
353 | } |
354 | |
355 | DEFINE_HELPERS(Add, NSW) // CreateNSWAdd |
356 | DEFINE_HELPERS(Add, NUW) // CreateNUWAdd |
357 | DEFINE_HELPERS(Sub, NSW) // CreateNSWSub |
358 | DEFINE_HELPERS(Sub, NUW) // CreateNUWSub |
359 | DEFINE_HELPERS(Mul, NSW) // CreateNSWMul |
360 | DEFINE_HELPERS(Mul, NUW) // CreateNUWMul |
361 | DEFINE_HELPERS(Shl, NSW) // CreateNSWShl |
362 | DEFINE_HELPERS(Shl, NUW) // CreateNUWShl |
363 | |
364 | DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv |
365 | DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv |
366 | DEFINE_HELPERS(AShr, Exact) // CreateExactAShr |
367 | DEFINE_HELPERS(LShr, Exact) // CreateExactLShr |
368 | |
369 | #undef DEFINE_HELPERS |
370 | |
371 | /// Helper functions to construct and inspect unary operations (NEG and NOT) |
372 | /// via binary operators SUB and XOR: |
373 | /// |
374 | /// Create the NEG and NOT instructions out of SUB and XOR instructions. |
375 | /// |
376 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "", |
377 | Instruction *InsertBefore = nullptr); |
378 | static BinaryOperator *CreateNeg(Value *Op, const Twine &Name, |
379 | BasicBlock *InsertAtEnd); |
380 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "", |
381 | Instruction *InsertBefore = nullptr); |
382 | static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name, |
383 | BasicBlock *InsertAtEnd); |
384 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "", |
385 | Instruction *InsertBefore = nullptr); |
386 | static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name, |
387 | BasicBlock *InsertAtEnd); |
388 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "", |
389 | Instruction *InsertBefore = nullptr); |
390 | static BinaryOperator *CreateNot(Value *Op, const Twine &Name, |
391 | BasicBlock *InsertAtEnd); |
392 | |
393 | BinaryOps getOpcode() const { |
394 | return static_cast<BinaryOps>(Instruction::getOpcode()); |
395 | } |
396 | |
397 | /// Exchange the two operands to this instruction. |
398 | /// This instruction is safe to use on any binary instruction and |
399 | /// does not modify the semantics of the instruction. If the instruction |
400 | /// cannot be reversed (ie, it's a Div), then return true. |
401 | /// |
402 | bool swapOperands(); |
403 | |
404 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
405 | static bool classof(const Instruction *I) { |
406 | return I->isBinaryOp(); |
407 | } |
408 | static bool classof(const Value *V) { |
409 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
410 | } |
411 | }; |
412 | |
413 | template <> |
414 | struct OperandTraits<BinaryOperator> : |
415 | public FixedNumOperandTraits<BinaryOperator, 2> { |
416 | }; |
417 | |
418 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)BinaryOperator::op_iterator BinaryOperator::op_begin() { return OperandTraits<BinaryOperator>::op_begin(this); } BinaryOperator ::const_op_iterator BinaryOperator::op_begin() const { return OperandTraits<BinaryOperator>::op_begin(const_cast< BinaryOperator*>(this)); } BinaryOperator::op_iterator BinaryOperator ::op_end() { return OperandTraits<BinaryOperator>::op_end (this); } BinaryOperator::const_op_iterator BinaryOperator::op_end () const { return OperandTraits<BinaryOperator>::op_end (const_cast<BinaryOperator*>(this)); } Value *BinaryOperator ::getOperand(unsigned i_nocapture) const { (static_cast<void > (0)); return cast_or_null<Value>( OperandTraits< BinaryOperator>::op_begin(const_cast<BinaryOperator*> (this))[i_nocapture].get()); } void BinaryOperator::setOperand (unsigned i_nocapture, Value *Val_nocapture) { (static_cast< void> (0)); OperandTraits<BinaryOperator>::op_begin( this)[i_nocapture] = Val_nocapture; } unsigned BinaryOperator ::getNumOperands() const { return OperandTraits<BinaryOperator >::operands(this); } template <int Idx_nocapture> Use &BinaryOperator::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & BinaryOperator::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
419 | |
420 | //===----------------------------------------------------------------------===// |
421 | // CastInst Class |
422 | //===----------------------------------------------------------------------===// |
423 | |
424 | /// This is the base class for all instructions that perform data |
425 | /// casts. It is simply provided so that instruction category testing |
426 | /// can be performed with code like: |
427 | /// |
428 | /// if (isa<CastInst>(Instr)) { ... } |
429 | /// Base class of casting instructions. |
430 | class CastInst : public UnaryInstruction { |
431 | protected: |
432 | /// Constructor with insert-before-instruction semantics for subclasses |
433 | CastInst(Type *Ty, unsigned iType, Value *S, |
434 | const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
435 | : UnaryInstruction(Ty, iType, S, InsertBefore) { |
436 | setName(NameStr); |
437 | } |
438 | /// Constructor with insert-at-end-of-block semantics for subclasses |
439 | CastInst(Type *Ty, unsigned iType, Value *S, |
440 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
441 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { |
442 | setName(NameStr); |
443 | } |
444 | |
445 | public: |
446 | /// Provides a way to construct any of the CastInst subclasses using an |
447 | /// opcode instead of the subclass's constructor. The opcode must be in the |
448 | /// CastOps category (Instruction::isCast(opcode) returns true). This |
449 | /// constructor has insert-before-instruction semantics to automatically |
450 | /// insert the new CastInst before InsertBefore (if it is non-null). |
451 | /// Construct any of the CastInst subclasses |
452 | static CastInst *Create( |
453 | Instruction::CastOps, ///< The opcode of the cast instruction |
454 | Value *S, ///< The value to be casted (operand 0) |
455 | Type *Ty, ///< The type to which cast should be made |
456 | const Twine &Name = "", ///< Name for the instruction |
457 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
458 | ); |
459 | /// Provides a way to construct any of the CastInst subclasses using an |
460 | /// opcode instead of the subclass's constructor. The opcode must be in the |
461 | /// CastOps category. This constructor has insert-at-end-of-block semantics |
462 | /// to automatically insert the new CastInst at the end of InsertAtEnd (if |
463 | /// its non-null). |
464 | /// Construct any of the CastInst subclasses |
465 | static CastInst *Create( |
466 | Instruction::CastOps, ///< The opcode for the cast instruction |
467 | Value *S, ///< The value to be casted (operand 0) |
468 | Type *Ty, ///< The type to which operand is casted |
469 | const Twine &Name, ///< The name for the instruction |
470 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
471 | ); |
472 | |
473 | /// Create a ZExt or BitCast cast instruction |
474 | static CastInst *CreateZExtOrBitCast( |
475 | Value *S, ///< The value to be casted (operand 0) |
476 | Type *Ty, ///< The type to which cast should be made |
477 | const Twine &Name = "", ///< Name for the instruction |
478 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
479 | ); |
480 | |
481 | /// Create a ZExt or BitCast cast instruction |
482 | static CastInst *CreateZExtOrBitCast( |
483 | Value *S, ///< The value to be casted (operand 0) |
484 | Type *Ty, ///< The type to which operand is casted |
485 | const Twine &Name, ///< The name for the instruction |
486 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
487 | ); |
488 | |
489 | /// Create a SExt or BitCast cast instruction |
490 | static CastInst *CreateSExtOrBitCast( |
491 | Value *S, ///< The value to be casted (operand 0) |
492 | Type *Ty, ///< The type to which cast should be made |
493 | const Twine &Name = "", ///< Name for the instruction |
494 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
495 | ); |
496 | |
497 | /// Create a SExt or BitCast cast instruction |
498 | static CastInst *CreateSExtOrBitCast( |
499 | Value *S, ///< The value to be casted (operand 0) |
500 | Type *Ty, ///< The type to which operand is casted |
501 | const Twine &Name, ///< The name for the instruction |
502 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
503 | ); |
504 | |
505 | /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction. |
506 | static CastInst *CreatePointerCast( |
507 | Value *S, ///< The pointer value to be casted (operand 0) |
508 | Type *Ty, ///< The type to which operand is casted |
509 | const Twine &Name, ///< The name for the instruction |
510 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
511 | ); |
512 | |
513 | /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction. |
514 | static CastInst *CreatePointerCast( |
515 | Value *S, ///< The pointer value to be casted (operand 0) |
516 | Type *Ty, ///< The type to which cast should be made |
517 | const Twine &Name = "", ///< Name for the instruction |
518 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
519 | ); |
520 | |
521 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
522 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
523 | Value *S, ///< The pointer value to be casted (operand 0) |
524 | Type *Ty, ///< The type to which operand is casted |
525 | const Twine &Name, ///< The name for the instruction |
526 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
527 | ); |
528 | |
529 | /// Create a BitCast or an AddrSpaceCast cast instruction. |
530 | static CastInst *CreatePointerBitCastOrAddrSpaceCast( |
531 | Value *S, ///< The pointer value to be casted (operand 0) |
532 | Type *Ty, ///< The type to which cast should be made |
533 | const Twine &Name = "", ///< Name for the instruction |
534 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
535 | ); |
536 | |
537 | /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction. |
538 | /// |
539 | /// If the value is a pointer type and the destination an integer type, |
540 | /// creates a PtrToInt cast. If the value is an integer type and the |
541 | /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates |
542 | /// a bitcast. |
543 | static CastInst *CreateBitOrPointerCast( |
544 | Value *S, ///< The pointer value to be casted (operand 0) |
545 | Type *Ty, ///< The type to which cast should be made |
546 | const Twine &Name = "", ///< Name for the instruction |
547 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
548 | ); |
549 | |
550 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
551 | static CastInst *CreateIntegerCast( |
552 | Value *S, ///< The pointer value to be casted (operand 0) |
553 | Type *Ty, ///< The type to which cast should be made |
554 | bool isSigned, ///< Whether to regard S as signed or not |
555 | const Twine &Name = "", ///< Name for the instruction |
556 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
557 | ); |
558 | |
559 | /// Create a ZExt, BitCast, or Trunc for int -> int casts. |
560 | static CastInst *CreateIntegerCast( |
561 | Value *S, ///< The integer value to be casted (operand 0) |
562 | Type *Ty, ///< The integer type to which operand is casted |
563 | bool isSigned, ///< Whether to regard S as signed or not |
564 | const Twine &Name, ///< The name for the instruction |
565 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
566 | ); |
567 | |
568 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
569 | static CastInst *CreateFPCast( |
570 | Value *S, ///< The floating point value to be casted |
571 | Type *Ty, ///< The floating point type to cast to |
572 | const Twine &Name = "", ///< Name for the instruction |
573 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
574 | ); |
575 | |
576 | /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts |
577 | static CastInst *CreateFPCast( |
578 | Value *S, ///< The floating point value to be casted |
579 | Type *Ty, ///< The floating point type to cast to |
580 | const Twine &Name, ///< The name for the instruction |
581 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
582 | ); |
583 | |
584 | /// Create a Trunc or BitCast cast instruction |
585 | static CastInst *CreateTruncOrBitCast( |
586 | Value *S, ///< The value to be casted (operand 0) |
587 | Type *Ty, ///< The type to which cast should be made |
588 | const Twine &Name = "", ///< Name for the instruction |
589 | Instruction *InsertBefore = nullptr ///< Place to insert the instruction |
590 | ); |
591 | |
592 | /// Create a Trunc or BitCast cast instruction |
593 | static CastInst *CreateTruncOrBitCast( |
594 | Value *S, ///< The value to be casted (operand 0) |
595 | Type *Ty, ///< The type to which operand is casted |
596 | const Twine &Name, ///< The name for the instruction |
597 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
598 | ); |
599 | |
600 | /// Check whether a bitcast between these types is valid |
601 | static bool isBitCastable( |
602 | Type *SrcTy, ///< The Type from which the value should be cast. |
603 | Type *DestTy ///< The Type to which the value should be cast. |
604 | ); |
605 | |
606 | /// Check whether a bitcast, inttoptr, or ptrtoint cast between these |
607 | /// types is valid and a no-op. |
608 | /// |
609 | /// This ensures that any pointer<->integer cast has enough bits in the |
610 | /// integer and any other cast is a bitcast. |
611 | static bool isBitOrNoopPointerCastable( |
612 | Type *SrcTy, ///< The Type from which the value should be cast. |
613 | Type *DestTy, ///< The Type to which the value should be cast. |
614 | const DataLayout &DL); |
615 | |
616 | /// Returns the opcode necessary to cast Val into Ty using usual casting |
617 | /// rules. |
618 | /// Infer the opcode for cast operand and type |
619 | static Instruction::CastOps getCastOpcode( |
620 | const Value *Val, ///< The value to cast |
621 | bool SrcIsSigned, ///< Whether to treat the source as signed |
622 | Type *Ty, ///< The Type to which the value should be casted |
623 | bool DstIsSigned ///< Whether to treate the dest. as signed |
624 | ); |
625 | |
626 | /// There are several places where we need to know if a cast instruction |
627 | /// only deals with integer source and destination types. To simplify that |
628 | /// logic, this method is provided. |
629 | /// @returns true iff the cast has only integral typed operand and dest type. |
630 | /// Determine if this is an integer-only cast. |
631 | bool isIntegerCast() const; |
632 | |
633 | /// A lossless cast is one that does not alter the basic value. It implies |
634 | /// a no-op cast but is more stringent, preventing things like int->float, |
635 | /// long->double, or int->ptr. |
636 | /// @returns true iff the cast is lossless. |
637 | /// Determine if this is a lossless cast. |
638 | bool isLosslessCast() const; |
639 | |
640 | /// A no-op cast is one that can be effected without changing any bits. |
641 | /// It implies that the source and destination types are the same size. The |
642 | /// DataLayout argument is to determine the pointer size when examining casts |
643 | /// involving Integer and Pointer types. They are no-op casts if the integer |
644 | /// is the same size as the pointer. However, pointer size varies with |
645 | /// platform. Note that a precondition of this method is that the cast is |
646 | /// legal - i.e. the instruction formed with these operands would verify. |
647 | static bool isNoopCast( |
648 | Instruction::CastOps Opcode, ///< Opcode of cast |
649 | Type *SrcTy, ///< SrcTy of cast |
650 | Type *DstTy, ///< DstTy of cast |
651 | const DataLayout &DL ///< DataLayout to get the Int Ptr type from. |
652 | ); |
653 | |
654 | /// Determine if this cast is a no-op cast. |
655 | /// |
656 | /// \param DL is the DataLayout to determine pointer size. |
657 | bool isNoopCast(const DataLayout &DL) const; |
658 | |
659 | /// Determine how a pair of casts can be eliminated, if they can be at all. |
660 | /// This is a helper function for both CastInst and ConstantExpr. |
661 | /// @returns 0 if the CastInst pair can't be eliminated, otherwise |
662 | /// returns Instruction::CastOps value for a cast that can replace |
663 | /// the pair, casting SrcTy to DstTy. |
664 | /// Determine if a cast pair is eliminable |
665 | static unsigned isEliminableCastPair( |
666 | Instruction::CastOps firstOpcode, ///< Opcode of first cast |
667 | Instruction::CastOps secondOpcode, ///< Opcode of second cast |
668 | Type *SrcTy, ///< SrcTy of 1st cast |
669 | Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast |
670 | Type *DstTy, ///< DstTy of 2nd cast |
671 | Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null |
672 | Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null |
673 | Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null |
674 | ); |
675 | |
676 | /// Return the opcode of this CastInst |
677 | Instruction::CastOps getOpcode() const { |
678 | return Instruction::CastOps(Instruction::getOpcode()); |
679 | } |
680 | |
681 | /// Return the source type, as a convenience |
682 | Type* getSrcTy() const { return getOperand(0)->getType(); } |
683 | /// Return the destination type, as a convenience |
684 | Type* getDestTy() const { return getType(); } |
685 | |
686 | /// This method can be used to determine if a cast from SrcTy to DstTy using |
687 | /// Opcode op is valid or not. |
688 | /// @returns true iff the proposed cast is valid. |
689 | /// Determine if a cast is valid without creating one. |
690 | static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy); |
691 | static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
692 | return castIsValid(op, S->getType(), DstTy); |
693 | } |
694 | |
695 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
696 | static bool classof(const Instruction *I) { |
697 | return I->isCast(); |
698 | } |
699 | static bool classof(const Value *V) { |
700 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
701 | } |
702 | }; |
703 | |
704 | //===----------------------------------------------------------------------===// |
705 | // CmpInst Class |
706 | //===----------------------------------------------------------------------===// |
707 | |
708 | /// This class is the base class for the comparison instructions. |
709 | /// Abstract base class of comparison instructions. |
710 | class CmpInst : public Instruction { |
711 | public: |
712 | /// This enumeration lists the possible predicates for CmpInst subclasses. |
713 | /// Values in the range 0-31 are reserved for FCmpInst, while values in the |
714 | /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the |
715 | /// predicate values are not overlapping between the classes. |
716 | /// |
717 | /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of |
718 | /// FCMP_* values. Changing the bit patterns requires a potential change to |
719 | /// those passes. |
720 | enum Predicate : unsigned { |
721 | // Opcode U L G E Intuitive operation |
722 | FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded) |
723 | FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal |
724 | FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than |
725 | FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal |
726 | FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than |
727 | FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal |
728 | FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal |
729 | FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans) |
730 | FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y) |
731 | FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal |
732 | FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than |
733 | FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal |
734 | FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than |
735 | FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal |
736 | FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal |
737 | FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded) |
738 | FIRST_FCMP_PREDICATE = FCMP_FALSE, |
739 | LAST_FCMP_PREDICATE = FCMP_TRUE, |
740 | BAD_FCMP_PREDICATE = FCMP_TRUE + 1, |
741 | ICMP_EQ = 32, ///< equal |
742 | ICMP_NE = 33, ///< not equal |
743 | ICMP_UGT = 34, ///< unsigned greater than |
744 | ICMP_UGE = 35, ///< unsigned greater or equal |
745 | ICMP_ULT = 36, ///< unsigned less than |
746 | ICMP_ULE = 37, ///< unsigned less or equal |
747 | ICMP_SGT = 38, ///< signed greater than |
748 | ICMP_SGE = 39, ///< signed greater or equal |
749 | ICMP_SLT = 40, ///< signed less than |
750 | ICMP_SLE = 41, ///< signed less or equal |
751 | FIRST_ICMP_PREDICATE = ICMP_EQ, |
752 | LAST_ICMP_PREDICATE = ICMP_SLE, |
753 | BAD_ICMP_PREDICATE = ICMP_SLE + 1 |
754 | }; |
755 | using PredicateField = |
756 | Bitfield::Element<Predicate, 0, 6, LAST_ICMP_PREDICATE>; |
757 | |
758 | protected: |
759 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
760 | Value *LHS, Value *RHS, const Twine &Name = "", |
761 | Instruction *InsertBefore = nullptr, |
762 | Instruction *FlagsSource = nullptr); |
763 | |
764 | CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred, |
765 | Value *LHS, Value *RHS, const Twine &Name, |
766 | BasicBlock *InsertAtEnd); |
767 | |
768 | public: |
769 | // allocate space for exactly two operands |
770 | void *operator new(size_t S) { return User::operator new(S, 2); } |
771 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
772 | |
773 | /// Construct a compare instruction, given the opcode, the predicate and |
774 | /// the two operands. Optionally (if InstBefore is specified) insert the |
775 | /// instruction into a BasicBlock right before the specified instruction. |
776 | /// The specified Instruction is allowed to be a dereferenced end iterator. |
777 | /// Create a CmpInst |
778 | static CmpInst *Create(OtherOps Op, |
779 | Predicate predicate, Value *S1, |
780 | Value *S2, const Twine &Name = "", |
781 | Instruction *InsertBefore = nullptr); |
782 | |
783 | /// Construct a compare instruction, given the opcode, the predicate and the |
784 | /// two operands. Also automatically insert this instruction to the end of |
785 | /// the BasicBlock specified. |
786 | /// Create a CmpInst |
787 | static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1, |
788 | Value *S2, const Twine &Name, BasicBlock *InsertAtEnd); |
789 | |
790 | /// Get the opcode casted to the right type |
791 | OtherOps getOpcode() const { |
792 | return static_cast<OtherOps>(Instruction::getOpcode()); |
793 | } |
794 | |
795 | /// Return the predicate for this instruction. |
796 | Predicate getPredicate() const { return getSubclassData<PredicateField>(); } |
797 | |
798 | /// Set the predicate for this instruction to the specified value. |
799 | void setPredicate(Predicate P) { setSubclassData<PredicateField>(P); } |
800 | |
801 | static bool isFPPredicate(Predicate P) { |
802 | static_assert(FIRST_FCMP_PREDICATE == 0, |
803 | "FIRST_FCMP_PREDICATE is required to be 0"); |
804 | return P <= LAST_FCMP_PREDICATE; |
805 | } |
806 | |
807 | static bool isIntPredicate(Predicate P) { |
808 | return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE; |
809 | } |
810 | |
811 | static StringRef getPredicateName(Predicate P); |
812 | |
813 | bool isFPPredicate() const { return isFPPredicate(getPredicate()); } |
814 | bool isIntPredicate() const { return isIntPredicate(getPredicate()); } |
815 | |
816 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
817 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
818 | /// @returns the inverse predicate for the instruction's current predicate. |
819 | /// Return the inverse of the instruction's predicate. |
820 | Predicate getInversePredicate() const { |
821 | return getInversePredicate(getPredicate()); |
822 | } |
823 | |
824 | /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, |
825 | /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. |
826 | /// @returns the inverse predicate for predicate provided in \p pred. |
827 | /// Return the inverse of a given predicate |
828 | static Predicate getInversePredicate(Predicate pred); |
829 | |
830 | /// For example, EQ->EQ, SLE->SGE, ULT->UGT, |
831 | /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc. |
832 | /// @returns the predicate that would be the result of exchanging the two |
833 | /// operands of the CmpInst instruction without changing the result |
834 | /// produced. |
835 | /// Return the predicate as if the operands were swapped |
836 | Predicate getSwappedPredicate() const { |
837 | return getSwappedPredicate(getPredicate()); |
838 | } |
839 | |
840 | /// This is a static version that you can use without an instruction |
841 | /// available. |
842 | /// Return the predicate as if the operands were swapped. |
843 | static Predicate getSwappedPredicate(Predicate pred); |
844 | |
845 | /// This is a static version that you can use without an instruction |
846 | /// available. |
847 | /// @returns true if the comparison predicate is strict, false otherwise. |
848 | static bool isStrictPredicate(Predicate predicate); |
849 | |
850 | /// @returns true if the comparison predicate is strict, false otherwise. |
851 | /// Determine if this instruction is using an strict comparison predicate. |
852 | bool isStrictPredicate() const { return isStrictPredicate(getPredicate()); } |
853 | |
854 | /// This is a static version that you can use without an instruction |
855 | /// available. |
856 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
857 | static bool isNonStrictPredicate(Predicate predicate); |
858 | |
859 | /// @returns true if the comparison predicate is non-strict, false otherwise. |
860 | /// Determine if this instruction is using an non-strict comparison predicate. |
861 | bool isNonStrictPredicate() const { |
862 | return isNonStrictPredicate(getPredicate()); |
863 | } |
864 | |
865 | /// For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT. |
866 | /// Returns the strict version of non-strict comparisons. |
867 | Predicate getStrictPredicate() const { |
868 | return getStrictPredicate(getPredicate()); |
869 | } |
870 | |
871 | /// This is a static version that you can use without an instruction |
872 | /// available. |
873 | /// @returns the strict version of comparison provided in \p pred. |
874 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
875 | /// Returns the strict version of non-strict comparisons. |
876 | static Predicate getStrictPredicate(Predicate pred); |
877 | |
878 | /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE. |
879 | /// Returns the non-strict version of strict comparisons. |
880 | Predicate getNonStrictPredicate() const { |
881 | return getNonStrictPredicate(getPredicate()); |
882 | } |
883 | |
884 | /// This is a static version that you can use without an instruction |
885 | /// available. |
886 | /// @returns the non-strict version of comparison provided in \p pred. |
887 | /// If \p pred is not a strict comparison predicate, returns \p pred. |
888 | /// Returns the non-strict version of strict comparisons. |
889 | static Predicate getNonStrictPredicate(Predicate pred); |
890 | |
891 | /// This is a static version that you can use without an instruction |
892 | /// available. |
893 | /// Return the flipped strictness of predicate |
894 | static Predicate getFlippedStrictnessPredicate(Predicate pred); |
895 | |
896 | /// For predicate of kind "is X or equal to 0" returns the predicate "is X". |
897 | /// For predicate of kind "is X" returns the predicate "is X or equal to 0". |
898 | /// does not support other kind of predicates. |
899 | /// @returns the predicate that does not contains is equal to zero if |
900 | /// it had and vice versa. |
901 | /// Return the flipped strictness of predicate |
902 | Predicate getFlippedStrictnessPredicate() const { |
903 | return getFlippedStrictnessPredicate(getPredicate()); |
904 | } |
905 | |
906 | /// Provide more efficient getOperand methods. |
907 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
908 | |
909 | /// This is just a convenience that dispatches to the subclasses. |
910 | /// Swap the operands and adjust predicate accordingly to retain |
911 | /// the same comparison. |
912 | void swapOperands(); |
913 | |
914 | /// This is just a convenience that dispatches to the subclasses. |
915 | /// Determine if this CmpInst is commutative. |
916 | bool isCommutative() const; |
917 | |
918 | /// Determine if this is an equals/not equals predicate. |
919 | /// This is a static version that you can use without an instruction |
920 | /// available. |
921 | static bool isEquality(Predicate pred); |
922 | |
923 | /// Determine if this is an equals/not equals predicate. |
924 | bool isEquality() const { return isEquality(getPredicate()); } |
925 | |
926 | /// Return true if the predicate is relational (not EQ or NE). |
927 | static bool isRelational(Predicate P) { return !isEquality(P); } |
928 | |
929 | /// Return true if the predicate is relational (not EQ or NE). |
930 | bool isRelational() const { return !isEquality(); } |
931 | |
932 | /// @returns true if the comparison is signed, false otherwise. |
933 | /// Determine if this instruction is using a signed comparison. |
934 | bool isSigned() const { |
935 | return isSigned(getPredicate()); |
936 | } |
937 | |
938 | /// @returns true if the comparison is unsigned, false otherwise. |
939 | /// Determine if this instruction is using an unsigned comparison. |
940 | bool isUnsigned() const { |
941 | return isUnsigned(getPredicate()); |
942 | } |
943 | |
944 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
945 | /// @returns the signed version of the unsigned predicate pred. |
946 | /// return the signed version of a predicate |
947 | static Predicate getSignedPredicate(Predicate pred); |
948 | |
949 | /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert |
950 | /// @returns the signed version of the predicate for this instruction (which |
951 | /// has to be an unsigned predicate). |
952 | /// return the signed version of a predicate |
953 | Predicate getSignedPredicate() { |
954 | return getSignedPredicate(getPredicate()); |
955 | } |
956 | |
957 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
958 | /// @returns the unsigned version of the signed predicate pred. |
959 | static Predicate getUnsignedPredicate(Predicate pred); |
960 | |
961 | /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert |
962 | /// @returns the unsigned version of the predicate for this instruction (which |
963 | /// has to be an signed predicate). |
964 | /// return the unsigned version of a predicate |
965 | Predicate getUnsignedPredicate() { |
966 | return getUnsignedPredicate(getPredicate()); |
967 | } |
968 | |
969 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
970 | /// @returns the unsigned version of the signed predicate pred or |
971 | /// the signed version of the signed predicate pred. |
972 | static Predicate getFlippedSignednessPredicate(Predicate pred); |
973 | |
974 | /// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert |
975 | /// @returns the unsigned version of the signed predicate pred or |
976 | /// the signed version of the signed predicate pred. |
977 | Predicate getFlippedSignednessPredicate() { |
978 | return getFlippedSignednessPredicate(getPredicate()); |
979 | } |
980 | |
981 | /// This is just a convenience. |
982 | /// Determine if this is true when both operands are the same. |
983 | bool isTrueWhenEqual() const { |
984 | return isTrueWhenEqual(getPredicate()); |
985 | } |
986 | |
987 | /// This is just a convenience. |
988 | /// Determine if this is false when both operands are the same. |
989 | bool isFalseWhenEqual() const { |
990 | return isFalseWhenEqual(getPredicate()); |
991 | } |
992 | |
993 | /// @returns true if the predicate is unsigned, false otherwise. |
994 | /// Determine if the predicate is an unsigned operation. |
995 | static bool isUnsigned(Predicate predicate); |
996 | |
997 | /// @returns true if the predicate is signed, false otherwise. |
998 | /// Determine if the predicate is an signed operation. |
999 | static bool isSigned(Predicate predicate); |
1000 | |
1001 | /// Determine if the predicate is an ordered operation. |
1002 | static bool isOrdered(Predicate predicate); |
1003 | |
1004 | /// Determine if the predicate is an unordered operation. |
1005 | static bool isUnordered(Predicate predicate); |
1006 | |
1007 | /// Determine if the predicate is true when comparing a value with itself. |
1008 | static bool isTrueWhenEqual(Predicate predicate); |
1009 | |
1010 | /// Determine if the predicate is false when comparing a value with itself. |
1011 | static bool isFalseWhenEqual(Predicate predicate); |
1012 | |
1013 | /// Determine if Pred1 implies Pred2 is true when two compares have matching |
1014 | /// operands. |
1015 | static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1016 | |
1017 | /// Determine if Pred1 implies Pred2 is false when two compares have matching |
1018 | /// operands. |
1019 | static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2); |
1020 | |
1021 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
1022 | static bool classof(const Instruction *I) { |
1023 | return I->getOpcode() == Instruction::ICmp || |
1024 | I->getOpcode() == Instruction::FCmp; |
1025 | } |
1026 | static bool classof(const Value *V) { |
1027 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1028 | } |
1029 | |
1030 | /// Create a result type for fcmp/icmp |
1031 | static Type* makeCmpResultType(Type* opnd_type) { |
1032 | if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) { |
1033 | return VectorType::get(Type::getInt1Ty(opnd_type->getContext()), |
1034 | vt->getElementCount()); |
1035 | } |
1036 | return Type::getInt1Ty(opnd_type->getContext()); |
1037 | } |
1038 | |
1039 | private: |
1040 | // Shadow Value::setValueSubclassData with a private forwarding method so that |
1041 | // subclasses cannot accidentally use it. |
1042 | void setValueSubclassData(unsigned short D) { |
1043 | Value::setValueSubclassData(D); |
1044 | } |
1045 | }; |
1046 | |
1047 | // FIXME: these are redundant if CmpInst < BinaryOperator |
1048 | template <> |
1049 | struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> { |
1050 | }; |
1051 | |
1052 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)CmpInst::op_iterator CmpInst::op_begin() { return OperandTraits <CmpInst>::op_begin(this); } CmpInst::const_op_iterator CmpInst::op_begin() const { return OperandTraits<CmpInst> ::op_begin(const_cast<CmpInst*>(this)); } CmpInst::op_iterator CmpInst::op_end() { return OperandTraits<CmpInst>::op_end (this); } CmpInst::const_op_iterator CmpInst::op_end() const { return OperandTraits<CmpInst>::op_end(const_cast<CmpInst *>(this)); } Value *CmpInst::getOperand(unsigned i_nocapture ) const { (static_cast<void> (0)); return cast_or_null< Value>( OperandTraits<CmpInst>::op_begin(const_cast< CmpInst*>(this))[i_nocapture].get()); } void CmpInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { (static_cast< void> (0)); OperandTraits<CmpInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned CmpInst::getNumOperands() const { return OperandTraits<CmpInst>::operands(this); } template <int Idx_nocapture> Use &CmpInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &CmpInst::Op() const { return this->OpFrom <Idx_nocapture>(this); } |
1053 | |
1054 | /// A lightweight accessor for an operand bundle meant to be passed |
1055 | /// around by value. |
1056 | struct OperandBundleUse { |
1057 | ArrayRef<Use> Inputs; |
1058 | |
1059 | OperandBundleUse() = default; |
1060 | explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs) |
1061 | : Inputs(Inputs), Tag(Tag) {} |
1062 | |
1063 | /// Return true if the operand at index \p Idx in this operand bundle |
1064 | /// has the attribute A. |
1065 | bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const { |
1066 | if (isDeoptOperandBundle()) |
1067 | if (A == Attribute::ReadOnly || A == Attribute::NoCapture) |
1068 | return Inputs[Idx]->getType()->isPointerTy(); |
1069 | |
1070 | // Conservative answer: no operands have any attributes. |
1071 | return false; |
1072 | } |
1073 | |
1074 | /// Return the tag of this operand bundle as a string. |
1075 | StringRef getTagName() const { |
1076 | return Tag->getKey(); |
1077 | } |
1078 | |
1079 | /// Return the tag of this operand bundle as an integer. |
1080 | /// |
1081 | /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag, |
1082 | /// and this function returns the unique integer getOrInsertBundleTag |
1083 | /// associated the tag of this operand bundle to. |
1084 | uint32_t getTagID() const { |
1085 | return Tag->getValue(); |
1086 | } |
1087 | |
1088 | /// Return true if this is a "deopt" operand bundle. |
1089 | bool isDeoptOperandBundle() const { |
1090 | return getTagID() == LLVMContext::OB_deopt; |
1091 | } |
1092 | |
1093 | /// Return true if this is a "funclet" operand bundle. |
1094 | bool isFuncletOperandBundle() const { |
1095 | return getTagID() == LLVMContext::OB_funclet; |
1096 | } |
1097 | |
1098 | /// Return true if this is a "cfguardtarget" operand bundle. |
1099 | bool isCFGuardTargetOperandBundle() const { |
1100 | return getTagID() == LLVMContext::OB_cfguardtarget; |
1101 | } |
1102 | |
1103 | private: |
1104 | /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag. |
1105 | StringMapEntry<uint32_t> *Tag; |
1106 | }; |
1107 | |
1108 | /// A container for an operand bundle being viewed as a set of values |
1109 | /// rather than a set of uses. |
1110 | /// |
1111 | /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and |
1112 | /// so it is possible to create and pass around "self-contained" instances of |
1113 | /// OperandBundleDef and ConstOperandBundleDef. |
1114 | template <typename InputTy> class OperandBundleDefT { |
1115 | std::string Tag; |
1116 | std::vector<InputTy> Inputs; |
1117 | |
1118 | public: |
1119 | explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs) |
1120 | : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {} |
1121 | explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs) |
1122 | : Tag(std::move(Tag)), Inputs(Inputs) {} |
1123 | |
1124 | explicit OperandBundleDefT(const OperandBundleUse &OBU) { |
1125 | Tag = std::string(OBU.getTagName()); |
1126 | llvm::append_range(Inputs, OBU.Inputs); |
1127 | } |
1128 | |
1129 | ArrayRef<InputTy> inputs() const { return Inputs; } |
1130 | |
1131 | using input_iterator = typename std::vector<InputTy>::const_iterator; |
1132 | |
1133 | size_t input_size() const { return Inputs.size(); } |
1134 | input_iterator input_begin() const { return Inputs.begin(); } |
1135 | input_iterator input_end() const { return Inputs.end(); } |
1136 | |
1137 | StringRef getTag() const { return Tag; } |
1138 | }; |
1139 | |
1140 | using OperandBundleDef = OperandBundleDefT<Value *>; |
1141 | using ConstOperandBundleDef = OperandBundleDefT<const Value *>; |
1142 | |
1143 | //===----------------------------------------------------------------------===// |
1144 | // CallBase Class |
1145 | //===----------------------------------------------------------------------===// |
1146 | |
1147 | /// Base class for all callable instructions (InvokeInst and CallInst) |
1148 | /// Holds everything related to calling a function. |
1149 | /// |
1150 | /// All call-like instructions are required to use a common operand layout: |
1151 | /// - Zero or more arguments to the call, |
1152 | /// - Zero or more operand bundles with zero or more operand inputs each |
1153 | /// bundle, |
1154 | /// - Zero or more subclass controlled operands |
1155 | /// - The called function. |
1156 | /// |
1157 | /// This allows this base class to easily access the called function and the |
1158 | /// start of the arguments without knowing how many other operands a particular |
1159 | /// subclass requires. Note that accessing the end of the argument list isn't |
1160 | /// as cheap as most other operations on the base class. |
1161 | class CallBase : public Instruction { |
1162 | protected: |
1163 | // The first two bits are reserved by CallInst for fast retrieval, |
1164 | using CallInstReservedField = Bitfield::Element<unsigned, 0, 2>; |
1165 | using CallingConvField = |
1166 | Bitfield::Element<CallingConv::ID, CallInstReservedField::NextBit, 10, |
1167 | CallingConv::MaxID>; |
1168 | static_assert( |
1169 | Bitfield::areContiguous<CallInstReservedField, CallingConvField>(), |
1170 | "Bitfields must be contiguous"); |
1171 | |
1172 | /// The last operand is the called operand. |
1173 | static constexpr int CalledOperandOpEndIdx = -1; |
1174 | |
1175 | AttributeList Attrs; ///< parameter attributes for callable |
1176 | FunctionType *FTy; |
1177 | |
1178 | template <class... ArgsTy> |
1179 | CallBase(AttributeList const &A, FunctionType *FT, ArgsTy &&... Args) |
1180 | : Instruction(std::forward<ArgsTy>(Args)...), Attrs(A), FTy(FT) {} |
1181 | |
1182 | using Instruction::Instruction; |
1183 | |
1184 | bool hasDescriptor() const { return Value::HasDescriptor; } |
1185 | |
1186 | unsigned getNumSubclassExtraOperands() const { |
1187 | switch (getOpcode()) { |
1188 | case Instruction::Call: |
1189 | return 0; |
1190 | case Instruction::Invoke: |
1191 | return 2; |
1192 | case Instruction::CallBr: |
1193 | return getNumSubclassExtraOperandsDynamic(); |
1194 | } |
1195 | llvm_unreachable("Invalid opcode!")__builtin_unreachable(); |
1196 | } |
1197 | |
1198 | /// Get the number of extra operands for instructions that don't have a fixed |
1199 | /// number of extra operands. |
1200 | unsigned getNumSubclassExtraOperandsDynamic() const; |
1201 | |
1202 | public: |
1203 | using Instruction::getContext; |
1204 | |
1205 | /// Create a clone of \p CB with a different set of operand bundles and |
1206 | /// insert it before \p InsertPt. |
1207 | /// |
1208 | /// The returned call instruction is identical \p CB in every way except that |
1209 | /// the operand bundles for the new instruction are set to the operand bundles |
1210 | /// in \p Bundles. |
1211 | static CallBase *Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, |
1212 | Instruction *InsertPt = nullptr); |
1213 | |
1214 | /// Create a clone of \p CB with the operand bundle with the tag matching |
1215 | /// \p Bundle's tag replaced with Bundle, and insert it before \p InsertPt. |
1216 | /// |
1217 | /// The returned call instruction is identical \p CI in every way except that |
1218 | /// the specified operand bundle has been replaced. |
1219 | static CallBase *Create(CallBase *CB, |
1220 | OperandBundleDef Bundle, |
1221 | Instruction *InsertPt = nullptr); |
1222 | |
1223 | /// Create a clone of \p CB with operand bundle \p OB added. |
1224 | static CallBase *addOperandBundle(CallBase *CB, uint32_t ID, |
1225 | OperandBundleDef OB, |
1226 | Instruction *InsertPt = nullptr); |
1227 | |
1228 | /// Create a clone of \p CB with operand bundle \p ID removed. |
1229 | static CallBase *removeOperandBundle(CallBase *CB, uint32_t ID, |
1230 | Instruction *InsertPt = nullptr); |
1231 | |
1232 | static bool classof(const Instruction *I) { |
1233 | return I->getOpcode() == Instruction::Call || |
1234 | I->getOpcode() == Instruction::Invoke || |
1235 | I->getOpcode() == Instruction::CallBr; |
1236 | } |
1237 | static bool classof(const Value *V) { |
1238 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1239 | } |
1240 | |
1241 | FunctionType *getFunctionType() const { return FTy; } |
1242 | |
1243 | void mutateFunctionType(FunctionType *FTy) { |
1244 | Value::mutateType(FTy->getReturnType()); |
1245 | this->FTy = FTy; |
1246 | } |
1247 | |
1248 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
1249 | |
1250 | /// data_operands_begin/data_operands_end - Return iterators iterating over |
1251 | /// the call / invoke argument list and bundle operands. For invokes, this is |
1252 | /// the set of instruction operands except the invoke target and the two |
1253 | /// successor blocks; and for calls this is the set of instruction operands |
1254 | /// except the call target. |
1255 | User::op_iterator data_operands_begin() { return op_begin(); } |
1256 | User::const_op_iterator data_operands_begin() const { |
1257 | return const_cast<CallBase *>(this)->data_operands_begin(); |
1258 | } |
1259 | User::op_iterator data_operands_end() { |
1260 | // Walk from the end of the operands over the called operand and any |
1261 | // subclass operands. |
1262 | return op_end() - getNumSubclassExtraOperands() - 1; |
1263 | } |
1264 | User::const_op_iterator data_operands_end() const { |
1265 | return const_cast<CallBase *>(this)->data_operands_end(); |
1266 | } |
1267 | iterator_range<User::op_iterator> data_ops() { |
1268 | return make_range(data_operands_begin(), data_operands_end()); |
1269 | } |
1270 | iterator_range<User::const_op_iterator> data_ops() const { |
1271 | return make_range(data_operands_begin(), data_operands_end()); |
1272 | } |
1273 | bool data_operands_empty() const { |
1274 | return data_operands_end() == data_operands_begin(); |
1275 | } |
1276 | unsigned data_operands_size() const { |
1277 | return std::distance(data_operands_begin(), data_operands_end()); |
1278 | } |
1279 | |
1280 | bool isDataOperand(const Use *U) const { |
1281 | assert(this == U->getUser() &&(static_cast<void> (0)) |
1282 | "Only valid to query with a use of this instruction!")(static_cast<void> (0)); |
1283 | return data_operands_begin() <= U && U < data_operands_end(); |
1284 | } |
1285 | bool isDataOperand(Value::const_user_iterator UI) const { |
1286 | return isDataOperand(&UI.getUse()); |
1287 | } |
1288 | |
1289 | /// Given a value use iterator, return the data operand corresponding to it. |
1290 | /// Iterator must actually correspond to a data operand. |
1291 | unsigned getDataOperandNo(Value::const_user_iterator UI) const { |
1292 | return getDataOperandNo(&UI.getUse()); |
1293 | } |
1294 | |
1295 | /// Given a use for a data operand, get the data operand number that |
1296 | /// corresponds to it. |
1297 | unsigned getDataOperandNo(const Use *U) const { |
1298 | assert(isDataOperand(U) && "Data operand # out of range!")(static_cast<void> (0)); |
1299 | return U - data_operands_begin(); |
1300 | } |
1301 | |
1302 | /// Return the iterator pointing to the beginning of the argument list. |
1303 | User::op_iterator arg_begin() { return op_begin(); } |
1304 | User::const_op_iterator arg_begin() const { |
1305 | return const_cast<CallBase *>(this)->arg_begin(); |
1306 | } |
1307 | |
1308 | /// Return the iterator pointing to the end of the argument list. |
1309 | User::op_iterator arg_end() { |
1310 | // From the end of the data operands, walk backwards past the bundle |
1311 | // operands. |
1312 | return data_operands_end() - getNumTotalBundleOperands(); |
1313 | } |
1314 | User::const_op_iterator arg_end() const { |
1315 | return const_cast<CallBase *>(this)->arg_end(); |
1316 | } |
1317 | |
1318 | /// Iteration adapter for range-for loops. |
1319 | iterator_range<User::op_iterator> args() { |
1320 | return make_range(arg_begin(), arg_end()); |
1321 | } |
1322 | iterator_range<User::const_op_iterator> args() const { |
1323 | return make_range(arg_begin(), arg_end()); |
1324 | } |
1325 | bool arg_empty() const { return arg_end() == arg_begin(); } |
1326 | unsigned arg_size() const { return arg_end() - arg_begin(); } |
1327 | |
1328 | // Legacy API names that duplicate the above and will be removed once users |
1329 | // are migrated. |
1330 | iterator_range<User::op_iterator> arg_operands() { |
1331 | return make_range(arg_begin(), arg_end()); |
1332 | } |
1333 | iterator_range<User::const_op_iterator> arg_operands() const { |
1334 | return make_range(arg_begin(), arg_end()); |
1335 | } |
1336 | unsigned getNumArgOperands() const { return arg_size(); } |
1337 | |
1338 | Value *getArgOperand(unsigned i) const { |
1339 | assert(i < getNumArgOperands() && "Out of bounds!")(static_cast<void> (0)); |
1340 | return getOperand(i); |
1341 | } |
1342 | |
1343 | void setArgOperand(unsigned i, Value *v) { |
1344 | assert(i < getNumArgOperands() && "Out of bounds!")(static_cast<void> (0)); |
1345 | setOperand(i, v); |
1346 | } |
1347 | |
1348 | /// Wrappers for getting the \c Use of a call argument. |
1349 | const Use &getArgOperandUse(unsigned i) const { |
1350 | assert(i < getNumArgOperands() && "Out of bounds!")(static_cast<void> (0)); |
1351 | return User::getOperandUse(i); |
1352 | } |
1353 | Use &getArgOperandUse(unsigned i) { |
1354 | assert(i < getNumArgOperands() && "Out of bounds!")(static_cast<void> (0)); |
1355 | return User::getOperandUse(i); |
1356 | } |
1357 | |
1358 | bool isArgOperand(const Use *U) const { |
1359 | assert(this == U->getUser() &&(static_cast<void> (0)) |
1360 | "Only valid to query with a use of this instruction!")(static_cast<void> (0)); |
1361 | return arg_begin() <= U && U < arg_end(); |
1362 | } |
1363 | bool isArgOperand(Value::const_user_iterator UI) const { |
1364 | return isArgOperand(&UI.getUse()); |
1365 | } |
1366 | |
1367 | /// Given a use for a arg operand, get the arg operand number that |
1368 | /// corresponds to it. |
1369 | unsigned getArgOperandNo(const Use *U) const { |
1370 | assert(isArgOperand(U) && "Arg operand # out of range!")(static_cast<void> (0)); |
1371 | return U - arg_begin(); |
1372 | } |
1373 | |
1374 | /// Given a value use iterator, return the arg operand number corresponding to |
1375 | /// it. Iterator must actually correspond to a data operand. |
1376 | unsigned getArgOperandNo(Value::const_user_iterator UI) const { |
1377 | return getArgOperandNo(&UI.getUse()); |
1378 | } |
1379 | |
1380 | /// Returns true if this CallSite passes the given Value* as an argument to |
1381 | /// the called function. |
1382 | bool hasArgument(const Value *V) const { |
1383 | return llvm::is_contained(args(), V); |
1384 | } |
1385 | |
1386 | Value *getCalledOperand() const { return Op<CalledOperandOpEndIdx>(); } |
1387 | |
1388 | const Use &getCalledOperandUse() const { return Op<CalledOperandOpEndIdx>(); } |
1389 | Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); } |
1390 | |
1391 | /// Returns the function called, or null if this is an |
1392 | /// indirect function invocation. |
1393 | Function *getCalledFunction() const { |
1394 | return dyn_cast_or_null<Function>(getCalledOperand()); |
1395 | } |
1396 | |
1397 | /// Return true if the callsite is an indirect call. |
1398 | bool isIndirectCall() const; |
1399 | |
1400 | /// Determine whether the passed iterator points to the callee operand's Use. |
1401 | bool isCallee(Value::const_user_iterator UI) const { |
1402 | return isCallee(&UI.getUse()); |
1403 | } |
1404 | |
1405 | /// Determine whether this Use is the callee operand's Use. |
1406 | bool isCallee(const Use *U) const { return &getCalledOperandUse() == U; } |
1407 | |
1408 | /// Helper to get the caller (the parent function). |
1409 | Function *getCaller(); |
1410 | const Function *getCaller() const { |
1411 | return const_cast<CallBase *>(this)->getCaller(); |
1412 | } |
1413 | |
1414 | /// Tests if this call site must be tail call optimized. Only a CallInst can |
1415 | /// be tail call optimized. |
1416 | bool isMustTailCall() const; |
1417 | |
1418 | /// Tests if this call site is marked as a tail call. |
1419 | bool isTailCall() const; |
1420 | |
1421 | /// Returns the intrinsic ID of the intrinsic called or |
1422 | /// Intrinsic::not_intrinsic if the called function is not an intrinsic, or if |
1423 | /// this is an indirect call. |
1424 | Intrinsic::ID getIntrinsicID() const; |
1425 | |
1426 | void setCalledOperand(Value *V) { Op<CalledOperandOpEndIdx>() = V; } |
1427 | |
1428 | /// Sets the function called, including updating the function type. |
1429 | void setCalledFunction(Function *Fn) { |
1430 | setCalledFunction(Fn->getFunctionType(), Fn); |
1431 | } |
1432 | |
1433 | /// Sets the function called, including updating the function type. |
1434 | void setCalledFunction(FunctionCallee Fn) { |
1435 | setCalledFunction(Fn.getFunctionType(), Fn.getCallee()); |
1436 | } |
1437 | |
1438 | /// Sets the function called, including updating to the specified function |
1439 | /// type. |
1440 | void setCalledFunction(FunctionType *FTy, Value *Fn) { |
1441 | this->FTy = FTy; |
1442 | assert(cast<PointerType>(Fn->getType())->isOpaqueOrPointeeTypeMatches(FTy))(static_cast<void> (0)); |
1443 | // This function doesn't mutate the return type, only the function |
1444 | // type. Seems broken, but I'm just gonna stick an assert in for now. |
1445 | assert(getType() == FTy->getReturnType())(static_cast<void> (0)); |
1446 | setCalledOperand(Fn); |
1447 | } |
1448 | |
1449 | CallingConv::ID getCallingConv() const { |
1450 | return getSubclassData<CallingConvField>(); |
1451 | } |
1452 | |
1453 | void setCallingConv(CallingConv::ID CC) { |
1454 | setSubclassData<CallingConvField>(CC); |
1455 | } |
1456 | |
1457 | /// Check if this call is an inline asm statement. |
1458 | bool isInlineAsm() const { return isa<InlineAsm>(getCalledOperand()); } |
1459 | |
1460 | /// \name Attribute API |
1461 | /// |
1462 | /// These methods access and modify attributes on this call (including |
1463 | /// looking through to the attributes on the called function when necessary). |
1464 | ///@{ |
1465 | |
1466 | /// Return the parameter attributes for this call. |
1467 | /// |
1468 | AttributeList getAttributes() const { return Attrs; } |
1469 | |
1470 | /// Set the parameter attributes for this call. |
1471 | /// |
1472 | void setAttributes(AttributeList A) { Attrs = A; } |
1473 | |
1474 | /// Determine whether this call has the given attribute. If it does not |
1475 | /// then determine if the called function has the attribute, but only if |
1476 | /// the attribute is allowed for the call. |
1477 | bool hasFnAttr(Attribute::AttrKind Kind) const { |
1478 | assert(Kind != Attribute::NoBuiltin &&(static_cast<void> (0)) |
1479 | "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")(static_cast<void> (0)); |
1480 | return hasFnAttrImpl(Kind); |
1481 | } |
1482 | |
1483 | /// Determine whether this call has the given attribute. If it does not |
1484 | /// then determine if the called function has the attribute, but only if |
1485 | /// the attribute is allowed for the call. |
1486 | bool hasFnAttr(StringRef Kind) const { return hasFnAttrImpl(Kind); } |
1487 | |
1488 | // TODO: remove non-AtIndex versions of these methods. |
1489 | /// adds the attribute to the list of attributes. |
1490 | void addAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { |
1491 | Attrs = Attrs.addAttributeAtIndex(getContext(), i, Kind); |
1492 | } |
1493 | |
1494 | /// adds the attribute to the list of attributes. |
1495 | void addAttributeAtIndex(unsigned i, Attribute Attr) { |
1496 | Attrs = Attrs.addAttributeAtIndex(getContext(), i, Attr); |
1497 | } |
1498 | |
1499 | /// Adds the attribute to the function. |
1500 | void addFnAttr(Attribute::AttrKind Kind) { |
1501 | Attrs = Attrs.addFnAttribute(getContext(), Kind); |
1502 | } |
1503 | |
1504 | /// Adds the attribute to the function. |
1505 | void addFnAttr(Attribute Attr) { |
1506 | Attrs = Attrs.addFnAttribute(getContext(), Attr); |
1507 | } |
1508 | |
1509 | /// Adds the attribute to the return value. |
1510 | void addRetAttr(Attribute::AttrKind Kind) { |
1511 | Attrs = Attrs.addRetAttribute(getContext(), Kind); |
1512 | } |
1513 | |
1514 | /// Adds the attribute to the return value. |
1515 | void addRetAttr(Attribute Attr) { |
1516 | Attrs = Attrs.addRetAttribute(getContext(), Attr); |
1517 | } |
1518 | |
1519 | /// Adds the attribute to the indicated argument |
1520 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1521 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1522 | Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Kind); |
1523 | } |
1524 | |
1525 | /// Adds the attribute to the indicated argument |
1526 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1527 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1528 | Attrs = Attrs.addParamAttribute(getContext(), ArgNo, Attr); |
1529 | } |
1530 | |
1531 | /// removes the attribute from the list of attributes. |
1532 | void removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { |
1533 | Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind); |
1534 | } |
1535 | |
1536 | /// removes the attribute from the list of attributes. |
1537 | void removeAttributeAtIndex(unsigned i, StringRef Kind) { |
1538 | Attrs = Attrs.removeAttributeAtIndex(getContext(), i, Kind); |
1539 | } |
1540 | |
1541 | /// Removes the attributes from the function |
1542 | void removeFnAttrs(const AttrBuilder &AttrsToRemove) { |
1543 | Attrs = Attrs.removeFnAttributes(getContext(), AttrsToRemove); |
1544 | } |
1545 | |
1546 | /// Removes the attribute from the function |
1547 | void removeFnAttr(Attribute::AttrKind Kind) { |
1548 | Attrs = Attrs.removeFnAttribute(getContext(), Kind); |
1549 | } |
1550 | |
1551 | /// Removes the attribute from the return value |
1552 | void removeRetAttr(Attribute::AttrKind Kind) { |
1553 | Attrs = Attrs.removeRetAttribute(getContext(), Kind); |
1554 | } |
1555 | |
1556 | /// Removes the attributes from the return value |
1557 | void removeRetAttrs(const AttrBuilder &AttrsToRemove) { |
1558 | Attrs = Attrs.removeRetAttributes(getContext(), AttrsToRemove); |
1559 | } |
1560 | |
1561 | /// Removes the attribute from the given argument |
1562 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1563 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1564 | Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind); |
1565 | } |
1566 | |
1567 | /// Removes the attribute from the given argument |
1568 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1569 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1570 | Attrs = Attrs.removeParamAttribute(getContext(), ArgNo, Kind); |
1571 | } |
1572 | |
1573 | /// Removes the attributes from the given argument |
1574 | void removeParamAttrs(unsigned ArgNo, const AttrBuilder &AttrsToRemove) { |
1575 | Attrs = Attrs.removeParamAttributes(getContext(), ArgNo, AttrsToRemove); |
1576 | } |
1577 | |
1578 | /// adds the dereferenceable attribute to the list of attributes. |
1579 | void addDereferenceableParamAttr(unsigned i, uint64_t Bytes) { |
1580 | Attrs = Attrs.addDereferenceableParamAttr(getContext(), i, Bytes); |
1581 | } |
1582 | |
1583 | /// adds the dereferenceable attribute to the list of attributes. |
1584 | void addDereferenceableRetAttr(uint64_t Bytes) { |
1585 | Attrs = Attrs.addDereferenceableRetAttr(getContext(), Bytes); |
1586 | } |
1587 | |
1588 | /// Determine whether the return value has the given attribute. |
1589 | bool hasRetAttr(Attribute::AttrKind Kind) const { |
1590 | return hasRetAttrImpl(Kind); |
1591 | } |
1592 | /// Determine whether the return value has the given attribute. |
1593 | bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } |
1594 | |
1595 | /// Determine whether the argument or parameter has the given attribute. |
1596 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1597 | |
1598 | /// Get the attribute of a given kind at a position. |
1599 | Attribute getAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) const { |
1600 | return getAttributes().getAttributeAtIndex(i, Kind); |
1601 | } |
1602 | |
1603 | /// Get the attribute of a given kind at a position. |
1604 | Attribute getAttributeAtIndex(unsigned i, StringRef Kind) const { |
1605 | return getAttributes().getAttributeAtIndex(i, Kind); |
1606 | } |
1607 | |
1608 | /// Get the attribute of a given kind for the function. |
1609 | Attribute getFnAttr(StringRef Kind) const { |
1610 | return getAttributes().getFnAttr(Kind); |
1611 | } |
1612 | |
1613 | /// Get the attribute of a given kind for the function. |
1614 | Attribute getFnAttr(Attribute::AttrKind Kind) const { |
1615 | return getAttributes().getFnAttr(Kind); |
1616 | } |
1617 | |
1618 | /// Get the attribute of a given kind from a given arg |
1619 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1620 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1621 | return getAttributes().getParamAttr(ArgNo, Kind); |
1622 | } |
1623 | |
1624 | /// Get the attribute of a given kind from a given arg |
1625 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1626 | assert(ArgNo < getNumArgOperands() && "Out of bounds")(static_cast<void> (0)); |
1627 | return getAttributes().getParamAttr(ArgNo, Kind); |
1628 | } |
1629 | |
1630 | /// Return true if the data operand at index \p i has the attribute \p |
1631 | /// A. |
1632 | /// |
1633 | /// Data operands include call arguments and values used in operand bundles, |
1634 | /// but does not include the callee operand. This routine dispatches to the |
1635 | /// underlying AttributeList or the OperandBundleUser as appropriate. |
1636 | /// |
1637 | /// The index \p i is interpreted as |
1638 | /// |
1639 | /// \p i == Attribute::ReturnIndex -> the return value |
1640 | /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) |
1641 | /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index |
1642 | /// (\p i - 1) in the operand list. |
1643 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1644 | // Note that we have to add one because `i` isn't zero-indexed. |
1645 | assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&(static_cast<void> (0)) |
1646 | "Data operand index out of bounds!")(static_cast<void> (0)); |
1647 | |
1648 | // The attribute A can either be directly specified, if the operand in |
1649 | // question is a call argument; or be indirectly implied by the kind of its |
1650 | // containing operand bundle, if the operand is a bundle operand. |
1651 | |
1652 | if (i == AttributeList::ReturnIndex) |
1653 | return hasRetAttr(Kind); |
1654 | |
1655 | // FIXME: Avoid these i - 1 calculations and update the API to use |
1656 | // zero-based indices. |
1657 | if (i < (getNumArgOperands() + 1)) |
1658 | return paramHasAttr(i - 1, Kind); |
1659 | |
1660 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&(static_cast<void> (0)) |
1661 | "Must be either a call argument or an operand bundle!")(static_cast<void> (0)); |
1662 | return bundleOperandHasAttr(i - 1, Kind); |
1663 | } |
1664 | |
1665 | /// Determine whether this data operand is not captured. |
1666 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1667 | // better indicate that this may return a conservative answer. |
1668 | bool doesNotCapture(unsigned OpNo) const { |
1669 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); |
1670 | } |
1671 | |
1672 | /// Determine whether this argument is passed by value. |
1673 | bool isByValArgument(unsigned ArgNo) const { |
1674 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1675 | } |
1676 | |
1677 | /// Determine whether this argument is passed in an alloca. |
1678 | bool isInAllocaArgument(unsigned ArgNo) const { |
1679 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1680 | } |
1681 | |
1682 | /// Determine whether this argument is passed by value, in an alloca, or is |
1683 | /// preallocated. |
1684 | bool isPassPointeeByValueArgument(unsigned ArgNo) const { |
1685 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1686 | paramHasAttr(ArgNo, Attribute::InAlloca) || |
1687 | paramHasAttr(ArgNo, Attribute::Preallocated); |
1688 | } |
1689 | |
1690 | /// Determine whether passing undef to this argument is undefined behavior. |
1691 | /// If passing undef to this argument is UB, passing poison is UB as well |
1692 | /// because poison is more undefined than undef. |
1693 | bool isPassingUndefUB(unsigned ArgNo) const { |
1694 | return paramHasAttr(ArgNo, Attribute::NoUndef) || |
1695 | // dereferenceable implies noundef. |
1696 | paramHasAttr(ArgNo, Attribute::Dereferenceable) || |
1697 | // dereferenceable implies noundef, and null is a well-defined value. |
1698 | paramHasAttr(ArgNo, Attribute::DereferenceableOrNull); |
1699 | } |
1700 | |
1701 | /// Determine if there are is an inalloca argument. Only the last argument can |
1702 | /// have the inalloca attribute. |
1703 | bool hasInAllocaArgument() const { |
1704 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1705 | } |
1706 | |
1707 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1708 | // better indicate that this may return a conservative answer. |
1709 | bool doesNotAccessMemory(unsigned OpNo) const { |
1710 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1711 | } |
1712 | |
1713 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1714 | // better indicate that this may return a conservative answer. |
1715 | bool onlyReadsMemory(unsigned OpNo) const { |
1716 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || |
1717 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1718 | } |
1719 | |
1720 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1721 | // better indicate that this may return a conservative answer. |
1722 | bool doesNotReadMemory(unsigned OpNo) const { |
1723 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || |
1724 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1725 | } |
1726 | |
1727 | /// Extract the alignment of the return value. |
1728 | MaybeAlign getRetAlign() const { return Attrs.getRetAlignment(); } |
1729 | |
1730 | /// Extract the alignment for a call or parameter (0=unknown). |
1731 | MaybeAlign getParamAlign(unsigned ArgNo) const { |
1732 | return Attrs.getParamAlignment(ArgNo); |
1733 | } |
1734 | |
1735 | MaybeAlign getParamStackAlign(unsigned ArgNo) const { |
1736 | return Attrs.getParamStackAlignment(ArgNo); |
1737 | } |
1738 | |
1739 | /// Extract the byval type for a call or parameter. |
1740 | Type *getParamByValType(unsigned ArgNo) const { |
1741 | if (auto *Ty = Attrs.getParamByValType(ArgNo)) |
1742 | return Ty; |
1743 | if (const Function *F = getCalledFunction()) |
1744 | return F->getAttributes().getParamByValType(ArgNo); |
1745 | return nullptr; |
1746 | } |
1747 | |
1748 | /// Extract the preallocated type for a call or parameter. |
1749 | Type *getParamPreallocatedType(unsigned ArgNo) const { |
1750 | if (auto *Ty = Attrs.getParamPreallocatedType(ArgNo)) |
1751 | return Ty; |
1752 | if (const Function *F = getCalledFunction()) |
1753 | return F->getAttributes().getParamPreallocatedType(ArgNo); |
1754 | return nullptr; |
1755 | } |
1756 | |
1757 | /// Extract the preallocated type for a call or parameter. |
1758 | Type *getParamInAllocaType(unsigned ArgNo) const { |
1759 | if (auto *Ty = Attrs.getParamInAllocaType(ArgNo)) |
1760 | return Ty; |
1761 | if (const Function *F = getCalledFunction()) |
1762 | return F->getAttributes().getParamInAllocaType(ArgNo); |
1763 | return nullptr; |
1764 | } |
1765 | |
1766 | /// Extract the number of dereferenceable bytes for a call or |
1767 | /// parameter (0=unknown). |
1768 | uint64_t getRetDereferenceableBytes() const { |
1769 | return Attrs.getRetDereferenceableBytes(); |
1770 | } |
1771 | |
1772 | /// Extract the number of dereferenceable bytes for a call or |
1773 | /// parameter (0=unknown). |
1774 | uint64_t getParamDereferenceableBytes(unsigned i) const { |
1775 | return Attrs.getParamDereferenceableBytes(i); |
1776 | } |
1777 | |
1778 | /// Extract the number of dereferenceable_or_null bytes for a call |
1779 | /// (0=unknown). |
1780 | uint64_t getRetDereferenceableOrNullBytes() const { |
1781 | return Attrs.getRetDereferenceableOrNullBytes(); |
1782 | } |
1783 | |
1784 | /// Extract the number of dereferenceable_or_null bytes for a |
1785 | /// parameter (0=unknown). |
1786 | uint64_t getParamDereferenceableOrNullBytes(unsigned i) const { |
1787 | return Attrs.getParamDereferenceableOrNullBytes(i); |
1788 | } |
1789 | |
1790 | /// Return true if the return value is known to be not null. |
1791 | /// This may be because it has the nonnull attribute, or because at least |
1792 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1793 | bool isReturnNonNull() const; |
1794 | |
1795 | /// Determine if the return value is marked with NoAlias attribute. |
1796 | bool returnDoesNotAlias() const { |
1797 | return Attrs.hasRetAttr(Attribute::NoAlias); |
1798 | } |
1799 | |
1800 | /// If one of the arguments has the 'returned' attribute, returns its |
1801 | /// operand value. Otherwise, return nullptr. |
1802 | Value *getReturnedArgOperand() const; |
1803 | |
1804 | /// Return true if the call should not be treated as a call to a |
1805 | /// builtin. |
1806 | bool isNoBuiltin() const { |
1807 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1808 | !hasFnAttrImpl(Attribute::Builtin); |
1809 | } |
1810 | |
1811 | /// Determine if the call requires strict floating point semantics. |
1812 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1813 | |
1814 | /// Return true if the call should not be inlined. |
1815 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1816 | void setIsNoInline() { addFnAttr(Attribute::NoInline); } |
1817 | /// Determine if the call does not access memory. |
1818 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1819 | void setDoesNotAccessMemory() { addFnAttr(Attribute::ReadNone); } |
1820 | |
1821 | /// Determine if the call does not access or only reads memory. |
1822 | bool onlyReadsMemory() const { |
1823 | return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); |
1824 | } |
1825 | |
1826 | void setOnlyReadsMemory() { addFnAttr(Attribute::ReadOnly); } |
1827 | |
1828 | /// Determine if the call does not access or only writes memory. |
1829 | bool doesNotReadMemory() const { |
1830 | return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); |
1831 | } |
1832 | void setDoesNotReadMemory() { addFnAttr(Attribute::WriteOnly); } |
1833 | |
1834 | /// Determine if the call can access memmory only using pointers based |
1835 | /// on its arguments. |
1836 | bool onlyAccessesArgMemory() const { |
1837 | return hasFnAttr(Attribute::ArgMemOnly); |
1838 | } |
1839 | void setOnlyAccessesArgMemory() { addFnAttr(Attribute::ArgMemOnly); } |
1840 | |
1841 | /// Determine if the function may only access memory that is |
1842 | /// inaccessible from the IR. |
1843 | bool onlyAccessesInaccessibleMemory() const { |
1844 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1845 | } |
1846 | void setOnlyAccessesInaccessibleMemory() { |
1847 | addFnAttr(Attribute::InaccessibleMemOnly); |
1848 | } |
1849 | |
1850 | /// Determine if the function may only access memory that is |
1851 | /// either inaccessible from the IR or pointed to by its arguments. |
1852 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1853 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1854 | } |
1855 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1856 | addFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1857 | } |
1858 | /// Determine if the call cannot return. |
1859 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1860 | void setDoesNotReturn() { addFnAttr(Attribute::NoReturn); } |
1861 | |
1862 | /// Determine if the call should not perform indirect branch tracking. |
1863 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1864 | |
1865 | /// Determine if the call cannot unwind. |
1866 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1867 | void setDoesNotThrow() { addFnAttr(Attribute::NoUnwind); } |
1868 | |
1869 | /// Determine if the invoke cannot be duplicated. |
1870 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1871 | void setCannotDuplicate() { addFnAttr(Attribute::NoDuplicate); } |
1872 | |
1873 | /// Determine if the call cannot be tail merged. |
1874 | bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); } |
1875 | void setCannotMerge() { addFnAttr(Attribute::NoMerge); } |
1876 | |
1877 | /// Determine if the invoke is convergent |
1878 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1879 | void setConvergent() { addFnAttr(Attribute::Convergent); } |
1880 | void setNotConvergent() { removeFnAttr(Attribute::Convergent); } |
1881 | |
1882 | /// Determine if the call returns a structure through first |
1883 | /// pointer argument. |
1884 | bool hasStructRetAttr() const { |
1885 | if (getNumArgOperands() == 0) |
1886 | return false; |
1887 | |
1888 | // Be friendly and also check the callee. |
1889 | return paramHasAttr(0, Attribute::StructRet); |
1890 | } |
1891 | |
1892 | /// Determine if any call argument is an aggregate passed by value. |
1893 | bool hasByValArgument() const { |
1894 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1895 | } |
1896 | |
1897 | ///@{ |
1898 | // End of attribute API. |
1899 | |
1900 | /// \name Operand Bundle API |
1901 | /// |
1902 | /// This group of methods provides the API to access and manipulate operand |
1903 | /// bundles on this call. |
1904 | /// @{ |
1905 | |
1906 | /// Return the number of operand bundles associated with this User. |
1907 | unsigned getNumOperandBundles() const { |
1908 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1909 | } |
1910 | |
1911 | /// Return true if this User has any operand bundles. |
1912 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1913 | |
1914 | /// Return the index of the first bundle operand in the Use array. |
1915 | unsigned getBundleOperandsStartIndex() const { |
1916 | assert(hasOperandBundles() && "Don't call otherwise!")(static_cast<void> (0)); |
1917 | return bundle_op_info_begin()->Begin; |
1918 | } |
1919 | |
1920 | /// Return the index of the last bundle operand in the Use array. |
1921 | unsigned getBundleOperandsEndIndex() const { |
1922 | assert(hasOperandBundles() && "Don't call otherwise!")(static_cast<void> (0)); |
1923 | return bundle_op_info_end()[-1].End; |
1924 | } |
1925 | |
1926 | /// Return true if the operand at index \p Idx is a bundle operand. |
1927 | bool isBundleOperand(unsigned Idx) const { |
1928 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1929 | Idx < getBundleOperandsEndIndex(); |
1930 | } |
1931 | |
1932 | /// Returns true if the use is a bundle operand. |
1933 | bool isBundleOperand(const Use *U) const { |
1934 | assert(this == U->getUser() &&(static_cast<void> (0)) |
1935 | "Only valid to query with a use of this instruction!")(static_cast<void> (0)); |
1936 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1937 | } |
1938 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1939 | return isBundleOperand(&UI.getUse()); |
1940 | } |
1941 | |
1942 | /// Return the total number operands (not operand bundles) used by |
1943 | /// every operand bundle in this OperandBundleUser. |
1944 | unsigned getNumTotalBundleOperands() const { |
1945 | if (!hasOperandBundles()) |
1946 | return 0; |
1947 | |
1948 | unsigned Begin = getBundleOperandsStartIndex(); |
1949 | unsigned End = getBundleOperandsEndIndex(); |
1950 | |
1951 | assert(Begin <= End && "Should be!")(static_cast<void> (0)); |
1952 | return End - Begin; |
1953 | } |
1954 | |
1955 | /// Return the operand bundle at a specific index. |
1956 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1957 | assert(Index < getNumOperandBundles() && "Index out of bounds!")(static_cast<void> (0)); |
1958 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1959 | } |
1960 | |
1961 | /// Return the number of operand bundles with the tag Name attached to |
1962 | /// this instruction. |
1963 | unsigned countOperandBundlesOfType(StringRef Name) const { |
1964 | unsigned Count = 0; |
1965 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1966 | if (getOperandBundleAt(i).getTagName() == Name) |
1967 | Count++; |
1968 | |
1969 | return Count; |
1970 | } |
1971 | |
1972 | /// Return the number of operand bundles with the tag ID attached to |
1973 | /// this instruction. |
1974 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
1975 | unsigned Count = 0; |
1976 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1977 | if (getOperandBundleAt(i).getTagID() == ID) |
1978 | Count++; |
1979 | |
1980 | return Count; |
1981 | } |
1982 | |
1983 | /// Return an operand bundle by name, if present. |
1984 | /// |
1985 | /// It is an error to call this for operand bundle types that may have |
1986 | /// multiple instances of them on the same instruction. |
1987 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
1988 | assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")(static_cast<void> (0)); |
1989 | |
1990 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1991 | OperandBundleUse U = getOperandBundleAt(i); |
1992 | if (U.getTagName() == Name) |
1993 | return U; |
1994 | } |
1995 | |
1996 | return None; |
1997 | } |
1998 | |
1999 | /// Return an operand bundle by tag ID, if present. |
2000 | /// |
2001 | /// It is an error to call this for operand bundle types that may have |
2002 | /// multiple instances of them on the same instruction. |
2003 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
2004 | assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")(static_cast<void> (0)); |
2005 | |
2006 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2007 | OperandBundleUse U = getOperandBundleAt(i); |
2008 | if (U.getTagID() == ID) |
2009 | return U; |
2010 | } |
2011 | |
2012 | return None; |
2013 | } |
2014 | |
2015 | /// Return the list of operand bundles attached to this instruction as |
2016 | /// a vector of OperandBundleDefs. |
2017 | /// |
2018 | /// This function copies the OperandBundeUse instances associated with this |
2019 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
2020 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
2021 | /// representations of operand bundles (see documentation above). |
2022 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const; |
2023 | |
2024 | /// Return the operand bundle for the operand at index OpIdx. |
2025 | /// |
2026 | /// It is an error to call this with an OpIdx that does not correspond to an |
2027 | /// bundle operand. |
2028 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
2029 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
2030 | } |
2031 | |
2032 | /// Return true if this operand bundle user has operand bundles that |
2033 | /// may read from the heap. |
2034 | bool hasReadingOperandBundles() const; |
2035 | |
2036 | /// Return true if this operand bundle user has operand bundles that |
2037 | /// may write to the heap. |
2038 | bool hasClobberingOperandBundles() const { |
2039 | for (auto &BOI : bundle_op_infos()) { |
2040 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
2041 | BOI.Tag->second == LLVMContext::OB_funclet) |
2042 | continue; |
2043 | |
2044 | // This instruction has an operand bundle that is not known to us. |
2045 | // Assume the worst. |
2046 | return true; |
2047 | } |
2048 | |
2049 | return false; |
2050 | } |
2051 | |
2052 | /// Return true if the bundle operand at index \p OpIdx has the |
2053 | /// attribute \p A. |
2054 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
2055 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
2056 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
2057 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
2058 | } |
2059 | |
2060 | /// Return true if \p Other has the same sequence of operand bundle |
2061 | /// tags with the same number of operands on each one of them as this |
2062 | /// OperandBundleUser. |
2063 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
2064 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
2065 | return false; |
2066 | |
2067 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
2068 | Other.bundle_op_info_begin()); |
2069 | } |
2070 | |
2071 | /// Return true if this operand bundle user contains operand bundles |
2072 | /// with tags other than those specified in \p IDs. |
2073 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
2074 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2075 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
2076 | if (!is_contained(IDs, ID)) |
2077 | return true; |
2078 | } |
2079 | return false; |
2080 | } |
2081 | |
2082 | /// Is the function attribute S disallowed by some operand bundle on |
2083 | /// this operand bundle user? |
2084 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
2085 | // Operand bundles only possibly disallow readnone, readonly and argmemonly |
2086 | // attributes. All String attributes are fine. |
2087 | return false; |
2088 | } |
2089 | |
2090 | /// Is the function attribute A disallowed by some operand bundle on |
2091 | /// this operand bundle user? |
2092 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
2093 | switch (A) { |
2094 | default: |
2095 | return false; |
2096 | |
2097 | case Attribute::InaccessibleMemOrArgMemOnly: |
2098 | return hasReadingOperandBundles(); |
2099 | |
2100 | case Attribute::InaccessibleMemOnly: |
2101 | return hasReadingOperandBundles(); |
2102 | |
2103 | case Attribute::ArgMemOnly: |
2104 | return hasReadingOperandBundles(); |
2105 | |
2106 | case Attribute::ReadNone: |
2107 | return hasReadingOperandBundles(); |
2108 | |
2109 | case Attribute::ReadOnly: |
2110 | return hasClobberingOperandBundles(); |
2111 | } |
2112 | |
2113 | llvm_unreachable("switch has a default case!")__builtin_unreachable(); |
2114 | } |
2115 | |
2116 | /// Used to keep track of an operand bundle. See the main comment on |
2117 | /// OperandBundleUser above. |
2118 | struct BundleOpInfo { |
2119 | /// The operand bundle tag, interned by |
2120 | /// LLVMContextImpl::getOrInsertBundleTag. |
2121 | StringMapEntry<uint32_t> *Tag; |
2122 | |
2123 | /// The index in the Use& vector where operands for this operand |
2124 | /// bundle starts. |
2125 | uint32_t Begin; |
2126 | |
2127 | /// The index in the Use& vector where operands for this operand |
2128 | /// bundle ends. |
2129 | uint32_t End; |
2130 | |
2131 | bool operator==(const BundleOpInfo &Other) const { |
2132 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
2133 | } |
2134 | }; |
2135 | |
2136 | /// Simple helper function to map a BundleOpInfo to an |
2137 | /// OperandBundleUse. |
2138 | OperandBundleUse |
2139 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
2140 | auto begin = op_begin(); |
2141 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
2142 | return OperandBundleUse(BOI.Tag, Inputs); |
2143 | } |
2144 | |
2145 | using bundle_op_iterator = BundleOpInfo *; |
2146 | using const_bundle_op_iterator = const BundleOpInfo *; |
2147 | |
2148 | /// Return the start of the list of BundleOpInfo instances associated |
2149 | /// with this OperandBundleUser. |
2150 | /// |
2151 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
2152 | /// to store some meta information about which operands are "normal" operands, |
2153 | /// and which ones belong to some operand bundle. |
2154 | /// |
2155 | /// The layout of an operand bundle user is |
2156 | /// |
2157 | /// +-----------uint32_t End-------------------------------------+ |
2158 | /// | | |
2159 | /// | +--------uint32_t Begin--------------------+ | |
2160 | /// | | | | |
2161 | /// ^ ^ v v |
2162 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2163 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
2164 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2165 | /// v v ^ ^ |
2166 | /// | | | | |
2167 | /// | +--------uint32_t Begin------------+ | |
2168 | /// | | |
2169 | /// +-----------uint32_t End-----------------------------+ |
2170 | /// |
2171 | /// |
2172 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
2173 | /// list. These descriptions are installed and managed by this class, and |
2174 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
2175 | /// |
2176 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
2177 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
2178 | /// access or modify DU in any way, it's an implementation detail private to |
2179 | /// User. |
2180 | /// |
2181 | /// The regular Use& vector for the User starts at U0. The operand bundle |
2182 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
2183 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
2184 | /// BundleOpInfo has information about a contiguous set of uses constituting |
2185 | /// an operand bundle, and the total set of operand bundle uses themselves |
2186 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2187 | /// corresponding to individual operand bundles). |
2188 | /// |
2189 | /// This class does not know the location of the set of operand bundle uses |
2190 | /// within the use list -- that is decided by the User using this class via |
2191 | /// the BeginIdx argument in populateBundleOperandInfos. |
2192 | /// |
2193 | /// Currently operand bundle users with hung-off operands are not supported. |
2194 | bundle_op_iterator bundle_op_info_begin() { |
2195 | if (!hasDescriptor()) |
2196 | return nullptr; |
2197 | |
2198 | uint8_t *BytesBegin = getDescriptor().begin(); |
2199 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2200 | } |
2201 | |
2202 | /// Return the start of the list of BundleOpInfo instances associated |
2203 | /// with this OperandBundleUser. |
2204 | const_bundle_op_iterator bundle_op_info_begin() const { |
2205 | auto *NonConstThis = const_cast<CallBase *>(this); |
2206 | return NonConstThis->bundle_op_info_begin(); |
2207 | } |
2208 | |
2209 | /// Return the end of the list of BundleOpInfo instances associated |
2210 | /// with this OperandBundleUser. |
2211 | bundle_op_iterator bundle_op_info_end() { |
2212 | if (!hasDescriptor()) |
2213 | return nullptr; |
2214 | |
2215 | uint8_t *BytesEnd = getDescriptor().end(); |
2216 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2217 | } |
2218 | |
2219 | /// Return the end of the list of BundleOpInfo instances associated |
2220 | /// with this OperandBundleUser. |
2221 | const_bundle_op_iterator bundle_op_info_end() const { |
2222 | auto *NonConstThis = const_cast<CallBase *>(this); |
2223 | return NonConstThis->bundle_op_info_end(); |
2224 | } |
2225 | |
2226 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2227 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2228 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2229 | } |
2230 | |
2231 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2232 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2233 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2234 | } |
2235 | |
2236 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2237 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2238 | /// last bundle operand use. |
2239 | /// |
2240 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2241 | /// instance allocated in this User's descriptor. |
2242 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2243 | const unsigned BeginIndex); |
2244 | |
2245 | public: |
2246 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2247 | /// |
2248 | /// It is an error to call this with an OpIdx that does not correspond to an |
2249 | /// bundle operand. |
2250 | BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx); |
2251 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2252 | return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx); |
2253 | } |
2254 | |
2255 | protected: |
2256 | /// Return the total number of values used in \p Bundles. |
2257 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2258 | unsigned Total = 0; |
2259 | for (auto &B : Bundles) |
2260 | Total += B.input_size(); |
2261 | return Total; |
2262 | } |
2263 | |
2264 | /// @} |
2265 | // End of operand bundle API. |
2266 | |
2267 | private: |
2268 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2269 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2270 | |
2271 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2272 | if (Attrs.hasFnAttr(Kind)) |
2273 | return true; |
2274 | |
2275 | // Operand bundles override attributes on the called function, but don't |
2276 | // override attributes directly present on the call instruction. |
2277 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2278 | return false; |
2279 | |
2280 | return hasFnAttrOnCalledFunction(Kind); |
2281 | } |
2282 | |
2283 | /// Determine whether the return value has the given attribute. Supports |
2284 | /// Attribute::AttrKind and StringRef as \p AttrKind types. |
2285 | template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const { |
2286 | if (Attrs.hasRetAttr(Kind)) |
2287 | return true; |
2288 | |
2289 | // Look at the callee, if available. |
2290 | if (const Function *F = getCalledFunction()) |
2291 | return F->getAttributes().hasRetAttr(Kind); |
2292 | return false; |
2293 | } |
2294 | }; |
2295 | |
2296 | template <> |
2297 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2298 | |
2299 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallBase, Value)CallBase::op_iterator CallBase::op_begin() { return OperandTraits <CallBase>::op_begin(this); } CallBase::const_op_iterator CallBase::op_begin() const { return OperandTraits<CallBase >::op_begin(const_cast<CallBase*>(this)); } CallBase ::op_iterator CallBase::op_end() { return OperandTraits<CallBase >::op_end(this); } CallBase::const_op_iterator CallBase::op_end () const { return OperandTraits<CallBase>::op_end(const_cast <CallBase*>(this)); } Value *CallBase::getOperand(unsigned i_nocapture) const { (static_cast<void> (0)); return cast_or_null <Value>( OperandTraits<CallBase>::op_begin(const_cast <CallBase*>(this))[i_nocapture].get()); } void CallBase ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (static_cast <void> (0)); OperandTraits<CallBase>::op_begin(this )[i_nocapture] = Val_nocapture; } unsigned CallBase::getNumOperands () const { return OperandTraits<CallBase>::operands(this ); } template <int Idx_nocapture> Use &CallBase::Op () { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CallBase::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
2300 | |
2301 | //===----------------------------------------------------------------------===// |
2302 | // FuncletPadInst Class |
2303 | //===----------------------------------------------------------------------===// |
2304 | class FuncletPadInst : public Instruction { |
2305 | private: |
2306 | FuncletPadInst(const FuncletPadInst &CPI); |
2307 | |
2308 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2309 | ArrayRef<Value *> Args, unsigned Values, |
2310 | const Twine &NameStr, Instruction *InsertBefore); |
2311 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2312 | ArrayRef<Value *> Args, unsigned Values, |
2313 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2314 | |
2315 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2316 | |
2317 | protected: |
2318 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2319 | friend class Instruction; |
2320 | friend class CatchPadInst; |
2321 | friend class CleanupPadInst; |
2322 | |
2323 | FuncletPadInst *cloneImpl() const; |
2324 | |
2325 | public: |
2326 | /// Provide fast operand accessors |
2327 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
2328 | |
2329 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2330 | /// |
2331 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2332 | |
2333 | /// Convenience accessors |
2334 | |
2335 | /// Return the outer EH-pad this funclet is nested within. |
2336 | /// |
2337 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2338 | /// is a CatchPadInst. |
2339 | Value *getParentPad() const { return Op<-1>(); } |
2340 | void setParentPad(Value *ParentPad) { |
2341 | assert(ParentPad)(static_cast<void> (0)); |
2342 | Op<-1>() = ParentPad; |
2343 | } |
2344 | |
2345 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2346 | /// |
2347 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2348 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2349 | |
2350 | /// arg_operands - iteration adapter for range-for loops. |
2351 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2352 | |
2353 | /// arg_operands - iteration adapter for range-for loops. |
2354 | const_op_range arg_operands() const { |
2355 | return const_op_range(op_begin(), op_end() - 1); |
2356 | } |
2357 | |
2358 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2359 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2360 | static bool classof(const Value *V) { |
2361 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2362 | } |
2363 | }; |
2364 | |
2365 | template <> |
2366 | struct OperandTraits<FuncletPadInst> |
2367 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2368 | |
2369 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)FuncletPadInst::op_iterator FuncletPadInst::op_begin() { return OperandTraits<FuncletPadInst>::op_begin(this); } FuncletPadInst ::const_op_iterator FuncletPadInst::op_begin() const { return OperandTraits<FuncletPadInst>::op_begin(const_cast< FuncletPadInst*>(this)); } FuncletPadInst::op_iterator FuncletPadInst ::op_end() { return OperandTraits<FuncletPadInst>::op_end (this); } FuncletPadInst::const_op_iterator FuncletPadInst::op_end () const { return OperandTraits<FuncletPadInst>::op_end (const_cast<FuncletPadInst*>(this)); } Value *FuncletPadInst ::getOperand(unsigned i_nocapture) const { (static_cast<void > (0)); return cast_or_null<Value>( OperandTraits< FuncletPadInst>::op_begin(const_cast<FuncletPadInst*> (this))[i_nocapture].get()); } void FuncletPadInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { (static_cast< void> (0)); OperandTraits<FuncletPadInst>::op_begin( this)[i_nocapture] = Val_nocapture; } unsigned FuncletPadInst ::getNumOperands() const { return OperandTraits<FuncletPadInst >::operands(this); } template <int Idx_nocapture> Use &FuncletPadInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & FuncletPadInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
2370 | |
2371 | } // end namespace llvm |
2372 | |
2373 | #endif // LLVM_IR_INSTRTYPES_H |