Line data Source code
1 : //===- llvm/InstrTypes.h - Important Instruction subclasses -----*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file defines various meta classes of instructions that exist in the VM
11 : // representation. Specific concrete subclasses of these may be found in the
12 : // i*.h files...
13 : //
14 : //===----------------------------------------------------------------------===//
15 :
16 : #ifndef LLVM_IR_INSTRTYPES_H
17 : #define LLVM_IR_INSTRTYPES_H
18 :
19 : #include "llvm/ADT/ArrayRef.h"
20 : #include "llvm/ADT/None.h"
21 : #include "llvm/ADT/Optional.h"
22 : #include "llvm/ADT/STLExtras.h"
23 : #include "llvm/ADT/StringMap.h"
24 : #include "llvm/ADT/StringRef.h"
25 : #include "llvm/ADT/Twine.h"
26 : #include "llvm/ADT/iterator_range.h"
27 : #include "llvm/IR/Attributes.h"
28 : #include "llvm/IR/Constants.h"
29 : #include "llvm/IR/DerivedTypes.h"
30 : #include "llvm/IR/Instruction.h"
31 : #include "llvm/IR/LLVMContext.h"
32 : #include "llvm/IR/OperandTraits.h"
33 : #include "llvm/IR/Type.h"
34 : #include "llvm/IR/User.h"
35 : #include "llvm/IR/Value.h"
36 : #include "llvm/Support/Casting.h"
37 : #include "llvm/Support/ErrorHandling.h"
38 : #include <algorithm>
39 : #include <cassert>
40 : #include <cstddef>
41 : #include <cstdint>
42 : #include <iterator>
43 : #include <string>
44 : #include <vector>
45 :
46 : namespace llvm {
47 :
48 : //===----------------------------------------------------------------------===//
49 : // TerminatorInst Class
50 : //===----------------------------------------------------------------------===//
51 :
52 : /// Subclasses of this class are all able to terminate a basic
53 : /// block. Thus, these are all the flow control type of operations.
54 : ///
55 5330429 : class TerminatorInst : public Instruction {
56 : protected:
57 : TerminatorInst(Type *Ty, Instruction::TermOps iType,
58 : Use *Ops, unsigned NumOps,
59 : Instruction *InsertBefore = nullptr)
60 7405946 : : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
61 :
62 : TerminatorInst(Type *Ty, Instruction::TermOps iType,
63 : Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
64 918501 : : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
65 :
66 : public:
67 : // Methods for support type inquiry through isa, cast, and dyn_cast:
68 : static bool classof(const Instruction *I) {
69 : return I->isTerminator();
70 : }
71 : static bool classof(const Value *V) {
72 0 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
73 : }
74 : };
75 :
76 : //===----------------------------------------------------------------------===//
77 : // UnaryInstruction Class
78 : //===----------------------------------------------------------------------===//
79 :
80 13068887 : class UnaryInstruction : public Instruction {
81 : protected:
82 30499259 : UnaryInstruction(Type *Ty, unsigned iType, Value *V,
83 : Instruction *IB = nullptr)
84 60998518 : : Instruction(Ty, iType, &Op<0>(), 1, IB) {
85 : Op<0>() = V;
86 30499259 : }
87 829659 : UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
88 1659318 : : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
89 : Op<0>() = V;
90 829659 : }
91 :
92 : public:
93 : // allocate space for exactly one operand
94 : void *operator new(size_t s) {
95 31321330 : return User::operator new(s, 1);
96 : }
97 :
98 : /// Transparently provide more efficient getOperand methods.
99 : DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
100 :
101 : // Methods for support type inquiry through isa, cast, and dyn_cast:
102 : static bool classof(const Instruction *I) {
103 22 : return I->getOpcode() == Instruction::Alloca ||
104 22 : I->getOpcode() == Instruction::Load ||
105 22 : I->getOpcode() == Instruction::VAArg ||
106 44 : I->getOpcode() == Instruction::ExtractValue ||
107 8 : (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
108 : }
109 : static bool classof(const Value *V) {
110 0 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
111 : }
112 : };
113 :
114 : template <>
115 : struct OperandTraits<UnaryInstruction> :
116 : public FixedNumOperandTraits<UnaryInstruction, 1> {
117 : };
118 :
119 157768885 : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
120 :
121 : //===----------------------------------------------------------------------===//
122 : // BinaryOperator Class
123 : //===----------------------------------------------------------------------===//
124 :
125 980883 : class BinaryOperator : public Instruction {
126 : void AssertOK();
127 :
128 : protected:
129 : BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
130 : const Twine &Name, Instruction *InsertBefore);
131 : BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
132 : const Twine &Name, BasicBlock *InsertAtEnd);
133 :
134 : // Note: Instruction needs to be a friend here to call cloneImpl.
135 : friend class Instruction;
136 :
137 : BinaryOperator *cloneImpl() const;
138 :
139 : public:
140 : // allocate space for exactly two operands
141 : void *operator new(size_t s) {
142 1803520 : return User::operator new(s, 2);
143 : }
144 :
145 : /// Transparently provide more efficient getOperand methods.
146 : DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
147 :
148 : /// Construct a binary instruction, given the opcode and the two
149 : /// operands. Optionally (if InstBefore is specified) insert the instruction
150 : /// into a BasicBlock right before the specified instruction. The specified
151 : /// Instruction is allowed to be a dereferenced end iterator.
152 : ///
153 : static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
154 : const Twine &Name = Twine(),
155 : Instruction *InsertBefore = nullptr);
156 :
157 : /// Construct a binary instruction, given the opcode and the two
158 : /// operands. Also automatically insert this instruction to the end of the
159 : /// BasicBlock specified.
160 : ///
161 : static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
162 : const Twine &Name, BasicBlock *InsertAtEnd);
163 :
164 : /// These methods just forward to Create, and are useful when you
165 : /// statically know what type of instruction you're going to create. These
166 : /// helpers just save some typing.
167 : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
168 : static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
169 : const Twine &Name = "") {\
170 : return Create(Instruction::OPC, V1, V2, Name);\
171 : }
172 : #include "llvm/IR/Instruction.def"
173 : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
174 : static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
175 : const Twine &Name, BasicBlock *BB) {\
176 : return Create(Instruction::OPC, V1, V2, Name, BB);\
177 : }
178 : #include "llvm/IR/Instruction.def"
179 : #define HANDLE_BINARY_INST(N, OPC, CLASS) \
180 : static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
181 : const Twine &Name, Instruction *I) {\
182 : return Create(Instruction::OPC, V1, V2, Name, I);\
183 : }
184 : #include "llvm/IR/Instruction.def"
185 :
186 : static BinaryOperator *CreateWithCopiedFlags(BinaryOps Opc,
187 : Value *V1, Value *V2,
188 : BinaryOperator *CopyBO,
189 : const Twine &Name = "") {
190 35 : BinaryOperator *BO = Create(Opc, V1, V2, Name);
191 198 : BO->copyIRFlags(CopyBO);
192 : return BO;
193 : }
194 :
195 35 : static BinaryOperator *CreateFAddFMF(Value *V1, Value *V2,
196 : BinaryOperator *FMFSource,
197 : const Twine &Name = "") {
198 35 : return CreateWithCopiedFlags(Instruction::FAdd, V1, V2, FMFSource, Name);
199 : }
200 13 : static BinaryOperator *CreateFSubFMF(Value *V1, Value *V2,
201 : BinaryOperator *FMFSource,
202 : const Twine &Name = "") {
203 13 : return CreateWithCopiedFlags(Instruction::FSub, V1, V2, FMFSource, Name);
204 : }
205 86 : static BinaryOperator *CreateFMulFMF(Value *V1, Value *V2,
206 : BinaryOperator *FMFSource,
207 : const Twine &Name = "") {
208 86 : return CreateWithCopiedFlags(Instruction::FMul, V1, V2, FMFSource, Name);
209 : }
210 29 : static BinaryOperator *CreateFDivFMF(Value *V1, Value *V2,
211 : BinaryOperator *FMFSource,
212 : const Twine &Name = "") {
213 29 : return CreateWithCopiedFlags(Instruction::FDiv, V1, V2, FMFSource, Name);
214 : }
215 : static BinaryOperator *CreateFRemFMF(Value *V1, Value *V2,
216 : BinaryOperator *FMFSource,
217 : const Twine &Name = "") {
218 : return CreateWithCopiedFlags(Instruction::FRem, V1, V2, FMFSource, Name);
219 : }
220 0 : static BinaryOperator *CreateFNegFMF(Value *Op, BinaryOperator *FMFSource,
221 : const Twine &Name = "") {
222 0 : Value *Zero = ConstantFP::getNegativeZero(Op->getType());
223 0 : return CreateWithCopiedFlags(Instruction::FSub, Zero, Op, FMFSource);
224 : }
225 :
226 : static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
227 : const Twine &Name = "") {
228 4 : BinaryOperator *BO = Create(Opc, V1, V2, Name);
229 4 : BO->setHasNoSignedWrap(true);
230 : return BO;
231 : }
232 : static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
233 : const Twine &Name, BasicBlock *BB) {
234 20 : BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
235 20 : BO->setHasNoSignedWrap(true);
236 : return BO;
237 : }
238 : static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
239 : const Twine &Name, Instruction *I) {
240 0 : BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
241 0 : BO->setHasNoSignedWrap(true);
242 : return BO;
243 : }
244 :
245 : static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
246 : const Twine &Name = "") {
247 4 : BinaryOperator *BO = Create(Opc, V1, V2, Name);
248 4 : BO->setHasNoUnsignedWrap(true);
249 : return BO;
250 : }
251 : static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
252 : const Twine &Name, BasicBlock *BB) {
253 0 : BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
254 0 : BO->setHasNoUnsignedWrap(true);
255 : return BO;
256 : }
257 : static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
258 : const Twine &Name, Instruction *I) {
259 0 : BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
260 0 : BO->setHasNoUnsignedWrap(true);
261 : return BO;
262 : }
263 :
264 : static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
265 : const Twine &Name = "") {
266 15195 : BinaryOperator *BO = Create(Opc, V1, V2, Name);
267 15195 : BO->setIsExact(true);
268 : return BO;
269 : }
270 : static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
271 : const Twine &Name, BasicBlock *BB) {
272 : BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
273 : BO->setIsExact(true);
274 : return BO;
275 : }
276 : static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
277 : const Twine &Name, Instruction *I) {
278 2 : BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
279 2 : BO->setIsExact(true);
280 : return BO;
281 : }
282 :
283 : #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
284 : static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \
285 : const Twine &Name = "") { \
286 : return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
287 : } \
288 : static BinaryOperator *Create##NUWNSWEXACT##OPC( \
289 : Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
290 : return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
291 : } \
292 : static BinaryOperator *Create##NUWNSWEXACT##OPC( \
293 : Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
294 : return Create##NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
295 : }
296 :
297 : DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
298 : DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
299 : DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
300 : DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
301 : DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
302 : DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
303 4 : DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
304 4 : DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
305 :
306 14557 : DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
307 34 : DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
308 601 : DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
309 3 : DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
310 :
311 : #undef DEFINE_HELPERS
312 :
313 : /// Helper functions to construct and inspect unary operations (NEG and NOT)
314 : /// via binary operators SUB and XOR:
315 : ///
316 : /// Create the NEG and NOT instructions out of SUB and XOR instructions.
317 : ///
318 : static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
319 : Instruction *InsertBefore = nullptr);
320 : static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
321 : BasicBlock *InsertAtEnd);
322 : static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
323 : Instruction *InsertBefore = nullptr);
324 : static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
325 : BasicBlock *InsertAtEnd);
326 : static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
327 : Instruction *InsertBefore = nullptr);
328 : static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
329 : BasicBlock *InsertAtEnd);
330 : static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
331 : Instruction *InsertBefore = nullptr);
332 : static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
333 : BasicBlock *InsertAtEnd);
334 : static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
335 : Instruction *InsertBefore = nullptr);
336 : static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
337 : BasicBlock *InsertAtEnd);
338 :
339 : /// Check if the given Value is a NEG, FNeg, or NOT instruction.
340 : ///
341 : static bool isNeg(const Value *V);
342 : static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
343 : static bool isNot(const Value *V);
344 :
345 : /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
346 : /// operation implemented via Sub, FSub, or Xor.
347 : ///
348 : static const Value *getNegArgument(const Value *BinOp);
349 : static Value *getNegArgument( Value *BinOp);
350 : static const Value *getFNegArgument(const Value *BinOp);
351 : static Value *getFNegArgument( Value *BinOp);
352 : static const Value *getNotArgument(const Value *BinOp);
353 : static Value *getNotArgument( Value *BinOp);
354 :
355 : BinaryOps getOpcode() const {
356 : return static_cast<BinaryOps>(Instruction::getOpcode());
357 : }
358 :
359 : /// Exchange the two operands to this instruction.
360 : /// This instruction is safe to use on any binary instruction and
361 : /// does not modify the semantics of the instruction. If the instruction
362 : /// cannot be reversed (ie, it's a Div), then return true.
363 : ///
364 : bool swapOperands();
365 :
366 : // Methods for support type inquiry through isa, cast, and dyn_cast:
367 : static bool classof(const Instruction *I) {
368 : return I->isBinaryOp();
369 : }
370 : static bool classof(const Value *V) {
371 55394537 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
372 : }
373 : };
374 :
375 : template <>
376 : struct OperandTraits<BinaryOperator> :
377 : public FixedNumOperandTraits<BinaryOperator, 2> {
378 : };
379 :
380 131158175 : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
381 :
382 : //===----------------------------------------------------------------------===//
383 : // CastInst Class
384 : //===----------------------------------------------------------------------===//
385 :
386 : /// This is the base class for all instructions that perform data
387 : /// casts. It is simply provided so that instruction category testing
388 : /// can be performed with code like:
389 : ///
390 : /// if (isa<CastInst>(Instr)) { ... }
391 : /// Base class of casting instructions.
392 8 : class CastInst : public UnaryInstruction {
393 : protected:
394 : /// Constructor with insert-before-instruction semantics for subclasses
395 : CastInst(Type *Ty, unsigned iType, Value *S,
396 : const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
397 5615682 : : UnaryInstruction(Ty, iType, S, InsertBefore) {
398 5615682 : setName(NameStr);
399 : }
400 : /// Constructor with insert-at-end-of-block semantics for subclasses
401 : CastInst(Type *Ty, unsigned iType, Value *S,
402 : const Twine &NameStr, BasicBlock *InsertAtEnd)
403 829607 : : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
404 829607 : setName(NameStr);
405 : }
406 :
407 : public:
408 : /// Provides a way to construct any of the CastInst subclasses using an
409 : /// opcode instead of the subclass's constructor. The opcode must be in the
410 : /// CastOps category (Instruction::isCast(opcode) returns true). This
411 : /// constructor has insert-before-instruction semantics to automatically
412 : /// insert the new CastInst before InsertBefore (if it is non-null).
413 : /// Construct any of the CastInst subclasses
414 : static CastInst *Create(
415 : Instruction::CastOps, ///< The opcode of the cast instruction
416 : Value *S, ///< The value to be casted (operand 0)
417 : Type *Ty, ///< The type to which cast should be made
418 : const Twine &Name = "", ///< Name for the instruction
419 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
420 : );
421 : /// Provides a way to construct any of the CastInst subclasses using an
422 : /// opcode instead of the subclass's constructor. The opcode must be in the
423 : /// CastOps category. This constructor has insert-at-end-of-block semantics
424 : /// to automatically insert the new CastInst at the end of InsertAtEnd (if
425 : /// its non-null).
426 : /// Construct any of the CastInst subclasses
427 : static CastInst *Create(
428 : Instruction::CastOps, ///< The opcode for the cast instruction
429 : Value *S, ///< The value to be casted (operand 0)
430 : Type *Ty, ///< The type to which operand is casted
431 : const Twine &Name, ///< The name for the instruction
432 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
433 : );
434 :
435 : /// Create a ZExt or BitCast cast instruction
436 : static CastInst *CreateZExtOrBitCast(
437 : Value *S, ///< The value to be casted (operand 0)
438 : Type *Ty, ///< The type to which cast should be made
439 : const Twine &Name = "", ///< Name for the instruction
440 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
441 : );
442 :
443 : /// Create a ZExt or BitCast cast instruction
444 : static CastInst *CreateZExtOrBitCast(
445 : Value *S, ///< The value to be casted (operand 0)
446 : Type *Ty, ///< The type to which operand is casted
447 : const Twine &Name, ///< The name for the instruction
448 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
449 : );
450 :
451 : /// Create a SExt or BitCast cast instruction
452 : static CastInst *CreateSExtOrBitCast(
453 : Value *S, ///< The value to be casted (operand 0)
454 : Type *Ty, ///< The type to which cast should be made
455 : const Twine &Name = "", ///< Name for the instruction
456 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
457 : );
458 :
459 : /// Create a SExt or BitCast cast instruction
460 : static CastInst *CreateSExtOrBitCast(
461 : Value *S, ///< The value to be casted (operand 0)
462 : Type *Ty, ///< The type to which operand is casted
463 : const Twine &Name, ///< The name for the instruction
464 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
465 : );
466 :
467 : /// Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
468 : static CastInst *CreatePointerCast(
469 : Value *S, ///< The pointer value to be casted (operand 0)
470 : Type *Ty, ///< The type to which operand is casted
471 : const Twine &Name, ///< The name for the instruction
472 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
473 : );
474 :
475 : /// Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
476 : static CastInst *CreatePointerCast(
477 : Value *S, ///< The pointer value to be casted (operand 0)
478 : Type *Ty, ///< The type to which cast should be made
479 : const Twine &Name = "", ///< Name for the instruction
480 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
481 : );
482 :
483 : /// Create a BitCast or an AddrSpaceCast cast instruction.
484 : static CastInst *CreatePointerBitCastOrAddrSpaceCast(
485 : Value *S, ///< The pointer value to be casted (operand 0)
486 : Type *Ty, ///< The type to which operand is casted
487 : const Twine &Name, ///< The name for the instruction
488 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
489 : );
490 :
491 : /// Create a BitCast or an AddrSpaceCast cast instruction.
492 : static CastInst *CreatePointerBitCastOrAddrSpaceCast(
493 : Value *S, ///< The pointer value to be casted (operand 0)
494 : Type *Ty, ///< The type to which cast should be made
495 : const Twine &Name = "", ///< Name for the instruction
496 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
497 : );
498 :
499 : /// Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
500 : ///
501 : /// If the value is a pointer type and the destination an integer type,
502 : /// creates a PtrToInt cast. If the value is an integer type and the
503 : /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
504 : /// a bitcast.
505 : static CastInst *CreateBitOrPointerCast(
506 : Value *S, ///< The pointer value to be casted (operand 0)
507 : Type *Ty, ///< The type to which cast should be made
508 : const Twine &Name = "", ///< Name for the instruction
509 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
510 : );
511 :
512 : /// Create a ZExt, BitCast, or Trunc for int -> int casts.
513 : static CastInst *CreateIntegerCast(
514 : Value *S, ///< The pointer value to be casted (operand 0)
515 : Type *Ty, ///< The type to which cast should be made
516 : bool isSigned, ///< Whether to regard S as signed or not
517 : const Twine &Name = "", ///< Name for the instruction
518 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
519 : );
520 :
521 : /// Create a ZExt, BitCast, or Trunc for int -> int casts.
522 : static CastInst *CreateIntegerCast(
523 : Value *S, ///< The integer value to be casted (operand 0)
524 : Type *Ty, ///< The integer type to which operand is casted
525 : bool isSigned, ///< Whether to regard S as signed or not
526 : const Twine &Name, ///< The name for the instruction
527 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
528 : );
529 :
530 : /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
531 : static CastInst *CreateFPCast(
532 : Value *S, ///< The floating point value to be casted
533 : Type *Ty, ///< The floating point type to cast to
534 : const Twine &Name = "", ///< Name for the instruction
535 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
536 : );
537 :
538 : /// Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
539 : static CastInst *CreateFPCast(
540 : Value *S, ///< The floating point value to be casted
541 : Type *Ty, ///< The floating point type to cast to
542 : const Twine &Name, ///< The name for the instruction
543 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
544 : );
545 :
546 : /// Create a Trunc or BitCast cast instruction
547 : static CastInst *CreateTruncOrBitCast(
548 : Value *S, ///< The value to be casted (operand 0)
549 : Type *Ty, ///< The type to which cast should be made
550 : const Twine &Name = "", ///< Name for the instruction
551 : Instruction *InsertBefore = nullptr ///< Place to insert the instruction
552 : );
553 :
554 : /// Create a Trunc or BitCast cast instruction
555 : static CastInst *CreateTruncOrBitCast(
556 : Value *S, ///< The value to be casted (operand 0)
557 : Type *Ty, ///< The type to which operand is casted
558 : const Twine &Name, ///< The name for the instruction
559 : BasicBlock *InsertAtEnd ///< The block to insert the instruction into
560 : );
561 :
562 : /// Check whether it is valid to call getCastOpcode for these types.
563 : static bool isCastable(
564 : Type *SrcTy, ///< The Type from which the value should be cast.
565 : Type *DestTy ///< The Type to which the value should be cast.
566 : );
567 :
568 : /// Check whether a bitcast between these types is valid
569 : static bool isBitCastable(
570 : Type *SrcTy, ///< The Type from which the value should be cast.
571 : Type *DestTy ///< The Type to which the value should be cast.
572 : );
573 :
574 : /// Check whether a bitcast, inttoptr, or ptrtoint cast between these
575 : /// types is valid and a no-op.
576 : ///
577 : /// This ensures that any pointer<->integer cast has enough bits in the
578 : /// integer and any other cast is a bitcast.
579 : static bool isBitOrNoopPointerCastable(
580 : Type *SrcTy, ///< The Type from which the value should be cast.
581 : Type *DestTy, ///< The Type to which the value should be cast.
582 : const DataLayout &DL);
583 :
584 : /// Returns the opcode necessary to cast Val into Ty using usual casting
585 : /// rules.
586 : /// Infer the opcode for cast operand and type
587 : static Instruction::CastOps getCastOpcode(
588 : const Value *Val, ///< The value to cast
589 : bool SrcIsSigned, ///< Whether to treat the source as signed
590 : Type *Ty, ///< The Type to which the value should be casted
591 : bool DstIsSigned ///< Whether to treate the dest. as signed
592 : );
593 :
594 : /// There are several places where we need to know if a cast instruction
595 : /// only deals with integer source and destination types. To simplify that
596 : /// logic, this method is provided.
597 : /// @returns true iff the cast has only integral typed operand and dest type.
598 : /// Determine if this is an integer-only cast.
599 : bool isIntegerCast() const;
600 :
601 : /// A lossless cast is one that does not alter the basic value. It implies
602 : /// a no-op cast but is more stringent, preventing things like int->float,
603 : /// long->double, or int->ptr.
604 : /// @returns true iff the cast is lossless.
605 : /// Determine if this is a lossless cast.
606 : bool isLosslessCast() const;
607 :
608 : /// A no-op cast is one that can be effected without changing any bits.
609 : /// It implies that the source and destination types are the same size. The
610 : /// DataLayout argument is to determine the pointer size when examining casts
611 : /// involving Integer and Pointer types. They are no-op casts if the integer
612 : /// is the same size as the pointer. However, pointer size varies with
613 : /// platform.
614 : /// Determine if the described cast is a no-op cast.
615 : static bool isNoopCast(
616 : Instruction::CastOps Opcode, ///< Opcode of cast
617 : Type *SrcTy, ///< SrcTy of cast
618 : Type *DstTy, ///< DstTy of cast
619 : const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
620 : );
621 :
622 : /// Determine if this cast is a no-op cast.
623 : ///
624 : /// \param DL is the DataLayout to determine pointer size.
625 : bool isNoopCast(const DataLayout &DL) const;
626 :
627 : /// Determine how a pair of casts can be eliminated, if they can be at all.
628 : /// This is a helper function for both CastInst and ConstantExpr.
629 : /// @returns 0 if the CastInst pair can't be eliminated, otherwise
630 : /// returns Instruction::CastOps value for a cast that can replace
631 : /// the pair, casting SrcTy to DstTy.
632 : /// Determine if a cast pair is eliminable
633 : static unsigned isEliminableCastPair(
634 : Instruction::CastOps firstOpcode, ///< Opcode of first cast
635 : Instruction::CastOps secondOpcode, ///< Opcode of second cast
636 : Type *SrcTy, ///< SrcTy of 1st cast
637 : Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
638 : Type *DstTy, ///< DstTy of 2nd cast
639 : Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
640 : Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
641 : Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
642 : );
643 :
644 : /// Return the opcode of this CastInst
645 : Instruction::CastOps getOpcode() const {
646 : return Instruction::CastOps(Instruction::getOpcode());
647 : }
648 :
649 : /// Return the source type, as a convenience
650 80981 : Type* getSrcTy() const { return getOperand(0)->getType(); }
651 : /// Return the destination type, as a convenience
652 17251 : Type* getDestTy() const { return getType(); }
653 :
654 : /// This method can be used to determine if a cast from S to DstTy using
655 : /// Opcode op is valid or not.
656 : /// @returns true iff the proposed cast is valid.
657 : /// Determine if a cast is valid without creating one.
658 : static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
659 :
660 : /// Methods for support type inquiry through isa, cast, and dyn_cast:
661 : static bool classof(const Instruction *I) {
662 : return I->isCast();
663 : }
664 : static bool classof(const Value *V) {
665 30296558 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
666 : }
667 : };
668 :
669 : //===----------------------------------------------------------------------===//
670 : // CmpInst Class
671 : //===----------------------------------------------------------------------===//
672 :
673 : /// This class is the base class for the comparison instructions.
674 : /// Abstract base class of comparison instructions.
675 385786 : class CmpInst : public Instruction {
676 : public:
677 : /// This enumeration lists the possible predicates for CmpInst subclasses.
678 : /// Values in the range 0-31 are reserved for FCmpInst, while values in the
679 : /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
680 : /// predicate values are not overlapping between the classes.
681 : ///
682 : /// Some passes (e.g. InstCombine) depend on the bit-wise characteristics of
683 : /// FCMP_* values. Changing the bit patterns requires a potential change to
684 : /// those passes.
685 : enum Predicate {
686 : // Opcode U L G E Intuitive operation
687 : FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
688 : FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
689 : FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
690 : FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
691 : FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
692 : FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
693 : FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
694 : FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
695 : FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
696 : FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
697 : FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
698 : FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
699 : FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
700 : FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
701 : FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
702 : FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
703 : FIRST_FCMP_PREDICATE = FCMP_FALSE,
704 : LAST_FCMP_PREDICATE = FCMP_TRUE,
705 : BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
706 : ICMP_EQ = 32, ///< equal
707 : ICMP_NE = 33, ///< not equal
708 : ICMP_UGT = 34, ///< unsigned greater than
709 : ICMP_UGE = 35, ///< unsigned greater or equal
710 : ICMP_ULT = 36, ///< unsigned less than
711 : ICMP_ULE = 37, ///< unsigned less or equal
712 : ICMP_SGT = 38, ///< signed greater than
713 : ICMP_SGE = 39, ///< signed greater or equal
714 : ICMP_SLT = 40, ///< signed less than
715 : ICMP_SLE = 41, ///< signed less or equal
716 : FIRST_ICMP_PREDICATE = ICMP_EQ,
717 : LAST_ICMP_PREDICATE = ICMP_SLE,
718 : BAD_ICMP_PREDICATE = ICMP_SLE + 1
719 : };
720 :
721 : protected:
722 : CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
723 : Value *LHS, Value *RHS, const Twine &Name = "",
724 : Instruction *InsertBefore = nullptr);
725 :
726 : CmpInst(Type *ty, Instruction::OtherOps op, Predicate pred,
727 : Value *LHS, Value *RHS, const Twine &Name,
728 : BasicBlock *InsertAtEnd);
729 :
730 : public:
731 : // allocate space for exactly two operands
732 : void *operator new(size_t s) {
733 904704 : return User::operator new(s, 2);
734 : }
735 :
736 : /// Construct a compare instruction, given the opcode, the predicate and
737 : /// the two operands. Optionally (if InstBefore is specified) insert the
738 : /// instruction into a BasicBlock right before the specified instruction.
739 : /// The specified Instruction is allowed to be a dereferenced end iterator.
740 : /// Create a CmpInst
741 : static CmpInst *Create(OtherOps Op,
742 : Predicate predicate, Value *S1,
743 : Value *S2, const Twine &Name = "",
744 : Instruction *InsertBefore = nullptr);
745 :
746 : /// Construct a compare instruction, given the opcode, the predicate and the
747 : /// two operands. Also automatically insert this instruction to the end of
748 : /// the BasicBlock specified.
749 : /// Create a CmpInst
750 : static CmpInst *Create(OtherOps Op, Predicate predicate, Value *S1,
751 : Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
752 :
753 : /// Get the opcode casted to the right type
754 : OtherOps getOpcode() const {
755 : return static_cast<OtherOps>(Instruction::getOpcode());
756 : }
757 :
758 : /// Return the predicate for this instruction.
759 : Predicate getPredicate() const {
760 : return Predicate(getSubclassDataFromInstruction());
761 : }
762 :
763 : /// Set the predicate for this instruction to the specified value.
764 920287 : void setPredicate(Predicate P) { setInstructionSubclassData(P); }
765 :
766 : static bool isFPPredicate(Predicate P) {
767 : return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
768 : }
769 :
770 : static bool isIntPredicate(Predicate P) {
771 441357 : return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
772 : }
773 :
774 : static StringRef getPredicateName(Predicate P);
775 :
776 : bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
777 : bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
778 :
779 : /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
780 : /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
781 : /// @returns the inverse predicate for the instruction's current predicate.
782 : /// Return the inverse of the instruction's predicate.
783 : Predicate getInversePredicate() const {
784 405819 : return getInversePredicate(getPredicate());
785 : }
786 :
787 : /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
788 : /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
789 : /// @returns the inverse predicate for predicate provided in \p pred.
790 : /// Return the inverse of a given predicate
791 : static Predicate getInversePredicate(Predicate pred);
792 :
793 : /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
794 : /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
795 : /// @returns the predicate that would be the result of exchanging the two
796 : /// operands of the CmpInst instruction without changing the result
797 : /// produced.
798 : /// Return the predicate as if the operands were swapped
799 : Predicate getSwappedPredicate() const {
800 394198 : return getSwappedPredicate(getPredicate());
801 : }
802 :
803 : /// This is a static version that you can use without an instruction
804 : /// available.
805 : /// Return the predicate as if the operands were swapped.
806 : static Predicate getSwappedPredicate(Predicate pred);
807 :
808 : /// For predicate of kind "is X or equal to 0" returns the predicate "is X".
809 : /// For predicate of kind "is X" returns the predicate "is X or equal to 0".
810 : /// does not support other kind of predicates.
811 : /// @returns the predicate that does not contains is equal to zero if
812 : /// it had and vice versa.
813 : /// Return the flipped strictness of predicate
814 : Predicate getFlippedStrictnessPredicate() const {
815 : return getFlippedStrictnessPredicate(getPredicate());
816 : }
817 :
818 : /// This is a static version that you can use without an instruction
819 : /// available.
820 : /// Return the flipped strictness of predicate
821 : static Predicate getFlippedStrictnessPredicate(Predicate pred);
822 :
823 : /// For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
824 : /// Returns the non-strict version of strict comparisons.
825 : Predicate getNonStrictPredicate() const {
826 : return getNonStrictPredicate(getPredicate());
827 : }
828 :
829 : /// This is a static version that you can use without an instruction
830 : /// available.
831 : /// @returns the non-strict version of comparison provided in \p pred.
832 : /// If \p pred is not a strict comparison predicate, returns \p pred.
833 : /// Returns the non-strict version of strict comparisons.
834 : static Predicate getNonStrictPredicate(Predicate pred);
835 :
836 : /// Provide more efficient getOperand methods.
837 : DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
838 :
839 : /// This is just a convenience that dispatches to the subclasses.
840 : /// Swap the operands and adjust predicate accordingly to retain
841 : /// the same comparison.
842 : void swapOperands();
843 :
844 : /// This is just a convenience that dispatches to the subclasses.
845 : /// Determine if this CmpInst is commutative.
846 : bool isCommutative() const;
847 :
848 : /// This is just a convenience that dispatches to the subclasses.
849 : /// Determine if this is an equals/not equals predicate.
850 : bool isEquality() const;
851 :
852 : /// @returns true if the comparison is signed, false otherwise.
853 : /// Determine if this instruction is using a signed comparison.
854 : bool isSigned() const {
855 2057351 : return isSigned(getPredicate());
856 : }
857 :
858 : /// @returns true if the comparison is unsigned, false otherwise.
859 : /// Determine if this instruction is using an unsigned comparison.
860 : bool isUnsigned() const {
861 770598 : return isUnsigned(getPredicate());
862 : }
863 :
864 : /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
865 : /// @returns the signed version of the unsigned predicate pred.
866 : /// return the signed version of a predicate
867 : static Predicate getSignedPredicate(Predicate pred);
868 :
869 : /// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
870 : /// @returns the signed version of the predicate for this instruction (which
871 : /// has to be an unsigned predicate).
872 : /// return the signed version of a predicate
873 : Predicate getSignedPredicate() {
874 : return getSignedPredicate(getPredicate());
875 : }
876 :
877 : /// This is just a convenience.
878 : /// Determine if this is true when both operands are the same.
879 : bool isTrueWhenEqual() const {
880 542 : return isTrueWhenEqual(getPredicate());
881 : }
882 :
883 : /// This is just a convenience.
884 : /// Determine if this is false when both operands are the same.
885 : bool isFalseWhenEqual() const {
886 12 : return isFalseWhenEqual(getPredicate());
887 : }
888 :
889 : /// @returns true if the predicate is unsigned, false otherwise.
890 : /// Determine if the predicate is an unsigned operation.
891 : static bool isUnsigned(Predicate predicate);
892 :
893 : /// @returns true if the predicate is signed, false otherwise.
894 : /// Determine if the predicate is an signed operation.
895 : static bool isSigned(Predicate predicate);
896 :
897 : /// Determine if the predicate is an ordered operation.
898 : static bool isOrdered(Predicate predicate);
899 :
900 : /// Determine if the predicate is an unordered operation.
901 : static bool isUnordered(Predicate predicate);
902 :
903 : /// Determine if the predicate is true when comparing a value with itself.
904 : static bool isTrueWhenEqual(Predicate predicate);
905 :
906 : /// Determine if the predicate is false when comparing a value with itself.
907 : static bool isFalseWhenEqual(Predicate predicate);
908 :
909 : /// Determine if Pred1 implies Pred2 is true when two compares have matching
910 : /// operands.
911 : static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2);
912 :
913 : /// Determine if Pred1 implies Pred2 is false when two compares have matching
914 : /// operands.
915 : static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2);
916 :
917 : /// Methods for support type inquiry through isa, cast, and dyn_cast:
918 : static bool classof(const Instruction *I) {
919 72884181 : return I->getOpcode() == Instruction::ICmp ||
920 : I->getOpcode() == Instruction::FCmp;
921 : }
922 : static bool classof(const Value *V) {
923 37888355 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
924 : }
925 :
926 : /// Create a result type for fcmp/icmp
927 9702883 : static Type* makeCmpResultType(Type* opnd_type) {
928 : if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
929 70936 : return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
930 141872 : vt->getNumElements());
931 : }
932 9631947 : return Type::getInt1Ty(opnd_type->getContext());
933 : }
934 :
935 : private:
936 : // Shadow Value::setValueSubclassData with a private forwarding method so that
937 : // subclasses cannot accidentally use it.
938 : void setValueSubclassData(unsigned short D) {
939 : Value::setValueSubclassData(D);
940 : }
941 : };
942 :
943 : // FIXME: these are redundant if CmpInst < BinaryOperator
944 : template <>
945 : struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
946 : };
947 :
948 21370789 : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
949 :
950 : //===----------------------------------------------------------------------===//
951 : // FuncletPadInst Class
952 : //===----------------------------------------------------------------------===//
953 1098 : class FuncletPadInst : public Instruction {
954 : private:
955 : FuncletPadInst(const FuncletPadInst &CPI);
956 :
957 : explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
958 : ArrayRef<Value *> Args, unsigned Values,
959 : const Twine &NameStr, Instruction *InsertBefore);
960 : explicit FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
961 : ArrayRef<Value *> Args, unsigned Values,
962 : const Twine &NameStr, BasicBlock *InsertAtEnd);
963 :
964 : void init(Value *ParentPad, ArrayRef<Value *> Args, const Twine &NameStr);
965 :
966 : protected:
967 : // Note: Instruction needs to be a friend here to call cloneImpl.
968 : friend class Instruction;
969 : friend class CatchPadInst;
970 : friend class CleanupPadInst;
971 :
972 : FuncletPadInst *cloneImpl() const;
973 :
974 : public:
975 : /// Provide fast operand accessors
976 : DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
977 :
978 : /// getNumArgOperands - Return the number of funcletpad arguments.
979 : ///
980 819 : unsigned getNumArgOperands() const { return getNumOperands() - 1; }
981 :
982 : /// Convenience accessors
983 :
984 : /// Return the outer EH-pad this funclet is nested within.
985 : ///
986 : /// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
987 : /// is a CatchPadInst.
988 6066 : Value *getParentPad() const { return Op<-1>(); }
989 : void setParentPad(Value *ParentPad) {
990 : assert(ParentPad);
991 : Op<-1>() = ParentPad;
992 : }
993 :
994 : /// getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
995 : ///
996 : Value *getArgOperand(unsigned i) const { return getOperand(i); }
997 : void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
998 :
999 : /// arg_operands - iteration adapter for range-for loops.
1000 : op_range arg_operands() { return op_range(op_begin(), op_end() - 1); }
1001 :
1002 : /// arg_operands - iteration adapter for range-for loops.
1003 : const_op_range arg_operands() const {
1004 : return const_op_range(op_begin(), op_end() - 1);
1005 : }
1006 :
1007 : // Methods for support type inquiry through isa, cast, and dyn_cast:
1008 : static bool classof(const Instruction *I) { return I->isFuncletPad(); }
1009 : static bool classof(const Value *V) {
1010 5532 : return isa<Instruction>(V) && classof(cast<Instruction>(V));
1011 : }
1012 : };
1013 :
1014 : template <>
1015 : struct OperandTraits<FuncletPadInst>
1016 : : public VariadicOperandTraits<FuncletPadInst, /*MINARITY=*/1> {};
1017 :
1018 873 : DEFINE_TRANSPARENT_OPERAND_ACCESSORS(FuncletPadInst, Value)
1019 :
1020 : /// A lightweight accessor for an operand bundle meant to be passed
1021 : /// around by value.
1022 : struct OperandBundleUse {
1023 : ArrayRef<Use> Inputs;
1024 :
1025 : OperandBundleUse() = default;
1026 : explicit OperandBundleUse(StringMapEntry<uint32_t> *Tag, ArrayRef<Use> Inputs)
1027 8092 : : Inputs(Inputs), Tag(Tag) {}
1028 :
1029 : /// Return true if the operand at index \p Idx in this operand bundle
1030 : /// has the attribute A.
1031 : bool operandHasAttr(unsigned Idx, Attribute::AttrKind A) const {
1032 106 : if (isDeoptOperandBundle())
1033 77 : if (A == Attribute::ReadOnly || A == Attribute::NoCapture)
1034 132 : return Inputs[Idx]->getType()->isPointerTy();
1035 :
1036 : // Conservative answer: no operands have any attributes.
1037 : return false;
1038 : }
1039 :
1040 : /// Return the tag of this operand bundle as a string.
1041 0 : StringRef getTagName() const {
1042 0 : return Tag->getKey();
1043 : }
1044 :
1045 : /// Return the tag of this operand bundle as an integer.
1046 : ///
1047 : /// Operand bundle tags are interned by LLVMContextImpl::getOrInsertBundleTag,
1048 : /// and this function returns the unique integer getOrInsertBundleTag
1049 : /// associated the tag of this operand bundle to.
1050 0 : uint32_t getTagID() const {
1051 4465 : return Tag->getValue();
1052 : }
1053 :
1054 : /// Return true if this is a "deopt" operand bundle.
1055 : bool isDeoptOperandBundle() const {
1056 : return getTagID() == LLVMContext::OB_deopt;
1057 : }
1058 :
1059 : /// Return true if this is a "funclet" operand bundle.
1060 : bool isFuncletOperandBundle() const {
1061 : return getTagID() == LLVMContext::OB_funclet;
1062 : }
1063 :
1064 : private:
1065 : /// Pointer to an entry in LLVMContextImpl::getOrInsertBundleTag.
1066 : StringMapEntry<uint32_t> *Tag;
1067 : };
1068 :
1069 : /// A container for an operand bundle being viewed as a set of values
1070 : /// rather than a set of uses.
1071 : ///
1072 : /// Unlike OperandBundleUse, OperandBundleDefT owns the memory it carries, and
1073 : /// so it is possible to create and pass around "self-contained" instances of
1074 : /// OperandBundleDef and ConstOperandBundleDef.
1075 80 : template <typename InputTy> class OperandBundleDefT {
1076 : std::string Tag;
1077 : std::vector<InputTy> Inputs;
1078 :
1079 : public:
1080 1633 : explicit OperandBundleDefT(std::string Tag, std::vector<InputTy> Inputs)
1081 : : Tag(std::move(Tag)), Inputs(std::move(Inputs)) {}
1082 352 : explicit OperandBundleDefT(std::string Tag, ArrayRef<InputTy> Inputs)
1083 352 : : Tag(std::move(Tag)), Inputs(Inputs) {}
1084 :
1085 624 : explicit OperandBundleDefT(const OperandBundleUse &OBU) {
1086 624 : Tag = OBU.getTagName();
1087 312 : Inputs.insert(Inputs.end(), OBU.Inputs.begin(), OBU.Inputs.end());
1088 312 : }
1089 :
1090 : ArrayRef<InputTy> inputs() const { return Inputs; }
1091 :
1092 : using input_iterator = typename std::vector<InputTy>::const_iterator;
1093 :
1094 5402 : size_t input_size() const { return Inputs.size(); }
1095 2048 : input_iterator input_begin() const { return Inputs.begin(); }
1096 2048 : input_iterator input_end() const { return Inputs.end(); }
1097 :
1098 : StringRef getTag() const { return Tag; }
1099 : };
1100 :
1101 : using OperandBundleDef = OperandBundleDefT<Value *>;
1102 : using ConstOperandBundleDef = OperandBundleDefT<const Value *>;
1103 :
1104 : /// A mixin to add operand bundle functionality to llvm instruction
1105 : /// classes.
1106 : ///
1107 : /// OperandBundleUser uses the descriptor area co-allocated with the host User
1108 : /// to store some meta information about which operands are "normal" operands,
1109 : /// and which ones belong to some operand bundle.
1110 : ///
1111 : /// The layout of an operand bundle user is
1112 : ///
1113 : /// +-----------uint32_t End-------------------------------------+
1114 : /// | |
1115 : /// | +--------uint32_t Begin--------------------+ |
1116 : /// | | | |
1117 : /// ^ ^ v v
1118 : /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1119 : /// | BOI0 | BOI1 | .. | DU | U0 | U1 | .. | BOI0_U0 | .. | BOI1_U0 | .. | Un
1120 : /// |------|------|----|----|----|----|----|---------|----|---------|----|-----
1121 : /// v v ^ ^
1122 : /// | | | |
1123 : /// | +--------uint32_t Begin------------+ |
1124 : /// | |
1125 : /// +-----------uint32_t End-----------------------------+
1126 : ///
1127 : ///
1128 : /// BOI0, BOI1 ... are descriptions of operand bundles in this User's use list.
1129 : /// These descriptions are installed and managed by this class, and they're all
1130 : /// instances of OperandBundleUser<T>::BundleOpInfo.
1131 : ///
1132 : /// DU is an additional descriptor installed by User's 'operator new' to keep
1133 : /// track of the 'BOI0 ... BOIN' co-allocation. OperandBundleUser does not
1134 : /// access or modify DU in any way, it's an implementation detail private to
1135 : /// User.
1136 : ///
1137 : /// The regular Use& vector for the User starts at U0. The operand bundle uses
1138 : /// are part of the Use& vector, just like normal uses. In the diagram above,
1139 : /// the operand bundle uses start at BOI0_U0. Each instance of BundleOpInfo has
1140 : /// information about a contiguous set of uses constituting an operand bundle,
1141 : /// and the total set of operand bundle uses themselves form a contiguous set of
1142 : /// uses (i.e. there are no gaps between uses corresponding to individual
1143 : /// operand bundles).
1144 : ///
1145 : /// This class does not know the location of the set of operand bundle uses
1146 : /// within the use list -- that is decided by the User using this class via the
1147 : /// BeginIdx argument in populateBundleOperandInfos.
1148 : ///
1149 : /// Currently operand bundle users with hung-off operands are not supported.
1150 : template <typename InstrTy, typename OpIteratorTy> class OperandBundleUser {
1151 : public:
1152 : /// Return the number of operand bundles associated with this User.
1153 140796194 : unsigned getNumOperandBundles() const {
1154 140796194 : return std::distance(bundle_op_info_begin(), bundle_op_info_end());
1155 : }
1156 14893898 :
1157 14893898 : /// Return true if this User has any operand bundles.
1158 683403 : bool hasOperandBundles() const { return getNumOperandBundles() != 0; }
1159 122409769 :
1160 122409769 : /// Return the index of the first bundle operand in the Use array.
1161 : unsigned getBundleOperandsStartIndex() const {
1162 : assert(hasOperandBundles() && "Don't call otherwise!");
1163 50 : return bundle_op_info_begin()->Begin;
1164 109478107 : }
1165 :
1166 : /// Return the index of the last bundle operand in the Use array.
1167 : unsigned getBundleOperandsEndIndex() const {
1168 : assert(hasOperandBundles() && "Don't call otherwise!");
1169 17327 : return bundle_op_info_end()[-1].End;
1170 : }
1171 :
1172 : /// Return true if the operand at index \p Idx is a bundle operand.
1173 : bool isBundleOperand(unsigned Idx) const {
1174 : return hasOperandBundles() && Idx >= getBundleOperandsStartIndex() &&
1175 17431 : Idx < getBundleOperandsEndIndex();
1176 : }
1177 :
1178 : /// Return the total number operands (not operand bundles) used by
1179 185504 : /// every operand bundle in this OperandBundleUser.
1180 220447 : unsigned getNumTotalBundleOperands() const {
1181 220085 : if (!hasOperandBundles())
1182 : return 0;
1183 77152 :
1184 77270 : unsigned Begin = getBundleOperandsStartIndex();
1185 77152 : unsigned End = getBundleOperandsEndIndex();
1186 34461363 :
1187 34569715 : assert(Begin <= End && "Should be!");
1188 108644 : return End - Begin;
1189 108352 : }
1190 :
1191 : /// Return the operand bundle at a specific index.
1192 272 : OperandBundleUse getOperandBundleAt(unsigned Index) const {
1193 : assert(Index < getNumOperandBundles() && "Index out of bounds!");
1194 151261 : return operandBundleFromBundleOpInfo(*(bundle_op_info_begin() + Index));
1195 133464 : }
1196 3949650 :
1197 3949650 : /// Return the number of operand bundles with the tag Name attached to
1198 180 : /// this instruction.
1199 : unsigned countOperandBundlesOfType(StringRef Name) const {
1200 360 : unsigned Count = 0;
1201 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
1202 79 : if (getOperandBundleAt(i).getTagName() == Name)
1203 : Count++;
1204 127338 :
1205 125201 : return Count;
1206 30511833 : }
1207 30511708 :
1208 250 : /// Return the number of operand bundles with the tag ID attached to
1209 : /// this instruction.
1210 : unsigned countOperandBundlesOfType(uint32_t ID) const {
1211 : unsigned Count = 0;
1212 21 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
1213 : if (getOperandBundleAt(i).getTagID() == ID)
1214 23489 : Count++;
1215 8263 :
1216 : return Count;
1217 : }
1218 7615 :
1219 : /// Return an operand bundle by name, if present.
1220 15230 : ///
1221 : /// It is an error to call this for operand bundle types that may have
1222 1417 : /// multiple instances of them on the same instruction.
1223 : Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
1224 2828 : assert(countOperandBundlesOfType(Name) < 2 && "Precondition violated!");
1225 :
1226 6226 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
1227 : OperandBundleUse U = getOperandBundleAt(i);
1228 12452 : if (U.getTagName() == Name)
1229 : return U;
1230 22 : }
1231 :
1232 44 : return None;
1233 : }
1234 3 :
1235 : /// Return an operand bundle by tag ID, if present.
1236 12 : ///
1237 : /// It is an error to call this for operand bundle types that may have
1238 6 : /// multiple instances of them on the same instruction.
1239 74 : Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
1240 0 : assert(countOperandBundlesOfType(ID) < 2 && "Precondition violated!");
1241 :
1242 80 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
1243 14 : OperandBundleUse U = getOperandBundleAt(i);
1244 991515 : if (U.getTagID() == ID)
1245 14 : return U;
1246 992887 : }
1247 1386 :
1248 1257 : return None;
1249 : }
1250 991501 :
1251 : /// Return the list of operand bundles attached to this instruction as
1252 493266 : /// a vector of OperandBundleDefs.
1253 497 : ///
1254 493357 : /// This function copies the OperandBundeUse instances associated with this
1255 91 : /// OperandBundleUser to a vector of OperandBundleDefs. Note:
1256 500 : /// OperandBundeUses and OperandBundleDefs are non-trivially *different*
1257 170 : /// representations of operand bundles (see documentation above).
1258 3950279 : void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
1259 3457269 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i)
1260 498493 : Defs.emplace_back(getOperandBundleAt(i));
1261 3456847 : }
1262 499532 :
1263 1297 : /// Return the operand bundle for the operand at index OpIdx.
1264 1464 : ///
1265 : /// It is an error to call this with an OpIdx that does not correspond to an
1266 498235 : /// bundle operand.
1267 210 : OperandBundleUse getOperandBundleForOperand(unsigned OpIdx) const {
1268 50 : return operandBundleFromBundleOpInfo(getBundleOpInfoForOperand(OpIdx));
1269 49 : }
1270 49 :
1271 1 : /// Return true if this operand bundle user has operand bundles that
1272 1 : /// may read from the heap.
1273 1525470 : bool hasReadingOperandBundles() const {
1274 1 : // Implementation note: this is a conservative implementation of operand
1275 287 : // bundle semantics, where *any* operand bundle forces a callsite to be at
1276 1525729 : // least readonly.
1277 818 : return hasOperandBundles();
1278 1105 : }
1279 678 :
1280 119 : /// Return true if this operand bundle user has operand bundles that
1281 119 : /// may write to the heap.
1282 28 : bool hasClobberingOperandBundles() const {
1283 28 : for (auto &BOI : bundle_op_infos()) {
1284 267293 : if (BOI.Tag->second == LLVMContext::OB_deopt ||
1285 1 : BOI.Tag->second == LLVMContext::OB_funclet)
1286 0 : continue;
1287 267308 :
1288 13619315 : // This instruction has an operand bundle that is not known to us.
1289 13621726 : // Assume the worst.
1290 886 : return true;
1291 : }
1292 4540 :
1293 3336 : return false;
1294 407 : }
1295 1261508 :
1296 115 : /// Return true if the bundle operand at index \p OpIdx has the
1297 115 : /// attribute \p A.
1298 1258421 : bool bundleOperandHasAttr(unsigned OpIdx, Attribute::AttrKind A) const {
1299 880 : auto &BOI = getBundleOpInfoForOperand(OpIdx);
1300 137043 : auto OBU = operandBundleFromBundleOpInfo(BOI);
1301 892872 : return OBU.operandHasAttr(OpIdx - BOI.Begin, A);
1302 756077 : }
1303 136282 :
1304 125201 : /// Return true if \p Other has the same sequence of operand bundle
1305 125223 : /// tags with the same number of operands on each one of them as this
1306 22 : /// OperandBundleUser.
1307 125201 : bool hasIdenticalOperandBundleSchema(
1308 5473777 : const OperandBundleUser<InstrTy, OpIteratorTy> &Other) const {
1309 5473852 : if (getNumOperandBundles() != Other.getNumOperandBundles())
1310 109 : return false;
1311 8263 :
1312 : return std::equal(bundle_op_info_begin(), bundle_op_info_end(),
1313 : Other.bundle_op_info_begin());
1314 12863323 : }
1315 12864128 :
1316 854 : /// Return true if this operand bundle user contains operand bundles
1317 104 : /// with tags other than those specified in \p IDs.
1318 4 : bool hasOperandBundlesOtherThan(ArrayRef<uint32_t> IDs) const {
1319 4 : for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) {
1320 0 : uint32_t ID = getOperandBundleAt(i).getTagID();
1321 1265216 : if (!is_contained(IDs, ID))
1322 1265313 : return true;
1323 110 : }
1324 9 : return false;
1325 100 : }
1326 0 :
1327 0 : protected:
1328 : /// Is the function attribute S disallowed by some operand bundle on
1329 0 : /// this operand bundle user?
1330 0 : bool isFnAttrDisallowedByOpBundle(StringRef S) const {
1331 0 : // Operand bundles only possibly disallow readnone, readonly and argmenonly
1332 11495328 : // attributes. All String attributes are fine.
1333 11495682 : return false;
1334 4200740 : }
1335 4200373 :
1336 105 : /// Is the function attribute A disallowed by some operand bundle on
1337 0 : /// this operand bundle user?
1338 3637157 : bool isFnAttrDisallowedByOpBundle(Attribute::AttrKind A) const {
1339 3693027 : switch (A) {
1340 0 : default:
1341 55870 : return false;
1342 0 :
1343 0 : case Attribute::InaccessibleMemOrArgMemOnly:
1344 0 : return hasReadingOperandBundles();
1345 1004921 :
1346 1004926 : case Attribute::InaccessibleMemOnly:
1347 6 : return hasReadingOperandBundles();
1348 0 :
1349 0 : case Attribute::ArgMemOnly:
1350 106 : return hasReadingOperandBundles();
1351 106 :
1352 0 : case Attribute::ReadNone:
1353 650944 : return hasReadingOperandBundles();
1354 :
1355 55897 : case Attribute::ReadOnly:
1356 27 : return hasClobberingOperandBundles();
1357 55870 : }
1358 10490407 :
1359 10490756 : llvm_unreachable("switch has a default case!");
1360 538 : }
1361 106 :
1362 0 : /// Used to keep track of an operand bundle. See the main comment on
1363 212 : /// OperandBundleUser above.
1364 70 : struct BundleOpInfo {
1365 70 : /// The operand bundle tag, interned by
1366 0 : /// LLVMContextImpl::getOrInsertBundleTag.
1367 : StringMapEntry<uint32_t> *Tag;
1368 :
1369 : /// The index in the Use& vector where operands for this operand
1370 27023316 : /// bundle starts.
1371 27023316 : uint32_t Begin;
1372 0 :
1373 0 : /// The index in the Use& vector where operands for this operand
1374 0 : /// bundle ends.
1375 : uint32_t End;
1376 0 :
1377 : bool operator==(const BundleOpInfo &Other) const {
1378 0 : return Tag == Other.Tag && Begin == Other.Begin && End == Other.End;
1379 70 : }
1380 : };
1381 0 :
1382 17159 : /// Simple helper function to map a BundleOpInfo to an
1383 : /// OperandBundleUse.
1384 : OperandBundleUse
1385 258022 : operandBundleFromBundleOpInfo(const BundleOpInfo &BOI) const {
1386 31058193 : auto op_begin = static_cast<const InstrTy *>(this)->op_begin();
1387 31077054 : ArrayRef<Use> Inputs(op_begin + BOI.Begin, op_begin + BOI.End);
1388 19133 : return OperandBundleUse(BOI.Tag, Inputs);
1389 0 : }
1390 24306638 :
1391 24306638 : using bundle_op_iterator = BundleOpInfo *;
1392 0 : using const_bundle_op_iterator = const BundleOpInfo *;
1393 4499211 :
1394 4499211 : /// Return the start of the list of BundleOpInfo instances associated
1395 0 : /// with this OperandBundleUser.
1396 0 : bundle_op_iterator bundle_op_info_begin() {
1397 3492876 : if (!static_cast<InstrTy *>(this)->hasDescriptor())
1398 991917 : return nullptr;
1399 991917 :
1400 11090321 : uint8_t *BytesBegin = static_cast<InstrTy *>(this)->getDescriptor().begin();
1401 27282771 : return reinterpret_cast<bundle_op_iterator>(BytesBegin);
1402 177912 : }
1403 15460045 :
1404 13600401 : /// Return the start of the list of BundleOpInfo instances associated
1405 185302 : /// with this OperandBundleUser.
1406 2483341 : const_bundle_op_iterator bundle_op_info_begin() const {
1407 2484410 : auto *NonConstThis =
1408 1166 : const_cast<OperandBundleUser<InstrTy, OpIteratorTy> *>(this);
1409 1707052 : return NonConstThis->bundle_op_info_begin();
1410 1707052 : }
1411 303 :
1412 0 : /// Return the end of the list of BundleOpInfo instances associated
1413 611545 : /// with this OperandBundleUser.
1414 78636234 : bundle_op_iterator bundle_op_info_end() {
1415 87141678 : if (!static_cast<InstrTy *>(this)->hasDescriptor())
1416 22524105 : return nullptr;
1417 27988453 :
1418 5464715 : uint8_t *BytesEnd = static_cast<InstrTy *>(this)->getDescriptor().end();
1419 0 : return reinterpret_cast<bundle_op_iterator>(BytesEnd);
1420 2675949 : }
1421 1861555 :
1422 1709 : /// Return the end of the list of BundleOpInfo instances associated
1423 22221473 : /// with this OperandBundleUser.
1424 8038946 : const_bundle_op_iterator bundle_op_info_end() const {
1425 4580377 : auto *NonConstThis =
1426 11057701 : const_cast<OperandBundleUser<InstrTy, OpIteratorTy> *>(this);
1427 756043 : return NonConstThis->bundle_op_info_end();
1428 1147323 : }
1429 33715865 :
1430 532475 : /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
1431 15157189 : iterator_range<bundle_op_iterator> bundle_op_infos() {
1432 40846215 : return make_range(bundle_op_info_begin(), bundle_op_info_end());
1433 34627631 : }
1434 27376 :
1435 1 : /// Return the range [\p bundle_op_info_begin, \p bundle_op_info_end).
1436 23695146 : iterator_range<const_bundle_op_iterator> bundle_op_infos() const {
1437 32462041 : return make_range(bundle_op_info_begin(), bundle_op_info_end());
1438 10031694 : }
1439 5819 :
1440 1264773 : /// Populate the BundleOpInfo instances and the Use& vector from \p
1441 16393503 : /// Bundles. Return the op_iterator pointing to the Use& one past the last
1442 17 : /// last bundle operand use.
1443 868234 : ///
1444 995104 : /// Each \p OperandBundleDef instance is tracked by a OperandBundleInfo
1445 991917 : /// instance allocated in this User's descriptor.
1446 9376654 : OpIteratorTy populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles,
1447 23802202 : const unsigned BeginIndex) {
1448 155815 : auto It = static_cast<InstrTy *>(this)->op_begin() + BeginIndex;
1449 17062648 : for (auto &B : Bundles)
1450 12844358 : It = std::copy(B.input_begin(), B.input_end(), It);
1451 5533340 :
1452 4687938 : auto *ContextImpl = static_cast<InstrTy *>(this)->getContext().pImpl;
1453 1952840 : auto BI = Bundles.begin();
1454 1006430 : unsigned CurrentIndex = BeginIndex;
1455 1004921 :
1456 12 : for (auto &BOI : bundle_op_infos()) {
1457 3730439 : assert(BI != Bundles.end() && "Incorrect allocation?");
1458 3724623 :
1459 0 : BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag());
1460 69932918 : BOI.Begin = CurrentIndex;
1461 74292550 : BOI.End = CurrentIndex + BI->input_size();
1462 0 : CurrentIndex = BOI.End;
1463 7250721 : BI++;
1464 7249029 : }
1465 0 :
1466 1866754 : assert(BI == Bundles.end() && "Incorrect allocation?");
1467 154 :
1468 0 : return It;
1469 1858019 : }
1470 4558292 :
1471 4558435 : /// Return the BundleOpInfo for the operand at index OpIdx.
1472 9481815 : ///
1473 0 : /// It is an error to call this with an OpIdx that does not correspond to an
1474 670885 : /// bundle operand.
1475 30748420 : const BundleOpInfo &getBundleOpInfoForOperand(unsigned OpIdx) const {
1476 0 : for (auto &BOI : bundle_op_infos())
1477 10490407 : if (BOI.Begin <= OpIdx && OpIdx < BOI.End)
1478 14215033 : return BOI;
1479 0 :
1480 3724623 : llvm_unreachable("Did not find operand bundle for operand!");
1481 3727608 : }
1482 0 :
1483 0 : /// Return the total number of values used in \p Bundles.
1484 3725379 : static unsigned CountBundleInputs(ArrayRef<OperandBundleDef> Bundles) {
1485 0 : unsigned Total = 0;
1486 1646820 : for (auto &B : Bundles)
1487 400 : Total += B.input_size();
1488 3726671 : return Total;
1489 44163 : }
1490 : };
1491 30019019 :
1492 2048 : } // end namespace llvm
1493 296095 :
1494 2319 : #endif // LLVM_IR_INSTRTYPES_H
|