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