blob: 01746e4b6a2919e6fa7831f52a85c4ac1f73a399 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/Operator.h - Operator utility subclass -------------*- 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 classes for working with Instructions and
11// ConstantExprs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_OPERATOR_H
16#define LLVM_IR_OPERATOR_H
17
18#include "llvm/ADT/None.h"
19#include "llvm/ADT/Optional.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Type.h"
23#include "llvm/IR/Value.h"
24#include "llvm/Support/Casting.h"
25#include <cstddef>
26
27namespace llvm {
28
29/// This is a utility class that provides an abstraction for the common
30/// functionality between Instructions and ConstantExprs.
31class Operator : public User {
32public:
33 // The Operator class is intended to be used as a utility, and is never itself
34 // instantiated.
35 Operator() = delete;
36 ~Operator() = delete;
37
38 void *operator new(size_t s) = delete;
39
40 /// Return the opcode for this Instruction or ConstantExpr.
41 unsigned getOpcode() const {
42 if (const Instruction *I = dyn_cast<Instruction>(this))
43 return I->getOpcode();
44 return cast<ConstantExpr>(this)->getOpcode();
45 }
46
47 /// If V is an Instruction or ConstantExpr, return its opcode.
48 /// Otherwise return UserOp1.
49 static unsigned getOpcode(const Value *V) {
50 if (const Instruction *I = dyn_cast<Instruction>(V))
51 return I->getOpcode();
52 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
53 return CE->getOpcode();
54 return Instruction::UserOp1;
55 }
56
57 static bool classof(const Instruction *) { return true; }
58 static bool classof(const ConstantExpr *) { return true; }
59 static bool classof(const Value *V) {
60 return isa<Instruction>(V) || isa<ConstantExpr>(V);
61 }
62};
63
64/// Utility class for integer operators which may exhibit overflow - Add, Sub,
65/// Mul, and Shl. It does not include SDiv, despite that operator having the
66/// potential for overflow.
67class OverflowingBinaryOperator : public Operator {
68public:
69 enum {
70 NoUnsignedWrap = (1 << 0),
71 NoSignedWrap = (1 << 1)
72 };
73
74private:
75 friend class Instruction;
76 friend class ConstantExpr;
77
78 void setHasNoUnsignedWrap(bool B) {
79 SubclassOptionalData =
80 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
81 }
82 void setHasNoSignedWrap(bool B) {
83 SubclassOptionalData =
84 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
85 }
86
87public:
88 /// Test whether this operation is known to never
89 /// undergo unsigned overflow, aka the nuw property.
90 bool hasNoUnsignedWrap() const {
91 return SubclassOptionalData & NoUnsignedWrap;
92 }
93
94 /// Test whether this operation is known to never
95 /// undergo signed overflow, aka the nsw property.
96 bool hasNoSignedWrap() const {
97 return (SubclassOptionalData & NoSignedWrap) != 0;
98 }
99
100 static bool classof(const Instruction *I) {
101 return I->getOpcode() == Instruction::Add ||
102 I->getOpcode() == Instruction::Sub ||
103 I->getOpcode() == Instruction::Mul ||
104 I->getOpcode() == Instruction::Shl;
105 }
106 static bool classof(const ConstantExpr *CE) {
107 return CE->getOpcode() == Instruction::Add ||
108 CE->getOpcode() == Instruction::Sub ||
109 CE->getOpcode() == Instruction::Mul ||
110 CE->getOpcode() == Instruction::Shl;
111 }
112 static bool classof(const Value *V) {
113 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
114 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
115 }
116};
117
118/// A udiv or sdiv instruction, which can be marked as "exact",
119/// indicating that no bits are destroyed.
120class PossiblyExactOperator : public Operator {
121public:
122 enum {
123 IsExact = (1 << 0)
124 };
125
126private:
127 friend class Instruction;
128 friend class ConstantExpr;
129
130 void setIsExact(bool B) {
131 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
132 }
133
134public:
135 /// Test whether this division is known to be exact, with zero remainder.
136 bool isExact() const {
137 return SubclassOptionalData & IsExact;
138 }
139
140 static bool isPossiblyExactOpcode(unsigned OpC) {
141 return OpC == Instruction::SDiv ||
142 OpC == Instruction::UDiv ||
143 OpC == Instruction::AShr ||
144 OpC == Instruction::LShr;
145 }
146
147 static bool classof(const ConstantExpr *CE) {
148 return isPossiblyExactOpcode(CE->getOpcode());
149 }
150 static bool classof(const Instruction *I) {
151 return isPossiblyExactOpcode(I->getOpcode());
152 }
153 static bool classof(const Value *V) {
154 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
155 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
156 }
157};
158
159/// Convenience struct for specifying and reasoning about fast-math flags.
160class FastMathFlags {
161private:
162 friend class FPMathOperator;
163
164 unsigned Flags = 0;
165
166 FastMathFlags(unsigned F) {
167 // If all 7 bits are set, turn this into -1. If the number of bits grows,
168 // this must be updated. This is intended to provide some forward binary
169 // compatibility insurance for the meaning of 'fast' in case bits are added.
170 if (F == 0x7F) Flags = ~0U;
171 else Flags = F;
172 }
173
174public:
175 // This is how the bits are used in Value::SubclassOptionalData so they
176 // should fit there too.
177 // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
178 // functionality will require a change in how this information is stored.
179 enum {
180 AllowReassoc = (1 << 0),
181 NoNaNs = (1 << 1),
182 NoInfs = (1 << 2),
183 NoSignedZeros = (1 << 3),
184 AllowReciprocal = (1 << 4),
185 AllowContract = (1 << 5),
186 ApproxFunc = (1 << 6)
187 };
188
189 FastMathFlags() = default;
190
191 bool any() const { return Flags != 0; }
192 bool none() const { return Flags == 0; }
193 bool all() const { return Flags == ~0U; }
194
195 void clear() { Flags = 0; }
196 void set() { Flags = ~0U; }
197
198 /// Flag queries
199 bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
200 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
201 bool noInfs() const { return 0 != (Flags & NoInfs); }
202 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
203 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
204 bool allowContract() const { return 0 != (Flags & AllowContract); }
205 bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
206 /// 'Fast' means all bits are set.
207 bool isFast() const { return all(); }
208
209 /// Flag setters
210 void setAllowReassoc() { Flags |= AllowReassoc; }
211 void setNoNaNs() { Flags |= NoNaNs; }
212 void setNoInfs() { Flags |= NoInfs; }
213 void setNoSignedZeros() { Flags |= NoSignedZeros; }
214 void setAllowReciprocal() { Flags |= AllowReciprocal; }
215 // TODO: Change the other set* functions to take a parameter?
216 void setAllowContract(bool B) {
217 Flags = (Flags & ~AllowContract) | B * AllowContract;
218 }
219 void setApproxFunc() { Flags |= ApproxFunc; }
220 void setFast() { set(); }
221
222 void operator&=(const FastMathFlags &OtherFlags) {
223 Flags &= OtherFlags.Flags;
224 }
225};
226
227/// Utility class for floating point operations which can have
228/// information about relaxed accuracy requirements attached to them.
229class FPMathOperator : public Operator {
230private:
231 friend class Instruction;
232
233 /// 'Fast' means all bits are set.
234 void setFast(bool B) {
235 setHasAllowReassoc(B);
236 setHasNoNaNs(B);
237 setHasNoInfs(B);
238 setHasNoSignedZeros(B);
239 setHasAllowReciprocal(B);
240 setHasAllowContract(B);
241 setHasApproxFunc(B);
242 }
243
244 void setHasAllowReassoc(bool B) {
245 SubclassOptionalData =
246 (SubclassOptionalData & ~FastMathFlags::AllowReassoc) |
247 (B * FastMathFlags::AllowReassoc);
248 }
249
250 void setHasNoNaNs(bool B) {
251 SubclassOptionalData =
252 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
253 (B * FastMathFlags::NoNaNs);
254 }
255
256 void setHasNoInfs(bool B) {
257 SubclassOptionalData =
258 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
259 (B * FastMathFlags::NoInfs);
260 }
261
262 void setHasNoSignedZeros(bool B) {
263 SubclassOptionalData =
264 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
265 (B * FastMathFlags::NoSignedZeros);
266 }
267
268 void setHasAllowReciprocal(bool B) {
269 SubclassOptionalData =
270 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
271 (B * FastMathFlags::AllowReciprocal);
272 }
273
274 void setHasAllowContract(bool B) {
275 SubclassOptionalData =
276 (SubclassOptionalData & ~FastMathFlags::AllowContract) |
277 (B * FastMathFlags::AllowContract);
278 }
279
280 void setHasApproxFunc(bool B) {
281 SubclassOptionalData =
282 (SubclassOptionalData & ~FastMathFlags::ApproxFunc) |
283 (B * FastMathFlags::ApproxFunc);
284 }
285
286 /// Convenience function for setting multiple fast-math flags.
287 /// FMF is a mask of the bits to set.
288 void setFastMathFlags(FastMathFlags FMF) {
289 SubclassOptionalData |= FMF.Flags;
290 }
291
292 /// Convenience function for copying all fast-math flags.
293 /// All values in FMF are transferred to this operator.
294 void copyFastMathFlags(FastMathFlags FMF) {
295 SubclassOptionalData = FMF.Flags;
296 }
297
298public:
299 /// Test if this operation allows all non-strict floating-point transforms.
300 bool isFast() const {
301 return ((SubclassOptionalData & FastMathFlags::AllowReassoc) != 0 &&
302 (SubclassOptionalData & FastMathFlags::NoNaNs) != 0 &&
303 (SubclassOptionalData & FastMathFlags::NoInfs) != 0 &&
304 (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0 &&
305 (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0 &&
306 (SubclassOptionalData & FastMathFlags::AllowContract) != 0 &&
307 (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0);
308 }
309
310 /// Test if this operation may be simplified with reassociative transforms.
311 bool hasAllowReassoc() const {
312 return (SubclassOptionalData & FastMathFlags::AllowReassoc) != 0;
313 }
314
315 /// Test if this operation's arguments and results are assumed not-NaN.
316 bool hasNoNaNs() const {
317 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
318 }
319
320 /// Test if this operation's arguments and results are assumed not-infinite.
321 bool hasNoInfs() const {
322 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
323 }
324
325 /// Test if this operation can ignore the sign of zero.
326 bool hasNoSignedZeros() const {
327 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
328 }
329
330 /// Test if this operation can use reciprocal multiply instead of division.
331 bool hasAllowReciprocal() const {
332 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
333 }
334
335 /// Test if this operation can be floating-point contracted (FMA).
336 bool hasAllowContract() const {
337 return (SubclassOptionalData & FastMathFlags::AllowContract) != 0;
338 }
339
340 /// Test if this operation allows approximations of math library functions or
341 /// intrinsics.
342 bool hasApproxFunc() const {
343 return (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0;
344 }
345
346 /// Convenience function for getting all the fast-math flags
347 FastMathFlags getFastMathFlags() const {
348 return FastMathFlags(SubclassOptionalData);
349 }
350
351 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
352 /// 0.0 means that the operation should be performed with the default
353 /// precision.
354 float getFPAccuracy() const;
355
356 static bool classof(const Instruction *I) {
357 return I->getType()->isFPOrFPVectorTy() ||
358 I->getOpcode() == Instruction::FCmp;
359 }
360
361 static bool classof(const ConstantExpr *CE) {
362 return CE->getType()->isFPOrFPVectorTy() ||
363 CE->getOpcode() == Instruction::FCmp;
364 }
365
366 static bool classof(const Value *V) {
367 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
368 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
369 }
370};
371
372/// A helper template for defining operators for individual opcodes.
373template<typename SuperClass, unsigned Opc>
374class ConcreteOperator : public SuperClass {
375public:
376 static bool classof(const Instruction *I) {
377 return I->getOpcode() == Opc;
378 }
379 static bool classof(const ConstantExpr *CE) {
380 return CE->getOpcode() == Opc;
381 }
382 static bool classof(const Value *V) {
383 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
384 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
385 }
386};
387
388class AddOperator
389 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
390};
391class SubOperator
392 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
393};
394class MulOperator
395 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
396};
397class ShlOperator
398 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
399};
400
401class SDivOperator
402 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
403};
404class UDivOperator
405 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
406};
407class AShrOperator
408 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
409};
410class LShrOperator
411 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
412};
413
414class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
415
416class GEPOperator
417 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
418 friend class GetElementPtrInst;
419 friend class ConstantExpr;
420
421 enum {
422 IsInBounds = (1 << 0),
423 // InRangeIndex: bits 1-6
424 };
425
426 void setIsInBounds(bool B) {
427 SubclassOptionalData =
428 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
429 }
430
431public:
432 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
433 bool isInBounds() const {
434 return SubclassOptionalData & IsInBounds;
435 }
436
437 /// Returns the offset of the index with an inrange attachment, or None if
438 /// none.
439 Optional<unsigned> getInRangeIndex() const {
440 if (SubclassOptionalData >> 1 == 0) return None;
441 return (SubclassOptionalData >> 1) - 1;
442 }
443
444 inline op_iterator idx_begin() { return op_begin()+1; }
445 inline const_op_iterator idx_begin() const { return op_begin()+1; }
446 inline op_iterator idx_end() { return op_end(); }
447 inline const_op_iterator idx_end() const { return op_end(); }
448
449 Value *getPointerOperand() {
450 return getOperand(0);
451 }
452 const Value *getPointerOperand() const {
453 return getOperand(0);
454 }
455 static unsigned getPointerOperandIndex() {
456 return 0U; // get index for modifying correct operand
457 }
458
459 /// Method to return the pointer operand as a PointerType.
460 Type *getPointerOperandType() const {
461 return getPointerOperand()->getType();
462 }
463
464 Type *getSourceElementType() const;
465 Type *getResultElementType() const;
466
467 /// Method to return the address space of the pointer operand.
468 unsigned getPointerAddressSpace() const {
469 return getPointerOperandType()->getPointerAddressSpace();
470 }
471
472 unsigned getNumIndices() const { // Note: always non-negative
473 return getNumOperands() - 1;
474 }
475
476 bool hasIndices() const {
477 return getNumOperands() > 1;
478 }
479
480 /// Return true if all of the indices of this GEP are zeros.
481 /// If so, the result pointer and the first operand have the same
482 /// value, just potentially different types.
483 bool hasAllZeroIndices() const {
484 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
485 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
486 if (C->isZero())
487 continue;
488 return false;
489 }
490 return true;
491 }
492
493 /// Return true if all of the indices of this GEP are constant integers.
494 /// If so, the result pointer and the first operand have
495 /// a constant offset between them.
496 bool hasAllConstantIndices() const {
497 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
498 if (!isa<ConstantInt>(I))
499 return false;
500 }
501 return true;
502 }
503
504 unsigned countNonConstantIndices() const {
505 return count_if(make_range(idx_begin(), idx_end()), [](const Use& use) {
506 return !isa<ConstantInt>(*use);
507 });
508 }
509
510 /// \brief Accumulate the constant address offset of this GEP if possible.
511 ///
512 /// This routine accepts an APInt into which it will accumulate the constant
513 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
514 /// all-constant, it returns false and the value of the offset APInt is
515 /// undefined (it is *not* preserved!). The APInt passed into this routine
516 /// must be at exactly as wide as the IntPtr type for the address space of the
517 /// base GEP pointer.
518 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
519};
520
521class PtrToIntOperator
522 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
523 friend class PtrToInt;
524 friend class ConstantExpr;
525
526public:
527 Value *getPointerOperand() {
528 return getOperand(0);
529 }
530 const Value *getPointerOperand() const {
531 return getOperand(0);
532 }
533
534 static unsigned getPointerOperandIndex() {
535 return 0U; // get index for modifying correct operand
536 }
537
538 /// Method to return the pointer operand as a PointerType.
539 Type *getPointerOperandType() const {
540 return getPointerOperand()->getType();
541 }
542
543 /// Method to return the address space of the pointer operand.
544 unsigned getPointerAddressSpace() const {
545 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
546 }
547};
548
549class BitCastOperator
550 : public ConcreteOperator<Operator, Instruction::BitCast> {
551 friend class BitCastInst;
552 friend class ConstantExpr;
553
554public:
555 Type *getSrcTy() const {
556 return getOperand(0)->getType();
557 }
558
559 Type *getDestTy() const {
560 return getType();
561 }
562};
563
564} // end namespace llvm
565
566#endif // LLVM_IR_OPERATOR_H