File: | src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/IR/Instructions.cpp |
Warning: | line 441, 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")((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!")((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!")((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 (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 && | |||
322 | !NullPointerIsDefined(getCaller(), | |||
323 | getType()->getPointerAddressSpace())) | |||
324 | return true; | |||
325 | ||||
326 | return false; | |||
327 | } | |||
328 | ||||
329 | Value *CallBase::getReturnedArgOperand() const { | |||
330 | unsigned Index; | |||
331 | ||||
332 | if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index) | |||
333 | return getArgOperand(Index - AttributeList::FirstArgIndex); | |||
334 | if (const Function *F = getCalledFunction()) | |||
335 | if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) && | |||
336 | Index) | |||
337 | return getArgOperand(Index - AttributeList::FirstArgIndex); | |||
338 | ||||
339 | return nullptr; | |||
340 | } | |||
341 | ||||
342 | /// Determine whether the argument or parameter has the given attribute. | |||
343 | bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { | |||
344 | assert(ArgNo < getNumArgOperands() && "Param index out of bounds!")((void)0); | |||
345 | ||||
346 | if (Attrs.hasParamAttribute(ArgNo, Kind)) | |||
347 | return true; | |||
348 | if (const Function *F = getCalledFunction()) | |||
349 | return F->getAttributes().hasParamAttribute(ArgNo, Kind); | |||
350 | return false; | |||
351 | } | |||
352 | ||||
353 | bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { | |||
354 | if (const Function *F = getCalledFunction()) | |||
355 | return F->getAttributes().hasFnAttribute(Kind); | |||
356 | return false; | |||
357 | } | |||
358 | ||||
359 | bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { | |||
360 | if (const Function *F = getCalledFunction()) | |||
361 | return F->getAttributes().hasFnAttribute(Kind); | |||
362 | return false; | |||
363 | } | |||
364 | ||||
365 | void CallBase::getOperandBundlesAsDefs( | |||
366 | SmallVectorImpl<OperandBundleDef> &Defs) const { | |||
367 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) | |||
368 | Defs.emplace_back(getOperandBundleAt(i)); | |||
369 | } | |||
370 | ||||
371 | CallBase::op_iterator | |||
372 | CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, | |||
373 | const unsigned BeginIndex) { | |||
374 | auto It = op_begin() + BeginIndex; | |||
375 | for (auto &B : Bundles) | |||
376 | It = std::copy(B.input_begin(), B.input_end(), It); | |||
377 | ||||
378 | auto *ContextImpl = getContext().pImpl; | |||
379 | auto BI = Bundles.begin(); | |||
380 | unsigned CurrentIndex = BeginIndex; | |||
381 | ||||
382 | for (auto &BOI : bundle_op_infos()) { | |||
383 | assert(BI != Bundles.end() && "Incorrect allocation?")((void)0); | |||
384 | ||||
385 | BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); | |||
386 | BOI.Begin = CurrentIndex; | |||
387 | BOI.End = CurrentIndex + BI->input_size(); | |||
388 | CurrentIndex = BOI.End; | |||
389 | BI++; | |||
390 | } | |||
391 | ||||
392 | assert(BI == Bundles.end() && "Incorrect allocation?")((void)0); | |||
393 | ||||
394 | return It; | |||
395 | } | |||
396 | ||||
397 | CallBase::BundleOpInfo &CallBase::getBundleOpInfoForOperand(unsigned OpIdx) { | |||
398 | /// When there isn't many bundles, we do a simple linear search. | |||
399 | /// Else fallback to a binary-search that use the fact that bundles usually | |||
400 | /// have similar number of argument to get faster convergence. | |||
401 | if (bundle_op_info_end() - bundle_op_info_begin() < 8) { | |||
| ||||
402 | for (auto &BOI : bundle_op_infos()) | |||
403 | if (BOI.Begin <= OpIdx && OpIdx < BOI.End) | |||
404 | return BOI; | |||
405 | ||||
406 | llvm_unreachable("Did not find operand bundle for operand!")__builtin_unreachable(); | |||
407 | } | |||
408 | ||||
409 | assert(OpIdx >= arg_size() && "the Idx is not in the operand bundles")((void)0); | |||
410 | assert(bundle_op_info_end() - bundle_op_info_begin() > 0 &&((void)0) | |||
411 | OpIdx < std::prev(bundle_op_info_end())->End &&((void)0) | |||
412 | "The Idx isn't in the operand bundle")((void)0); | |||
413 | ||||
414 | /// We need a decimal number below and to prevent using floating point numbers | |||
415 | /// we use an intergal value multiplied by this constant. | |||
416 | constexpr unsigned NumberScaling = 1024; | |||
417 | ||||
418 | bundle_op_iterator Begin = bundle_op_info_begin(); | |||
419 | bundle_op_iterator End = bundle_op_info_end(); | |||
420 | bundle_op_iterator Current = Begin; | |||
421 | ||||
422 | while (Begin != End) { | |||
423 | unsigned ScaledOperandPerBundle = | |||
424 | NumberScaling * (std::prev(End)->End - Begin->Begin) / (End - Begin); | |||
425 | Current = Begin + (((OpIdx - Begin->Begin) * NumberScaling) / | |||
426 | ScaledOperandPerBundle); | |||
427 | if (Current >= End) | |||
428 | Current = std::prev(End); | |||
429 | assert(Current < End && Current >= Begin &&((void)0) | |||
430 | "the operand bundle doesn't cover every value in the range")((void)0); | |||
431 | if (OpIdx >= Current->Begin && OpIdx < Current->End) | |||
432 | break; | |||
433 | if (OpIdx >= Current->End) | |||
434 | Begin = Current + 1; | |||
435 | else | |||
436 | End = Current; | |||
437 | } | |||
438 | ||||
439 | assert(OpIdx >= Current->Begin && OpIdx < Current->End &&((void)0) | |||
440 | "the operand bundle doesn't cover every value in the range")((void)0); | |||
441 | return *Current; | |||
| ||||
442 | } | |||
443 | ||||
444 | CallBase *CallBase::addOperandBundle(CallBase *CB, uint32_t ID, | |||
445 | OperandBundleDef OB, | |||
446 | Instruction *InsertPt) { | |||
447 | if (CB->getOperandBundle(ID)) | |||
448 | return CB; | |||
449 | ||||
450 | SmallVector<OperandBundleDef, 1> Bundles; | |||
451 | CB->getOperandBundlesAsDefs(Bundles); | |||
452 | Bundles.push_back(OB); | |||
453 | return Create(CB, Bundles, InsertPt); | |||
454 | } | |||
455 | ||||
456 | CallBase *CallBase::removeOperandBundle(CallBase *CB, uint32_t ID, | |||
457 | Instruction *InsertPt) { | |||
458 | SmallVector<OperandBundleDef, 1> Bundles; | |||
459 | bool CreateNew = false; | |||
460 | ||||
461 | for (unsigned I = 0, E = CB->getNumOperandBundles(); I != E; ++I) { | |||
462 | auto Bundle = CB->getOperandBundleAt(I); | |||
463 | if (Bundle.getTagID() == ID) { | |||
464 | CreateNew = true; | |||
465 | continue; | |||
466 | } | |||
467 | Bundles.emplace_back(Bundle); | |||
468 | } | |||
469 | ||||
470 | return CreateNew ? Create(CB, Bundles, InsertPt) : CB; | |||
471 | } | |||
472 | ||||
473 | bool CallBase::hasReadingOperandBundles() const { | |||
474 | // Implementation note: this is a conservative implementation of operand | |||
475 | // bundle semantics, where *any* non-assume operand bundle forces a callsite | |||
476 | // to be at least readonly. | |||
477 | return hasOperandBundles() && getIntrinsicID() != Intrinsic::assume; | |||
478 | } | |||
479 | ||||
480 | //===----------------------------------------------------------------------===// | |||
481 | // CallInst Implementation | |||
482 | //===----------------------------------------------------------------------===// | |||
483 | ||||
484 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, | |||
485 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { | |||
486 | this->FTy = FTy; | |||
487 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&((void)0) | |||
488 | "NumOperands not set up?")((void)0); | |||
489 | ||||
490 | #ifndef NDEBUG1 | |||
491 | assert((Args.size() == FTy->getNumParams() ||((void)0) | |||
492 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&((void)0) | |||
493 | "Calling a function with bad signature!")((void)0); | |||
494 | ||||
495 | for (unsigned i = 0; i != Args.size(); ++i) | |||
496 | assert((i >= FTy->getNumParams() ||((void)0) | |||
497 | FTy->getParamType(i) == Args[i]->getType()) &&((void)0) | |||
498 | "Calling a function with a bad signature!")((void)0); | |||
499 | #endif | |||
500 | ||||
501 | // Set operands in order of their index to match use-list-order | |||
502 | // prediction. | |||
503 | llvm::copy(Args, op_begin()); | |||
504 | setCalledOperand(Func); | |||
505 | ||||
506 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
507 | (void)It; | |||
508 | assert(It + 1 == op_end() && "Should add up!")((void)0); | |||
509 | ||||
510 | setName(NameStr); | |||
511 | } | |||
512 | ||||
513 | void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { | |||
514 | this->FTy = FTy; | |||
515 | assert(getNumOperands() == 1 && "NumOperands not set up?")((void)0); | |||
516 | setCalledOperand(Func); | |||
517 | ||||
518 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature")((void)0); | |||
519 | ||||
520 | setName(NameStr); | |||
521 | } | |||
522 | ||||
523 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, | |||
524 | Instruction *InsertBefore) | |||
525 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
526 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { | |||
527 | init(Ty, Func, Name); | |||
528 | } | |||
529 | ||||
530 | CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, | |||
531 | BasicBlock *InsertAtEnd) | |||
532 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
533 | OperandTraits<CallBase>::op_end(this) - 1, 1, InsertAtEnd) { | |||
534 | init(Ty, Func, Name); | |||
535 | } | |||
536 | ||||
537 | CallInst::CallInst(const CallInst &CI) | |||
538 | : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, | |||
539 | OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), | |||
540 | CI.getNumOperands()) { | |||
541 | setTailCallKind(CI.getTailCallKind()); | |||
542 | setCallingConv(CI.getCallingConv()); | |||
543 | ||||
544 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); | |||
545 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), | |||
546 | bundle_op_info_begin()); | |||
547 | SubclassOptionalData = CI.SubclassOptionalData; | |||
548 | } | |||
549 | ||||
550 | CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, | |||
551 | Instruction *InsertPt) { | |||
552 | std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); | |||
553 | ||||
554 | auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledOperand(), | |||
555 | Args, OpB, CI->getName(), InsertPt); | |||
556 | NewCI->setTailCallKind(CI->getTailCallKind()); | |||
557 | NewCI->setCallingConv(CI->getCallingConv()); | |||
558 | NewCI->SubclassOptionalData = CI->SubclassOptionalData; | |||
559 | NewCI->setAttributes(CI->getAttributes()); | |||
560 | NewCI->setDebugLoc(CI->getDebugLoc()); | |||
561 | return NewCI; | |||
562 | } | |||
563 | ||||
564 | // Update profile weight for call instruction by scaling it using the ratio | |||
565 | // of S/T. The meaning of "branch_weights" meta data for call instruction is | |||
566 | // transfered to represent call count. | |||
567 | void CallInst::updateProfWeight(uint64_t S, uint64_t T) { | |||
568 | auto *ProfileData = getMetadata(LLVMContext::MD_prof); | |||
569 | if (ProfileData == nullptr) | |||
570 | return; | |||
571 | ||||
572 | auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); | |||
573 | if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && | |||
574 | !ProfDataName->getString().equals("VP"))) | |||
575 | return; | |||
576 | ||||
577 | if (T == 0) { | |||
578 | LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in "do { } while (false) | |||
579 | "div by 0. Ignoring. Likely the function "do { } while (false) | |||
580 | << getParent()->getParent()->getName()do { } while (false) | |||
581 | << " has 0 entry count, and contains call instructions "do { } while (false) | |||
582 | "with non-zero prof info.")do { } while (false); | |||
583 | return; | |||
584 | } | |||
585 | ||||
586 | MDBuilder MDB(getContext()); | |||
587 | SmallVector<Metadata *, 3> Vals; | |||
588 | Vals.push_back(ProfileData->getOperand(0)); | |||
589 | APInt APS(128, S), APT(128, T); | |||
590 | if (ProfDataName->getString().equals("branch_weights") && | |||
591 | ProfileData->getNumOperands() > 0) { | |||
592 | // Using APInt::div may be expensive, but most cases should fit 64 bits. | |||
593 | APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)) | |||
594 | ->getValue() | |||
595 | .getZExtValue()); | |||
596 | Val *= APS; | |||
597 | Vals.push_back(MDB.createConstant( | |||
598 | ConstantInt::get(Type::getInt32Ty(getContext()), | |||
599 | Val.udiv(APT).getLimitedValue(UINT32_MAX0xffffffffU)))); | |||
600 | } else if (ProfDataName->getString().equals("VP")) | |||
601 | for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { | |||
602 | // The first value is the key of the value profile, which will not change. | |||
603 | Vals.push_back(ProfileData->getOperand(i)); | |||
604 | uint64_t Count = | |||
605 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) | |||
606 | ->getValue() | |||
607 | .getZExtValue(); | |||
608 | // Don't scale the magic number. | |||
609 | if (Count == NOMORE_ICP_MAGICNUM) { | |||
610 | Vals.push_back(ProfileData->getOperand(i + 1)); | |||
611 | continue; | |||
612 | } | |||
613 | // Using APInt::div may be expensive, but most cases should fit 64 bits. | |||
614 | APInt Val(128, Count); | |||
615 | Val *= APS; | |||
616 | Vals.push_back(MDB.createConstant( | |||
617 | ConstantInt::get(Type::getInt64Ty(getContext()), | |||
618 | Val.udiv(APT).getLimitedValue()))); | |||
619 | } | |||
620 | setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); | |||
621 | } | |||
622 | ||||
623 | /// IsConstantOne - Return true only if val is constant int 1 | |||
624 | static bool IsConstantOne(Value *val) { | |||
625 | assert(val && "IsConstantOne does not work with nullptr val")((void)0); | |||
626 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); | |||
627 | return CVal && CVal->isOne(); | |||
628 | } | |||
629 | ||||
630 | static Instruction *createMalloc(Instruction *InsertBefore, | |||
631 | BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
632 | Type *AllocTy, Value *AllocSize, | |||
633 | Value *ArraySize, | |||
634 | ArrayRef<OperandBundleDef> OpB, | |||
635 | Function *MallocF, const Twine &Name) { | |||
636 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&((void)0) | |||
637 | "createMalloc needs either InsertBefore or InsertAtEnd")((void)0); | |||
638 | ||||
639 | // malloc(type) becomes: | |||
640 | // bitcast (i8* malloc(typeSize)) to type* | |||
641 | // malloc(type, arraySize) becomes: | |||
642 | // bitcast (i8* malloc(typeSize*arraySize)) to type* | |||
643 | if (!ArraySize) | |||
644 | ArraySize = ConstantInt::get(IntPtrTy, 1); | |||
645 | else if (ArraySize->getType() != IntPtrTy) { | |||
646 | if (InsertBefore) | |||
647 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, | |||
648 | "", InsertBefore); | |||
649 | else | |||
650 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, | |||
651 | "", InsertAtEnd); | |||
652 | } | |||
653 | ||||
654 | if (!IsConstantOne(ArraySize)) { | |||
655 | if (IsConstantOne(AllocSize)) { | |||
656 | AllocSize = ArraySize; // Operand * 1 = Operand | |||
657 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { | |||
658 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, | |||
659 | false /*ZExt*/); | |||
660 | // Malloc arg is constant product of type size and array size | |||
661 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); | |||
662 | } else { | |||
663 | // Multiply type size by the array size... | |||
664 | if (InsertBefore) | |||
665 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, | |||
666 | "mallocsize", InsertBefore); | |||
667 | else | |||
668 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, | |||
669 | "mallocsize", InsertAtEnd); | |||
670 | } | |||
671 | } | |||
672 | ||||
673 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size")((void)0); | |||
674 | // Create the call to Malloc. | |||
675 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; | |||
676 | Module *M = BB->getParent()->getParent(); | |||
677 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); | |||
678 | FunctionCallee MallocFunc = MallocF; | |||
679 | if (!MallocFunc) | |||
680 | // prototype malloc as "void *malloc(size_t)" | |||
681 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy); | |||
682 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); | |||
683 | CallInst *MCall = nullptr; | |||
684 | Instruction *Result = nullptr; | |||
685 | if (InsertBefore) { | |||
686 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall", | |||
687 | InsertBefore); | |||
688 | Result = MCall; | |||
689 | if (Result->getType() != AllocPtrType) | |||
690 | // Create a cast instruction to convert to the right type... | |||
691 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); | |||
692 | } else { | |||
693 | MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall"); | |||
694 | Result = MCall; | |||
695 | if (Result->getType() != AllocPtrType) { | |||
696 | InsertAtEnd->getInstList().push_back(MCall); | |||
697 | // Create a cast instruction to convert to the right type... | |||
698 | Result = new BitCastInst(MCall, AllocPtrType, Name); | |||
699 | } | |||
700 | } | |||
701 | MCall->setTailCall(); | |||
702 | if (Function *F = dyn_cast<Function>(MallocFunc.getCallee())) { | |||
703 | MCall->setCallingConv(F->getCallingConv()); | |||
704 | if (!F->returnDoesNotAlias()) | |||
705 | F->setReturnDoesNotAlias(); | |||
706 | } | |||
707 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type")((void)0); | |||
708 | ||||
709 | return Result; | |||
710 | } | |||
711 | ||||
712 | /// CreateMalloc - Generate the IR for a call to malloc: | |||
713 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
714 | /// possibly multiplied by the array size if the array size is not | |||
715 | /// constant 1. | |||
716 | /// 2. Call malloc with that argument. | |||
717 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
718 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, | |||
719 | Type *IntPtrTy, Type *AllocTy, | |||
720 | Value *AllocSize, Value *ArraySize, | |||
721 | Function *MallocF, | |||
722 | const Twine &Name) { | |||
723 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, | |||
724 | ArraySize, None, MallocF, Name); | |||
725 | } | |||
726 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, | |||
727 | Type *IntPtrTy, Type *AllocTy, | |||
728 | Value *AllocSize, Value *ArraySize, | |||
729 | ArrayRef<OperandBundleDef> OpB, | |||
730 | Function *MallocF, | |||
731 | const Twine &Name) { | |||
732 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, | |||
733 | ArraySize, OpB, MallocF, Name); | |||
734 | } | |||
735 | ||||
736 | /// CreateMalloc - Generate the IR for a call to malloc: | |||
737 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
738 | /// possibly multiplied by the array size if the array size is not | |||
739 | /// constant 1. | |||
740 | /// 2. Call malloc with that argument. | |||
741 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
742 | /// Note: This function does not add the bitcast to the basic block, that is the | |||
743 | /// responsibility of the caller. | |||
744 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, | |||
745 | Type *IntPtrTy, Type *AllocTy, | |||
746 | Value *AllocSize, Value *ArraySize, | |||
747 | Function *MallocF, const Twine &Name) { | |||
748 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, | |||
749 | ArraySize, None, MallocF, Name); | |||
750 | } | |||
751 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, | |||
752 | Type *IntPtrTy, Type *AllocTy, | |||
753 | Value *AllocSize, Value *ArraySize, | |||
754 | ArrayRef<OperandBundleDef> OpB, | |||
755 | Function *MallocF, const Twine &Name) { | |||
756 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, | |||
757 | ArraySize, OpB, MallocF, Name); | |||
758 | } | |||
759 | ||||
760 | static Instruction *createFree(Value *Source, | |||
761 | ArrayRef<OperandBundleDef> Bundles, | |||
762 | Instruction *InsertBefore, | |||
763 | BasicBlock *InsertAtEnd) { | |||
764 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&((void)0) | |||
765 | "createFree needs either InsertBefore or InsertAtEnd")((void)0); | |||
766 | assert(Source->getType()->isPointerTy() &&((void)0) | |||
767 | "Can not free something of nonpointer type!")((void)0); | |||
768 | ||||
769 | BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; | |||
770 | Module *M = BB->getParent()->getParent(); | |||
771 | ||||
772 | Type *VoidTy = Type::getVoidTy(M->getContext()); | |||
773 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); | |||
774 | // prototype free as "void free(void*)" | |||
775 | FunctionCallee FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy); | |||
776 | CallInst *Result = nullptr; | |||
777 | Value *PtrCast = Source; | |||
778 | if (InsertBefore) { | |||
779 | if (Source->getType() != IntPtrTy) | |||
780 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); | |||
781 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore); | |||
782 | } else { | |||
783 | if (Source->getType() != IntPtrTy) | |||
784 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); | |||
785 | Result = CallInst::Create(FreeFunc, PtrCast, Bundles, ""); | |||
786 | } | |||
787 | Result->setTailCall(); | |||
788 | if (Function *F = dyn_cast<Function>(FreeFunc.getCallee())) | |||
789 | Result->setCallingConv(F->getCallingConv()); | |||
790 | ||||
791 | return Result; | |||
792 | } | |||
793 | ||||
794 | /// CreateFree - Generate the IR for a call to the builtin free function. | |||
795 | Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) { | |||
796 | return createFree(Source, None, InsertBefore, nullptr); | |||
797 | } | |||
798 | Instruction *CallInst::CreateFree(Value *Source, | |||
799 | ArrayRef<OperandBundleDef> Bundles, | |||
800 | Instruction *InsertBefore) { | |||
801 | return createFree(Source, Bundles, InsertBefore, nullptr); | |||
802 | } | |||
803 | ||||
804 | /// CreateFree - Generate the IR for a call to the builtin free function. | |||
805 | /// Note: This function does not add the call to the basic block, that is the | |||
806 | /// responsibility of the caller. | |||
807 | Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) { | |||
808 | Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd); | |||
809 | assert(FreeCall && "CreateFree did not create a CallInst")((void)0); | |||
810 | return FreeCall; | |||
811 | } | |||
812 | Instruction *CallInst::CreateFree(Value *Source, | |||
813 | ArrayRef<OperandBundleDef> Bundles, | |||
814 | BasicBlock *InsertAtEnd) { | |||
815 | Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd); | |||
816 | assert(FreeCall && "CreateFree did not create a CallInst")((void)0); | |||
817 | return FreeCall; | |||
818 | } | |||
819 | ||||
820 | //===----------------------------------------------------------------------===// | |||
821 | // InvokeInst Implementation | |||
822 | //===----------------------------------------------------------------------===// | |||
823 | ||||
824 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, | |||
825 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
826 | ArrayRef<OperandBundleDef> Bundles, | |||
827 | const Twine &NameStr) { | |||
828 | this->FTy = FTy; | |||
829 | ||||
830 | assert((int)getNumOperands() ==((void)0) | |||
831 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) &&((void)0) | |||
832 | "NumOperands not set up?")((void)0); | |||
833 | ||||
834 | #ifndef NDEBUG1 | |||
835 | assert(((Args.size() == FTy->getNumParams()) ||((void)0) | |||
836 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&((void)0) | |||
837 | "Invoking a function with bad signature")((void)0); | |||
838 | ||||
839 | for (unsigned i = 0, e = Args.size(); i != e; i++) | |||
840 | assert((i >= FTy->getNumParams() ||((void)0) | |||
841 | FTy->getParamType(i) == Args[i]->getType()) &&((void)0) | |||
842 | "Invoking a function with a bad signature!")((void)0); | |||
843 | #endif | |||
844 | ||||
845 | // Set operands in order of their index to match use-list-order | |||
846 | // prediction. | |||
847 | llvm::copy(Args, op_begin()); | |||
848 | setNormalDest(IfNormal); | |||
849 | setUnwindDest(IfException); | |||
850 | setCalledOperand(Fn); | |||
851 | ||||
852 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
853 | (void)It; | |||
854 | assert(It + 3 == op_end() && "Should add up!")((void)0); | |||
855 | ||||
856 | setName(NameStr); | |||
857 | } | |||
858 | ||||
859 | InvokeInst::InvokeInst(const InvokeInst &II) | |||
860 | : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, | |||
861 | OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), | |||
862 | II.getNumOperands()) { | |||
863 | setCallingConv(II.getCallingConv()); | |||
864 | std::copy(II.op_begin(), II.op_end(), op_begin()); | |||
865 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), | |||
866 | bundle_op_info_begin()); | |||
867 | SubclassOptionalData = II.SubclassOptionalData; | |||
868 | } | |||
869 | ||||
870 | InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, | |||
871 | Instruction *InsertPt) { | |||
872 | std::vector<Value *> Args(II->arg_begin(), II->arg_end()); | |||
873 | ||||
874 | auto *NewII = InvokeInst::Create( | |||
875 | II->getFunctionType(), II->getCalledOperand(), II->getNormalDest(), | |||
876 | II->getUnwindDest(), Args, OpB, II->getName(), InsertPt); | |||
877 | NewII->setCallingConv(II->getCallingConv()); | |||
878 | NewII->SubclassOptionalData = II->SubclassOptionalData; | |||
879 | NewII->setAttributes(II->getAttributes()); | |||
880 | NewII->setDebugLoc(II->getDebugLoc()); | |||
881 | return NewII; | |||
882 | } | |||
883 | ||||
884 | LandingPadInst *InvokeInst::getLandingPadInst() const { | |||
885 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); | |||
886 | } | |||
887 | ||||
888 | //===----------------------------------------------------------------------===// | |||
889 | // CallBrInst Implementation | |||
890 | //===----------------------------------------------------------------------===// | |||
891 | ||||
892 | void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough, | |||
893 | ArrayRef<BasicBlock *> IndirectDests, | |||
894 | ArrayRef<Value *> Args, | |||
895 | ArrayRef<OperandBundleDef> Bundles, | |||
896 | const Twine &NameStr) { | |||
897 | this->FTy = FTy; | |||
898 | ||||
899 | assert((int)getNumOperands() ==((void)0) | |||
900 | ComputeNumOperands(Args.size(), IndirectDests.size(),((void)0) | |||
901 | CountBundleInputs(Bundles)) &&((void)0) | |||
902 | "NumOperands not set up?")((void)0); | |||
903 | ||||
904 | #ifndef NDEBUG1 | |||
905 | assert(((Args.size() == FTy->getNumParams()) ||((void)0) | |||
906 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&((void)0) | |||
907 | "Calling a function with bad signature")((void)0); | |||
908 | ||||
909 | for (unsigned i = 0, e = Args.size(); i != e; i++) | |||
910 | assert((i >= FTy->getNumParams() ||((void)0) | |||
911 | FTy->getParamType(i) == Args[i]->getType()) &&((void)0) | |||
912 | "Calling a function with a bad signature!")((void)0); | |||
913 | #endif | |||
914 | ||||
915 | // Set operands in order of their index to match use-list-order | |||
916 | // prediction. | |||
917 | std::copy(Args.begin(), Args.end(), op_begin()); | |||
918 | NumIndirectDests = IndirectDests.size(); | |||
919 | setDefaultDest(Fallthrough); | |||
920 | for (unsigned i = 0; i != NumIndirectDests; ++i) | |||
921 | setIndirectDest(i, IndirectDests[i]); | |||
922 | setCalledOperand(Fn); | |||
923 | ||||
924 | auto It = populateBundleOperandInfos(Bundles, Args.size()); | |||
925 | (void)It; | |||
926 | assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!")((void)0); | |||
927 | ||||
928 | setName(NameStr); | |||
929 | } | |||
930 | ||||
931 | void CallBrInst::updateArgBlockAddresses(unsigned i, BasicBlock *B) { | |||
932 | assert(getNumIndirectDests() > i && "IndirectDest # out of range for callbr")((void)0); | |||
933 | if (BasicBlock *OldBB = getIndirectDest(i)) { | |||
934 | BlockAddress *Old = BlockAddress::get(OldBB); | |||
935 | BlockAddress *New = BlockAddress::get(B); | |||
936 | for (unsigned ArgNo = 0, e = getNumArgOperands(); ArgNo != e; ++ArgNo) | |||
937 | if (dyn_cast<BlockAddress>(getArgOperand(ArgNo)) == Old) | |||
938 | setArgOperand(ArgNo, New); | |||
939 | } | |||
940 | } | |||
941 | ||||
942 | CallBrInst::CallBrInst(const CallBrInst &CBI) | |||
943 | : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, | |||
944 | OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), | |||
945 | CBI.getNumOperands()) { | |||
946 | setCallingConv(CBI.getCallingConv()); | |||
947 | std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); | |||
948 | std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), | |||
949 | bundle_op_info_begin()); | |||
950 | SubclassOptionalData = CBI.SubclassOptionalData; | |||
951 | NumIndirectDests = CBI.NumIndirectDests; | |||
952 | } | |||
953 | ||||
954 | CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, | |||
955 | Instruction *InsertPt) { | |||
956 | std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); | |||
957 | ||||
958 | auto *NewCBI = CallBrInst::Create( | |||
959 | CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(), | |||
960 | CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt); | |||
961 | NewCBI->setCallingConv(CBI->getCallingConv()); | |||
962 | NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; | |||
963 | NewCBI->setAttributes(CBI->getAttributes()); | |||
964 | NewCBI->setDebugLoc(CBI->getDebugLoc()); | |||
965 | NewCBI->NumIndirectDests = CBI->NumIndirectDests; | |||
966 | return NewCBI; | |||
967 | } | |||
968 | ||||
969 | //===----------------------------------------------------------------------===// | |||
970 | // ReturnInst Implementation | |||
971 | //===----------------------------------------------------------------------===// | |||
972 | ||||
973 | ReturnInst::ReturnInst(const ReturnInst &RI) | |||
974 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, | |||
975 | OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), | |||
976 | RI.getNumOperands()) { | |||
977 | if (RI.getNumOperands()) | |||
978 | Op<0>() = RI.Op<0>(); | |||
979 | SubclassOptionalData = RI.SubclassOptionalData; | |||
980 | } | |||
981 | ||||
982 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) | |||
983 | : Instruction(Type::getVoidTy(C), Instruction::Ret, | |||
984 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, | |||
985 | InsertBefore) { | |||
986 | if (retVal) | |||
987 | Op<0>() = retVal; | |||
988 | } | |||
989 | ||||
990 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) | |||
991 | : Instruction(Type::getVoidTy(C), Instruction::Ret, | |||
992 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, | |||
993 | InsertAtEnd) { | |||
994 | if (retVal) | |||
995 | Op<0>() = retVal; | |||
996 | } | |||
997 | ||||
998 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) | |||
999 | : Instruction(Type::getVoidTy(Context), Instruction::Ret, | |||
1000 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {} | |||
1001 | ||||
1002 | //===----------------------------------------------------------------------===// | |||
1003 | // ResumeInst Implementation | |||
1004 | //===----------------------------------------------------------------------===// | |||
1005 | ||||
1006 | ResumeInst::ResumeInst(const ResumeInst &RI) | |||
1007 | : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, | |||
1008 | OperandTraits<ResumeInst>::op_begin(this), 1) { | |||
1009 | Op<0>() = RI.Op<0>(); | |||
1010 | } | |||
1011 | ||||
1012 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) | |||
1013 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, | |||
1014 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { | |||
1015 | Op<0>() = Exn; | |||
1016 | } | |||
1017 | ||||
1018 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) | |||
1019 | : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, | |||
1020 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { | |||
1021 | Op<0>() = Exn; | |||
1022 | } | |||
1023 | ||||
1024 | //===----------------------------------------------------------------------===// | |||
1025 | // CleanupReturnInst Implementation | |||
1026 | //===----------------------------------------------------------------------===// | |||
1027 | ||||
1028 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) | |||
1029 | : Instruction(CRI.getType(), Instruction::CleanupRet, | |||
1030 | OperandTraits<CleanupReturnInst>::op_end(this) - | |||
1031 | CRI.getNumOperands(), | |||
1032 | CRI.getNumOperands()) { | |||
1033 | setSubclassData<Instruction::OpaqueField>( | |||
1034 | CRI.getSubclassData<Instruction::OpaqueField>()); | |||
1035 | Op<0>() = CRI.Op<0>(); | |||
1036 | if (CRI.hasUnwindDest()) | |||
1037 | Op<1>() = CRI.Op<1>(); | |||
1038 | } | |||
1039 | ||||
1040 | void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { | |||
1041 | if (UnwindBB) | |||
1042 | setSubclassData<UnwindDestField>(true); | |||
1043 | ||||
1044 | Op<0>() = CleanupPad; | |||
1045 | if (UnwindBB) | |||
1046 | Op<1>() = UnwindBB; | |||
1047 | } | |||
1048 | ||||
1049 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, | |||
1050 | unsigned Values, Instruction *InsertBefore) | |||
1051 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), | |||
1052 | Instruction::CleanupRet, | |||
1053 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, | |||
1054 | Values, InsertBefore) { | |||
1055 | init(CleanupPad, UnwindBB); | |||
1056 | } | |||
1057 | ||||
1058 | CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, | |||
1059 | unsigned Values, BasicBlock *InsertAtEnd) | |||
1060 | : Instruction(Type::getVoidTy(CleanupPad->getContext()), | |||
1061 | Instruction::CleanupRet, | |||
1062 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, | |||
1063 | Values, InsertAtEnd) { | |||
1064 | init(CleanupPad, UnwindBB); | |||
1065 | } | |||
1066 | ||||
1067 | //===----------------------------------------------------------------------===// | |||
1068 | // CatchReturnInst Implementation | |||
1069 | //===----------------------------------------------------------------------===// | |||
1070 | void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { | |||
1071 | Op<0>() = CatchPad; | |||
1072 | Op<1>() = BB; | |||
1073 | } | |||
1074 | ||||
1075 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) | |||
1076 | : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, | |||
1077 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { | |||
1078 | Op<0>() = CRI.Op<0>(); | |||
1079 | Op<1>() = CRI.Op<1>(); | |||
1080 | } | |||
1081 | ||||
1082 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, | |||
1083 | Instruction *InsertBefore) | |||
1084 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, | |||
1085 | OperandTraits<CatchReturnInst>::op_begin(this), 2, | |||
1086 | InsertBefore) { | |||
1087 | init(CatchPad, BB); | |||
1088 | } | |||
1089 | ||||
1090 | CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, | |||
1091 | BasicBlock *InsertAtEnd) | |||
1092 | : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, | |||
1093 | OperandTraits<CatchReturnInst>::op_begin(this), 2, | |||
1094 | InsertAtEnd) { | |||
1095 | init(CatchPad, BB); | |||
1096 | } | |||
1097 | ||||
1098 | //===----------------------------------------------------------------------===// | |||
1099 | // CatchSwitchInst Implementation | |||
1100 | //===----------------------------------------------------------------------===// | |||
1101 | ||||
1102 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
1103 | unsigned NumReservedValues, | |||
1104 | const Twine &NameStr, | |||
1105 | Instruction *InsertBefore) | |||
1106 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, | |||
1107 | InsertBefore) { | |||
1108 | if (UnwindDest) | |||
1109 | ++NumReservedValues; | |||
1110 | init(ParentPad, UnwindDest, NumReservedValues + 1); | |||
1111 | setName(NameStr); | |||
1112 | } | |||
1113 | ||||
1114 | CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
1115 | unsigned NumReservedValues, | |||
1116 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
1117 | : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, | |||
1118 | InsertAtEnd) { | |||
1119 | if (UnwindDest) | |||
1120 | ++NumReservedValues; | |||
1121 | init(ParentPad, UnwindDest, NumReservedValues + 1); | |||
1122 | setName(NameStr); | |||
1123 | } | |||
1124 | ||||
1125 | CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) | |||
1126 | : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, | |||
1127 | CSI.getNumOperands()) { | |||
1128 | init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); | |||
1129 | setNumHungOffUseOperands(ReservedSpace); | |||
1130 | Use *OL = getOperandList(); | |||
1131 | const Use *InOL = CSI.getOperandList(); | |||
1132 | for (unsigned I = 1, E = ReservedSpace; I != E; ++I) | |||
1133 | OL[I] = InOL[I]; | |||
1134 | } | |||
1135 | ||||
1136 | void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, | |||
1137 | unsigned NumReservedValues) { | |||
1138 | assert(ParentPad && NumReservedValues)((void)0); | |||
1139 | ||||
1140 | ReservedSpace = NumReservedValues; | |||
1141 | setNumHungOffUseOperands(UnwindDest ? 2 : 1); | |||
1142 | allocHungoffUses(ReservedSpace); | |||
1143 | ||||
1144 | Op<0>() = ParentPad; | |||
1145 | if (UnwindDest) { | |||
1146 | setSubclassData<UnwindDestField>(true); | |||
1147 | setUnwindDest(UnwindDest); | |||
1148 | } | |||
1149 | } | |||
1150 | ||||
1151 | /// growOperands - grow operands - This grows the operand list in response to a | |||
1152 | /// push_back style of operation. This grows the number of ops by 2 times. | |||
1153 | void CatchSwitchInst::growOperands(unsigned Size) { | |||
1154 | unsigned NumOperands = getNumOperands(); | |||
1155 | assert(NumOperands >= 1)((void)0); | |||
1156 | if (ReservedSpace >= NumOperands + Size) | |||
1157 | return; | |||
1158 | ReservedSpace = (NumOperands + Size / 2) * 2; | |||
1159 | growHungoffUses(ReservedSpace); | |||
1160 | } | |||
1161 | ||||
1162 | void CatchSwitchInst::addHandler(BasicBlock *Handler) { | |||
1163 | unsigned OpNo = getNumOperands(); | |||
1164 | growOperands(1); | |||
1165 | assert(OpNo < ReservedSpace && "Growing didn't work!")((void)0); | |||
1166 | setNumHungOffUseOperands(getNumOperands() + 1); | |||
1167 | getOperandList()[OpNo] = Handler; | |||
1168 | } | |||
1169 | ||||
1170 | void CatchSwitchInst::removeHandler(handler_iterator HI) { | |||
1171 | // Move all subsequent handlers up one. | |||
1172 | Use *EndDst = op_end() - 1; | |||
1173 | for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) | |||
1174 | *CurDst = *(CurDst + 1); | |||
1175 | // Null out the last handler use. | |||
1176 | *EndDst = nullptr; | |||
1177 | ||||
1178 | setNumHungOffUseOperands(getNumOperands() - 1); | |||
1179 | } | |||
1180 | ||||
1181 | //===----------------------------------------------------------------------===// | |||
1182 | // FuncletPadInst Implementation | |||
1183 | //===----------------------------------------------------------------------===// | |||
1184 | void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, | |||
1185 | const Twine &NameStr) { | |||
1186 | assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?")((void)0); | |||
1187 | llvm::copy(Args, op_begin()); | |||
1188 | setParentPad(ParentPad); | |||
1189 | setName(NameStr); | |||
1190 | } | |||
1191 | ||||
1192 | FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) | |||
1193 | : Instruction(FPI.getType(), FPI.getOpcode(), | |||
1194 | OperandTraits<FuncletPadInst>::op_end(this) - | |||
1195 | FPI.getNumOperands(), | |||
1196 | FPI.getNumOperands()) { | |||
1197 | std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); | |||
1198 | setParentPad(FPI.getParentPad()); | |||
1199 | } | |||
1200 | ||||
1201 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, | |||
1202 | ArrayRef<Value *> Args, unsigned Values, | |||
1203 | const Twine &NameStr, Instruction *InsertBefore) | |||
1204 | : Instruction(ParentPad->getType(), Op, | |||
1205 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, | |||
1206 | InsertBefore) { | |||
1207 | init(ParentPad, Args, NameStr); | |||
1208 | } | |||
1209 | ||||
1210 | FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, | |||
1211 | ArrayRef<Value *> Args, unsigned Values, | |||
1212 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
1213 | : Instruction(ParentPad->getType(), Op, | |||
1214 | OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, | |||
1215 | InsertAtEnd) { | |||
1216 | init(ParentPad, Args, NameStr); | |||
1217 | } | |||
1218 | ||||
1219 | //===----------------------------------------------------------------------===// | |||
1220 | // UnreachableInst Implementation | |||
1221 | //===----------------------------------------------------------------------===// | |||
1222 | ||||
1223 | UnreachableInst::UnreachableInst(LLVMContext &Context, | |||
1224 | Instruction *InsertBefore) | |||
1225 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, | |||
1226 | 0, InsertBefore) {} | |||
1227 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) | |||
1228 | : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, | |||
1229 | 0, InsertAtEnd) {} | |||
1230 | ||||
1231 | //===----------------------------------------------------------------------===// | |||
1232 | // BranchInst Implementation | |||
1233 | //===----------------------------------------------------------------------===// | |||
1234 | ||||
1235 | void BranchInst::AssertOK() { | |||
1236 | if (isConditional()) | |||
1237 | assert(getCondition()->getType()->isIntegerTy(1) &&((void)0) | |||
1238 | "May only branch on boolean predicates!")((void)0); | |||
1239 | } | |||
1240 | ||||
1241 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) | |||
1242 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1243 | OperandTraits<BranchInst>::op_end(this) - 1, 1, | |||
1244 | InsertBefore) { | |||
1245 | assert(IfTrue && "Branch destination may not be null!")((void)0); | |||
1246 | Op<-1>() = IfTrue; | |||
1247 | } | |||
1248 | ||||
1249 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
1250 | Instruction *InsertBefore) | |||
1251 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1252 | OperandTraits<BranchInst>::op_end(this) - 3, 3, | |||
1253 | InsertBefore) { | |||
1254 | // Assign in order of operand index to make use-list order predictable. | |||
1255 | Op<-3>() = Cond; | |||
1256 | Op<-2>() = IfFalse; | |||
1257 | Op<-1>() = IfTrue; | |||
1258 | #ifndef NDEBUG1 | |||
1259 | AssertOK(); | |||
1260 | #endif | |||
1261 | } | |||
1262 | ||||
1263 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) | |||
1264 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1265 | OperandTraits<BranchInst>::op_end(this) - 1, 1, InsertAtEnd) { | |||
1266 | assert(IfTrue && "Branch destination may not be null!")((void)0); | |||
1267 | Op<-1>() = IfTrue; | |||
1268 | } | |||
1269 | ||||
1270 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
1271 | BasicBlock *InsertAtEnd) | |||
1272 | : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, | |||
1273 | OperandTraits<BranchInst>::op_end(this) - 3, 3, InsertAtEnd) { | |||
1274 | // Assign in order of operand index to make use-list order predictable. | |||
1275 | Op<-3>() = Cond; | |||
1276 | Op<-2>() = IfFalse; | |||
1277 | Op<-1>() = IfTrue; | |||
1278 | #ifndef NDEBUG1 | |||
1279 | AssertOK(); | |||
1280 | #endif | |||
1281 | } | |||
1282 | ||||
1283 | BranchInst::BranchInst(const BranchInst &BI) | |||
1284 | : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, | |||
1285 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), | |||
1286 | BI.getNumOperands()) { | |||
1287 | // Assign in order of operand index to make use-list order predictable. | |||
1288 | if (BI.getNumOperands() != 1) { | |||
1289 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!")((void)0); | |||
1290 | Op<-3>() = BI.Op<-3>(); | |||
1291 | Op<-2>() = BI.Op<-2>(); | |||
1292 | } | |||
1293 | Op<-1>() = BI.Op<-1>(); | |||
1294 | SubclassOptionalData = BI.SubclassOptionalData; | |||
1295 | } | |||
1296 | ||||
1297 | void BranchInst::swapSuccessors() { | |||
1298 | assert(isConditional() &&((void)0) | |||
1299 | "Cannot swap successors of an unconditional branch")((void)0); | |||
1300 | Op<-1>().swap(Op<-2>()); | |||
1301 | ||||
1302 | // Update profile metadata if present and it matches our structural | |||
1303 | // expectations. | |||
1304 | swapProfMetadata(); | |||
1305 | } | |||
1306 | ||||
1307 | //===----------------------------------------------------------------------===// | |||
1308 | // AllocaInst Implementation | |||
1309 | //===----------------------------------------------------------------------===// | |||
1310 | ||||
1311 | static Value *getAISize(LLVMContext &Context, Value *Amt) { | |||
1312 | if (!Amt) | |||
1313 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); | |||
1314 | else { | |||
1315 | assert(!isa<BasicBlock>(Amt) &&((void)0) | |||
1316 | "Passed basic block into allocation size parameter! Use other ctor")((void)0); | |||
1317 | assert(Amt->getType()->isIntegerTy() &&((void)0) | |||
1318 | "Allocation array size is not an integer!")((void)0); | |||
1319 | } | |||
1320 | return Amt; | |||
1321 | } | |||
1322 | ||||
1323 | static Align computeAllocaDefaultAlign(Type *Ty, BasicBlock *BB) { | |||
1324 | assert(BB && "Insertion BB cannot be null when alignment not provided!")((void)0); | |||
1325 | assert(BB->getParent() &&((void)0) | |||
1326 | "BB must be in a Function when alignment not provided!")((void)0); | |||
1327 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1328 | return DL.getPrefTypeAlign(Ty); | |||
1329 | } | |||
1330 | ||||
1331 | static Align computeAllocaDefaultAlign(Type *Ty, Instruction *I) { | |||
1332 | assert(I && "Insertion position cannot be null when alignment not provided!")((void)0); | |||
1333 | return computeAllocaDefaultAlign(Ty, I->getParent()); | |||
1334 | } | |||
1335 | ||||
1336 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, | |||
1337 | Instruction *InsertBefore) | |||
1338 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} | |||
1339 | ||||
1340 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, | |||
1341 | BasicBlock *InsertAtEnd) | |||
1342 | : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} | |||
1343 | ||||
1344 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1345 | const Twine &Name, Instruction *InsertBefore) | |||
1346 | : AllocaInst(Ty, AddrSpace, ArraySize, | |||
1347 | computeAllocaDefaultAlign(Ty, InsertBefore), Name, | |||
1348 | InsertBefore) {} | |||
1349 | ||||
1350 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1351 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1352 | : AllocaInst(Ty, AddrSpace, ArraySize, | |||
1353 | computeAllocaDefaultAlign(Ty, InsertAtEnd), Name, | |||
1354 | InsertAtEnd) {} | |||
1355 | ||||
1356 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1357 | Align Align, const Twine &Name, | |||
1358 | Instruction *InsertBefore) | |||
1359 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, | |||
1360 | getAISize(Ty->getContext(), ArraySize), InsertBefore), | |||
1361 | AllocatedType(Ty) { | |||
1362 | setAlignment(Align); | |||
1363 | assert(!Ty->isVoidTy() && "Cannot allocate void!")((void)0); | |||
1364 | setName(Name); | |||
1365 | } | |||
1366 | ||||
1367 | AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
1368 | Align Align, const Twine &Name, BasicBlock *InsertAtEnd) | |||
1369 | : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, | |||
1370 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), | |||
1371 | AllocatedType(Ty) { | |||
1372 | setAlignment(Align); | |||
1373 | assert(!Ty->isVoidTy() && "Cannot allocate void!")((void)0); | |||
1374 | setName(Name); | |||
1375 | } | |||
1376 | ||||
1377 | ||||
1378 | bool AllocaInst::isArrayAllocation() const { | |||
1379 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) | |||
1380 | return !CI->isOne(); | |||
1381 | return true; | |||
1382 | } | |||
1383 | ||||
1384 | /// isStaticAlloca - Return true if this alloca is in the entry block of the | |||
1385 | /// function and is a constant size. If so, the code generator will fold it | |||
1386 | /// into the prolog/epilog code, so it is basically free. | |||
1387 | bool AllocaInst::isStaticAlloca() const { | |||
1388 | // Must be constant size. | |||
1389 | if (!isa<ConstantInt>(getArraySize())) return false; | |||
1390 | ||||
1391 | // Must be in the entry block. | |||
1392 | const BasicBlock *Parent = getParent(); | |||
1393 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); | |||
1394 | } | |||
1395 | ||||
1396 | //===----------------------------------------------------------------------===// | |||
1397 | // LoadInst Implementation | |||
1398 | //===----------------------------------------------------------------------===// | |||
1399 | ||||
1400 | void LoadInst::AssertOK() { | |||
1401 | assert(getOperand(0)->getType()->isPointerTy() &&((void)0) | |||
1402 | "Ptr must have pointer type.")((void)0); | |||
1403 | assert(!(isAtomic() && getAlignment() == 0) &&((void)0) | |||
1404 | "Alignment required for atomic load")((void)0); | |||
1405 | } | |||
1406 | ||||
1407 | static Align computeLoadStoreDefaultAlign(Type *Ty, BasicBlock *BB) { | |||
1408 | assert(BB && "Insertion BB cannot be null when alignment not provided!")((void)0); | |||
1409 | assert(BB->getParent() &&((void)0) | |||
1410 | "BB must be in a Function when alignment not provided!")((void)0); | |||
1411 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1412 | return DL.getABITypeAlign(Ty); | |||
1413 | } | |||
1414 | ||||
1415 | static Align computeLoadStoreDefaultAlign(Type *Ty, Instruction *I) { | |||
1416 | assert(I && "Insertion position cannot be null when alignment not provided!")((void)0); | |||
1417 | return computeLoadStoreDefaultAlign(Ty, I->getParent()); | |||
1418 | } | |||
1419 | ||||
1420 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, | |||
1421 | Instruction *InsertBef) | |||
1422 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} | |||
1423 | ||||
1424 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, | |||
1425 | BasicBlock *InsertAE) | |||
1426 | : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertAE) {} | |||
1427 | ||||
1428 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1429 | Instruction *InsertBef) | |||
1430 | : LoadInst(Ty, Ptr, Name, isVolatile, | |||
1431 | computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {} | |||
1432 | ||||
1433 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1434 | BasicBlock *InsertAE) | |||
1435 | : LoadInst(Ty, Ptr, Name, isVolatile, | |||
1436 | computeLoadStoreDefaultAlign(Ty, InsertAE), InsertAE) {} | |||
1437 | ||||
1438 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1439 | Align Align, Instruction *InsertBef) | |||
1440 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1441 | SyncScope::System, InsertBef) {} | |||
1442 | ||||
1443 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1444 | Align Align, BasicBlock *InsertAE) | |||
1445 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1446 | SyncScope::System, InsertAE) {} | |||
1447 | ||||
1448 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1449 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
1450 | Instruction *InsertBef) | |||
1451 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { | |||
1452 | assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty))((void)0); | |||
1453 | setVolatile(isVolatile); | |||
1454 | setAlignment(Align); | |||
1455 | setAtomic(Order, SSID); | |||
1456 | AssertOK(); | |||
1457 | setName(Name); | |||
1458 | } | |||
1459 | ||||
1460 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, | |||
1461 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
1462 | BasicBlock *InsertAE) | |||
1463 | : UnaryInstruction(Ty, Load, Ptr, InsertAE) { | |||
1464 | assert(cast<PointerType>(Ptr->getType())->isOpaqueOrPointeeTypeMatches(Ty))((void)0); | |||
1465 | setVolatile(isVolatile); | |||
1466 | setAlignment(Align); | |||
1467 | setAtomic(Order, SSID); | |||
1468 | AssertOK(); | |||
1469 | setName(Name); | |||
1470 | } | |||
1471 | ||||
1472 | //===----------------------------------------------------------------------===// | |||
1473 | // StoreInst Implementation | |||
1474 | //===----------------------------------------------------------------------===// | |||
1475 | ||||
1476 | void StoreInst::AssertOK() { | |||
1477 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!")((void)0); | |||
1478 | assert(getOperand(1)->getType()->isPointerTy() &&((void)0) | |||
1479 | "Ptr must have pointer type!")((void)0); | |||
1480 | assert(cast<PointerType>(getOperand(1)->getType())((void)0) | |||
1481 | ->isOpaqueOrPointeeTypeMatches(getOperand(0)->getType()) &&((void)0) | |||
1482 | "Ptr must be a pointer to Val type!")((void)0); | |||
1483 | assert(!(isAtomic() && getAlignment() == 0) &&((void)0) | |||
1484 | "Alignment required for atomic store")((void)0); | |||
1485 | } | |||
1486 | ||||
1487 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) | |||
1488 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} | |||
1489 | ||||
1490 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) | |||
1491 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} | |||
1492 | ||||
1493 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, | |||
1494 | Instruction *InsertBefore) | |||
1495 | : StoreInst(val, addr, isVolatile, | |||
1496 | computeLoadStoreDefaultAlign(val->getType(), InsertBefore), | |||
1497 | InsertBefore) {} | |||
1498 | ||||
1499 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, | |||
1500 | BasicBlock *InsertAtEnd) | |||
1501 | : StoreInst(val, addr, isVolatile, | |||
1502 | computeLoadStoreDefaultAlign(val->getType(), InsertAtEnd), | |||
1503 | InsertAtEnd) {} | |||
1504 | ||||
1505 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1506 | Instruction *InsertBefore) | |||
1507 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1508 | SyncScope::System, InsertBefore) {} | |||
1509 | ||||
1510 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1511 | BasicBlock *InsertAtEnd) | |||
1512 | : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, | |||
1513 | SyncScope::System, InsertAtEnd) {} | |||
1514 | ||||
1515 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1516 | AtomicOrdering Order, SyncScope::ID SSID, | |||
1517 | Instruction *InsertBefore) | |||
1518 | : Instruction(Type::getVoidTy(val->getContext()), Store, | |||
1519 | OperandTraits<StoreInst>::op_begin(this), | |||
1520 | OperandTraits<StoreInst>::operands(this), InsertBefore) { | |||
1521 | Op<0>() = val; | |||
1522 | Op<1>() = addr; | |||
1523 | setVolatile(isVolatile); | |||
1524 | setAlignment(Align); | |||
1525 | setAtomic(Order, SSID); | |||
1526 | AssertOK(); | |||
1527 | } | |||
1528 | ||||
1529 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, | |||
1530 | AtomicOrdering Order, SyncScope::ID SSID, | |||
1531 | BasicBlock *InsertAtEnd) | |||
1532 | : Instruction(Type::getVoidTy(val->getContext()), Store, | |||
1533 | OperandTraits<StoreInst>::op_begin(this), | |||
1534 | OperandTraits<StoreInst>::operands(this), InsertAtEnd) { | |||
1535 | Op<0>() = val; | |||
1536 | Op<1>() = addr; | |||
1537 | setVolatile(isVolatile); | |||
1538 | setAlignment(Align); | |||
1539 | setAtomic(Order, SSID); | |||
1540 | AssertOK(); | |||
1541 | } | |||
1542 | ||||
1543 | ||||
1544 | //===----------------------------------------------------------------------===// | |||
1545 | // AtomicCmpXchgInst Implementation | |||
1546 | //===----------------------------------------------------------------------===// | |||
1547 | ||||
1548 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1549 | Align Alignment, AtomicOrdering SuccessOrdering, | |||
1550 | AtomicOrdering FailureOrdering, | |||
1551 | SyncScope::ID SSID) { | |||
1552 | Op<0>() = Ptr; | |||
1553 | Op<1>() = Cmp; | |||
1554 | Op<2>() = NewVal; | |||
1555 | setSuccessOrdering(SuccessOrdering); | |||
1556 | setFailureOrdering(FailureOrdering); | |||
1557 | setSyncScopeID(SSID); | |||
1558 | setAlignment(Alignment); | |||
1559 | ||||
1560 | assert(getOperand(0) && getOperand(1) && getOperand(2) &&((void)0) | |||
1561 | "All operands must be non-null!")((void)0); | |||
1562 | assert(getOperand(0)->getType()->isPointerTy() &&((void)0) | |||
1563 | "Ptr must have pointer type!")((void)0); | |||
1564 | assert(cast<PointerType>(getOperand(0)->getType())((void)0) | |||
1565 | ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&((void)0) | |||
1566 | "Ptr must be a pointer to Cmp type!")((void)0); | |||
1567 | assert(cast<PointerType>(getOperand(0)->getType())((void)0) | |||
1568 | ->isOpaqueOrPointeeTypeMatches(getOperand(2)->getType()) &&((void)0) | |||
1569 | "Ptr must be a pointer to NewVal type!")((void)0); | |||
1570 | assert(getOperand(1)->getType() == getOperand(2)->getType() &&((void)0) | |||
1571 | "Cmp type and NewVal type must be same!")((void)0); | |||
1572 | } | |||
1573 | ||||
1574 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1575 | Align Alignment, | |||
1576 | AtomicOrdering SuccessOrdering, | |||
1577 | AtomicOrdering FailureOrdering, | |||
1578 | SyncScope::ID SSID, | |||
1579 | Instruction *InsertBefore) | |||
1580 | : Instruction( | |||
1581 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), | |||
1582 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), | |||
1583 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { | |||
1584 | Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); | |||
1585 | } | |||
1586 | ||||
1587 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, | |||
1588 | Align Alignment, | |||
1589 | AtomicOrdering SuccessOrdering, | |||
1590 | AtomicOrdering FailureOrdering, | |||
1591 | SyncScope::ID SSID, | |||
1592 | BasicBlock *InsertAtEnd) | |||
1593 | : Instruction( | |||
1594 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), | |||
1595 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), | |||
1596 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { | |||
1597 | Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); | |||
1598 | } | |||
1599 | ||||
1600 | //===----------------------------------------------------------------------===// | |||
1601 | // AtomicRMWInst Implementation | |||
1602 | //===----------------------------------------------------------------------===// | |||
1603 | ||||
1604 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, | |||
1605 | Align Alignment, AtomicOrdering Ordering, | |||
1606 | SyncScope::ID SSID) { | |||
1607 | Op<0>() = Ptr; | |||
1608 | Op<1>() = Val; | |||
1609 | setOperation(Operation); | |||
1610 | setOrdering(Ordering); | |||
1611 | setSyncScopeID(SSID); | |||
1612 | setAlignment(Alignment); | |||
1613 | ||||
1614 | assert(getOperand(0) && getOperand(1) &&((void)0) | |||
1615 | "All operands must be non-null!")((void)0); | |||
1616 | assert(getOperand(0)->getType()->isPointerTy() &&((void)0) | |||
1617 | "Ptr must have pointer type!")((void)0); | |||
1618 | assert(cast<PointerType>(getOperand(0)->getType())((void)0) | |||
1619 | ->isOpaqueOrPointeeTypeMatches(getOperand(1)->getType()) &&((void)0) | |||
1620 | "Ptr must be a pointer to Val type!")((void)0); | |||
1621 | assert(Ordering != AtomicOrdering::NotAtomic &&((void)0) | |||
1622 | "AtomicRMW instructions must be atomic!")((void)0); | |||
1623 | } | |||
1624 | ||||
1625 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
1626 | Align Alignment, AtomicOrdering Ordering, | |||
1627 | SyncScope::ID SSID, Instruction *InsertBefore) | |||
1628 | : Instruction(Val->getType(), AtomicRMW, | |||
1629 | OperandTraits<AtomicRMWInst>::op_begin(this), | |||
1630 | OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) { | |||
1631 | Init(Operation, Ptr, Val, Alignment, Ordering, SSID); | |||
1632 | } | |||
1633 | ||||
1634 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, | |||
1635 | Align Alignment, AtomicOrdering Ordering, | |||
1636 | SyncScope::ID SSID, BasicBlock *InsertAtEnd) | |||
1637 | : Instruction(Val->getType(), AtomicRMW, | |||
1638 | OperandTraits<AtomicRMWInst>::op_begin(this), | |||
1639 | OperandTraits<AtomicRMWInst>::operands(this), InsertAtEnd) { | |||
1640 | Init(Operation, Ptr, Val, Alignment, Ordering, SSID); | |||
1641 | } | |||
1642 | ||||
1643 | StringRef AtomicRMWInst::getOperationName(BinOp Op) { | |||
1644 | switch (Op) { | |||
1645 | case AtomicRMWInst::Xchg: | |||
1646 | return "xchg"; | |||
1647 | case AtomicRMWInst::Add: | |||
1648 | return "add"; | |||
1649 | case AtomicRMWInst::Sub: | |||
1650 | return "sub"; | |||
1651 | case AtomicRMWInst::And: | |||
1652 | return "and"; | |||
1653 | case AtomicRMWInst::Nand: | |||
1654 | return "nand"; | |||
1655 | case AtomicRMWInst::Or: | |||
1656 | return "or"; | |||
1657 | case AtomicRMWInst::Xor: | |||
1658 | return "xor"; | |||
1659 | case AtomicRMWInst::Max: | |||
1660 | return "max"; | |||
1661 | case AtomicRMWInst::Min: | |||
1662 | return "min"; | |||
1663 | case AtomicRMWInst::UMax: | |||
1664 | return "umax"; | |||
1665 | case AtomicRMWInst::UMin: | |||
1666 | return "umin"; | |||
1667 | case AtomicRMWInst::FAdd: | |||
1668 | return "fadd"; | |||
1669 | case AtomicRMWInst::FSub: | |||
1670 | return "fsub"; | |||
1671 | case AtomicRMWInst::BAD_BINOP: | |||
1672 | return "<invalid operation>"; | |||
1673 | } | |||
1674 | ||||
1675 | llvm_unreachable("invalid atomicrmw operation")__builtin_unreachable(); | |||
1676 | } | |||
1677 | ||||
1678 | //===----------------------------------------------------------------------===// | |||
1679 | // FenceInst Implementation | |||
1680 | //===----------------------------------------------------------------------===// | |||
1681 | ||||
1682 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
1683 | SyncScope::ID SSID, | |||
1684 | Instruction *InsertBefore) | |||
1685 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { | |||
1686 | setOrdering(Ordering); | |||
1687 | setSyncScopeID(SSID); | |||
1688 | } | |||
1689 | ||||
1690 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
1691 | SyncScope::ID SSID, | |||
1692 | BasicBlock *InsertAtEnd) | |||
1693 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { | |||
1694 | setOrdering(Ordering); | |||
1695 | setSyncScopeID(SSID); | |||
1696 | } | |||
1697 | ||||
1698 | //===----------------------------------------------------------------------===// | |||
1699 | // GetElementPtrInst Implementation | |||
1700 | //===----------------------------------------------------------------------===// | |||
1701 | ||||
1702 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, | |||
1703 | const Twine &Name) { | |||
1704 | assert(getNumOperands() == 1 + IdxList.size() &&((void)0) | |||
1705 | "NumOperands not initialized?")((void)0); | |||
1706 | Op<0>() = Ptr; | |||
1707 | llvm::copy(IdxList, op_begin() + 1); | |||
1708 | setName(Name); | |||
1709 | } | |||
1710 | ||||
1711 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) | |||
1712 | : Instruction(GEPI.getType(), GetElementPtr, | |||
1713 | OperandTraits<GetElementPtrInst>::op_end(this) - | |||
1714 | GEPI.getNumOperands(), | |||
1715 | GEPI.getNumOperands()), | |||
1716 | SourceElementType(GEPI.SourceElementType), | |||
1717 | ResultElementType(GEPI.ResultElementType) { | |||
1718 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); | |||
1719 | SubclassOptionalData = GEPI.SubclassOptionalData; | |||
1720 | } | |||
1721 | ||||
1722 | Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) { | |||
1723 | if (auto *Struct = dyn_cast<StructType>(Ty)) { | |||
1724 | if (!Struct->indexValid(Idx)) | |||
1725 | return nullptr; | |||
1726 | return Struct->getTypeAtIndex(Idx); | |||
1727 | } | |||
1728 | if (!Idx->getType()->isIntOrIntVectorTy()) | |||
1729 | return nullptr; | |||
1730 | if (auto *Array = dyn_cast<ArrayType>(Ty)) | |||
1731 | return Array->getElementType(); | |||
1732 | if (auto *Vector = dyn_cast<VectorType>(Ty)) | |||
1733 | return Vector->getElementType(); | |||
1734 | return nullptr; | |||
1735 | } | |||
1736 | ||||
1737 | Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) { | |||
1738 | if (auto *Struct = dyn_cast<StructType>(Ty)) { | |||
1739 | if (Idx >= Struct->getNumElements()) | |||
1740 | return nullptr; | |||
1741 | return Struct->getElementType(Idx); | |||
1742 | } | |||
1743 | if (auto *Array = dyn_cast<ArrayType>(Ty)) | |||
1744 | return Array->getElementType(); | |||
1745 | if (auto *Vector = dyn_cast<VectorType>(Ty)) | |||
1746 | return Vector->getElementType(); | |||
1747 | return nullptr; | |||
1748 | } | |||
1749 | ||||
1750 | template <typename IndexTy> | |||
1751 | static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) { | |||
1752 | if (IdxList.empty()) | |||
1753 | return Ty; | |||
1754 | for (IndexTy V : IdxList.slice(1)) { | |||
1755 | Ty = GetElementPtrInst::getTypeAtIndex(Ty, V); | |||
1756 | if (!Ty) | |||
1757 | return Ty; | |||
1758 | } | |||
1759 | return Ty; | |||
1760 | } | |||
1761 | ||||
1762 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { | |||
1763 | return getIndexedTypeInternal(Ty, IdxList); | |||
1764 | } | |||
1765 | ||||
1766 | Type *GetElementPtrInst::getIndexedType(Type *Ty, | |||
1767 | ArrayRef<Constant *> IdxList) { | |||
1768 | return getIndexedTypeInternal(Ty, IdxList); | |||
1769 | } | |||
1770 | ||||
1771 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { | |||
1772 | return getIndexedTypeInternal(Ty, IdxList); | |||
1773 | } | |||
1774 | ||||
1775 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are | |||
1776 | /// zeros. If so, the result pointer and the first operand have the same | |||
1777 | /// value, just potentially different types. | |||
1778 | bool GetElementPtrInst::hasAllZeroIndices() const { | |||
1779 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { | |||
1780 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { | |||
1781 | if (!CI->isZero()) return false; | |||
1782 | } else { | |||
1783 | return false; | |||
1784 | } | |||
1785 | } | |||
1786 | return true; | |||
1787 | } | |||
1788 | ||||
1789 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are | |||
1790 | /// constant integers. If so, the result pointer and the first operand have | |||
1791 | /// a constant offset between them. | |||
1792 | bool GetElementPtrInst::hasAllConstantIndices() const { | |||
1793 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { | |||
1794 | if (!isa<ConstantInt>(getOperand(i))) | |||
1795 | return false; | |||
1796 | } | |||
1797 | return true; | |||
1798 | } | |||
1799 | ||||
1800 | void GetElementPtrInst::setIsInBounds(bool B) { | |||
1801 | cast<GEPOperator>(this)->setIsInBounds(B); | |||
1802 | } | |||
1803 | ||||
1804 | bool GetElementPtrInst::isInBounds() const { | |||
1805 | return cast<GEPOperator>(this)->isInBounds(); | |||
1806 | } | |||
1807 | ||||
1808 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, | |||
1809 | APInt &Offset) const { | |||
1810 | // Delegate to the generic GEPOperator implementation. | |||
1811 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); | |||
1812 | } | |||
1813 | ||||
1814 | bool GetElementPtrInst::collectOffset( | |||
1815 | const DataLayout &DL, unsigned BitWidth, | |||
1816 | MapVector<Value *, APInt> &VariableOffsets, | |||
1817 | APInt &ConstantOffset) const { | |||
1818 | // Delegate to the generic GEPOperator implementation. | |||
1819 | return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets, | |||
1820 | ConstantOffset); | |||
1821 | } | |||
1822 | ||||
1823 | //===----------------------------------------------------------------------===// | |||
1824 | // ExtractElementInst Implementation | |||
1825 | //===----------------------------------------------------------------------===// | |||
1826 | ||||
1827 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, | |||
1828 | const Twine &Name, | |||
1829 | Instruction *InsertBef) | |||
1830 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), | |||
1831 | ExtractElement, | |||
1832 | OperandTraits<ExtractElementInst>::op_begin(this), | |||
1833 | 2, InsertBef) { | |||
1834 | assert(isValidOperands(Val, Index) &&((void)0) | |||
1835 | "Invalid extractelement instruction operands!")((void)0); | |||
1836 | Op<0>() = Val; | |||
1837 | Op<1>() = Index; | |||
1838 | setName(Name); | |||
1839 | } | |||
1840 | ||||
1841 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, | |||
1842 | const Twine &Name, | |||
1843 | BasicBlock *InsertAE) | |||
1844 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), | |||
1845 | ExtractElement, | |||
1846 | OperandTraits<ExtractElementInst>::op_begin(this), | |||
1847 | 2, InsertAE) { | |||
1848 | assert(isValidOperands(Val, Index) &&((void)0) | |||
1849 | "Invalid extractelement instruction operands!")((void)0); | |||
1850 | ||||
1851 | Op<0>() = Val; | |||
1852 | Op<1>() = Index; | |||
1853 | setName(Name); | |||
1854 | } | |||
1855 | ||||
1856 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { | |||
1857 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) | |||
1858 | return false; | |||
1859 | return true; | |||
1860 | } | |||
1861 | ||||
1862 | //===----------------------------------------------------------------------===// | |||
1863 | // InsertElementInst Implementation | |||
1864 | //===----------------------------------------------------------------------===// | |||
1865 | ||||
1866 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, | |||
1867 | const Twine &Name, | |||
1868 | Instruction *InsertBef) | |||
1869 | : Instruction(Vec->getType(), InsertElement, | |||
1870 | OperandTraits<InsertElementInst>::op_begin(this), | |||
1871 | 3, InsertBef) { | |||
1872 | assert(isValidOperands(Vec, Elt, Index) &&((void)0) | |||
1873 | "Invalid insertelement instruction operands!")((void)0); | |||
1874 | Op<0>() = Vec; | |||
1875 | Op<1>() = Elt; | |||
1876 | Op<2>() = Index; | |||
1877 | setName(Name); | |||
1878 | } | |||
1879 | ||||
1880 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, | |||
1881 | const Twine &Name, | |||
1882 | BasicBlock *InsertAE) | |||
1883 | : Instruction(Vec->getType(), InsertElement, | |||
1884 | OperandTraits<InsertElementInst>::op_begin(this), | |||
1885 | 3, InsertAE) { | |||
1886 | assert(isValidOperands(Vec, Elt, Index) &&((void)0) | |||
1887 | "Invalid insertelement instruction operands!")((void)0); | |||
1888 | ||||
1889 | Op<0>() = Vec; | |||
1890 | Op<1>() = Elt; | |||
1891 | Op<2>() = Index; | |||
1892 | setName(Name); | |||
1893 | } | |||
1894 | ||||
1895 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, | |||
1896 | const Value *Index) { | |||
1897 | if (!Vec->getType()->isVectorTy()) | |||
1898 | return false; // First operand of insertelement must be vector type. | |||
1899 | ||||
1900 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) | |||
1901 | return false;// Second operand of insertelement must be vector element type. | |||
1902 | ||||
1903 | if (!Index->getType()->isIntegerTy()) | |||
1904 | return false; // Third operand of insertelement must be i32. | |||
1905 | return true; | |||
1906 | } | |||
1907 | ||||
1908 | //===----------------------------------------------------------------------===// | |||
1909 | // ShuffleVectorInst Implementation | |||
1910 | //===----------------------------------------------------------------------===// | |||
1911 | ||||
1912 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1913 | const Twine &Name, | |||
1914 | Instruction *InsertBefore) | |||
1915 | : Instruction( | |||
1916 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1917 | cast<VectorType>(Mask->getType())->getElementCount()), | |||
1918 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1919 | OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { | |||
1920 | assert(isValidOperands(V1, V2, Mask) &&((void)0) | |||
1921 | "Invalid shuffle vector instruction operands!")((void)0); | |||
1922 | ||||
1923 | Op<0>() = V1; | |||
1924 | Op<1>() = V2; | |||
1925 | SmallVector<int, 16> MaskArr; | |||
1926 | getShuffleMask(cast<Constant>(Mask), MaskArr); | |||
1927 | setShuffleMask(MaskArr); | |||
1928 | setName(Name); | |||
1929 | } | |||
1930 | ||||
1931 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
1932 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1933 | : Instruction( | |||
1934 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1935 | cast<VectorType>(Mask->getType())->getElementCount()), | |||
1936 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1937 | OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { | |||
1938 | assert(isValidOperands(V1, V2, Mask) &&((void)0) | |||
1939 | "Invalid shuffle vector instruction operands!")((void)0); | |||
1940 | ||||
1941 | Op<0>() = V1; | |||
1942 | Op<1>() = V2; | |||
1943 | SmallVector<int, 16> MaskArr; | |||
1944 | getShuffleMask(cast<Constant>(Mask), MaskArr); | |||
1945 | setShuffleMask(MaskArr); | |||
1946 | setName(Name); | |||
1947 | } | |||
1948 | ||||
1949 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
1950 | const Twine &Name, | |||
1951 | Instruction *InsertBefore) | |||
1952 | : Instruction( | |||
1953 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1954 | Mask.size(), isa<ScalableVectorType>(V1->getType())), | |||
1955 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1956 | OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { | |||
1957 | assert(isValidOperands(V1, V2, Mask) &&((void)0) | |||
1958 | "Invalid shuffle vector instruction operands!")((void)0); | |||
1959 | Op<0>() = V1; | |||
1960 | Op<1>() = V2; | |||
1961 | setShuffleMask(Mask); | |||
1962 | setName(Name); | |||
1963 | } | |||
1964 | ||||
1965 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
1966 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
1967 | : Instruction( | |||
1968 | VectorType::get(cast<VectorType>(V1->getType())->getElementType(), | |||
1969 | Mask.size(), isa<ScalableVectorType>(V1->getType())), | |||
1970 | ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), | |||
1971 | OperandTraits<ShuffleVectorInst>::operands(this), InsertAtEnd) { | |||
1972 | assert(isValidOperands(V1, V2, Mask) &&((void)0) | |||
1973 | "Invalid shuffle vector instruction operands!")((void)0); | |||
1974 | ||||
1975 | Op<0>() = V1; | |||
1976 | Op<1>() = V2; | |||
1977 | setShuffleMask(Mask); | |||
1978 | setName(Name); | |||
1979 | } | |||
1980 | ||||
1981 | void ShuffleVectorInst::commute() { | |||
1982 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
1983 | int NumMaskElts = ShuffleMask.size(); | |||
1984 | SmallVector<int, 16> NewMask(NumMaskElts); | |||
1985 | for (int i = 0; i != NumMaskElts; ++i) { | |||
1986 | int MaskElt = getMaskValue(i); | |||
1987 | if (MaskElt == UndefMaskElem) { | |||
1988 | NewMask[i] = UndefMaskElem; | |||
1989 | continue; | |||
1990 | } | |||
1991 | assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask")((void)0); | |||
1992 | MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; | |||
1993 | NewMask[i] = MaskElt; | |||
1994 | } | |||
1995 | setShuffleMask(NewMask); | |||
1996 | Op<0>().swap(Op<1>()); | |||
1997 | } | |||
1998 | ||||
1999 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, | |||
2000 | ArrayRef<int> Mask) { | |||
2001 | // V1 and V2 must be vectors of the same type. | |||
2002 | if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) | |||
2003 | return false; | |||
2004 | ||||
2005 | // Make sure the mask elements make sense. | |||
2006 | int V1Size = | |||
2007 | cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue(); | |||
2008 | for (int Elem : Mask) | |||
2009 | if (Elem != UndefMaskElem && Elem >= V1Size * 2) | |||
2010 | return false; | |||
2011 | ||||
2012 | if (isa<ScalableVectorType>(V1->getType())) | |||
2013 | if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !is_splat(Mask)) | |||
2014 | return false; | |||
2015 | ||||
2016 | return true; | |||
2017 | } | |||
2018 | ||||
2019 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, | |||
2020 | const Value *Mask) { | |||
2021 | // V1 and V2 must be vectors of the same type. | |||
2022 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) | |||
2023 | return false; | |||
2024 | ||||
2025 | // Mask must be vector of i32, and must be the same kind of vector as the | |||
2026 | // input vectors | |||
2027 | auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); | |||
2028 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) || | |||
2029 | isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType())) | |||
2030 | return false; | |||
2031 | ||||
2032 | // Check to see if Mask is valid. | |||
2033 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) | |||
2034 | return true; | |||
2035 | ||||
2036 | if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { | |||
2037 | unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); | |||
2038 | for (Value *Op : MV->operands()) { | |||
2039 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { | |||
2040 | if (CI->uge(V1Size*2)) | |||
2041 | return false; | |||
2042 | } else if (!isa<UndefValue>(Op)) { | |||
2043 | return false; | |||
2044 | } | |||
2045 | } | |||
2046 | return true; | |||
2047 | } | |||
2048 | ||||
2049 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { | |||
2050 | unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); | |||
2051 | for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements(); | |||
2052 | i != e; ++i) | |||
2053 | if (CDS->getElementAsInteger(i) >= V1Size*2) | |||
2054 | return false; | |||
2055 | return true; | |||
2056 | } | |||
2057 | ||||
2058 | return false; | |||
2059 | } | |||
2060 | ||||
2061 | void ShuffleVectorInst::getShuffleMask(const Constant *Mask, | |||
2062 | SmallVectorImpl<int> &Result) { | |||
2063 | ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount(); | |||
2064 | ||||
2065 | if (isa<ConstantAggregateZero>(Mask)) { | |||
2066 | Result.resize(EC.getKnownMinValue(), 0); | |||
2067 | return; | |||
2068 | } | |||
2069 | ||||
2070 | Result.reserve(EC.getKnownMinValue()); | |||
2071 | ||||
2072 | if (EC.isScalable()) { | |||
2073 | assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) &&((void)0) | |||
2074 | "Scalable vector shuffle mask must be undef or zeroinitializer")((void)0); | |||
2075 | int MaskVal = isa<UndefValue>(Mask) ? -1 : 0; | |||
2076 | for (unsigned I = 0; I < EC.getKnownMinValue(); ++I) | |||
2077 | Result.emplace_back(MaskVal); | |||
2078 | return; | |||
2079 | } | |||
2080 | ||||
2081 | unsigned NumElts = EC.getKnownMinValue(); | |||
2082 | ||||
2083 | if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { | |||
2084 | for (unsigned i = 0; i != NumElts; ++i) | |||
2085 | Result.push_back(CDS->getElementAsInteger(i)); | |||
2086 | return; | |||
2087 | } | |||
2088 | for (unsigned i = 0; i != NumElts; ++i) { | |||
2089 | Constant *C = Mask->getAggregateElement(i); | |||
2090 | Result.push_back(isa<UndefValue>(C) ? -1 : | |||
2091 | cast<ConstantInt>(C)->getZExtValue()); | |||
2092 | } | |||
2093 | } | |||
2094 | ||||
2095 | void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) { | |||
2096 | ShuffleMask.assign(Mask.begin(), Mask.end()); | |||
2097 | ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType()); | |||
2098 | } | |||
2099 | ||||
2100 | Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask, | |||
2101 | Type *ResultTy) { | |||
2102 | Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext()); | |||
2103 | if (isa<ScalableVectorType>(ResultTy)) { | |||
2104 | assert(is_splat(Mask) && "Unexpected shuffle")((void)0); | |||
2105 | Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true); | |||
2106 | if (Mask[0] == 0) | |||
2107 | return Constant::getNullValue(VecTy); | |||
2108 | return UndefValue::get(VecTy); | |||
2109 | } | |||
2110 | SmallVector<Constant *, 16> MaskConst; | |||
2111 | for (int Elem : Mask) { | |||
2112 | if (Elem == UndefMaskElem) | |||
2113 | MaskConst.push_back(UndefValue::get(Int32Ty)); | |||
2114 | else | |||
2115 | MaskConst.push_back(ConstantInt::get(Int32Ty, Elem)); | |||
2116 | } | |||
2117 | return ConstantVector::get(MaskConst); | |||
2118 | } | |||
2119 | ||||
2120 | static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { | |||
2121 | assert(!Mask.empty() && "Shuffle mask must contain elements")((void)0); | |||
2122 | bool UsesLHS = false; | |||
2123 | bool UsesRHS = false; | |||
2124 | for (int I : Mask) { | |||
2125 | if (I == -1) | |||
2126 | continue; | |||
2127 | assert(I >= 0 && I < (NumOpElts * 2) &&((void)0) | |||
2128 | "Out-of-bounds shuffle mask element")((void)0); | |||
2129 | UsesLHS |= (I < NumOpElts); | |||
2130 | UsesRHS |= (I >= NumOpElts); | |||
2131 | if (UsesLHS && UsesRHS) | |||
2132 | return false; | |||
2133 | } | |||
2134 | // Allow for degenerate case: completely undef mask means neither source is used. | |||
2135 | return UsesLHS || UsesRHS; | |||
2136 | } | |||
2137 | ||||
2138 | bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) { | |||
2139 | // We don't have vector operand size information, so assume operands are the | |||
2140 | // same size as the mask. | |||
2141 | return isSingleSourceMaskImpl(Mask, Mask.size()); | |||
2142 | } | |||
2143 | ||||
2144 | static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { | |||
2145 | if (!isSingleSourceMaskImpl(Mask, NumOpElts)) | |||
2146 | return false; | |||
2147 | for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { | |||
2148 | if (Mask[i] == -1) | |||
2149 | continue; | |||
2150 | if (Mask[i] != i && Mask[i] != (NumOpElts + i)) | |||
2151 | return false; | |||
2152 | } | |||
2153 | return true; | |||
2154 | } | |||
2155 | ||||
2156 | bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) { | |||
2157 | // We don't have vector operand size information, so assume operands are the | |||
2158 | // same size as the mask. | |||
2159 | return isIdentityMaskImpl(Mask, Mask.size()); | |||
2160 | } | |||
2161 | ||||
2162 | bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) { | |||
2163 | if (!isSingleSourceMask(Mask)) | |||
2164 | return false; | |||
2165 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2166 | if (Mask[i] == -1) | |||
2167 | continue; | |||
2168 | if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i)) | |||
2169 | return false; | |||
2170 | } | |||
2171 | return true; | |||
2172 | } | |||
2173 | ||||
2174 | bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) { | |||
2175 | if (!isSingleSourceMask(Mask)) | |||
2176 | return false; | |||
2177 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2178 | if (Mask[i] == -1) | |||
2179 | continue; | |||
2180 | if (Mask[i] != 0 && Mask[i] != NumElts) | |||
2181 | return false; | |||
2182 | } | |||
2183 | return true; | |||
2184 | } | |||
2185 | ||||
2186 | bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) { | |||
2187 | // Select is differentiated from identity. It requires using both sources. | |||
2188 | if (isSingleSourceMask(Mask)) | |||
2189 | return false; | |||
2190 | for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) { | |||
2191 | if (Mask[i] == -1) | |||
2192 | continue; | |||
2193 | if (Mask[i] != i && Mask[i] != (NumElts + i)) | |||
2194 | return false; | |||
2195 | } | |||
2196 | return true; | |||
2197 | } | |||
2198 | ||||
2199 | bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) { | |||
2200 | // Example masks that will return true: | |||
2201 | // v1 = <a, b, c, d> | |||
2202 | // v2 = <e, f, g, h> | |||
2203 | // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> | |||
2204 | // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> | |||
2205 | ||||
2206 | // 1. The number of elements in the mask must be a power-of-2 and at least 2. | |||
2207 | int NumElts = Mask.size(); | |||
2208 | if (NumElts < 2 || !isPowerOf2_32(NumElts)) | |||
2209 | return false; | |||
2210 | ||||
2211 | // 2. The first element of the mask must be either a 0 or a 1. | |||
2212 | if (Mask[0] != 0 && Mask[0] != 1) | |||
2213 | return false; | |||
2214 | ||||
2215 | // 3. The difference between the first 2 elements must be equal to the | |||
2216 | // number of elements in the mask. | |||
2217 | if ((Mask[1] - Mask[0]) != NumElts) | |||
2218 | return false; | |||
2219 | ||||
2220 | // 4. The difference between consecutive even-numbered and odd-numbered | |||
2221 | // elements must be equal to 2. | |||
2222 | for (int i = 2; i < NumElts; ++i) { | |||
2223 | int MaskEltVal = Mask[i]; | |||
2224 | if (MaskEltVal == -1) | |||
2225 | return false; | |||
2226 | int MaskEltPrevVal = Mask[i - 2]; | |||
2227 | if (MaskEltVal - MaskEltPrevVal != 2) | |||
2228 | return false; | |||
2229 | } | |||
2230 | return true; | |||
2231 | } | |||
2232 | ||||
2233 | bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, | |||
2234 | int NumSrcElts, int &Index) { | |||
2235 | // Must extract from a single source. | |||
2236 | if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) | |||
2237 | return false; | |||
2238 | ||||
2239 | // Must be smaller (else this is an Identity shuffle). | |||
2240 | if (NumSrcElts <= (int)Mask.size()) | |||
2241 | return false; | |||
2242 | ||||
2243 | // Find start of extraction, accounting that we may start with an UNDEF. | |||
2244 | int SubIndex = -1; | |||
2245 | for (int i = 0, e = Mask.size(); i != e; ++i) { | |||
2246 | int M = Mask[i]; | |||
2247 | if (M < 0) | |||
2248 | continue; | |||
2249 | int Offset = (M % NumSrcElts) - i; | |||
2250 | if (0 <= SubIndex && SubIndex != Offset) | |||
2251 | return false; | |||
2252 | SubIndex = Offset; | |||
2253 | } | |||
2254 | ||||
2255 | if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) { | |||
2256 | Index = SubIndex; | |||
2257 | return true; | |||
2258 | } | |||
2259 | return false; | |||
2260 | } | |||
2261 | ||||
2262 | bool ShuffleVectorInst::isIdentityWithPadding() const { | |||
2263 | if (isa<UndefValue>(Op<2>())) | |||
2264 | return false; | |||
2265 | ||||
2266 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2267 | // vector for this case. | |||
2268 | if (isa<ScalableVectorType>(getType())) | |||
2269 | return false; | |||
2270 | ||||
2271 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2272 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2273 | if (NumMaskElts <= NumOpElts) | |||
2274 | return false; | |||
2275 | ||||
2276 | // The first part of the mask must choose elements from exactly 1 source op. | |||
2277 | ArrayRef<int> Mask = getShuffleMask(); | |||
2278 | if (!isIdentityMaskImpl(Mask, NumOpElts)) | |||
2279 | return false; | |||
2280 | ||||
2281 | // All extending must be with undef elements. | |||
2282 | for (int i = NumOpElts; i < NumMaskElts; ++i) | |||
2283 | if (Mask[i] != -1) | |||
2284 | return false; | |||
2285 | ||||
2286 | return true; | |||
2287 | } | |||
2288 | ||||
2289 | bool ShuffleVectorInst::isIdentityWithExtract() const { | |||
2290 | if (isa<UndefValue>(Op<2>())) | |||
2291 | return false; | |||
2292 | ||||
2293 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2294 | // vector for this case. | |||
2295 | if (isa<ScalableVectorType>(getType())) | |||
2296 | return false; | |||
2297 | ||||
2298 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2299 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2300 | if (NumMaskElts >= NumOpElts) | |||
2301 | return false; | |||
2302 | ||||
2303 | return isIdentityMaskImpl(getShuffleMask(), NumOpElts); | |||
2304 | } | |||
2305 | ||||
2306 | bool ShuffleVectorInst::isConcat() const { | |||
2307 | // Vector concatenation is differentiated from identity with padding. | |||
2308 | if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>()) || | |||
2309 | isa<UndefValue>(Op<2>())) | |||
2310 | return false; | |||
2311 | ||||
2312 | // FIXME: Not currently possible to express a shuffle mask for a scalable | |||
2313 | // vector for this case. | |||
2314 | if (isa<ScalableVectorType>(getType())) | |||
2315 | return false; | |||
2316 | ||||
2317 | int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2318 | int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); | |||
2319 | if (NumMaskElts != NumOpElts * 2) | |||
2320 | return false; | |||
2321 | ||||
2322 | // Use the mask length rather than the operands' vector lengths here. We | |||
2323 | // already know that the shuffle returns a vector twice as long as the inputs, | |||
2324 | // and neither of the inputs are undef vectors. If the mask picks consecutive | |||
2325 | // elements from both inputs, then this is a concatenation of the inputs. | |||
2326 | return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); | |||
2327 | } | |||
2328 | ||||
2329 | //===----------------------------------------------------------------------===// | |||
2330 | // InsertValueInst Class | |||
2331 | //===----------------------------------------------------------------------===// | |||
2332 | ||||
2333 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, | |||
2334 | const Twine &Name) { | |||
2335 | assert(getNumOperands() == 2 && "NumOperands not initialized?")((void)0); | |||
2336 | ||||
2337 | // There's no fundamental reason why we require at least one index | |||
2338 | // (other than weirdness with &*IdxBegin being invalid; see | |||
2339 | // getelementptr's init routine for example). But there's no | |||
2340 | // present need to support it. | |||
2341 | assert(!Idxs.empty() && "InsertValueInst must have at least one index")((void)0); | |||
2342 | ||||
2343 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==((void)0) | |||
2344 | Val->getType() && "Inserted value must match indexed type!")((void)0); | |||
2345 | Op<0>() = Agg; | |||
2346 | Op<1>() = Val; | |||
2347 | ||||
2348 | Indices.append(Idxs.begin(), Idxs.end()); | |||
2349 | setName(Name); | |||
2350 | } | |||
2351 | ||||
2352 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) | |||
2353 | : Instruction(IVI.getType(), InsertValue, | |||
2354 | OperandTraits<InsertValueInst>::op_begin(this), 2), | |||
2355 | Indices(IVI.Indices) { | |||
2356 | Op<0>() = IVI.getOperand(0); | |||
2357 | Op<1>() = IVI.getOperand(1); | |||
2358 | SubclassOptionalData = IVI.SubclassOptionalData; | |||
2359 | } | |||
2360 | ||||
2361 | //===----------------------------------------------------------------------===// | |||
2362 | // ExtractValueInst Class | |||
2363 | //===----------------------------------------------------------------------===// | |||
2364 | ||||
2365 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { | |||
2366 | assert(getNumOperands() == 1 && "NumOperands not initialized?")((void)0); | |||
2367 | ||||
2368 | // There's no fundamental reason why we require at least one index. | |||
2369 | // But there's no present need to support it. | |||
2370 | assert(!Idxs.empty() && "ExtractValueInst must have at least one index")((void)0); | |||
2371 | ||||
2372 | Indices.append(Idxs.begin(), Idxs.end()); | |||
2373 | setName(Name); | |||
2374 | } | |||
2375 | ||||
2376 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) | |||
2377 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), | |||
2378 | Indices(EVI.Indices) { | |||
2379 | SubclassOptionalData = EVI.SubclassOptionalData; | |||
2380 | } | |||
2381 | ||||
2382 | // getIndexedType - Returns the type of the element that would be extracted | |||
2383 | // with an extractvalue instruction with the specified parameters. | |||
2384 | // | |||
2385 | // A null type is returned if the indices are invalid for the specified | |||
2386 | // pointer type. | |||
2387 | // | |||
2388 | Type *ExtractValueInst::getIndexedType(Type *Agg, | |||
2389 | ArrayRef<unsigned> Idxs) { | |||
2390 | for (unsigned Index : Idxs) { | |||
2391 | // We can't use CompositeType::indexValid(Index) here. | |||
2392 | // indexValid() always returns true for arrays because getelementptr allows | |||
2393 | // out-of-bounds indices. Since we don't allow those for extractvalue and | |||
2394 | // insertvalue we need to check array indexing manually. | |||
2395 | // Since the only other types we can index into are struct types it's just | |||
2396 | // as easy to check those manually as well. | |||
2397 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { | |||
2398 | if (Index >= AT->getNumElements()) | |||
2399 | return nullptr; | |||
2400 | Agg = AT->getElementType(); | |||
2401 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { | |||
2402 | if (Index >= ST->getNumElements()) | |||
2403 | return nullptr; | |||
2404 | Agg = ST->getElementType(Index); | |||
2405 | } else { | |||
2406 | // Not a valid type to index into. | |||
2407 | return nullptr; | |||
2408 | } | |||
2409 | } | |||
2410 | return const_cast<Type*>(Agg); | |||
2411 | } | |||
2412 | ||||
2413 | //===----------------------------------------------------------------------===// | |||
2414 | // UnaryOperator Class | |||
2415 | //===----------------------------------------------------------------------===// | |||
2416 | ||||
2417 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, | |||
2418 | Type *Ty, const Twine &Name, | |||
2419 | Instruction *InsertBefore) | |||
2420 | : UnaryInstruction(Ty, iType, S, InsertBefore) { | |||
2421 | Op<0>() = S; | |||
2422 | setName(Name); | |||
2423 | AssertOK(); | |||
2424 | } | |||
2425 | ||||
2426 | UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, | |||
2427 | Type *Ty, const Twine &Name, | |||
2428 | BasicBlock *InsertAtEnd) | |||
2429 | : UnaryInstruction(Ty, iType, S, InsertAtEnd) { | |||
2430 | Op<0>() = S; | |||
2431 | setName(Name); | |||
2432 | AssertOK(); | |||
2433 | } | |||
2434 | ||||
2435 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, | |||
2436 | const Twine &Name, | |||
2437 | Instruction *InsertBefore) { | |||
2438 | return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); | |||
2439 | } | |||
2440 | ||||
2441 | UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, | |||
2442 | const Twine &Name, | |||
2443 | BasicBlock *InsertAtEnd) { | |||
2444 | UnaryOperator *Res = Create(Op, S, Name); | |||
2445 | InsertAtEnd->getInstList().push_back(Res); | |||
2446 | return Res; | |||
2447 | } | |||
2448 | ||||
2449 | void UnaryOperator::AssertOK() { | |||
2450 | Value *LHS = getOperand(0); | |||
2451 | (void)LHS; // Silence warnings. | |||
2452 | #ifndef NDEBUG1 | |||
2453 | switch (getOpcode()) { | |||
2454 | case FNeg: | |||
2455 | assert(getType() == LHS->getType() &&((void)0) | |||
2456 | "Unary operation should return same type as operand!")((void)0); | |||
2457 | assert(getType()->isFPOrFPVectorTy() &&((void)0) | |||
2458 | "Tried to create a floating-point operation on a "((void)0) | |||
2459 | "non-floating-point type!")((void)0); | |||
2460 | break; | |||
2461 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
2462 | } | |||
2463 | #endif | |||
2464 | } | |||
2465 | ||||
2466 | //===----------------------------------------------------------------------===// | |||
2467 | // BinaryOperator Class | |||
2468 | //===----------------------------------------------------------------------===// | |||
2469 | ||||
2470 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, | |||
2471 | Type *Ty, const Twine &Name, | |||
2472 | Instruction *InsertBefore) | |||
2473 | : Instruction(Ty, iType, | |||
2474 | OperandTraits<BinaryOperator>::op_begin(this), | |||
2475 | OperandTraits<BinaryOperator>::operands(this), | |||
2476 | InsertBefore) { | |||
2477 | Op<0>() = S1; | |||
2478 | Op<1>() = S2; | |||
2479 | setName(Name); | |||
2480 | AssertOK(); | |||
2481 | } | |||
2482 | ||||
2483 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, | |||
2484 | Type *Ty, const Twine &Name, | |||
2485 | BasicBlock *InsertAtEnd) | |||
2486 | : Instruction(Ty, iType, | |||
2487 | OperandTraits<BinaryOperator>::op_begin(this), | |||
2488 | OperandTraits<BinaryOperator>::operands(this), | |||
2489 | InsertAtEnd) { | |||
2490 | Op<0>() = S1; | |||
2491 | Op<1>() = S2; | |||
2492 | setName(Name); | |||
2493 | AssertOK(); | |||
2494 | } | |||
2495 | ||||
2496 | void BinaryOperator::AssertOK() { | |||
2497 | Value *LHS = getOperand(0), *RHS = getOperand(1); | |||
2498 | (void)LHS; (void)RHS; // Silence warnings. | |||
2499 | assert(LHS->getType() == RHS->getType() &&((void)0) | |||
2500 | "Binary operator operand types must match!")((void)0); | |||
2501 | #ifndef NDEBUG1 | |||
2502 | switch (getOpcode()) { | |||
2503 | case Add: case Sub: | |||
2504 | case Mul: | |||
2505 | assert(getType() == LHS->getType() &&((void)0) | |||
2506 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2507 | assert(getType()->isIntOrIntVectorTy() &&((void)0) | |||
2508 | "Tried to create an integer operation on a non-integer type!")((void)0); | |||
2509 | break; | |||
2510 | case FAdd: case FSub: | |||
2511 | case FMul: | |||
2512 | assert(getType() == LHS->getType() &&((void)0) | |||
2513 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2514 | assert(getType()->isFPOrFPVectorTy() &&((void)0) | |||
2515 | "Tried to create a floating-point operation on a "((void)0) | |||
2516 | "non-floating-point type!")((void)0); | |||
2517 | break; | |||
2518 | case UDiv: | |||
2519 | case SDiv: | |||
2520 | assert(getType() == LHS->getType() &&((void)0) | |||
2521 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2522 | assert(getType()->isIntOrIntVectorTy() &&((void)0) | |||
2523 | "Incorrect operand type (not integer) for S/UDIV")((void)0); | |||
2524 | break; | |||
2525 | case FDiv: | |||
2526 | assert(getType() == LHS->getType() &&((void)0) | |||
2527 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2528 | assert(getType()->isFPOrFPVectorTy() &&((void)0) | |||
2529 | "Incorrect operand type (not floating point) for FDIV")((void)0); | |||
2530 | break; | |||
2531 | case URem: | |||
2532 | case SRem: | |||
2533 | assert(getType() == LHS->getType() &&((void)0) | |||
2534 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2535 | assert(getType()->isIntOrIntVectorTy() &&((void)0) | |||
2536 | "Incorrect operand type (not integer) for S/UREM")((void)0); | |||
2537 | break; | |||
2538 | case FRem: | |||
2539 | assert(getType() == LHS->getType() &&((void)0) | |||
2540 | "Arithmetic operation should return same type as operands!")((void)0); | |||
2541 | assert(getType()->isFPOrFPVectorTy() &&((void)0) | |||
2542 | "Incorrect operand type (not floating point) for FREM")((void)0); | |||
2543 | break; | |||
2544 | case Shl: | |||
2545 | case LShr: | |||
2546 | case AShr: | |||
2547 | assert(getType() == LHS->getType() &&((void)0) | |||
2548 | "Shift operation should return same type as operands!")((void)0); | |||
2549 | assert(getType()->isIntOrIntVectorTy() &&((void)0) | |||
2550 | "Tried to create a shift operation on a non-integral type!")((void)0); | |||
2551 | break; | |||
2552 | case And: case Or: | |||
2553 | case Xor: | |||
2554 | assert(getType() == LHS->getType() &&((void)0) | |||
2555 | "Logical operation should return same type as operands!")((void)0); | |||
2556 | assert(getType()->isIntOrIntVectorTy() &&((void)0) | |||
2557 | "Tried to create a logical operation on a non-integral type!")((void)0); | |||
2558 | break; | |||
2559 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
2560 | } | |||
2561 | #endif | |||
2562 | } | |||
2563 | ||||
2564 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, | |||
2565 | const Twine &Name, | |||
2566 | Instruction *InsertBefore) { | |||
2567 | assert(S1->getType() == S2->getType() &&((void)0) | |||
2568 | "Cannot create binary operator with two operands of differing type!")((void)0); | |||
2569 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); | |||
2570 | } | |||
2571 | ||||
2572 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, | |||
2573 | const Twine &Name, | |||
2574 | BasicBlock *InsertAtEnd) { | |||
2575 | BinaryOperator *Res = Create(Op, S1, S2, Name); | |||
2576 | InsertAtEnd->getInstList().push_back(Res); | |||
2577 | return Res; | |||
2578 | } | |||
2579 | ||||
2580 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, | |||
2581 | Instruction *InsertBefore) { | |||
2582 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2583 | return new BinaryOperator(Instruction::Sub, | |||
2584 | zero, Op, | |||
2585 | Op->getType(), Name, InsertBefore); | |||
2586 | } | |||
2587 | ||||
2588 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, | |||
2589 | BasicBlock *InsertAtEnd) { | |||
2590 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2591 | return new BinaryOperator(Instruction::Sub, | |||
2592 | zero, Op, | |||
2593 | Op->getType(), Name, InsertAtEnd); | |||
2594 | } | |||
2595 | ||||
2596 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, | |||
2597 | Instruction *InsertBefore) { | |||
2598 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2599 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); | |||
2600 | } | |||
2601 | ||||
2602 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, | |||
2603 | BasicBlock *InsertAtEnd) { | |||
2604 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2605 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); | |||
2606 | } | |||
2607 | ||||
2608 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, | |||
2609 | Instruction *InsertBefore) { | |||
2610 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2611 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); | |||
2612 | } | |||
2613 | ||||
2614 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, | |||
2615 | BasicBlock *InsertAtEnd) { | |||
2616 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); | |||
2617 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); | |||
2618 | } | |||
2619 | ||||
2620 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, | |||
2621 | Instruction *InsertBefore) { | |||
2622 | Constant *C = Constant::getAllOnesValue(Op->getType()); | |||
2623 | return new BinaryOperator(Instruction::Xor, Op, C, | |||
2624 | Op->getType(), Name, InsertBefore); | |||
2625 | } | |||
2626 | ||||
2627 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, | |||
2628 | BasicBlock *InsertAtEnd) { | |||
2629 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); | |||
2630 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, | |||
2631 | Op->getType(), Name, InsertAtEnd); | |||
2632 | } | |||
2633 | ||||
2634 | // Exchange the two operands to this instruction. This instruction is safe to | |||
2635 | // use on any binary instruction and does not modify the semantics of the | |||
2636 | // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode | |||
2637 | // is changed. | |||
2638 | bool BinaryOperator::swapOperands() { | |||
2639 | if (!isCommutative()) | |||
2640 | return true; // Can't commute operands | |||
2641 | Op<0>().swap(Op<1>()); | |||
2642 | return false; | |||
2643 | } | |||
2644 | ||||
2645 | //===----------------------------------------------------------------------===// | |||
2646 | // FPMathOperator Class | |||
2647 | //===----------------------------------------------------------------------===// | |||
2648 | ||||
2649 | float FPMathOperator::getFPAccuracy() const { | |||
2650 | const MDNode *MD = | |||
2651 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); | |||
2652 | if (!MD) | |||
2653 | return 0.0; | |||
2654 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); | |||
2655 | return Accuracy->getValueAPF().convertToFloat(); | |||
2656 | } | |||
2657 | ||||
2658 | //===----------------------------------------------------------------------===// | |||
2659 | // CastInst Class | |||
2660 | //===----------------------------------------------------------------------===// | |||
2661 | ||||
2662 | // Just determine if this cast only deals with integral->integral conversion. | |||
2663 | bool CastInst::isIntegerCast() const { | |||
2664 | switch (getOpcode()) { | |||
2665 | default: return false; | |||
2666 | case Instruction::ZExt: | |||
2667 | case Instruction::SExt: | |||
2668 | case Instruction::Trunc: | |||
2669 | return true; | |||
2670 | case Instruction::BitCast: | |||
2671 | return getOperand(0)->getType()->isIntegerTy() && | |||
2672 | getType()->isIntegerTy(); | |||
2673 | } | |||
2674 | } | |||
2675 | ||||
2676 | bool CastInst::isLosslessCast() const { | |||
2677 | // Only BitCast can be lossless, exit fast if we're not BitCast | |||
2678 | if (getOpcode() != Instruction::BitCast) | |||
2679 | return false; | |||
2680 | ||||
2681 | // Identity cast is always lossless | |||
2682 | Type *SrcTy = getOperand(0)->getType(); | |||
2683 | Type *DstTy = getType(); | |||
2684 | if (SrcTy == DstTy) | |||
2685 | return true; | |||
2686 | ||||
2687 | // Pointer to pointer is always lossless. | |||
2688 | if (SrcTy->isPointerTy()) | |||
2689 | return DstTy->isPointerTy(); | |||
2690 | return false; // Other types have no identity values | |||
2691 | } | |||
2692 | ||||
2693 | /// This function determines if the CastInst does not require any bits to be | |||
2694 | /// changed in order to effect the cast. Essentially, it identifies cases where | |||
2695 | /// no code gen is necessary for the cast, hence the name no-op cast. For | |||
2696 | /// example, the following are all no-op casts: | |||
2697 | /// # bitcast i32* %x to i8* | |||
2698 | /// # bitcast <2 x i32> %x to <4 x i16> | |||
2699 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only | |||
2700 | /// Determine if the described cast is a no-op. | |||
2701 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, | |||
2702 | Type *SrcTy, | |||
2703 | Type *DestTy, | |||
2704 | const DataLayout &DL) { | |||
2705 | assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition")((void)0); | |||
2706 | switch (Opcode) { | |||
2707 | default: llvm_unreachable("Invalid CastOp")__builtin_unreachable(); | |||
2708 | case Instruction::Trunc: | |||
2709 | case Instruction::ZExt: | |||
2710 | case Instruction::SExt: | |||
2711 | case Instruction::FPTrunc: | |||
2712 | case Instruction::FPExt: | |||
2713 | case Instruction::UIToFP: | |||
2714 | case Instruction::SIToFP: | |||
2715 | case Instruction::FPToUI: | |||
2716 | case Instruction::FPToSI: | |||
2717 | case Instruction::AddrSpaceCast: | |||
2718 | // TODO: Target informations may give a more accurate answer here. | |||
2719 | return false; | |||
2720 | case Instruction::BitCast: | |||
2721 | return true; // BitCast never modifies bits. | |||
2722 | case Instruction::PtrToInt: | |||
2723 | return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == | |||
2724 | DestTy->getScalarSizeInBits(); | |||
2725 | case Instruction::IntToPtr: | |||
2726 | return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == | |||
2727 | SrcTy->getScalarSizeInBits(); | |||
2728 | } | |||
2729 | } | |||
2730 | ||||
2731 | bool CastInst::isNoopCast(const DataLayout &DL) const { | |||
2732 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); | |||
2733 | } | |||
2734 | ||||
2735 | /// This function determines if a pair of casts can be eliminated and what | |||
2736 | /// opcode should be used in the elimination. This assumes that there are two | |||
2737 | /// instructions like this: | |||
2738 | /// * %F = firstOpcode SrcTy %x to MidTy | |||
2739 | /// * %S = secondOpcode MidTy %F to DstTy | |||
2740 | /// The function returns a resultOpcode so these two casts can be replaced with: | |||
2741 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy | |||
2742 | /// If no such cast is permitted, the function returns 0. | |||
2743 | unsigned CastInst::isEliminableCastPair( | |||
2744 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, | |||
2745 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, | |||
2746 | Type *DstIntPtrTy) { | |||
2747 | // Define the 144 possibilities for these two cast instructions. The values | |||
2748 | // in this matrix determine what to do in a given situation and select the | |||
2749 | // case in the switch below. The rows correspond to firstOp, the columns | |||
2750 | // correspond to secondOp. In looking at the table below, keep in mind | |||
2751 | // the following cast properties: | |||
2752 | // | |||
2753 | // Size Compare Source Destination | |||
2754 | // Operator Src ? Size Type Sign Type Sign | |||
2755 | // -------- ------------ ------------------- --------------------- | |||
2756 | // TRUNC > Integer Any Integral Any | |||
2757 | // ZEXT < Integral Unsigned Integer Any | |||
2758 | // SEXT < Integral Signed Integer Any | |||
2759 | // FPTOUI n/a FloatPt n/a Integral Unsigned | |||
2760 | // FPTOSI n/a FloatPt n/a Integral Signed | |||
2761 | // UITOFP n/a Integral Unsigned FloatPt n/a | |||
2762 | // SITOFP n/a Integral Signed FloatPt n/a | |||
2763 | // FPTRUNC > FloatPt n/a FloatPt n/a | |||
2764 | // FPEXT < FloatPt n/a FloatPt n/a | |||
2765 | // PTRTOINT n/a Pointer n/a Integral Unsigned | |||
2766 | // INTTOPTR n/a Integral Unsigned Pointer n/a | |||
2767 | // BITCAST = FirstClass n/a FirstClass n/a | |||
2768 | // ADDRSPCST n/a Pointer n/a Pointer n/a | |||
2769 | // | |||
2770 | // NOTE: some transforms are safe, but we consider them to be non-profitable. | |||
2771 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", | |||
2772 | // into "fptoui double to i64", but this loses information about the range | |||
2773 | // of the produced value (we no longer know the top-part is all zeros). | |||
2774 | // Further this conversion is often much more expensive for typical hardware, | |||
2775 | // and causes issues when building libgcc. We disallow fptosi+sext for the | |||
2776 | // same reason. | |||
2777 | const unsigned numCastOps = | |||
2778 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; | |||
2779 | static const uint8_t CastResults[numCastOps][numCastOps] = { | |||
2780 | // T F F U S F F P I B A -+ | |||
2781 | // R Z S P P I I T P 2 N T S | | |||
2782 | // U E E 2 2 2 2 R E I T C C +- secondOp | |||
2783 | // N X X U S F F N X N 2 V V | | |||
2784 | // C T T I I P P C T T P T T -+ | |||
2785 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ | |||
2786 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | | |||
2787 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | | |||
2788 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | | |||
2789 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | | |||
2790 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp | |||
2791 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | | |||
2792 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | | |||
2793 | { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | | |||
2794 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | | |||
2795 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | | |||
2796 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | | |||
2797 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ | |||
2798 | }; | |||
2799 | ||||
2800 | // TODO: This logic could be encoded into the table above and handled in the | |||
2801 | // switch below. | |||
2802 | // If either of the casts are a bitcast from scalar to vector, disallow the | |||
2803 | // merging. However, any pair of bitcasts are allowed. | |||
2804 | bool IsFirstBitcast = (firstOp == Instruction::BitCast); | |||
2805 | bool IsSecondBitcast = (secondOp == Instruction::BitCast); | |||
2806 | bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; | |||
2807 | ||||
2808 | // Check if any of the casts convert scalars <-> vectors. | |||
2809 | if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || | |||
2810 | (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) | |||
2811 | if (!AreBothBitcasts) | |||
2812 | return 0; | |||
2813 | ||||
2814 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] | |||
2815 | [secondOp-Instruction::CastOpsBegin]; | |||
2816 | switch (ElimCase) { | |||
2817 | case 0: | |||
2818 | // Categorically disallowed. | |||
2819 | return 0; | |||
2820 | case 1: | |||
2821 | // Allowed, use first cast's opcode. | |||
2822 | return firstOp; | |||
2823 | case 2: | |||
2824 | // Allowed, use second cast's opcode. | |||
2825 | return secondOp; | |||
2826 | case 3: | |||
2827 | // No-op cast in second op implies firstOp as long as the DestTy | |||
2828 | // is integer and we are not converting between a vector and a | |||
2829 | // non-vector type. | |||
2830 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) | |||
2831 | return firstOp; | |||
2832 | return 0; | |||
2833 | case 4: | |||
2834 | // No-op cast in second op implies firstOp as long as the DestTy | |||
2835 | // is floating point. | |||
2836 | if (DstTy->isFloatingPointTy()) | |||
2837 | return firstOp; | |||
2838 | return 0; | |||
2839 | case 5: | |||
2840 | // No-op cast in first op implies secondOp as long as the SrcTy | |||
2841 | // is an integer. | |||
2842 | if (SrcTy->isIntegerTy()) | |||
2843 | return secondOp; | |||
2844 | return 0; | |||
2845 | case 6: | |||
2846 | // No-op cast in first op implies secondOp as long as the SrcTy | |||
2847 | // is a floating point. | |||
2848 | if (SrcTy->isFloatingPointTy()) | |||
2849 | return secondOp; | |||
2850 | return 0; | |||
2851 | case 7: { | |||
2852 | // Disable inttoptr/ptrtoint optimization if enabled. | |||
2853 | if (DisableI2pP2iOpt) | |||
2854 | return 0; | |||
2855 | ||||
2856 | // Cannot simplify if address spaces are different! | |||
2857 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) | |||
2858 | return 0; | |||
2859 | ||||
2860 | unsigned MidSize = MidTy->getScalarSizeInBits(); | |||
2861 | // We can still fold this without knowing the actual sizes as long we | |||
2862 | // know that the intermediate pointer is the largest possible | |||
2863 | // pointer size. | |||
2864 | // FIXME: Is this always true? | |||
2865 | if (MidSize == 64) | |||
2866 | return Instruction::BitCast; | |||
2867 | ||||
2868 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. | |||
2869 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) | |||
2870 | return 0; | |||
2871 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); | |||
2872 | if (MidSize >= PtrSize) | |||
2873 | return Instruction::BitCast; | |||
2874 | return 0; | |||
2875 | } | |||
2876 | case 8: { | |||
2877 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size | |||
2878 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) | |||
2879 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) | |||
2880 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); | |||
2881 | unsigned DstSize = DstTy->getScalarSizeInBits(); | |||
2882 | if (SrcSize == DstSize) | |||
2883 | return Instruction::BitCast; | |||
2884 | else if (SrcSize < DstSize) | |||
2885 | return firstOp; | |||
2886 | return secondOp; | |||
2887 | } | |||
2888 | case 9: | |||
2889 | // zext, sext -> zext, because sext can't sign extend after zext | |||
2890 | return Instruction::ZExt; | |||
2891 | case 11: { | |||
2892 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize | |||
2893 | if (!MidIntPtrTy) | |||
2894 | return 0; | |||
2895 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); | |||
2896 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); | |||
2897 | unsigned DstSize = DstTy->getScalarSizeInBits(); | |||
2898 | if (SrcSize <= PtrSize && SrcSize == DstSize) | |||
2899 | return Instruction::BitCast; | |||
2900 | return 0; | |||
2901 | } | |||
2902 | case 12: | |||
2903 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS | |||
2904 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS | |||
2905 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) | |||
2906 | return Instruction::AddrSpaceCast; | |||
2907 | return Instruction::BitCast; | |||
2908 | case 13: | |||
2909 | // FIXME: this state can be merged with (1), but the following assert | |||
2910 | // is useful to check the correcteness of the sequence due to semantic | |||
2911 | // change of bitcast. | |||
2912 | assert(((void)0) | |||
2913 | SrcTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2914 | MidTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2915 | DstTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2916 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&((void)0) | |||
2917 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&((void)0) | |||
2918 | "Illegal addrspacecast, bitcast sequence!")((void)0); | |||
2919 | // Allowed, use first cast's opcode | |||
2920 | return firstOp; | |||
2921 | case 14: { | |||
2922 | // bitcast, addrspacecast -> addrspacecast if the element type of | |||
2923 | // bitcast's source is the same as that of addrspacecast's destination. | |||
2924 | PointerType *SrcPtrTy = cast<PointerType>(SrcTy->getScalarType()); | |||
2925 | PointerType *DstPtrTy = cast<PointerType>(DstTy->getScalarType()); | |||
2926 | if (SrcPtrTy->hasSameElementTypeAs(DstPtrTy)) | |||
2927 | return Instruction::AddrSpaceCast; | |||
2928 | return 0; | |||
2929 | } | |||
2930 | case 15: | |||
2931 | // FIXME: this state can be merged with (1), but the following assert | |||
2932 | // is useful to check the correcteness of the sequence due to semantic | |||
2933 | // change of bitcast. | |||
2934 | assert(((void)0) | |||
2935 | SrcTy->isIntOrIntVectorTy() &&((void)0) | |||
2936 | MidTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2937 | DstTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2938 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&((void)0) | |||
2939 | "Illegal inttoptr, bitcast sequence!")((void)0); | |||
2940 | // Allowed, use first cast's opcode | |||
2941 | return firstOp; | |||
2942 | case 16: | |||
2943 | // FIXME: this state can be merged with (2), but the following assert | |||
2944 | // is useful to check the correcteness of the sequence due to semantic | |||
2945 | // change of bitcast. | |||
2946 | assert(((void)0) | |||
2947 | SrcTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2948 | MidTy->isPtrOrPtrVectorTy() &&((void)0) | |||
2949 | DstTy->isIntOrIntVectorTy() &&((void)0) | |||
2950 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&((void)0) | |||
2951 | "Illegal bitcast, ptrtoint sequence!")((void)0); | |||
2952 | // Allowed, use second cast's opcode | |||
2953 | return secondOp; | |||
2954 | case 17: | |||
2955 | // (sitofp (zext x)) -> (uitofp x) | |||
2956 | return Instruction::UIToFP; | |||
2957 | case 99: | |||
2958 | // Cast combination can't happen (error in input). This is for all cases | |||
2959 | // where the MidTy is not the same for the two cast instructions. | |||
2960 | llvm_unreachable("Invalid Cast Combination")__builtin_unreachable(); | |||
2961 | default: | |||
2962 | llvm_unreachable("Error in CastResults table!!!")__builtin_unreachable(); | |||
2963 | } | |||
2964 | } | |||
2965 | ||||
2966 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, | |||
2967 | const Twine &Name, Instruction *InsertBefore) { | |||
2968 | assert(castIsValid(op, S, Ty) && "Invalid cast!")((void)0); | |||
2969 | // Construct and return the appropriate CastInst subclass | |||
2970 | switch (op) { | |||
2971 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); | |||
2972 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); | |||
2973 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); | |||
2974 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); | |||
2975 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); | |||
2976 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); | |||
2977 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); | |||
2978 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); | |||
2979 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); | |||
2980 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); | |||
2981 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); | |||
2982 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); | |||
2983 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); | |||
2984 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
2985 | } | |||
2986 | } | |||
2987 | ||||
2988 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, | |||
2989 | const Twine &Name, BasicBlock *InsertAtEnd) { | |||
2990 | assert(castIsValid(op, S, Ty) && "Invalid cast!")((void)0); | |||
2991 | // Construct and return the appropriate CastInst subclass | |||
2992 | switch (op) { | |||
2993 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); | |||
2994 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); | |||
2995 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); | |||
2996 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); | |||
2997 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); | |||
2998 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); | |||
2999 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); | |||
3000 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); | |||
3001 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); | |||
3002 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); | |||
3003 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); | |||
3004 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); | |||
3005 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); | |||
3006 | default: llvm_unreachable("Invalid opcode provided")__builtin_unreachable(); | |||
3007 | } | |||
3008 | } | |||
3009 | ||||
3010 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, | |||
3011 | const Twine &Name, | |||
3012 | Instruction *InsertBefore) { | |||
3013 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3014 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3015 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); | |||
3016 | } | |||
3017 | ||||
3018 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, | |||
3019 | const Twine &Name, | |||
3020 | BasicBlock *InsertAtEnd) { | |||
3021 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3022 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3023 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); | |||
3024 | } | |||
3025 | ||||
3026 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, | |||
3027 | const Twine &Name, | |||
3028 | Instruction *InsertBefore) { | |||
3029 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3030 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3031 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); | |||
3032 | } | |||
3033 | ||||
3034 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, | |||
3035 | const Twine &Name, | |||
3036 | BasicBlock *InsertAtEnd) { | |||
3037 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3038 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3039 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); | |||
3040 | } | |||
3041 | ||||
3042 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, | |||
3043 | const Twine &Name, | |||
3044 | Instruction *InsertBefore) { | |||
3045 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3046 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3047 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); | |||
3048 | } | |||
3049 | ||||
3050 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, | |||
3051 | const Twine &Name, | |||
3052 | BasicBlock *InsertAtEnd) { | |||
3053 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) | |||
3054 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3055 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); | |||
3056 | } | |||
3057 | ||||
3058 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, | |||
3059 | const Twine &Name, | |||
3060 | BasicBlock *InsertAtEnd) { | |||
3061 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3062 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&((void)0) | |||
3063 | "Invalid cast")((void)0); | |||
3064 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast")((void)0); | |||
3065 | assert((!Ty->isVectorTy() ||((void)0) | |||
3066 | cast<VectorType>(Ty)->getElementCount() ==((void)0) | |||
3067 | cast<VectorType>(S->getType())->getElementCount()) &&((void)0) | |||
3068 | "Invalid cast")((void)0); | |||
3069 | ||||
3070 | if (Ty->isIntOrIntVectorTy()) | |||
3071 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); | |||
3072 | ||||
3073 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); | |||
3074 | } | |||
3075 | ||||
3076 | /// Create a BitCast or a PtrToInt cast instruction | |||
3077 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, | |||
3078 | const Twine &Name, | |||
3079 | Instruction *InsertBefore) { | |||
3080 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3081 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&((void)0) | |||
3082 | "Invalid cast")((void)0); | |||
3083 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast")((void)0); | |||
3084 | assert((!Ty->isVectorTy() ||((void)0) | |||
3085 | cast<VectorType>(Ty)->getElementCount() ==((void)0) | |||
3086 | cast<VectorType>(S->getType())->getElementCount()) &&((void)0) | |||
3087 | "Invalid cast")((void)0); | |||
3088 | ||||
3089 | if (Ty->isIntOrIntVectorTy()) | |||
3090 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); | |||
3091 | ||||
3092 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); | |||
3093 | } | |||
3094 | ||||
3095 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( | |||
3096 | Value *S, Type *Ty, | |||
3097 | const Twine &Name, | |||
3098 | BasicBlock *InsertAtEnd) { | |||
3099 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3100 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3101 | ||||
3102 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) | |||
3103 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); | |||
3104 | ||||
3105 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); | |||
3106 | } | |||
3107 | ||||
3108 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( | |||
3109 | Value *S, Type *Ty, | |||
3110 | const Twine &Name, | |||
3111 | Instruction *InsertBefore) { | |||
3112 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3113 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast")((void)0); | |||
3114 | ||||
3115 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) | |||
3116 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); | |||
3117 | ||||
3118 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3119 | } | |||
3120 | ||||
3121 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, | |||
3122 | const Twine &Name, | |||
3123 | Instruction *InsertBefore) { | |||
3124 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) | |||
3125 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); | |||
3126 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) | |||
3127 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); | |||
3128 | ||||
3129 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); | |||
3130 | } | |||
3131 | ||||
3132 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, | |||
3133 | bool isSigned, const Twine &Name, | |||
3134 | Instruction *InsertBefore) { | |||
3135 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&((void)0) | |||
3136 | "Invalid integer cast")((void)0); | |||
3137 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3138 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3139 | Instruction::CastOps opcode = | |||
3140 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3141 | (SrcBits > DstBits ? Instruction::Trunc : | |||
3142 | (isSigned ? Instruction::SExt : Instruction::ZExt))); | |||
3143 | return Create(opcode, C, Ty, Name, InsertBefore); | |||
3144 | } | |||
3145 | ||||
3146 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, | |||
3147 | bool isSigned, const Twine &Name, | |||
3148 | BasicBlock *InsertAtEnd) { | |||
3149 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&((void)0) | |||
3150 | "Invalid cast")((void)0); | |||
3151 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3152 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3153 | Instruction::CastOps opcode = | |||
3154 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3155 | (SrcBits > DstBits ? Instruction::Trunc : | |||
3156 | (isSigned ? Instruction::SExt : Instruction::ZExt))); | |||
3157 | return Create(opcode, C, Ty, Name, InsertAtEnd); | |||
3158 | } | |||
3159 | ||||
3160 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, | |||
3161 | const Twine &Name, | |||
3162 | Instruction *InsertBefore) { | |||
3163 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&((void)0) | |||
3164 | "Invalid cast")((void)0); | |||
3165 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3166 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3167 | Instruction::CastOps opcode = | |||
3168 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3169 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); | |||
3170 | return Create(opcode, C, Ty, Name, InsertBefore); | |||
3171 | } | |||
3172 | ||||
3173 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, | |||
3174 | const Twine &Name, | |||
3175 | BasicBlock *InsertAtEnd) { | |||
3176 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&((void)0) | |||
3177 | "Invalid cast")((void)0); | |||
3178 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); | |||
3179 | unsigned DstBits = Ty->getScalarSizeInBits(); | |||
3180 | Instruction::CastOps opcode = | |||
3181 | (SrcBits == DstBits ? Instruction::BitCast : | |||
3182 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); | |||
3183 | return Create(opcode, C, Ty, Name, InsertAtEnd); | |||
3184 | } | |||
3185 | ||||
3186 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { | |||
3187 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) | |||
3188 | return false; | |||
3189 | ||||
3190 | if (SrcTy == DestTy) | |||
3191 | return true; | |||
3192 | ||||
3193 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { | |||
3194 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { | |||
3195 | if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { | |||
3196 | // An element by element cast. Valid if casting the elements is valid. | |||
3197 | SrcTy = SrcVecTy->getElementType(); | |||
3198 | DestTy = DestVecTy->getElementType(); | |||
3199 | } | |||
3200 | } | |||
3201 | } | |||
3202 | ||||
3203 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { | |||
3204 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { | |||
3205 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); | |||
3206 | } | |||
3207 | } | |||
3208 | ||||
3209 | TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3210 | TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3211 | ||||
3212 | // Could still have vectors of pointers if the number of elements doesn't | |||
3213 | // match | |||
3214 | if (SrcBits.getKnownMinSize() == 0 || DestBits.getKnownMinSize() == 0) | |||
3215 | return false; | |||
3216 | ||||
3217 | if (SrcBits != DestBits) | |||
3218 | return false; | |||
3219 | ||||
3220 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) | |||
3221 | return false; | |||
3222 | ||||
3223 | return true; | |||
3224 | } | |||
3225 | ||||
3226 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, | |||
3227 | const DataLayout &DL) { | |||
3228 | // ptrtoint and inttoptr are not allowed on non-integral pointers | |||
3229 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) | |||
3230 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) | |||
3231 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && | |||
3232 | !DL.isNonIntegralPointerType(PtrTy)); | |||
3233 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) | |||
3234 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) | |||
3235 | return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && | |||
3236 | !DL.isNonIntegralPointerType(PtrTy)); | |||
3237 | ||||
3238 | return isBitCastable(SrcTy, DestTy); | |||
3239 | } | |||
3240 | ||||
3241 | // Provide a way to get a "cast" where the cast opcode is inferred from the | |||
3242 | // types and size of the operand. This, basically, is a parallel of the | |||
3243 | // logic in the castIsValid function below. This axiom should hold: | |||
3244 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) | |||
3245 | // should not assert in castIsValid. In other words, this produces a "correct" | |||
3246 | // casting opcode for the arguments passed to it. | |||
3247 | Instruction::CastOps | |||
3248 | CastInst::getCastOpcode( | |||
3249 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { | |||
3250 | Type *SrcTy = Src->getType(); | |||
3251 | ||||
3252 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&((void)0) | |||
3253 | "Only first class types are castable!")((void)0); | |||
3254 | ||||
3255 | if (SrcTy == DestTy) | |||
3256 | return BitCast; | |||
3257 | ||||
3258 | // FIXME: Check address space sizes here | |||
3259 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) | |||
3260 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) | |||
3261 | if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { | |||
3262 | // An element by element cast. Find the appropriate opcode based on the | |||
3263 | // element types. | |||
3264 | SrcTy = SrcVecTy->getElementType(); | |||
3265 | DestTy = DestVecTy->getElementType(); | |||
3266 | } | |||
3267 | ||||
3268 | // Get the bit sizes, we'll need these | |||
3269 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3270 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr | |||
3271 | ||||
3272 | // Run through the possibilities ... | |||
3273 | if (DestTy->isIntegerTy()) { // Casting to integral | |||
3274 | if (SrcTy->isIntegerTy()) { // Casting from integral | |||
3275 | if (DestBits < SrcBits) | |||
3276 | return Trunc; // int -> smaller int | |||
3277 | else if (DestBits > SrcBits) { // its an extension | |||
3278 | if (SrcIsSigned) | |||
3279 | return SExt; // signed -> SEXT | |||
3280 | else | |||
3281 | return ZExt; // unsigned -> ZEXT | |||
3282 | } else { | |||
3283 | return BitCast; // Same size, No-op cast | |||
3284 | } | |||
3285 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt | |||
3286 | if (DestIsSigned) | |||
3287 | return FPToSI; // FP -> sint | |||
3288 | else | |||
3289 | return FPToUI; // FP -> uint | |||
3290 | } else if (SrcTy->isVectorTy()) { | |||
3291 | assert(DestBits == SrcBits &&((void)0) | |||
3292 | "Casting vector to integer of different width")((void)0); | |||
3293 | return BitCast; // Same size, no-op cast | |||
3294 | } else { | |||
3295 | assert(SrcTy->isPointerTy() &&((void)0) | |||
3296 | "Casting from a value that is not first-class type")((void)0); | |||
3297 | return PtrToInt; // ptr -> int | |||
3298 | } | |||
3299 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt | |||
3300 | if (SrcTy->isIntegerTy()) { // Casting from integral | |||
3301 | if (SrcIsSigned) | |||
3302 | return SIToFP; // sint -> FP | |||
3303 | else | |||
3304 | return UIToFP; // uint -> FP | |||
3305 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt | |||
3306 | if (DestBits < SrcBits) { | |||
3307 | return FPTrunc; // FP -> smaller FP | |||
3308 | } else if (DestBits > SrcBits) { | |||
3309 | return FPExt; // FP -> larger FP | |||
3310 | } else { | |||
3311 | return BitCast; // same size, no-op cast | |||
3312 | } | |||
3313 | } else if (SrcTy->isVectorTy()) { | |||
3314 | assert(DestBits == SrcBits &&((void)0) | |||
3315 | "Casting vector to floating point of different width")((void)0); | |||
3316 | return BitCast; // same size, no-op cast | |||
3317 | } | |||
3318 | llvm_unreachable("Casting pointer or non-first class to float")__builtin_unreachable(); | |||
3319 | } else if (DestTy->isVectorTy()) { | |||
3320 | assert(DestBits == SrcBits &&((void)0) | |||
3321 | "Illegal cast to vector (wrong type or size)")((void)0); | |||
3322 | return BitCast; | |||
3323 | } else if (DestTy->isPointerTy()) { | |||
3324 | if (SrcTy->isPointerTy()) { | |||
3325 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) | |||
3326 | return AddrSpaceCast; | |||
3327 | return BitCast; // ptr -> ptr | |||
3328 | } else if (SrcTy->isIntegerTy()) { | |||
3329 | return IntToPtr; // int -> ptr | |||
3330 | } | |||
3331 | llvm_unreachable("Casting pointer to other than pointer or int")__builtin_unreachable(); | |||
3332 | } else if (DestTy->isX86_MMXTy()) { | |||
3333 | if (SrcTy->isVectorTy()) { | |||
3334 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX")((void)0); | |||
3335 | return BitCast; // 64-bit vector to MMX | |||
3336 | } | |||
3337 | llvm_unreachable("Illegal cast to X86_MMX")__builtin_unreachable(); | |||
3338 | } | |||
3339 | llvm_unreachable("Casting to type that is not first-class")__builtin_unreachable(); | |||
3340 | } | |||
3341 | ||||
3342 | //===----------------------------------------------------------------------===// | |||
3343 | // CastInst SubClass Constructors | |||
3344 | //===----------------------------------------------------------------------===// | |||
3345 | ||||
3346 | /// Check that the construction parameters for a CastInst are correct. This | |||
3347 | /// could be broken out into the separate constructors but it is useful to have | |||
3348 | /// it in one place and to eliminate the redundant code for getting the sizes | |||
3349 | /// of the types involved. | |||
3350 | bool | |||
3351 | CastInst::castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy) { | |||
3352 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || | |||
3353 | SrcTy->isAggregateType() || DstTy->isAggregateType()) | |||
3354 | return false; | |||
3355 | ||||
3356 | // Get the size of the types in bits, and whether we are dealing | |||
3357 | // with vector types, we'll need this later. | |||
3358 | bool SrcIsVec = isa<VectorType>(SrcTy); | |||
3359 | bool DstIsVec = isa<VectorType>(DstTy); | |||
3360 | unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits(); | |||
3361 | unsigned DstScalarBitSize = DstTy->getScalarSizeInBits(); | |||
3362 | ||||
3363 | // If these are vector types, get the lengths of the vectors (using zero for | |||
3364 | // scalar types means that checking that vector lengths match also checks that | |||
3365 | // scalars are not being converted to vectors or vectors to scalars). | |||
3366 | ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount() | |||
3367 | : ElementCount::getFixed(0); | |||
3368 | ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount() | |||
3369 | : ElementCount::getFixed(0); | |||
3370 | ||||
3371 | // Switch on the opcode provided | |||
3372 | switch (op) { | |||
3373 | default: return false; // This is an input error | |||
3374 | case Instruction::Trunc: | |||
3375 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3376 | SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; | |||
3377 | case Instruction::ZExt: | |||
3378 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3379 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3380 | case Instruction::SExt: | |||
3381 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3382 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3383 | case Instruction::FPTrunc: | |||
3384 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3385 | SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; | |||
3386 | case Instruction::FPExt: | |||
3387 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3388 | SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; | |||
3389 | case Instruction::UIToFP: | |||
3390 | case Instruction::SIToFP: | |||
3391 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && | |||
3392 | SrcEC == DstEC; | |||
3393 | case Instruction::FPToUI: | |||
3394 | case Instruction::FPToSI: | |||
3395 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && | |||
3396 | SrcEC == DstEC; | |||
3397 | case Instruction::PtrToInt: | |||
3398 | if (SrcEC != DstEC) | |||
3399 | return false; | |||
3400 | return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); | |||
3401 | case Instruction::IntToPtr: | |||
3402 | if (SrcEC != DstEC) | |||
3403 | return false; | |||
3404 | return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); | |||
3405 | case Instruction::BitCast: { | |||
3406 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); | |||
3407 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); | |||
3408 | ||||
3409 | // BitCast implies a no-op cast of type only. No bits change. | |||
3410 | // However, you can't cast pointers to anything but pointers. | |||
3411 | if (!SrcPtrTy != !DstPtrTy) | |||
3412 | return false; | |||
3413 | ||||
3414 | // For non-pointer cases, the cast is okay if the source and destination bit | |||
3415 | // widths are identical. | |||
3416 | if (!SrcPtrTy) | |||
3417 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); | |||
3418 | ||||
3419 | // If both are pointers then the address spaces must match. | |||
3420 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) | |||
3421 | return false; | |||
3422 | ||||
3423 | // A vector of pointers must have the same number of elements. | |||
3424 | if (SrcIsVec && DstIsVec) | |||
3425 | return SrcEC == DstEC; | |||
3426 | if (SrcIsVec) | |||
3427 | return SrcEC == ElementCount::getFixed(1); | |||
3428 | if (DstIsVec) | |||
3429 | return DstEC == ElementCount::getFixed(1); | |||
3430 | ||||
3431 | return true; | |||
3432 | } | |||
3433 | case Instruction::AddrSpaceCast: { | |||
3434 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); | |||
3435 | if (!SrcPtrTy) | |||
3436 | return false; | |||
3437 | ||||
3438 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); | |||
3439 | if (!DstPtrTy) | |||
3440 | return false; | |||
3441 | ||||
3442 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) | |||
3443 | return false; | |||
3444 | ||||
3445 | return SrcEC == DstEC; | |||
3446 | } | |||
3447 | } | |||
3448 | } | |||
3449 | ||||
3450 | TruncInst::TruncInst( | |||
3451 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3452 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { | |||
3453 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc")((void)0); | |||
3454 | } | |||
3455 | ||||
3456 | TruncInst::TruncInst( | |||
3457 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3458 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { | |||
3459 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc")((void)0); | |||
3460 | } | |||
3461 | ||||
3462 | ZExtInst::ZExtInst( | |||
3463 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3464 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { | |||
3465 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt")((void)0); | |||
3466 | } | |||
3467 | ||||
3468 | ZExtInst::ZExtInst( | |||
3469 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3470 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { | |||
3471 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt")((void)0); | |||
3472 | } | |||
3473 | SExtInst::SExtInst( | |||
3474 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3475 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { | |||
3476 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt")((void)0); | |||
3477 | } | |||
3478 | ||||
3479 | SExtInst::SExtInst( | |||
3480 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3481 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { | |||
3482 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt")((void)0); | |||
3483 | } | |||
3484 | ||||
3485 | FPTruncInst::FPTruncInst( | |||
3486 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3487 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { | |||
3488 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc")((void)0); | |||
3489 | } | |||
3490 | ||||
3491 | FPTruncInst::FPTruncInst( | |||
3492 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3493 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { | |||
3494 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc")((void)0); | |||
3495 | } | |||
3496 | ||||
3497 | FPExtInst::FPExtInst( | |||
3498 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3499 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { | |||
3500 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt")((void)0); | |||
3501 | } | |||
3502 | ||||
3503 | FPExtInst::FPExtInst( | |||
3504 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3505 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { | |||
3506 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt")((void)0); | |||
3507 | } | |||
3508 | ||||
3509 | UIToFPInst::UIToFPInst( | |||
3510 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3511 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { | |||
3512 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP")((void)0); | |||
3513 | } | |||
3514 | ||||
3515 | UIToFPInst::UIToFPInst( | |||
3516 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3517 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { | |||
3518 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP")((void)0); | |||
3519 | } | |||
3520 | ||||
3521 | SIToFPInst::SIToFPInst( | |||
3522 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3523 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { | |||
3524 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP")((void)0); | |||
3525 | } | |||
3526 | ||||
3527 | SIToFPInst::SIToFPInst( | |||
3528 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3529 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { | |||
3530 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP")((void)0); | |||
3531 | } | |||
3532 | ||||
3533 | FPToUIInst::FPToUIInst( | |||
3534 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3535 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { | |||
3536 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI")((void)0); | |||
3537 | } | |||
3538 | ||||
3539 | FPToUIInst::FPToUIInst( | |||
3540 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3541 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { | |||
3542 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI")((void)0); | |||
3543 | } | |||
3544 | ||||
3545 | FPToSIInst::FPToSIInst( | |||
3546 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3547 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { | |||
3548 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI")((void)0); | |||
3549 | } | |||
3550 | ||||
3551 | FPToSIInst::FPToSIInst( | |||
3552 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3553 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { | |||
3554 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI")((void)0); | |||
3555 | } | |||
3556 | ||||
3557 | PtrToIntInst::PtrToIntInst( | |||
3558 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3559 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { | |||
3560 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt")((void)0); | |||
3561 | } | |||
3562 | ||||
3563 | PtrToIntInst::PtrToIntInst( | |||
3564 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3565 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { | |||
3566 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt")((void)0); | |||
3567 | } | |||
3568 | ||||
3569 | IntToPtrInst::IntToPtrInst( | |||
3570 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3571 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { | |||
3572 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr")((void)0); | |||
3573 | } | |||
3574 | ||||
3575 | IntToPtrInst::IntToPtrInst( | |||
3576 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3577 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { | |||
3578 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr")((void)0); | |||
3579 | } | |||
3580 | ||||
3581 | BitCastInst::BitCastInst( | |||
3582 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3583 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { | |||
3584 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast")((void)0); | |||
3585 | } | |||
3586 | ||||
3587 | BitCastInst::BitCastInst( | |||
3588 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3589 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { | |||
3590 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast")((void)0); | |||
3591 | } | |||
3592 | ||||
3593 | AddrSpaceCastInst::AddrSpaceCastInst( | |||
3594 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore | |||
3595 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { | |||
3596 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast")((void)0); | |||
3597 | } | |||
3598 | ||||
3599 | AddrSpaceCastInst::AddrSpaceCastInst( | |||
3600 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd | |||
3601 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { | |||
3602 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast")((void)0); | |||
3603 | } | |||
3604 | ||||
3605 | //===----------------------------------------------------------------------===// | |||
3606 | // CmpInst Classes | |||
3607 | //===----------------------------------------------------------------------===// | |||
3608 | ||||
3609 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, | |||
3610 | Value *RHS, const Twine &Name, Instruction *InsertBefore, | |||
3611 | Instruction *FlagsSource) | |||
3612 | : Instruction(ty, op, | |||
3613 | OperandTraits<CmpInst>::op_begin(this), | |||
3614 | OperandTraits<CmpInst>::operands(this), | |||
3615 | InsertBefore) { | |||
3616 | Op<0>() = LHS; | |||
3617 | Op<1>() = RHS; | |||
3618 | setPredicate((Predicate)predicate); | |||
3619 | setName(Name); | |||
3620 | if (FlagsSource) | |||
3621 | copyIRFlags(FlagsSource); | |||
3622 | } | |||
3623 | ||||
3624 | CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, | |||
3625 | Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd) | |||
3626 | : Instruction(ty, op, | |||
3627 | OperandTraits<CmpInst>::op_begin(this), | |||
3628 | OperandTraits<CmpInst>::operands(this), | |||
3629 | InsertAtEnd) { | |||
3630 | Op<0>() = LHS; | |||
3631 | Op<1>() = RHS; | |||
3632 | setPredicate((Predicate)predicate); | |||
3633 | setName(Name); | |||
3634 | } | |||
3635 | ||||
3636 | CmpInst * | |||
3637 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, | |||
3638 | const Twine &Name, Instruction *InsertBefore) { | |||
3639 | if (Op == Instruction::ICmp) { | |||
3640 | if (InsertBefore) | |||
3641 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), | |||
3642 | S1, S2, Name); | |||
3643 | else | |||
3644 | return new ICmpInst(CmpInst::Predicate(predicate), | |||
3645 | S1, S2, Name); | |||
3646 | } | |||
3647 | ||||
3648 | if (InsertBefore) | |||
3649 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), | |||
3650 | S1, S2, Name); | |||
3651 | else | |||
3652 | return new FCmpInst(CmpInst::Predicate(predicate), | |||
3653 | S1, S2, Name); | |||
3654 | } | |||
3655 | ||||
3656 | CmpInst * | |||
3657 | CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, | |||
3658 | const Twine &Name, BasicBlock *InsertAtEnd) { | |||
3659 | if (Op == Instruction::ICmp) { | |||
3660 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), | |||
3661 | S1, S2, Name); | |||
3662 | } | |||
3663 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), | |||
3664 | S1, S2, Name); | |||
3665 | } | |||
3666 | ||||
3667 | void CmpInst::swapOperands() { | |||
3668 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) | |||
3669 | IC->swapOperands(); | |||
3670 | else | |||
3671 | cast<FCmpInst>(this)->swapOperands(); | |||
3672 | } | |||
3673 | ||||
3674 | bool CmpInst::isCommutative() const { | |||
3675 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) | |||
3676 | return IC->isCommutative(); | |||
3677 | return cast<FCmpInst>(this)->isCommutative(); | |||
3678 | } | |||
3679 | ||||
3680 | bool CmpInst::isEquality(Predicate P) { | |||
3681 | if (ICmpInst::isIntPredicate(P)) | |||
3682 | return ICmpInst::isEquality(P); | |||
3683 | if (FCmpInst::isFPPredicate(P)) | |||
3684 | return FCmpInst::isEquality(P); | |||
3685 | llvm_unreachable("Unsupported predicate kind")__builtin_unreachable(); | |||
3686 | } | |||
3687 | ||||
3688 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { | |||
3689 | switch (pred) { | |||
3690 | default: llvm_unreachable("Unknown cmp predicate!")__builtin_unreachable(); | |||
3691 | case ICMP_EQ: return ICMP_NE; | |||
3692 | case ICMP_NE: return ICMP_EQ; | |||
3693 | case ICMP_UGT: return ICMP_ULE; | |||
3694 | case ICMP_ULT: return ICMP_UGE; | |||
3695 | case ICMP_UGE: return ICMP_ULT; | |||
3696 | case ICMP_ULE: return ICMP_UGT; | |||
3697 | case ICMP_SGT: return ICMP_SLE; | |||
3698 | case ICMP_SLT: return ICMP_SGE; | |||
3699 | case ICMP_SGE: return ICMP_SLT; | |||
3700 | case ICMP_SLE: return ICMP_SGT; | |||
3701 | ||||
3702 | case FCMP_OEQ: return FCMP_UNE; | |||
3703 | case FCMP_ONE: return FCMP_UEQ; | |||
3704 | case FCMP_OGT: return FCMP_ULE; | |||
3705 | case FCMP_OLT: return FCMP_UGE; | |||
3706 | case FCMP_OGE: return FCMP_ULT; | |||
3707 | case FCMP_OLE: return FCMP_UGT; | |||
3708 | case FCMP_UEQ: return FCMP_ONE; | |||
3709 | case FCMP_UNE: return FCMP_OEQ; | |||
3710 | case FCMP_UGT: return FCMP_OLE; | |||
3711 | case FCMP_ULT: return FCMP_OGE; | |||
3712 | case FCMP_UGE: return FCMP_OLT; | |||
3713 | case FCMP_ULE: return FCMP_OGT; | |||
3714 | case FCMP_ORD: return FCMP_UNO; | |||
3715 | case FCMP_UNO: return FCMP_ORD; | |||
3716 | case FCMP_TRUE: return FCMP_FALSE; | |||
3717 | case FCMP_FALSE: return FCMP_TRUE; | |||
3718 | } | |||
3719 | } | |||
3720 | ||||
3721 | StringRef CmpInst::getPredicateName(Predicate Pred) { | |||
3722 | switch (Pred) { | |||
3723 | default: return "unknown"; | |||
3724 | case FCmpInst::FCMP_FALSE: return "false"; | |||
3725 | case FCmpInst::FCMP_OEQ: return "oeq"; | |||
3726 | case FCmpInst::FCMP_OGT: return "ogt"; | |||
3727 | case FCmpInst::FCMP_OGE: return "oge"; | |||
3728 | case FCmpInst::FCMP_OLT: return "olt"; | |||
3729 | case FCmpInst::FCMP_OLE: return "ole"; | |||
3730 | case FCmpInst::FCMP_ONE: return "one"; | |||
3731 | case FCmpInst::FCMP_ORD: return "ord"; | |||
3732 | case FCmpInst::FCMP_UNO: return "uno"; | |||
3733 | case FCmpInst::FCMP_UEQ: return "ueq"; | |||
3734 | case FCmpInst::FCMP_UGT: return "ugt"; | |||
3735 | case FCmpInst::FCMP_UGE: return "uge"; | |||
3736 | case FCmpInst::FCMP_ULT: return "ult"; | |||
3737 | case FCmpInst::FCMP_ULE: return "ule"; | |||
3738 | case FCmpInst::FCMP_UNE: return "une"; | |||
3739 | case FCmpInst::FCMP_TRUE: return "true"; | |||
3740 | case ICmpInst::ICMP_EQ: return "eq"; | |||
3741 | case ICmpInst::ICMP_NE: return "ne"; | |||
3742 | case ICmpInst::ICMP_SGT: return "sgt"; | |||
3743 | case ICmpInst::ICMP_SGE: return "sge"; | |||
3744 | case ICmpInst::ICMP_SLT: return "slt"; | |||
3745 | case ICmpInst::ICMP_SLE: return "sle"; | |||
3746 | case ICmpInst::ICMP_UGT: return "ugt"; | |||
3747 | case ICmpInst::ICMP_UGE: return "uge"; | |||
3748 | case ICmpInst::ICMP_ULT: return "ult"; | |||
3749 | case ICmpInst::ICMP_ULE: return "ule"; | |||
3750 | } | |||
3751 | } | |||
3752 | ||||
3753 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { | |||
3754 | switch (pred) { | |||
3755 | default: llvm_unreachable("Unknown icmp predicate!")__builtin_unreachable(); | |||
3756 | case ICMP_EQ: case ICMP_NE: | |||
3757 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: | |||
3758 | return pred; | |||
3759 | case ICMP_UGT: return ICMP_SGT; | |||
3760 | case ICMP_ULT: return ICMP_SLT; | |||
3761 | case ICMP_UGE: return ICMP_SGE; | |||
3762 | case ICMP_ULE: return ICMP_SLE; | |||
3763 | } | |||
3764 | } | |||
3765 | ||||
3766 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { | |||
3767 | switch (pred) { | |||
3768 | default: llvm_unreachable("Unknown icmp predicate!")__builtin_unreachable(); | |||
3769 | case ICMP_EQ: case ICMP_NE: | |||
3770 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: | |||
3771 | return pred; | |||
3772 | case ICMP_SGT: return ICMP_UGT; | |||
3773 | case ICMP_SLT: return ICMP_ULT; | |||
3774 | case ICMP_SGE: return ICMP_UGE; | |||
3775 | case ICMP_SLE: return ICMP_ULE; | |||
3776 | } | |||
3777 | } | |||
3778 | ||||
3779 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { | |||
3780 | switch (pred) { | |||
3781 | default: llvm_unreachable("Unknown cmp predicate!")__builtin_unreachable(); | |||
3782 | case ICMP_EQ: case ICMP_NE: | |||
3783 | return pred; | |||
3784 | case ICMP_SGT: return ICMP_SLT; | |||
3785 | case ICMP_SLT: return ICMP_SGT; | |||
3786 | case ICMP_SGE: return ICMP_SLE; | |||
3787 | case ICMP_SLE: return ICMP_SGE; | |||
3788 | case ICMP_UGT: return ICMP_ULT; | |||
3789 | case ICMP_ULT: return ICMP_UGT; | |||
3790 | case ICMP_UGE: return ICMP_ULE; | |||
3791 | case ICMP_ULE: return ICMP_UGE; | |||
3792 | ||||
3793 | case FCMP_FALSE: case FCMP_TRUE: | |||
3794 | case FCMP_OEQ: case FCMP_ONE: | |||
3795 | case FCMP_UEQ: case FCMP_UNE: | |||
3796 | case FCMP_ORD: case FCMP_UNO: | |||
3797 | return pred; | |||
3798 | case FCMP_OGT: return FCMP_OLT; | |||
3799 | case FCMP_OLT: return FCMP_OGT; | |||
3800 | case FCMP_OGE: return FCMP_OLE; | |||
3801 | case FCMP_OLE: return FCMP_OGE; | |||
3802 | case FCMP_UGT: return FCMP_ULT; | |||
3803 | case FCMP_ULT: return FCMP_UGT; | |||
3804 | case FCMP_UGE: return FCMP_ULE; | |||
3805 | case FCMP_ULE: return FCMP_UGE; | |||
3806 | } | |||
3807 | } | |||
3808 | ||||
3809 | bool CmpInst::isNonStrictPredicate(Predicate pred) { | |||
3810 | switch (pred) { | |||
3811 | case ICMP_SGE: | |||
3812 | case ICMP_SLE: | |||
3813 | case ICMP_UGE: | |||
3814 | case ICMP_ULE: | |||
3815 | case FCMP_OGE: | |||
3816 | case FCMP_OLE: | |||
3817 | case FCMP_UGE: | |||
3818 | case FCMP_ULE: | |||
3819 | return true; | |||
3820 | default: | |||
3821 | return false; | |||
3822 | } | |||
3823 | } | |||
3824 | ||||
3825 | bool CmpInst::isStrictPredicate(Predicate pred) { | |||
3826 | switch (pred) { | |||
3827 | case ICMP_SGT: | |||
3828 | case ICMP_SLT: | |||
3829 | case ICMP_UGT: | |||
3830 | case ICMP_ULT: | |||
3831 | case FCMP_OGT: | |||
3832 | case FCMP_OLT: | |||
3833 | case FCMP_UGT: | |||
3834 | case FCMP_ULT: | |||
3835 | return true; | |||
3836 | default: | |||
3837 | return false; | |||
3838 | } | |||
3839 | } | |||
3840 | ||||
3841 | CmpInst::Predicate CmpInst::getStrictPredicate(Predicate pred) { | |||
3842 | switch (pred) { | |||
3843 | case ICMP_SGE: | |||
3844 | return ICMP_SGT; | |||
3845 | case ICMP_SLE: | |||
3846 | return ICMP_SLT; | |||
3847 | case ICMP_UGE: | |||
3848 | return ICMP_UGT; | |||
3849 | case ICMP_ULE: | |||
3850 | return ICMP_ULT; | |||
3851 | case FCMP_OGE: | |||
3852 | return FCMP_OGT; | |||
3853 | case FCMP_OLE: | |||
3854 | return FCMP_OLT; | |||
3855 | case FCMP_UGE: | |||
3856 | return FCMP_UGT; | |||
3857 | case FCMP_ULE: | |||
3858 | return FCMP_ULT; | |||
3859 | default: | |||
3860 | return pred; | |||
3861 | } | |||
3862 | } | |||
3863 | ||||
3864 | CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { | |||
3865 | switch (pred) { | |||
3866 | case ICMP_SGT: | |||
3867 | return ICMP_SGE; | |||
3868 | case ICMP_SLT: | |||
3869 | return ICMP_SLE; | |||
3870 | case ICMP_UGT: | |||
3871 | return ICMP_UGE; | |||
3872 | case ICMP_ULT: | |||
3873 | return ICMP_ULE; | |||
3874 | case FCMP_OGT: | |||
3875 | return FCMP_OGE; | |||
3876 | case FCMP_OLT: | |||
3877 | return FCMP_OLE; | |||
3878 | case FCMP_UGT: | |||
3879 | return FCMP_UGE; | |||
3880 | case FCMP_ULT: | |||
3881 | return FCMP_ULE; | |||
3882 | default: | |||
3883 | return pred; | |||
3884 | } | |||
3885 | } | |||
3886 | ||||
3887 | CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { | |||
3888 | assert(CmpInst::isRelational(pred) && "Call only with relational predicate!")((void)0); | |||
3889 | ||||
3890 | if (isStrictPredicate(pred)) | |||
3891 | return getNonStrictPredicate(pred); | |||
3892 | if (isNonStrictPredicate(pred)) | |||
3893 | return getStrictPredicate(pred); | |||
3894 | ||||
3895 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3896 | } | |||
3897 | ||||
3898 | CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { | |||
3899 | assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!")((void)0); | |||
3900 | ||||
3901 | switch (pred) { | |||
3902 | default: | |||
3903 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3904 | case CmpInst::ICMP_ULT: | |||
3905 | return CmpInst::ICMP_SLT; | |||
3906 | case CmpInst::ICMP_ULE: | |||
3907 | return CmpInst::ICMP_SLE; | |||
3908 | case CmpInst::ICMP_UGT: | |||
3909 | return CmpInst::ICMP_SGT; | |||
3910 | case CmpInst::ICMP_UGE: | |||
3911 | return CmpInst::ICMP_SGE; | |||
3912 | } | |||
3913 | } | |||
3914 | ||||
3915 | CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) { | |||
3916 | assert(CmpInst::isSigned(pred) && "Call only with signed predicates!")((void)0); | |||
3917 | ||||
3918 | switch (pred) { | |||
3919 | default: | |||
3920 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3921 | case CmpInst::ICMP_SLT: | |||
3922 | return CmpInst::ICMP_ULT; | |||
3923 | case CmpInst::ICMP_SLE: | |||
3924 | return CmpInst::ICMP_ULE; | |||
3925 | case CmpInst::ICMP_SGT: | |||
3926 | return CmpInst::ICMP_UGT; | |||
3927 | case CmpInst::ICMP_SGE: | |||
3928 | return CmpInst::ICMP_UGE; | |||
3929 | } | |||
3930 | } | |||
3931 | ||||
3932 | bool CmpInst::isUnsigned(Predicate predicate) { | |||
3933 | switch (predicate) { | |||
3934 | default: return false; | |||
3935 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: | |||
3936 | case ICmpInst::ICMP_UGE: return true; | |||
3937 | } | |||
3938 | } | |||
3939 | ||||
3940 | bool CmpInst::isSigned(Predicate predicate) { | |||
3941 | switch (predicate) { | |||
3942 | default: return false; | |||
3943 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: | |||
3944 | case ICmpInst::ICMP_SGE: return true; | |||
3945 | } | |||
3946 | } | |||
3947 | ||||
3948 | CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) { | |||
3949 | assert(CmpInst::isRelational(pred) &&((void)0) | |||
3950 | "Call only with non-equality predicates!")((void)0); | |||
3951 | ||||
3952 | if (isSigned(pred)) | |||
3953 | return getUnsignedPredicate(pred); | |||
3954 | if (isUnsigned(pred)) | |||
3955 | return getSignedPredicate(pred); | |||
3956 | ||||
3957 | llvm_unreachable("Unknown predicate!")__builtin_unreachable(); | |||
3958 | } | |||
3959 | ||||
3960 | bool CmpInst::isOrdered(Predicate predicate) { | |||
3961 | switch (predicate) { | |||
3962 | default: return false; | |||
3963 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: | |||
3964 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: | |||
3965 | case FCmpInst::FCMP_ORD: return true; | |||
3966 | } | |||
3967 | } | |||
3968 | ||||
3969 | bool CmpInst::isUnordered(Predicate predicate) { | |||
3970 | switch (predicate) { | |||
3971 | default: return false; | |||
3972 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: | |||
3973 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: | |||
3974 | case FCmpInst::FCMP_UNO: return true; | |||
3975 | } | |||
3976 | } | |||
3977 | ||||
3978 | bool CmpInst::isTrueWhenEqual(Predicate predicate) { | |||
3979 | switch(predicate) { | |||
3980 | default: return false; | |||
3981 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: | |||
3982 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; | |||
3983 | } | |||
3984 | } | |||
3985 | ||||
3986 | bool CmpInst::isFalseWhenEqual(Predicate predicate) { | |||
3987 | switch(predicate) { | |||
3988 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: | |||
3989 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; | |||
3990 | default: return false; | |||
3991 | } | |||
3992 | } | |||
3993 | ||||
3994 | bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { | |||
3995 | // If the predicates match, then we know the first condition implies the | |||
3996 | // second is true. | |||
3997 | if (Pred1 == Pred2) | |||
3998 | return true; | |||
3999 | ||||
4000 | switch (Pred1) { | |||
4001 | default: | |||
4002 | break; | |||
4003 | case ICMP_EQ: | |||
4004 | // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. | |||
4005 | return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || | |||
4006 | Pred2 == ICMP_SLE; | |||
4007 | case ICMP_UGT: // A >u B implies A != B and A >=u B are true. | |||
4008 | return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; | |||
4009 | case ICMP_ULT: // A <u B implies A != B and A <=u B are true. | |||
4010 | return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; | |||
4011 | case ICMP_SGT: // A >s B implies A != B and A >=s B are true. | |||
4012 | return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; | |||
4013 | case ICMP_SLT: // A <s B implies A != B and A <=s B are true. | |||
4014 | return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; | |||
4015 | } | |||
4016 | return false; | |||
4017 | } | |||
4018 | ||||
4019 | bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { | |||
4020 | return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); | |||
4021 | } | |||
4022 | ||||
4023 | //===----------------------------------------------------------------------===// | |||
4024 | // SwitchInst Implementation | |||
4025 | //===----------------------------------------------------------------------===// | |||
4026 | ||||
4027 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { | |||
4028 | assert(Value && Default && NumReserved)((void)0); | |||
4029 | ReservedSpace = NumReserved; | |||
4030 | setNumHungOffUseOperands(2); | |||
4031 | allocHungoffUses(ReservedSpace); | |||
4032 | ||||
4033 | Op<0>() = Value; | |||
4034 | Op<1>() = Default; | |||
4035 | } | |||
4036 | ||||
4037 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to | |||
4038 | /// switch on and a default destination. The number of additional cases can | |||
4039 | /// be specified here to make memory allocation more efficient. This | |||
4040 | /// constructor can also autoinsert before another instruction. | |||
4041 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
4042 | Instruction *InsertBefore) | |||
4043 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, | |||
4044 | nullptr, 0, InsertBefore) { | |||
4045 | init(Value, Default, 2+NumCases*2); | |||
4046 | } | |||
4047 | ||||
4048 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to | |||
4049 | /// switch on and a default destination. The number of additional cases can | |||
4050 | /// be specified here to make memory allocation more efficient. This | |||
4051 | /// constructor also autoinserts at the end of the specified BasicBlock. | |||
4052 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
4053 | BasicBlock *InsertAtEnd) | |||
4054 | : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, | |||
4055 | nullptr, 0, InsertAtEnd) { | |||
4056 | init(Value, Default, 2+NumCases*2); | |||
4057 | } | |||
4058 | ||||
4059 | SwitchInst::SwitchInst(const SwitchInst &SI) | |||
4060 | : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { | |||
4061 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); | |||
4062 | setNumHungOffUseOperands(SI.getNumOperands()); | |||
4063 | Use *OL = getOperandList(); | |||
4064 | const Use *InOL = SI.getOperandList(); | |||
4065 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { | |||
4066 | OL[i] = InOL[i]; | |||
4067 | OL[i+1] = InOL[i+1]; | |||
4068 | } | |||
4069 | SubclassOptionalData = SI.SubclassOptionalData; | |||
4070 | } | |||
4071 | ||||
4072 | /// addCase - Add an entry to the switch instruction... | |||
4073 | /// | |||
4074 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { | |||
4075 | unsigned NewCaseIdx = getNumCases(); | |||
4076 | unsigned OpNo = getNumOperands(); | |||
4077 | if (OpNo+2 > ReservedSpace) | |||
4078 | growOperands(); // Get more space! | |||
4079 | // Initialize some new operands. | |||
4080 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!")((void)0); | |||
4081 | setNumHungOffUseOperands(OpNo+2); | |||
4082 | CaseHandle Case(this, NewCaseIdx); | |||
4083 | Case.setValue(OnVal); | |||
4084 | Case.setSuccessor(Dest); | |||
4085 | } | |||
4086 | ||||
4087 | /// removeCase - This method removes the specified case and its successor | |||
4088 | /// from the switch instruction. | |||
4089 | SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { | |||
4090 | unsigned idx = I->getCaseIndex(); | |||
4091 | ||||
4092 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!")((void)0); | |||
4093 | ||||
4094 | unsigned NumOps = getNumOperands(); | |||
4095 | Use *OL = getOperandList(); | |||
4096 | ||||
4097 | // Overwrite this case with the end of the list. | |||
4098 | if (2 + (idx + 1) * 2 != NumOps) { | |||
4099 | OL[2 + idx * 2] = OL[NumOps - 2]; | |||
4100 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; | |||
4101 | } | |||
4102 | ||||
4103 | // Nuke the last value. | |||
4104 | OL[NumOps-2].set(nullptr); | |||
4105 | OL[NumOps-2+1].set(nullptr); | |||
4106 | setNumHungOffUseOperands(NumOps-2); | |||
4107 | ||||
4108 | return CaseIt(this, idx); | |||
4109 | } | |||
4110 | ||||
4111 | /// growOperands - grow operands - This grows the operand list in response | |||
4112 | /// to a push_back style of operation. This grows the number of ops by 3 times. | |||
4113 | /// | |||
4114 | void SwitchInst::growOperands() { | |||
4115 | unsigned e = getNumOperands(); | |||
4116 | unsigned NumOps = e*3; | |||
4117 | ||||
4118 | ReservedSpace = NumOps; | |||
4119 | growHungoffUses(ReservedSpace); | |||
4120 | } | |||
4121 | ||||
4122 | MDNode * | |||
4123 | SwitchInstProfUpdateWrapper::getProfBranchWeightsMD(const SwitchInst &SI) { | |||
4124 | if (MDNode *ProfileData = SI.getMetadata(LLVMContext::MD_prof)) | |||
4125 | if (auto *MDName = dyn_cast<MDString>(ProfileData->getOperand(0))) | |||
4126 | if (MDName->getString() == "branch_weights") | |||
4127 | return ProfileData; | |||
4128 | return nullptr; | |||
4129 | } | |||
4130 | ||||
4131 | MDNode *SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() { | |||
4132 | assert(Changed && "called only if metadata has changed")((void)0); | |||
4133 | ||||
4134 | if (!Weights) | |||
4135 | return nullptr; | |||
4136 | ||||
4137 | assert(SI.getNumSuccessors() == Weights->size() &&((void)0) | |||
4138 | "num of prof branch_weights must accord with num of successors")((void)0); | |||
4139 | ||||
4140 | bool AllZeroes = | |||
4141 | all_of(Weights.getValue(), [](uint32_t W) { return W == 0; }); | |||
4142 | ||||
4143 | if (AllZeroes || Weights.getValue().size() < 2) | |||
4144 | return nullptr; | |||
4145 | ||||
4146 | return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights); | |||
4147 | } | |||
4148 | ||||
4149 | void SwitchInstProfUpdateWrapper::init() { | |||
4150 | MDNode *ProfileData = getProfBranchWeightsMD(SI); | |||
4151 | if (!ProfileData) | |||
4152 | return; | |||
4153 | ||||
4154 | if (ProfileData->getNumOperands() != SI.getNumSuccessors() + 1) { | |||
4155 | llvm_unreachable("number of prof branch_weights metadata operands does "__builtin_unreachable() | |||
4156 | "not correspond to number of succesors")__builtin_unreachable(); | |||
4157 | } | |||
4158 | ||||
4159 | SmallVector<uint32_t, 8> Weights; | |||
4160 | for (unsigned CI = 1, CE = SI.getNumSuccessors(); CI <= CE; ++CI) { | |||
4161 | ConstantInt *C = mdconst::extract<ConstantInt>(ProfileData->getOperand(CI)); | |||
4162 | uint32_t CW = C->getValue().getZExtValue(); | |||
4163 | Weights.push_back(CW); | |||
4164 | } | |||
4165 | this->Weights = std::move(Weights); | |||
4166 | } | |||
4167 | ||||
4168 | SwitchInst::CaseIt | |||
4169 | SwitchInstProfUpdateWrapper::removeCase(SwitchInst::CaseIt I) { | |||
4170 | if (Weights) { | |||
4171 | assert(SI.getNumSuccessors() == Weights->size() &&((void)0) | |||
4172 | "num of prof branch_weights must accord with num of successors")((void)0); | |||
4173 | Changed = true; | |||
4174 | // Copy the last case to the place of the removed one and shrink. | |||
4175 | // This is tightly coupled with the way SwitchInst::removeCase() removes | |||
4176 | // the cases in SwitchInst::removeCase(CaseIt). | |||
4177 | Weights.getValue()[I->getCaseIndex() + 1] = Weights.getValue().back(); | |||
4178 | Weights.getValue().pop_back(); | |||
4179 | } | |||
4180 | return SI.removeCase(I); | |||
4181 | } | |||
4182 | ||||
4183 | void SwitchInstProfUpdateWrapper::addCase( | |||
4184 | ConstantInt *OnVal, BasicBlock *Dest, | |||
4185 | SwitchInstProfUpdateWrapper::CaseWeightOpt W) { | |||
4186 | SI.addCase(OnVal, Dest); | |||
4187 | ||||
4188 | if (!Weights && W && *W) { | |||
4189 | Changed = true; | |||
4190 | Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); | |||
4191 | Weights.getValue()[SI.getNumSuccessors() - 1] = *W; | |||
4192 | } else if (Weights) { | |||
4193 | Changed = true; | |||
4194 | Weights.getValue().push_back(W ? *W : 0); | |||
4195 | } | |||
4196 | if (Weights) | |||
4197 | assert(SI.getNumSuccessors() == Weights->size() &&((void)0) | |||
4198 | "num of prof branch_weights must accord with num of successors")((void)0); | |||
4199 | } | |||
4200 | ||||
4201 | SymbolTableList<Instruction>::iterator | |||
4202 | SwitchInstProfUpdateWrapper::eraseFromParent() { | |||
4203 | // Instruction is erased. Mark as unchanged to not touch it in the destructor. | |||
4204 | Changed = false; | |||
4205 | if (Weights) | |||
4206 | Weights->resize(0); | |||
4207 | return SI.eraseFromParent(); | |||
4208 | } | |||
4209 | ||||
4210 | SwitchInstProfUpdateWrapper::CaseWeightOpt | |||
4211 | SwitchInstProfUpdateWrapper::getSuccessorWeight(unsigned idx) { | |||
4212 | if (!Weights) | |||
4213 | return None; | |||
4214 | return Weights.getValue()[idx]; | |||
4215 | } | |||
4216 | ||||
4217 | void SwitchInstProfUpdateWrapper::setSuccessorWeight( | |||
4218 | unsigned idx, SwitchInstProfUpdateWrapper::CaseWeightOpt W) { | |||
4219 | if (!W) | |||
4220 | return; | |||
4221 | ||||
4222 | if (!Weights && *W) | |||
4223 | Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); | |||
4224 | ||||
4225 | if (Weights) { | |||
4226 | auto &OldW = Weights.getValue()[idx]; | |||
4227 | if (*W != OldW) { | |||
4228 | Changed = true; | |||
4229 | OldW = *W; | |||
4230 | } | |||
4231 | } | |||
4232 | } | |||
4233 | ||||
4234 | SwitchInstProfUpdateWrapper::CaseWeightOpt | |||
4235 | SwitchInstProfUpdateWrapper::getSuccessorWeight(const SwitchInst &SI, | |||
4236 | unsigned idx) { | |||
4237 | if (MDNode *ProfileData = getProfBranchWeightsMD(SI)) | |||
4238 | if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1) | |||
4239 | return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1)) | |||
4240 | ->getValue() | |||
4241 | .getZExtValue(); | |||
4242 | ||||
4243 | return None; | |||
4244 | } | |||
4245 | ||||
4246 | //===----------------------------------------------------------------------===// | |||
4247 | // IndirectBrInst Implementation | |||
4248 | //===----------------------------------------------------------------------===// | |||
4249 | ||||
4250 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { | |||
4251 | assert(Address && Address->getType()->isPointerTy() &&((void)0) | |||
4252 | "Address of indirectbr must be a pointer")((void)0); | |||
4253 | ReservedSpace = 1+NumDests; | |||
4254 | setNumHungOffUseOperands(1); | |||
4255 | allocHungoffUses(ReservedSpace); | |||
4256 | ||||
4257 | Op<0>() = Address; | |||
4258 | } | |||
4259 | ||||
4260 | ||||
4261 | /// growOperands - grow operands - This grows the operand list in response | |||
4262 | /// to a push_back style of operation. This grows the number of ops by 2 times. | |||
4263 | /// | |||
4264 | void IndirectBrInst::growOperands() { | |||
4265 | unsigned e = getNumOperands(); | |||
4266 | unsigned NumOps = e*2; | |||
4267 | ||||
4268 | ReservedSpace = NumOps; | |||
4269 | growHungoffUses(ReservedSpace); | |||
4270 | } | |||
4271 | ||||
4272 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, | |||
4273 | Instruction *InsertBefore) | |||
4274 | : Instruction(Type::getVoidTy(Address->getContext()), | |||
4275 | Instruction::IndirectBr, nullptr, 0, InsertBefore) { | |||
4276 | init(Address, NumCases); | |||
4277 | } | |||
4278 | ||||
4279 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, | |||
4280 | BasicBlock *InsertAtEnd) | |||
4281 | : Instruction(Type::getVoidTy(Address->getContext()), | |||
4282 | Instruction::IndirectBr, nullptr, 0, InsertAtEnd) { | |||
4283 | init(Address, NumCases); | |||
4284 | } | |||
4285 | ||||
4286 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) | |||
4287 | : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, | |||
4288 | nullptr, IBI.getNumOperands()) { | |||
4289 | allocHungoffUses(IBI.getNumOperands()); | |||
4290 | Use *OL = getOperandList(); | |||
4291 | const Use *InOL = IBI.getOperandList(); | |||
4292 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) | |||
4293 | OL[i] = InOL[i]; | |||
4294 | SubclassOptionalData = IBI.SubclassOptionalData; | |||
4295 | } | |||
4296 | ||||
4297 | /// addDestination - Add a destination. | |||
4298 | /// | |||
4299 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { | |||
4300 | unsigned OpNo = getNumOperands(); | |||
4301 | if (OpNo+1 > ReservedSpace) | |||
4302 | growOperands(); // Get more space! | |||
4303 | // Initialize some new operands. | |||
4304 | assert(OpNo < ReservedSpace && "Growing didn't work!")((void)0); | |||
4305 | setNumHungOffUseOperands(OpNo+1); | |||
4306 | getOperandList()[OpNo] = DestBB; | |||
4307 | } | |||
4308 | ||||
4309 | /// removeDestination - This method removes the specified successor from the | |||
4310 | /// indirectbr instruction. | |||
4311 | void IndirectBrInst::removeDestination(unsigned idx) { | |||
4312 | assert(idx < getNumOperands()-1 && "Successor index out of range!")((void)0); | |||
4313 | ||||
4314 | unsigned NumOps = getNumOperands(); | |||
4315 | Use *OL = getOperandList(); | |||
4316 | ||||
4317 | // Replace this value with the last one. | |||
4318 | OL[idx+1] = OL[NumOps-1]; | |||
4319 | ||||
4320 | // Nuke the last value. | |||
4321 | OL[NumOps-1].set(nullptr); | |||
4322 | setNumHungOffUseOperands(NumOps-1); | |||
4323 | } | |||
4324 | ||||
4325 | //===----------------------------------------------------------------------===// | |||
4326 | // FreezeInst Implementation | |||
4327 | //===----------------------------------------------------------------------===// | |||
4328 | ||||
4329 | FreezeInst::FreezeInst(Value *S, | |||
4330 | const Twine &Name, Instruction *InsertBefore) | |||
4331 | : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) { | |||
4332 | setName(Name); | |||
4333 | } | |||
4334 | ||||
4335 | FreezeInst::FreezeInst(Value *S, | |||
4336 | const Twine &Name, BasicBlock *InsertAtEnd) | |||
4337 | : UnaryInstruction(S->getType(), Freeze, S, InsertAtEnd) { | |||
4338 | setName(Name); | |||
4339 | } | |||
4340 | ||||
4341 | //===----------------------------------------------------------------------===// | |||
4342 | // cloneImpl() implementations | |||
4343 | //===----------------------------------------------------------------------===// | |||
4344 | ||||
4345 | // Define these methods here so vtables don't get emitted into every translation | |||
4346 | // unit that uses these classes. | |||
4347 | ||||
4348 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { | |||
4349 | return new (getNumOperands()) GetElementPtrInst(*this); | |||
4350 | } | |||
4351 | ||||
4352 | UnaryOperator *UnaryOperator::cloneImpl() const { | |||
4353 | return Create(getOpcode(), Op<0>()); | |||
4354 | } | |||
4355 | ||||
4356 | BinaryOperator *BinaryOperator::cloneImpl() const { | |||
4357 | return Create(getOpcode(), Op<0>(), Op<1>()); | |||
4358 | } | |||
4359 | ||||
4360 | FCmpInst *FCmpInst::cloneImpl() const { | |||
4361 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); | |||
4362 | } | |||
4363 | ||||
4364 | ICmpInst *ICmpInst::cloneImpl() const { | |||
4365 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); | |||
4366 | } | |||
4367 | ||||
4368 | ExtractValueInst *ExtractValueInst::cloneImpl() const { | |||
4369 | return new ExtractValueInst(*this); | |||
4370 | } | |||
4371 | ||||
4372 | InsertValueInst *InsertValueInst::cloneImpl() const { | |||
4373 | return new InsertValueInst(*this); | |||
4374 | } | |||
4375 | ||||
4376 | AllocaInst *AllocaInst::cloneImpl() const { | |||
4377 | AllocaInst *Result = | |||
4378 | new AllocaInst(getAllocatedType(), getType()->getAddressSpace(), | |||
4379 | getOperand(0), getAlign()); | |||
4380 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); | |||
4381 | Result->setSwiftError(isSwiftError()); | |||
4382 | return Result; | |||
4383 | } | |||
4384 | ||||
4385 | LoadInst *LoadInst::cloneImpl() const { | |||
4386 | return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), | |||
4387 | getAlign(), getOrdering(), getSyncScopeID()); | |||
4388 | } | |||
4389 | ||||
4390 | StoreInst *StoreInst::cloneImpl() const { | |||
4391 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), getAlign(), | |||
4392 | getOrdering(), getSyncScopeID()); | |||
4393 | } | |||
4394 | ||||
4395 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { | |||
4396 | AtomicCmpXchgInst *Result = new AtomicCmpXchgInst( | |||
4397 | getOperand(0), getOperand(1), getOperand(2), getAlign(), | |||
4398 | getSuccessOrdering(), getFailureOrdering(), getSyncScopeID()); | |||
4399 | Result->setVolatile(isVolatile()); | |||
4400 | Result->setWeak(isWeak()); | |||
4401 | return Result; | |||
4402 | } | |||
4403 | ||||
4404 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { | |||
4405 | AtomicRMWInst *Result = | |||
4406 | new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), | |||
4407 | getAlign(), getOrdering(), getSyncScopeID()); | |||
4408 | Result->setVolatile(isVolatile()); | |||
4409 | return Result; | |||
4410 | } | |||
4411 | ||||
4412 | FenceInst *FenceInst::cloneImpl() const { | |||
4413 | return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); | |||
4414 | } | |||
4415 | ||||
4416 | TruncInst *TruncInst::cloneImpl() const { | |||
4417 | return new TruncInst(getOperand(0), getType()); | |||
4418 | } | |||
4419 | ||||
4420 | ZExtInst *ZExtInst::cloneImpl() const { | |||
4421 | return new ZExtInst(getOperand(0), getType()); | |||
4422 | } | |||
4423 | ||||
4424 | SExtInst *SExtInst::cloneImpl() const { | |||
4425 | return new SExtInst(getOperand(0), getType()); | |||
4426 | } | |||
4427 | ||||
4428 | FPTruncInst *FPTruncInst::cloneImpl() const { | |||
4429 | return new FPTruncInst(getOperand(0), getType()); | |||
4430 | } | |||
4431 | ||||
4432 | FPExtInst *FPExtInst::cloneImpl() const { | |||
4433 | return new FPExtInst(getOperand(0), getType()); | |||
4434 | } | |||
4435 | ||||
4436 | UIToFPInst *UIToFPInst::cloneImpl() const { | |||
4437 | return new UIToFPInst(getOperand(0), getType()); | |||
4438 | } | |||
4439 | ||||
4440 | SIToFPInst *SIToFPInst::cloneImpl() const { | |||
4441 | return new SIToFPInst(getOperand(0), getType()); | |||
4442 | } | |||
4443 | ||||
4444 | FPToUIInst *FPToUIInst::cloneImpl() const { | |||
4445 | return new FPToUIInst(getOperand(0), getType()); | |||
4446 | } | |||
4447 | ||||
4448 | FPToSIInst *FPToSIInst::cloneImpl() const { | |||
4449 | return new FPToSIInst(getOperand(0), getType()); | |||
4450 | } | |||
4451 | ||||
4452 | PtrToIntInst *PtrToIntInst::cloneImpl() const { | |||
4453 | return new PtrToIntInst(getOperand(0), getType()); | |||
4454 | } | |||
4455 | ||||
4456 | IntToPtrInst *IntToPtrInst::cloneImpl() const { | |||
4457 | return new IntToPtrInst(getOperand(0), getType()); | |||
4458 | } | |||
4459 | ||||
4460 | BitCastInst *BitCastInst::cloneImpl() const { | |||
4461 | return new BitCastInst(getOperand(0), getType()); | |||
4462 | } | |||
4463 | ||||
4464 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { | |||
4465 | return new AddrSpaceCastInst(getOperand(0), getType()); | |||
4466 | } | |||
4467 | ||||
4468 | CallInst *CallInst::cloneImpl() const { | |||
4469 | if (hasOperandBundles()) { | |||
4470 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4471 | return new(getNumOperands(), DescriptorBytes) CallInst(*this); | |||
4472 | } | |||
4473 | return new(getNumOperands()) CallInst(*this); | |||
4474 | } | |||
4475 | ||||
4476 | SelectInst *SelectInst::cloneImpl() const { | |||
4477 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); | |||
4478 | } | |||
4479 | ||||
4480 | VAArgInst *VAArgInst::cloneImpl() const { | |||
4481 | return new VAArgInst(getOperand(0), getType()); | |||
4482 | } | |||
4483 | ||||
4484 | ExtractElementInst *ExtractElementInst::cloneImpl() const { | |||
4485 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); | |||
4486 | } | |||
4487 | ||||
4488 | InsertElementInst *InsertElementInst::cloneImpl() const { | |||
4489 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); | |||
4490 | } | |||
4491 | ||||
4492 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { | |||
4493 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getShuffleMask()); | |||
4494 | } | |||
4495 | ||||
4496 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } | |||
4497 | ||||
4498 | LandingPadInst *LandingPadInst::cloneImpl() const { | |||
4499 | return new LandingPadInst(*this); | |||
4500 | } | |||
4501 | ||||
4502 | ReturnInst *ReturnInst::cloneImpl() const { | |||
4503 | return new(getNumOperands()) ReturnInst(*this); | |||
4504 | } | |||
4505 | ||||
4506 | BranchInst *BranchInst::cloneImpl() const { | |||
4507 | return new(getNumOperands()) BranchInst(*this); | |||
4508 | } | |||
4509 | ||||
4510 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } | |||
4511 | ||||
4512 | IndirectBrInst *IndirectBrInst::cloneImpl() const { | |||
4513 | return new IndirectBrInst(*this); | |||
4514 | } | |||
4515 | ||||
4516 | InvokeInst *InvokeInst::cloneImpl() const { | |||
4517 | if (hasOperandBundles()) { | |||
4518 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4519 | return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); | |||
4520 | } | |||
4521 | return new(getNumOperands()) InvokeInst(*this); | |||
4522 | } | |||
4523 | ||||
4524 | CallBrInst *CallBrInst::cloneImpl() const { | |||
4525 | if (hasOperandBundles()) { | |||
4526 | unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); | |||
4527 | return new (getNumOperands(), DescriptorBytes) CallBrInst(*this); | |||
4528 | } | |||
4529 | return new (getNumOperands()) CallBrInst(*this); | |||
4530 | } | |||
4531 | ||||
4532 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } | |||
4533 | ||||
4534 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { | |||
4535 | return new (getNumOperands()) CleanupReturnInst(*this); | |||
4536 | } | |||
4537 | ||||
4538 | CatchReturnInst *CatchReturnInst::cloneImpl() const { | |||
4539 | return new (getNumOperands()) CatchReturnInst(*this); | |||
4540 | } | |||
4541 | ||||
4542 | CatchSwitchInst *CatchSwitchInst::cloneImpl() const { | |||
4543 | return new CatchSwitchInst(*this); | |||
4544 | } | |||
4545 | ||||
4546 | FuncletPadInst *FuncletPadInst::cloneImpl() const { | |||
4547 | return new (getNumOperands()) FuncletPadInst(*this); | |||
4548 | } | |||
4549 | ||||
4550 | UnreachableInst *UnreachableInst::cloneImpl() const { | |||
4551 | LLVMContext &Context = getContext(); | |||
4552 | return new UnreachableInst(Context); | |||
4553 | } | |||
4554 | ||||
4555 | FreezeInst *FreezeInst::cloneImpl() const { | |||
4556 | return new FreezeInst(getOperand(0)); | |||
4557 | } |
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 { (( 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) { ((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 { ((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) { ((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 { ((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) { ((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() &&((void)0) |
1282 | "Only valid to query with a use of this instruction!")((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!")((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!")((void)0); |
1340 | return getOperand(i); |
1341 | } |
1342 | |
1343 | void setArgOperand(unsigned i, Value *v) { |
1344 | assert(i < getNumArgOperands() && "Out of bounds!")((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!")((void)0); |
1351 | return User::getOperandUse(i); |
1352 | } |
1353 | Use &getArgOperandUse(unsigned i) { |
1354 | assert(i < getNumArgOperands() && "Out of bounds!")((void)0); |
1355 | return User::getOperandUse(i); |
1356 | } |
1357 | |
1358 | bool isArgOperand(const Use *U) const { |
1359 | assert(this == U->getUser() &&((void)0) |
1360 | "Only valid to query with a use of this instruction!")((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!")((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))((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())((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 &&((void)0) |
1479 | "Use CallBase::isNoBuiltin() to check for Attribute::NoBuiltin")((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 | /// adds the attribute to the list of attributes. |
1489 | void addAttribute(unsigned i, Attribute::AttrKind Kind) { |
1490 | AttributeList PAL = getAttributes(); |
1491 | PAL = PAL.addAttribute(getContext(), i, Kind); |
1492 | setAttributes(PAL); |
1493 | } |
1494 | |
1495 | /// adds the attribute to the list of attributes. |
1496 | void addAttribute(unsigned i, Attribute Attr) { |
1497 | AttributeList PAL = getAttributes(); |
1498 | PAL = PAL.addAttribute(getContext(), i, Attr); |
1499 | setAttributes(PAL); |
1500 | } |
1501 | |
1502 | /// Adds the attribute to the indicated argument |
1503 | void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1504 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1505 | AttributeList PAL = getAttributes(); |
1506 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); |
1507 | setAttributes(PAL); |
1508 | } |
1509 | |
1510 | /// Adds the attribute to the indicated argument |
1511 | void addParamAttr(unsigned ArgNo, Attribute Attr) { |
1512 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1513 | AttributeList PAL = getAttributes(); |
1514 | PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); |
1515 | setAttributes(PAL); |
1516 | } |
1517 | |
1518 | /// removes the attribute from the list of attributes. |
1519 | void removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
1520 | AttributeList PAL = getAttributes(); |
1521 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1522 | setAttributes(PAL); |
1523 | } |
1524 | |
1525 | /// removes the attribute from the list of attributes. |
1526 | void removeAttribute(unsigned i, StringRef Kind) { |
1527 | AttributeList PAL = getAttributes(); |
1528 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
1529 | setAttributes(PAL); |
1530 | } |
1531 | |
1532 | void removeAttributes(unsigned i, const AttrBuilder &Attrs) { |
1533 | AttributeList PAL = getAttributes(); |
1534 | PAL = PAL.removeAttributes(getContext(), i, Attrs); |
1535 | setAttributes(PAL); |
1536 | } |
1537 | |
1538 | /// Removes the attribute from the given argument |
1539 | void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
1540 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1541 | AttributeList PAL = getAttributes(); |
1542 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1543 | setAttributes(PAL); |
1544 | } |
1545 | |
1546 | /// Removes the attribute from the given argument |
1547 | void removeParamAttr(unsigned ArgNo, StringRef Kind) { |
1548 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1549 | AttributeList PAL = getAttributes(); |
1550 | PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); |
1551 | setAttributes(PAL); |
1552 | } |
1553 | |
1554 | /// Removes the attributes from the given argument |
1555 | void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { |
1556 | AttributeList PAL = getAttributes(); |
1557 | PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs); |
1558 | setAttributes(PAL); |
1559 | } |
1560 | |
1561 | /// adds the dereferenceable attribute to the list of attributes. |
1562 | void addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
1563 | AttributeList PAL = getAttributes(); |
1564 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
1565 | setAttributes(PAL); |
1566 | } |
1567 | |
1568 | /// adds the dereferenceable_or_null attribute to the list of |
1569 | /// attributes. |
1570 | void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
1571 | AttributeList PAL = getAttributes(); |
1572 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
1573 | setAttributes(PAL); |
1574 | } |
1575 | |
1576 | /// Determine whether the return value has the given attribute. |
1577 | bool hasRetAttr(Attribute::AttrKind Kind) const { |
1578 | return hasRetAttrImpl(Kind); |
1579 | } |
1580 | /// Determine whether the return value has the given attribute. |
1581 | bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } |
1582 | |
1583 | /// Determine whether the argument or parameter has the given attribute. |
1584 | bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; |
1585 | |
1586 | /// Get the attribute of a given kind at a position. |
1587 | Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { |
1588 | return getAttributes().getAttribute(i, Kind); |
1589 | } |
1590 | |
1591 | /// Get the attribute of a given kind at a position. |
1592 | Attribute getAttribute(unsigned i, StringRef Kind) const { |
1593 | return getAttributes().getAttribute(i, Kind); |
1594 | } |
1595 | |
1596 | /// Get the attribute of a given kind from a given arg |
1597 | Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { |
1598 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1599 | return getAttributes().getParamAttr(ArgNo, Kind); |
1600 | } |
1601 | |
1602 | /// Get the attribute of a given kind from a given arg |
1603 | Attribute getParamAttr(unsigned ArgNo, StringRef Kind) const { |
1604 | assert(ArgNo < getNumArgOperands() && "Out of bounds")((void)0); |
1605 | return getAttributes().getParamAttr(ArgNo, Kind); |
1606 | } |
1607 | |
1608 | /// Return true if the data operand at index \p i has the attribute \p |
1609 | /// A. |
1610 | /// |
1611 | /// Data operands include call arguments and values used in operand bundles, |
1612 | /// but does not include the callee operand. This routine dispatches to the |
1613 | /// underlying AttributeList or the OperandBundleUser as appropriate. |
1614 | /// |
1615 | /// The index \p i is interpreted as |
1616 | /// |
1617 | /// \p i == Attribute::ReturnIndex -> the return value |
1618 | /// \p i in [1, arg_size + 1) -> argument number (\p i - 1) |
1619 | /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index |
1620 | /// (\p i - 1) in the operand list. |
1621 | bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const { |
1622 | // Note that we have to add one because `i` isn't zero-indexed. |
1623 | assert(i < (getNumArgOperands() + getNumTotalBundleOperands() + 1) &&((void)0) |
1624 | "Data operand index out of bounds!")((void)0); |
1625 | |
1626 | // The attribute A can either be directly specified, if the operand in |
1627 | // question is a call argument; or be indirectly implied by the kind of its |
1628 | // containing operand bundle, if the operand is a bundle operand. |
1629 | |
1630 | if (i == AttributeList::ReturnIndex) |
1631 | return hasRetAttr(Kind); |
1632 | |
1633 | // FIXME: Avoid these i - 1 calculations and update the API to use |
1634 | // zero-based indices. |
1635 | if (i < (getNumArgOperands() + 1)) |
1636 | return paramHasAttr(i - 1, Kind); |
1637 | |
1638 | assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&((void)0) |
1639 | "Must be either a call argument or an operand bundle!")((void)0); |
1640 | return bundleOperandHasAttr(i - 1, Kind); |
1641 | } |
1642 | |
1643 | /// Determine whether this data operand is not captured. |
1644 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1645 | // better indicate that this may return a conservative answer. |
1646 | bool doesNotCapture(unsigned OpNo) const { |
1647 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture); |
1648 | } |
1649 | |
1650 | /// Determine whether this argument is passed by value. |
1651 | bool isByValArgument(unsigned ArgNo) const { |
1652 | return paramHasAttr(ArgNo, Attribute::ByVal); |
1653 | } |
1654 | |
1655 | /// Determine whether this argument is passed in an alloca. |
1656 | bool isInAllocaArgument(unsigned ArgNo) const { |
1657 | return paramHasAttr(ArgNo, Attribute::InAlloca); |
1658 | } |
1659 | |
1660 | /// Determine whether this argument is passed by value, in an alloca, or is |
1661 | /// preallocated. |
1662 | bool isPassPointeeByValueArgument(unsigned ArgNo) const { |
1663 | return paramHasAttr(ArgNo, Attribute::ByVal) || |
1664 | paramHasAttr(ArgNo, Attribute::InAlloca) || |
1665 | paramHasAttr(ArgNo, Attribute::Preallocated); |
1666 | } |
1667 | |
1668 | /// Determine whether passing undef to this argument is undefined behavior. |
1669 | /// If passing undef to this argument is UB, passing poison is UB as well |
1670 | /// because poison is more undefined than undef. |
1671 | bool isPassingUndefUB(unsigned ArgNo) const { |
1672 | return paramHasAttr(ArgNo, Attribute::NoUndef) || |
1673 | // dereferenceable implies noundef. |
1674 | paramHasAttr(ArgNo, Attribute::Dereferenceable) || |
1675 | // dereferenceable implies noundef, and null is a well-defined value. |
1676 | paramHasAttr(ArgNo, Attribute::DereferenceableOrNull); |
1677 | } |
1678 | |
1679 | /// Determine if there are is an inalloca argument. Only the last argument can |
1680 | /// have the inalloca attribute. |
1681 | bool hasInAllocaArgument() const { |
1682 | return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca); |
1683 | } |
1684 | |
1685 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1686 | // better indicate that this may return a conservative answer. |
1687 | bool doesNotAccessMemory(unsigned OpNo) const { |
1688 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1689 | } |
1690 | |
1691 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1692 | // better indicate that this may return a conservative answer. |
1693 | bool onlyReadsMemory(unsigned OpNo) const { |
1694 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) || |
1695 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1696 | } |
1697 | |
1698 | // FIXME: Once this API is no longer duplicated in `CallSite`, rename this to |
1699 | // better indicate that this may return a conservative answer. |
1700 | bool doesNotReadMemory(unsigned OpNo) const { |
1701 | return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) || |
1702 | dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone); |
1703 | } |
1704 | |
1705 | /// Extract the alignment of the return value. |
1706 | MaybeAlign getRetAlign() const { return Attrs.getRetAlignment(); } |
1707 | |
1708 | /// Extract the alignment for a call or parameter (0=unknown). |
1709 | MaybeAlign getParamAlign(unsigned ArgNo) const { |
1710 | return Attrs.getParamAlignment(ArgNo); |
1711 | } |
1712 | |
1713 | MaybeAlign getParamStackAlign(unsigned ArgNo) const { |
1714 | return Attrs.getParamStackAlignment(ArgNo); |
1715 | } |
1716 | |
1717 | /// Extract the byval type for a call or parameter. |
1718 | Type *getParamByValType(unsigned ArgNo) const { |
1719 | if (auto *Ty = Attrs.getParamByValType(ArgNo)) |
1720 | return Ty; |
1721 | if (const Function *F = getCalledFunction()) |
1722 | return F->getAttributes().getParamByValType(ArgNo); |
1723 | return nullptr; |
1724 | } |
1725 | |
1726 | /// Extract the preallocated type for a call or parameter. |
1727 | Type *getParamPreallocatedType(unsigned ArgNo) const { |
1728 | if (auto *Ty = Attrs.getParamPreallocatedType(ArgNo)) |
1729 | return Ty; |
1730 | if (const Function *F = getCalledFunction()) |
1731 | return F->getAttributes().getParamPreallocatedType(ArgNo); |
1732 | return nullptr; |
1733 | } |
1734 | |
1735 | /// Extract the preallocated type for a call or parameter. |
1736 | Type *getParamInAllocaType(unsigned ArgNo) const { |
1737 | if (auto *Ty = Attrs.getParamInAllocaType(ArgNo)) |
1738 | return Ty; |
1739 | if (const Function *F = getCalledFunction()) |
1740 | return F->getAttributes().getParamInAllocaType(ArgNo); |
1741 | return nullptr; |
1742 | } |
1743 | |
1744 | /// Extract the number of dereferenceable bytes for a call or |
1745 | /// parameter (0=unknown). |
1746 | uint64_t getDereferenceableBytes(unsigned i) const { |
1747 | return Attrs.getDereferenceableBytes(i); |
1748 | } |
1749 | |
1750 | /// Extract the number of dereferenceable_or_null bytes for a call or |
1751 | /// parameter (0=unknown). |
1752 | uint64_t getDereferenceableOrNullBytes(unsigned i) const { |
1753 | return Attrs.getDereferenceableOrNullBytes(i); |
1754 | } |
1755 | |
1756 | /// Return true if the return value is known to be not null. |
1757 | /// This may be because it has the nonnull attribute, or because at least |
1758 | /// one byte is dereferenceable and the pointer is in addrspace(0). |
1759 | bool isReturnNonNull() const; |
1760 | |
1761 | /// Determine if the return value is marked with NoAlias attribute. |
1762 | bool returnDoesNotAlias() const { |
1763 | return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); |
1764 | } |
1765 | |
1766 | /// If one of the arguments has the 'returned' attribute, returns its |
1767 | /// operand value. Otherwise, return nullptr. |
1768 | Value *getReturnedArgOperand() const; |
1769 | |
1770 | /// Return true if the call should not be treated as a call to a |
1771 | /// builtin. |
1772 | bool isNoBuiltin() const { |
1773 | return hasFnAttrImpl(Attribute::NoBuiltin) && |
1774 | !hasFnAttrImpl(Attribute::Builtin); |
1775 | } |
1776 | |
1777 | /// Determine if the call requires strict floating point semantics. |
1778 | bool isStrictFP() const { return hasFnAttr(Attribute::StrictFP); } |
1779 | |
1780 | /// Return true if the call should not be inlined. |
1781 | bool isNoInline() const { return hasFnAttr(Attribute::NoInline); } |
1782 | void setIsNoInline() { |
1783 | addAttribute(AttributeList::FunctionIndex, Attribute::NoInline); |
1784 | } |
1785 | /// Determine if the call does not access memory. |
1786 | bool doesNotAccessMemory() const { return hasFnAttr(Attribute::ReadNone); } |
1787 | void setDoesNotAccessMemory() { |
1788 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); |
1789 | } |
1790 | |
1791 | /// Determine if the call does not access or only reads memory. |
1792 | bool onlyReadsMemory() const { |
1793 | return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly); |
1794 | } |
1795 | |
1796 | void setOnlyReadsMemory() { |
1797 | addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly); |
1798 | } |
1799 | |
1800 | /// Determine if the call does not access or only writes memory. |
1801 | bool doesNotReadMemory() const { |
1802 | return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly); |
1803 | } |
1804 | void setDoesNotReadMemory() { |
1805 | addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly); |
1806 | } |
1807 | |
1808 | /// Determine if the call can access memmory only using pointers based |
1809 | /// on its arguments. |
1810 | bool onlyAccessesArgMemory() const { |
1811 | return hasFnAttr(Attribute::ArgMemOnly); |
1812 | } |
1813 | void setOnlyAccessesArgMemory() { |
1814 | addAttribute(AttributeList::FunctionIndex, Attribute::ArgMemOnly); |
1815 | } |
1816 | |
1817 | /// Determine if the function may only access memory that is |
1818 | /// inaccessible from the IR. |
1819 | bool onlyAccessesInaccessibleMemory() const { |
1820 | return hasFnAttr(Attribute::InaccessibleMemOnly); |
1821 | } |
1822 | void setOnlyAccessesInaccessibleMemory() { |
1823 | addAttribute(AttributeList::FunctionIndex, Attribute::InaccessibleMemOnly); |
1824 | } |
1825 | |
1826 | /// Determine if the function may only access memory that is |
1827 | /// either inaccessible from the IR or pointed to by its arguments. |
1828 | bool onlyAccessesInaccessibleMemOrArgMem() const { |
1829 | return hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly); |
1830 | } |
1831 | void setOnlyAccessesInaccessibleMemOrArgMem() { |
1832 | addAttribute(AttributeList::FunctionIndex, |
1833 | Attribute::InaccessibleMemOrArgMemOnly); |
1834 | } |
1835 | /// Determine if the call cannot return. |
1836 | bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); } |
1837 | void setDoesNotReturn() { |
1838 | addAttribute(AttributeList::FunctionIndex, Attribute::NoReturn); |
1839 | } |
1840 | |
1841 | /// Determine if the call should not perform indirect branch tracking. |
1842 | bool doesNoCfCheck() const { return hasFnAttr(Attribute::NoCfCheck); } |
1843 | |
1844 | /// Determine if the call cannot unwind. |
1845 | bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); } |
1846 | void setDoesNotThrow() { |
1847 | addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); |
1848 | } |
1849 | |
1850 | /// Determine if the invoke cannot be duplicated. |
1851 | bool cannotDuplicate() const { return hasFnAttr(Attribute::NoDuplicate); } |
1852 | void setCannotDuplicate() { |
1853 | addAttribute(AttributeList::FunctionIndex, Attribute::NoDuplicate); |
1854 | } |
1855 | |
1856 | /// Determine if the call cannot be tail merged. |
1857 | bool cannotMerge() const { return hasFnAttr(Attribute::NoMerge); } |
1858 | void setCannotMerge() { |
1859 | addAttribute(AttributeList::FunctionIndex, Attribute::NoMerge); |
1860 | } |
1861 | |
1862 | /// Determine if the invoke is convergent |
1863 | bool isConvergent() const { return hasFnAttr(Attribute::Convergent); } |
1864 | void setConvergent() { |
1865 | addAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1866 | } |
1867 | void setNotConvergent() { |
1868 | removeAttribute(AttributeList::FunctionIndex, Attribute::Convergent); |
1869 | } |
1870 | |
1871 | /// Determine if the call returns a structure through first |
1872 | /// pointer argument. |
1873 | bool hasStructRetAttr() const { |
1874 | if (getNumArgOperands() == 0) |
1875 | return false; |
1876 | |
1877 | // Be friendly and also check the callee. |
1878 | return paramHasAttr(0, Attribute::StructRet); |
1879 | } |
1880 | |
1881 | /// Determine if any call argument is an aggregate passed by value. |
1882 | bool hasByValArgument() const { |
1883 | return Attrs.hasAttrSomewhere(Attribute::ByVal); |
1884 | } |
1885 | |
1886 | ///@{ |
1887 | // End of attribute API. |
1888 | |
1889 | /// \name Operand Bundle API |
1890 | /// |
1891 | /// This group of methods provides the API to access and manipulate operand |
1892 | /// bundles on this call. |
1893 | /// @{ |
1894 | |
1895 | /// Return the number of operand bundles associated with this User. |
1896 | unsigned getNumOperandBundles() const { |
1897 | return std::distance(bundle_op_info_begin(), bundle_op_info_end()); |
1898 | } |
1899 | |
1900 | /// Return true if this User has any operand bundles. |
1901 | bool hasOperandBundles() const { return getNumOperandBundles() != 0; } |
1902 | |
1903 | /// Return the index of the first bundle operand in the Use array. |
1904 | unsigned getBundleOperandsStartIndex() const { |
1905 | assert(hasOperandBundles() && "Don't call otherwise!")((void)0); |
1906 | return bundle_op_info_begin()->Begin; |
1907 | } |
1908 | |
1909 | /// Return the index of the last bundle operand in the Use array. |
1910 | unsigned getBundleOperandsEndIndex() const { |
1911 | assert(hasOperandBundles() && "Don't call otherwise!")((void)0); |
1912 | return bundle_op_info_end()[-1].End; |
1913 | } |
1914 | |
1915 | /// Return true if the operand at index \p Idx is a bundle operand. |
1916 | bool isBundleOperand(unsigned Idx) const { |
1917 | return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() && |
1918 | Idx < getBundleOperandsEndIndex(); |
1919 | } |
1920 | |
1921 | /// Returns true if the use is a bundle operand. |
1922 | bool isBundleOperand(const Use *U) const { |
1923 | assert(this == U->getUser() &&((void)0) |
1924 | "Only valid to query with a use of this instruction!")((void)0); |
1925 | return hasOperandBundles() && isBundleOperand(U - op_begin()); |
1926 | } |
1927 | bool isBundleOperand(Value::const_user_iterator UI) const { |
1928 | return isBundleOperand(&UI.getUse()); |
1929 | } |
1930 | |
1931 | /// Return the total number operands (not operand bundles) used by |
1932 | /// every operand bundle in this OperandBundleUser. |
1933 | unsigned getNumTotalBundleOperands() const { |
1934 | if (!hasOperandBundles()) |
1935 | return 0; |
1936 | |
1937 | unsigned Begin = getBundleOperandsStartIndex(); |
1938 | unsigned End = getBundleOperandsEndIndex(); |
1939 | |
1940 | assert(Begin <= End && "Should be!")((void)0); |
1941 | return End - Begin; |
1942 | } |
1943 | |
1944 | /// Return the operand bundle at a specific index. |
1945 | OperandBundleUse getOperandBundleAt(unsigned Index) const { |
1946 | assert(Index < getNumOperandBundles() && "Index out of bounds!")((void)0); |
1947 | return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index)); |
1948 | } |
1949 | |
1950 | /// Return the number of operand bundles with the tag Name attached to |
1951 | /// this instruction. |
1952 | unsigned countOperandBundlesOfType(StringRef Name) const { |
1953 | unsigned Count = 0; |
1954 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1955 | if (getOperandBundleAt(i).getTagName() == Name) |
1956 | Count++; |
1957 | |
1958 | return Count; |
1959 | } |
1960 | |
1961 | /// Return the number of operand bundles with the tag ID attached to |
1962 | /// this instruction. |
1963 | unsigned countOperandBundlesOfType(uint32_t ID) const { |
1964 | unsigned Count = 0; |
1965 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) |
1966 | if (getOperandBundleAt(i).getTagID() == ID) |
1967 | Count++; |
1968 | |
1969 | return Count; |
1970 | } |
1971 | |
1972 | /// Return an operand bundle by name, if present. |
1973 | /// |
1974 | /// It is an error to call this for operand bundle types that may have |
1975 | /// multiple instances of them on the same instruction. |
1976 | Optional<OperandBundleUse> getOperandBundle(StringRef Name) const { |
1977 | assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!")((void)0); |
1978 | |
1979 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1980 | OperandBundleUse U = getOperandBundleAt(i); |
1981 | if (U.getTagName() == Name) |
1982 | return U; |
1983 | } |
1984 | |
1985 | return None; |
1986 | } |
1987 | |
1988 | /// Return an operand bundle by tag ID, if present. |
1989 | /// |
1990 | /// It is an error to call this for operand bundle types that may have |
1991 | /// multiple instances of them on the same instruction. |
1992 | Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const { |
1993 | assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!")((void)0); |
1994 | |
1995 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
1996 | OperandBundleUse U = getOperandBundleAt(i); |
1997 | if (U.getTagID() == ID) |
1998 | return U; |
1999 | } |
2000 | |
2001 | return None; |
2002 | } |
2003 | |
2004 | /// Return the list of operand bundles attached to this instruction as |
2005 | /// a vector of OperandBundleDefs. |
2006 | /// |
2007 | /// This function copies the OperandBundeUse instances associated with this |
2008 | /// OperandBundleUser to a vector of OperandBundleDefs. Note: |
2009 | /// OperandBundeUses and OperandBundleDefs are non-trivially *different* |
2010 | /// representations of operand bundles (see documentation above). |
2011 | void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const; |
2012 | |
2013 | /// Return the operand bundle for the operand at index OpIdx. |
2014 | /// |
2015 | /// It is an error to call this with an OpIdx that does not correspond to an |
2016 | /// bundle operand. |
2017 | OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const { |
2018 | return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx)); |
2019 | } |
2020 | |
2021 | /// Return true if this operand bundle user has operand bundles that |
2022 | /// may read from the heap. |
2023 | bool hasReadingOperandBundles() const; |
2024 | |
2025 | /// Return true if this operand bundle user has operand bundles that |
2026 | /// may write to the heap. |
2027 | bool hasClobberingOperandBundles() const { |
2028 | for (auto &BOI : bundle_op_infos()) { |
2029 | if (BOI.Tag->second == LLVMContext::OB_deopt || |
2030 | BOI.Tag->second == LLVMContext::OB_funclet) |
2031 | continue; |
2032 | |
2033 | // This instruction has an operand bundle that is not known to us. |
2034 | // Assume the worst. |
2035 | return true; |
2036 | } |
2037 | |
2038 | return false; |
2039 | } |
2040 | |
2041 | /// Return true if the bundle operand at index \p OpIdx has the |
2042 | /// attribute \p A. |
2043 | bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const { |
2044 | auto &BOI = getBundleOpInfoForOperand(OpIdx); |
2045 | auto OBU = operandBundleFromBundleOpInfo(BOI); |
2046 | return OBU.operandHasAttr(OpIdx - BOI.Begin, A); |
2047 | } |
2048 | |
2049 | /// Return true if \p Other has the same sequence of operand bundle |
2050 | /// tags with the same number of operands on each one of them as this |
2051 | /// OperandBundleUser. |
2052 | bool hasIdenticalOperandBundleSchema(const CallBase &Other) const { |
2053 | if (getNumOperandBundles() != Other.getNumOperandBundles()) |
2054 | return false; |
2055 | |
2056 | return std::equal(bundle_op_info_begin(), bundle_op_info_end(), |
2057 | Other.bundle_op_info_begin()); |
2058 | } |
2059 | |
2060 | /// Return true if this operand bundle user contains operand bundles |
2061 | /// with tags other than those specified in \p IDs. |
2062 | bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const { |
2063 | for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) { |
2064 | uint32_t ID = getOperandBundleAt(i).getTagID(); |
2065 | if (!is_contained(IDs, ID)) |
2066 | return true; |
2067 | } |
2068 | return false; |
2069 | } |
2070 | |
2071 | /// Is the function attribute S disallowed by some operand bundle on |
2072 | /// this operand bundle user? |
2073 | bool isFnAttrDisallowedByOpBundle(StringRef S) const { |
2074 | // Operand bundles only possibly disallow readnone, readonly and argmemonly |
2075 | // attributes. All String attributes are fine. |
2076 | return false; |
2077 | } |
2078 | |
2079 | /// Is the function attribute A disallowed by some operand bundle on |
2080 | /// this operand bundle user? |
2081 | bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const { |
2082 | switch (A) { |
2083 | default: |
2084 | return false; |
2085 | |
2086 | case Attribute::InaccessibleMemOrArgMemOnly: |
2087 | return hasReadingOperandBundles(); |
2088 | |
2089 | case Attribute::InaccessibleMemOnly: |
2090 | return hasReadingOperandBundles(); |
2091 | |
2092 | case Attribute::ArgMemOnly: |
2093 | return hasReadingOperandBundles(); |
2094 | |
2095 | case Attribute::ReadNone: |
2096 | return hasReadingOperandBundles(); |
2097 | |
2098 | case Attribute::ReadOnly: |
2099 | return hasClobberingOperandBundles(); |
2100 | } |
2101 | |
2102 | llvm_unreachable("switch has a default case!")__builtin_unreachable(); |
2103 | } |
2104 | |
2105 | /// Used to keep track of an operand bundle. See the main comment on |
2106 | /// OperandBundleUser above. |
2107 | struct BundleOpInfo { |
2108 | /// The operand bundle tag, interned by |
2109 | /// LLVMContextImpl::getOrInsertBundleTag. |
2110 | StringMapEntry<uint32_t> *Tag; |
2111 | |
2112 | /// The index in the Use& vector where operands for this operand |
2113 | /// bundle starts. |
2114 | uint32_t Begin; |
2115 | |
2116 | /// The index in the Use& vector where operands for this operand |
2117 | /// bundle ends. |
2118 | uint32_t End; |
2119 | |
2120 | bool operator==(const BundleOpInfo &Other) const { |
2121 | return Tag == Other.Tag && Begin == Other.Begin && End == Other.End; |
2122 | } |
2123 | }; |
2124 | |
2125 | /// Simple helper function to map a BundleOpInfo to an |
2126 | /// OperandBundleUse. |
2127 | OperandBundleUse |
2128 | operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const { |
2129 | auto begin = op_begin(); |
2130 | ArrayRef<Use> Inputs(begin + BOI.Begin, begin + BOI.End); |
2131 | return OperandBundleUse(BOI.Tag, Inputs); |
2132 | } |
2133 | |
2134 | using bundle_op_iterator = BundleOpInfo *; |
2135 | using const_bundle_op_iterator = const BundleOpInfo *; |
2136 | |
2137 | /// Return the start of the list of BundleOpInfo instances associated |
2138 | /// with this OperandBundleUser. |
2139 | /// |
2140 | /// OperandBundleUser uses the descriptor area co-allocated with the host User |
2141 | /// to store some meta information about which operands are "normal" operands, |
2142 | /// and which ones belong to some operand bundle. |
2143 | /// |
2144 | /// The layout of an operand bundle user is |
2145 | /// |
2146 | /// +-----------uint32_t End-------------------------------------+ |
2147 | /// | | |
2148 | /// | +--------uint32_t Begin--------------------+ | |
2149 | /// | | | | |
2150 | /// ^ ^ v v |
2151 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2152 | /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un |
2153 | /// |------|------|----|----|----|----|----|---------|----|---------|----|----- |
2154 | /// v v ^ ^ |
2155 | /// | | | | |
2156 | /// | +--------uint32_t Begin------------+ | |
2157 | /// | | |
2158 | /// +-----------uint32_t End-----------------------------+ |
2159 | /// |
2160 | /// |
2161 | /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use |
2162 | /// list. These descriptions are installed and managed by this class, and |
2163 | /// they're all instances of OperandBundleUser<T>::BundleOpInfo. |
2164 | /// |
2165 | /// DU is an additional descriptor installed by User's 'operator new' to keep |
2166 | /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not |
2167 | /// access or modify DU in any way, it's an implementation detail private to |
2168 | /// User. |
2169 | /// |
2170 | /// The regular Use& vector for the User starts at U0. The operand bundle |
2171 | /// uses are part of the Use& vector, just like normal uses. In the diagram |
2172 | /// above, the operand bundle uses start at BOI0_U0. Each instance of |
2173 | /// BundleOpInfo has information about a contiguous set of uses constituting |
2174 | /// an operand bundle, and the total set of operand bundle uses themselves |
2175 | /// form a contiguous set of uses (i.e. there are no gaps between uses |
2176 | /// corresponding to individual operand bundles). |
2177 | /// |
2178 | /// This class does not know the location of the set of operand bundle uses |
2179 | /// within the use list -- that is decided by the User using this class via |
2180 | /// the BeginIdx argument in populateBundleOperandInfos. |
2181 | /// |
2182 | /// Currently operand bundle users with hung-off operands are not supported. |
2183 | bundle_op_iterator bundle_op_info_begin() { |
2184 | if (!hasDescriptor()) |
2185 | return nullptr; |
2186 | |
2187 | uint8_t *BytesBegin = getDescriptor().begin(); |
2188 | return reinterpret_cast<bundle_op_iterator>(BytesBegin); |
2189 | } |
2190 | |
2191 | /// Return the start of the list of BundleOpInfo instances associated |
2192 | /// with this OperandBundleUser. |
2193 | const_bundle_op_iterator bundle_op_info_begin() const { |
2194 | auto *NonConstThis = const_cast<CallBase *>(this); |
2195 | return NonConstThis->bundle_op_info_begin(); |
2196 | } |
2197 | |
2198 | /// Return the end of the list of BundleOpInfo instances associated |
2199 | /// with this OperandBundleUser. |
2200 | bundle_op_iterator bundle_op_info_end() { |
2201 | if (!hasDescriptor()) |
2202 | return nullptr; |
2203 | |
2204 | uint8_t *BytesEnd = getDescriptor().end(); |
2205 | return reinterpret_cast<bundle_op_iterator>(BytesEnd); |
2206 | } |
2207 | |
2208 | /// Return the end of the list of BundleOpInfo instances associated |
2209 | /// with this OperandBundleUser. |
2210 | const_bundle_op_iterator bundle_op_info_end() const { |
2211 | auto *NonConstThis = const_cast<CallBase *>(this); |
2212 | return NonConstThis->bundle_op_info_end(); |
2213 | } |
2214 | |
2215 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2216 | iterator_range<bundle_op_iterator> bundle_op_infos() { |
2217 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2218 | } |
2219 | |
2220 | /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end). |
2221 | iterator_range<const_bundle_op_iterator> bundle_op_infos() const { |
2222 | return make_range(bundle_op_info_begin(), bundle_op_info_end()); |
2223 | } |
2224 | |
2225 | /// Populate the BundleOpInfo instances and the Use& vector from \p |
2226 | /// Bundles. Return the op_iterator pointing to the Use& one past the last |
2227 | /// last bundle operand use. |
2228 | /// |
2229 | /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo |
2230 | /// instance allocated in this User's descriptor. |
2231 | op_iterator populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, |
2232 | const unsigned BeginIndex); |
2233 | |
2234 | public: |
2235 | /// Return the BundleOpInfo for the operand at index OpIdx. |
2236 | /// |
2237 | /// It is an error to call this with an OpIdx that does not correspond to an |
2238 | /// bundle operand. |
2239 | BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx); |
2240 | const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const { |
2241 | return const_cast<CallBase *>(this)->getBundleOpInfoForOperand(OpIdx); |
2242 | } |
2243 | |
2244 | protected: |
2245 | /// Return the total number of values used in \p Bundles. |
2246 | static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) { |
2247 | unsigned Total = 0; |
2248 | for (auto &B : Bundles) |
2249 | Total += B.input_size(); |
2250 | return Total; |
2251 | } |
2252 | |
2253 | /// @} |
2254 | // End of operand bundle API. |
2255 | |
2256 | private: |
2257 | bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; |
2258 | bool hasFnAttrOnCalledFunction(StringRef Kind) const; |
2259 | |
2260 | template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const { |
2261 | if (Attrs.hasFnAttribute(Kind)) |
2262 | return true; |
2263 | |
2264 | // Operand bundles override attributes on the called function, but don't |
2265 | // override attributes directly present on the call instruction. |
2266 | if (isFnAttrDisallowedByOpBundle(Kind)) |
2267 | return false; |
2268 | |
2269 | return hasFnAttrOnCalledFunction(Kind); |
2270 | } |
2271 | |
2272 | /// Determine whether the return value has the given attribute. Supports |
2273 | /// Attribute::AttrKind and StringRef as \p AttrKind types. |
2274 | template <typename AttrKind> bool hasRetAttrImpl(AttrKind Kind) const { |
2275 | if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) |
2276 | return true; |
2277 | |
2278 | // Look at the callee, if available. |
2279 | if (const Function *F = getCalledFunction()) |
2280 | return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); |
2281 | return false; |
2282 | } |
2283 | }; |
2284 | |
2285 | template <> |
2286 | struct OperandTraits<CallBase> : public VariadicOperandTraits<CallBase, 1> {}; |
2287 | |
2288 | 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 { ((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) { ((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); } |
2289 | |
2290 | //===----------------------------------------------------------------------===// |
2291 | // FuncletPadInst Class |
2292 | //===----------------------------------------------------------------------===// |
2293 | class FuncletPadInst : public Instruction { |
2294 | private: |
2295 | FuncletPadInst(const FuncletPadInst &CPI); |
2296 | |
2297 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2298 | ArrayRef<Value *> Args, unsigned Values, |
2299 | const Twine &NameStr, Instruction *InsertBefore); |
2300 | explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, |
2301 | ArrayRef<Value *> Args, unsigned Values, |
2302 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2303 | |
2304 | void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr); |
2305 | |
2306 | protected: |
2307 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2308 | friend class Instruction; |
2309 | friend class CatchPadInst; |
2310 | friend class CleanupPadInst; |
2311 | |
2312 | FuncletPadInst *cloneImpl() const; |
2313 | |
2314 | public: |
2315 | /// Provide fast operand accessors |
2316 | 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; |
2317 | |
2318 | /// getNumArgOperands - Return the number of funcletpad arguments. |
2319 | /// |
2320 | unsigned getNumArgOperands() const { return getNumOperands() - 1; } |
2321 | |
2322 | /// Convenience accessors |
2323 | |
2324 | /// Return the outer EH-pad this funclet is nested within. |
2325 | /// |
2326 | /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst |
2327 | /// is a CatchPadInst. |
2328 | Value *getParentPad() const { return Op<-1>(); } |
2329 | void setParentPad(Value *ParentPad) { |
2330 | assert(ParentPad)((void)0); |
2331 | Op<-1>() = ParentPad; |
2332 | } |
2333 | |
2334 | /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument. |
2335 | /// |
2336 | Value *getArgOperand(unsigned i) const { return getOperand(i); } |
2337 | void setArgOperand(unsigned i, Value *v) { setOperand(i, v); } |
2338 | |
2339 | /// arg_operands - iteration adapter for range-for loops. |
2340 | op_range arg_operands() { return op_range(op_begin(), op_end() - 1); } |
2341 | |
2342 | /// arg_operands - iteration adapter for range-for loops. |
2343 | const_op_range arg_operands() const { |
2344 | return const_op_range(op_begin(), op_end() - 1); |
2345 | } |
2346 | |
2347 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2348 | static bool classof(const Instruction *I) { return I->isFuncletPad(); } |
2349 | static bool classof(const Value *V) { |
2350 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2351 | } |
2352 | }; |
2353 | |
2354 | template <> |
2355 | struct OperandTraits<FuncletPadInst> |
2356 | : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {}; |
2357 | |
2358 | 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 { ((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) { ((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); } |
2359 | |
2360 | } // end namespace llvm |
2361 | |
2362 | #endif // LLVM_IR_INSTRTYPES_H |