| File: | src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Alignment.h |
| Warning: | line 85, column 47 The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===- ValueTracking.cpp - Walk computations to compute properties --------===// | ||||||||
| 2 | // | ||||||||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||
| 6 | // | ||||||||
| 7 | //===----------------------------------------------------------------------===// | ||||||||
| 8 | // | ||||||||
| 9 | // This file contains routines that help analyze properties that chains of | ||||||||
| 10 | // computations have. | ||||||||
| 11 | // | ||||||||
| 12 | //===----------------------------------------------------------------------===// | ||||||||
| 13 | |||||||||
| 14 | #include "llvm/Analysis/ValueTracking.h" | ||||||||
| 15 | #include "llvm/ADT/APFloat.h" | ||||||||
| 16 | #include "llvm/ADT/APInt.h" | ||||||||
| 17 | #include "llvm/ADT/ArrayRef.h" | ||||||||
| 18 | #include "llvm/ADT/None.h" | ||||||||
| 19 | #include "llvm/ADT/Optional.h" | ||||||||
| 20 | #include "llvm/ADT/STLExtras.h" | ||||||||
| 21 | #include "llvm/ADT/SmallPtrSet.h" | ||||||||
| 22 | #include "llvm/ADT/SmallSet.h" | ||||||||
| 23 | #include "llvm/ADT/SmallVector.h" | ||||||||
| 24 | #include "llvm/ADT/StringRef.h" | ||||||||
| 25 | #include "llvm/ADT/iterator_range.h" | ||||||||
| 26 | #include "llvm/Analysis/AliasAnalysis.h" | ||||||||
| 27 | #include "llvm/Analysis/AssumeBundleQueries.h" | ||||||||
| 28 | #include "llvm/Analysis/AssumptionCache.h" | ||||||||
| 29 | #include "llvm/Analysis/EHPersonalities.h" | ||||||||
| 30 | #include "llvm/Analysis/GuardUtils.h" | ||||||||
| 31 | #include "llvm/Analysis/InstructionSimplify.h" | ||||||||
| 32 | #include "llvm/Analysis/Loads.h" | ||||||||
| 33 | #include "llvm/Analysis/LoopInfo.h" | ||||||||
| 34 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||||||
| 35 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||||||
| 36 | #include "llvm/IR/Argument.h" | ||||||||
| 37 | #include "llvm/IR/Attributes.h" | ||||||||
| 38 | #include "llvm/IR/BasicBlock.h" | ||||||||
| 39 | #include "llvm/IR/Constant.h" | ||||||||
| 40 | #include "llvm/IR/ConstantRange.h" | ||||||||
| 41 | #include "llvm/IR/Constants.h" | ||||||||
| 42 | #include "llvm/IR/DerivedTypes.h" | ||||||||
| 43 | #include "llvm/IR/DiagnosticInfo.h" | ||||||||
| 44 | #include "llvm/IR/Dominators.h" | ||||||||
| 45 | #include "llvm/IR/Function.h" | ||||||||
| 46 | #include "llvm/IR/GetElementPtrTypeIterator.h" | ||||||||
| 47 | #include "llvm/IR/GlobalAlias.h" | ||||||||
| 48 | #include "llvm/IR/GlobalValue.h" | ||||||||
| 49 | #include "llvm/IR/GlobalVariable.h" | ||||||||
| 50 | #include "llvm/IR/InstrTypes.h" | ||||||||
| 51 | #include "llvm/IR/Instruction.h" | ||||||||
| 52 | #include "llvm/IR/Instructions.h" | ||||||||
| 53 | #include "llvm/IR/IntrinsicInst.h" | ||||||||
| 54 | #include "llvm/IR/Intrinsics.h" | ||||||||
| 55 | #include "llvm/IR/IntrinsicsAArch64.h" | ||||||||
| 56 | #include "llvm/IR/IntrinsicsRISCV.h" | ||||||||
| 57 | #include "llvm/IR/IntrinsicsX86.h" | ||||||||
| 58 | #include "llvm/IR/LLVMContext.h" | ||||||||
| 59 | #include "llvm/IR/Metadata.h" | ||||||||
| 60 | #include "llvm/IR/Module.h" | ||||||||
| 61 | #include "llvm/IR/Operator.h" | ||||||||
| 62 | #include "llvm/IR/PatternMatch.h" | ||||||||
| 63 | #include "llvm/IR/Type.h" | ||||||||
| 64 | #include "llvm/IR/User.h" | ||||||||
| 65 | #include "llvm/IR/Value.h" | ||||||||
| 66 | #include "llvm/Support/Casting.h" | ||||||||
| 67 | #include "llvm/Support/CommandLine.h" | ||||||||
| 68 | #include "llvm/Support/Compiler.h" | ||||||||
| 69 | #include "llvm/Support/ErrorHandling.h" | ||||||||
| 70 | #include "llvm/Support/KnownBits.h" | ||||||||
| 71 | #include "llvm/Support/MathExtras.h" | ||||||||
| 72 | #include <algorithm> | ||||||||
| 73 | #include <array> | ||||||||
| 74 | #include <cassert> | ||||||||
| 75 | #include <cstdint> | ||||||||
| 76 | #include <iterator> | ||||||||
| 77 | #include <utility> | ||||||||
| 78 | |||||||||
| 79 | using namespace llvm; | ||||||||
| 80 | using namespace llvm::PatternMatch; | ||||||||
| 81 | |||||||||
| 82 | // Controls the number of uses of the value searched for possible | ||||||||
| 83 | // dominating comparisons. | ||||||||
| 84 | static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses", | ||||||||
| 85 | cl::Hidden, cl::init(20)); | ||||||||
| 86 | |||||||||
| 87 | /// Returns the bitwidth of the given scalar or pointer type. For vector types, | ||||||||
| 88 | /// returns the element type's bitwidth. | ||||||||
| 89 | static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { | ||||||||
| 90 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) | ||||||||
| 91 | return BitWidth; | ||||||||
| 92 | |||||||||
| 93 | return DL.getPointerTypeSizeInBits(Ty); | ||||||||
| 94 | } | ||||||||
| 95 | |||||||||
| 96 | namespace { | ||||||||
| 97 | |||||||||
| 98 | // Simplifying using an assume can only be done in a particular control-flow | ||||||||
| 99 | // context (the context instruction provides that context). If an assume and | ||||||||
| 100 | // the context instruction are not in the same block then the DT helps in | ||||||||
| 101 | // figuring out if we can use it. | ||||||||
| 102 | struct Query { | ||||||||
| 103 | const DataLayout &DL; | ||||||||
| 104 | AssumptionCache *AC; | ||||||||
| 105 | const Instruction *CxtI; | ||||||||
| 106 | const DominatorTree *DT; | ||||||||
| 107 | |||||||||
| 108 | // Unlike the other analyses, this may be a nullptr because not all clients | ||||||||
| 109 | // provide it currently. | ||||||||
| 110 | OptimizationRemarkEmitter *ORE; | ||||||||
| 111 | |||||||||
| 112 | /// If true, it is safe to use metadata during simplification. | ||||||||
| 113 | InstrInfoQuery IIQ; | ||||||||
| 114 | |||||||||
| 115 | Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 116 | const DominatorTree *DT, bool UseInstrInfo, | ||||||||
| 117 | OptimizationRemarkEmitter *ORE = nullptr) | ||||||||
| 118 | : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {} | ||||||||
| 119 | }; | ||||||||
| 120 | |||||||||
| 121 | } // end anonymous namespace | ||||||||
| 122 | |||||||||
| 123 | // Given the provided Value and, potentially, a context instruction, return | ||||||||
| 124 | // the preferred context instruction (if any). | ||||||||
| 125 | static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { | ||||||||
| 126 | // If we've been provided with a context instruction, then use that (provided | ||||||||
| 127 | // it has been inserted). | ||||||||
| 128 | if (CxtI && CxtI->getParent()) | ||||||||
| 129 | return CxtI; | ||||||||
| 130 | |||||||||
| 131 | // If the value is really an already-inserted instruction, then use that. | ||||||||
| 132 | CxtI = dyn_cast<Instruction>(V); | ||||||||
| 133 | if (CxtI && CxtI->getParent()) | ||||||||
| 134 | return CxtI; | ||||||||
| 135 | |||||||||
| 136 | return nullptr; | ||||||||
| 137 | } | ||||||||
| 138 | |||||||||
| 139 | static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) { | ||||||||
| 140 | // If we've been provided with a context instruction, then use that (provided | ||||||||
| 141 | // it has been inserted). | ||||||||
| 142 | if (CxtI && CxtI->getParent()) | ||||||||
| 143 | return CxtI; | ||||||||
| 144 | |||||||||
| 145 | // If the value is really an already-inserted instruction, then use that. | ||||||||
| 146 | CxtI = dyn_cast<Instruction>(V1); | ||||||||
| 147 | if (CxtI && CxtI->getParent()) | ||||||||
| 148 | return CxtI; | ||||||||
| 149 | |||||||||
| 150 | CxtI = dyn_cast<Instruction>(V2); | ||||||||
| 151 | if (CxtI && CxtI->getParent()) | ||||||||
| 152 | return CxtI; | ||||||||
| 153 | |||||||||
| 154 | return nullptr; | ||||||||
| 155 | } | ||||||||
| 156 | |||||||||
| 157 | static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, | ||||||||
| 158 | const APInt &DemandedElts, | ||||||||
| 159 | APInt &DemandedLHS, APInt &DemandedRHS) { | ||||||||
| 160 | // The length of scalable vectors is unknown at compile time, thus we | ||||||||
| 161 | // cannot check their values | ||||||||
| 162 | if (isa<ScalableVectorType>(Shuf->getType())) | ||||||||
| 163 | return false; | ||||||||
| 164 | |||||||||
| 165 | int NumElts = | ||||||||
| 166 | cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements(); | ||||||||
| 167 | int NumMaskElts = cast<FixedVectorType>(Shuf->getType())->getNumElements(); | ||||||||
| 168 | DemandedLHS = DemandedRHS = APInt::getNullValue(NumElts); | ||||||||
| 169 | if (DemandedElts.isNullValue()) | ||||||||
| 170 | return true; | ||||||||
| 171 | // Simple case of a shuffle with zeroinitializer. | ||||||||
| 172 | if (all_of(Shuf->getShuffleMask(), [](int Elt) { return Elt == 0; })) { | ||||||||
| 173 | DemandedLHS.setBit(0); | ||||||||
| 174 | return true; | ||||||||
| 175 | } | ||||||||
| 176 | for (int i = 0; i != NumMaskElts; ++i) { | ||||||||
| 177 | if (!DemandedElts[i]) | ||||||||
| 178 | continue; | ||||||||
| 179 | int M = Shuf->getMaskValue(i); | ||||||||
| 180 | assert(M < (NumElts * 2) && "Invalid shuffle mask constant")((void)0); | ||||||||
| 181 | |||||||||
| 182 | // For undef elements, we don't know anything about the common state of | ||||||||
| 183 | // the shuffle result. | ||||||||
| 184 | if (M == -1) | ||||||||
| 185 | return false; | ||||||||
| 186 | if (M < NumElts) | ||||||||
| 187 | DemandedLHS.setBit(M % NumElts); | ||||||||
| 188 | else | ||||||||
| 189 | DemandedRHS.setBit(M % NumElts); | ||||||||
| 190 | } | ||||||||
| 191 | |||||||||
| 192 | return true; | ||||||||
| 193 | } | ||||||||
| 194 | |||||||||
| 195 | static void computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 196 | KnownBits &Known, unsigned Depth, const Query &Q); | ||||||||
| 197 | |||||||||
| 198 | static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, | ||||||||
| 199 | const Query &Q) { | ||||||||
| 200 | // FIXME: We currently have no way to represent the DemandedElts of a scalable | ||||||||
| 201 | // vector | ||||||||
| 202 | if (isa<ScalableVectorType>(V->getType())) { | ||||||||
| 203 | Known.resetAll(); | ||||||||
| 204 | return; | ||||||||
| 205 | } | ||||||||
| 206 | |||||||||
| 207 | auto *FVTy = dyn_cast<FixedVectorType>(V->getType()); | ||||||||
| 208 | APInt DemandedElts = | ||||||||
| 209 | FVTy ? APInt::getAllOnesValue(FVTy->getNumElements()) : APInt(1, 1); | ||||||||
| 210 | computeKnownBits(V, DemandedElts, Known, Depth, Q); | ||||||||
| 211 | } | ||||||||
| 212 | |||||||||
| 213 | void llvm::computeKnownBits(const Value *V, KnownBits &Known, | ||||||||
| 214 | const DataLayout &DL, unsigned Depth, | ||||||||
| 215 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 216 | const DominatorTree *DT, | ||||||||
| 217 | OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { | ||||||||
| 218 | ::computeKnownBits(V, Known, Depth, | ||||||||
| 219 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||||||
| 220 | } | ||||||||
| 221 | |||||||||
| 222 | void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 223 | KnownBits &Known, const DataLayout &DL, | ||||||||
| 224 | unsigned Depth, AssumptionCache *AC, | ||||||||
| 225 | const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 226 | OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { | ||||||||
| 227 | ::computeKnownBits(V, DemandedElts, Known, Depth, | ||||||||
| 228 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||||||
| 229 | } | ||||||||
| 230 | |||||||||
| 231 | static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 232 | unsigned Depth, const Query &Q); | ||||||||
| 233 | |||||||||
| 234 | static KnownBits computeKnownBits(const Value *V, unsigned Depth, | ||||||||
| 235 | const Query &Q); | ||||||||
| 236 | |||||||||
| 237 | KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL, | ||||||||
| 238 | unsigned Depth, AssumptionCache *AC, | ||||||||
| 239 | const Instruction *CxtI, | ||||||||
| 240 | const DominatorTree *DT, | ||||||||
| 241 | OptimizationRemarkEmitter *ORE, | ||||||||
| 242 | bool UseInstrInfo) { | ||||||||
| 243 | return ::computeKnownBits( | ||||||||
| 244 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||||||
| 245 | } | ||||||||
| 246 | |||||||||
| 247 | KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 248 | const DataLayout &DL, unsigned Depth, | ||||||||
| 249 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 250 | const DominatorTree *DT, | ||||||||
| 251 | OptimizationRemarkEmitter *ORE, | ||||||||
| 252 | bool UseInstrInfo) { | ||||||||
| 253 | return ::computeKnownBits( | ||||||||
| 254 | V, DemandedElts, Depth, | ||||||||
| 255 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); | ||||||||
| 256 | } | ||||||||
| 257 | |||||||||
| 258 | bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, | ||||||||
| 259 | const DataLayout &DL, AssumptionCache *AC, | ||||||||
| 260 | const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 261 | bool UseInstrInfo) { | ||||||||
| 262 | assert(LHS->getType() == RHS->getType() &&((void)0) | ||||||||
| 263 | "LHS and RHS should have the same type")((void)0); | ||||||||
| 264 | assert(LHS->getType()->isIntOrIntVectorTy() &&((void)0) | ||||||||
| 265 | "LHS and RHS should be integers")((void)0); | ||||||||
| 266 | // Look for an inverted mask: (X & ~M) op (Y & M). | ||||||||
| 267 | Value *M; | ||||||||
| 268 | if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) && | ||||||||
| 269 | match(RHS, m_c_And(m_Specific(M), m_Value()))) | ||||||||
| 270 | return true; | ||||||||
| 271 | if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && | ||||||||
| 272 | match(LHS, m_c_And(m_Specific(M), m_Value()))) | ||||||||
| 273 | return true; | ||||||||
| 274 | IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType()); | ||||||||
| 275 | KnownBits LHSKnown(IT->getBitWidth()); | ||||||||
| 276 | KnownBits RHSKnown(IT->getBitWidth()); | ||||||||
| 277 | computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||||||
| 278 | computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||||||
| 279 | return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown); | ||||||||
| 280 | } | ||||||||
| 281 | |||||||||
| 282 | bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) { | ||||||||
| 283 | for (const User *U : CxtI->users()) { | ||||||||
| 284 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(U)) | ||||||||
| 285 | if (IC->isEquality()) | ||||||||
| 286 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) | ||||||||
| 287 | if (C->isNullValue()) | ||||||||
| 288 | continue; | ||||||||
| 289 | return false; | ||||||||
| 290 | } | ||||||||
| 291 | return true; | ||||||||
| 292 | } | ||||||||
| 293 | |||||||||
| 294 | static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | ||||||||
| 295 | const Query &Q); | ||||||||
| 296 | |||||||||
| 297 | bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, | ||||||||
| 298 | bool OrZero, unsigned Depth, | ||||||||
| 299 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 300 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 301 | return ::isKnownToBeAPowerOfTwo( | ||||||||
| 302 | V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||||||
| 303 | } | ||||||||
| 304 | |||||||||
| 305 | static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, | ||||||||
| 306 | unsigned Depth, const Query &Q); | ||||||||
| 307 | |||||||||
| 308 | static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q); | ||||||||
| 309 | |||||||||
| 310 | bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, | ||||||||
| 311 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 312 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 313 | return ::isKnownNonZero(V, Depth, | ||||||||
| 314 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||||||
| 315 | } | ||||||||
| 316 | |||||||||
| 317 | bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, | ||||||||
| 318 | unsigned Depth, AssumptionCache *AC, | ||||||||
| 319 | const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 320 | bool UseInstrInfo) { | ||||||||
| 321 | KnownBits Known = | ||||||||
| 322 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||||||
| 323 | return Known.isNonNegative(); | ||||||||
| 324 | } | ||||||||
| 325 | |||||||||
| 326 | bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, | ||||||||
| 327 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 328 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 329 | if (auto *CI = dyn_cast<ConstantInt>(V)) | ||||||||
| 330 | return CI->getValue().isStrictlyPositive(); | ||||||||
| 331 | |||||||||
| 332 | // TODO: We'd doing two recursive queries here. We should factor this such | ||||||||
| 333 | // that only a single query is needed. | ||||||||
| 334 | return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) && | ||||||||
| 335 | isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); | ||||||||
| 336 | } | ||||||||
| 337 | |||||||||
| 338 | bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, | ||||||||
| 339 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 340 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 341 | KnownBits Known = | ||||||||
| 342 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); | ||||||||
| 343 | return Known.isNegative(); | ||||||||
| 344 | } | ||||||||
| 345 | |||||||||
| 346 | static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, | ||||||||
| 347 | const Query &Q); | ||||||||
| 348 | |||||||||
| 349 | bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, | ||||||||
| 350 | const DataLayout &DL, AssumptionCache *AC, | ||||||||
| 351 | const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 352 | bool UseInstrInfo) { | ||||||||
| 353 | return ::isKnownNonEqual(V1, V2, 0, | ||||||||
| 354 | Query(DL, AC, safeCxtI(V2, V1, CxtI), DT, | ||||||||
| 355 | UseInstrInfo, /*ORE=*/nullptr)); | ||||||||
| 356 | } | ||||||||
| 357 | |||||||||
| 358 | static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, | ||||||||
| 359 | const Query &Q); | ||||||||
| 360 | |||||||||
| 361 | bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, | ||||||||
| 362 | const DataLayout &DL, unsigned Depth, | ||||||||
| 363 | AssumptionCache *AC, const Instruction *CxtI, | ||||||||
| 364 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 365 | return ::MaskedValueIsZero( | ||||||||
| 366 | V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||||||
| 367 | } | ||||||||
| 368 | |||||||||
| 369 | static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 370 | unsigned Depth, const Query &Q); | ||||||||
| 371 | |||||||||
| 372 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, | ||||||||
| 373 | const Query &Q) { | ||||||||
| 374 | // FIXME: We currently have no way to represent the DemandedElts of a scalable | ||||||||
| 375 | // vector | ||||||||
| 376 | if (isa<ScalableVectorType>(V->getType())) | ||||||||
| 377 | return 1; | ||||||||
| 378 | |||||||||
| 379 | auto *FVTy = dyn_cast<FixedVectorType>(V->getType()); | ||||||||
| 380 | APInt DemandedElts = | ||||||||
| 381 | FVTy ? APInt::getAllOnesValue(FVTy->getNumElements()) : APInt(1, 1); | ||||||||
| 382 | return ComputeNumSignBits(V, DemandedElts, Depth, Q); | ||||||||
| 383 | } | ||||||||
| 384 | |||||||||
| 385 | unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, | ||||||||
| 386 | unsigned Depth, AssumptionCache *AC, | ||||||||
| 387 | const Instruction *CxtI, | ||||||||
| 388 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 389 | return ::ComputeNumSignBits( | ||||||||
| 390 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); | ||||||||
| 391 | } | ||||||||
| 392 | |||||||||
| 393 | static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, | ||||||||
| 394 | bool NSW, const APInt &DemandedElts, | ||||||||
| 395 | KnownBits &KnownOut, KnownBits &Known2, | ||||||||
| 396 | unsigned Depth, const Query &Q) { | ||||||||
| 397 | computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q); | ||||||||
| 398 | |||||||||
| 399 | // If one operand is unknown and we have no nowrap information, | ||||||||
| 400 | // the result will be unknown independently of the second operand. | ||||||||
| 401 | if (KnownOut.isUnknown() && !NSW) | ||||||||
| 402 | return; | ||||||||
| 403 | |||||||||
| 404 | computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 405 | KnownOut = KnownBits::computeForAddSub(Add, NSW, Known2, KnownOut); | ||||||||
| 406 | } | ||||||||
| 407 | |||||||||
| 408 | static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, | ||||||||
| 409 | const APInt &DemandedElts, KnownBits &Known, | ||||||||
| 410 | KnownBits &Known2, unsigned Depth, | ||||||||
| 411 | const Query &Q) { | ||||||||
| 412 | computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q); | ||||||||
| 413 | computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 414 | |||||||||
| 415 | bool isKnownNegative = false; | ||||||||
| 416 | bool isKnownNonNegative = false; | ||||||||
| 417 | // If the multiplication is known not to overflow, compute the sign bit. | ||||||||
| 418 | if (NSW) { | ||||||||
| 419 | if (Op0 == Op1) { | ||||||||
| 420 | // The product of a number with itself is non-negative. | ||||||||
| 421 | isKnownNonNegative = true; | ||||||||
| 422 | } else { | ||||||||
| 423 | bool isKnownNonNegativeOp1 = Known.isNonNegative(); | ||||||||
| 424 | bool isKnownNonNegativeOp0 = Known2.isNonNegative(); | ||||||||
| 425 | bool isKnownNegativeOp1 = Known.isNegative(); | ||||||||
| 426 | bool isKnownNegativeOp0 = Known2.isNegative(); | ||||||||
| 427 | // The product of two numbers with the same sign is non-negative. | ||||||||
| 428 | isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || | ||||||||
| 429 | (isKnownNonNegativeOp1 && isKnownNonNegativeOp0); | ||||||||
| 430 | // The product of a negative number and a non-negative number is either | ||||||||
| 431 | // negative or zero. | ||||||||
| 432 | if (!isKnownNonNegative) | ||||||||
| 433 | isKnownNegative = | ||||||||
| 434 | (isKnownNegativeOp1 && isKnownNonNegativeOp0 && | ||||||||
| 435 | Known2.isNonZero()) || | ||||||||
| 436 | (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero()); | ||||||||
| 437 | } | ||||||||
| 438 | } | ||||||||
| 439 | |||||||||
| 440 | Known = KnownBits::mul(Known, Known2); | ||||||||
| 441 | |||||||||
| 442 | // Only make use of no-wrap flags if we failed to compute the sign bit | ||||||||
| 443 | // directly. This matters if the multiplication always overflows, in | ||||||||
| 444 | // which case we prefer to follow the result of the direct computation, | ||||||||
| 445 | // though as the program is invoking undefined behaviour we can choose | ||||||||
| 446 | // whatever we like here. | ||||||||
| 447 | if (isKnownNonNegative && !Known.isNegative()) | ||||||||
| 448 | Known.makeNonNegative(); | ||||||||
| 449 | else if (isKnownNegative && !Known.isNonNegative()) | ||||||||
| 450 | Known.makeNegative(); | ||||||||
| 451 | } | ||||||||
| 452 | |||||||||
| 453 | void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, | ||||||||
| 454 | KnownBits &Known) { | ||||||||
| 455 | unsigned BitWidth = Known.getBitWidth(); | ||||||||
| 456 | unsigned NumRanges = Ranges.getNumOperands() / 2; | ||||||||
| 457 | assert(NumRanges >= 1)((void)0); | ||||||||
| 458 | |||||||||
| 459 | Known.Zero.setAllBits(); | ||||||||
| 460 | Known.One.setAllBits(); | ||||||||
| 461 | |||||||||
| 462 | for (unsigned i = 0; i < NumRanges; ++i) { | ||||||||
| 463 | ConstantInt *Lower = | ||||||||
| 464 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); | ||||||||
| 465 | ConstantInt *Upper = | ||||||||
| 466 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); | ||||||||
| 467 | ConstantRange Range(Lower->getValue(), Upper->getValue()); | ||||||||
| 468 | |||||||||
| 469 | // The first CommonPrefixBits of all values in Range are equal. | ||||||||
| 470 | unsigned CommonPrefixBits = | ||||||||
| 471 | (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros(); | ||||||||
| 472 | APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits); | ||||||||
| 473 | APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth); | ||||||||
| 474 | Known.One &= UnsignedMax & Mask; | ||||||||
| 475 | Known.Zero &= ~UnsignedMax & Mask; | ||||||||
| 476 | } | ||||||||
| 477 | } | ||||||||
| 478 | |||||||||
| 479 | static bool isEphemeralValueOf(const Instruction *I, const Value *E) { | ||||||||
| 480 | SmallVector<const Value *, 16> WorkSet(1, I); | ||||||||
| 481 | SmallPtrSet<const Value *, 32> Visited; | ||||||||
| 482 | SmallPtrSet<const Value *, 16> EphValues; | ||||||||
| 483 | |||||||||
| 484 | // The instruction defining an assumption's condition itself is always | ||||||||
| 485 | // considered ephemeral to that assumption (even if it has other | ||||||||
| 486 | // non-ephemeral users). See r246696's test case for an example. | ||||||||
| 487 | if (is_contained(I->operands(), E)) | ||||||||
| 488 | return true; | ||||||||
| 489 | |||||||||
| 490 | while (!WorkSet.empty()) { | ||||||||
| 491 | const Value *V = WorkSet.pop_back_val(); | ||||||||
| 492 | if (!Visited.insert(V).second) | ||||||||
| 493 | continue; | ||||||||
| 494 | |||||||||
| 495 | // If all uses of this value are ephemeral, then so is this value. | ||||||||
| 496 | if (llvm::all_of(V->users(), [&](const User *U) { | ||||||||
| 497 | return EphValues.count(U); | ||||||||
| 498 | })) { | ||||||||
| 499 | if (V == E) | ||||||||
| 500 | return true; | ||||||||
| 501 | |||||||||
| 502 | if (V == I || isSafeToSpeculativelyExecute(V)) { | ||||||||
| 503 | EphValues.insert(V); | ||||||||
| 504 | if (const User *U = dyn_cast<User>(V)) | ||||||||
| 505 | append_range(WorkSet, U->operands()); | ||||||||
| 506 | } | ||||||||
| 507 | } | ||||||||
| 508 | } | ||||||||
| 509 | |||||||||
| 510 | return false; | ||||||||
| 511 | } | ||||||||
| 512 | |||||||||
| 513 | // Is this an intrinsic that cannot be speculated but also cannot trap? | ||||||||
| 514 | bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { | ||||||||
| 515 | if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I)) | ||||||||
| 516 | return CI->isAssumeLikeIntrinsic(); | ||||||||
| 517 | |||||||||
| 518 | return false; | ||||||||
| 519 | } | ||||||||
| 520 | |||||||||
| 521 | bool llvm::isValidAssumeForContext(const Instruction *Inv, | ||||||||
| 522 | const Instruction *CxtI, | ||||||||
| 523 | const DominatorTree *DT) { | ||||||||
| 524 | // There are two restrictions on the use of an assume: | ||||||||
| 525 | // 1. The assume must dominate the context (or the control flow must | ||||||||
| 526 | // reach the assume whenever it reaches the context). | ||||||||
| 527 | // 2. The context must not be in the assume's set of ephemeral values | ||||||||
| 528 | // (otherwise we will use the assume to prove that the condition | ||||||||
| 529 | // feeding the assume is trivially true, thus causing the removal of | ||||||||
| 530 | // the assume). | ||||||||
| 531 | |||||||||
| 532 | if (Inv->getParent() == CxtI->getParent()) { | ||||||||
| 533 | // If Inv and CtxI are in the same block, check if the assume (Inv) is first | ||||||||
| 534 | // in the BB. | ||||||||
| 535 | if (Inv->comesBefore(CxtI)) | ||||||||
| 536 | return true; | ||||||||
| 537 | |||||||||
| 538 | // Don't let an assume affect itself - this would cause the problems | ||||||||
| 539 | // `isEphemeralValueOf` is trying to prevent, and it would also make | ||||||||
| 540 | // the loop below go out of bounds. | ||||||||
| 541 | if (Inv == CxtI) | ||||||||
| 542 | return false; | ||||||||
| 543 | |||||||||
| 544 | // The context comes first, but they're both in the same block. | ||||||||
| 545 | // Make sure there is nothing in between that might interrupt | ||||||||
| 546 | // the control flow, not even CxtI itself. | ||||||||
| 547 | // We limit the scan distance between the assume and its context instruction | ||||||||
| 548 | // to avoid a compile-time explosion. This limit is chosen arbitrarily, so | ||||||||
| 549 | // it can be adjusted if needed (could be turned into a cl::opt). | ||||||||
| 550 | unsigned ScanLimit = 15; | ||||||||
| 551 | for (BasicBlock::const_iterator I(CxtI), IE(Inv); I != IE; ++I) | ||||||||
| 552 | if (!isGuaranteedToTransferExecutionToSuccessor(&*I) || --ScanLimit == 0) | ||||||||
| 553 | return false; | ||||||||
| 554 | |||||||||
| 555 | return !isEphemeralValueOf(Inv, CxtI); | ||||||||
| 556 | } | ||||||||
| 557 | |||||||||
| 558 | // Inv and CxtI are in different blocks. | ||||||||
| 559 | if (DT) { | ||||||||
| 560 | if (DT->dominates(Inv, CxtI)) | ||||||||
| 561 | return true; | ||||||||
| 562 | } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) { | ||||||||
| 563 | // We don't have a DT, but this trivially dominates. | ||||||||
| 564 | return true; | ||||||||
| 565 | } | ||||||||
| 566 | |||||||||
| 567 | return false; | ||||||||
| 568 | } | ||||||||
| 569 | |||||||||
| 570 | static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) { | ||||||||
| 571 | // v u> y implies v != 0. | ||||||||
| 572 | if (Pred == ICmpInst::ICMP_UGT) | ||||||||
| 573 | return true; | ||||||||
| 574 | |||||||||
| 575 | // Special-case v != 0 to also handle v != null. | ||||||||
| 576 | if (Pred == ICmpInst::ICMP_NE) | ||||||||
| 577 | return match(RHS, m_Zero()); | ||||||||
| 578 | |||||||||
| 579 | // All other predicates - rely on generic ConstantRange handling. | ||||||||
| 580 | const APInt *C; | ||||||||
| 581 | if (!match(RHS, m_APInt(C))) | ||||||||
| 582 | return false; | ||||||||
| 583 | |||||||||
| 584 | ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C); | ||||||||
| 585 | return !TrueValues.contains(APInt::getNullValue(C->getBitWidth())); | ||||||||
| 586 | } | ||||||||
| 587 | |||||||||
| 588 | static bool isKnownNonZeroFromAssume(const Value *V, const Query &Q) { | ||||||||
| 589 | // Use of assumptions is context-sensitive. If we don't have a context, we | ||||||||
| 590 | // cannot use them! | ||||||||
| 591 | if (!Q.AC || !Q.CxtI) | ||||||||
| 592 | return false; | ||||||||
| 593 | |||||||||
| 594 | if (Q.CxtI && V->getType()->isPointerTy()) { | ||||||||
| 595 | SmallVector<Attribute::AttrKind, 2> AttrKinds{Attribute::NonNull}; | ||||||||
| 596 | if (!NullPointerIsDefined(Q.CxtI->getFunction(), | ||||||||
| 597 | V->getType()->getPointerAddressSpace())) | ||||||||
| 598 | AttrKinds.push_back(Attribute::Dereferenceable); | ||||||||
| 599 | |||||||||
| 600 | if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT, Q.AC)) | ||||||||
| 601 | return true; | ||||||||
| 602 | } | ||||||||
| 603 | |||||||||
| 604 | for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { | ||||||||
| 605 | if (!AssumeVH) | ||||||||
| 606 | continue; | ||||||||
| 607 | CallInst *I = cast<CallInst>(AssumeVH); | ||||||||
| 608 | assert(I->getFunction() == Q.CxtI->getFunction() &&((void)0) | ||||||||
| 609 | "Got assumption for the wrong function!")((void)0); | ||||||||
| 610 | |||||||||
| 611 | // Warning: This loop can end up being somewhat performance sensitive. | ||||||||
| 612 | // We're running this loop for once for each value queried resulting in a | ||||||||
| 613 | // runtime of ~O(#assumes * #values). | ||||||||
| 614 | |||||||||
| 615 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&((void)0) | ||||||||
| 616 | "must be an assume intrinsic")((void)0); | ||||||||
| 617 | |||||||||
| 618 | Value *RHS; | ||||||||
| 619 | CmpInst::Predicate Pred; | ||||||||
| 620 | auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V))); | ||||||||
| 621 | if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS)))) | ||||||||
| 622 | return false; | ||||||||
| 623 | |||||||||
| 624 | if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT)) | ||||||||
| 625 | return true; | ||||||||
| 626 | } | ||||||||
| 627 | |||||||||
| 628 | return false; | ||||||||
| 629 | } | ||||||||
| 630 | |||||||||
| 631 | static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known, | ||||||||
| 632 | unsigned Depth, const Query &Q) { | ||||||||
| 633 | // Use of assumptions is context-sensitive. If we don't have a context, we | ||||||||
| 634 | // cannot use them! | ||||||||
| 635 | if (!Q.AC || !Q.CxtI) | ||||||||
| 636 | return; | ||||||||
| 637 | |||||||||
| 638 | unsigned BitWidth = Known.getBitWidth(); | ||||||||
| 639 | |||||||||
| 640 | // Refine Known set if the pointer alignment is set by assume bundles. | ||||||||
| 641 | if (V->getType()->isPointerTy()) { | ||||||||
| 642 | if (RetainedKnowledge RK = getKnowledgeValidInContext( | ||||||||
| 643 | V, {Attribute::Alignment}, Q.CxtI, Q.DT, Q.AC)) { | ||||||||
| 644 | Known.Zero.setLowBits(Log2_32(RK.ArgValue)); | ||||||||
| 645 | } | ||||||||
| 646 | } | ||||||||
| 647 | |||||||||
| 648 | // Note that the patterns below need to be kept in sync with the code | ||||||||
| 649 | // in AssumptionCache::updateAffectedValues. | ||||||||
| 650 | |||||||||
| 651 | for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { | ||||||||
| 652 | if (!AssumeVH) | ||||||||
| 653 | continue; | ||||||||
| 654 | CallInst *I = cast<CallInst>(AssumeVH); | ||||||||
| 655 | assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&((void)0) | ||||||||
| 656 | "Got assumption for the wrong function!")((void)0); | ||||||||
| 657 | |||||||||
| 658 | // Warning: This loop can end up being somewhat performance sensitive. | ||||||||
| 659 | // We're running this loop for once for each value queried resulting in a | ||||||||
| 660 | // runtime of ~O(#assumes * #values). | ||||||||
| 661 | |||||||||
| 662 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&((void)0) | ||||||||
| 663 | "must be an assume intrinsic")((void)0); | ||||||||
| 664 | |||||||||
| 665 | Value *Arg = I->getArgOperand(0); | ||||||||
| 666 | |||||||||
| 667 | if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 668 | assert(BitWidth == 1 && "assume operand is not i1?")((void)0); | ||||||||
| 669 | Known.setAllOnes(); | ||||||||
| 670 | return; | ||||||||
| 671 | } | ||||||||
| 672 | if (match(Arg, m_Not(m_Specific(V))) && | ||||||||
| 673 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 674 | assert(BitWidth == 1 && "assume operand is not i1?")((void)0); | ||||||||
| 675 | Known.setAllZero(); | ||||||||
| 676 | return; | ||||||||
| 677 | } | ||||||||
| 678 | |||||||||
| 679 | // The remaining tests are all recursive, so bail out if we hit the limit. | ||||||||
| 680 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 681 | continue; | ||||||||
| 682 | |||||||||
| 683 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg); | ||||||||
| 684 | if (!Cmp) | ||||||||
| 685 | continue; | ||||||||
| 686 | |||||||||
| 687 | // We are attempting to compute known bits for the operands of an assume. | ||||||||
| 688 | // Do not try to use other assumptions for those recursive calls because | ||||||||
| 689 | // that can lead to mutual recursion and a compile-time explosion. | ||||||||
| 690 | // An example of the mutual recursion: computeKnownBits can call | ||||||||
| 691 | // isKnownNonZero which calls computeKnownBitsFromAssume (this function) | ||||||||
| 692 | // and so on. | ||||||||
| 693 | Query QueryNoAC = Q; | ||||||||
| 694 | QueryNoAC.AC = nullptr; | ||||||||
| 695 | |||||||||
| 696 | // Note that ptrtoint may change the bitwidth. | ||||||||
| 697 | Value *A, *B; | ||||||||
| 698 | auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V))); | ||||||||
| 699 | |||||||||
| 700 | CmpInst::Predicate Pred; | ||||||||
| 701 | uint64_t C; | ||||||||
| 702 | switch (Cmp->getPredicate()) { | ||||||||
| 703 | default: | ||||||||
| 704 | break; | ||||||||
| 705 | case ICmpInst::ICMP_EQ: | ||||||||
| 706 | // assume(v = a) | ||||||||
| 707 | if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 708 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 709 | KnownBits RHSKnown = | ||||||||
| 710 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 711 | Known.Zero |= RHSKnown.Zero; | ||||||||
| 712 | Known.One |= RHSKnown.One; | ||||||||
| 713 | // assume(v & b = a) | ||||||||
| 714 | } else if (match(Cmp, | ||||||||
| 715 | m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) && | ||||||||
| 716 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 717 | KnownBits RHSKnown = | ||||||||
| 718 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 719 | KnownBits MaskKnown = | ||||||||
| 720 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 721 | |||||||||
| 722 | // For those bits in the mask that are known to be one, we can propagate | ||||||||
| 723 | // known bits from the RHS to V. | ||||||||
| 724 | Known.Zero |= RHSKnown.Zero & MaskKnown.One; | ||||||||
| 725 | Known.One |= RHSKnown.One & MaskKnown.One; | ||||||||
| 726 | // assume(~(v & b) = a) | ||||||||
| 727 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), | ||||||||
| 728 | m_Value(A))) && | ||||||||
| 729 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 730 | KnownBits RHSKnown = | ||||||||
| 731 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 732 | KnownBits MaskKnown = | ||||||||
| 733 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 734 | |||||||||
| 735 | // For those bits in the mask that are known to be one, we can propagate | ||||||||
| 736 | // inverted known bits from the RHS to V. | ||||||||
| 737 | Known.Zero |= RHSKnown.One & MaskKnown.One; | ||||||||
| 738 | Known.One |= RHSKnown.Zero & MaskKnown.One; | ||||||||
| 739 | // assume(v | b = a) | ||||||||
| 740 | } else if (match(Cmp, | ||||||||
| 741 | m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) && | ||||||||
| 742 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 743 | KnownBits RHSKnown = | ||||||||
| 744 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 745 | KnownBits BKnown = | ||||||||
| 746 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 747 | |||||||||
| 748 | // For those bits in B that are known to be zero, we can propagate known | ||||||||
| 749 | // bits from the RHS to V. | ||||||||
| 750 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; | ||||||||
| 751 | Known.One |= RHSKnown.One & BKnown.Zero; | ||||||||
| 752 | // assume(~(v | b) = a) | ||||||||
| 753 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), | ||||||||
| 754 | m_Value(A))) && | ||||||||
| 755 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 756 | KnownBits RHSKnown = | ||||||||
| 757 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 758 | KnownBits BKnown = | ||||||||
| 759 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 760 | |||||||||
| 761 | // For those bits in B that are known to be zero, we can propagate | ||||||||
| 762 | // inverted known bits from the RHS to V. | ||||||||
| 763 | Known.Zero |= RHSKnown.One & BKnown.Zero; | ||||||||
| 764 | Known.One |= RHSKnown.Zero & BKnown.Zero; | ||||||||
| 765 | // assume(v ^ b = a) | ||||||||
| 766 | } else if (match(Cmp, | ||||||||
| 767 | m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) && | ||||||||
| 768 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 769 | KnownBits RHSKnown = | ||||||||
| 770 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 771 | KnownBits BKnown = | ||||||||
| 772 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 773 | |||||||||
| 774 | // For those bits in B that are known to be zero, we can propagate known | ||||||||
| 775 | // bits from the RHS to V. For those bits in B that are known to be one, | ||||||||
| 776 | // we can propagate inverted known bits from the RHS to V. | ||||||||
| 777 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; | ||||||||
| 778 | Known.One |= RHSKnown.One & BKnown.Zero; | ||||||||
| 779 | Known.Zero |= RHSKnown.One & BKnown.One; | ||||||||
| 780 | Known.One |= RHSKnown.Zero & BKnown.One; | ||||||||
| 781 | // assume(~(v ^ b) = a) | ||||||||
| 782 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), | ||||||||
| 783 | m_Value(A))) && | ||||||||
| 784 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 785 | KnownBits RHSKnown = | ||||||||
| 786 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 787 | KnownBits BKnown = | ||||||||
| 788 | computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 789 | |||||||||
| 790 | // For those bits in B that are known to be zero, we can propagate | ||||||||
| 791 | // inverted known bits from the RHS to V. For those bits in B that are | ||||||||
| 792 | // known to be one, we can propagate known bits from the RHS to V. | ||||||||
| 793 | Known.Zero |= RHSKnown.One & BKnown.Zero; | ||||||||
| 794 | Known.One |= RHSKnown.Zero & BKnown.Zero; | ||||||||
| 795 | Known.Zero |= RHSKnown.Zero & BKnown.One; | ||||||||
| 796 | Known.One |= RHSKnown.One & BKnown.One; | ||||||||
| 797 | // assume(v << c = a) | ||||||||
| 798 | } else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), | ||||||||
| 799 | m_Value(A))) && | ||||||||
| 800 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||||||
| 801 | KnownBits RHSKnown = | ||||||||
| 802 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 803 | |||||||||
| 804 | // For those bits in RHS that are known, we can propagate them to known | ||||||||
| 805 | // bits in V shifted to the right by C. | ||||||||
| 806 | RHSKnown.Zero.lshrInPlace(C); | ||||||||
| 807 | Known.Zero |= RHSKnown.Zero; | ||||||||
| 808 | RHSKnown.One.lshrInPlace(C); | ||||||||
| 809 | Known.One |= RHSKnown.One; | ||||||||
| 810 | // assume(~(v << c) = a) | ||||||||
| 811 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), | ||||||||
| 812 | m_Value(A))) && | ||||||||
| 813 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||||||
| 814 | KnownBits RHSKnown = | ||||||||
| 815 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 816 | // For those bits in RHS that are known, we can propagate them inverted | ||||||||
| 817 | // to known bits in V shifted to the right by C. | ||||||||
| 818 | RHSKnown.One.lshrInPlace(C); | ||||||||
| 819 | Known.Zero |= RHSKnown.One; | ||||||||
| 820 | RHSKnown.Zero.lshrInPlace(C); | ||||||||
| 821 | Known.One |= RHSKnown.Zero; | ||||||||
| 822 | // assume(v >> c = a) | ||||||||
| 823 | } else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), | ||||||||
| 824 | m_Value(A))) && | ||||||||
| 825 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||||||
| 826 | KnownBits RHSKnown = | ||||||||
| 827 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 828 | // For those bits in RHS that are known, we can propagate them to known | ||||||||
| 829 | // bits in V shifted to the right by C. | ||||||||
| 830 | Known.Zero |= RHSKnown.Zero << C; | ||||||||
| 831 | Known.One |= RHSKnown.One << C; | ||||||||
| 832 | // assume(~(v >> c) = a) | ||||||||
| 833 | } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))), | ||||||||
| 834 | m_Value(A))) && | ||||||||
| 835 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) { | ||||||||
| 836 | KnownBits RHSKnown = | ||||||||
| 837 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 838 | // For those bits in RHS that are known, we can propagate them inverted | ||||||||
| 839 | // to known bits in V shifted to the right by C. | ||||||||
| 840 | Known.Zero |= RHSKnown.One << C; | ||||||||
| 841 | Known.One |= RHSKnown.Zero << C; | ||||||||
| 842 | } | ||||||||
| 843 | break; | ||||||||
| 844 | case ICmpInst::ICMP_SGE: | ||||||||
| 845 | // assume(v >=_s c) where c is non-negative | ||||||||
| 846 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 847 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 848 | KnownBits RHSKnown = | ||||||||
| 849 | computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 850 | |||||||||
| 851 | if (RHSKnown.isNonNegative()) { | ||||||||
| 852 | // We know that the sign bit is zero. | ||||||||
| 853 | Known.makeNonNegative(); | ||||||||
| 854 | } | ||||||||
| 855 | } | ||||||||
| 856 | break; | ||||||||
| 857 | case ICmpInst::ICMP_SGT: | ||||||||
| 858 | // assume(v >_s c) where c is at least -1. | ||||||||
| 859 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 860 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 861 | KnownBits RHSKnown = | ||||||||
| 862 | computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 863 | |||||||||
| 864 | if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) { | ||||||||
| 865 | // We know that the sign bit is zero. | ||||||||
| 866 | Known.makeNonNegative(); | ||||||||
| 867 | } | ||||||||
| 868 | } | ||||||||
| 869 | break; | ||||||||
| 870 | case ICmpInst::ICMP_SLE: | ||||||||
| 871 | // assume(v <=_s c) where c is negative | ||||||||
| 872 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 873 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 874 | KnownBits RHSKnown = | ||||||||
| 875 | computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 876 | |||||||||
| 877 | if (RHSKnown.isNegative()) { | ||||||||
| 878 | // We know that the sign bit is one. | ||||||||
| 879 | Known.makeNegative(); | ||||||||
| 880 | } | ||||||||
| 881 | } | ||||||||
| 882 | break; | ||||||||
| 883 | case ICmpInst::ICMP_SLT: | ||||||||
| 884 | // assume(v <_s c) where c is non-positive | ||||||||
| 885 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 886 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 887 | KnownBits RHSKnown = | ||||||||
| 888 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 889 | |||||||||
| 890 | if (RHSKnown.isZero() || RHSKnown.isNegative()) { | ||||||||
| 891 | // We know that the sign bit is one. | ||||||||
| 892 | Known.makeNegative(); | ||||||||
| 893 | } | ||||||||
| 894 | } | ||||||||
| 895 | break; | ||||||||
| 896 | case ICmpInst::ICMP_ULE: | ||||||||
| 897 | // assume(v <=_u c) | ||||||||
| 898 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 899 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 900 | KnownBits RHSKnown = | ||||||||
| 901 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 902 | |||||||||
| 903 | // Whatever high bits in c are zero are known to be zero. | ||||||||
| 904 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); | ||||||||
| 905 | } | ||||||||
| 906 | break; | ||||||||
| 907 | case ICmpInst::ICMP_ULT: | ||||||||
| 908 | // assume(v <_u c) | ||||||||
| 909 | if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) && | ||||||||
| 910 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { | ||||||||
| 911 | KnownBits RHSKnown = | ||||||||
| 912 | computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth); | ||||||||
| 913 | |||||||||
| 914 | // If the RHS is known zero, then this assumption must be wrong (nothing | ||||||||
| 915 | // is unsigned less than zero). Signal a conflict and get out of here. | ||||||||
| 916 | if (RHSKnown.isZero()) { | ||||||||
| 917 | Known.Zero.setAllBits(); | ||||||||
| 918 | Known.One.setAllBits(); | ||||||||
| 919 | break; | ||||||||
| 920 | } | ||||||||
| 921 | |||||||||
| 922 | // Whatever high bits in c are zero are known to be zero (if c is a power | ||||||||
| 923 | // of 2, then one more). | ||||||||
| 924 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, QueryNoAC)) | ||||||||
| 925 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1); | ||||||||
| 926 | else | ||||||||
| 927 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); | ||||||||
| 928 | } | ||||||||
| 929 | break; | ||||||||
| 930 | } | ||||||||
| 931 | } | ||||||||
| 932 | |||||||||
| 933 | // If assumptions conflict with each other or previous known bits, then we | ||||||||
| 934 | // have a logical fallacy. It's possible that the assumption is not reachable, | ||||||||
| 935 | // so this isn't a real bug. On the other hand, the program may have undefined | ||||||||
| 936 | // behavior, or we might have a bug in the compiler. We can't assert/crash, so | ||||||||
| 937 | // clear out the known bits, try to warn the user, and hope for the best. | ||||||||
| 938 | if (Known.Zero.intersects(Known.One)) { | ||||||||
| 939 | Known.resetAll(); | ||||||||
| 940 | |||||||||
| 941 | if (Q.ORE) | ||||||||
| 942 | Q.ORE->emit([&]() { | ||||||||
| 943 | auto *CxtI = const_cast<Instruction *>(Q.CxtI); | ||||||||
| 944 | return OptimizationRemarkAnalysis("value-tracking", "BadAssumption", | ||||||||
| 945 | CxtI) | ||||||||
| 946 | << "Detected conflicting code assumptions. Program may " | ||||||||
| 947 | "have undefined behavior, or compiler may have " | ||||||||
| 948 | "internal error."; | ||||||||
| 949 | }); | ||||||||
| 950 | } | ||||||||
| 951 | } | ||||||||
| 952 | |||||||||
| 953 | /// Compute known bits from a shift operator, including those with a | ||||||||
| 954 | /// non-constant shift amount. Known is the output of this function. Known2 is a | ||||||||
| 955 | /// pre-allocated temporary with the same bit width as Known and on return | ||||||||
| 956 | /// contains the known bit of the shift value source. KF is an | ||||||||
| 957 | /// operator-specific function that, given the known-bits and a shift amount, | ||||||||
| 958 | /// compute the implied known-bits of the shift operator's result respectively | ||||||||
| 959 | /// for that shift amount. The results from calling KF are conservatively | ||||||||
| 960 | /// combined for all permitted shift amounts. | ||||||||
| 961 | static void computeKnownBitsFromShiftOperator( | ||||||||
| 962 | const Operator *I, const APInt &DemandedElts, KnownBits &Known, | ||||||||
| 963 | KnownBits &Known2, unsigned Depth, const Query &Q, | ||||||||
| 964 | function_ref<KnownBits(const KnownBits &, const KnownBits &)> KF) { | ||||||||
| 965 | unsigned BitWidth = Known.getBitWidth(); | ||||||||
| 966 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 967 | computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); | ||||||||
| 968 | |||||||||
| 969 | // Note: We cannot use Known.Zero.getLimitedValue() here, because if | ||||||||
| 970 | // BitWidth > 64 and any upper bits are known, we'll end up returning the | ||||||||
| 971 | // limit value (which implies all bits are known). | ||||||||
| 972 | uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue(); | ||||||||
| 973 | uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue(); | ||||||||
| 974 | bool ShiftAmtIsConstant = Known.isConstant(); | ||||||||
| 975 | bool MaxShiftAmtIsOutOfRange = Known.getMaxValue().uge(BitWidth); | ||||||||
| 976 | |||||||||
| 977 | if (ShiftAmtIsConstant) { | ||||||||
| 978 | Known = KF(Known2, Known); | ||||||||
| 979 | |||||||||
| 980 | // If the known bits conflict, this must be an overflowing left shift, so | ||||||||
| 981 | // the shift result is poison. We can return anything we want. Choose 0 for | ||||||||
| 982 | // the best folding opportunity. | ||||||||
| 983 | if (Known.hasConflict()) | ||||||||
| 984 | Known.setAllZero(); | ||||||||
| 985 | |||||||||
| 986 | return; | ||||||||
| 987 | } | ||||||||
| 988 | |||||||||
| 989 | // If the shift amount could be greater than or equal to the bit-width of the | ||||||||
| 990 | // LHS, the value could be poison, but bail out because the check below is | ||||||||
| 991 | // expensive. | ||||||||
| 992 | // TODO: Should we just carry on? | ||||||||
| 993 | if (MaxShiftAmtIsOutOfRange) { | ||||||||
| 994 | Known.resetAll(); | ||||||||
| 995 | return; | ||||||||
| 996 | } | ||||||||
| 997 | |||||||||
| 998 | // It would be more-clearly correct to use the two temporaries for this | ||||||||
| 999 | // calculation. Reusing the APInts here to prevent unnecessary allocations. | ||||||||
| 1000 | Known.resetAll(); | ||||||||
| 1001 | |||||||||
| 1002 | // If we know the shifter operand is nonzero, we can sometimes infer more | ||||||||
| 1003 | // known bits. However this is expensive to compute, so be lazy about it and | ||||||||
| 1004 | // only compute it when absolutely necessary. | ||||||||
| 1005 | Optional<bool> ShifterOperandIsNonZero; | ||||||||
| 1006 | |||||||||
| 1007 | // Early exit if we can't constrain any well-defined shift amount. | ||||||||
| 1008 | if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) && | ||||||||
| 1009 | !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) { | ||||||||
| 1010 | ShifterOperandIsNonZero = | ||||||||
| 1011 | isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q); | ||||||||
| 1012 | if (!*ShifterOperandIsNonZero) | ||||||||
| 1013 | return; | ||||||||
| 1014 | } | ||||||||
| 1015 | |||||||||
| 1016 | Known.Zero.setAllBits(); | ||||||||
| 1017 | Known.One.setAllBits(); | ||||||||
| 1018 | for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) { | ||||||||
| 1019 | // Combine the shifted known input bits only for those shift amounts | ||||||||
| 1020 | // compatible with its known constraints. | ||||||||
| 1021 | if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt) | ||||||||
| 1022 | continue; | ||||||||
| 1023 | if ((ShiftAmt | ShiftAmtKO) != ShiftAmt) | ||||||||
| 1024 | continue; | ||||||||
| 1025 | // If we know the shifter is nonzero, we may be able to infer more known | ||||||||
| 1026 | // bits. This check is sunk down as far as possible to avoid the expensive | ||||||||
| 1027 | // call to isKnownNonZero if the cheaper checks above fail. | ||||||||
| 1028 | if (ShiftAmt == 0) { | ||||||||
| 1029 | if (!ShifterOperandIsNonZero.hasValue()) | ||||||||
| 1030 | ShifterOperandIsNonZero = | ||||||||
| 1031 | isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q); | ||||||||
| 1032 | if (*ShifterOperandIsNonZero) | ||||||||
| 1033 | continue; | ||||||||
| 1034 | } | ||||||||
| 1035 | |||||||||
| 1036 | Known = KnownBits::commonBits( | ||||||||
| 1037 | Known, KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt)))); | ||||||||
| 1038 | } | ||||||||
| 1039 | |||||||||
| 1040 | // If the known bits conflict, the result is poison. Return a 0 and hope the | ||||||||
| 1041 | // caller can further optimize that. | ||||||||
| 1042 | if (Known.hasConflict()) | ||||||||
| 1043 | Known.setAllZero(); | ||||||||
| 1044 | } | ||||||||
| 1045 | |||||||||
| 1046 | static void computeKnownBitsFromOperator(const Operator *I, | ||||||||
| 1047 | const APInt &DemandedElts, | ||||||||
| 1048 | KnownBits &Known, unsigned Depth, | ||||||||
| 1049 | const Query &Q) { | ||||||||
| 1050 | unsigned BitWidth = Known.getBitWidth(); | ||||||||
| 1051 | |||||||||
| 1052 | KnownBits Known2(BitWidth); | ||||||||
| 1053 | switch (I->getOpcode()) { | ||||||||
| 1054 | default: break; | ||||||||
| 1055 | case Instruction::Load: | ||||||||
| 1056 | if (MDNode *MD = | ||||||||
| 1057 | Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range)) | ||||||||
| 1058 | computeKnownBitsFromRangeMetadata(*MD, Known); | ||||||||
| 1059 | break; | ||||||||
| 1060 | case Instruction::And: { | ||||||||
| 1061 | // If either the LHS or the RHS are Zero, the result is zero. | ||||||||
| 1062 | computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); | ||||||||
| 1063 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1064 | |||||||||
| 1065 | Known &= Known2; | ||||||||
| 1066 | |||||||||
| 1067 | // and(x, add (x, -1)) is a common idiom that always clears the low bit; | ||||||||
| 1068 | // here we handle the more general case of adding any odd number by | ||||||||
| 1069 | // matching the form add(x, add(x, y)) where y is odd. | ||||||||
| 1070 | // TODO: This could be generalized to clearing any bit set in y where the | ||||||||
| 1071 | // following bit is known to be unset in y. | ||||||||
| 1072 | Value *X = nullptr, *Y = nullptr; | ||||||||
| 1073 | if (!Known.Zero[0] && !Known.One[0] && | ||||||||
| 1074 | match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) { | ||||||||
| 1075 | Known2.resetAll(); | ||||||||
| 1076 | computeKnownBits(Y, DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1077 | if (Known2.countMinTrailingOnes() > 0) | ||||||||
| 1078 | Known.Zero.setBit(0); | ||||||||
| 1079 | } | ||||||||
| 1080 | break; | ||||||||
| 1081 | } | ||||||||
| 1082 | case Instruction::Or: | ||||||||
| 1083 | computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); | ||||||||
| 1084 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1085 | |||||||||
| 1086 | Known |= Known2; | ||||||||
| 1087 | break; | ||||||||
| 1088 | case Instruction::Xor: | ||||||||
| 1089 | computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); | ||||||||
| 1090 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1091 | |||||||||
| 1092 | Known ^= Known2; | ||||||||
| 1093 | break; | ||||||||
| 1094 | case Instruction::Mul: { | ||||||||
| 1095 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||||||
| 1096 | computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts, | ||||||||
| 1097 | Known, Known2, Depth, Q); | ||||||||
| 1098 | break; | ||||||||
| 1099 | } | ||||||||
| 1100 | case Instruction::UDiv: { | ||||||||
| 1101 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1102 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1103 | Known = KnownBits::udiv(Known, Known2); | ||||||||
| 1104 | break; | ||||||||
| 1105 | } | ||||||||
| 1106 | case Instruction::Select: { | ||||||||
| 1107 | const Value *LHS = nullptr, *RHS = nullptr; | ||||||||
| 1108 | SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; | ||||||||
| 1109 | if (SelectPatternResult::isMinOrMax(SPF)) { | ||||||||
| 1110 | computeKnownBits(RHS, Known, Depth + 1, Q); | ||||||||
| 1111 | computeKnownBits(LHS, Known2, Depth + 1, Q); | ||||||||
| 1112 | switch (SPF) { | ||||||||
| 1113 | default: | ||||||||
| 1114 | llvm_unreachable("Unhandled select pattern flavor!")__builtin_unreachable(); | ||||||||
| 1115 | case SPF_SMAX: | ||||||||
| 1116 | Known = KnownBits::smax(Known, Known2); | ||||||||
| 1117 | break; | ||||||||
| 1118 | case SPF_SMIN: | ||||||||
| 1119 | Known = KnownBits::smin(Known, Known2); | ||||||||
| 1120 | break; | ||||||||
| 1121 | case SPF_UMAX: | ||||||||
| 1122 | Known = KnownBits::umax(Known, Known2); | ||||||||
| 1123 | break; | ||||||||
| 1124 | case SPF_UMIN: | ||||||||
| 1125 | Known = KnownBits::umin(Known, Known2); | ||||||||
| 1126 | break; | ||||||||
| 1127 | } | ||||||||
| 1128 | break; | ||||||||
| 1129 | } | ||||||||
| 1130 | |||||||||
| 1131 | computeKnownBits(I->getOperand(2), Known, Depth + 1, Q); | ||||||||
| 1132 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1133 | |||||||||
| 1134 | // Only known if known in both the LHS and RHS. | ||||||||
| 1135 | Known = KnownBits::commonBits(Known, Known2); | ||||||||
| 1136 | |||||||||
| 1137 | if (SPF == SPF_ABS) { | ||||||||
| 1138 | // RHS from matchSelectPattern returns the negation part of abs pattern. | ||||||||
| 1139 | // If the negate has an NSW flag we can assume the sign bit of the result | ||||||||
| 1140 | // will be 0 because that makes abs(INT_MIN) undefined. | ||||||||
| 1141 | if (match(RHS, m_Neg(m_Specific(LHS))) && | ||||||||
| 1142 | Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS))) | ||||||||
| 1143 | Known.Zero.setSignBit(); | ||||||||
| 1144 | } | ||||||||
| 1145 | |||||||||
| 1146 | break; | ||||||||
| 1147 | } | ||||||||
| 1148 | case Instruction::FPTrunc: | ||||||||
| 1149 | case Instruction::FPExt: | ||||||||
| 1150 | case Instruction::FPToUI: | ||||||||
| 1151 | case Instruction::FPToSI: | ||||||||
| 1152 | case Instruction::SIToFP: | ||||||||
| 1153 | case Instruction::UIToFP: | ||||||||
| 1154 | break; // Can't work with floating point. | ||||||||
| 1155 | case Instruction::PtrToInt: | ||||||||
| 1156 | case Instruction::IntToPtr: | ||||||||
| 1157 | // Fall through and handle them the same as zext/trunc. | ||||||||
| 1158 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||
| 1159 | case Instruction::ZExt: | ||||||||
| 1160 | case Instruction::Trunc: { | ||||||||
| 1161 | Type *SrcTy = I->getOperand(0)->getType(); | ||||||||
| 1162 | |||||||||
| 1163 | unsigned SrcBitWidth; | ||||||||
| 1164 | // Note that we handle pointer operands here because of inttoptr/ptrtoint | ||||||||
| 1165 | // which fall through here. | ||||||||
| 1166 | Type *ScalarTy = SrcTy->getScalarType(); | ||||||||
| 1167 | SrcBitWidth = ScalarTy->isPointerTy() ? | ||||||||
| 1168 | Q.DL.getPointerTypeSizeInBits(ScalarTy) : | ||||||||
| 1169 | Q.DL.getTypeSizeInBits(ScalarTy); | ||||||||
| 1170 | |||||||||
| 1171 | assert(SrcBitWidth && "SrcBitWidth can't be zero")((void)0); | ||||||||
| 1172 | Known = Known.anyextOrTrunc(SrcBitWidth); | ||||||||
| 1173 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1174 | Known = Known.zextOrTrunc(BitWidth); | ||||||||
| 1175 | break; | ||||||||
| 1176 | } | ||||||||
| 1177 | case Instruction::BitCast: { | ||||||||
| 1178 | Type *SrcTy = I->getOperand(0)->getType(); | ||||||||
| 1179 | if (SrcTy->isIntOrPtrTy() && | ||||||||
| 1180 | // TODO: For now, not handling conversions like: | ||||||||
| 1181 | // (bitcast i64 %x to <2 x i32>) | ||||||||
| 1182 | !I->getType()->isVectorTy()) { | ||||||||
| 1183 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1184 | break; | ||||||||
| 1185 | } | ||||||||
| 1186 | |||||||||
| 1187 | // Handle cast from vector integer type to scalar or vector integer. | ||||||||
| 1188 | auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy); | ||||||||
| 1189 | if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() || | ||||||||
| 1190 | !I->getType()->isIntOrIntVectorTy()) | ||||||||
| 1191 | break; | ||||||||
| 1192 | |||||||||
| 1193 | // Look through a cast from narrow vector elements to wider type. | ||||||||
| 1194 | // Examples: v4i32 -> v2i64, v3i8 -> v24 | ||||||||
| 1195 | unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits(); | ||||||||
| 1196 | if (BitWidth % SubBitWidth == 0) { | ||||||||
| 1197 | // Known bits are automatically intersected across demanded elements of a | ||||||||
| 1198 | // vector. So for example, if a bit is computed as known zero, it must be | ||||||||
| 1199 | // zero across all demanded elements of the vector. | ||||||||
| 1200 | // | ||||||||
| 1201 | // For this bitcast, each demanded element of the output is sub-divided | ||||||||
| 1202 | // across a set of smaller vector elements in the source vector. To get | ||||||||
| 1203 | // the known bits for an entire element of the output, compute the known | ||||||||
| 1204 | // bits for each sub-element sequentially. This is done by shifting the | ||||||||
| 1205 | // one-set-bit demanded elements parameter across the sub-elements for | ||||||||
| 1206 | // consecutive calls to computeKnownBits. We are using the demanded | ||||||||
| 1207 | // elements parameter as a mask operator. | ||||||||
| 1208 | // | ||||||||
| 1209 | // The known bits of each sub-element are then inserted into place | ||||||||
| 1210 | // (dependent on endian) to form the full result of known bits. | ||||||||
| 1211 | unsigned NumElts = DemandedElts.getBitWidth(); | ||||||||
| 1212 | unsigned SubScale = BitWidth / SubBitWidth; | ||||||||
| 1213 | APInt SubDemandedElts = APInt::getNullValue(NumElts * SubScale); | ||||||||
| 1214 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||
| 1215 | if (DemandedElts[i]) | ||||||||
| 1216 | SubDemandedElts.setBit(i * SubScale); | ||||||||
| 1217 | } | ||||||||
| 1218 | |||||||||
| 1219 | KnownBits KnownSrc(SubBitWidth); | ||||||||
| 1220 | for (unsigned i = 0; i != SubScale; ++i) { | ||||||||
| 1221 | computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, | ||||||||
| 1222 | Depth + 1, Q); | ||||||||
| 1223 | unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i; | ||||||||
| 1224 | Known.insertBits(KnownSrc, ShiftElt * SubBitWidth); | ||||||||
| 1225 | } | ||||||||
| 1226 | } | ||||||||
| 1227 | break; | ||||||||
| 1228 | } | ||||||||
| 1229 | case Instruction::SExt: { | ||||||||
| 1230 | // Compute the bits in the result that are not present in the input. | ||||||||
| 1231 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); | ||||||||
| 1232 | |||||||||
| 1233 | Known = Known.trunc(SrcBitWidth); | ||||||||
| 1234 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1235 | // If the sign bit of the input is known set or clear, then we know the | ||||||||
| 1236 | // top bits of the result. | ||||||||
| 1237 | Known = Known.sext(BitWidth); | ||||||||
| 1238 | break; | ||||||||
| 1239 | } | ||||||||
| 1240 | case Instruction::Shl: { | ||||||||
| 1241 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||||||
| 1242 | auto KF = [NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt) { | ||||||||
| 1243 | KnownBits Result = KnownBits::shl(KnownVal, KnownAmt); | ||||||||
| 1244 | // If this shift has "nsw" keyword, then the result is either a poison | ||||||||
| 1245 | // value or has the same sign bit as the first operand. | ||||||||
| 1246 | if (NSW) { | ||||||||
| 1247 | if (KnownVal.Zero.isSignBitSet()) | ||||||||
| 1248 | Result.Zero.setSignBit(); | ||||||||
| 1249 | if (KnownVal.One.isSignBitSet()) | ||||||||
| 1250 | Result.One.setSignBit(); | ||||||||
| 1251 | } | ||||||||
| 1252 | return Result; | ||||||||
| 1253 | }; | ||||||||
| 1254 | computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, | ||||||||
| 1255 | KF); | ||||||||
| 1256 | // Trailing zeros of a right-shifted constant never decrease. | ||||||||
| 1257 | const APInt *C; | ||||||||
| 1258 | if (match(I->getOperand(0), m_APInt(C))) | ||||||||
| 1259 | Known.Zero.setLowBits(C->countTrailingZeros()); | ||||||||
| 1260 | break; | ||||||||
| 1261 | } | ||||||||
| 1262 | case Instruction::LShr: { | ||||||||
| 1263 | auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt) { | ||||||||
| 1264 | return KnownBits::lshr(KnownVal, KnownAmt); | ||||||||
| 1265 | }; | ||||||||
| 1266 | computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, | ||||||||
| 1267 | KF); | ||||||||
| 1268 | // Leading zeros of a left-shifted constant never decrease. | ||||||||
| 1269 | const APInt *C; | ||||||||
| 1270 | if (match(I->getOperand(0), m_APInt(C))) | ||||||||
| 1271 | Known.Zero.setHighBits(C->countLeadingZeros()); | ||||||||
| 1272 | break; | ||||||||
| 1273 | } | ||||||||
| 1274 | case Instruction::AShr: { | ||||||||
| 1275 | auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt) { | ||||||||
| 1276 | return KnownBits::ashr(KnownVal, KnownAmt); | ||||||||
| 1277 | }; | ||||||||
| 1278 | computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, | ||||||||
| 1279 | KF); | ||||||||
| 1280 | break; | ||||||||
| 1281 | } | ||||||||
| 1282 | case Instruction::Sub: { | ||||||||
| 1283 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||||||
| 1284 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, | ||||||||
| 1285 | DemandedElts, Known, Known2, Depth, Q); | ||||||||
| 1286 | break; | ||||||||
| 1287 | } | ||||||||
| 1288 | case Instruction::Add: { | ||||||||
| 1289 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); | ||||||||
| 1290 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, | ||||||||
| 1291 | DemandedElts, Known, Known2, Depth, Q); | ||||||||
| 1292 | break; | ||||||||
| 1293 | } | ||||||||
| 1294 | case Instruction::SRem: | ||||||||
| 1295 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1296 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1297 | Known = KnownBits::srem(Known, Known2); | ||||||||
| 1298 | break; | ||||||||
| 1299 | |||||||||
| 1300 | case Instruction::URem: | ||||||||
| 1301 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1302 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1303 | Known = KnownBits::urem(Known, Known2); | ||||||||
| 1304 | break; | ||||||||
| 1305 | case Instruction::Alloca: | ||||||||
| 1306 | Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign())); | ||||||||
| 1307 | break; | ||||||||
| 1308 | case Instruction::GetElementPtr: { | ||||||||
| 1309 | // Analyze all of the subscripts of this getelementptr instruction | ||||||||
| 1310 | // to determine if we can prove known low zero bits. | ||||||||
| 1311 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1312 | // Accumulate the constant indices in a separate variable | ||||||||
| 1313 | // to minimize the number of calls to computeForAddSub. | ||||||||
| 1314 | APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true); | ||||||||
| 1315 | |||||||||
| 1316 | gep_type_iterator GTI = gep_type_begin(I); | ||||||||
| 1317 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { | ||||||||
| 1318 | // TrailZ can only become smaller, short-circuit if we hit zero. | ||||||||
| 1319 | if (Known.isUnknown()) | ||||||||
| 1320 | break; | ||||||||
| 1321 | |||||||||
| 1322 | Value *Index = I->getOperand(i); | ||||||||
| 1323 | |||||||||
| 1324 | // Handle case when index is zero. | ||||||||
| 1325 | Constant *CIndex = dyn_cast<Constant>(Index); | ||||||||
| 1326 | if (CIndex && CIndex->isZeroValue()) | ||||||||
| 1327 | continue; | ||||||||
| 1328 | |||||||||
| 1329 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||||||
| 1330 | // Handle struct member offset arithmetic. | ||||||||
| 1331 | |||||||||
| 1332 | assert(CIndex &&((void)0) | ||||||||
| 1333 | "Access to structure field must be known at compile time")((void)0); | ||||||||
| 1334 | |||||||||
| 1335 | if (CIndex->getType()->isVectorTy()) | ||||||||
| 1336 | Index = CIndex->getSplatValue(); | ||||||||
| 1337 | |||||||||
| 1338 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); | ||||||||
| 1339 | const StructLayout *SL = Q.DL.getStructLayout(STy); | ||||||||
| 1340 | uint64_t Offset = SL->getElementOffset(Idx); | ||||||||
| 1341 | AccConstIndices += Offset; | ||||||||
| 1342 | continue; | ||||||||
| 1343 | } | ||||||||
| 1344 | |||||||||
| 1345 | // Handle array index arithmetic. | ||||||||
| 1346 | Type *IndexedTy = GTI.getIndexedType(); | ||||||||
| 1347 | if (!IndexedTy->isSized()) { | ||||||||
| 1348 | Known.resetAll(); | ||||||||
| 1349 | break; | ||||||||
| 1350 | } | ||||||||
| 1351 | |||||||||
| 1352 | unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits(); | ||||||||
| 1353 | KnownBits IndexBits(IndexBitWidth); | ||||||||
| 1354 | computeKnownBits(Index, IndexBits, Depth + 1, Q); | ||||||||
| 1355 | TypeSize IndexTypeSize = Q.DL.getTypeAllocSize(IndexedTy); | ||||||||
| 1356 | uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinSize(); | ||||||||
| 1357 | KnownBits ScalingFactor(IndexBitWidth); | ||||||||
| 1358 | // Multiply by current sizeof type. | ||||||||
| 1359 | // &A[i] == A + i * sizeof(*A[i]). | ||||||||
| 1360 | if (IndexTypeSize.isScalable()) { | ||||||||
| 1361 | // For scalable types the only thing we know about sizeof is | ||||||||
| 1362 | // that this is a multiple of the minimum size. | ||||||||
| 1363 | ScalingFactor.Zero.setLowBits(countTrailingZeros(TypeSizeInBytes)); | ||||||||
| 1364 | } else if (IndexBits.isConstant()) { | ||||||||
| 1365 | APInt IndexConst = IndexBits.getConstant(); | ||||||||
| 1366 | APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes); | ||||||||
| 1367 | IndexConst *= ScalingFactor; | ||||||||
| 1368 | AccConstIndices += IndexConst.sextOrTrunc(BitWidth); | ||||||||
| 1369 | continue; | ||||||||
| 1370 | } else { | ||||||||
| 1371 | ScalingFactor = | ||||||||
| 1372 | KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes)); | ||||||||
| 1373 | } | ||||||||
| 1374 | IndexBits = KnownBits::mul(IndexBits, ScalingFactor); | ||||||||
| 1375 | |||||||||
| 1376 | // If the offsets have a different width from the pointer, according | ||||||||
| 1377 | // to the language reference we need to sign-extend or truncate them | ||||||||
| 1378 | // to the width of the pointer. | ||||||||
| 1379 | IndexBits = IndexBits.sextOrTrunc(BitWidth); | ||||||||
| 1380 | |||||||||
| 1381 | // Note that inbounds does *not* guarantee nsw for the addition, as only | ||||||||
| 1382 | // the offset is signed, while the base address is unsigned. | ||||||||
| 1383 | Known = KnownBits::computeForAddSub( | ||||||||
| 1384 | /*Add=*/true, /*NSW=*/false, Known, IndexBits); | ||||||||
| 1385 | } | ||||||||
| 1386 | if (!Known.isUnknown() && !AccConstIndices.isNullValue()) { | ||||||||
| 1387 | KnownBits Index = KnownBits::makeConstant(AccConstIndices); | ||||||||
| 1388 | Known = KnownBits::computeForAddSub( | ||||||||
| 1389 | /*Add=*/true, /*NSW=*/false, Known, Index); | ||||||||
| 1390 | } | ||||||||
| 1391 | break; | ||||||||
| 1392 | } | ||||||||
| 1393 | case Instruction::PHI: { | ||||||||
| 1394 | const PHINode *P = cast<PHINode>(I); | ||||||||
| 1395 | BinaryOperator *BO = nullptr; | ||||||||
| 1396 | Value *R = nullptr, *L = nullptr; | ||||||||
| 1397 | if (matchSimpleRecurrence(P, BO, R, L)) { | ||||||||
| 1398 | // Handle the case of a simple two-predecessor recurrence PHI. | ||||||||
| 1399 | // There's a lot more that could theoretically be done here, but | ||||||||
| 1400 | // this is sufficient to catch some interesting cases. | ||||||||
| 1401 | unsigned Opcode = BO->getOpcode(); | ||||||||
| 1402 | |||||||||
| 1403 | // If this is a shift recurrence, we know the bits being shifted in. | ||||||||
| 1404 | // We can combine that with information about the start value of the | ||||||||
| 1405 | // recurrence to conclude facts about the result. | ||||||||
| 1406 | if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr || | ||||||||
| 1407 | Opcode == Instruction::Shl) && | ||||||||
| 1408 | BO->getOperand(0) == I) { | ||||||||
| 1409 | |||||||||
| 1410 | // We have matched a recurrence of the form: | ||||||||
| 1411 | // %iv = [R, %entry], [%iv.next, %backedge] | ||||||||
| 1412 | // %iv.next = shift_op %iv, L | ||||||||
| 1413 | |||||||||
| 1414 | // Recurse with the phi context to avoid concern about whether facts | ||||||||
| 1415 | // inferred hold at original context instruction. TODO: It may be | ||||||||
| 1416 | // correct to use the original context. IF warranted, explore and | ||||||||
| 1417 | // add sufficient tests to cover. | ||||||||
| 1418 | Query RecQ = Q; | ||||||||
| 1419 | RecQ.CxtI = P; | ||||||||
| 1420 | computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ); | ||||||||
| 1421 | switch (Opcode) { | ||||||||
| 1422 | case Instruction::Shl: | ||||||||
| 1423 | // A shl recurrence will only increase the tailing zeros | ||||||||
| 1424 | Known.Zero.setLowBits(Known2.countMinTrailingZeros()); | ||||||||
| 1425 | break; | ||||||||
| 1426 | case Instruction::LShr: | ||||||||
| 1427 | // A lshr recurrence will preserve the leading zeros of the | ||||||||
| 1428 | // start value | ||||||||
| 1429 | Known.Zero.setHighBits(Known2.countMinLeadingZeros()); | ||||||||
| 1430 | break; | ||||||||
| 1431 | case Instruction::AShr: | ||||||||
| 1432 | // An ashr recurrence will extend the initial sign bit | ||||||||
| 1433 | Known.Zero.setHighBits(Known2.countMinLeadingZeros()); | ||||||||
| 1434 | Known.One.setHighBits(Known2.countMinLeadingOnes()); | ||||||||
| 1435 | break; | ||||||||
| 1436 | }; | ||||||||
| 1437 | } | ||||||||
| 1438 | |||||||||
| 1439 | // Check for operations that have the property that if | ||||||||
| 1440 | // both their operands have low zero bits, the result | ||||||||
| 1441 | // will have low zero bits. | ||||||||
| 1442 | if (Opcode == Instruction::Add || | ||||||||
| 1443 | Opcode == Instruction::Sub || | ||||||||
| 1444 | Opcode == Instruction::And || | ||||||||
| 1445 | Opcode == Instruction::Or || | ||||||||
| 1446 | Opcode == Instruction::Mul) { | ||||||||
| 1447 | // Change the context instruction to the "edge" that flows into the | ||||||||
| 1448 | // phi. This is important because that is where the value is actually | ||||||||
| 1449 | // "evaluated" even though it is used later somewhere else. (see also | ||||||||
| 1450 | // D69571). | ||||||||
| 1451 | Query RecQ = Q; | ||||||||
| 1452 | |||||||||
| 1453 | unsigned OpNum = P->getOperand(0) == R ? 0 : 1; | ||||||||
| 1454 | Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator(); | ||||||||
| 1455 | Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator(); | ||||||||
| 1456 | |||||||||
| 1457 | // Ok, we have a PHI of the form L op= R. Check for low | ||||||||
| 1458 | // zero bits. | ||||||||
| 1459 | RecQ.CxtI = RInst; | ||||||||
| 1460 | computeKnownBits(R, Known2, Depth + 1, RecQ); | ||||||||
| 1461 | |||||||||
| 1462 | // We need to take the minimum number of known bits | ||||||||
| 1463 | KnownBits Known3(BitWidth); | ||||||||
| 1464 | RecQ.CxtI = LInst; | ||||||||
| 1465 | computeKnownBits(L, Known3, Depth + 1, RecQ); | ||||||||
| 1466 | |||||||||
| 1467 | Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), | ||||||||
| 1468 | Known3.countMinTrailingZeros())); | ||||||||
| 1469 | |||||||||
| 1470 | auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO); | ||||||||
| 1471 | if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { | ||||||||
| 1472 | // If initial value of recurrence is nonnegative, and we are adding | ||||||||
| 1473 | // a nonnegative number with nsw, the result can only be nonnegative | ||||||||
| 1474 | // or poison value regardless of the number of times we execute the | ||||||||
| 1475 | // add in phi recurrence. If initial value is negative and we are | ||||||||
| 1476 | // adding a negative number with nsw, the result can only be | ||||||||
| 1477 | // negative or poison value. Similar arguments apply to sub and mul. | ||||||||
| 1478 | // | ||||||||
| 1479 | // (add non-negative, non-negative) --> non-negative | ||||||||
| 1480 | // (add negative, negative) --> negative | ||||||||
| 1481 | if (Opcode == Instruction::Add) { | ||||||||
| 1482 | if (Known2.isNonNegative() && Known3.isNonNegative()) | ||||||||
| 1483 | Known.makeNonNegative(); | ||||||||
| 1484 | else if (Known2.isNegative() && Known3.isNegative()) | ||||||||
| 1485 | Known.makeNegative(); | ||||||||
| 1486 | } | ||||||||
| 1487 | |||||||||
| 1488 | // (sub nsw non-negative, negative) --> non-negative | ||||||||
| 1489 | // (sub nsw negative, non-negative) --> negative | ||||||||
| 1490 | else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) { | ||||||||
| 1491 | if (Known2.isNonNegative() && Known3.isNegative()) | ||||||||
| 1492 | Known.makeNonNegative(); | ||||||||
| 1493 | else if (Known2.isNegative() && Known3.isNonNegative()) | ||||||||
| 1494 | Known.makeNegative(); | ||||||||
| 1495 | } | ||||||||
| 1496 | |||||||||
| 1497 | // (mul nsw non-negative, non-negative) --> non-negative | ||||||||
| 1498 | else if (Opcode == Instruction::Mul && Known2.isNonNegative() && | ||||||||
| 1499 | Known3.isNonNegative()) | ||||||||
| 1500 | Known.makeNonNegative(); | ||||||||
| 1501 | } | ||||||||
| 1502 | |||||||||
| 1503 | break; | ||||||||
| 1504 | } | ||||||||
| 1505 | } | ||||||||
| 1506 | |||||||||
| 1507 | // Unreachable blocks may have zero-operand PHI nodes. | ||||||||
| 1508 | if (P->getNumIncomingValues() == 0) | ||||||||
| 1509 | break; | ||||||||
| 1510 | |||||||||
| 1511 | // Otherwise take the unions of the known bit sets of the operands, | ||||||||
| 1512 | // taking conservative care to avoid excessive recursion. | ||||||||
| 1513 | if (Depth < MaxAnalysisRecursionDepth - 1 && !Known.Zero && !Known.One) { | ||||||||
| 1514 | // Skip if every incoming value references to ourself. | ||||||||
| 1515 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) | ||||||||
| 1516 | break; | ||||||||
| 1517 | |||||||||
| 1518 | Known.Zero.setAllBits(); | ||||||||
| 1519 | Known.One.setAllBits(); | ||||||||
| 1520 | for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) { | ||||||||
| 1521 | Value *IncValue = P->getIncomingValue(u); | ||||||||
| 1522 | // Skip direct self references. | ||||||||
| 1523 | if (IncValue == P) continue; | ||||||||
| 1524 | |||||||||
| 1525 | // Change the context instruction to the "edge" that flows into the | ||||||||
| 1526 | // phi. This is important because that is where the value is actually | ||||||||
| 1527 | // "evaluated" even though it is used later somewhere else. (see also | ||||||||
| 1528 | // D69571). | ||||||||
| 1529 | Query RecQ = Q; | ||||||||
| 1530 | RecQ.CxtI = P->getIncomingBlock(u)->getTerminator(); | ||||||||
| 1531 | |||||||||
| 1532 | Known2 = KnownBits(BitWidth); | ||||||||
| 1533 | // Recurse, but cap the recursion to one level, because we don't | ||||||||
| 1534 | // want to waste time spinning around in loops. | ||||||||
| 1535 | computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ); | ||||||||
| 1536 | Known = KnownBits::commonBits(Known, Known2); | ||||||||
| 1537 | // If all bits have been ruled out, there's no need to check | ||||||||
| 1538 | // more operands. | ||||||||
| 1539 | if (Known.isUnknown()) | ||||||||
| 1540 | break; | ||||||||
| 1541 | } | ||||||||
| 1542 | } | ||||||||
| 1543 | break; | ||||||||
| 1544 | } | ||||||||
| 1545 | case Instruction::Call: | ||||||||
| 1546 | case Instruction::Invoke: | ||||||||
| 1547 | // If range metadata is attached to this call, set known bits from that, | ||||||||
| 1548 | // and then intersect with known bits based on other properties of the | ||||||||
| 1549 | // function. | ||||||||
| 1550 | if (MDNode *MD = | ||||||||
| 1551 | Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range)) | ||||||||
| 1552 | computeKnownBitsFromRangeMetadata(*MD, Known); | ||||||||
| 1553 | if (const Value *RV = cast<CallBase>(I)->getReturnedArgOperand()) { | ||||||||
| 1554 | computeKnownBits(RV, Known2, Depth + 1, Q); | ||||||||
| 1555 | Known.Zero |= Known2.Zero; | ||||||||
| 1556 | Known.One |= Known2.One; | ||||||||
| 1557 | } | ||||||||
| 1558 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | ||||||||
| 1559 | switch (II->getIntrinsicID()) { | ||||||||
| 1560 | default: break; | ||||||||
| 1561 | case Intrinsic::abs: { | ||||||||
| 1562 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||||||
| 1563 | bool IntMinIsPoison = match(II->getArgOperand(1), m_One()); | ||||||||
| 1564 | Known = Known2.abs(IntMinIsPoison); | ||||||||
| 1565 | break; | ||||||||
| 1566 | } | ||||||||
| 1567 | case Intrinsic::bitreverse: | ||||||||
| 1568 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1569 | Known.Zero |= Known2.Zero.reverseBits(); | ||||||||
| 1570 | Known.One |= Known2.One.reverseBits(); | ||||||||
| 1571 | break; | ||||||||
| 1572 | case Intrinsic::bswap: | ||||||||
| 1573 | computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); | ||||||||
| 1574 | Known.Zero |= Known2.Zero.byteSwap(); | ||||||||
| 1575 | Known.One |= Known2.One.byteSwap(); | ||||||||
| 1576 | break; | ||||||||
| 1577 | case Intrinsic::ctlz: { | ||||||||
| 1578 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||||||
| 1579 | // If we have a known 1, its position is our upper bound. | ||||||||
| 1580 | unsigned PossibleLZ = Known2.countMaxLeadingZeros(); | ||||||||
| 1581 | // If this call is undefined for 0, the result will be less than 2^n. | ||||||||
| 1582 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) | ||||||||
| 1583 | PossibleLZ = std::min(PossibleLZ, BitWidth - 1); | ||||||||
| 1584 | unsigned LowBits = Log2_32(PossibleLZ)+1; | ||||||||
| 1585 | Known.Zero.setBitsFrom(LowBits); | ||||||||
| 1586 | break; | ||||||||
| 1587 | } | ||||||||
| 1588 | case Intrinsic::cttz: { | ||||||||
| 1589 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||||||
| 1590 | // If we have a known 1, its position is our upper bound. | ||||||||
| 1591 | unsigned PossibleTZ = Known2.countMaxTrailingZeros(); | ||||||||
| 1592 | // If this call is undefined for 0, the result will be less than 2^n. | ||||||||
| 1593 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) | ||||||||
| 1594 | PossibleTZ = std::min(PossibleTZ, BitWidth - 1); | ||||||||
| 1595 | unsigned LowBits = Log2_32(PossibleTZ)+1; | ||||||||
| 1596 | Known.Zero.setBitsFrom(LowBits); | ||||||||
| 1597 | break; | ||||||||
| 1598 | } | ||||||||
| 1599 | case Intrinsic::ctpop: { | ||||||||
| 1600 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||||||
| 1601 | // We can bound the space the count needs. Also, bits known to be zero | ||||||||
| 1602 | // can't contribute to the population. | ||||||||
| 1603 | unsigned BitsPossiblySet = Known2.countMaxPopulation(); | ||||||||
| 1604 | unsigned LowBits = Log2_32(BitsPossiblySet)+1; | ||||||||
| 1605 | Known.Zero.setBitsFrom(LowBits); | ||||||||
| 1606 | // TODO: we could bound KnownOne using the lower bound on the number | ||||||||
| 1607 | // of bits which might be set provided by popcnt KnownOne2. | ||||||||
| 1608 | break; | ||||||||
| 1609 | } | ||||||||
| 1610 | case Intrinsic::fshr: | ||||||||
| 1611 | case Intrinsic::fshl: { | ||||||||
| 1612 | const APInt *SA; | ||||||||
| 1613 | if (!match(I->getOperand(2), m_APInt(SA))) | ||||||||
| 1614 | break; | ||||||||
| 1615 | |||||||||
| 1616 | // Normalize to funnel shift left. | ||||||||
| 1617 | uint64_t ShiftAmt = SA->urem(BitWidth); | ||||||||
| 1618 | if (II->getIntrinsicID() == Intrinsic::fshr) | ||||||||
| 1619 | ShiftAmt = BitWidth - ShiftAmt; | ||||||||
| 1620 | |||||||||
| 1621 | KnownBits Known3(BitWidth); | ||||||||
| 1622 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); | ||||||||
| 1623 | computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q); | ||||||||
| 1624 | |||||||||
| 1625 | Known.Zero = | ||||||||
| 1626 | Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt); | ||||||||
| 1627 | Known.One = | ||||||||
| 1628 | Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt); | ||||||||
| 1629 | break; | ||||||||
| 1630 | } | ||||||||
| 1631 | case Intrinsic::uadd_sat: | ||||||||
| 1632 | case Intrinsic::usub_sat: { | ||||||||
| 1633 | bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat; | ||||||||
| 1634 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1635 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1636 | |||||||||
| 1637 | // Add: Leading ones of either operand are preserved. | ||||||||
| 1638 | // Sub: Leading zeros of LHS and leading ones of RHS are preserved | ||||||||
| 1639 | // as leading zeros in the result. | ||||||||
| 1640 | unsigned LeadingKnown; | ||||||||
| 1641 | if (IsAdd) | ||||||||
| 1642 | LeadingKnown = std::max(Known.countMinLeadingOnes(), | ||||||||
| 1643 | Known2.countMinLeadingOnes()); | ||||||||
| 1644 | else | ||||||||
| 1645 | LeadingKnown = std::max(Known.countMinLeadingZeros(), | ||||||||
| 1646 | Known2.countMinLeadingOnes()); | ||||||||
| 1647 | |||||||||
| 1648 | Known = KnownBits::computeForAddSub( | ||||||||
| 1649 | IsAdd, /* NSW */ false, Known, Known2); | ||||||||
| 1650 | |||||||||
| 1651 | // We select between the operation result and all-ones/zero | ||||||||
| 1652 | // respectively, so we can preserve known ones/zeros. | ||||||||
| 1653 | if (IsAdd) { | ||||||||
| 1654 | Known.One.setHighBits(LeadingKnown); | ||||||||
| 1655 | Known.Zero.clearAllBits(); | ||||||||
| 1656 | } else { | ||||||||
| 1657 | Known.Zero.setHighBits(LeadingKnown); | ||||||||
| 1658 | Known.One.clearAllBits(); | ||||||||
| 1659 | } | ||||||||
| 1660 | break; | ||||||||
| 1661 | } | ||||||||
| 1662 | case Intrinsic::umin: | ||||||||
| 1663 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1664 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1665 | Known = KnownBits::umin(Known, Known2); | ||||||||
| 1666 | break; | ||||||||
| 1667 | case Intrinsic::umax: | ||||||||
| 1668 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1669 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1670 | Known = KnownBits::umax(Known, Known2); | ||||||||
| 1671 | break; | ||||||||
| 1672 | case Intrinsic::smin: | ||||||||
| 1673 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1674 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1675 | Known = KnownBits::smin(Known, Known2); | ||||||||
| 1676 | break; | ||||||||
| 1677 | case Intrinsic::smax: | ||||||||
| 1678 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1679 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); | ||||||||
| 1680 | Known = KnownBits::smax(Known, Known2); | ||||||||
| 1681 | break; | ||||||||
| 1682 | case Intrinsic::x86_sse42_crc32_64_64: | ||||||||
| 1683 | Known.Zero.setBitsFrom(32); | ||||||||
| 1684 | break; | ||||||||
| 1685 | case Intrinsic::riscv_vsetvli: | ||||||||
| 1686 | case Intrinsic::riscv_vsetvlimax: | ||||||||
| 1687 | // Assume that VL output is positive and would fit in an int32_t. | ||||||||
| 1688 | // TODO: VLEN might be capped at 16 bits in a future V spec update. | ||||||||
| 1689 | if (BitWidth >= 32) | ||||||||
| 1690 | Known.Zero.setBitsFrom(31); | ||||||||
| 1691 | break; | ||||||||
| 1692 | } | ||||||||
| 1693 | } | ||||||||
| 1694 | break; | ||||||||
| 1695 | case Instruction::ShuffleVector: { | ||||||||
| 1696 | auto *Shuf = dyn_cast<ShuffleVectorInst>(I); | ||||||||
| 1697 | // FIXME: Do we need to handle ConstantExpr involving shufflevectors? | ||||||||
| 1698 | if (!Shuf) { | ||||||||
| 1699 | Known.resetAll(); | ||||||||
| 1700 | return; | ||||||||
| 1701 | } | ||||||||
| 1702 | // For undef elements, we don't know anything about the common state of | ||||||||
| 1703 | // the shuffle result. | ||||||||
| 1704 | APInt DemandedLHS, DemandedRHS; | ||||||||
| 1705 | if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) { | ||||||||
| 1706 | Known.resetAll(); | ||||||||
| 1707 | return; | ||||||||
| 1708 | } | ||||||||
| 1709 | Known.One.setAllBits(); | ||||||||
| 1710 | Known.Zero.setAllBits(); | ||||||||
| 1711 | if (!!DemandedLHS) { | ||||||||
| 1712 | const Value *LHS = Shuf->getOperand(0); | ||||||||
| 1713 | computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q); | ||||||||
| 1714 | // If we don't know any bits, early out. | ||||||||
| 1715 | if (Known.isUnknown()) | ||||||||
| 1716 | break; | ||||||||
| 1717 | } | ||||||||
| 1718 | if (!!DemandedRHS) { | ||||||||
| 1719 | const Value *RHS = Shuf->getOperand(1); | ||||||||
| 1720 | computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q); | ||||||||
| 1721 | Known = KnownBits::commonBits(Known, Known2); | ||||||||
| 1722 | } | ||||||||
| 1723 | break; | ||||||||
| 1724 | } | ||||||||
| 1725 | case Instruction::InsertElement: { | ||||||||
| 1726 | const Value *Vec = I->getOperand(0); | ||||||||
| 1727 | const Value *Elt = I->getOperand(1); | ||||||||
| 1728 | auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2)); | ||||||||
| 1729 | // Early out if the index is non-constant or out-of-range. | ||||||||
| 1730 | unsigned NumElts = DemandedElts.getBitWidth(); | ||||||||
| 1731 | if (!CIdx || CIdx->getValue().uge(NumElts)) { | ||||||||
| 1732 | Known.resetAll(); | ||||||||
| 1733 | return; | ||||||||
| 1734 | } | ||||||||
| 1735 | Known.One.setAllBits(); | ||||||||
| 1736 | Known.Zero.setAllBits(); | ||||||||
| 1737 | unsigned EltIdx = CIdx->getZExtValue(); | ||||||||
| 1738 | // Do we demand the inserted element? | ||||||||
| 1739 | if (DemandedElts[EltIdx]) { | ||||||||
| 1740 | computeKnownBits(Elt, Known, Depth + 1, Q); | ||||||||
| 1741 | // If we don't know any bits, early out. | ||||||||
| 1742 | if (Known.isUnknown()) | ||||||||
| 1743 | break; | ||||||||
| 1744 | } | ||||||||
| 1745 | // We don't need the base vector element that has been inserted. | ||||||||
| 1746 | APInt DemandedVecElts = DemandedElts; | ||||||||
| 1747 | DemandedVecElts.clearBit(EltIdx); | ||||||||
| 1748 | if (!!DemandedVecElts) { | ||||||||
| 1749 | computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q); | ||||||||
| 1750 | Known = KnownBits::commonBits(Known, Known2); | ||||||||
| 1751 | } | ||||||||
| 1752 | break; | ||||||||
| 1753 | } | ||||||||
| 1754 | case Instruction::ExtractElement: { | ||||||||
| 1755 | // Look through extract element. If the index is non-constant or | ||||||||
| 1756 | // out-of-range demand all elements, otherwise just the extracted element. | ||||||||
| 1757 | const Value *Vec = I->getOperand(0); | ||||||||
| 1758 | const Value *Idx = I->getOperand(1); | ||||||||
| 1759 | auto *CIdx = dyn_cast<ConstantInt>(Idx); | ||||||||
| 1760 | if (isa<ScalableVectorType>(Vec->getType())) { | ||||||||
| 1761 | // FIXME: there's probably *something* we can do with scalable vectors | ||||||||
| 1762 | Known.resetAll(); | ||||||||
| 1763 | break; | ||||||||
| 1764 | } | ||||||||
| 1765 | unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements(); | ||||||||
| 1766 | APInt DemandedVecElts = APInt::getAllOnesValue(NumElts); | ||||||||
| 1767 | if (CIdx && CIdx->getValue().ult(NumElts)) | ||||||||
| 1768 | DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); | ||||||||
| 1769 | computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q); | ||||||||
| 1770 | break; | ||||||||
| 1771 | } | ||||||||
| 1772 | case Instruction::ExtractValue: | ||||||||
| 1773 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { | ||||||||
| 1774 | const ExtractValueInst *EVI = cast<ExtractValueInst>(I); | ||||||||
| 1775 | if (EVI->getNumIndices() != 1) break; | ||||||||
| 1776 | if (EVI->getIndices()[0] == 0) { | ||||||||
| 1777 | switch (II->getIntrinsicID()) { | ||||||||
| 1778 | default: break; | ||||||||
| 1779 | case Intrinsic::uadd_with_overflow: | ||||||||
| 1780 | case Intrinsic::sadd_with_overflow: | ||||||||
| 1781 | computeKnownBitsAddSub(true, II->getArgOperand(0), | ||||||||
| 1782 | II->getArgOperand(1), false, DemandedElts, | ||||||||
| 1783 | Known, Known2, Depth, Q); | ||||||||
| 1784 | break; | ||||||||
| 1785 | case Intrinsic::usub_with_overflow: | ||||||||
| 1786 | case Intrinsic::ssub_with_overflow: | ||||||||
| 1787 | computeKnownBitsAddSub(false, II->getArgOperand(0), | ||||||||
| 1788 | II->getArgOperand(1), false, DemandedElts, | ||||||||
| 1789 | Known, Known2, Depth, Q); | ||||||||
| 1790 | break; | ||||||||
| 1791 | case Intrinsic::umul_with_overflow: | ||||||||
| 1792 | case Intrinsic::smul_with_overflow: | ||||||||
| 1793 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, | ||||||||
| 1794 | DemandedElts, Known, Known2, Depth, Q); | ||||||||
| 1795 | break; | ||||||||
| 1796 | } | ||||||||
| 1797 | } | ||||||||
| 1798 | } | ||||||||
| 1799 | break; | ||||||||
| 1800 | case Instruction::Freeze: | ||||||||
| 1801 | if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT, | ||||||||
| 1802 | Depth + 1)) | ||||||||
| 1803 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 1804 | break; | ||||||||
| 1805 | } | ||||||||
| 1806 | } | ||||||||
| 1807 | |||||||||
| 1808 | /// Determine which bits of V are known to be either zero or one and return | ||||||||
| 1809 | /// them. | ||||||||
| 1810 | KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 1811 | unsigned Depth, const Query &Q) { | ||||||||
| 1812 | KnownBits Known(getBitWidth(V->getType(), Q.DL)); | ||||||||
| 1813 | computeKnownBits(V, DemandedElts, Known, Depth, Q); | ||||||||
| 1814 | return Known; | ||||||||
| 1815 | } | ||||||||
| 1816 | |||||||||
| 1817 | /// Determine which bits of V are known to be either zero or one and return | ||||||||
| 1818 | /// them. | ||||||||
| 1819 | KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) { | ||||||||
| 1820 | KnownBits Known(getBitWidth(V->getType(), Q.DL)); | ||||||||
| 1821 | computeKnownBits(V, Known, Depth, Q); | ||||||||
| 1822 | return Known; | ||||||||
| 1823 | } | ||||||||
| 1824 | |||||||||
| 1825 | /// Determine which bits of V are known to be either zero or one and return | ||||||||
| 1826 | /// them in the Known bit set. | ||||||||
| 1827 | /// | ||||||||
| 1828 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that | ||||||||
| 1829 | /// we cannot optimize based on the assumption that it is zero without changing | ||||||||
| 1830 | /// it to be an explicit zero. If we don't change it to zero, other code could | ||||||||
| 1831 | /// optimized based on the contradictory assumption that it is non-zero. | ||||||||
| 1832 | /// Because instcombine aggressively folds operations with undef args anyway, | ||||||||
| 1833 | /// this won't lose us code quality. | ||||||||
| 1834 | /// | ||||||||
| 1835 | /// This function is defined on values with integer type, values with pointer | ||||||||
| 1836 | /// type, and vectors of integers. In the case | ||||||||
| 1837 | /// where V is a vector, known zero, and known one values are the | ||||||||
| 1838 | /// same width as the vector element, and the bit is set only if it is true | ||||||||
| 1839 | /// for all of the demanded elements in the vector specified by DemandedElts. | ||||||||
| 1840 | void computeKnownBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 1841 | KnownBits &Known, unsigned Depth, const Query &Q) { | ||||||||
| 1842 | if (!DemandedElts || isa<ScalableVectorType>(V->getType())) { | ||||||||
| 1843 | // No demanded elts or V is a scalable vector, better to assume we don't | ||||||||
| 1844 | // know anything. | ||||||||
| 1845 | Known.resetAll(); | ||||||||
| 1846 | return; | ||||||||
| 1847 | } | ||||||||
| 1848 | |||||||||
| 1849 | assert(V && "No Value?")((void)0); | ||||||||
| 1850 | assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth")((void)0); | ||||||||
| 1851 | |||||||||
| 1852 | #ifndef NDEBUG1 | ||||||||
| 1853 | Type *Ty = V->getType(); | ||||||||
| 1854 | unsigned BitWidth = Known.getBitWidth(); | ||||||||
| 1855 | |||||||||
| 1856 | assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&((void)0) | ||||||||
| 1857 | "Not integer or pointer type!")((void)0); | ||||||||
| 1858 | |||||||||
| 1859 | if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) { | ||||||||
| 1860 | assert(((void)0) | ||||||||
| 1861 | FVTy->getNumElements() == DemandedElts.getBitWidth() &&((void)0) | ||||||||
| 1862 | "DemandedElt width should equal the fixed vector number of elements")((void)0); | ||||||||
| 1863 | } else { | ||||||||
| 1864 | assert(DemandedElts == APInt(1, 1) &&((void)0) | ||||||||
| 1865 | "DemandedElt width should be 1 for scalars")((void)0); | ||||||||
| 1866 | } | ||||||||
| 1867 | |||||||||
| 1868 | Type *ScalarTy = Ty->getScalarType(); | ||||||||
| 1869 | if (ScalarTy->isPointerTy()) { | ||||||||
| 1870 | assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&((void)0) | ||||||||
| 1871 | "V and Known should have same BitWidth")((void)0); | ||||||||
| 1872 | } else { | ||||||||
| 1873 | assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&((void)0) | ||||||||
| 1874 | "V and Known should have same BitWidth")((void)0); | ||||||||
| 1875 | } | ||||||||
| 1876 | #endif | ||||||||
| 1877 | |||||||||
| 1878 | const APInt *C; | ||||||||
| 1879 | if (match(V, m_APInt(C))) { | ||||||||
| 1880 | // We know all of the bits for a scalar constant or a splat vector constant! | ||||||||
| 1881 | Known = KnownBits::makeConstant(*C); | ||||||||
| 1882 | return; | ||||||||
| 1883 | } | ||||||||
| 1884 | // Null and aggregate-zero are all-zeros. | ||||||||
| 1885 | if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) { | ||||||||
| 1886 | Known.setAllZero(); | ||||||||
| 1887 | return; | ||||||||
| 1888 | } | ||||||||
| 1889 | // Handle a constant vector by taking the intersection of the known bits of | ||||||||
| 1890 | // each element. | ||||||||
| 1891 | if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) { | ||||||||
| 1892 | // We know that CDV must be a vector of integers. Take the intersection of | ||||||||
| 1893 | // each element. | ||||||||
| 1894 | Known.Zero.setAllBits(); Known.One.setAllBits(); | ||||||||
| 1895 | for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) { | ||||||||
| 1896 | if (!DemandedElts[i]) | ||||||||
| 1897 | continue; | ||||||||
| 1898 | APInt Elt = CDV->getElementAsAPInt(i); | ||||||||
| 1899 | Known.Zero &= ~Elt; | ||||||||
| 1900 | Known.One &= Elt; | ||||||||
| 1901 | } | ||||||||
| 1902 | return; | ||||||||
| 1903 | } | ||||||||
| 1904 | |||||||||
| 1905 | if (const auto *CV = dyn_cast<ConstantVector>(V)) { | ||||||||
| 1906 | // We know that CV must be a vector of integers. Take the intersection of | ||||||||
| 1907 | // each element. | ||||||||
| 1908 | Known.Zero.setAllBits(); Known.One.setAllBits(); | ||||||||
| 1909 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { | ||||||||
| 1910 | if (!DemandedElts[i]) | ||||||||
| 1911 | continue; | ||||||||
| 1912 | Constant *Element = CV->getAggregateElement(i); | ||||||||
| 1913 | auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); | ||||||||
| 1914 | if (!ElementCI) { | ||||||||
| 1915 | Known.resetAll(); | ||||||||
| 1916 | return; | ||||||||
| 1917 | } | ||||||||
| 1918 | const APInt &Elt = ElementCI->getValue(); | ||||||||
| 1919 | Known.Zero &= ~Elt; | ||||||||
| 1920 | Known.One &= Elt; | ||||||||
| 1921 | } | ||||||||
| 1922 | return; | ||||||||
| 1923 | } | ||||||||
| 1924 | |||||||||
| 1925 | // Start out not knowing anything. | ||||||||
| 1926 | Known.resetAll(); | ||||||||
| 1927 | |||||||||
| 1928 | // We can't imply anything about undefs. | ||||||||
| 1929 | if (isa<UndefValue>(V)) | ||||||||
| 1930 | return; | ||||||||
| 1931 | |||||||||
| 1932 | // There's no point in looking through other users of ConstantData for | ||||||||
| 1933 | // assumptions. Confirm that we've handled them all. | ||||||||
| 1934 | assert(!isa<ConstantData>(V) && "Unhandled constant data!")((void)0); | ||||||||
| 1935 | |||||||||
| 1936 | // All recursive calls that increase depth must come after this. | ||||||||
| 1937 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 1938 | return; | ||||||||
| 1939 | |||||||||
| 1940 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has | ||||||||
| 1941 | // the bits of its aliasee. | ||||||||
| 1942 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { | ||||||||
| 1943 | if (!GA->isInterposable()) | ||||||||
| 1944 | computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); | ||||||||
| 1945 | return; | ||||||||
| 1946 | } | ||||||||
| 1947 | |||||||||
| 1948 | if (const Operator *I = dyn_cast<Operator>(V)) | ||||||||
| 1949 | computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q); | ||||||||
| 1950 | |||||||||
| 1951 | // Aligned pointers have trailing zeros - refine Known.Zero set | ||||||||
| 1952 | if (isa<PointerType>(V->getType())) { | ||||||||
| 1953 | Align Alignment = V->getPointerAlignment(Q.DL); | ||||||||
| 1954 | Known.Zero.setLowBits(Log2(Alignment)); | ||||||||
| 1955 | } | ||||||||
| 1956 | |||||||||
| 1957 | // computeKnownBitsFromAssume strictly refines Known. | ||||||||
| 1958 | // Therefore, we run them after computeKnownBitsFromOperator. | ||||||||
| 1959 | |||||||||
| 1960 | // Check whether a nearby assume intrinsic can determine some known bits. | ||||||||
| 1961 | computeKnownBitsFromAssume(V, Known, Depth, Q); | ||||||||
| 1962 | |||||||||
| 1963 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?")((void)0); | ||||||||
| 1964 | } | ||||||||
| 1965 | |||||||||
| 1966 | /// Return true if the given value is known to have exactly one | ||||||||
| 1967 | /// bit set when defined. For vectors return true if every element is known to | ||||||||
| 1968 | /// be a power of two when defined. Supports values with integer or pointer | ||||||||
| 1969 | /// types and vectors of integers. | ||||||||
| 1970 | bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, | ||||||||
| 1971 | const Query &Q) { | ||||||||
| 1972 | assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth")((void)0); | ||||||||
| 1973 | |||||||||
| 1974 | // Attempt to match against constants. | ||||||||
| 1975 | if (OrZero && match(V, m_Power2OrZero())) | ||||||||
| 1976 | return true; | ||||||||
| 1977 | if (match(V, m_Power2())) | ||||||||
| 1978 | return true; | ||||||||
| 1979 | |||||||||
| 1980 | // 1 << X is clearly a power of two if the one is not shifted off the end. If | ||||||||
| 1981 | // it is shifted off the end then the result is undefined. | ||||||||
| 1982 | if (match(V, m_Shl(m_One(), m_Value()))) | ||||||||
| 1983 | return true; | ||||||||
| 1984 | |||||||||
| 1985 | // (signmask) >>l X is clearly a power of two if the one is not shifted off | ||||||||
| 1986 | // the bottom. If it is shifted off the bottom then the result is undefined. | ||||||||
| 1987 | if (match(V, m_LShr(m_SignMask(), m_Value()))) | ||||||||
| 1988 | return true; | ||||||||
| 1989 | |||||||||
| 1990 | // The remaining tests are all recursive, so bail out if we hit the limit. | ||||||||
| 1991 | if (Depth++ == MaxAnalysisRecursionDepth) | ||||||||
| 1992 | return false; | ||||||||
| 1993 | |||||||||
| 1994 | Value *X = nullptr, *Y = nullptr; | ||||||||
| 1995 | // A shift left or a logical shift right of a power of two is a power of two | ||||||||
| 1996 | // or zero. | ||||||||
| 1997 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || | ||||||||
| 1998 | match(V, m_LShr(m_Value(X), m_Value())))) | ||||||||
| 1999 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); | ||||||||
| 2000 | |||||||||
| 2001 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V)) | ||||||||
| 2002 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); | ||||||||
| 2003 | |||||||||
| 2004 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) | ||||||||
| 2005 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && | ||||||||
| 2006 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); | ||||||||
| 2007 | |||||||||
| 2008 | // Peek through min/max. | ||||||||
| 2009 | if (match(V, m_MaxOrMin(m_Value(X), m_Value(Y)))) { | ||||||||
| 2010 | return isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q) && | ||||||||
| 2011 | isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q); | ||||||||
| 2012 | } | ||||||||
| 2013 | |||||||||
| 2014 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { | ||||||||
| 2015 | // A power of two and'd with anything is a power of two or zero. | ||||||||
| 2016 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || | ||||||||
| 2017 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) | ||||||||
| 2018 | return true; | ||||||||
| 2019 | // X & (-X) is always a power of two or zero. | ||||||||
| 2020 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) | ||||||||
| 2021 | return true; | ||||||||
| 2022 | return false; | ||||||||
| 2023 | } | ||||||||
| 2024 | |||||||||
| 2025 | // Adding a power-of-two or zero to the same power-of-two or zero yields | ||||||||
| 2026 | // either the original power-of-two, a larger power-of-two or zero. | ||||||||
| 2027 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { | ||||||||
| 2028 | const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); | ||||||||
| 2029 | if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || | ||||||||
| 2030 | Q.IIQ.hasNoSignedWrap(VOBO)) { | ||||||||
| 2031 | if (match(X, m_And(m_Specific(Y), m_Value())) || | ||||||||
| 2032 | match(X, m_And(m_Value(), m_Specific(Y)))) | ||||||||
| 2033 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) | ||||||||
| 2034 | return true; | ||||||||
| 2035 | if (match(Y, m_And(m_Specific(X), m_Value())) || | ||||||||
| 2036 | match(Y, m_And(m_Value(), m_Specific(X)))) | ||||||||
| 2037 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) | ||||||||
| 2038 | return true; | ||||||||
| 2039 | |||||||||
| 2040 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); | ||||||||
| 2041 | KnownBits LHSBits(BitWidth); | ||||||||
| 2042 | computeKnownBits(X, LHSBits, Depth, Q); | ||||||||
| 2043 | |||||||||
| 2044 | KnownBits RHSBits(BitWidth); | ||||||||
| 2045 | computeKnownBits(Y, RHSBits, Depth, Q); | ||||||||
| 2046 | // If i8 V is a power of two or zero: | ||||||||
| 2047 | // ZeroBits: 1 1 1 0 1 1 1 1 | ||||||||
| 2048 | // ~ZeroBits: 0 0 0 1 0 0 0 0 | ||||||||
| 2049 | if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) | ||||||||
| 2050 | // If OrZero isn't set, we cannot give back a zero result. | ||||||||
| 2051 | // Make sure either the LHS or RHS has a bit set. | ||||||||
| 2052 | if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) | ||||||||
| 2053 | return true; | ||||||||
| 2054 | } | ||||||||
| 2055 | } | ||||||||
| 2056 | |||||||||
| 2057 | // An exact divide or right shift can only shift off zero bits, so the result | ||||||||
| 2058 | // is a power of two only if the first operand is a power of two and not | ||||||||
| 2059 | // copying a sign bit (sdiv int_min, 2). | ||||||||
| 2060 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || | ||||||||
| 2061 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { | ||||||||
| 2062 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, | ||||||||
| 2063 | Depth, Q); | ||||||||
| 2064 | } | ||||||||
| 2065 | |||||||||
| 2066 | return false; | ||||||||
| 2067 | } | ||||||||
| 2068 | |||||||||
| 2069 | /// Test whether a GEP's result is known to be non-null. | ||||||||
| 2070 | /// | ||||||||
| 2071 | /// Uses properties inherent in a GEP to try to determine whether it is known | ||||||||
| 2072 | /// to be non-null. | ||||||||
| 2073 | /// | ||||||||
| 2074 | /// Currently this routine does not support vector GEPs. | ||||||||
| 2075 | static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, | ||||||||
| 2076 | const Query &Q) { | ||||||||
| 2077 | const Function *F = nullptr; | ||||||||
| 2078 | if (const Instruction *I = dyn_cast<Instruction>(GEP)) | ||||||||
| 2079 | F = I->getFunction(); | ||||||||
| 2080 | |||||||||
| 2081 | if (!GEP->isInBounds() || | ||||||||
| 2082 | NullPointerIsDefined(F, GEP->getPointerAddressSpace())) | ||||||||
| 2083 | return false; | ||||||||
| 2084 | |||||||||
| 2085 | // FIXME: Support vector-GEPs. | ||||||||
| 2086 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP")((void)0); | ||||||||
| 2087 | |||||||||
| 2088 | // If the base pointer is non-null, we cannot walk to a null address with an | ||||||||
| 2089 | // inbounds GEP in address space zero. | ||||||||
| 2090 | if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) | ||||||||
| 2091 | return true; | ||||||||
| 2092 | |||||||||
| 2093 | // Walk the GEP operands and see if any operand introduces a non-zero offset. | ||||||||
| 2094 | // If so, then the GEP cannot produce a null pointer, as doing so would | ||||||||
| 2095 | // inherently violate the inbounds contract within address space zero. | ||||||||
| 2096 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); | ||||||||
| 2097 | GTI != GTE; ++GTI) { | ||||||||
| 2098 | // Struct types are easy -- they must always be indexed by a constant. | ||||||||
| 2099 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||||||
| 2100 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); | ||||||||
| 2101 | unsigned ElementIdx = OpC->getZExtValue(); | ||||||||
| 2102 | const StructLayout *SL = Q.DL.getStructLayout(STy); | ||||||||
| 2103 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); | ||||||||
| 2104 | if (ElementOffset > 0) | ||||||||
| 2105 | return true; | ||||||||
| 2106 | continue; | ||||||||
| 2107 | } | ||||||||
| 2108 | |||||||||
| 2109 | // If we have a zero-sized type, the index doesn't matter. Keep looping. | ||||||||
| 2110 | if (Q.DL.getTypeAllocSize(GTI.getIndexedType()).getKnownMinSize() == 0) | ||||||||
| 2111 | continue; | ||||||||
| 2112 | |||||||||
| 2113 | // Fast path the constant operand case both for efficiency and so we don't | ||||||||
| 2114 | // increment Depth when just zipping down an all-constant GEP. | ||||||||
| 2115 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { | ||||||||
| 2116 | if (!OpC->isZero()) | ||||||||
| 2117 | return true; | ||||||||
| 2118 | continue; | ||||||||
| 2119 | } | ||||||||
| 2120 | |||||||||
| 2121 | // We post-increment Depth here because while isKnownNonZero increments it | ||||||||
| 2122 | // as well, when we pop back up that increment won't persist. We don't want | ||||||||
| 2123 | // to recurse 10k times just because we have 10k GEP operands. We don't | ||||||||
| 2124 | // bail completely out because we want to handle constant GEPs regardless | ||||||||
| 2125 | // of depth. | ||||||||
| 2126 | if (Depth++ >= MaxAnalysisRecursionDepth) | ||||||||
| 2127 | continue; | ||||||||
| 2128 | |||||||||
| 2129 | if (isKnownNonZero(GTI.getOperand(), Depth, Q)) | ||||||||
| 2130 | return true; | ||||||||
| 2131 | } | ||||||||
| 2132 | |||||||||
| 2133 | return false; | ||||||||
| 2134 | } | ||||||||
| 2135 | |||||||||
| 2136 | static bool isKnownNonNullFromDominatingCondition(const Value *V, | ||||||||
| 2137 | const Instruction *CtxI, | ||||||||
| 2138 | const DominatorTree *DT) { | ||||||||
| 2139 | if (isa<Constant>(V)) | ||||||||
| 2140 | return false; | ||||||||
| 2141 | |||||||||
| 2142 | if (!CtxI || !DT) | ||||||||
| 2143 | return false; | ||||||||
| 2144 | |||||||||
| 2145 | unsigned NumUsesExplored = 0; | ||||||||
| 2146 | for (auto *U : V->users()) { | ||||||||
| 2147 | // Avoid massive lists | ||||||||
| 2148 | if (NumUsesExplored >= DomConditionsMaxUses) | ||||||||
| 2149 | break; | ||||||||
| 2150 | NumUsesExplored++; | ||||||||
| 2151 | |||||||||
| 2152 | // If the value is used as an argument to a call or invoke, then argument | ||||||||
| 2153 | // attributes may provide an answer about null-ness. | ||||||||
| 2154 | if (const auto *CB = dyn_cast<CallBase>(U)) | ||||||||
| 2155 | if (auto *CalledFunc = CB->getCalledFunction()) | ||||||||
| 2156 | for (const Argument &Arg : CalledFunc->args()) | ||||||||
| 2157 | if (CB->getArgOperand(Arg.getArgNo()) == V && | ||||||||
| 2158 | Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) && | ||||||||
| 2159 | DT->dominates(CB, CtxI)) | ||||||||
| 2160 | return true; | ||||||||
| 2161 | |||||||||
| 2162 | // If the value is used as a load/store, then the pointer must be non null. | ||||||||
| 2163 | if (V == getLoadStorePointerOperand(U)) { | ||||||||
| 2164 | const Instruction *I = cast<Instruction>(U); | ||||||||
| 2165 | if (!NullPointerIsDefined(I->getFunction(), | ||||||||
| 2166 | V->getType()->getPointerAddressSpace()) && | ||||||||
| 2167 | DT->dominates(I, CtxI)) | ||||||||
| 2168 | return true; | ||||||||
| 2169 | } | ||||||||
| 2170 | |||||||||
| 2171 | // Consider only compare instructions uniquely controlling a branch | ||||||||
| 2172 | Value *RHS; | ||||||||
| 2173 | CmpInst::Predicate Pred; | ||||||||
| 2174 | if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) | ||||||||
| 2175 | continue; | ||||||||
| 2176 | |||||||||
| 2177 | bool NonNullIfTrue; | ||||||||
| 2178 | if (cmpExcludesZero(Pred, RHS)) | ||||||||
| 2179 | NonNullIfTrue = true; | ||||||||
| 2180 | else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS)) | ||||||||
| 2181 | NonNullIfTrue = false; | ||||||||
| 2182 | else | ||||||||
| 2183 | continue; | ||||||||
| 2184 | |||||||||
| 2185 | SmallVector<const User *, 4> WorkList; | ||||||||
| 2186 | SmallPtrSet<const User *, 4> Visited; | ||||||||
| 2187 | for (auto *CmpU : U->users()) { | ||||||||
| 2188 | assert(WorkList.empty() && "Should be!")((void)0); | ||||||||
| 2189 | if (Visited.insert(CmpU).second) | ||||||||
| 2190 | WorkList.push_back(CmpU); | ||||||||
| 2191 | |||||||||
| 2192 | while (!WorkList.empty()) { | ||||||||
| 2193 | auto *Curr = WorkList.pop_back_val(); | ||||||||
| 2194 | |||||||||
| 2195 | // If a user is an AND, add all its users to the work list. We only | ||||||||
| 2196 | // propagate "pred != null" condition through AND because it is only | ||||||||
| 2197 | // correct to assume that all conditions of AND are met in true branch. | ||||||||
| 2198 | // TODO: Support similar logic of OR and EQ predicate? | ||||||||
| 2199 | if (NonNullIfTrue) | ||||||||
| 2200 | if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) { | ||||||||
| 2201 | for (auto *CurrU : Curr->users()) | ||||||||
| 2202 | if (Visited.insert(CurrU).second) | ||||||||
| 2203 | WorkList.push_back(CurrU); | ||||||||
| 2204 | continue; | ||||||||
| 2205 | } | ||||||||
| 2206 | |||||||||
| 2207 | if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) { | ||||||||
| 2208 | assert(BI->isConditional() && "uses a comparison!")((void)0); | ||||||||
| 2209 | |||||||||
| 2210 | BasicBlock *NonNullSuccessor = | ||||||||
| 2211 | BI->getSuccessor(NonNullIfTrue ? 0 : 1); | ||||||||
| 2212 | BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); | ||||||||
| 2213 | if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) | ||||||||
| 2214 | return true; | ||||||||
| 2215 | } else if (NonNullIfTrue && isGuard(Curr) && | ||||||||
| 2216 | DT->dominates(cast<Instruction>(Curr), CtxI)) { | ||||||||
| 2217 | return true; | ||||||||
| 2218 | } | ||||||||
| 2219 | } | ||||||||
| 2220 | } | ||||||||
| 2221 | } | ||||||||
| 2222 | |||||||||
| 2223 | return false; | ||||||||
| 2224 | } | ||||||||
| 2225 | |||||||||
| 2226 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) | ||||||||
| 2227 | /// ensure that the value it's attached to is never Value? 'RangeType' is | ||||||||
| 2228 | /// is the type of the value described by the range. | ||||||||
| 2229 | static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { | ||||||||
| 2230 | const unsigned NumRanges = Ranges->getNumOperands() / 2; | ||||||||
| 2231 | assert(NumRanges >= 1)((void)0); | ||||||||
| 2232 | for (unsigned i = 0; i < NumRanges; ++i) { | ||||||||
| 2233 | ConstantInt *Lower = | ||||||||
| 2234 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); | ||||||||
| 2235 | ConstantInt *Upper = | ||||||||
| 2236 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); | ||||||||
| 2237 | ConstantRange Range(Lower->getValue(), Upper->getValue()); | ||||||||
| 2238 | if (Range.contains(Value)) | ||||||||
| 2239 | return false; | ||||||||
| 2240 | } | ||||||||
| 2241 | return true; | ||||||||
| 2242 | } | ||||||||
| 2243 | |||||||||
| 2244 | /// Try to detect a recurrence that monotonically increases/decreases from a | ||||||||
| 2245 | /// non-zero starting value. These are common as induction variables. | ||||||||
| 2246 | static bool isNonZeroRecurrence(const PHINode *PN) { | ||||||||
| 2247 | BinaryOperator *BO = nullptr; | ||||||||
| 2248 | Value *Start = nullptr, *Step = nullptr; | ||||||||
| 2249 | const APInt *StartC, *StepC; | ||||||||
| 2250 | if (!matchSimpleRecurrence(PN, BO, Start, Step) || | ||||||||
| 2251 | !match(Start, m_APInt(StartC)) || StartC->isNullValue()) | ||||||||
| 2252 | return false; | ||||||||
| 2253 | |||||||||
| 2254 | switch (BO->getOpcode()) { | ||||||||
| 2255 | case Instruction::Add: | ||||||||
| 2256 | // Starting from non-zero and stepping away from zero can never wrap back | ||||||||
| 2257 | // to zero. | ||||||||
| 2258 | return BO->hasNoUnsignedWrap() || | ||||||||
| 2259 | (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) && | ||||||||
| 2260 | StartC->isNegative() == StepC->isNegative()); | ||||||||
| 2261 | case Instruction::Mul: | ||||||||
| 2262 | return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) && | ||||||||
| 2263 | match(Step, m_APInt(StepC)) && !StepC->isNullValue(); | ||||||||
| 2264 | case Instruction::Shl: | ||||||||
| 2265 | return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap(); | ||||||||
| 2266 | case Instruction::AShr: | ||||||||
| 2267 | case Instruction::LShr: | ||||||||
| 2268 | return BO->isExact(); | ||||||||
| 2269 | default: | ||||||||
| 2270 | return false; | ||||||||
| 2271 | } | ||||||||
| 2272 | } | ||||||||
| 2273 | |||||||||
| 2274 | /// Return true if the given value is known to be non-zero when defined. For | ||||||||
| 2275 | /// vectors, return true if every demanded element is known to be non-zero when | ||||||||
| 2276 | /// defined. For pointers, if the context instruction and dominator tree are | ||||||||
| 2277 | /// specified, perform context-sensitive analysis and return true if the | ||||||||
| 2278 | /// pointer couldn't possibly be null at the specified instruction. | ||||||||
| 2279 | /// Supports values with integer or pointer type and vectors of integers. | ||||||||
| 2280 | bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth, | ||||||||
| 2281 | const Query &Q) { | ||||||||
| 2282 | // FIXME: We currently have no way to represent the DemandedElts of a scalable | ||||||||
| 2283 | // vector | ||||||||
| 2284 | if (isa<ScalableVectorType>(V->getType())) | ||||||||
| 2285 | return false; | ||||||||
| 2286 | |||||||||
| 2287 | if (auto *C = dyn_cast<Constant>(V)) { | ||||||||
| 2288 | if (C->isNullValue()) | ||||||||
| 2289 | return false; | ||||||||
| 2290 | if (isa<ConstantInt>(C)) | ||||||||
| 2291 | // Must be non-zero due to null test above. | ||||||||
| 2292 | return true; | ||||||||
| 2293 | |||||||||
| 2294 | if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||||||||
| 2295 | // See the comment for IntToPtr/PtrToInt instructions below. | ||||||||
| 2296 | if (CE->getOpcode() == Instruction::IntToPtr || | ||||||||
| 2297 | CE->getOpcode() == Instruction::PtrToInt) | ||||||||
| 2298 | if (Q.DL.getTypeSizeInBits(CE->getOperand(0)->getType()) | ||||||||
| 2299 | .getFixedSize() <= | ||||||||
| 2300 | Q.DL.getTypeSizeInBits(CE->getType()).getFixedSize()) | ||||||||
| 2301 | return isKnownNonZero(CE->getOperand(0), Depth, Q); | ||||||||
| 2302 | } | ||||||||
| 2303 | |||||||||
| 2304 | // For constant vectors, check that all elements are undefined or known | ||||||||
| 2305 | // non-zero to determine that the whole vector is known non-zero. | ||||||||
| 2306 | if (auto *VecTy = dyn_cast<FixedVectorType>(C->getType())) { | ||||||||
| 2307 | for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { | ||||||||
| 2308 | if (!DemandedElts[i]) | ||||||||
| 2309 | continue; | ||||||||
| 2310 | Constant *Elt = C->getAggregateElement(i); | ||||||||
| 2311 | if (!Elt || Elt->isNullValue()) | ||||||||
| 2312 | return false; | ||||||||
| 2313 | if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt)) | ||||||||
| 2314 | return false; | ||||||||
| 2315 | } | ||||||||
| 2316 | return true; | ||||||||
| 2317 | } | ||||||||
| 2318 | |||||||||
| 2319 | // A global variable in address space 0 is non null unless extern weak | ||||||||
| 2320 | // or an absolute symbol reference. Other address spaces may have null as a | ||||||||
| 2321 | // valid address for a global, so we can't assume anything. | ||||||||
| 2322 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { | ||||||||
| 2323 | if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && | ||||||||
| 2324 | GV->getType()->getAddressSpace() == 0) | ||||||||
| 2325 | return true; | ||||||||
| 2326 | } else | ||||||||
| 2327 | return false; | ||||||||
| 2328 | } | ||||||||
| 2329 | |||||||||
| 2330 | if (auto *I = dyn_cast<Instruction>(V)) { | ||||||||
| 2331 | if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { | ||||||||
| 2332 | // If the possible ranges don't contain zero, then the value is | ||||||||
| 2333 | // definitely non-zero. | ||||||||
| 2334 | if (auto *Ty = dyn_cast<IntegerType>(V->getType())) { | ||||||||
| 2335 | const APInt ZeroValue(Ty->getBitWidth(), 0); | ||||||||
| 2336 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) | ||||||||
| 2337 | return true; | ||||||||
| 2338 | } | ||||||||
| 2339 | } | ||||||||
| 2340 | } | ||||||||
| 2341 | |||||||||
| 2342 | if (isKnownNonZeroFromAssume(V, Q)) | ||||||||
| 2343 | return true; | ||||||||
| 2344 | |||||||||
| 2345 | // Some of the tests below are recursive, so bail out if we hit the limit. | ||||||||
| 2346 | if (Depth++ >= MaxAnalysisRecursionDepth) | ||||||||
| 2347 | return false; | ||||||||
| 2348 | |||||||||
| 2349 | // Check for pointer simplifications. | ||||||||
| 2350 | |||||||||
| 2351 | if (PointerType *PtrTy = dyn_cast<PointerType>(V->getType())) { | ||||||||
| 2352 | // Alloca never returns null, malloc might. | ||||||||
| 2353 | if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0) | ||||||||
| 2354 | return true; | ||||||||
| 2355 | |||||||||
| 2356 | // A byval, inalloca may not be null in a non-default addres space. A | ||||||||
| 2357 | // nonnull argument is assumed never 0. | ||||||||
| 2358 | if (const Argument *A = dyn_cast<Argument>(V)) { | ||||||||
| 2359 | if (((A->hasPassPointeeByValueCopyAttr() && | ||||||||
| 2360 | !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) || | ||||||||
| 2361 | A->hasNonNullAttr())) | ||||||||
| 2362 | return true; | ||||||||
| 2363 | } | ||||||||
| 2364 | |||||||||
| 2365 | // A Load tagged with nonnull metadata is never null. | ||||||||
| 2366 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) | ||||||||
| 2367 | if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull)) | ||||||||
| 2368 | return true; | ||||||||
| 2369 | |||||||||
| 2370 | if (const auto *Call = dyn_cast<CallBase>(V)) { | ||||||||
| 2371 | if (Call->isReturnNonNull()) | ||||||||
| 2372 | return true; | ||||||||
| 2373 | if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) | ||||||||
| 2374 | return isKnownNonZero(RP, Depth, Q); | ||||||||
| 2375 | } | ||||||||
| 2376 | } | ||||||||
| 2377 | |||||||||
| 2378 | if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) | ||||||||
| 2379 | return true; | ||||||||
| 2380 | |||||||||
| 2381 | // Check for recursive pointer simplifications. | ||||||||
| 2382 | if (V->getType()->isPointerTy()) { | ||||||||
| 2383 | // Look through bitcast operations, GEPs, and int2ptr instructions as they | ||||||||
| 2384 | // do not alter the value, or at least not the nullness property of the | ||||||||
| 2385 | // value, e.g., int2ptr is allowed to zero/sign extend the value. | ||||||||
| 2386 | // | ||||||||
| 2387 | // Note that we have to take special care to avoid looking through | ||||||||
| 2388 | // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well | ||||||||
| 2389 | // as casts that can alter the value, e.g., AddrSpaceCasts. | ||||||||
| 2390 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) | ||||||||
| 2391 | return isGEPKnownNonNull(GEP, Depth, Q); | ||||||||
| 2392 | |||||||||
| 2393 | if (auto *BCO = dyn_cast<BitCastOperator>(V)) | ||||||||
| 2394 | return isKnownNonZero(BCO->getOperand(0), Depth, Q); | ||||||||
| 2395 | |||||||||
| 2396 | if (auto *I2P = dyn_cast<IntToPtrInst>(V)) | ||||||||
| 2397 | if (Q.DL.getTypeSizeInBits(I2P->getSrcTy()).getFixedSize() <= | ||||||||
| 2398 | Q.DL.getTypeSizeInBits(I2P->getDestTy()).getFixedSize()) | ||||||||
| 2399 | return isKnownNonZero(I2P->getOperand(0), Depth, Q); | ||||||||
| 2400 | } | ||||||||
| 2401 | |||||||||
| 2402 | // Similar to int2ptr above, we can look through ptr2int here if the cast | ||||||||
| 2403 | // is a no-op or an extend and not a truncate. | ||||||||
| 2404 | if (auto *P2I = dyn_cast<PtrToIntInst>(V)) | ||||||||
| 2405 | if (Q.DL.getTypeSizeInBits(P2I->getSrcTy()).getFixedSize() <= | ||||||||
| 2406 | Q.DL.getTypeSizeInBits(P2I->getDestTy()).getFixedSize()) | ||||||||
| 2407 | return isKnownNonZero(P2I->getOperand(0), Depth, Q); | ||||||||
| 2408 | |||||||||
| 2409 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL); | ||||||||
| 2410 | |||||||||
| 2411 | // X | Y != 0 if X != 0 or Y != 0. | ||||||||
| 2412 | Value *X = nullptr, *Y = nullptr; | ||||||||
| 2413 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) | ||||||||
| 2414 | return isKnownNonZero(X, DemandedElts, Depth, Q) || | ||||||||
| 2415 | isKnownNonZero(Y, DemandedElts, Depth, Q); | ||||||||
| 2416 | |||||||||
| 2417 | // ext X != 0 if X != 0. | ||||||||
| 2418 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) | ||||||||
| 2419 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q); | ||||||||
| 2420 | |||||||||
| 2421 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined | ||||||||
| 2422 | // if the lowest bit is shifted off the end. | ||||||||
| 2423 | if (match(V, m_Shl(m_Value(X), m_Value(Y)))) { | ||||||||
| 2424 | // shl nuw can't remove any non-zero bits. | ||||||||
| 2425 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); | ||||||||
| 2426 | if (Q.IIQ.hasNoUnsignedWrap(BO)) | ||||||||
| 2427 | return isKnownNonZero(X, Depth, Q); | ||||||||
| 2428 | |||||||||
| 2429 | KnownBits Known(BitWidth); | ||||||||
| 2430 | computeKnownBits(X, DemandedElts, Known, Depth, Q); | ||||||||
| 2431 | if (Known.One[0]) | ||||||||
| 2432 | return true; | ||||||||
| 2433 | } | ||||||||
| 2434 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not | ||||||||
| 2435 | // defined if the sign bit is shifted off the end. | ||||||||
| 2436 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { | ||||||||
| 2437 | // shr exact can only shift out zero bits. | ||||||||
| 2438 | const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); | ||||||||
| 2439 | if (BO->isExact()) | ||||||||
| 2440 | return isKnownNonZero(X, Depth, Q); | ||||||||
| 2441 | |||||||||
| 2442 | KnownBits Known = computeKnownBits(X, DemandedElts, Depth, Q); | ||||||||
| 2443 | if (Known.isNegative()) | ||||||||
| 2444 | return true; | ||||||||
| 2445 | |||||||||
| 2446 | // If the shifter operand is a constant, and all of the bits shifted | ||||||||
| 2447 | // out are known to be zero, and X is known non-zero then at least one | ||||||||
| 2448 | // non-zero bit must remain. | ||||||||
| 2449 | if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) { | ||||||||
| 2450 | auto ShiftVal = Shift->getLimitedValue(BitWidth - 1); | ||||||||
| 2451 | // Is there a known one in the portion not shifted out? | ||||||||
| 2452 | if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal) | ||||||||
| 2453 | return true; | ||||||||
| 2454 | // Are all the bits to be shifted out known zero? | ||||||||
| 2455 | if (Known.countMinTrailingZeros() >= ShiftVal) | ||||||||
| 2456 | return isKnownNonZero(X, DemandedElts, Depth, Q); | ||||||||
| 2457 | } | ||||||||
| 2458 | } | ||||||||
| 2459 | // div exact can only produce a zero if the dividend is zero. | ||||||||
| 2460 | else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) { | ||||||||
| 2461 | return isKnownNonZero(X, DemandedElts, Depth, Q); | ||||||||
| 2462 | } | ||||||||
| 2463 | // X + Y. | ||||||||
| 2464 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { | ||||||||
| 2465 | KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q); | ||||||||
| 2466 | KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q); | ||||||||
| 2467 | |||||||||
| 2468 | // If X and Y are both non-negative (as signed values) then their sum is not | ||||||||
| 2469 | // zero unless both X and Y are zero. | ||||||||
| 2470 | if (XKnown.isNonNegative() && YKnown.isNonNegative()) | ||||||||
| 2471 | if (isKnownNonZero(X, DemandedElts, Depth, Q) || | ||||||||
| 2472 | isKnownNonZero(Y, DemandedElts, Depth, Q)) | ||||||||
| 2473 | return true; | ||||||||
| 2474 | |||||||||
| 2475 | // If X and Y are both negative (as signed values) then their sum is not | ||||||||
| 2476 | // zero unless both X and Y equal INT_MIN. | ||||||||
| 2477 | if (XKnown.isNegative() && YKnown.isNegative()) { | ||||||||
| 2478 | APInt Mask = APInt::getSignedMaxValue(BitWidth); | ||||||||
| 2479 | // The sign bit of X is set. If some other bit is set then X is not equal | ||||||||
| 2480 | // to INT_MIN. | ||||||||
| 2481 | if (XKnown.One.intersects(Mask)) | ||||||||
| 2482 | return true; | ||||||||
| 2483 | // The sign bit of Y is set. If some other bit is set then Y is not equal | ||||||||
| 2484 | // to INT_MIN. | ||||||||
| 2485 | if (YKnown.One.intersects(Mask)) | ||||||||
| 2486 | return true; | ||||||||
| 2487 | } | ||||||||
| 2488 | |||||||||
| 2489 | // The sum of a non-negative number and a power of two is not zero. | ||||||||
| 2490 | if (XKnown.isNonNegative() && | ||||||||
| 2491 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) | ||||||||
| 2492 | return true; | ||||||||
| 2493 | if (YKnown.isNonNegative() && | ||||||||
| 2494 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) | ||||||||
| 2495 | return true; | ||||||||
| 2496 | } | ||||||||
| 2497 | // X * Y. | ||||||||
| 2498 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { | ||||||||
| 2499 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); | ||||||||
| 2500 | // If X and Y are non-zero then so is X * Y as long as the multiplication | ||||||||
| 2501 | // does not overflow. | ||||||||
| 2502 | if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) && | ||||||||
| 2503 | isKnownNonZero(X, DemandedElts, Depth, Q) && | ||||||||
| 2504 | isKnownNonZero(Y, DemandedElts, Depth, Q)) | ||||||||
| 2505 | return true; | ||||||||
| 2506 | } | ||||||||
| 2507 | // (C ? X : Y) != 0 if X != 0 and Y != 0. | ||||||||
| 2508 | else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { | ||||||||
| 2509 | if (isKnownNonZero(SI->getTrueValue(), DemandedElts, Depth, Q) && | ||||||||
| 2510 | isKnownNonZero(SI->getFalseValue(), DemandedElts, Depth, Q)) | ||||||||
| 2511 | return true; | ||||||||
| 2512 | } | ||||||||
| 2513 | // PHI | ||||||||
| 2514 | else if (const PHINode *PN = dyn_cast<PHINode>(V)) { | ||||||||
| 2515 | if (Q.IIQ.UseInstrInfo && isNonZeroRecurrence(PN)) | ||||||||
| 2516 | return true; | ||||||||
| 2517 | |||||||||
| 2518 | // Check if all incoming values are non-zero using recursion. | ||||||||
| 2519 | Query RecQ = Q; | ||||||||
| 2520 | unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); | ||||||||
| 2521 | return llvm::all_of(PN->operands(), [&](const Use &U) { | ||||||||
| 2522 | if (U.get() == PN) | ||||||||
| 2523 | return true; | ||||||||
| 2524 | RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); | ||||||||
| 2525 | return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ); | ||||||||
| 2526 | }); | ||||||||
| 2527 | } | ||||||||
| 2528 | // ExtractElement | ||||||||
| 2529 | else if (const auto *EEI = dyn_cast<ExtractElementInst>(V)) { | ||||||||
| 2530 | const Value *Vec = EEI->getVectorOperand(); | ||||||||
| 2531 | const Value *Idx = EEI->getIndexOperand(); | ||||||||
| 2532 | auto *CIdx = dyn_cast<ConstantInt>(Idx); | ||||||||
| 2533 | if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) { | ||||||||
| 2534 | unsigned NumElts = VecTy->getNumElements(); | ||||||||
| 2535 | APInt DemandedVecElts = APInt::getAllOnesValue(NumElts); | ||||||||
| 2536 | if (CIdx && CIdx->getValue().ult(NumElts)) | ||||||||
| 2537 | DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); | ||||||||
| 2538 | return isKnownNonZero(Vec, DemandedVecElts, Depth, Q); | ||||||||
| 2539 | } | ||||||||
| 2540 | } | ||||||||
| 2541 | // Freeze | ||||||||
| 2542 | else if (const FreezeInst *FI = dyn_cast<FreezeInst>(V)) { | ||||||||
| 2543 | auto *Op = FI->getOperand(0); | ||||||||
| 2544 | if (isKnownNonZero(Op, Depth, Q) && | ||||||||
| 2545 | isGuaranteedNotToBePoison(Op, Q.AC, Q.CxtI, Q.DT, Depth)) | ||||||||
| 2546 | return true; | ||||||||
| 2547 | } | ||||||||
| 2548 | |||||||||
| 2549 | KnownBits Known(BitWidth); | ||||||||
| 2550 | computeKnownBits(V, DemandedElts, Known, Depth, Q); | ||||||||
| 2551 | return Known.One != 0; | ||||||||
| 2552 | } | ||||||||
| 2553 | |||||||||
| 2554 | bool isKnownNonZero(const Value* V, unsigned Depth, const Query& Q) { | ||||||||
| 2555 | // FIXME: We currently have no way to represent the DemandedElts of a scalable | ||||||||
| 2556 | // vector | ||||||||
| 2557 | if (isa<ScalableVectorType>(V->getType())) | ||||||||
| 2558 | return false; | ||||||||
| 2559 | |||||||||
| 2560 | auto *FVTy = dyn_cast<FixedVectorType>(V->getType()); | ||||||||
| 2561 | APInt DemandedElts = | ||||||||
| 2562 | FVTy ? APInt::getAllOnesValue(FVTy->getNumElements()) : APInt(1, 1); | ||||||||
| 2563 | return isKnownNonZero(V, DemandedElts, Depth, Q); | ||||||||
| 2564 | } | ||||||||
| 2565 | |||||||||
| 2566 | /// If the pair of operators are the same invertible function, return the | ||||||||
| 2567 | /// the operands of the function corresponding to each input. Otherwise, | ||||||||
| 2568 | /// return None. An invertible function is one that is 1-to-1 and maps | ||||||||
| 2569 | /// every input value to exactly one output value. This is equivalent to | ||||||||
| 2570 | /// saying that Op1 and Op2 are equal exactly when the specified pair of | ||||||||
| 2571 | /// operands are equal, (except that Op1 and Op2 may be poison more often.) | ||||||||
| 2572 | static Optional<std::pair<Value*, Value*>> | ||||||||
| 2573 | getInvertibleOperands(const Operator *Op1, | ||||||||
| 2574 | const Operator *Op2) { | ||||||||
| 2575 | if (Op1->getOpcode() != Op2->getOpcode()) | ||||||||
| 2576 | return None; | ||||||||
| 2577 | |||||||||
| 2578 | auto getOperands = [&](unsigned OpNum) -> auto { | ||||||||
| 2579 | return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum)); | ||||||||
| 2580 | }; | ||||||||
| 2581 | |||||||||
| 2582 | switch (Op1->getOpcode()) { | ||||||||
| 2583 | default: | ||||||||
| 2584 | break; | ||||||||
| 2585 | case Instruction::Add: | ||||||||
| 2586 | case Instruction::Sub: | ||||||||
| 2587 | if (Op1->getOperand(0) == Op2->getOperand(0)) | ||||||||
| 2588 | return getOperands(1); | ||||||||
| 2589 | if (Op1->getOperand(1) == Op2->getOperand(1)) | ||||||||
| 2590 | return getOperands(0); | ||||||||
| 2591 | break; | ||||||||
| 2592 | case Instruction::Mul: { | ||||||||
| 2593 | // invertible if A * B == (A * B) mod 2^N where A, and B are integers | ||||||||
| 2594 | // and N is the bitwdith. The nsw case is non-obvious, but proven by | ||||||||
| 2595 | // alive2: https://alive2.llvm.org/ce/z/Z6D5qK | ||||||||
| 2596 | auto *OBO1 = cast<OverflowingBinaryOperator>(Op1); | ||||||||
| 2597 | auto *OBO2 = cast<OverflowingBinaryOperator>(Op2); | ||||||||
| 2598 | if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && | ||||||||
| 2599 | (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) | ||||||||
| 2600 | break; | ||||||||
| 2601 | |||||||||
| 2602 | // Assume operand order has been canonicalized | ||||||||
| 2603 | if (Op1->getOperand(1) == Op2->getOperand(1) && | ||||||||
| 2604 | isa<ConstantInt>(Op1->getOperand(1)) && | ||||||||
| 2605 | !cast<ConstantInt>(Op1->getOperand(1))->isZero()) | ||||||||
| 2606 | return getOperands(0); | ||||||||
| 2607 | break; | ||||||||
| 2608 | } | ||||||||
| 2609 | case Instruction::Shl: { | ||||||||
| 2610 | // Same as multiplies, with the difference that we don't need to check | ||||||||
| 2611 | // for a non-zero multiply. Shifts always multiply by non-zero. | ||||||||
| 2612 | auto *OBO1 = cast<OverflowingBinaryOperator>(Op1); | ||||||||
| 2613 | auto *OBO2 = cast<OverflowingBinaryOperator>(Op2); | ||||||||
| 2614 | if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && | ||||||||
| 2615 | (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) | ||||||||
| 2616 | break; | ||||||||
| 2617 | |||||||||
| 2618 | if (Op1->getOperand(1) == Op2->getOperand(1)) | ||||||||
| 2619 | return getOperands(0); | ||||||||
| 2620 | break; | ||||||||
| 2621 | } | ||||||||
| 2622 | case Instruction::AShr: | ||||||||
| 2623 | case Instruction::LShr: { | ||||||||
| 2624 | auto *PEO1 = cast<PossiblyExactOperator>(Op1); | ||||||||
| 2625 | auto *PEO2 = cast<PossiblyExactOperator>(Op2); | ||||||||
| 2626 | if (!PEO1->isExact() || !PEO2->isExact()) | ||||||||
| 2627 | break; | ||||||||
| 2628 | |||||||||
| 2629 | if (Op1->getOperand(1) == Op2->getOperand(1)) | ||||||||
| 2630 | return getOperands(0); | ||||||||
| 2631 | break; | ||||||||
| 2632 | } | ||||||||
| 2633 | case Instruction::SExt: | ||||||||
| 2634 | case Instruction::ZExt: | ||||||||
| 2635 | if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType()) | ||||||||
| 2636 | return getOperands(0); | ||||||||
| 2637 | break; | ||||||||
| 2638 | case Instruction::PHI: { | ||||||||
| 2639 | const PHINode *PN1 = cast<PHINode>(Op1); | ||||||||
| 2640 | const PHINode *PN2 = cast<PHINode>(Op2); | ||||||||
| 2641 | |||||||||
| 2642 | // If PN1 and PN2 are both recurrences, can we prove the entire recurrences | ||||||||
| 2643 | // are a single invertible function of the start values? Note that repeated | ||||||||
| 2644 | // application of an invertible function is also invertible | ||||||||
| 2645 | BinaryOperator *BO1 = nullptr; | ||||||||
| 2646 | Value *Start1 = nullptr, *Step1 = nullptr; | ||||||||
| 2647 | BinaryOperator *BO2 = nullptr; | ||||||||
| 2648 | Value *Start2 = nullptr, *Step2 = nullptr; | ||||||||
| 2649 | if (PN1->getParent() != PN2->getParent() || | ||||||||
| 2650 | !matchSimpleRecurrence(PN1, BO1, Start1, Step1) || | ||||||||
| 2651 | !matchSimpleRecurrence(PN2, BO2, Start2, Step2)) | ||||||||
| 2652 | break; | ||||||||
| 2653 | |||||||||
| 2654 | auto Values = getInvertibleOperands(cast<Operator>(BO1), | ||||||||
| 2655 | cast<Operator>(BO2)); | ||||||||
| 2656 | if (!Values) | ||||||||
| 2657 | break; | ||||||||
| 2658 | |||||||||
| 2659 | // We have to be careful of mutually defined recurrences here. Ex: | ||||||||
| 2660 | // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V | ||||||||
| 2661 | // * X_i = Y_i = X_(i-1) OP Y_(i-1) | ||||||||
| 2662 | // The invertibility of these is complicated, and not worth reasoning | ||||||||
| 2663 | // about (yet?). | ||||||||
| 2664 | if (Values->first != PN1 || Values->second != PN2) | ||||||||
| 2665 | break; | ||||||||
| 2666 | |||||||||
| 2667 | return std::make_pair(Start1, Start2); | ||||||||
| 2668 | } | ||||||||
| 2669 | } | ||||||||
| 2670 | return None; | ||||||||
| 2671 | } | ||||||||
| 2672 | |||||||||
| 2673 | /// Return true if V2 == V1 + X, where X is known non-zero. | ||||||||
| 2674 | static bool isAddOfNonZero(const Value *V1, const Value *V2, unsigned Depth, | ||||||||
| 2675 | const Query &Q) { | ||||||||
| 2676 | const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1); | ||||||||
| 2677 | if (!BO || BO->getOpcode() != Instruction::Add) | ||||||||
| 2678 | return false; | ||||||||
| 2679 | Value *Op = nullptr; | ||||||||
| 2680 | if (V2 == BO->getOperand(0)) | ||||||||
| 2681 | Op = BO->getOperand(1); | ||||||||
| 2682 | else if (V2 == BO->getOperand(1)) | ||||||||
| 2683 | Op = BO->getOperand(0); | ||||||||
| 2684 | else | ||||||||
| 2685 | return false; | ||||||||
| 2686 | return isKnownNonZero(Op, Depth + 1, Q); | ||||||||
| 2687 | } | ||||||||
| 2688 | |||||||||
| 2689 | /// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and | ||||||||
| 2690 | /// the multiplication is nuw or nsw. | ||||||||
| 2691 | static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth, | ||||||||
| 2692 | const Query &Q) { | ||||||||
| 2693 | if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) { | ||||||||
| 2694 | const APInt *C; | ||||||||
| 2695 | return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) && | ||||||||
| 2696 | (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && | ||||||||
| 2697 | !C->isNullValue() && !C->isOneValue() && | ||||||||
| 2698 | isKnownNonZero(V1, Depth + 1, Q); | ||||||||
| 2699 | } | ||||||||
| 2700 | return false; | ||||||||
| 2701 | } | ||||||||
| 2702 | |||||||||
| 2703 | /// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and | ||||||||
| 2704 | /// the shift is nuw or nsw. | ||||||||
| 2705 | static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth, | ||||||||
| 2706 | const Query &Q) { | ||||||||
| 2707 | if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) { | ||||||||
| 2708 | const APInt *C; | ||||||||
| 2709 | return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) && | ||||||||
| 2710 | (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && | ||||||||
| 2711 | !C->isNullValue() && isKnownNonZero(V1, Depth + 1, Q); | ||||||||
| 2712 | } | ||||||||
| 2713 | return false; | ||||||||
| 2714 | } | ||||||||
| 2715 | |||||||||
| 2716 | static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, | ||||||||
| 2717 | unsigned Depth, const Query &Q) { | ||||||||
| 2718 | // Check two PHIs are in same block. | ||||||||
| 2719 | if (PN1->getParent() != PN2->getParent()) | ||||||||
| 2720 | return false; | ||||||||
| 2721 | |||||||||
| 2722 | SmallPtrSet<const BasicBlock *, 8> VisitedBBs; | ||||||||
| 2723 | bool UsedFullRecursion = false; | ||||||||
| 2724 | for (const BasicBlock *IncomBB : PN1->blocks()) { | ||||||||
| 2725 | if (!VisitedBBs.insert(IncomBB).second) | ||||||||
| 2726 | continue; // Don't reprocess blocks that we have dealt with already. | ||||||||
| 2727 | const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB); | ||||||||
| 2728 | const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB); | ||||||||
| 2729 | const APInt *C1, *C2; | ||||||||
| 2730 | if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2) | ||||||||
| 2731 | continue; | ||||||||
| 2732 | |||||||||
| 2733 | // Only one pair of phi operands is allowed for full recursion. | ||||||||
| 2734 | if (UsedFullRecursion) | ||||||||
| 2735 | return false; | ||||||||
| 2736 | |||||||||
| 2737 | Query RecQ = Q; | ||||||||
| 2738 | RecQ.CxtI = IncomBB->getTerminator(); | ||||||||
| 2739 | if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ)) | ||||||||
| 2740 | return false; | ||||||||
| 2741 | UsedFullRecursion = true; | ||||||||
| 2742 | } | ||||||||
| 2743 | return true; | ||||||||
| 2744 | } | ||||||||
| 2745 | |||||||||
| 2746 | /// Return true if it is known that V1 != V2. | ||||||||
| 2747 | static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, | ||||||||
| 2748 | const Query &Q) { | ||||||||
| 2749 | if (V1 == V2) | ||||||||
| 2750 | return false; | ||||||||
| 2751 | if (V1->getType() != V2->getType()) | ||||||||
| 2752 | // We can't look through casts yet. | ||||||||
| 2753 | return false; | ||||||||
| 2754 | |||||||||
| 2755 | if (Depth >= MaxAnalysisRecursionDepth) | ||||||||
| 2756 | return false; | ||||||||
| 2757 | |||||||||
| 2758 | // See if we can recurse through (exactly one of) our operands. This | ||||||||
| 2759 | // requires our operation be 1-to-1 and map every input value to exactly | ||||||||
| 2760 | // one output value. Such an operation is invertible. | ||||||||
| 2761 | auto *O1 = dyn_cast<Operator>(V1); | ||||||||
| 2762 | auto *O2 = dyn_cast<Operator>(V2); | ||||||||
| 2763 | if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) { | ||||||||
| 2764 | if (auto Values = getInvertibleOperands(O1, O2)) | ||||||||
| 2765 | return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q); | ||||||||
| 2766 | |||||||||
| 2767 | if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) { | ||||||||
| 2768 | const PHINode *PN2 = cast<PHINode>(V2); | ||||||||
| 2769 | // FIXME: This is missing a generalization to handle the case where one is | ||||||||
| 2770 | // a PHI and another one isn't. | ||||||||
| 2771 | if (isNonEqualPHIs(PN1, PN2, Depth, Q)) | ||||||||
| 2772 | return true; | ||||||||
| 2773 | }; | ||||||||
| 2774 | } | ||||||||
| 2775 | |||||||||
| 2776 | if (isAddOfNonZero(V1, V2, Depth, Q) || isAddOfNonZero(V2, V1, Depth, Q)) | ||||||||
| 2777 | return true; | ||||||||
| 2778 | |||||||||
| 2779 | if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q)) | ||||||||
| 2780 | return true; | ||||||||
| 2781 | |||||||||
| 2782 | if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q)) | ||||||||
| 2783 | return true; | ||||||||
| 2784 | |||||||||
| 2785 | if (V1->getType()->isIntOrIntVectorTy()) { | ||||||||
| 2786 | // Are any known bits in V1 contradictory to known bits in V2? If V1 | ||||||||
| 2787 | // has a known zero where V2 has a known one, they must not be equal. | ||||||||
| 2788 | KnownBits Known1 = computeKnownBits(V1, Depth, Q); | ||||||||
| 2789 | KnownBits Known2 = computeKnownBits(V2, Depth, Q); | ||||||||
| 2790 | |||||||||
| 2791 | if (Known1.Zero.intersects(Known2.One) || | ||||||||
| 2792 | Known2.Zero.intersects(Known1.One)) | ||||||||
| 2793 | return true; | ||||||||
| 2794 | } | ||||||||
| 2795 | return false; | ||||||||
| 2796 | } | ||||||||
| 2797 | |||||||||
| 2798 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to | ||||||||
| 2799 | /// simplify operations downstream. Mask is known to be zero for bits that V | ||||||||
| 2800 | /// cannot have. | ||||||||
| 2801 | /// | ||||||||
| 2802 | /// This function is defined on values with integer type, values with pointer | ||||||||
| 2803 | /// type, and vectors of integers. In the case | ||||||||
| 2804 | /// where V is a vector, the mask, known zero, and known one values are the | ||||||||
| 2805 | /// same width as the vector element, and the bit is set only if it is true | ||||||||
| 2806 | /// for all of the elements in the vector. | ||||||||
| 2807 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, | ||||||||
| 2808 | const Query &Q) { | ||||||||
| 2809 | KnownBits Known(Mask.getBitWidth()); | ||||||||
| 2810 | computeKnownBits(V, Known, Depth, Q); | ||||||||
| 2811 | return Mask.isSubsetOf(Known.Zero); | ||||||||
| 2812 | } | ||||||||
| 2813 | |||||||||
| 2814 | // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). | ||||||||
| 2815 | // Returns the input and lower/upper bounds. | ||||||||
| 2816 | static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, | ||||||||
| 2817 | const APInt *&CLow, const APInt *&CHigh) { | ||||||||
| 2818 | assert(isa<Operator>(Select) &&((void)0) | ||||||||
| 2819 | cast<Operator>(Select)->getOpcode() == Instruction::Select &&((void)0) | ||||||||
| 2820 | "Input should be a Select!")((void)0); | ||||||||
| 2821 | |||||||||
| 2822 | const Value *LHS = nullptr, *RHS = nullptr; | ||||||||
| 2823 | SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; | ||||||||
| 2824 | if (SPF != SPF_SMAX && SPF != SPF_SMIN) | ||||||||
| 2825 | return false; | ||||||||
| 2826 | |||||||||
| 2827 | if (!match(RHS, m_APInt(CLow))) | ||||||||
| 2828 | return false; | ||||||||
| 2829 | |||||||||
| 2830 | const Value *LHS2 = nullptr, *RHS2 = nullptr; | ||||||||
| 2831 | SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; | ||||||||
| 2832 | if (getInverseMinMaxFlavor(SPF) != SPF2) | ||||||||
| 2833 | return false; | ||||||||
| 2834 | |||||||||
| 2835 | if (!match(RHS2, m_APInt(CHigh))) | ||||||||
| 2836 | return false; | ||||||||
| 2837 | |||||||||
| 2838 | if (SPF == SPF_SMIN) | ||||||||
| 2839 | std::swap(CLow, CHigh); | ||||||||
| 2840 | |||||||||
| 2841 | In = LHS2; | ||||||||
| 2842 | return CLow->sle(*CHigh); | ||||||||
| 2843 | } | ||||||||
| 2844 | |||||||||
| 2845 | /// For vector constants, loop over the elements and find the constant with the | ||||||||
| 2846 | /// minimum number of sign bits. Return 0 if the value is not a vector constant | ||||||||
| 2847 | /// or if any element was not analyzed; otherwise, return the count for the | ||||||||
| 2848 | /// element with the minimum number of sign bits. | ||||||||
| 2849 | static unsigned computeNumSignBitsVectorConstant(const Value *V, | ||||||||
| 2850 | const APInt &DemandedElts, | ||||||||
| 2851 | unsigned TyBits) { | ||||||||
| 2852 | const auto *CV = dyn_cast<Constant>(V); | ||||||||
| 2853 | if (!CV || !isa<FixedVectorType>(CV->getType())) | ||||||||
| 2854 | return 0; | ||||||||
| 2855 | |||||||||
| 2856 | unsigned MinSignBits = TyBits; | ||||||||
| 2857 | unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements(); | ||||||||
| 2858 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||
| 2859 | if (!DemandedElts[i]) | ||||||||
| 2860 | continue; | ||||||||
| 2861 | // If we find a non-ConstantInt, bail out. | ||||||||
| 2862 | auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i)); | ||||||||
| 2863 | if (!Elt) | ||||||||
| 2864 | return 0; | ||||||||
| 2865 | |||||||||
| 2866 | MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); | ||||||||
| 2867 | } | ||||||||
| 2868 | |||||||||
| 2869 | return MinSignBits; | ||||||||
| 2870 | } | ||||||||
| 2871 | |||||||||
| 2872 | static unsigned ComputeNumSignBitsImpl(const Value *V, | ||||||||
| 2873 | const APInt &DemandedElts, | ||||||||
| 2874 | unsigned Depth, const Query &Q); | ||||||||
| 2875 | |||||||||
| 2876 | static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, | ||||||||
| 2877 | unsigned Depth, const Query &Q) { | ||||||||
| 2878 | unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q); | ||||||||
| 2879 | assert(Result > 0 && "At least one sign bit needs to be present!")((void)0); | ||||||||
| 2880 | return Result; | ||||||||
| 2881 | } | ||||||||
| 2882 | |||||||||
| 2883 | /// Return the number of times the sign bit of the register is replicated into | ||||||||
| 2884 | /// the other bits. We know that at least 1 bit is always equal to the sign bit | ||||||||
| 2885 | /// (itself), but other cases can give us information. For example, immediately | ||||||||
| 2886 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each | ||||||||
| 2887 | /// other, so we return 3. For vectors, return the number of sign bits for the | ||||||||
| 2888 | /// vector element with the minimum number of known sign bits of the demanded | ||||||||
| 2889 | /// elements in the vector specified by DemandedElts. | ||||||||
| 2890 | static unsigned ComputeNumSignBitsImpl(const Value *V, | ||||||||
| 2891 | const APInt &DemandedElts, | ||||||||
| 2892 | unsigned Depth, const Query &Q) { | ||||||||
| 2893 | Type *Ty = V->getType(); | ||||||||
| 2894 | |||||||||
| 2895 | // FIXME: We currently have no way to represent the DemandedElts of a scalable | ||||||||
| 2896 | // vector | ||||||||
| 2897 | if (isa<ScalableVectorType>(Ty)) | ||||||||
| 2898 | return 1; | ||||||||
| 2899 | |||||||||
| 2900 | #ifndef NDEBUG1 | ||||||||
| 2901 | assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth")((void)0); | ||||||||
| 2902 | |||||||||
| 2903 | if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) { | ||||||||
| 2904 | assert(((void)0) | ||||||||
| 2905 | FVTy->getNumElements() == DemandedElts.getBitWidth() &&((void)0) | ||||||||
| 2906 | "DemandedElt width should equal the fixed vector number of elements")((void)0); | ||||||||
| 2907 | } else { | ||||||||
| 2908 | assert(DemandedElts == APInt(1, 1) &&((void)0) | ||||||||
| 2909 | "DemandedElt width should be 1 for scalars")((void)0); | ||||||||
| 2910 | } | ||||||||
| 2911 | #endif | ||||||||
| 2912 | |||||||||
| 2913 | // We return the minimum number of sign bits that are guaranteed to be present | ||||||||
| 2914 | // in V, so for undef we have to conservatively return 1. We don't have the | ||||||||
| 2915 | // same behavior for poison though -- that's a FIXME today. | ||||||||
| 2916 | |||||||||
| 2917 | Type *ScalarTy = Ty->getScalarType(); | ||||||||
| 2918 | unsigned TyBits = ScalarTy->isPointerTy() ? | ||||||||
| 2919 | Q.DL.getPointerTypeSizeInBits(ScalarTy) : | ||||||||
| 2920 | Q.DL.getTypeSizeInBits(ScalarTy); | ||||||||
| 2921 | |||||||||
| 2922 | unsigned Tmp, Tmp2; | ||||||||
| 2923 | unsigned FirstAnswer = 1; | ||||||||
| 2924 | |||||||||
| 2925 | // Note that ConstantInt is handled by the general computeKnownBits case | ||||||||
| 2926 | // below. | ||||||||
| 2927 | |||||||||
| 2928 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 2929 | return 1; | ||||||||
| 2930 | |||||||||
| 2931 | if (auto *U = dyn_cast<Operator>(V)) { | ||||||||
| 2932 | switch (Operator::getOpcode(V)) { | ||||||||
| 2933 | default: break; | ||||||||
| 2934 | case Instruction::SExt: | ||||||||
| 2935 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); | ||||||||
| 2936 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; | ||||||||
| 2937 | |||||||||
| 2938 | case Instruction::SDiv: { | ||||||||
| 2939 | const APInt *Denominator; | ||||||||
| 2940 | // sdiv X, C -> adds log(C) sign bits. | ||||||||
| 2941 | if (match(U->getOperand(1), m_APInt(Denominator))) { | ||||||||
| 2942 | |||||||||
| 2943 | // Ignore non-positive denominator. | ||||||||
| 2944 | if (!Denominator->isStrictlyPositive()) | ||||||||
| 2945 | break; | ||||||||
| 2946 | |||||||||
| 2947 | // Calculate the incoming numerator bits. | ||||||||
| 2948 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 2949 | |||||||||
| 2950 | // Add floor(log(C)) bits to the numerator bits. | ||||||||
| 2951 | return std::min(TyBits, NumBits + Denominator->logBase2()); | ||||||||
| 2952 | } | ||||||||
| 2953 | break; | ||||||||
| 2954 | } | ||||||||
| 2955 | |||||||||
| 2956 | case Instruction::SRem: { | ||||||||
| 2957 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 2958 | |||||||||
| 2959 | const APInt *Denominator; | ||||||||
| 2960 | // srem X, C -> we know that the result is within [-C+1,C) when C is a | ||||||||
| 2961 | // positive constant. This let us put a lower bound on the number of sign | ||||||||
| 2962 | // bits. | ||||||||
| 2963 | if (match(U->getOperand(1), m_APInt(Denominator))) { | ||||||||
| 2964 | |||||||||
| 2965 | // Ignore non-positive denominator. | ||||||||
| 2966 | if (Denominator->isStrictlyPositive()) { | ||||||||
| 2967 | // Calculate the leading sign bit constraints by examining the | ||||||||
| 2968 | // denominator. Given that the denominator is positive, there are two | ||||||||
| 2969 | // cases: | ||||||||
| 2970 | // | ||||||||
| 2971 | // 1. The numerator is positive. The result range is [0,C) and | ||||||||
| 2972 | // [0,C) u< (1 << ceilLogBase2(C)). | ||||||||
| 2973 | // | ||||||||
| 2974 | // 2. The numerator is negative. Then the result range is (-C,0] and | ||||||||
| 2975 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). | ||||||||
| 2976 | // | ||||||||
| 2977 | // Thus a lower bound on the number of sign bits is `TyBits - | ||||||||
| 2978 | // ceilLogBase2(C)`. | ||||||||
| 2979 | |||||||||
| 2980 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); | ||||||||
| 2981 | Tmp = std::max(Tmp, ResBits); | ||||||||
| 2982 | } | ||||||||
| 2983 | } | ||||||||
| 2984 | return Tmp; | ||||||||
| 2985 | } | ||||||||
| 2986 | |||||||||
| 2987 | case Instruction::AShr: { | ||||||||
| 2988 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 2989 | // ashr X, C -> adds C sign bits. Vectors too. | ||||||||
| 2990 | const APInt *ShAmt; | ||||||||
| 2991 | if (match(U->getOperand(1), m_APInt(ShAmt))) { | ||||||||
| 2992 | if (ShAmt->uge(TyBits)) | ||||||||
| 2993 | break; // Bad shift. | ||||||||
| 2994 | unsigned ShAmtLimited = ShAmt->getZExtValue(); | ||||||||
| 2995 | Tmp += ShAmtLimited; | ||||||||
| 2996 | if (Tmp > TyBits) Tmp = TyBits; | ||||||||
| 2997 | } | ||||||||
| 2998 | return Tmp; | ||||||||
| 2999 | } | ||||||||
| 3000 | case Instruction::Shl: { | ||||||||
| 3001 | const APInt *ShAmt; | ||||||||
| 3002 | if (match(U->getOperand(1), m_APInt(ShAmt))) { | ||||||||
| 3003 | // shl destroys sign bits. | ||||||||
| 3004 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3005 | if (ShAmt->uge(TyBits) || // Bad shift. | ||||||||
| 3006 | ShAmt->uge(Tmp)) break; // Shifted all sign bits out. | ||||||||
| 3007 | Tmp2 = ShAmt->getZExtValue(); | ||||||||
| 3008 | return Tmp - Tmp2; | ||||||||
| 3009 | } | ||||||||
| 3010 | break; | ||||||||
| 3011 | } | ||||||||
| 3012 | case Instruction::And: | ||||||||
| 3013 | case Instruction::Or: | ||||||||
| 3014 | case Instruction::Xor: // NOT is handled here. | ||||||||
| 3015 | // Logical binary ops preserve the number of sign bits at the worst. | ||||||||
| 3016 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3017 | if (Tmp != 1) { | ||||||||
| 3018 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||||||
| 3019 | FirstAnswer = std::min(Tmp, Tmp2); | ||||||||
| 3020 | // We computed what we know about the sign bits as our first | ||||||||
| 3021 | // answer. Now proceed to the generic code that uses | ||||||||
| 3022 | // computeKnownBits, and pick whichever answer is better. | ||||||||
| 3023 | } | ||||||||
| 3024 | break; | ||||||||
| 3025 | |||||||||
| 3026 | case Instruction::Select: { | ||||||||
| 3027 | // If we have a clamp pattern, we know that the number of sign bits will | ||||||||
| 3028 | // be the minimum of the clamp min/max range. | ||||||||
| 3029 | const Value *X; | ||||||||
| 3030 | const APInt *CLow, *CHigh; | ||||||||
| 3031 | if (isSignedMinMaxClamp(U, X, CLow, CHigh)) | ||||||||
| 3032 | return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); | ||||||||
| 3033 | |||||||||
| 3034 | Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||||||
| 3035 | if (Tmp == 1) break; | ||||||||
| 3036 | Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); | ||||||||
| 3037 | return std::min(Tmp, Tmp2); | ||||||||
| 3038 | } | ||||||||
| 3039 | |||||||||
| 3040 | case Instruction::Add: | ||||||||
| 3041 | // Add can have at most one carry bit. Thus we know that the output | ||||||||
| 3042 | // is, at worst, one more bit than the inputs. | ||||||||
| 3043 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3044 | if (Tmp == 1) break; | ||||||||
| 3045 | |||||||||
| 3046 | // Special case decrementing a value (ADD X, -1): | ||||||||
| 3047 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) | ||||||||
| 3048 | if (CRHS->isAllOnesValue()) { | ||||||||
| 3049 | KnownBits Known(TyBits); | ||||||||
| 3050 | computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); | ||||||||
| 3051 | |||||||||
| 3052 | // If the input is known to be 0 or 1, the output is 0/-1, which is | ||||||||
| 3053 | // all sign bits set. | ||||||||
| 3054 | if ((Known.Zero | 1).isAllOnesValue()) | ||||||||
| 3055 | return TyBits; | ||||||||
| 3056 | |||||||||
| 3057 | // If we are subtracting one from a positive number, there is no carry | ||||||||
| 3058 | // out of the result. | ||||||||
| 3059 | if (Known.isNonNegative()) | ||||||||
| 3060 | return Tmp; | ||||||||
| 3061 | } | ||||||||
| 3062 | |||||||||
| 3063 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||||||
| 3064 | if (Tmp2 == 1) break; | ||||||||
| 3065 | return std::min(Tmp, Tmp2) - 1; | ||||||||
| 3066 | |||||||||
| 3067 | case Instruction::Sub: | ||||||||
| 3068 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||||||
| 3069 | if (Tmp2 == 1) break; | ||||||||
| 3070 | |||||||||
| 3071 | // Handle NEG. | ||||||||
| 3072 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) | ||||||||
| 3073 | if (CLHS->isNullValue()) { | ||||||||
| 3074 | KnownBits Known(TyBits); | ||||||||
| 3075 | computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); | ||||||||
| 3076 | // If the input is known to be 0 or 1, the output is 0/-1, which is | ||||||||
| 3077 | // all sign bits set. | ||||||||
| 3078 | if ((Known.Zero | 1).isAllOnesValue()) | ||||||||
| 3079 | return TyBits; | ||||||||
| 3080 | |||||||||
| 3081 | // If the input is known to be positive (the sign bit is known clear), | ||||||||
| 3082 | // the output of the NEG has the same number of sign bits as the | ||||||||
| 3083 | // input. | ||||||||
| 3084 | if (Known.isNonNegative()) | ||||||||
| 3085 | return Tmp2; | ||||||||
| 3086 | |||||||||
| 3087 | // Otherwise, we treat this like a SUB. | ||||||||
| 3088 | } | ||||||||
| 3089 | |||||||||
| 3090 | // Sub can have at most one carry bit. Thus we know that the output | ||||||||
| 3091 | // is, at worst, one more bit than the inputs. | ||||||||
| 3092 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3093 | if (Tmp == 1) break; | ||||||||
| 3094 | return std::min(Tmp, Tmp2) - 1; | ||||||||
| 3095 | |||||||||
| 3096 | case Instruction::Mul: { | ||||||||
| 3097 | // The output of the Mul can be at most twice the valid bits in the | ||||||||
| 3098 | // inputs. | ||||||||
| 3099 | unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3100 | if (SignBitsOp0 == 1) break; | ||||||||
| 3101 | unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); | ||||||||
| 3102 | if (SignBitsOp1 == 1) break; | ||||||||
| 3103 | unsigned OutValidBits = | ||||||||
| 3104 | (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); | ||||||||
| 3105 | return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; | ||||||||
| 3106 | } | ||||||||
| 3107 | |||||||||
| 3108 | case Instruction::PHI: { | ||||||||
| 3109 | const PHINode *PN = cast<PHINode>(U); | ||||||||
| 3110 | unsigned NumIncomingValues = PN->getNumIncomingValues(); | ||||||||
| 3111 | // Don't analyze large in-degree PHIs. | ||||||||
| 3112 | if (NumIncomingValues > 4) break; | ||||||||
| 3113 | // Unreachable blocks may have zero-operand PHI nodes. | ||||||||
| 3114 | if (NumIncomingValues == 0) break; | ||||||||
| 3115 | |||||||||
| 3116 | // Take the minimum of all incoming values. This can't infinitely loop | ||||||||
| 3117 | // because of our depth threshold. | ||||||||
| 3118 | Query RecQ = Q; | ||||||||
| 3119 | Tmp = TyBits; | ||||||||
| 3120 | for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) { | ||||||||
| 3121 | if (Tmp == 1) return Tmp; | ||||||||
| 3122 | RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator(); | ||||||||
| 3123 | Tmp = std::min( | ||||||||
| 3124 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ)); | ||||||||
| 3125 | } | ||||||||
| 3126 | return Tmp; | ||||||||
| 3127 | } | ||||||||
| 3128 | |||||||||
| 3129 | case Instruction::Trunc: | ||||||||
| 3130 | // FIXME: it's tricky to do anything useful for this, but it is an | ||||||||
| 3131 | // important case for targets like X86. | ||||||||
| 3132 | break; | ||||||||
| 3133 | |||||||||
| 3134 | case Instruction::ExtractElement: | ||||||||
| 3135 | // Look through extract element. At the moment we keep this simple and | ||||||||
| 3136 | // skip tracking the specific element. But at least we might find | ||||||||
| 3137 | // information valid for all elements of the vector (for example if vector | ||||||||
| 3138 | // is sign extended, shifted, etc). | ||||||||
| 3139 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3140 | |||||||||
| 3141 | case Instruction::ShuffleVector: { | ||||||||
| 3142 | // Collect the minimum number of sign bits that are shared by every vector | ||||||||
| 3143 | // element referenced by the shuffle. | ||||||||
| 3144 | auto *Shuf = dyn_cast<ShuffleVectorInst>(U); | ||||||||
| 3145 | if (!Shuf) { | ||||||||
| 3146 | // FIXME: Add support for shufflevector constant expressions. | ||||||||
| 3147 | return 1; | ||||||||
| 3148 | } | ||||||||
| 3149 | APInt DemandedLHS, DemandedRHS; | ||||||||
| 3150 | // For undef elements, we don't know anything about the common state of | ||||||||
| 3151 | // the shuffle result. | ||||||||
| 3152 | if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) | ||||||||
| 3153 | return 1; | ||||||||
| 3154 | Tmp = std::numeric_limits<unsigned>::max(); | ||||||||
| 3155 | if (!!DemandedLHS) { | ||||||||
| 3156 | const Value *LHS = Shuf->getOperand(0); | ||||||||
| 3157 | Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q); | ||||||||
| 3158 | } | ||||||||
| 3159 | // If we don't know anything, early out and try computeKnownBits | ||||||||
| 3160 | // fall-back. | ||||||||
| 3161 | if (Tmp == 1) | ||||||||
| 3162 | break; | ||||||||
| 3163 | if (!!DemandedRHS) { | ||||||||
| 3164 | const Value *RHS = Shuf->getOperand(1); | ||||||||
| 3165 | Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q); | ||||||||
| 3166 | Tmp = std::min(Tmp, Tmp2); | ||||||||
| 3167 | } | ||||||||
| 3168 | // If we don't know anything, early out and try computeKnownBits | ||||||||
| 3169 | // fall-back. | ||||||||
| 3170 | if (Tmp == 1) | ||||||||
| 3171 | break; | ||||||||
| 3172 | assert(Tmp <= TyBits && "Failed to determine minimum sign bits")((void)0); | ||||||||
| 3173 | return Tmp; | ||||||||
| 3174 | } | ||||||||
| 3175 | case Instruction::Call: { | ||||||||
| 3176 | if (const auto *II = dyn_cast<IntrinsicInst>(U)) { | ||||||||
| 3177 | switch (II->getIntrinsicID()) { | ||||||||
| 3178 | default: break; | ||||||||
| 3179 | case Intrinsic::abs: | ||||||||
| 3180 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); | ||||||||
| 3181 | if (Tmp == 1) break; | ||||||||
| 3182 | |||||||||
| 3183 | // Absolute value reduces number of sign bits by at most 1. | ||||||||
| 3184 | return Tmp - 1; | ||||||||
| 3185 | } | ||||||||
| 3186 | } | ||||||||
| 3187 | } | ||||||||
| 3188 | } | ||||||||
| 3189 | } | ||||||||
| 3190 | |||||||||
| 3191 | // Finally, if we can prove that the top bits of the result are 0's or 1's, | ||||||||
| 3192 | // use this information. | ||||||||
| 3193 | |||||||||
| 3194 | // If we can examine all elements of a vector constant successfully, we're | ||||||||
| 3195 | // done (we can't do any better than that). If not, keep trying. | ||||||||
| 3196 | if (unsigned VecSignBits = | ||||||||
| 3197 | computeNumSignBitsVectorConstant(V, DemandedElts, TyBits)) | ||||||||
| 3198 | return VecSignBits; | ||||||||
| 3199 | |||||||||
| 3200 | KnownBits Known(TyBits); | ||||||||
| 3201 | computeKnownBits(V, DemandedElts, Known, Depth, Q); | ||||||||
| 3202 | |||||||||
| 3203 | // If we know that the sign bit is either zero or one, determine the number of | ||||||||
| 3204 | // identical bits in the top of the input value. | ||||||||
| 3205 | return std::max(FirstAnswer, Known.countMinSignBits()); | ||||||||
| 3206 | } | ||||||||
| 3207 | |||||||||
| 3208 | /// This function computes the integer multiple of Base that equals V. | ||||||||
| 3209 | /// If successful, it returns true and returns the multiple in | ||||||||
| 3210 | /// Multiple. If unsuccessful, it returns false. It looks | ||||||||
| 3211 | /// through SExt instructions only if LookThroughSExt is true. | ||||||||
| 3212 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, | ||||||||
| 3213 | bool LookThroughSExt, unsigned Depth) { | ||||||||
| 3214 | assert(V && "No Value?")((void)0); | ||||||||
| 3215 | assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth")((void)0); | ||||||||
| 3216 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!")((void)0); | ||||||||
| 3217 | |||||||||
| 3218 | Type *T = V->getType(); | ||||||||
| 3219 | |||||||||
| 3220 | ConstantInt *CI = dyn_cast<ConstantInt>(V); | ||||||||
| 3221 | |||||||||
| 3222 | if (Base == 0) | ||||||||
| 3223 | return false; | ||||||||
| 3224 | |||||||||
| 3225 | if (Base == 1) { | ||||||||
| 3226 | Multiple = V; | ||||||||
| 3227 | return true; | ||||||||
| 3228 | } | ||||||||
| 3229 | |||||||||
| 3230 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); | ||||||||
| 3231 | Constant *BaseVal = ConstantInt::get(T, Base); | ||||||||
| 3232 | if (CO && CO == BaseVal) { | ||||||||
| 3233 | // Multiple is 1. | ||||||||
| 3234 | Multiple = ConstantInt::get(T, 1); | ||||||||
| 3235 | return true; | ||||||||
| 3236 | } | ||||||||
| 3237 | |||||||||
| 3238 | if (CI && CI->getZExtValue() % Base == 0) { | ||||||||
| 3239 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); | ||||||||
| 3240 | return true; | ||||||||
| 3241 | } | ||||||||
| 3242 | |||||||||
| 3243 | if (Depth == MaxAnalysisRecursionDepth) return false; | ||||||||
| 3244 | |||||||||
| 3245 | Operator *I = dyn_cast<Operator>(V); | ||||||||
| 3246 | if (!I) return false; | ||||||||
| 3247 | |||||||||
| 3248 | switch (I->getOpcode()) { | ||||||||
| 3249 | default: break; | ||||||||
| 3250 | case Instruction::SExt: | ||||||||
| 3251 | if (!LookThroughSExt) return false; | ||||||||
| 3252 | // otherwise fall through to ZExt | ||||||||
| 3253 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||
| 3254 | case Instruction::ZExt: | ||||||||
| 3255 | return ComputeMultiple(I->getOperand(0), Base, Multiple, | ||||||||
| 3256 | LookThroughSExt, Depth+1); | ||||||||
| 3257 | case Instruction::Shl: | ||||||||
| 3258 | case Instruction::Mul: { | ||||||||
| 3259 | Value *Op0 = I->getOperand(0); | ||||||||
| 3260 | Value *Op1 = I->getOperand(1); | ||||||||
| 3261 | |||||||||
| 3262 | if (I->getOpcode() == Instruction::Shl) { | ||||||||
| 3263 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); | ||||||||
| 3264 | if (!Op1CI) return false; | ||||||||
| 3265 | // Turn Op0 << Op1 into Op0 * 2^Op1 | ||||||||
| 3266 | APInt Op1Int = Op1CI->getValue(); | ||||||||
| 3267 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); | ||||||||
| 3268 | APInt API(Op1Int.getBitWidth(), 0); | ||||||||
| 3269 | API.setBit(BitToSet); | ||||||||
| 3270 | Op1 = ConstantInt::get(V->getContext(), API); | ||||||||
| 3271 | } | ||||||||
| 3272 | |||||||||
| 3273 | Value *Mul0 = nullptr; | ||||||||
| 3274 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { | ||||||||
| 3275 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) | ||||||||
| 3276 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { | ||||||||
| 3277 | if (Op1C->getType()->getPrimitiveSizeInBits().getFixedSize() < | ||||||||
| 3278 | MulC->getType()->getPrimitiveSizeInBits().getFixedSize()) | ||||||||
| 3279 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); | ||||||||
| 3280 | if (Op1C->getType()->getPrimitiveSizeInBits().getFixedSize() > | ||||||||
| 3281 | MulC->getType()->getPrimitiveSizeInBits().getFixedSize()) | ||||||||
| 3282 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); | ||||||||
| 3283 | |||||||||
| 3284 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) | ||||||||
| 3285 | Multiple = ConstantExpr::getMul(MulC, Op1C); | ||||||||
| 3286 | return true; | ||||||||
| 3287 | } | ||||||||
| 3288 | |||||||||
| 3289 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) | ||||||||
| 3290 | if (Mul0CI->getValue() == 1) { | ||||||||
| 3291 | // V == Base * Op1, so return Op1 | ||||||||
| 3292 | Multiple = Op1; | ||||||||
| 3293 | return true; | ||||||||
| 3294 | } | ||||||||
| 3295 | } | ||||||||
| 3296 | |||||||||
| 3297 | Value *Mul1 = nullptr; | ||||||||
| 3298 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { | ||||||||
| 3299 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) | ||||||||
| 3300 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { | ||||||||
| 3301 | if (Op0C->getType()->getPrimitiveSizeInBits().getFixedSize() < | ||||||||
| 3302 | MulC->getType()->getPrimitiveSizeInBits().getFixedSize()) | ||||||||
| 3303 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); | ||||||||
| 3304 | if (Op0C->getType()->getPrimitiveSizeInBits().getFixedSize() > | ||||||||
| 3305 | MulC->getType()->getPrimitiveSizeInBits().getFixedSize()) | ||||||||
| 3306 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); | ||||||||
| 3307 | |||||||||
| 3308 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) | ||||||||
| 3309 | Multiple = ConstantExpr::getMul(MulC, Op0C); | ||||||||
| 3310 | return true; | ||||||||
| 3311 | } | ||||||||
| 3312 | |||||||||
| 3313 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) | ||||||||
| 3314 | if (Mul1CI->getValue() == 1) { | ||||||||
| 3315 | // V == Base * Op0, so return Op0 | ||||||||
| 3316 | Multiple = Op0; | ||||||||
| 3317 | return true; | ||||||||
| 3318 | } | ||||||||
| 3319 | } | ||||||||
| 3320 | } | ||||||||
| 3321 | } | ||||||||
| 3322 | |||||||||
| 3323 | // We could not determine if V is a multiple of Base. | ||||||||
| 3324 | return false; | ||||||||
| 3325 | } | ||||||||
| 3326 | |||||||||
| 3327 | Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB, | ||||||||
| 3328 | const TargetLibraryInfo *TLI) { | ||||||||
| 3329 | const Function *F = CB.getCalledFunction(); | ||||||||
| 3330 | if (!F) | ||||||||
| 3331 | return Intrinsic::not_intrinsic; | ||||||||
| 3332 | |||||||||
| 3333 | if (F->isIntrinsic()) | ||||||||
| 3334 | return F->getIntrinsicID(); | ||||||||
| 3335 | |||||||||
| 3336 | // We are going to infer semantics of a library function based on mapping it | ||||||||
| 3337 | // to an LLVM intrinsic. Check that the library function is available from | ||||||||
| 3338 | // this callbase and in this environment. | ||||||||
| 3339 | LibFunc Func; | ||||||||
| 3340 | if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) || | ||||||||
| 3341 | !CB.onlyReadsMemory()) | ||||||||
| 3342 | return Intrinsic::not_intrinsic; | ||||||||
| 3343 | |||||||||
| 3344 | switch (Func) { | ||||||||
| 3345 | default: | ||||||||
| 3346 | break; | ||||||||
| 3347 | case LibFunc_sin: | ||||||||
| 3348 | case LibFunc_sinf: | ||||||||
| 3349 | case LibFunc_sinl: | ||||||||
| 3350 | return Intrinsic::sin; | ||||||||
| 3351 | case LibFunc_cos: | ||||||||
| 3352 | case LibFunc_cosf: | ||||||||
| 3353 | case LibFunc_cosl: | ||||||||
| 3354 | return Intrinsic::cos; | ||||||||
| 3355 | case LibFunc_exp: | ||||||||
| 3356 | case LibFunc_expf: | ||||||||
| 3357 | case LibFunc_expl: | ||||||||
| 3358 | return Intrinsic::exp; | ||||||||
| 3359 | case LibFunc_exp2: | ||||||||
| 3360 | case LibFunc_exp2f: | ||||||||
| 3361 | case LibFunc_exp2l: | ||||||||
| 3362 | return Intrinsic::exp2; | ||||||||
| 3363 | case LibFunc_log: | ||||||||
| 3364 | case LibFunc_logf: | ||||||||
| 3365 | case LibFunc_logl: | ||||||||
| 3366 | return Intrinsic::log; | ||||||||
| 3367 | case LibFunc_log10: | ||||||||
| 3368 | case LibFunc_log10f: | ||||||||
| 3369 | case LibFunc_log10l: | ||||||||
| 3370 | return Intrinsic::log10; | ||||||||
| 3371 | case LibFunc_log2: | ||||||||
| 3372 | case LibFunc_log2f: | ||||||||
| 3373 | case LibFunc_log2l: | ||||||||
| 3374 | return Intrinsic::log2; | ||||||||
| 3375 | case LibFunc_fabs: | ||||||||
| 3376 | case LibFunc_fabsf: | ||||||||
| 3377 | case LibFunc_fabsl: | ||||||||
| 3378 | return Intrinsic::fabs; | ||||||||
| 3379 | case LibFunc_fmin: | ||||||||
| 3380 | case LibFunc_fminf: | ||||||||
| 3381 | case LibFunc_fminl: | ||||||||
| 3382 | return Intrinsic::minnum; | ||||||||
| 3383 | case LibFunc_fmax: | ||||||||
| 3384 | case LibFunc_fmaxf: | ||||||||
| 3385 | case LibFunc_fmaxl: | ||||||||
| 3386 | return Intrinsic::maxnum; | ||||||||
| 3387 | case LibFunc_copysign: | ||||||||
| 3388 | case LibFunc_copysignf: | ||||||||
| 3389 | case LibFunc_copysignl: | ||||||||
| 3390 | return Intrinsic::copysign; | ||||||||
| 3391 | case LibFunc_floor: | ||||||||
| 3392 | case LibFunc_floorf: | ||||||||
| 3393 | case LibFunc_floorl: | ||||||||
| 3394 | return Intrinsic::floor; | ||||||||
| 3395 | case LibFunc_ceil: | ||||||||
| 3396 | case LibFunc_ceilf: | ||||||||
| 3397 | case LibFunc_ceill: | ||||||||
| 3398 | return Intrinsic::ceil; | ||||||||
| 3399 | case LibFunc_trunc: | ||||||||
| 3400 | case LibFunc_truncf: | ||||||||
| 3401 | case LibFunc_truncl: | ||||||||
| 3402 | return Intrinsic::trunc; | ||||||||
| 3403 | case LibFunc_rint: | ||||||||
| 3404 | case LibFunc_rintf: | ||||||||
| 3405 | case LibFunc_rintl: | ||||||||
| 3406 | return Intrinsic::rint; | ||||||||
| 3407 | case LibFunc_nearbyint: | ||||||||
| 3408 | case LibFunc_nearbyintf: | ||||||||
| 3409 | case LibFunc_nearbyintl: | ||||||||
| 3410 | return Intrinsic::nearbyint; | ||||||||
| 3411 | case LibFunc_round: | ||||||||
| 3412 | case LibFunc_roundf: | ||||||||
| 3413 | case LibFunc_roundl: | ||||||||
| 3414 | return Intrinsic::round; | ||||||||
| 3415 | case LibFunc_roundeven: | ||||||||
| 3416 | case LibFunc_roundevenf: | ||||||||
| 3417 | case LibFunc_roundevenl: | ||||||||
| 3418 | return Intrinsic::roundeven; | ||||||||
| 3419 | case LibFunc_pow: | ||||||||
| 3420 | case LibFunc_powf: | ||||||||
| 3421 | case LibFunc_powl: | ||||||||
| 3422 | return Intrinsic::pow; | ||||||||
| 3423 | case LibFunc_sqrt: | ||||||||
| 3424 | case LibFunc_sqrtf: | ||||||||
| 3425 | case LibFunc_sqrtl: | ||||||||
| 3426 | return Intrinsic::sqrt; | ||||||||
| 3427 | } | ||||||||
| 3428 | |||||||||
| 3429 | return Intrinsic::not_intrinsic; | ||||||||
| 3430 | } | ||||||||
| 3431 | |||||||||
| 3432 | /// Return true if we can prove that the specified FP value is never equal to | ||||||||
| 3433 | /// -0.0. | ||||||||
| 3434 | /// NOTE: Do not check 'nsz' here because that fast-math-flag does not guarantee | ||||||||
| 3435 | /// that a value is not -0.0. It only guarantees that -0.0 may be treated | ||||||||
| 3436 | /// the same as +0.0 in floating-point ops. | ||||||||
| 3437 | /// | ||||||||
| 3438 | /// NOTE: this function will need to be revisited when we support non-default | ||||||||
| 3439 | /// rounding modes! | ||||||||
| 3440 | bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, | ||||||||
| 3441 | unsigned Depth) { | ||||||||
| 3442 | if (auto *CFP = dyn_cast<ConstantFP>(V)) | ||||||||
| 3443 | return !CFP->getValueAPF().isNegZero(); | ||||||||
| 3444 | |||||||||
| 3445 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 3446 | return false; | ||||||||
| 3447 | |||||||||
| 3448 | auto *Op = dyn_cast<Operator>(V); | ||||||||
| 3449 | if (!Op) | ||||||||
| 3450 | return false; | ||||||||
| 3451 | |||||||||
| 3452 | // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. | ||||||||
| 3453 | if (match(Op, m_FAdd(m_Value(), m_PosZeroFP()))) | ||||||||
| 3454 | return true; | ||||||||
| 3455 | |||||||||
| 3456 | // sitofp and uitofp turn into +0.0 for zero. | ||||||||
| 3457 | if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) | ||||||||
| 3458 | return true; | ||||||||
| 3459 | |||||||||
| 3460 | if (auto *Call = dyn_cast<CallInst>(Op)) { | ||||||||
| 3461 | Intrinsic::ID IID = getIntrinsicForCallSite(*Call, TLI); | ||||||||
| 3462 | switch (IID) { | ||||||||
| 3463 | default: | ||||||||
| 3464 | break; | ||||||||
| 3465 | // sqrt(-0.0) = -0.0, no other negative results are possible. | ||||||||
| 3466 | case Intrinsic::sqrt: | ||||||||
| 3467 | case Intrinsic::canonicalize: | ||||||||
| 3468 | return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1); | ||||||||
| 3469 | // fabs(x) != -0.0 | ||||||||
| 3470 | case Intrinsic::fabs: | ||||||||
| 3471 | return true; | ||||||||
| 3472 | } | ||||||||
| 3473 | } | ||||||||
| 3474 | |||||||||
| 3475 | return false; | ||||||||
| 3476 | } | ||||||||
| 3477 | |||||||||
| 3478 | /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a | ||||||||
| 3479 | /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign | ||||||||
| 3480 | /// bit despite comparing equal. | ||||||||
| 3481 | static bool cannotBeOrderedLessThanZeroImpl(const Value *V, | ||||||||
| 3482 | const TargetLibraryInfo *TLI, | ||||||||
| 3483 | bool SignBitOnly, | ||||||||
| 3484 | unsigned Depth) { | ||||||||
| 3485 | // TODO: This function does not do the right thing when SignBitOnly is true | ||||||||
| 3486 | // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform | ||||||||
| 3487 | // which flips the sign bits of NaNs. See | ||||||||
| 3488 | // https://llvm.org/bugs/show_bug.cgi?id=31702. | ||||||||
| 3489 | |||||||||
| 3490 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { | ||||||||
| 3491 | return !CFP->getValueAPF().isNegative() || | ||||||||
| 3492 | (!SignBitOnly && CFP->getValueAPF().isZero()); | ||||||||
| 3493 | } | ||||||||
| 3494 | |||||||||
| 3495 | // Handle vector of constants. | ||||||||
| 3496 | if (auto *CV = dyn_cast<Constant>(V)) { | ||||||||
| 3497 | if (auto *CVFVTy = dyn_cast<FixedVectorType>(CV->getType())) { | ||||||||
| 3498 | unsigned NumElts = CVFVTy->getNumElements(); | ||||||||
| 3499 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||
| 3500 | auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i)); | ||||||||
| 3501 | if (!CFP) | ||||||||
| 3502 | return false; | ||||||||
| 3503 | if (CFP->getValueAPF().isNegative() && | ||||||||
| 3504 | (SignBitOnly || !CFP->getValueAPF().isZero())) | ||||||||
| 3505 | return false; | ||||||||
| 3506 | } | ||||||||
| 3507 | |||||||||
| 3508 | // All non-negative ConstantFPs. | ||||||||
| 3509 | return true; | ||||||||
| 3510 | } | ||||||||
| 3511 | } | ||||||||
| 3512 | |||||||||
| 3513 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 3514 | return false; | ||||||||
| 3515 | |||||||||
| 3516 | const Operator *I = dyn_cast<Operator>(V); | ||||||||
| 3517 | if (!I) | ||||||||
| 3518 | return false; | ||||||||
| 3519 | |||||||||
| 3520 | switch (I->getOpcode()) { | ||||||||
| 3521 | default: | ||||||||
| 3522 | break; | ||||||||
| 3523 | // Unsigned integers are always nonnegative. | ||||||||
| 3524 | case Instruction::UIToFP: | ||||||||
| 3525 | return true; | ||||||||
| 3526 | case Instruction::FMul: | ||||||||
| 3527 | case Instruction::FDiv: | ||||||||
| 3528 | // X * X is always non-negative or a NaN. | ||||||||
| 3529 | // X / X is always exactly 1.0 or a NaN. | ||||||||
| 3530 | if (I->getOperand(0) == I->getOperand(1) && | ||||||||
| 3531 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs())) | ||||||||
| 3532 | return true; | ||||||||
| 3533 | |||||||||
| 3534 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||
| 3535 | case Instruction::FAdd: | ||||||||
| 3536 | case Instruction::FRem: | ||||||||
| 3537 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3538 | Depth + 1) && | ||||||||
| 3539 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||||||
| 3540 | Depth + 1); | ||||||||
| 3541 | case Instruction::Select: | ||||||||
| 3542 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||||||
| 3543 | Depth + 1) && | ||||||||
| 3544 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, | ||||||||
| 3545 | Depth + 1); | ||||||||
| 3546 | case Instruction::FPExt: | ||||||||
| 3547 | case Instruction::FPTrunc: | ||||||||
| 3548 | // Widening/narrowing never change sign. | ||||||||
| 3549 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3550 | Depth + 1); | ||||||||
| 3551 | case Instruction::ExtractElement: | ||||||||
| 3552 | // Look through extract element. At the moment we keep this simple and skip | ||||||||
| 3553 | // tracking the specific element. But at least we might find information | ||||||||
| 3554 | // valid for all elements of the vector. | ||||||||
| 3555 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3556 | Depth + 1); | ||||||||
| 3557 | case Instruction::Call: | ||||||||
| 3558 | const auto *CI = cast<CallInst>(I); | ||||||||
| 3559 | Intrinsic::ID IID = getIntrinsicForCallSite(*CI, TLI); | ||||||||
| 3560 | switch (IID) { | ||||||||
| 3561 | default: | ||||||||
| 3562 | break; | ||||||||
| 3563 | case Intrinsic::maxnum: { | ||||||||
| 3564 | Value *V0 = I->getOperand(0), *V1 = I->getOperand(1); | ||||||||
| 3565 | auto isPositiveNum = [&](Value *V) { | ||||||||
| 3566 | if (SignBitOnly) { | ||||||||
| 3567 | // With SignBitOnly, this is tricky because the result of | ||||||||
| 3568 | // maxnum(+0.0, -0.0) is unspecified. Just check if the operand is | ||||||||
| 3569 | // a constant strictly greater than 0.0. | ||||||||
| 3570 | const APFloat *C; | ||||||||
| 3571 | return match(V, m_APFloat(C)) && | ||||||||
| 3572 | *C > APFloat::getZero(C->getSemantics()); | ||||||||
| 3573 | } | ||||||||
| 3574 | |||||||||
| 3575 | // -0.0 compares equal to 0.0, so if this operand is at least -0.0, | ||||||||
| 3576 | // maxnum can't be ordered-less-than-zero. | ||||||||
| 3577 | return isKnownNeverNaN(V, TLI) && | ||||||||
| 3578 | cannotBeOrderedLessThanZeroImpl(V, TLI, false, Depth + 1); | ||||||||
| 3579 | }; | ||||||||
| 3580 | |||||||||
| 3581 | // TODO: This could be improved. We could also check that neither operand | ||||||||
| 3582 | // has its sign bit set (and at least 1 is not-NAN?). | ||||||||
| 3583 | return isPositiveNum(V0) || isPositiveNum(V1); | ||||||||
| 3584 | } | ||||||||
| 3585 | |||||||||
| 3586 | case Intrinsic::maximum: | ||||||||
| 3587 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3588 | Depth + 1) || | ||||||||
| 3589 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||||||
| 3590 | Depth + 1); | ||||||||
| 3591 | case Intrinsic::minnum: | ||||||||
| 3592 | case Intrinsic::minimum: | ||||||||
| 3593 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3594 | Depth + 1) && | ||||||||
| 3595 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, | ||||||||
| 3596 | Depth + 1); | ||||||||
| 3597 | case Intrinsic::exp: | ||||||||
| 3598 | case Intrinsic::exp2: | ||||||||
| 3599 | case Intrinsic::fabs: | ||||||||
| 3600 | return true; | ||||||||
| 3601 | |||||||||
| 3602 | case Intrinsic::sqrt: | ||||||||
| 3603 | // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. | ||||||||
| 3604 | if (!SignBitOnly) | ||||||||
| 3605 | return true; | ||||||||
| 3606 | return CI->hasNoNaNs() && (CI->hasNoSignedZeros() || | ||||||||
| 3607 | CannotBeNegativeZero(CI->getOperand(0), TLI)); | ||||||||
| 3608 | |||||||||
| 3609 | case Intrinsic::powi: | ||||||||
| 3610 | if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) { | ||||||||
| 3611 | // powi(x,n) is non-negative if n is even. | ||||||||
| 3612 | if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) | ||||||||
| 3613 | return true; | ||||||||
| 3614 | } | ||||||||
| 3615 | // TODO: This is not correct. Given that exp is an integer, here are the | ||||||||
| 3616 | // ways that pow can return a negative value: | ||||||||
| 3617 | // | ||||||||
| 3618 | // pow(x, exp) --> negative if exp is odd and x is negative. | ||||||||
| 3619 | // pow(-0, exp) --> -inf if exp is negative odd. | ||||||||
| 3620 | // pow(-0, exp) --> -0 if exp is positive odd. | ||||||||
| 3621 | // pow(-inf, exp) --> -0 if exp is negative odd. | ||||||||
| 3622 | // pow(-inf, exp) --> -inf if exp is positive odd. | ||||||||
| 3623 | // | ||||||||
| 3624 | // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, | ||||||||
| 3625 | // but we must return false if x == -0. Unfortunately we do not currently | ||||||||
| 3626 | // have a way of expressing this constraint. See details in | ||||||||
| 3627 | // https://llvm.org/bugs/show_bug.cgi?id=31702. | ||||||||
| 3628 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, | ||||||||
| 3629 | Depth + 1); | ||||||||
| 3630 | |||||||||
| 3631 | case Intrinsic::fma: | ||||||||
| 3632 | case Intrinsic::fmuladd: | ||||||||
| 3633 | // x*x+y is non-negative if y is non-negative. | ||||||||
| 3634 | return I->getOperand(0) == I->getOperand(1) && | ||||||||
| 3635 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) && | ||||||||
| 3636 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, | ||||||||
| 3637 | Depth + 1); | ||||||||
| 3638 | } | ||||||||
| 3639 | break; | ||||||||
| 3640 | } | ||||||||
| 3641 | return false; | ||||||||
| 3642 | } | ||||||||
| 3643 | |||||||||
| 3644 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, | ||||||||
| 3645 | const TargetLibraryInfo *TLI) { | ||||||||
| 3646 | return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0); | ||||||||
| 3647 | } | ||||||||
| 3648 | |||||||||
| 3649 | bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) { | ||||||||
| 3650 | return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0); | ||||||||
| 3651 | } | ||||||||
| 3652 | |||||||||
| 3653 | bool llvm::isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI, | ||||||||
| 3654 | unsigned Depth) { | ||||||||
| 3655 | assert(V->getType()->isFPOrFPVectorTy() && "Querying for Inf on non-FP type")((void)0); | ||||||||
| 3656 | |||||||||
| 3657 | // If we're told that infinities won't happen, assume they won't. | ||||||||
| 3658 | if (auto *FPMathOp = dyn_cast<FPMathOperator>(V)) | ||||||||
| 3659 | if (FPMathOp->hasNoInfs()) | ||||||||
| 3660 | return true; | ||||||||
| 3661 | |||||||||
| 3662 | // Handle scalar constants. | ||||||||
| 3663 | if (auto *CFP = dyn_cast<ConstantFP>(V)) | ||||||||
| 3664 | return !CFP->isInfinity(); | ||||||||
| 3665 | |||||||||
| 3666 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 3667 | return false; | ||||||||
| 3668 | |||||||||
| 3669 | if (auto *Inst = dyn_cast<Instruction>(V)) { | ||||||||
| 3670 | switch (Inst->getOpcode()) { | ||||||||
| 3671 | case Instruction::Select: { | ||||||||
| 3672 | return isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1) && | ||||||||
| 3673 | isKnownNeverInfinity(Inst->getOperand(2), TLI, Depth + 1); | ||||||||
| 3674 | } | ||||||||
| 3675 | case Instruction::SIToFP: | ||||||||
| 3676 | case Instruction::UIToFP: { | ||||||||
| 3677 | // Get width of largest magnitude integer (remove a bit if signed). | ||||||||
| 3678 | // This still works for a signed minimum value because the largest FP | ||||||||
| 3679 | // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx). | ||||||||
| 3680 | int IntSize = Inst->getOperand(0)->getType()->getScalarSizeInBits(); | ||||||||
| 3681 | if (Inst->getOpcode() == Instruction::SIToFP) | ||||||||
| 3682 | --IntSize; | ||||||||
| 3683 | |||||||||
| 3684 | // If the exponent of the largest finite FP value can hold the largest | ||||||||
| 3685 | // integer, the result of the cast must be finite. | ||||||||
| 3686 | Type *FPTy = Inst->getType()->getScalarType(); | ||||||||
| 3687 | return ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize; | ||||||||
| 3688 | } | ||||||||
| 3689 | default: | ||||||||
| 3690 | break; | ||||||||
| 3691 | } | ||||||||
| 3692 | } | ||||||||
| 3693 | |||||||||
| 3694 | // try to handle fixed width vector constants | ||||||||
| 3695 | auto *VFVTy = dyn_cast<FixedVectorType>(V->getType()); | ||||||||
| 3696 | if (VFVTy && isa<Constant>(V)) { | ||||||||
| 3697 | // For vectors, verify that each element is not infinity. | ||||||||
| 3698 | unsigned NumElts = VFVTy->getNumElements(); | ||||||||
| 3699 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||
| 3700 | Constant *Elt = cast<Constant>(V)->getAggregateElement(i); | ||||||||
| 3701 | if (!Elt) | ||||||||
| 3702 | return false; | ||||||||
| 3703 | if (isa<UndefValue>(Elt)) | ||||||||
| 3704 | continue; | ||||||||
| 3705 | auto *CElt = dyn_cast<ConstantFP>(Elt); | ||||||||
| 3706 | if (!CElt || CElt->isInfinity()) | ||||||||
| 3707 | return false; | ||||||||
| 3708 | } | ||||||||
| 3709 | // All elements were confirmed non-infinity or undefined. | ||||||||
| 3710 | return true; | ||||||||
| 3711 | } | ||||||||
| 3712 | |||||||||
| 3713 | // was not able to prove that V never contains infinity | ||||||||
| 3714 | return false; | ||||||||
| 3715 | } | ||||||||
| 3716 | |||||||||
| 3717 | bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, | ||||||||
| 3718 | unsigned Depth) { | ||||||||
| 3719 | assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type")((void)0); | ||||||||
| 3720 | |||||||||
| 3721 | // If we're told that NaNs won't happen, assume they won't. | ||||||||
| 3722 | if (auto *FPMathOp = dyn_cast<FPMathOperator>(V)) | ||||||||
| 3723 | if (FPMathOp->hasNoNaNs()) | ||||||||
| 3724 | return true; | ||||||||
| 3725 | |||||||||
| 3726 | // Handle scalar constants. | ||||||||
| 3727 | if (auto *CFP = dyn_cast<ConstantFP>(V)) | ||||||||
| 3728 | return !CFP->isNaN(); | ||||||||
| 3729 | |||||||||
| 3730 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 3731 | return false; | ||||||||
| 3732 | |||||||||
| 3733 | if (auto *Inst = dyn_cast<Instruction>(V)) { | ||||||||
| 3734 | switch (Inst->getOpcode()) { | ||||||||
| 3735 | case Instruction::FAdd: | ||||||||
| 3736 | case Instruction::FSub: | ||||||||
| 3737 | // Adding positive and negative infinity produces NaN. | ||||||||
| 3738 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) && | ||||||||
| 3739 | isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && | ||||||||
| 3740 | (isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) || | ||||||||
| 3741 | isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1)); | ||||||||
| 3742 | |||||||||
| 3743 | case Instruction::FMul: | ||||||||
| 3744 | // Zero multiplied with infinity produces NaN. | ||||||||
| 3745 | // FIXME: If neither side can be zero fmul never produces NaN. | ||||||||
| 3746 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) && | ||||||||
| 3747 | isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) && | ||||||||
| 3748 | isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && | ||||||||
| 3749 | isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1); | ||||||||
| 3750 | |||||||||
| 3751 | case Instruction::FDiv: | ||||||||
| 3752 | case Instruction::FRem: | ||||||||
| 3753 | // FIXME: Only 0/0, Inf/Inf, Inf REM x and x REM 0 produce NaN. | ||||||||
| 3754 | return false; | ||||||||
| 3755 | |||||||||
| 3756 | case Instruction::Select: { | ||||||||
| 3757 | return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && | ||||||||
| 3758 | isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1); | ||||||||
| 3759 | } | ||||||||
| 3760 | case Instruction::SIToFP: | ||||||||
| 3761 | case Instruction::UIToFP: | ||||||||
| 3762 | return true; | ||||||||
| 3763 | case Instruction::FPTrunc: | ||||||||
| 3764 | case Instruction::FPExt: | ||||||||
| 3765 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1); | ||||||||
| 3766 | default: | ||||||||
| 3767 | break; | ||||||||
| 3768 | } | ||||||||
| 3769 | } | ||||||||
| 3770 | |||||||||
| 3771 | if (const auto *II = dyn_cast<IntrinsicInst>(V)) { | ||||||||
| 3772 | switch (II->getIntrinsicID()) { | ||||||||
| 3773 | case Intrinsic::canonicalize: | ||||||||
| 3774 | case Intrinsic::fabs: | ||||||||
| 3775 | case Intrinsic::copysign: | ||||||||
| 3776 | case Intrinsic::exp: | ||||||||
| 3777 | case Intrinsic::exp2: | ||||||||
| 3778 | case Intrinsic::floor: | ||||||||
| 3779 | case Intrinsic::ceil: | ||||||||
| 3780 | case Intrinsic::trunc: | ||||||||
| 3781 | case Intrinsic::rint: | ||||||||
| 3782 | case Intrinsic::nearbyint: | ||||||||
| 3783 | case Intrinsic::round: | ||||||||
| 3784 | case Intrinsic::roundeven: | ||||||||
| 3785 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1); | ||||||||
| 3786 | case Intrinsic::sqrt: | ||||||||
| 3787 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) && | ||||||||
| 3788 | CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI); | ||||||||
| 3789 | case Intrinsic::minnum: | ||||||||
| 3790 | case Intrinsic::maxnum: | ||||||||
| 3791 | // If either operand is not NaN, the result is not NaN. | ||||||||
| 3792 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) || | ||||||||
| 3793 | isKnownNeverNaN(II->getArgOperand(1), TLI, Depth + 1); | ||||||||
| 3794 | default: | ||||||||
| 3795 | return false; | ||||||||
| 3796 | } | ||||||||
| 3797 | } | ||||||||
| 3798 | |||||||||
| 3799 | // Try to handle fixed width vector constants | ||||||||
| 3800 | auto *VFVTy = dyn_cast<FixedVectorType>(V->getType()); | ||||||||
| 3801 | if (VFVTy && isa<Constant>(V)) { | ||||||||
| 3802 | // For vectors, verify that each element is not NaN. | ||||||||
| 3803 | unsigned NumElts = VFVTy->getNumElements(); | ||||||||
| 3804 | for (unsigned i = 0; i != NumElts; ++i) { | ||||||||
| 3805 | Constant *Elt = cast<Constant>(V)->getAggregateElement(i); | ||||||||
| 3806 | if (!Elt) | ||||||||
| 3807 | return false; | ||||||||
| 3808 | if (isa<UndefValue>(Elt)) | ||||||||
| 3809 | continue; | ||||||||
| 3810 | auto *CElt = dyn_cast<ConstantFP>(Elt); | ||||||||
| 3811 | if (!CElt || CElt->isNaN()) | ||||||||
| 3812 | return false; | ||||||||
| 3813 | } | ||||||||
| 3814 | // All elements were confirmed not-NaN or undefined. | ||||||||
| 3815 | return true; | ||||||||
| 3816 | } | ||||||||
| 3817 | |||||||||
| 3818 | // Was not able to prove that V never contains NaN | ||||||||
| 3819 | return false; | ||||||||
| 3820 | } | ||||||||
| 3821 | |||||||||
| 3822 | Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { | ||||||||
| 3823 | |||||||||
| 3824 | // All byte-wide stores are splatable, even of arbitrary variables. | ||||||||
| 3825 | if (V->getType()->isIntegerTy(8)) | ||||||||
| 3826 | return V; | ||||||||
| 3827 | |||||||||
| 3828 | LLVMContext &Ctx = V->getContext(); | ||||||||
| 3829 | |||||||||
| 3830 | // Undef don't care. | ||||||||
| 3831 | auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); | ||||||||
| 3832 | if (isa<UndefValue>(V)) | ||||||||
| 3833 | return UndefInt8; | ||||||||
| 3834 | |||||||||
| 3835 | // Return Undef for zero-sized type. | ||||||||
| 3836 | if (!DL.getTypeStoreSize(V->getType()).isNonZero()) | ||||||||
| 3837 | return UndefInt8; | ||||||||
| 3838 | |||||||||
| 3839 | Constant *C = dyn_cast<Constant>(V); | ||||||||
| 3840 | if (!C) { | ||||||||
| 3841 | // Conceptually, we could handle things like: | ||||||||
| 3842 | // %a = zext i8 %X to i16 | ||||||||
| 3843 | // %b = shl i16 %a, 8 | ||||||||
| 3844 | // %c = or i16 %a, %b | ||||||||
| 3845 | // but until there is an example that actually needs this, it doesn't seem | ||||||||
| 3846 | // worth worrying about. | ||||||||
| 3847 | return nullptr; | ||||||||
| 3848 | } | ||||||||
| 3849 | |||||||||
| 3850 | // Handle 'null' ConstantArrayZero etc. | ||||||||
| 3851 | if (C->isNullValue()) | ||||||||
| 3852 | return Constant::getNullValue(Type::getInt8Ty(Ctx)); | ||||||||
| 3853 | |||||||||
| 3854 | // Constant floating-point values can be handled as integer values if the | ||||||||
| 3855 | // corresponding integer value is "byteable". An important case is 0.0. | ||||||||
| 3856 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { | ||||||||
| 3857 | Type *Ty = nullptr; | ||||||||
| 3858 | if (CFP->getType()->isHalfTy()) | ||||||||
| 3859 | Ty = Type::getInt16Ty(Ctx); | ||||||||
| 3860 | else if (CFP->getType()->isFloatTy()) | ||||||||
| 3861 | Ty = Type::getInt32Ty(Ctx); | ||||||||
| 3862 | else if (CFP->getType()->isDoubleTy()) | ||||||||
| 3863 | Ty = Type::getInt64Ty(Ctx); | ||||||||
| 3864 | // Don't handle long double formats, which have strange constraints. | ||||||||
| 3865 | return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL) | ||||||||
| 3866 | : nullptr; | ||||||||
| 3867 | } | ||||||||
| 3868 | |||||||||
| 3869 | // We can handle constant integers that are multiple of 8 bits. | ||||||||
| 3870 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { | ||||||||
| 3871 | if (CI->getBitWidth() % 8 == 0) { | ||||||||
| 3872 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!")((void)0); | ||||||||
| 3873 | if (!CI->getValue().isSplat(8)) | ||||||||
| 3874 | return nullptr; | ||||||||
| 3875 | return ConstantInt::get(Ctx, CI->getValue().trunc(8)); | ||||||||
| 3876 | } | ||||||||
| 3877 | } | ||||||||
| 3878 | |||||||||
| 3879 | if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||||||||
| 3880 | if (CE->getOpcode() == Instruction::IntToPtr) { | ||||||||
| 3881 | if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) { | ||||||||
| 3882 | unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace()); | ||||||||
| 3883 | return isBytewiseValue( | ||||||||
| 3884 | ConstantExpr::getIntegerCast(CE->getOperand(0), | ||||||||
| 3885 | Type::getIntNTy(Ctx, BitWidth), false), | ||||||||
| 3886 | DL); | ||||||||
| 3887 | } | ||||||||
| 3888 | } | ||||||||
| 3889 | } | ||||||||
| 3890 | |||||||||
| 3891 | auto Merge = [&](Value *LHS, Value *RHS) -> Value * { | ||||||||
| 3892 | if (LHS == RHS) | ||||||||
| 3893 | return LHS; | ||||||||
| 3894 | if (!LHS || !RHS) | ||||||||
| 3895 | return nullptr; | ||||||||
| 3896 | if (LHS == UndefInt8) | ||||||||
| 3897 | return RHS; | ||||||||
| 3898 | if (RHS == UndefInt8) | ||||||||
| 3899 | return LHS; | ||||||||
| 3900 | return nullptr; | ||||||||
| 3901 | }; | ||||||||
| 3902 | |||||||||
| 3903 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) { | ||||||||
| 3904 | Value *Val = UndefInt8; | ||||||||
| 3905 | for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) | ||||||||
| 3906 | if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL)))) | ||||||||
| 3907 | return nullptr; | ||||||||
| 3908 | return Val; | ||||||||
| 3909 | } | ||||||||
| 3910 | |||||||||
| 3911 | if (isa<ConstantAggregate>(C)) { | ||||||||
| 3912 | Value *Val = UndefInt8; | ||||||||
| 3913 | for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) | ||||||||
| 3914 | if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL)))) | ||||||||
| 3915 | return nullptr; | ||||||||
| 3916 | return Val; | ||||||||
| 3917 | } | ||||||||
| 3918 | |||||||||
| 3919 | // Don't try to handle the handful of other constants. | ||||||||
| 3920 | return nullptr; | ||||||||
| 3921 | } | ||||||||
| 3922 | |||||||||
| 3923 | // This is the recursive version of BuildSubAggregate. It takes a few different | ||||||||
| 3924 | // arguments. Idxs is the index within the nested struct From that we are | ||||||||
| 3925 | // looking at now (which is of type IndexedType). IdxSkip is the number of | ||||||||
| 3926 | // indices from Idxs that should be left out when inserting into the resulting | ||||||||
| 3927 | // struct. To is the result struct built so far, new insertvalue instructions | ||||||||
| 3928 | // build on that. | ||||||||
| 3929 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, | ||||||||
| 3930 | SmallVectorImpl<unsigned> &Idxs, | ||||||||
| 3931 | unsigned IdxSkip, | ||||||||
| 3932 | Instruction *InsertBefore) { | ||||||||
| 3933 | StructType *STy = dyn_cast<StructType>(IndexedType); | ||||||||
| 3934 | if (STy) { | ||||||||
| 3935 | // Save the original To argument so we can modify it | ||||||||
| 3936 | Value *OrigTo = To; | ||||||||
| 3937 | // General case, the type indexed by Idxs is a struct | ||||||||
| 3938 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { | ||||||||
| 3939 | // Process each struct element recursively | ||||||||
| 3940 | Idxs.push_back(i); | ||||||||
| 3941 | Value *PrevTo = To; | ||||||||
| 3942 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, | ||||||||
| 3943 | InsertBefore); | ||||||||
| 3944 | Idxs.pop_back(); | ||||||||
| 3945 | if (!To) { | ||||||||
| 3946 | // Couldn't find any inserted value for this index? Cleanup | ||||||||
| 3947 | while (PrevTo != OrigTo) { | ||||||||
| 3948 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); | ||||||||
| 3949 | PrevTo = Del->getAggregateOperand(); | ||||||||
| 3950 | Del->eraseFromParent(); | ||||||||
| 3951 | } | ||||||||
| 3952 | // Stop processing elements | ||||||||
| 3953 | break; | ||||||||
| 3954 | } | ||||||||
| 3955 | } | ||||||||
| 3956 | // If we successfully found a value for each of our subaggregates | ||||||||
| 3957 | if (To) | ||||||||
| 3958 | return To; | ||||||||
| 3959 | } | ||||||||
| 3960 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of | ||||||||
| 3961 | // the struct's elements had a value that was inserted directly. In the latter | ||||||||
| 3962 | // case, perhaps we can't determine each of the subelements individually, but | ||||||||
| 3963 | // we might be able to find the complete struct somewhere. | ||||||||
| 3964 | |||||||||
| 3965 | // Find the value that is at that particular spot | ||||||||
| 3966 | Value *V = FindInsertedValue(From, Idxs); | ||||||||
| 3967 | |||||||||
| 3968 | if (!V) | ||||||||
| 3969 | return nullptr; | ||||||||
| 3970 | |||||||||
| 3971 | // Insert the value in the new (sub) aggregate | ||||||||
| 3972 | return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), | ||||||||
| 3973 | "tmp", InsertBefore); | ||||||||
| 3974 | } | ||||||||
| 3975 | |||||||||
| 3976 | // This helper takes a nested struct and extracts a part of it (which is again a | ||||||||
| 3977 | // struct) into a new value. For example, given the struct: | ||||||||
| 3978 | // { a, { b, { c, d }, e } } | ||||||||
| 3979 | // and the indices "1, 1" this returns | ||||||||
| 3980 | // { c, d }. | ||||||||
| 3981 | // | ||||||||
| 3982 | // It does this by inserting an insertvalue for each element in the resulting | ||||||||
| 3983 | // struct, as opposed to just inserting a single struct. This will only work if | ||||||||
| 3984 | // each of the elements of the substruct are known (ie, inserted into From by an | ||||||||
| 3985 | // insertvalue instruction somewhere). | ||||||||
| 3986 | // | ||||||||
| 3987 | // All inserted insertvalue instructions are inserted before InsertBefore | ||||||||
| 3988 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, | ||||||||
| 3989 | Instruction *InsertBefore) { | ||||||||
| 3990 | assert(InsertBefore && "Must have someplace to insert!")((void)0); | ||||||||
| 3991 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), | ||||||||
| 3992 | idx_range); | ||||||||
| 3993 | Value *To = UndefValue::get(IndexedType); | ||||||||
| 3994 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); | ||||||||
| 3995 | unsigned IdxSkip = Idxs.size(); | ||||||||
| 3996 | |||||||||
| 3997 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); | ||||||||
| 3998 | } | ||||||||
| 3999 | |||||||||
| 4000 | /// Given an aggregate and a sequence of indices, see if the scalar value | ||||||||
| 4001 | /// indexed is already around as a register, for example if it was inserted | ||||||||
| 4002 | /// directly into the aggregate. | ||||||||
| 4003 | /// | ||||||||
| 4004 | /// If InsertBefore is not null, this function will duplicate (modified) | ||||||||
| 4005 | /// insertvalues when a part of a nested struct is extracted. | ||||||||
| 4006 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, | ||||||||
| 4007 | Instruction *InsertBefore) { | ||||||||
| 4008 | // Nothing to index? Just return V then (this is useful at the end of our | ||||||||
| 4009 | // recursion). | ||||||||
| 4010 | if (idx_range.empty()) | ||||||||
| 4011 | return V; | ||||||||
| 4012 | // We have indices, so V should have an indexable type. | ||||||||
| 4013 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&((void)0) | ||||||||
| 4014 | "Not looking at a struct or array?")((void)0); | ||||||||
| 4015 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&((void)0) | ||||||||
| 4016 | "Invalid indices for type?")((void)0); | ||||||||
| 4017 | |||||||||
| 4018 | if (Constant *C = dyn_cast<Constant>(V)) { | ||||||||
| 4019 | C = C->getAggregateElement(idx_range[0]); | ||||||||
| 4020 | if (!C) return nullptr; | ||||||||
| 4021 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); | ||||||||
| 4022 | } | ||||||||
| 4023 | |||||||||
| 4024 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { | ||||||||
| 4025 | // Loop the indices for the insertvalue instruction in parallel with the | ||||||||
| 4026 | // requested indices | ||||||||
| 4027 | const unsigned *req_idx = idx_range.begin(); | ||||||||
| 4028 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); | ||||||||
| 4029 | i != e; ++i, ++req_idx) { | ||||||||
| 4030 | if (req_idx == idx_range.end()) { | ||||||||
| 4031 | // We can't handle this without inserting insertvalues | ||||||||
| 4032 | if (!InsertBefore) | ||||||||
| 4033 | return nullptr; | ||||||||
| 4034 | |||||||||
| 4035 | // The requested index identifies a part of a nested aggregate. Handle | ||||||||
| 4036 | // this specially. For example, | ||||||||
| 4037 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 | ||||||||
| 4038 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 | ||||||||
| 4039 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 | ||||||||
| 4040 | // This can be changed into | ||||||||
| 4041 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 | ||||||||
| 4042 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 | ||||||||
| 4043 | // which allows the unused 0,0 element from the nested struct to be | ||||||||
| 4044 | // removed. | ||||||||
| 4045 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), | ||||||||
| 4046 | InsertBefore); | ||||||||
| 4047 | } | ||||||||
| 4048 | |||||||||
| 4049 | // This insert value inserts something else than what we are looking for. | ||||||||
| 4050 | // See if the (aggregate) value inserted into has the value we are | ||||||||
| 4051 | // looking for, then. | ||||||||
| 4052 | if (*req_idx != *i) | ||||||||
| 4053 | return FindInsertedValue(I->getAggregateOperand(), idx_range, | ||||||||
| 4054 | InsertBefore); | ||||||||
| 4055 | } | ||||||||
| 4056 | // If we end up here, the indices of the insertvalue match with those | ||||||||
| 4057 | // requested (though possibly only partially). Now we recursively look at | ||||||||
| 4058 | // the inserted value, passing any remaining indices. | ||||||||
| 4059 | return FindInsertedValue(I->getInsertedValueOperand(), | ||||||||
| 4060 | makeArrayRef(req_idx, idx_range.end()), | ||||||||
| 4061 | InsertBefore); | ||||||||
| 4062 | } | ||||||||
| 4063 | |||||||||
| 4064 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { | ||||||||
| 4065 | // If we're extracting a value from an aggregate that was extracted from | ||||||||
| 4066 | // something else, we can extract from that something else directly instead. | ||||||||
| 4067 | // However, we will need to chain I's indices with the requested indices. | ||||||||
| 4068 | |||||||||
| 4069 | // Calculate the number of indices required | ||||||||
| 4070 | unsigned size = I->getNumIndices() + idx_range.size(); | ||||||||
| 4071 | // Allocate some space to put the new indices in | ||||||||
| 4072 | SmallVector<unsigned, 5> Idxs; | ||||||||
| 4073 | Idxs.reserve(size); | ||||||||
| 4074 | // Add indices from the extract value instruction | ||||||||
| 4075 | Idxs.append(I->idx_begin(), I->idx_end()); | ||||||||
| 4076 | |||||||||
| 4077 | // Add requested indices | ||||||||
| 4078 | Idxs.append(idx_range.begin(), idx_range.end()); | ||||||||
| 4079 | |||||||||
| 4080 | assert(Idxs.size() == size((void)0) | ||||||||
| 4081 | && "Number of indices added not correct?")((void)0); | ||||||||
| 4082 | |||||||||
| 4083 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); | ||||||||
| 4084 | } | ||||||||
| 4085 | // Otherwise, we don't know (such as, extracting from a function return value | ||||||||
| 4086 | // or load instruction) | ||||||||
| 4087 | return nullptr; | ||||||||
| 4088 | } | ||||||||
| 4089 | |||||||||
| 4090 | bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, | ||||||||
| 4091 | unsigned CharSize) { | ||||||||
| 4092 | // Make sure the GEP has exactly three arguments. | ||||||||
| 4093 | if (GEP->getNumOperands() != 3) | ||||||||
| 4094 | return false; | ||||||||
| 4095 | |||||||||
| 4096 | // Make sure the index-ee is a pointer to array of \p CharSize integers. | ||||||||
| 4097 | // CharSize. | ||||||||
| 4098 | ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType()); | ||||||||
| 4099 | if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) | ||||||||
| 4100 | return false; | ||||||||
| 4101 | |||||||||
| 4102 | // Check to make sure that the first operand of the GEP is an integer and | ||||||||
| 4103 | // has value 0 so that we are sure we're indexing into the initializer. | ||||||||
| 4104 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); | ||||||||
| 4105 | if (!FirstIdx || !FirstIdx->isZero()) | ||||||||
| 4106 | return false; | ||||||||
| 4107 | |||||||||
| 4108 | return true; | ||||||||
| 4109 | } | ||||||||
| 4110 | |||||||||
| 4111 | bool llvm::getConstantDataArrayInfo(const Value *V, | ||||||||
| 4112 | ConstantDataArraySlice &Slice, | ||||||||
| 4113 | unsigned ElementSize, uint64_t Offset) { | ||||||||
| 4114 | assert(V)((void)0); | ||||||||
| 4115 | |||||||||
| 4116 | // Look through bitcast instructions and geps. | ||||||||
| 4117 | V = V->stripPointerCasts(); | ||||||||
| 4118 | |||||||||
| 4119 | // If the value is a GEP instruction or constant expression, treat it as an | ||||||||
| 4120 | // offset. | ||||||||
| 4121 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { | ||||||||
| 4122 | // The GEP operator should be based on a pointer to string constant, and is | ||||||||
| 4123 | // indexing into the string constant. | ||||||||
| 4124 | if (!isGEPBasedOnPointerToString(GEP, ElementSize)) | ||||||||
| 4125 | return false; | ||||||||
| 4126 | |||||||||
| 4127 | // If the second index isn't a ConstantInt, then this is a variable index | ||||||||
| 4128 | // into the array. If this occurs, we can't say anything meaningful about | ||||||||
| 4129 | // the string. | ||||||||
| 4130 | uint64_t StartIdx = 0; | ||||||||
| 4131 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) | ||||||||
| 4132 | StartIdx = CI->getZExtValue(); | ||||||||
| 4133 | else | ||||||||
| 4134 | return false; | ||||||||
| 4135 | return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize, | ||||||||
| 4136 | StartIdx + Offset); | ||||||||
| 4137 | } | ||||||||
| 4138 | |||||||||
| 4139 | // The GEP instruction, constant or instruction, must reference a global | ||||||||
| 4140 | // variable that is a constant and is initialized. The referenced constant | ||||||||
| 4141 | // initializer is the array that we'll use for optimization. | ||||||||
| 4142 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); | ||||||||
| 4143 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) | ||||||||
| 4144 | return false; | ||||||||
| 4145 | |||||||||
| 4146 | const ConstantDataArray *Array; | ||||||||
| 4147 | ArrayType *ArrayTy; | ||||||||
| 4148 | if (GV->getInitializer()->isNullValue()) { | ||||||||
| 4149 | Type *GVTy = GV->getValueType(); | ||||||||
| 4150 | if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) { | ||||||||
| 4151 | // A zeroinitializer for the array; there is no ConstantDataArray. | ||||||||
| 4152 | Array = nullptr; | ||||||||
| 4153 | } else { | ||||||||
| 4154 | const DataLayout &DL = GV->getParent()->getDataLayout(); | ||||||||
| 4155 | uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedSize(); | ||||||||
| 4156 | uint64_t Length = SizeInBytes / (ElementSize / 8); | ||||||||
| 4157 | if (Length <= Offset) | ||||||||
| 4158 | return false; | ||||||||
| 4159 | |||||||||
| 4160 | Slice.Array = nullptr; | ||||||||
| 4161 | Slice.Offset = 0; | ||||||||
| 4162 | Slice.Length = Length - Offset; | ||||||||
| 4163 | return true; | ||||||||
| 4164 | } | ||||||||
| 4165 | } else { | ||||||||
| 4166 | // This must be a ConstantDataArray. | ||||||||
| 4167 | Array = dyn_cast<ConstantDataArray>(GV->getInitializer()); | ||||||||
| 4168 | if (!Array) | ||||||||
| 4169 | return false; | ||||||||
| 4170 | ArrayTy = Array->getType(); | ||||||||
| 4171 | } | ||||||||
| 4172 | if (!ArrayTy->getElementType()->isIntegerTy(ElementSize)) | ||||||||
| 4173 | return false; | ||||||||
| 4174 | |||||||||
| 4175 | uint64_t NumElts = ArrayTy->getArrayNumElements(); | ||||||||
| 4176 | if (Offset > NumElts) | ||||||||
| 4177 | return false; | ||||||||
| 4178 | |||||||||
| 4179 | Slice.Array = Array; | ||||||||
| 4180 | Slice.Offset = Offset; | ||||||||
| 4181 | Slice.Length = NumElts - Offset; | ||||||||
| 4182 | return true; | ||||||||
| 4183 | } | ||||||||
| 4184 | |||||||||
| 4185 | /// This function computes the length of a null-terminated C string pointed to | ||||||||
| 4186 | /// by V. If successful, it returns true and returns the string in Str. | ||||||||
| 4187 | /// If unsuccessful, it returns false. | ||||||||
| 4188 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, | ||||||||
| 4189 | uint64_t Offset, bool TrimAtNul) { | ||||||||
| 4190 | ConstantDataArraySlice Slice; | ||||||||
| 4191 | if (!getConstantDataArrayInfo(V, Slice, 8, Offset)) | ||||||||
| 4192 | return false; | ||||||||
| 4193 | |||||||||
| 4194 | if (Slice.Array == nullptr) { | ||||||||
| 4195 | if (TrimAtNul) { | ||||||||
| 4196 | Str = StringRef(); | ||||||||
| 4197 | return true; | ||||||||
| 4198 | } | ||||||||
| 4199 | if (Slice.Length == 1) { | ||||||||
| 4200 | Str = StringRef("", 1); | ||||||||
| 4201 | return true; | ||||||||
| 4202 | } | ||||||||
| 4203 | // We cannot instantiate a StringRef as we do not have an appropriate string | ||||||||
| 4204 | // of 0s at hand. | ||||||||
| 4205 | return false; | ||||||||
| 4206 | } | ||||||||
| 4207 | |||||||||
| 4208 | // Start out with the entire array in the StringRef. | ||||||||
| 4209 | Str = Slice.Array->getAsString(); | ||||||||
| 4210 | // Skip over 'offset' bytes. | ||||||||
| 4211 | Str = Str.substr(Slice.Offset); | ||||||||
| 4212 | |||||||||
| 4213 | if (TrimAtNul) { | ||||||||
| 4214 | // Trim off the \0 and anything after it. If the array is not nul | ||||||||
| 4215 | // terminated, we just return the whole end of string. The client may know | ||||||||
| 4216 | // some other way that the string is length-bound. | ||||||||
| 4217 | Str = Str.substr(0, Str.find('\0')); | ||||||||
| 4218 | } | ||||||||
| 4219 | return true; | ||||||||
| 4220 | } | ||||||||
| 4221 | |||||||||
| 4222 | // These next two are very similar to the above, but also look through PHI | ||||||||
| 4223 | // nodes. | ||||||||
| 4224 | // TODO: See if we can integrate these two together. | ||||||||
| 4225 | |||||||||
| 4226 | /// If we can compute the length of the string pointed to by | ||||||||
| 4227 | /// the specified pointer, return 'len+1'. If we can't, return 0. | ||||||||
| 4228 | static uint64_t GetStringLengthH(const Value *V, | ||||||||
| 4229 | SmallPtrSetImpl<const PHINode*> &PHIs, | ||||||||
| 4230 | unsigned CharSize) { | ||||||||
| 4231 | // Look through noop bitcast instructions. | ||||||||
| 4232 | V = V->stripPointerCasts(); | ||||||||
| 4233 | |||||||||
| 4234 | // If this is a PHI node, there are two cases: either we have already seen it | ||||||||
| 4235 | // or we haven't. | ||||||||
| 4236 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { | ||||||||
| 4237 | if (!PHIs.insert(PN).second) | ||||||||
| 4238 | return ~0ULL; // already in the set. | ||||||||
| 4239 | |||||||||
| 4240 | // If it was new, see if all the input strings are the same length. | ||||||||
| 4241 | uint64_t LenSoFar = ~0ULL; | ||||||||
| 4242 | for (Value *IncValue : PN->incoming_values()) { | ||||||||
| 4243 | uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); | ||||||||
| 4244 | if (Len == 0) return 0; // Unknown length -> unknown. | ||||||||
| 4245 | |||||||||
| 4246 | if (Len == ~0ULL) continue; | ||||||||
| 4247 | |||||||||
| 4248 | if (Len != LenSoFar && LenSoFar != ~0ULL) | ||||||||
| 4249 | return 0; // Disagree -> unknown. | ||||||||
| 4250 | LenSoFar = Len; | ||||||||
| 4251 | } | ||||||||
| 4252 | |||||||||
| 4253 | // Success, all agree. | ||||||||
| 4254 | return LenSoFar; | ||||||||
| 4255 | } | ||||||||
| 4256 | |||||||||
| 4257 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) | ||||||||
| 4258 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { | ||||||||
| 4259 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); | ||||||||
| 4260 | if (Len1 == 0) return 0; | ||||||||
| 4261 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); | ||||||||
| 4262 | if (Len2 == 0) return 0; | ||||||||
| 4263 | if (Len1 == ~0ULL) return Len2; | ||||||||
| 4264 | if (Len2 == ~0ULL) return Len1; | ||||||||
| 4265 | if (Len1 != Len2) return 0; | ||||||||
| 4266 | return Len1; | ||||||||
| 4267 | } | ||||||||
| 4268 | |||||||||
| 4269 | // Otherwise, see if we can read the string. | ||||||||
| 4270 | ConstantDataArraySlice Slice; | ||||||||
| 4271 | if (!getConstantDataArrayInfo(V, Slice, CharSize)) | ||||||||
| 4272 | return 0; | ||||||||
| 4273 | |||||||||
| 4274 | if (Slice.Array == nullptr) | ||||||||
| 4275 | return 1; | ||||||||
| 4276 | |||||||||
| 4277 | // Search for nul characters | ||||||||
| 4278 | unsigned NullIndex = 0; | ||||||||
| 4279 | for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { | ||||||||
| 4280 | if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) | ||||||||
| 4281 | break; | ||||||||
| 4282 | } | ||||||||
| 4283 | |||||||||
| 4284 | return NullIndex + 1; | ||||||||
| 4285 | } | ||||||||
| 4286 | |||||||||
| 4287 | /// If we can compute the length of the string pointed to by | ||||||||
| 4288 | /// the specified pointer, return 'len+1'. If we can't, return 0. | ||||||||
| 4289 | uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { | ||||||||
| 4290 | if (!V->getType()->isPointerTy()) | ||||||||
| 4291 | return 0; | ||||||||
| 4292 | |||||||||
| 4293 | SmallPtrSet<const PHINode*, 32> PHIs; | ||||||||
| 4294 | uint64_t Len = GetStringLengthH(V, PHIs, CharSize); | ||||||||
| 4295 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return | ||||||||
| 4296 | // an empty string as a length. | ||||||||
| 4297 | return Len == ~0ULL ? 1 : Len; | ||||||||
| 4298 | } | ||||||||
| 4299 | |||||||||
| 4300 | const Value * | ||||||||
| 4301 | llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call, | ||||||||
| 4302 | bool MustPreserveNullness) { | ||||||||
| 4303 | assert(Call &&((void)0) | ||||||||
| 4304 | "getArgumentAliasingToReturnedPointer only works on nonnull calls")((void)0); | ||||||||
| 4305 | if (const Value *RV = Call->getReturnedArgOperand()) | ||||||||
| 4306 | return RV; | ||||||||
| 4307 | // This can be used only as a aliasing property. | ||||||||
| 4308 | if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( | ||||||||
| 4309 | Call, MustPreserveNullness)) | ||||||||
| 4310 | return Call->getArgOperand(0); | ||||||||
| 4311 | return nullptr; | ||||||||
| 4312 | } | ||||||||
| 4313 | |||||||||
| 4314 | bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( | ||||||||
| 4315 | const CallBase *Call, bool MustPreserveNullness) { | ||||||||
| 4316 | switch (Call->getIntrinsicID()) { | ||||||||
| 4317 | case Intrinsic::launder_invariant_group: | ||||||||
| 4318 | case Intrinsic::strip_invariant_group: | ||||||||
| 4319 | case Intrinsic::aarch64_irg: | ||||||||
| 4320 | case Intrinsic::aarch64_tagp: | ||||||||
| 4321 | return true; | ||||||||
| 4322 | case Intrinsic::ptrmask: | ||||||||
| 4323 | return !MustPreserveNullness; | ||||||||
| 4324 | default: | ||||||||
| 4325 | return false; | ||||||||
| 4326 | } | ||||||||
| 4327 | } | ||||||||
| 4328 | |||||||||
| 4329 | /// \p PN defines a loop-variant pointer to an object. Check if the | ||||||||
| 4330 | /// previous iteration of the loop was referring to the same object as \p PN. | ||||||||
| 4331 | static bool isSameUnderlyingObjectInLoop(const PHINode *PN, | ||||||||
| 4332 | const LoopInfo *LI) { | ||||||||
| 4333 | // Find the loop-defined value. | ||||||||
| 4334 | Loop *L = LI->getLoopFor(PN->getParent()); | ||||||||
| 4335 | if (PN->getNumIncomingValues() != 2) | ||||||||
| 4336 | return true; | ||||||||
| 4337 | |||||||||
| 4338 | // Find the value from previous iteration. | ||||||||
| 4339 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); | ||||||||
| 4340 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) | ||||||||
| 4341 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); | ||||||||
| 4342 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) | ||||||||
| 4343 | return true; | ||||||||
| 4344 | |||||||||
| 4345 | // If a new pointer is loaded in the loop, the pointer references a different | ||||||||
| 4346 | // object in every iteration. E.g.: | ||||||||
| 4347 | // for (i) | ||||||||
| 4348 | // int *p = a[i]; | ||||||||
| 4349 | // ... | ||||||||
| 4350 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) | ||||||||
| 4351 | if (!L->isLoopInvariant(Load->getPointerOperand())) | ||||||||
| 4352 | return false; | ||||||||
| 4353 | return true; | ||||||||
| 4354 | } | ||||||||
| 4355 | |||||||||
| 4356 | const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) { | ||||||||
| 4357 | if (!V->getType()->isPointerTy()) | ||||||||
| 4358 | return V; | ||||||||
| 4359 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { | ||||||||
| 4360 | if (auto *GEP = dyn_cast<GEPOperator>(V)) { | ||||||||
| 4361 | V = GEP->getPointerOperand(); | ||||||||
| 4362 | } else if (Operator::getOpcode(V) == Instruction::BitCast || | ||||||||
| 4363 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { | ||||||||
| 4364 | V = cast<Operator>(V)->getOperand(0); | ||||||||
| 4365 | if (!V->getType()->isPointerTy()) | ||||||||
| 4366 | return V; | ||||||||
| 4367 | } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { | ||||||||
| 4368 | if (GA->isInterposable()) | ||||||||
| 4369 | return V; | ||||||||
| 4370 | V = GA->getAliasee(); | ||||||||
| 4371 | } else { | ||||||||
| 4372 | if (auto *PHI = dyn_cast<PHINode>(V)) { | ||||||||
| 4373 | // Look through single-arg phi nodes created by LCSSA. | ||||||||
| 4374 | if (PHI->getNumIncomingValues() == 1) { | ||||||||
| 4375 | V = PHI->getIncomingValue(0); | ||||||||
| 4376 | continue; | ||||||||
| 4377 | } | ||||||||
| 4378 | } else if (auto *Call = dyn_cast<CallBase>(V)) { | ||||||||
| 4379 | // CaptureTracking can know about special capturing properties of some | ||||||||
| 4380 | // intrinsics like launder.invariant.group, that can't be expressed with | ||||||||
| 4381 | // the attributes, but have properties like returning aliasing pointer. | ||||||||
| 4382 | // Because some analysis may assume that nocaptured pointer is not | ||||||||
| 4383 | // returned from some special intrinsic (because function would have to | ||||||||
| 4384 | // be marked with returns attribute), it is crucial to use this function | ||||||||
| 4385 | // because it should be in sync with CaptureTracking. Not using it may | ||||||||
| 4386 | // cause weird miscompilations where 2 aliasing pointers are assumed to | ||||||||
| 4387 | // noalias. | ||||||||
| 4388 | if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) { | ||||||||
| 4389 | V = RP; | ||||||||
| 4390 | continue; | ||||||||
| 4391 | } | ||||||||
| 4392 | } | ||||||||
| 4393 | |||||||||
| 4394 | return V; | ||||||||
| 4395 | } | ||||||||
| 4396 | assert(V->getType()->isPointerTy() && "Unexpected operand type!")((void)0); | ||||||||
| 4397 | } | ||||||||
| 4398 | return V; | ||||||||
| 4399 | } | ||||||||
| 4400 | |||||||||
| 4401 | void llvm::getUnderlyingObjects(const Value *V, | ||||||||
| 4402 | SmallVectorImpl<const Value *> &Objects, | ||||||||
| 4403 | LoopInfo *LI, unsigned MaxLookup) { | ||||||||
| 4404 | SmallPtrSet<const Value *, 4> Visited; | ||||||||
| 4405 | SmallVector<const Value *, 4> Worklist; | ||||||||
| 4406 | Worklist.push_back(V); | ||||||||
| 4407 | do { | ||||||||
| 4408 | const Value *P = Worklist.pop_back_val(); | ||||||||
| 4409 | P = getUnderlyingObject(P, MaxLookup); | ||||||||
| 4410 | |||||||||
| 4411 | if (!Visited.insert(P).second) | ||||||||
| 4412 | continue; | ||||||||
| 4413 | |||||||||
| 4414 | if (auto *SI = dyn_cast<SelectInst>(P)) { | ||||||||
| 4415 | Worklist.push_back(SI->getTrueValue()); | ||||||||
| 4416 | Worklist.push_back(SI->getFalseValue()); | ||||||||
| 4417 | continue; | ||||||||
| 4418 | } | ||||||||
| 4419 | |||||||||
| 4420 | if (auto *PN = dyn_cast<PHINode>(P)) { | ||||||||
| 4421 | // If this PHI changes the underlying object in every iteration of the | ||||||||
| 4422 | // loop, don't look through it. Consider: | ||||||||
| 4423 | // int **A; | ||||||||
| 4424 | // for (i) { | ||||||||
| 4425 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) | ||||||||
| 4426 | // Curr = A[i]; | ||||||||
| 4427 | // *Prev, *Curr; | ||||||||
| 4428 | // | ||||||||
| 4429 | // Prev is tracking Curr one iteration behind so they refer to different | ||||||||
| 4430 | // underlying objects. | ||||||||
| 4431 | if (!LI || !LI->isLoopHeader(PN->getParent()) || | ||||||||
| 4432 | isSameUnderlyingObjectInLoop(PN, LI)) | ||||||||
| 4433 | append_range(Worklist, PN->incoming_values()); | ||||||||
| 4434 | continue; | ||||||||
| 4435 | } | ||||||||
| 4436 | |||||||||
| 4437 | Objects.push_back(P); | ||||||||
| 4438 | } while (!Worklist.empty()); | ||||||||
| 4439 | } | ||||||||
| 4440 | |||||||||
| 4441 | /// This is the function that does the work of looking through basic | ||||||||
| 4442 | /// ptrtoint+arithmetic+inttoptr sequences. | ||||||||
| 4443 | static const Value *getUnderlyingObjectFromInt(const Value *V) { | ||||||||
| 4444 | do { | ||||||||
| 4445 | if (const Operator *U = dyn_cast<Operator>(V)) { | ||||||||
| 4446 | // If we find a ptrtoint, we can transfer control back to the | ||||||||
| 4447 | // regular getUnderlyingObjectFromInt. | ||||||||
| 4448 | if (U->getOpcode() == Instruction::PtrToInt) | ||||||||
| 4449 | return U->getOperand(0); | ||||||||
| 4450 | // If we find an add of a constant, a multiplied value, or a phi, it's | ||||||||
| 4451 | // likely that the other operand will lead us to the base | ||||||||
| 4452 | // object. We don't have to worry about the case where the | ||||||||
| 4453 | // object address is somehow being computed by the multiply, | ||||||||
| 4454 | // because our callers only care when the result is an | ||||||||
| 4455 | // identifiable object. | ||||||||
| 4456 | if (U->getOpcode() != Instruction::Add || | ||||||||
| 4457 | (!isa<ConstantInt>(U->getOperand(1)) && | ||||||||
| 4458 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && | ||||||||
| 4459 | !isa<PHINode>(U->getOperand(1)))) | ||||||||
| 4460 | return V; | ||||||||
| 4461 | V = U->getOperand(0); | ||||||||
| 4462 | } else { | ||||||||
| 4463 | return V; | ||||||||
| 4464 | } | ||||||||
| 4465 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!")((void)0); | ||||||||
| 4466 | } while (true); | ||||||||
| 4467 | } | ||||||||
| 4468 | |||||||||
| 4469 | /// This is a wrapper around getUnderlyingObjects and adds support for basic | ||||||||
| 4470 | /// ptrtoint+arithmetic+inttoptr sequences. | ||||||||
| 4471 | /// It returns false if unidentified object is found in getUnderlyingObjects. | ||||||||
| 4472 | bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, | ||||||||
| 4473 | SmallVectorImpl<Value *> &Objects) { | ||||||||
| 4474 | SmallPtrSet<const Value *, 16> Visited; | ||||||||
| 4475 | SmallVector<const Value *, 4> Working(1, V); | ||||||||
| 4476 | do { | ||||||||
| 4477 | V = Working.pop_back_val(); | ||||||||
| 4478 | |||||||||
| 4479 | SmallVector<const Value *, 4> Objs; | ||||||||
| 4480 | getUnderlyingObjects(V, Objs); | ||||||||
| 4481 | |||||||||
| 4482 | for (const Value *V : Objs) { | ||||||||
| 4483 | if (!Visited.insert(V).second) | ||||||||
| 4484 | continue; | ||||||||
| 4485 | if (Operator::getOpcode(V) == Instruction::IntToPtr) { | ||||||||
| 4486 | const Value *O = | ||||||||
| 4487 | getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); | ||||||||
| 4488 | if (O->getType()->isPointerTy()) { | ||||||||
| 4489 | Working.push_back(O); | ||||||||
| 4490 | continue; | ||||||||
| 4491 | } | ||||||||
| 4492 | } | ||||||||
| 4493 | // If getUnderlyingObjects fails to find an identifiable object, | ||||||||
| 4494 | // getUnderlyingObjectsForCodeGen also fails for safety. | ||||||||
| 4495 | if (!isIdentifiedObject(V)) { | ||||||||
| 4496 | Objects.clear(); | ||||||||
| 4497 | return false; | ||||||||
| 4498 | } | ||||||||
| 4499 | Objects.push_back(const_cast<Value *>(V)); | ||||||||
| 4500 | } | ||||||||
| 4501 | } while (!Working.empty()); | ||||||||
| 4502 | return true; | ||||||||
| 4503 | } | ||||||||
| 4504 | |||||||||
| 4505 | AllocaInst *llvm::findAllocaForValue(Value *V, bool OffsetZero) { | ||||||||
| 4506 | AllocaInst *Result = nullptr; | ||||||||
| 4507 | SmallPtrSet<Value *, 4> Visited; | ||||||||
| 4508 | SmallVector<Value *, 4> Worklist; | ||||||||
| 4509 | |||||||||
| 4510 | auto AddWork = [&](Value *V) { | ||||||||
| 4511 | if (Visited.insert(V).second) | ||||||||
| 4512 | Worklist.push_back(V); | ||||||||
| 4513 | }; | ||||||||
| 4514 | |||||||||
| 4515 | AddWork(V); | ||||||||
| 4516 | do { | ||||||||
| 4517 | V = Worklist.pop_back_val(); | ||||||||
| 4518 | assert(Visited.count(V))((void)0); | ||||||||
| 4519 | |||||||||
| 4520 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { | ||||||||
| 4521 | if (Result && Result != AI) | ||||||||
| 4522 | return nullptr; | ||||||||
| 4523 | Result = AI; | ||||||||
| 4524 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { | ||||||||
| 4525 | AddWork(CI->getOperand(0)); | ||||||||
| 4526 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { | ||||||||
| 4527 | for (Value *IncValue : PN->incoming_values()) | ||||||||
| 4528 | AddWork(IncValue); | ||||||||
| 4529 | } else if (auto *SI = dyn_cast<SelectInst>(V)) { | ||||||||
| 4530 | AddWork(SI->getTrueValue()); | ||||||||
| 4531 | AddWork(SI->getFalseValue()); | ||||||||
| 4532 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) { | ||||||||
| 4533 | if (OffsetZero && !GEP->hasAllZeroIndices()) | ||||||||
| 4534 | return nullptr; | ||||||||
| 4535 | AddWork(GEP->getPointerOperand()); | ||||||||
| 4536 | } else { | ||||||||
| 4537 | return nullptr; | ||||||||
| 4538 | } | ||||||||
| 4539 | } while (!Worklist.empty()); | ||||||||
| 4540 | |||||||||
| 4541 | return Result; | ||||||||
| 4542 | } | ||||||||
| 4543 | |||||||||
| 4544 | static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper( | ||||||||
| 4545 | const Value *V, bool AllowLifetime, bool AllowDroppable) { | ||||||||
| 4546 | for (const User *U : V->users()) { | ||||||||
| 4547 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||||||
| 4548 | if (!II) | ||||||||
| 4549 | return false; | ||||||||
| 4550 | |||||||||
| 4551 | if (AllowLifetime && II->isLifetimeStartOrEnd()) | ||||||||
| 4552 | continue; | ||||||||
| 4553 | |||||||||
| 4554 | if (AllowDroppable && II->isDroppable()) | ||||||||
| 4555 | continue; | ||||||||
| 4556 | |||||||||
| 4557 | return false; | ||||||||
| 4558 | } | ||||||||
| 4559 | return true; | ||||||||
| 4560 | } | ||||||||
| 4561 | |||||||||
| 4562 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { | ||||||||
| 4563 | return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( | ||||||||
| 4564 | V, /* AllowLifetime */ true, /* AllowDroppable */ false); | ||||||||
| 4565 | } | ||||||||
| 4566 | bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) { | ||||||||
| 4567 | return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( | ||||||||
| 4568 | V, /* AllowLifetime */ true, /* AllowDroppable */ true); | ||||||||
| 4569 | } | ||||||||
| 4570 | |||||||||
| 4571 | bool llvm::mustSuppressSpeculation(const LoadInst &LI) { | ||||||||
| 4572 | if (!LI.isUnordered()) | ||||||||
| 4573 | return true; | ||||||||
| 4574 | const Function &F = *LI.getFunction(); | ||||||||
| 4575 | // Speculative load may create a race that did not exist in the source. | ||||||||
| 4576 | return F.hasFnAttribute(Attribute::SanitizeThread) || | ||||||||
| 4577 | // Speculative load may load data from dirty regions. | ||||||||
| 4578 | F.hasFnAttribute(Attribute::SanitizeAddress) || | ||||||||
| 4579 | F.hasFnAttribute(Attribute::SanitizeHWAddress); | ||||||||
| 4580 | } | ||||||||
| 4581 | |||||||||
| 4582 | |||||||||
| 4583 | bool llvm::isSafeToSpeculativelyExecute(const Value *V, | ||||||||
| 4584 | const Instruction *CtxI, | ||||||||
| 4585 | const DominatorTree *DT, | ||||||||
| 4586 | const TargetLibraryInfo *TLI) { | ||||||||
| 4587 | const Operator *Inst = dyn_cast<Operator>(V); | ||||||||
| 4588 | if (!Inst
| ||||||||
| 4589 | return false; | ||||||||
| 4590 | |||||||||
| 4591 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) | ||||||||
| 4592 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) | ||||||||
| 4593 | if (C->canTrap()) | ||||||||
| 4594 | return false; | ||||||||
| 4595 | |||||||||
| 4596 | switch (Inst->getOpcode()) { | ||||||||
| 4597 | default: | ||||||||
| 4598 | return true; | ||||||||
| 4599 | case Instruction::UDiv: | ||||||||
| 4600 | case Instruction::URem: { | ||||||||
| 4601 | // x / y is undefined if y == 0. | ||||||||
| 4602 | const APInt *V; | ||||||||
| 4603 | if (match(Inst->getOperand(1), m_APInt(V))) | ||||||||
| 4604 | return *V != 0; | ||||||||
| 4605 | return false; | ||||||||
| 4606 | } | ||||||||
| 4607 | case Instruction::SDiv: | ||||||||
| 4608 | case Instruction::SRem: { | ||||||||
| 4609 | // x / y is undefined if y == 0 or x == INT_MIN and y == -1 | ||||||||
| 4610 | const APInt *Numerator, *Denominator; | ||||||||
| 4611 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) | ||||||||
| 4612 | return false; | ||||||||
| 4613 | // We cannot hoist this division if the denominator is 0. | ||||||||
| 4614 | if (*Denominator == 0) | ||||||||
| 4615 | return false; | ||||||||
| 4616 | // It's safe to hoist if the denominator is not 0 or -1. | ||||||||
| 4617 | if (!Denominator->isAllOnesValue()) | ||||||||
| 4618 | return true; | ||||||||
| 4619 | // At this point we know that the denominator is -1. It is safe to hoist as | ||||||||
| 4620 | // long we know that the numerator is not INT_MIN. | ||||||||
| 4621 | if (match(Inst->getOperand(0), m_APInt(Numerator))) | ||||||||
| 4622 | return !Numerator->isMinSignedValue(); | ||||||||
| 4623 | // The numerator *might* be MinSignedValue. | ||||||||
| 4624 | return false; | ||||||||
| 4625 | } | ||||||||
| 4626 | case Instruction::Load: { | ||||||||
| 4627 | const LoadInst *LI = cast<LoadInst>(Inst); | ||||||||
| 4628 | if (mustSuppressSpeculation(*LI)) | ||||||||
| 4629 | return false; | ||||||||
| 4630 | const DataLayout &DL = LI->getModule()->getDataLayout(); | ||||||||
| 4631 | return isDereferenceableAndAlignedPointer( | ||||||||
| 4632 | LI->getPointerOperand(), LI->getType(), MaybeAlign(LI->getAlignment()), | ||||||||
| 4633 | DL, CtxI, DT, TLI); | ||||||||
| 4634 | } | ||||||||
| 4635 | case Instruction::Call: { | ||||||||
| 4636 | auto *CI = cast<const CallInst>(Inst); | ||||||||
| 4637 | const Function *Callee = CI->getCalledFunction(); | ||||||||
| 4638 | |||||||||
| 4639 | // The called function could have undefined behavior or side-effects, even | ||||||||
| 4640 | // if marked readnone nounwind. | ||||||||
| 4641 | return Callee && Callee->isSpeculatable(); | ||||||||
| 4642 | } | ||||||||
| 4643 | case Instruction::VAArg: | ||||||||
| 4644 | case Instruction::Alloca: | ||||||||
| 4645 | case Instruction::Invoke: | ||||||||
| 4646 | case Instruction::CallBr: | ||||||||
| 4647 | case Instruction::PHI: | ||||||||
| 4648 | case Instruction::Store: | ||||||||
| 4649 | case Instruction::Ret: | ||||||||
| 4650 | case Instruction::Br: | ||||||||
| 4651 | case Instruction::IndirectBr: | ||||||||
| 4652 | case Instruction::Switch: | ||||||||
| 4653 | case Instruction::Unreachable: | ||||||||
| 4654 | case Instruction::Fence: | ||||||||
| 4655 | case Instruction::AtomicRMW: | ||||||||
| 4656 | case Instruction::AtomicCmpXchg: | ||||||||
| 4657 | case Instruction::LandingPad: | ||||||||
| 4658 | case Instruction::Resume: | ||||||||
| 4659 | case Instruction::CatchSwitch: | ||||||||
| 4660 | case Instruction::CatchPad: | ||||||||
| 4661 | case Instruction::CatchRet: | ||||||||
| 4662 | case Instruction::CleanupPad: | ||||||||
| 4663 | case Instruction::CleanupRet: | ||||||||
| 4664 | return false; // Misc instructions which have effects | ||||||||
| 4665 | } | ||||||||
| 4666 | } | ||||||||
| 4667 | |||||||||
| 4668 | bool llvm::mayBeMemoryDependent(const Instruction &I) { | ||||||||
| 4669 | return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I); | ||||||||
| |||||||||
| 4670 | } | ||||||||
| 4671 | |||||||||
| 4672 | /// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. | ||||||||
| 4673 | static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) { | ||||||||
| 4674 | switch (OR) { | ||||||||
| 4675 | case ConstantRange::OverflowResult::MayOverflow: | ||||||||
| 4676 | return OverflowResult::MayOverflow; | ||||||||
| 4677 | case ConstantRange::OverflowResult::AlwaysOverflowsLow: | ||||||||
| 4678 | return OverflowResult::AlwaysOverflowsLow; | ||||||||
| 4679 | case ConstantRange::OverflowResult::AlwaysOverflowsHigh: | ||||||||
| 4680 | return OverflowResult::AlwaysOverflowsHigh; | ||||||||
| 4681 | case ConstantRange::OverflowResult::NeverOverflows: | ||||||||
| 4682 | return OverflowResult::NeverOverflows; | ||||||||
| 4683 | } | ||||||||
| 4684 | llvm_unreachable("Unknown OverflowResult")__builtin_unreachable(); | ||||||||
| 4685 | } | ||||||||
| 4686 | |||||||||
| 4687 | /// Combine constant ranges from computeConstantRange() and computeKnownBits(). | ||||||||
| 4688 | static ConstantRange computeConstantRangeIncludingKnownBits( | ||||||||
| 4689 | const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth, | ||||||||
| 4690 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 4691 | OptimizationRemarkEmitter *ORE = nullptr, bool UseInstrInfo = true) { | ||||||||
| 4692 | KnownBits Known = computeKnownBits( | ||||||||
| 4693 | V, DL, Depth, AC, CxtI, DT, ORE, UseInstrInfo); | ||||||||
| 4694 | ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned); | ||||||||
| 4695 | ConstantRange CR2 = computeConstantRange(V, UseInstrInfo); | ||||||||
| 4696 | ConstantRange::PreferredRangeType RangeType = | ||||||||
| 4697 | ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned; | ||||||||
| 4698 | return CR1.intersectWith(CR2, RangeType); | ||||||||
| 4699 | } | ||||||||
| 4700 | |||||||||
| 4701 | OverflowResult llvm::computeOverflowForUnsignedMul( | ||||||||
| 4702 | const Value *LHS, const Value *RHS, const DataLayout &DL, | ||||||||
| 4703 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 4704 | bool UseInstrInfo) { | ||||||||
| 4705 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4706 | nullptr, UseInstrInfo); | ||||||||
| 4707 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4708 | nullptr, UseInstrInfo); | ||||||||
| 4709 | ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false); | ||||||||
| 4710 | ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false); | ||||||||
| 4711 | return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange)); | ||||||||
| 4712 | } | ||||||||
| 4713 | |||||||||
| 4714 | OverflowResult | ||||||||
| 4715 | llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, | ||||||||
| 4716 | const DataLayout &DL, AssumptionCache *AC, | ||||||||
| 4717 | const Instruction *CxtI, | ||||||||
| 4718 | const DominatorTree *DT, bool UseInstrInfo) { | ||||||||
| 4719 | // Multiplying n * m significant bits yields a result of n + m significant | ||||||||
| 4720 | // bits. If the total number of significant bits does not exceed the | ||||||||
| 4721 | // result bit width (minus 1), there is no overflow. | ||||||||
| 4722 | // This means if we have enough leading sign bits in the operands | ||||||||
| 4723 | // we can guarantee that the result does not overflow. | ||||||||
| 4724 | // Ref: "Hacker's Delight" by Henry Warren | ||||||||
| 4725 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); | ||||||||
| 4726 | |||||||||
| 4727 | // Note that underestimating the number of sign bits gives a more | ||||||||
| 4728 | // conservative answer. | ||||||||
| 4729 | unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + | ||||||||
| 4730 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); | ||||||||
| 4731 | |||||||||
| 4732 | // First handle the easy case: if we have enough sign bits there's | ||||||||
| 4733 | // definitely no overflow. | ||||||||
| 4734 | if (SignBits > BitWidth + 1) | ||||||||
| 4735 | return OverflowResult::NeverOverflows; | ||||||||
| 4736 | |||||||||
| 4737 | // There are two ambiguous cases where there can be no overflow: | ||||||||
| 4738 | // SignBits == BitWidth + 1 and | ||||||||
| 4739 | // SignBits == BitWidth | ||||||||
| 4740 | // The second case is difficult to check, therefore we only handle the | ||||||||
| 4741 | // first case. | ||||||||
| 4742 | if (SignBits == BitWidth + 1) { | ||||||||
| 4743 | // It overflows only when both arguments are negative and the true | ||||||||
| 4744 | // product is exactly the minimum negative number. | ||||||||
| 4745 | // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 | ||||||||
| 4746 | // For simplicity we just check if at least one side is not negative. | ||||||||
| 4747 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4748 | nullptr, UseInstrInfo); | ||||||||
| 4749 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4750 | nullptr, UseInstrInfo); | ||||||||
| 4751 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) | ||||||||
| 4752 | return OverflowResult::NeverOverflows; | ||||||||
| 4753 | } | ||||||||
| 4754 | return OverflowResult::MayOverflow; | ||||||||
| 4755 | } | ||||||||
| 4756 | |||||||||
| 4757 | OverflowResult llvm::computeOverflowForUnsignedAdd( | ||||||||
| 4758 | const Value *LHS, const Value *RHS, const DataLayout &DL, | ||||||||
| 4759 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, | ||||||||
| 4760 | bool UseInstrInfo) { | ||||||||
| 4761 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4762 | LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4763 | nullptr, UseInstrInfo); | ||||||||
| 4764 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4765 | RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, | ||||||||
| 4766 | nullptr, UseInstrInfo); | ||||||||
| 4767 | return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange)); | ||||||||
| 4768 | } | ||||||||
| 4769 | |||||||||
| 4770 | static OverflowResult computeOverflowForSignedAdd(const Value *LHS, | ||||||||
| 4771 | const Value *RHS, | ||||||||
| 4772 | const AddOperator *Add, | ||||||||
| 4773 | const DataLayout &DL, | ||||||||
| 4774 | AssumptionCache *AC, | ||||||||
| 4775 | const Instruction *CxtI, | ||||||||
| 4776 | const DominatorTree *DT) { | ||||||||
| 4777 | if (Add && Add->hasNoSignedWrap()) { | ||||||||
| 4778 | return OverflowResult::NeverOverflows; | ||||||||
| 4779 | } | ||||||||
| 4780 | |||||||||
| 4781 | // If LHS and RHS each have at least two sign bits, the addition will look | ||||||||
| 4782 | // like | ||||||||
| 4783 | // | ||||||||
| 4784 | // XX..... + | ||||||||
| 4785 | // YY..... | ||||||||
| 4786 | // | ||||||||
| 4787 | // If the carry into the most significant position is 0, X and Y can't both | ||||||||
| 4788 | // be 1 and therefore the carry out of the addition is also 0. | ||||||||
| 4789 | // | ||||||||
| 4790 | // If the carry into the most significant position is 1, X and Y can't both | ||||||||
| 4791 | // be 0 and therefore the carry out of the addition is also 1. | ||||||||
| 4792 | // | ||||||||
| 4793 | // Since the carry into the most significant position is always equal to | ||||||||
| 4794 | // the carry out of the addition, there is no signed overflow. | ||||||||
| 4795 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && | ||||||||
| 4796 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) | ||||||||
| 4797 | return OverflowResult::NeverOverflows; | ||||||||
| 4798 | |||||||||
| 4799 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4800 | LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4801 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4802 | RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4803 | OverflowResult OR = | ||||||||
| 4804 | mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange)); | ||||||||
| 4805 | if (OR != OverflowResult::MayOverflow) | ||||||||
| 4806 | return OR; | ||||||||
| 4807 | |||||||||
| 4808 | // The remaining code needs Add to be available. Early returns if not so. | ||||||||
| 4809 | if (!Add) | ||||||||
| 4810 | return OverflowResult::MayOverflow; | ||||||||
| 4811 | |||||||||
| 4812 | // If the sign of Add is the same as at least one of the operands, this add | ||||||||
| 4813 | // CANNOT overflow. If this can be determined from the known bits of the | ||||||||
| 4814 | // operands the above signedAddMayOverflow() check will have already done so. | ||||||||
| 4815 | // The only other way to improve on the known bits is from an assumption, so | ||||||||
| 4816 | // call computeKnownBitsFromAssume() directly. | ||||||||
| 4817 | bool LHSOrRHSKnownNonNegative = | ||||||||
| 4818 | (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative()); | ||||||||
| 4819 | bool LHSOrRHSKnownNegative = | ||||||||
| 4820 | (LHSRange.isAllNegative() || RHSRange.isAllNegative()); | ||||||||
| 4821 | if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { | ||||||||
| 4822 | KnownBits AddKnown(LHSRange.getBitWidth()); | ||||||||
| 4823 | computeKnownBitsFromAssume( | ||||||||
| 4824 | Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true)); | ||||||||
| 4825 | if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || | ||||||||
| 4826 | (AddKnown.isNegative() && LHSOrRHSKnownNegative)) | ||||||||
| 4827 | return OverflowResult::NeverOverflows; | ||||||||
| 4828 | } | ||||||||
| 4829 | |||||||||
| 4830 | return OverflowResult::MayOverflow; | ||||||||
| 4831 | } | ||||||||
| 4832 | |||||||||
| 4833 | OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, | ||||||||
| 4834 | const Value *RHS, | ||||||||
| 4835 | const DataLayout &DL, | ||||||||
| 4836 | AssumptionCache *AC, | ||||||||
| 4837 | const Instruction *CxtI, | ||||||||
| 4838 | const DominatorTree *DT) { | ||||||||
| 4839 | // Checking for conditions implied by dominating conditions may be expensive. | ||||||||
| 4840 | // Limit it to usub_with_overflow calls for now. | ||||||||
| 4841 | if (match(CxtI, | ||||||||
| 4842 | m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value()))) | ||||||||
| 4843 | if (auto C = | ||||||||
| 4844 | isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) { | ||||||||
| 4845 | if (*C) | ||||||||
| 4846 | return OverflowResult::NeverOverflows; | ||||||||
| 4847 | return OverflowResult::AlwaysOverflowsLow; | ||||||||
| 4848 | } | ||||||||
| 4849 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4850 | LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4851 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4852 | RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4853 | return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange)); | ||||||||
| 4854 | } | ||||||||
| 4855 | |||||||||
| 4856 | OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, | ||||||||
| 4857 | const Value *RHS, | ||||||||
| 4858 | const DataLayout &DL, | ||||||||
| 4859 | AssumptionCache *AC, | ||||||||
| 4860 | const Instruction *CxtI, | ||||||||
| 4861 | const DominatorTree *DT) { | ||||||||
| 4862 | // If LHS and RHS each have at least two sign bits, the subtraction | ||||||||
| 4863 | // cannot overflow. | ||||||||
| 4864 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && | ||||||||
| 4865 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) | ||||||||
| 4866 | return OverflowResult::NeverOverflows; | ||||||||
| 4867 | |||||||||
| 4868 | ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4869 | LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4870 | ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( | ||||||||
| 4871 | RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); | ||||||||
| 4872 | return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange)); | ||||||||
| 4873 | } | ||||||||
| 4874 | |||||||||
| 4875 | bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, | ||||||||
| 4876 | const DominatorTree &DT) { | ||||||||
| 4877 | SmallVector<const BranchInst *, 2> GuardingBranches; | ||||||||
| 4878 | SmallVector<const ExtractValueInst *, 2> Results; | ||||||||
| 4879 | |||||||||
| 4880 | for (const User *U : WO->users()) { | ||||||||
| 4881 | if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) { | ||||||||
| 4882 | assert(EVI->getNumIndices() == 1 && "Obvious from CI's type")((void)0); | ||||||||
| 4883 | |||||||||
| 4884 | if (EVI->getIndices()[0] == 0) | ||||||||
| 4885 | Results.push_back(EVI); | ||||||||
| 4886 | else { | ||||||||
| 4887 | assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type")((void)0); | ||||||||
| 4888 | |||||||||
| 4889 | for (const auto *U : EVI->users()) | ||||||||
| 4890 | if (const auto *B = dyn_cast<BranchInst>(U)) { | ||||||||
| 4891 | assert(B->isConditional() && "How else is it using an i1?")((void)0); | ||||||||
| 4892 | GuardingBranches.push_back(B); | ||||||||
| 4893 | } | ||||||||
| 4894 | } | ||||||||
| 4895 | } else { | ||||||||
| 4896 | // We are using the aggregate directly in a way we don't want to analyze | ||||||||
| 4897 | // here (storing it to a global, say). | ||||||||
| 4898 | return false; | ||||||||
| 4899 | } | ||||||||
| 4900 | } | ||||||||
| 4901 | |||||||||
| 4902 | auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { | ||||||||
| 4903 | BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); | ||||||||
| 4904 | if (!NoWrapEdge.isSingleEdge()) | ||||||||
| 4905 | return false; | ||||||||
| 4906 | |||||||||
| 4907 | // Check if all users of the add are provably no-wrap. | ||||||||
| 4908 | for (const auto *Result : Results) { | ||||||||
| 4909 | // If the extractvalue itself is not executed on overflow, the we don't | ||||||||
| 4910 | // need to check each use separately, since domination is transitive. | ||||||||
| 4911 | if (DT.dominates(NoWrapEdge, Result->getParent())) | ||||||||
| 4912 | continue; | ||||||||
| 4913 | |||||||||
| 4914 | for (auto &RU : Result->uses()) | ||||||||
| 4915 | if (!DT.dominates(NoWrapEdge, RU)) | ||||||||
| 4916 | return false; | ||||||||
| 4917 | } | ||||||||
| 4918 | |||||||||
| 4919 | return true; | ||||||||
| 4920 | }; | ||||||||
| 4921 | |||||||||
| 4922 | return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); | ||||||||
| 4923 | } | ||||||||
| 4924 | |||||||||
| 4925 | static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly) { | ||||||||
| 4926 | // See whether I has flags that may create poison | ||||||||
| 4927 | if (const auto *OvOp = dyn_cast<OverflowingBinaryOperator>(Op)) { | ||||||||
| 4928 | if (OvOp->hasNoSignedWrap() || OvOp->hasNoUnsignedWrap()) | ||||||||
| 4929 | return true; | ||||||||
| 4930 | } | ||||||||
| 4931 | if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(Op)) | ||||||||
| 4932 | if (ExactOp->isExact()) | ||||||||
| 4933 | return true; | ||||||||
| 4934 | if (const auto *FP = dyn_cast<FPMathOperator>(Op)) { | ||||||||
| 4935 | auto FMF = FP->getFastMathFlags(); | ||||||||
| 4936 | if (FMF.noNaNs() || FMF.noInfs()) | ||||||||
| 4937 | return true; | ||||||||
| 4938 | } | ||||||||
| 4939 | |||||||||
| 4940 | unsigned Opcode = Op->getOpcode(); | ||||||||
| 4941 | |||||||||
| 4942 | // Check whether opcode is a poison/undef-generating operation | ||||||||
| 4943 | switch (Opcode) { | ||||||||
| 4944 | case Instruction::Shl: | ||||||||
| 4945 | case Instruction::AShr: | ||||||||
| 4946 | case Instruction::LShr: { | ||||||||
| 4947 | // Shifts return poison if shiftwidth is larger than the bitwidth. | ||||||||
| 4948 | if (auto *C = dyn_cast<Constant>(Op->getOperand(1))) { | ||||||||
| 4949 | SmallVector<Constant *, 4> ShiftAmounts; | ||||||||
| 4950 | if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) { | ||||||||
| 4951 | unsigned NumElts = FVTy->getNumElements(); | ||||||||
| 4952 | for (unsigned i = 0; i < NumElts; ++i) | ||||||||
| 4953 | ShiftAmounts.push_back(C->getAggregateElement(i)); | ||||||||
| 4954 | } else if (isa<ScalableVectorType>(C->getType())) | ||||||||
| 4955 | return true; // Can't tell, just return true to be safe | ||||||||
| 4956 | else | ||||||||
| 4957 | ShiftAmounts.push_back(C); | ||||||||
| 4958 | |||||||||
| 4959 | bool Safe = llvm::all_of(ShiftAmounts, [](Constant *C) { | ||||||||
| 4960 | auto *CI = dyn_cast_or_null<ConstantInt>(C); | ||||||||
| 4961 | return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth()); | ||||||||
| 4962 | }); | ||||||||
| 4963 | return !Safe; | ||||||||
| 4964 | } | ||||||||
| 4965 | return true; | ||||||||
| 4966 | } | ||||||||
| 4967 | case Instruction::FPToSI: | ||||||||
| 4968 | case Instruction::FPToUI: | ||||||||
| 4969 | // fptosi/ui yields poison if the resulting value does not fit in the | ||||||||
| 4970 | // destination type. | ||||||||
| 4971 | return true; | ||||||||
| 4972 | case Instruction::Call: | ||||||||
| 4973 | if (auto *II = dyn_cast<IntrinsicInst>(Op)) { | ||||||||
| 4974 | switch (II->getIntrinsicID()) { | ||||||||
| 4975 | // TODO: Add more intrinsics. | ||||||||
| 4976 | case Intrinsic::ctpop: | ||||||||
| 4977 | case Intrinsic::sadd_with_overflow: | ||||||||
| 4978 | case Intrinsic::ssub_with_overflow: | ||||||||
| 4979 | case Intrinsic::smul_with_overflow: | ||||||||
| 4980 | case Intrinsic::uadd_with_overflow: | ||||||||
| 4981 | case Intrinsic::usub_with_overflow: | ||||||||
| 4982 | case Intrinsic::umul_with_overflow: | ||||||||
| 4983 | return false; | ||||||||
| 4984 | } | ||||||||
| 4985 | } | ||||||||
| 4986 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||||||
| 4987 | case Instruction::CallBr: | ||||||||
| 4988 | case Instruction::Invoke: { | ||||||||
| 4989 | const auto *CB = cast<CallBase>(Op); | ||||||||
| 4990 | return !CB->hasRetAttr(Attribute::NoUndef); | ||||||||
| 4991 | } | ||||||||
| 4992 | case Instruction::InsertElement: | ||||||||
| 4993 | case Instruction::ExtractElement: { | ||||||||
| 4994 | // If index exceeds the length of the vector, it returns poison | ||||||||
| 4995 | auto *VTy = cast<VectorType>(Op->getOperand(0)->getType()); | ||||||||
| 4996 | unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1; | ||||||||
| 4997 | auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp)); | ||||||||
| 4998 | if (!Idx || Idx->getValue().uge(VTy->getElementCount().getKnownMinValue())) | ||||||||
| 4999 | return true; | ||||||||
| 5000 | return false; | ||||||||
| 5001 | } | ||||||||
| 5002 | case Instruction::ShuffleVector: { | ||||||||
| 5003 | // shufflevector may return undef. | ||||||||
| 5004 | if (PoisonOnly) | ||||||||
| 5005 | return false; | ||||||||
| 5006 | ArrayRef<int> Mask = isa<ConstantExpr>(Op) | ||||||||
| 5007 | ? cast<ConstantExpr>(Op)->getShuffleMask() | ||||||||
| 5008 | : cast<ShuffleVectorInst>(Op)->getShuffleMask(); | ||||||||
| 5009 | return is_contained(Mask, UndefMaskElem); | ||||||||
| 5010 | } | ||||||||
| 5011 | case Instruction::FNeg: | ||||||||
| 5012 | case Instruction::PHI: | ||||||||
| 5013 | case Instruction::Select: | ||||||||
| 5014 | case Instruction::URem: | ||||||||
| 5015 | case Instruction::SRem: | ||||||||
| 5016 | case Instruction::ExtractValue: | ||||||||
| 5017 | case Instruction::InsertValue: | ||||||||
| 5018 | case Instruction::Freeze: | ||||||||
| 5019 | case Instruction::ICmp: | ||||||||
| 5020 | case Instruction::FCmp: | ||||||||
| 5021 | return false; | ||||||||
| 5022 | case Instruction::GetElementPtr: { | ||||||||
| 5023 | const auto *GEP = cast<GEPOperator>(Op); | ||||||||
| 5024 | return GEP->isInBounds(); | ||||||||
| 5025 | } | ||||||||
| 5026 | default: { | ||||||||
| 5027 | const auto *CE = dyn_cast<ConstantExpr>(Op); | ||||||||
| 5028 | if (isa<CastInst>(Op) || (CE && CE->isCast())) | ||||||||
| 5029 | return false; | ||||||||
| 5030 | else if (Instruction::isBinaryOp(Opcode)) | ||||||||
| 5031 | return false; | ||||||||
| 5032 | // Be conservative and return true. | ||||||||
| 5033 | return true; | ||||||||
| 5034 | } | ||||||||
| 5035 | } | ||||||||
| 5036 | } | ||||||||
| 5037 | |||||||||
| 5038 | bool llvm::canCreateUndefOrPoison(const Operator *Op) { | ||||||||
| 5039 | return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/false); | ||||||||
| 5040 | } | ||||||||
| 5041 | |||||||||
| 5042 | bool llvm::canCreatePoison(const Operator *Op) { | ||||||||
| 5043 | return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/true); | ||||||||
| 5044 | } | ||||||||
| 5045 | |||||||||
| 5046 | static bool directlyImpliesPoison(const Value *ValAssumedPoison, | ||||||||
| 5047 | const Value *V, unsigned Depth) { | ||||||||
| 5048 | if (ValAssumedPoison == V) | ||||||||
| 5049 | return true; | ||||||||
| 5050 | |||||||||
| 5051 | const unsigned MaxDepth = 2; | ||||||||
| 5052 | if (Depth >= MaxDepth) | ||||||||
| 5053 | return false; | ||||||||
| 5054 | |||||||||
| 5055 | if (const auto *I = dyn_cast<Instruction>(V)) { | ||||||||
| 5056 | if (propagatesPoison(cast<Operator>(I))) | ||||||||
| 5057 | return any_of(I->operands(), [=](const Value *Op) { | ||||||||
| 5058 | return directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1); | ||||||||
| 5059 | }); | ||||||||
| 5060 | |||||||||
| 5061 | // 'select ValAssumedPoison, _, _' is poison. | ||||||||
| 5062 | if (const auto *SI = dyn_cast<SelectInst>(I)) | ||||||||
| 5063 | return directlyImpliesPoison(ValAssumedPoison, SI->getCondition(), | ||||||||
| 5064 | Depth + 1); | ||||||||
| 5065 | // V = extractvalue V0, idx | ||||||||
| 5066 | // V2 = extractvalue V0, idx2 | ||||||||
| 5067 | // V0's elements are all poison or not. (e.g., add_with_overflow) | ||||||||
| 5068 | const WithOverflowInst *II; | ||||||||
| 5069 | if (match(I, m_ExtractValue(m_WithOverflowInst(II))) && | ||||||||
| 5070 | (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) || | ||||||||
| 5071 | llvm::is_contained(II->arg_operands(), ValAssumedPoison))) | ||||||||
| 5072 | return true; | ||||||||
| 5073 | } | ||||||||
| 5074 | return false; | ||||||||
| 5075 | } | ||||||||
| 5076 | |||||||||
| 5077 | static bool impliesPoison(const Value *ValAssumedPoison, const Value *V, | ||||||||
| 5078 | unsigned Depth) { | ||||||||
| 5079 | if (isGuaranteedNotToBeUndefOrPoison(ValAssumedPoison)) | ||||||||
| 5080 | return true; | ||||||||
| 5081 | |||||||||
| 5082 | if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0)) | ||||||||
| 5083 | return true; | ||||||||
| 5084 | |||||||||
| 5085 | const unsigned MaxDepth = 2; | ||||||||
| 5086 | if (Depth >= MaxDepth) | ||||||||
| 5087 | return false; | ||||||||
| 5088 | |||||||||
| 5089 | const auto *I = dyn_cast<Instruction>(ValAssumedPoison); | ||||||||
| 5090 | if (I && !canCreatePoison(cast<Operator>(I))) { | ||||||||
| 5091 | return all_of(I->operands(), [=](const Value *Op) { | ||||||||
| 5092 | return impliesPoison(Op, V, Depth + 1); | ||||||||
| 5093 | }); | ||||||||
| 5094 | } | ||||||||
| 5095 | return false; | ||||||||
| 5096 | } | ||||||||
| 5097 | |||||||||
| 5098 | bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) { | ||||||||
| 5099 | return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0); | ||||||||
| 5100 | } | ||||||||
| 5101 | |||||||||
| 5102 | static bool programUndefinedIfUndefOrPoison(const Value *V, | ||||||||
| 5103 | bool PoisonOnly); | ||||||||
| 5104 | |||||||||
| 5105 | static bool isGuaranteedNotToBeUndefOrPoison(const Value *V, | ||||||||
| 5106 | AssumptionCache *AC, | ||||||||
| 5107 | const Instruction *CtxI, | ||||||||
| 5108 | const DominatorTree *DT, | ||||||||
| 5109 | unsigned Depth, bool PoisonOnly) { | ||||||||
| 5110 | if (Depth >= MaxAnalysisRecursionDepth) | ||||||||
| 5111 | return false; | ||||||||
| 5112 | |||||||||
| 5113 | if (isa<MetadataAsValue>(V)) | ||||||||
| 5114 | return false; | ||||||||
| 5115 | |||||||||
| 5116 | if (const auto *A = dyn_cast<Argument>(V)) { | ||||||||
| 5117 | if (A->hasAttribute(Attribute::NoUndef)) | ||||||||
| 5118 | return true; | ||||||||
| 5119 | } | ||||||||
| 5120 | |||||||||
| 5121 | if (auto *C = dyn_cast<Constant>(V)) { | ||||||||
| 5122 | if (isa<UndefValue>(C)) | ||||||||
| 5123 | return PoisonOnly && !isa<PoisonValue>(C); | ||||||||
| 5124 | |||||||||
| 5125 | if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) || | ||||||||
| 5126 | isa<ConstantPointerNull>(C) || isa<Function>(C)) | ||||||||
| 5127 | return true; | ||||||||
| 5128 | |||||||||
| 5129 | if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C)) | ||||||||
| 5130 | return (PoisonOnly ? !C->containsPoisonElement() | ||||||||
| 5131 | : !C->containsUndefOrPoisonElement()) && | ||||||||
| 5132 | !C->containsConstantExpression(); | ||||||||
| 5133 | } | ||||||||
| 5134 | |||||||||
| 5135 | // Strip cast operations from a pointer value. | ||||||||
| 5136 | // Note that stripPointerCastsSameRepresentation can strip off getelementptr | ||||||||
| 5137 | // inbounds with zero offset. To guarantee that the result isn't poison, the | ||||||||
| 5138 | // stripped pointer is checked as it has to be pointing into an allocated | ||||||||
| 5139 | // object or be null `null` to ensure `inbounds` getelement pointers with a | ||||||||
| 5140 | // zero offset could not produce poison. | ||||||||
| 5141 | // It can strip off addrspacecast that do not change bit representation as | ||||||||
| 5142 | // well. We believe that such addrspacecast is equivalent to no-op. | ||||||||
| 5143 | auto *StrippedV = V->stripPointerCastsSameRepresentation(); | ||||||||
| 5144 | if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) || | ||||||||
| 5145 | isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV)) | ||||||||
| 5146 | return true; | ||||||||
| 5147 | |||||||||
| 5148 | auto OpCheck = [&](const Value *V) { | ||||||||
| 5149 | return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, | ||||||||
| 5150 | PoisonOnly); | ||||||||
| 5151 | }; | ||||||||
| 5152 | |||||||||
| 5153 | if (auto *Opr = dyn_cast<Operator>(V)) { | ||||||||
| 5154 | // If the value is a freeze instruction, then it can never | ||||||||
| 5155 | // be undef or poison. | ||||||||
| 5156 | if (isa<FreezeInst>(V)) | ||||||||
| 5157 | return true; | ||||||||
| 5158 | |||||||||
| 5159 | if (const auto *CB = dyn_cast<CallBase>(V)) { | ||||||||
| 5160 | if (CB->hasRetAttr(Attribute::NoUndef)) | ||||||||
| 5161 | return true; | ||||||||
| 5162 | } | ||||||||
| 5163 | |||||||||
| 5164 | if (const auto *PN = dyn_cast<PHINode>(V)) { | ||||||||
| 5165 | unsigned Num = PN->getNumIncomingValues(); | ||||||||
| 5166 | bool IsWellDefined = true; | ||||||||
| 5167 | for (unsigned i = 0; i < Num; ++i) { | ||||||||
| 5168 | auto *TI = PN->getIncomingBlock(i)->getTerminator(); | ||||||||
| 5169 | if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI, | ||||||||
| 5170 | DT, Depth + 1, PoisonOnly)) { | ||||||||
| 5171 | IsWellDefined = false; | ||||||||
| 5172 | break; | ||||||||
| 5173 | } | ||||||||
| 5174 | } | ||||||||
| 5175 | if (IsWellDefined) | ||||||||
| 5176 | return true; | ||||||||
| 5177 | } else if (!canCreateUndefOrPoison(Opr) && all_of(Opr->operands(), OpCheck)) | ||||||||
| 5178 | return true; | ||||||||
| 5179 | } | ||||||||
| 5180 | |||||||||
| 5181 | if (auto *I = dyn_cast<LoadInst>(V)) | ||||||||
| 5182 | if (I->getMetadata(LLVMContext::MD_noundef)) | ||||||||
| 5183 | return true; | ||||||||
| 5184 | |||||||||
| 5185 | if (programUndefinedIfUndefOrPoison(V, PoisonOnly)) | ||||||||
| 5186 | return true; | ||||||||
| 5187 | |||||||||
| 5188 | // CxtI may be null or a cloned instruction. | ||||||||
| 5189 | if (!CtxI || !CtxI->getParent() || !DT) | ||||||||
| 5190 | return false; | ||||||||
| 5191 | |||||||||
| 5192 | auto *DNode = DT->getNode(CtxI->getParent()); | ||||||||
| 5193 | if (!DNode) | ||||||||
| 5194 | // Unreachable block | ||||||||
| 5195 | return false; | ||||||||
| 5196 | |||||||||
| 5197 | // If V is used as a branch condition before reaching CtxI, V cannot be | ||||||||
| 5198 | // undef or poison. | ||||||||
| 5199 | // br V, BB1, BB2 | ||||||||
| 5200 | // BB1: | ||||||||
| 5201 | // CtxI ; V cannot be undef or poison here | ||||||||
| 5202 | auto *Dominator = DNode->getIDom(); | ||||||||
| 5203 | while (Dominator) { | ||||||||
| 5204 | auto *TI = Dominator->getBlock()->getTerminator(); | ||||||||
| 5205 | |||||||||
| 5206 | Value *Cond = nullptr; | ||||||||
| 5207 | if (auto BI = dyn_cast<BranchInst>(TI)) { | ||||||||
| 5208 | if (BI->isConditional()) | ||||||||
| 5209 | Cond = BI->getCondition(); | ||||||||
| 5210 | } else if (auto SI = dyn_cast<SwitchInst>(TI)) { | ||||||||
| 5211 | Cond = SI->getCondition(); | ||||||||
| 5212 | } | ||||||||
| 5213 | |||||||||
| 5214 | if (Cond) { | ||||||||
| 5215 | if (Cond == V) | ||||||||
| 5216 | return true; | ||||||||
| 5217 | else if (PoisonOnly && isa<Operator>(Cond)) { | ||||||||
| 5218 | // For poison, we can analyze further | ||||||||
| 5219 | auto *Opr = cast<Operator>(Cond); | ||||||||
| 5220 | if (propagatesPoison(Opr) && is_contained(Opr->operand_values(), V)) | ||||||||
| 5221 | return true; | ||||||||
| 5222 | } | ||||||||
| 5223 | } | ||||||||
| 5224 | |||||||||
| 5225 | Dominator = Dominator->getIDom(); | ||||||||
| 5226 | } | ||||||||
| 5227 | |||||||||
| 5228 | SmallVector<Attribute::AttrKind, 2> AttrKinds{Attribute::NoUndef}; | ||||||||
| 5229 | if (getKnowledgeValidInContext(V, AttrKinds, CtxI, DT, AC)) | ||||||||
| 5230 | return true; | ||||||||
| 5231 | |||||||||
| 5232 | return false; | ||||||||
| 5233 | } | ||||||||
| 5234 | |||||||||
| 5235 | bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC, | ||||||||
| 5236 | const Instruction *CtxI, | ||||||||
| 5237 | const DominatorTree *DT, | ||||||||
| 5238 | unsigned Depth) { | ||||||||
| 5239 | return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, false); | ||||||||
| 5240 | } | ||||||||
| 5241 | |||||||||
| 5242 | bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC, | ||||||||
| 5243 | const Instruction *CtxI, | ||||||||
| 5244 | const DominatorTree *DT, unsigned Depth) { | ||||||||
| 5245 | return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, true); | ||||||||
| 5246 | } | ||||||||
| 5247 | |||||||||
| 5248 | OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, | ||||||||
| 5249 | const DataLayout &DL, | ||||||||
| 5250 | AssumptionCache *AC, | ||||||||
| 5251 | const Instruction *CxtI, | ||||||||
| 5252 | const DominatorTree *DT) { | ||||||||
| 5253 | return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), | ||||||||
| 5254 | Add, DL, AC, CxtI, DT); | ||||||||
| 5255 | } | ||||||||
| 5256 | |||||||||
| 5257 | OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, | ||||||||
| 5258 | const Value *RHS, | ||||||||
| 5259 | const DataLayout &DL, | ||||||||
| 5260 | AssumptionCache *AC, | ||||||||
| 5261 | const Instruction *CxtI, | ||||||||
| 5262 | const DominatorTree *DT) { | ||||||||
| 5263 | return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); | ||||||||
| 5264 | } | ||||||||
| 5265 | |||||||||
| 5266 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { | ||||||||
| 5267 | // Note: An atomic operation isn't guaranteed to return in a reasonable amount | ||||||||
| 5268 | // of time because it's possible for another thread to interfere with it for an | ||||||||
| 5269 | // arbitrary length of time, but programs aren't allowed to rely on that. | ||||||||
| 5270 | |||||||||
| 5271 | // If there is no successor, then execution can't transfer to it. | ||||||||
| 5272 | if (isa<ReturnInst>(I)) | ||||||||
| 5273 | return false; | ||||||||
| 5274 | if (isa<UnreachableInst>(I)) | ||||||||
| 5275 | return false; | ||||||||
| 5276 | |||||||||
| 5277 | // Note: Do not add new checks here; instead, change Instruction::mayThrow or | ||||||||
| 5278 | // Instruction::willReturn. | ||||||||
| 5279 | // | ||||||||
| 5280 | // FIXME: Move this check into Instruction::willReturn. | ||||||||
| 5281 | if (isa<CatchPadInst>(I)) { | ||||||||
| 5282 | switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) { | ||||||||
| 5283 | default: | ||||||||
| 5284 | // A catchpad may invoke exception object constructors and such, which | ||||||||
| 5285 | // in some languages can be arbitrary code, so be conservative by default. | ||||||||
| 5286 | return false; | ||||||||
| 5287 | case EHPersonality::CoreCLR: | ||||||||
| 5288 | // For CoreCLR, it just involves a type test. | ||||||||
| 5289 | return true; | ||||||||
| 5290 | } | ||||||||
| 5291 | } | ||||||||
| 5292 | |||||||||
| 5293 | // An instruction that returns without throwing must transfer control flow | ||||||||
| 5294 | // to a successor. | ||||||||
| 5295 | return !I->mayThrow() && I->willReturn(); | ||||||||
| 5296 | } | ||||||||
| 5297 | |||||||||
| 5298 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { | ||||||||
| 5299 | // TODO: This is slightly conservative for invoke instruction since exiting | ||||||||
| 5300 | // via an exception *is* normal control for them. | ||||||||
| 5301 | for (const Instruction &I : *BB) | ||||||||
| 5302 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) | ||||||||
| 5303 | return false; | ||||||||
| 5304 | return true; | ||||||||
| 5305 | } | ||||||||
| 5306 | |||||||||
| 5307 | bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, | ||||||||
| 5308 | const Loop *L) { | ||||||||
| 5309 | // The loop header is guaranteed to be executed for every iteration. | ||||||||
| 5310 | // | ||||||||
| 5311 | // FIXME: Relax this constraint to cover all basic blocks that are | ||||||||
| 5312 | // guaranteed to be executed at every iteration. | ||||||||
| 5313 | if (I->getParent() != L->getHeader()) return false; | ||||||||
| 5314 | |||||||||
| 5315 | for (const Instruction &LI : *L->getHeader()) { | ||||||||
| 5316 | if (&LI == I) return true; | ||||||||
| 5317 | if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; | ||||||||
| 5318 | } | ||||||||
| 5319 | llvm_unreachable("Instruction not contained in its own parent basic block.")__builtin_unreachable(); | ||||||||
| 5320 | } | ||||||||
| 5321 | |||||||||
| 5322 | bool llvm::propagatesPoison(const Operator *I) { | ||||||||
| 5323 | switch (I->getOpcode()) { | ||||||||
| 5324 | case Instruction::Freeze: | ||||||||
| 5325 | case Instruction::Select: | ||||||||
| 5326 | case Instruction::PHI: | ||||||||
| 5327 | case Instruction::Invoke: | ||||||||
| 5328 | return false; | ||||||||
| 5329 | case Instruction::Call: | ||||||||
| 5330 | if (auto *II = dyn_cast<IntrinsicInst>(I)) { | ||||||||
| 5331 | switch (II->getIntrinsicID()) { | ||||||||
| 5332 | // TODO: Add more intrinsics. | ||||||||
| 5333 | case Intrinsic::sadd_with_overflow: | ||||||||
| 5334 | case Intrinsic::ssub_with_overflow: | ||||||||
| 5335 | case Intrinsic::smul_with_overflow: | ||||||||
| 5336 | case Intrinsic::uadd_with_overflow: | ||||||||
| 5337 | case Intrinsic::usub_with_overflow: | ||||||||
| 5338 | case Intrinsic::umul_with_overflow: | ||||||||
| 5339 | // If an input is a vector containing a poison element, the | ||||||||
| 5340 | // two output vectors (calculated results, overflow bits)' | ||||||||
| 5341 | // corresponding lanes are poison. | ||||||||
| 5342 | return true; | ||||||||
| 5343 | case Intrinsic::ctpop: | ||||||||
| 5344 | return true; | ||||||||
| 5345 | } | ||||||||
| 5346 | } | ||||||||
| 5347 | return false; | ||||||||
| 5348 | case Instruction::ICmp: | ||||||||
| 5349 | case Instruction::FCmp: | ||||||||
| 5350 | case Instruction::GetElementPtr: | ||||||||
| 5351 | return true; | ||||||||
| 5352 | default: | ||||||||
| 5353 | if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I)) | ||||||||
| 5354 | return true; | ||||||||
| 5355 | |||||||||
| 5356 | // Be conservative and return false. | ||||||||
| 5357 | return false; | ||||||||
| 5358 | } | ||||||||
| 5359 | } | ||||||||
| 5360 | |||||||||
| 5361 | void llvm::getGuaranteedWellDefinedOps( | ||||||||
| 5362 | const Instruction *I, SmallPtrSetImpl<const Value *> &Operands) { | ||||||||
| 5363 | switch (I->getOpcode()) { | ||||||||
| 5364 | case Instruction::Store: | ||||||||
| 5365 | Operands.insert(cast<StoreInst>(I)->getPointerOperand()); | ||||||||
| 5366 | break; | ||||||||
| 5367 | |||||||||
| 5368 | case Instruction::Load: | ||||||||
| 5369 | Operands.insert(cast<LoadInst>(I)->getPointerOperand()); | ||||||||
| 5370 | break; | ||||||||
| 5371 | |||||||||
| 5372 | // Since dereferenceable attribute imply noundef, atomic operations | ||||||||
| 5373 | // also implicitly have noundef pointers too | ||||||||
| 5374 | case Instruction::AtomicCmpXchg: | ||||||||
| 5375 | Operands.insert(cast<AtomicCmpXchgInst>(I)->getPointerOperand()); | ||||||||
| 5376 | break; | ||||||||
| 5377 | |||||||||
| 5378 | case Instruction::AtomicRMW: | ||||||||
| 5379 | Operands.insert(cast<AtomicRMWInst>(I)->getPointerOperand()); | ||||||||
| 5380 | break; | ||||||||
| 5381 | |||||||||
| 5382 | case Instruction::Call: | ||||||||
| 5383 | case Instruction::Invoke: { | ||||||||
| 5384 | const CallBase *CB = cast<CallBase>(I); | ||||||||
| 5385 | if (CB->isIndirectCall()) | ||||||||
| 5386 | Operands.insert(CB->getCalledOperand()); | ||||||||
| 5387 | for (unsigned i = 0; i < CB->arg_size(); ++i) { | ||||||||
| 5388 | if (CB->paramHasAttr(i, Attribute::NoUndef) || | ||||||||
| 5389 | CB->paramHasAttr(i, Attribute::Dereferenceable)) | ||||||||
| 5390 | Operands.insert(CB->getArgOperand(i)); | ||||||||
| 5391 | } | ||||||||
| 5392 | break; | ||||||||
| 5393 | } | ||||||||
| 5394 | |||||||||
| 5395 | default: | ||||||||
| 5396 | break; | ||||||||
| 5397 | } | ||||||||
| 5398 | } | ||||||||
| 5399 | |||||||||
| 5400 | void llvm::getGuaranteedNonPoisonOps(const Instruction *I, | ||||||||
| 5401 | SmallPtrSetImpl<const Value *> &Operands) { | ||||||||
| 5402 | getGuaranteedWellDefinedOps(I, Operands); | ||||||||
| 5403 | switch (I->getOpcode()) { | ||||||||
| 5404 | // Divisors of these operations are allowed to be partially undef. | ||||||||
| 5405 | case Instruction::UDiv: | ||||||||
| 5406 | case Instruction::SDiv: | ||||||||
| 5407 | case Instruction::URem: | ||||||||
| 5408 | case Instruction::SRem: | ||||||||
| 5409 | Operands.insert(I->getOperand(1)); | ||||||||
| 5410 | break; | ||||||||
| 5411 | |||||||||
| 5412 | default: | ||||||||
| 5413 | break; | ||||||||
| 5414 | } | ||||||||
| 5415 | } | ||||||||
| 5416 | |||||||||
| 5417 | bool llvm::mustTriggerUB(const Instruction *I, | ||||||||
| 5418 | const SmallSet<const Value *, 16>& KnownPoison) { | ||||||||
| 5419 | SmallPtrSet<const Value *, 4> NonPoisonOps; | ||||||||
| 5420 | getGuaranteedNonPoisonOps(I, NonPoisonOps); | ||||||||
| 5421 | |||||||||
| 5422 | for (const auto *V : NonPoisonOps) | ||||||||
| 5423 | if (KnownPoison.count(V)) | ||||||||
| 5424 | return true; | ||||||||
| 5425 | |||||||||
| 5426 | return false; | ||||||||
| 5427 | } | ||||||||
| 5428 | |||||||||
| 5429 | static bool programUndefinedIfUndefOrPoison(const Value *V, | ||||||||
| 5430 | bool PoisonOnly) { | ||||||||
| 5431 | // We currently only look for uses of values within the same basic | ||||||||
| 5432 | // block, as that makes it easier to guarantee that the uses will be | ||||||||
| 5433 | // executed given that Inst is executed. | ||||||||
| 5434 | // | ||||||||
| 5435 | // FIXME: Expand this to consider uses beyond the same basic block. To do | ||||||||
| 5436 | // this, look out for the distinction between post-dominance and strong | ||||||||
| 5437 | // post-dominance. | ||||||||
| 5438 | const BasicBlock *BB = nullptr; | ||||||||
| 5439 | BasicBlock::const_iterator Begin; | ||||||||
| 5440 | if (const auto *Inst = dyn_cast<Instruction>(V)) { | ||||||||
| 5441 | BB = Inst->getParent(); | ||||||||
| 5442 | Begin = Inst->getIterator(); | ||||||||
| 5443 | Begin++; | ||||||||
| 5444 | } else if (const auto *Arg = dyn_cast<Argument>(V)) { | ||||||||
| 5445 | BB = &Arg->getParent()->getEntryBlock(); | ||||||||
| 5446 | Begin = BB->begin(); | ||||||||
| 5447 | } else { | ||||||||
| 5448 | return false; | ||||||||
| 5449 | } | ||||||||
| 5450 | |||||||||
| 5451 | // Limit number of instructions we look at, to avoid scanning through large | ||||||||
| 5452 | // blocks. The current limit is chosen arbitrarily. | ||||||||
| 5453 | unsigned ScanLimit = 32; | ||||||||
| 5454 | BasicBlock::const_iterator End = BB->end(); | ||||||||
| 5455 | |||||||||
| 5456 | if (!PoisonOnly) { | ||||||||
| 5457 | // Since undef does not propagate eagerly, be conservative & just check | ||||||||
| 5458 | // whether a value is directly passed to an instruction that must take | ||||||||
| 5459 | // well-defined operands. | ||||||||
| 5460 | |||||||||
| 5461 | for (auto &I : make_range(Begin, End)) { | ||||||||
| 5462 | if (isa<DbgInfoIntrinsic>(I)) | ||||||||
| 5463 | continue; | ||||||||
| 5464 | if (--ScanLimit == 0) | ||||||||
| 5465 | break; | ||||||||
| 5466 | |||||||||
| 5467 | SmallPtrSet<const Value *, 4> WellDefinedOps; | ||||||||
| 5468 | getGuaranteedWellDefinedOps(&I, WellDefinedOps); | ||||||||
| 5469 | if (WellDefinedOps.contains(V)) | ||||||||
| 5470 | return true; | ||||||||
| 5471 | |||||||||
| 5472 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) | ||||||||
| 5473 | break; | ||||||||
| 5474 | } | ||||||||
| 5475 | return false; | ||||||||
| 5476 | } | ||||||||
| 5477 | |||||||||
| 5478 | // Set of instructions that we have proved will yield poison if Inst | ||||||||
| 5479 | // does. | ||||||||
| 5480 | SmallSet<const Value *, 16> YieldsPoison; | ||||||||
| 5481 | SmallSet<const BasicBlock *, 4> Visited; | ||||||||
| 5482 | |||||||||
| 5483 | YieldsPoison.insert(V); | ||||||||
| 5484 | auto Propagate = [&](const User *User) { | ||||||||
| 5485 | if (propagatesPoison(cast<Operator>(User))) | ||||||||
| 5486 | YieldsPoison.insert(User); | ||||||||
| 5487 | }; | ||||||||
| 5488 | for_each(V->users(), Propagate); | ||||||||
| 5489 | Visited.insert(BB); | ||||||||
| 5490 | |||||||||
| 5491 | while (true) { | ||||||||
| 5492 | for (auto &I : make_range(Begin, End)) { | ||||||||
| 5493 | if (isa<DbgInfoIntrinsic>(I)) | ||||||||
| 5494 | continue; | ||||||||
| 5495 | if (--ScanLimit == 0) | ||||||||
| 5496 | return false; | ||||||||
| 5497 | if (mustTriggerUB(&I, YieldsPoison)) | ||||||||
| 5498 | return true; | ||||||||
| 5499 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) | ||||||||
| 5500 | return false; | ||||||||
| 5501 | |||||||||
| 5502 | // Mark poison that propagates from I through uses of I. | ||||||||
| 5503 | if (YieldsPoison.count(&I)) | ||||||||
| 5504 | for_each(I.users(), Propagate); | ||||||||
| 5505 | } | ||||||||
| 5506 | |||||||||
| 5507 | BB = BB->getSingleSuccessor(); | ||||||||
| 5508 | if (!BB || !Visited.insert(BB).second) | ||||||||
| 5509 | break; | ||||||||
| 5510 | |||||||||
| 5511 | Begin = BB->getFirstNonPHI()->getIterator(); | ||||||||
| 5512 | End = BB->end(); | ||||||||
| 5513 | } | ||||||||
| 5514 | return false; | ||||||||
| 5515 | } | ||||||||
| 5516 | |||||||||
| 5517 | bool llvm::programUndefinedIfUndefOrPoison(const Instruction *Inst) { | ||||||||
| 5518 | return ::programUndefinedIfUndefOrPoison(Inst, false); | ||||||||
| 5519 | } | ||||||||
| 5520 | |||||||||
| 5521 | bool llvm::programUndefinedIfPoison(const Instruction *Inst) { | ||||||||
| 5522 | return ::programUndefinedIfUndefOrPoison(Inst, true); | ||||||||
| 5523 | } | ||||||||
| 5524 | |||||||||
| 5525 | static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { | ||||||||
| 5526 | if (FMF.noNaNs()) | ||||||||
| 5527 | return true; | ||||||||
| 5528 | |||||||||
| 5529 | if (auto *C = dyn_cast<ConstantFP>(V)) | ||||||||
| 5530 | return !C->isNaN(); | ||||||||
| 5531 | |||||||||
| 5532 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { | ||||||||
| 5533 | if (!C->getElementType()->isFloatingPointTy()) | ||||||||
| 5534 | return false; | ||||||||
| 5535 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { | ||||||||
| 5536 | if (C->getElementAsAPFloat(I).isNaN()) | ||||||||
| 5537 | return false; | ||||||||
| 5538 | } | ||||||||
| 5539 | return true; | ||||||||
| 5540 | } | ||||||||
| 5541 | |||||||||
| 5542 | if (isa<ConstantAggregateZero>(V)) | ||||||||
| 5543 | return true; | ||||||||
| 5544 | |||||||||
| 5545 | return false; | ||||||||
| 5546 | } | ||||||||
| 5547 | |||||||||
| 5548 | static bool isKnownNonZero(const Value *V) { | ||||||||
| 5549 | if (auto *C = dyn_cast<ConstantFP>(V)) | ||||||||
| 5550 | return !C->isZero(); | ||||||||
| 5551 | |||||||||
| 5552 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { | ||||||||
| 5553 | if (!C->getElementType()->isFloatingPointTy()) | ||||||||
| 5554 | return false; | ||||||||
| 5555 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { | ||||||||
| 5556 | if (C->getElementAsAPFloat(I).isZero()) | ||||||||
| 5557 | return false; | ||||||||
| 5558 | } | ||||||||
| 5559 | return true; | ||||||||
| 5560 | } | ||||||||
| 5561 | |||||||||
| 5562 | return false; | ||||||||
| 5563 | } | ||||||||
| 5564 | |||||||||
| 5565 | /// Match clamp pattern for float types without care about NaNs or signed zeros. | ||||||||
| 5566 | /// Given non-min/max outer cmp/select from the clamp pattern this | ||||||||
| 5567 | /// function recognizes if it can be substitued by a "canonical" min/max | ||||||||
| 5568 | /// pattern. | ||||||||
| 5569 | static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, | ||||||||
| 5570 | Value *CmpLHS, Value *CmpRHS, | ||||||||
| 5571 | Value *TrueVal, Value *FalseVal, | ||||||||
| 5572 | Value *&LHS, Value *&RHS) { | ||||||||
| 5573 | // Try to match | ||||||||
| 5574 | // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) | ||||||||
| 5575 | // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) | ||||||||
| 5576 | // and return description of the outer Max/Min. | ||||||||
| 5577 | |||||||||
| 5578 | // First, check if select has inverse order: | ||||||||
| 5579 | if (CmpRHS == FalseVal) { | ||||||||
| 5580 | std::swap(TrueVal, FalseVal); | ||||||||
| 5581 | Pred = CmpInst::getInversePredicate(Pred); | ||||||||
| 5582 | } | ||||||||
| 5583 | |||||||||
| 5584 | // Assume success now. If there's no match, callers should not use these anyway. | ||||||||
| 5585 | LHS = TrueVal; | ||||||||
| 5586 | RHS = FalseVal; | ||||||||
| 5587 | |||||||||
| 5588 | const APFloat *FC1; | ||||||||
| 5589 | if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) | ||||||||
| 5590 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5591 | |||||||||
| 5592 | const APFloat *FC2; | ||||||||
| 5593 | switch (Pred) { | ||||||||
| 5594 | case CmpInst::FCMP_OLT: | ||||||||
| 5595 | case CmpInst::FCMP_OLE: | ||||||||
| 5596 | case CmpInst::FCMP_ULT: | ||||||||
| 5597 | case CmpInst::FCMP_ULE: | ||||||||
| 5598 | if (match(FalseVal, | ||||||||
| 5599 | m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), | ||||||||
| 5600 | m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && | ||||||||
| 5601 | *FC1 < *FC2) | ||||||||
| 5602 | return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; | ||||||||
| 5603 | break; | ||||||||
| 5604 | case CmpInst::FCMP_OGT: | ||||||||
| 5605 | case CmpInst::FCMP_OGE: | ||||||||
| 5606 | case CmpInst::FCMP_UGT: | ||||||||
| 5607 | case CmpInst::FCMP_UGE: | ||||||||
| 5608 | if (match(FalseVal, | ||||||||
| 5609 | m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), | ||||||||
| 5610 | m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && | ||||||||
| 5611 | *FC1 > *FC2) | ||||||||
| 5612 | return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; | ||||||||
| 5613 | break; | ||||||||
| 5614 | default: | ||||||||
| 5615 | break; | ||||||||
| 5616 | } | ||||||||
| 5617 | |||||||||
| 5618 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5619 | } | ||||||||
| 5620 | |||||||||
| 5621 | /// Recognize variations of: | ||||||||
| 5622 | /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) | ||||||||
| 5623 | static SelectPatternResult matchClamp(CmpInst::Predicate Pred, | ||||||||
| 5624 | Value *CmpLHS, Value *CmpRHS, | ||||||||
| 5625 | Value *TrueVal, Value *FalseVal) { | ||||||||
| 5626 | // Swap the select operands and predicate to match the patterns below. | ||||||||
| 5627 | if (CmpRHS != TrueVal) { | ||||||||
| 5628 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||
| 5629 | std::swap(TrueVal, FalseVal); | ||||||||
| 5630 | } | ||||||||
| 5631 | const APInt *C1; | ||||||||
| 5632 | if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { | ||||||||
| 5633 | const APInt *C2; | ||||||||
| 5634 | // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) | ||||||||
| 5635 | if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && | ||||||||
| 5636 | C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) | ||||||||
| 5637 | return {SPF_SMAX, SPNB_NA, false}; | ||||||||
| 5638 | |||||||||
| 5639 | // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) | ||||||||
| 5640 | if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && | ||||||||
| 5641 | C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) | ||||||||
| 5642 | return {SPF_SMIN, SPNB_NA, false}; | ||||||||
| 5643 | |||||||||
| 5644 | // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) | ||||||||
| 5645 | if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && | ||||||||
| 5646 | C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) | ||||||||
| 5647 | return {SPF_UMAX, SPNB_NA, false}; | ||||||||
| 5648 | |||||||||
| 5649 | // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) | ||||||||
| 5650 | if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && | ||||||||
| 5651 | C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) | ||||||||
| 5652 | return {SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5653 | } | ||||||||
| 5654 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5655 | } | ||||||||
| 5656 | |||||||||
| 5657 | /// Recognize variations of: | ||||||||
| 5658 | /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) | ||||||||
| 5659 | static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, | ||||||||
| 5660 | Value *CmpLHS, Value *CmpRHS, | ||||||||
| 5661 | Value *TVal, Value *FVal, | ||||||||
| 5662 | unsigned Depth) { | ||||||||
| 5663 | // TODO: Allow FP min/max with nnan/nsz. | ||||||||
| 5664 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison")((void)0); | ||||||||
| 5665 | |||||||||
| 5666 | Value *A = nullptr, *B = nullptr; | ||||||||
| 5667 | SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); | ||||||||
| 5668 | if (!SelectPatternResult::isMinOrMax(L.Flavor)) | ||||||||
| 5669 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5670 | |||||||||
| 5671 | Value *C = nullptr, *D = nullptr; | ||||||||
| 5672 | SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); | ||||||||
| 5673 | if (L.Flavor != R.Flavor) | ||||||||
| 5674 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5675 | |||||||||
| 5676 | // We have something like: x Pred y ? min(a, b) : min(c, d). | ||||||||
| 5677 | // Try to match the compare to the min/max operations of the select operands. | ||||||||
| 5678 | // First, make sure we have the right compare predicate. | ||||||||
| 5679 | switch (L.Flavor) { | ||||||||
| 5680 | case SPF_SMIN: | ||||||||
| 5681 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { | ||||||||
| 5682 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||
| 5683 | std::swap(CmpLHS, CmpRHS); | ||||||||
| 5684 | } | ||||||||
| 5685 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) | ||||||||
| 5686 | break; | ||||||||
| 5687 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5688 | case SPF_SMAX: | ||||||||
| 5689 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { | ||||||||
| 5690 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||
| 5691 | std::swap(CmpLHS, CmpRHS); | ||||||||
| 5692 | } | ||||||||
| 5693 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) | ||||||||
| 5694 | break; | ||||||||
| 5695 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5696 | case SPF_UMIN: | ||||||||
| 5697 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { | ||||||||
| 5698 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||
| 5699 | std::swap(CmpLHS, CmpRHS); | ||||||||
| 5700 | } | ||||||||
| 5701 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) | ||||||||
| 5702 | break; | ||||||||
| 5703 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5704 | case SPF_UMAX: | ||||||||
| 5705 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { | ||||||||
| 5706 | Pred = ICmpInst::getSwappedPredicate(Pred); | ||||||||
| 5707 | std::swap(CmpLHS, CmpRHS); | ||||||||
| 5708 | } | ||||||||
| 5709 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) | ||||||||
| 5710 | break; | ||||||||
| 5711 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5712 | default: | ||||||||
| 5713 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5714 | } | ||||||||
| 5715 | |||||||||
| 5716 | // If there is a common operand in the already matched min/max and the other | ||||||||
| 5717 | // min/max operands match the compare operands (either directly or inverted), | ||||||||
| 5718 | // then this is min/max of the same flavor. | ||||||||
| 5719 | |||||||||
| 5720 | // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) | ||||||||
| 5721 | // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) | ||||||||
| 5722 | if (D == B) { | ||||||||
| 5723 | if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && | ||||||||
| 5724 | match(A, m_Not(m_Specific(CmpRHS))))) | ||||||||
| 5725 | return {L.Flavor, SPNB_NA, false}; | ||||||||
| 5726 | } | ||||||||
| 5727 | // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) | ||||||||
| 5728 | // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) | ||||||||
| 5729 | if (C == B) { | ||||||||
| 5730 | if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && | ||||||||
| 5731 | match(A, m_Not(m_Specific(CmpRHS))))) | ||||||||
| 5732 | return {L.Flavor, SPNB_NA, false}; | ||||||||
| 5733 | } | ||||||||
| 5734 | // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) | ||||||||
| 5735 | // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) | ||||||||
| 5736 | if (D == A) { | ||||||||
| 5737 | if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && | ||||||||
| 5738 | match(B, m_Not(m_Specific(CmpRHS))))) | ||||||||
| 5739 | return {L.Flavor, SPNB_NA, false}; | ||||||||
| 5740 | } | ||||||||
| 5741 | // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) | ||||||||
| 5742 | // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) | ||||||||
| 5743 | if (C == A) { | ||||||||
| 5744 | if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && | ||||||||
| 5745 | match(B, m_Not(m_Specific(CmpRHS))))) | ||||||||
| 5746 | return {L.Flavor, SPNB_NA, false}; | ||||||||
| 5747 | } | ||||||||
| 5748 | |||||||||
| 5749 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5750 | } | ||||||||
| 5751 | |||||||||
| 5752 | /// If the input value is the result of a 'not' op, constant integer, or vector | ||||||||
| 5753 | /// splat of a constant integer, return the bitwise-not source value. | ||||||||
| 5754 | /// TODO: This could be extended to handle non-splat vector integer constants. | ||||||||
| 5755 | static Value *getNotValue(Value *V) { | ||||||||
| 5756 | Value *NotV; | ||||||||
| 5757 | if (match(V, m_Not(m_Value(NotV)))) | ||||||||
| 5758 | return NotV; | ||||||||
| 5759 | |||||||||
| 5760 | const APInt *C; | ||||||||
| 5761 | if (match(V, m_APInt(C))) | ||||||||
| 5762 | return ConstantInt::get(V->getType(), ~(*C)); | ||||||||
| 5763 | |||||||||
| 5764 | return nullptr; | ||||||||
| 5765 | } | ||||||||
| 5766 | |||||||||
| 5767 | /// Match non-obvious integer minimum and maximum sequences. | ||||||||
| 5768 | static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, | ||||||||
| 5769 | Value *CmpLHS, Value *CmpRHS, | ||||||||
| 5770 | Value *TrueVal, Value *FalseVal, | ||||||||
| 5771 | Value *&LHS, Value *&RHS, | ||||||||
| 5772 | unsigned Depth) { | ||||||||
| 5773 | // Assume success. If there's no match, callers should not use these anyway. | ||||||||
| 5774 | LHS = TrueVal; | ||||||||
| 5775 | RHS = FalseVal; | ||||||||
| 5776 | |||||||||
| 5777 | SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); | ||||||||
| 5778 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) | ||||||||
| 5779 | return SPR; | ||||||||
| 5780 | |||||||||
| 5781 | SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); | ||||||||
| 5782 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) | ||||||||
| 5783 | return SPR; | ||||||||
| 5784 | |||||||||
| 5785 | // Look through 'not' ops to find disguised min/max. | ||||||||
| 5786 | // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y) | ||||||||
| 5787 | // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y) | ||||||||
| 5788 | if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) { | ||||||||
| 5789 | switch (Pred) { | ||||||||
| 5790 | case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false}; | ||||||||
| 5791 | case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false}; | ||||||||
| 5792 | case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5793 | case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false}; | ||||||||
| 5794 | default: break; | ||||||||
| 5795 | } | ||||||||
| 5796 | } | ||||||||
| 5797 | |||||||||
| 5798 | // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X) | ||||||||
| 5799 | // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X) | ||||||||
| 5800 | if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) { | ||||||||
| 5801 | switch (Pred) { | ||||||||
| 5802 | case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false}; | ||||||||
| 5803 | case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false}; | ||||||||
| 5804 | case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false}; | ||||||||
| 5805 | case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5806 | default: break; | ||||||||
| 5807 | } | ||||||||
| 5808 | } | ||||||||
| 5809 | |||||||||
| 5810 | if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) | ||||||||
| 5811 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5812 | |||||||||
| 5813 | // Z = X -nsw Y | ||||||||
| 5814 | // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0) | ||||||||
| 5815 | // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0) | ||||||||
| 5816 | if (match(TrueVal, m_Zero()) && | ||||||||
| 5817 | match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) | ||||||||
| 5818 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; | ||||||||
| 5819 | |||||||||
| 5820 | // Z = X -nsw Y | ||||||||
| 5821 | // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0) | ||||||||
| 5822 | // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0) | ||||||||
| 5823 | if (match(FalseVal, m_Zero()) && | ||||||||
| 5824 | match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) | ||||||||
| 5825 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; | ||||||||
| 5826 | |||||||||
| 5827 | const APInt *C1; | ||||||||
| 5828 | if (!match(CmpRHS, m_APInt(C1))) | ||||||||
| 5829 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5830 | |||||||||
| 5831 | // An unsigned min/max can be written with a signed compare. | ||||||||
| 5832 | const APInt *C2; | ||||||||
| 5833 | if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || | ||||||||
| 5834 | (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { | ||||||||
| 5835 | // Is the sign bit set? | ||||||||
| 5836 | // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX | ||||||||
| 5837 | // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN | ||||||||
| 5838 | if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() && | ||||||||
| 5839 | C2->isMaxSignedValue()) | ||||||||
| 5840 | return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5841 | |||||||||
| 5842 | // Is the sign bit clear? | ||||||||
| 5843 | // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX | ||||||||
| 5844 | // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN | ||||||||
| 5845 | if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() && | ||||||||
| 5846 | C2->isMinSignedValue()) | ||||||||
| 5847 | return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5848 | } | ||||||||
| 5849 | |||||||||
| 5850 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5851 | } | ||||||||
| 5852 | |||||||||
| 5853 | bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { | ||||||||
| 5854 | assert(X && Y && "Invalid operand")((void)0); | ||||||||
| 5855 | |||||||||
| 5856 | // X = sub (0, Y) || X = sub nsw (0, Y) | ||||||||
| 5857 | if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || | ||||||||
| 5858 | (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) | ||||||||
| 5859 | return true; | ||||||||
| 5860 | |||||||||
| 5861 | // Y = sub (0, X) || Y = sub nsw (0, X) | ||||||||
| 5862 | if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || | ||||||||
| 5863 | (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) | ||||||||
| 5864 | return true; | ||||||||
| 5865 | |||||||||
| 5866 | // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) | ||||||||
| 5867 | Value *A, *B; | ||||||||
| 5868 | return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && | ||||||||
| 5869 | match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || | ||||||||
| 5870 | (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && | ||||||||
| 5871 | match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); | ||||||||
| 5872 | } | ||||||||
| 5873 | |||||||||
| 5874 | static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, | ||||||||
| 5875 | FastMathFlags FMF, | ||||||||
| 5876 | Value *CmpLHS, Value *CmpRHS, | ||||||||
| 5877 | Value *TrueVal, Value *FalseVal, | ||||||||
| 5878 | Value *&LHS, Value *&RHS, | ||||||||
| 5879 | unsigned Depth) { | ||||||||
| 5880 | if (CmpInst::isFPPredicate(Pred)) { | ||||||||
| 5881 | // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one | ||||||||
| 5882 | // 0.0 operand, set the compare's 0.0 operands to that same value for the | ||||||||
| 5883 | // purpose of identifying min/max. Disregard vector constants with undefined | ||||||||
| 5884 | // elements because those can not be back-propagated for analysis. | ||||||||
| 5885 | Value *OutputZeroVal = nullptr; | ||||||||
| 5886 | if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && | ||||||||
| 5887 | !cast<Constant>(TrueVal)->containsUndefOrPoisonElement()) | ||||||||
| 5888 | OutputZeroVal = TrueVal; | ||||||||
| 5889 | else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && | ||||||||
| 5890 | !cast<Constant>(FalseVal)->containsUndefOrPoisonElement()) | ||||||||
| 5891 | OutputZeroVal = FalseVal; | ||||||||
| 5892 | |||||||||
| 5893 | if (OutputZeroVal) { | ||||||||
| 5894 | if (match(CmpLHS, m_AnyZeroFP())) | ||||||||
| 5895 | CmpLHS = OutputZeroVal; | ||||||||
| 5896 | if (match(CmpRHS, m_AnyZeroFP())) | ||||||||
| 5897 | CmpRHS = OutputZeroVal; | ||||||||
| 5898 | } | ||||||||
| 5899 | } | ||||||||
| 5900 | |||||||||
| 5901 | LHS = CmpLHS; | ||||||||
| 5902 | RHS = CmpRHS; | ||||||||
| 5903 | |||||||||
| 5904 | // Signed zero may return inconsistent results between implementations. | ||||||||
| 5905 | // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 | ||||||||
| 5906 | // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) | ||||||||
| 5907 | // Therefore, we behave conservatively and only proceed if at least one of the | ||||||||
| 5908 | // operands is known to not be zero or if we don't care about signed zero. | ||||||||
| 5909 | switch (Pred) { | ||||||||
| 5910 | default: break; | ||||||||
| 5911 | // FIXME: Include OGT/OLT/UGT/ULT. | ||||||||
| 5912 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: | ||||||||
| 5913 | case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: | ||||||||
| 5914 | if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && | ||||||||
| 5915 | !isKnownNonZero(CmpRHS)) | ||||||||
| 5916 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5917 | } | ||||||||
| 5918 | |||||||||
| 5919 | SelectPatternNaNBehavior NaNBehavior = SPNB_NA; | ||||||||
| 5920 | bool Ordered = false; | ||||||||
| 5921 | |||||||||
| 5922 | // When given one NaN and one non-NaN input: | ||||||||
| 5923 | // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. | ||||||||
| 5924 | // - A simple C99 (a < b ? a : b) construction will return 'b' (as the | ||||||||
| 5925 | // ordered comparison fails), which could be NaN or non-NaN. | ||||||||
| 5926 | // so here we discover exactly what NaN behavior is required/accepted. | ||||||||
| 5927 | if (CmpInst::isFPPredicate(Pred)) { | ||||||||
| 5928 | bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); | ||||||||
| 5929 | bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); | ||||||||
| 5930 | |||||||||
| 5931 | if (LHSSafe && RHSSafe) { | ||||||||
| 5932 | // Both operands are known non-NaN. | ||||||||
| 5933 | NaNBehavior = SPNB_RETURNS_ANY; | ||||||||
| 5934 | } else if (CmpInst::isOrdered(Pred)) { | ||||||||
| 5935 | // An ordered comparison will return false when given a NaN, so it | ||||||||
| 5936 | // returns the RHS. | ||||||||
| 5937 | Ordered = true; | ||||||||
| 5938 | if (LHSSafe) | ||||||||
| 5939 | // LHS is non-NaN, so if RHS is NaN then NaN will be returned. | ||||||||
| 5940 | NaNBehavior = SPNB_RETURNS_NAN; | ||||||||
| 5941 | else if (RHSSafe) | ||||||||
| 5942 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||||||
| 5943 | else | ||||||||
| 5944 | // Completely unsafe. | ||||||||
| 5945 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5946 | } else { | ||||||||
| 5947 | Ordered = false; | ||||||||
| 5948 | // An unordered comparison will return true when given a NaN, so it | ||||||||
| 5949 | // returns the LHS. | ||||||||
| 5950 | if (LHSSafe) | ||||||||
| 5951 | // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. | ||||||||
| 5952 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||||||
| 5953 | else if (RHSSafe) | ||||||||
| 5954 | NaNBehavior = SPNB_RETURNS_NAN; | ||||||||
| 5955 | else | ||||||||
| 5956 | // Completely unsafe. | ||||||||
| 5957 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 5958 | } | ||||||||
| 5959 | } | ||||||||
| 5960 | |||||||||
| 5961 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { | ||||||||
| 5962 | std::swap(CmpLHS, CmpRHS); | ||||||||
| 5963 | Pred = CmpInst::getSwappedPredicate(Pred); | ||||||||
| 5964 | if (NaNBehavior == SPNB_RETURNS_NAN) | ||||||||
| 5965 | NaNBehavior = SPNB_RETURNS_OTHER; | ||||||||
| 5966 | else if (NaNBehavior == SPNB_RETURNS_OTHER) | ||||||||
| 5967 | NaNBehavior = SPNB_RETURNS_NAN; | ||||||||
| 5968 | Ordered = !Ordered; | ||||||||
| 5969 | } | ||||||||
| 5970 | |||||||||
| 5971 | // ([if]cmp X, Y) ? X : Y | ||||||||
| 5972 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { | ||||||||
| 5973 | switch (Pred) { | ||||||||
| 5974 | default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. | ||||||||
| 5975 | case ICmpInst::ICMP_UGT: | ||||||||
| 5976 | case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; | ||||||||
| 5977 | case ICmpInst::ICMP_SGT: | ||||||||
| 5978 | case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; | ||||||||
| 5979 | case ICmpInst::ICMP_ULT: | ||||||||
| 5980 | case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; | ||||||||
| 5981 | case ICmpInst::ICMP_SLT: | ||||||||
| 5982 | case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; | ||||||||
| 5983 | case FCmpInst::FCMP_UGT: | ||||||||
| 5984 | case FCmpInst::FCMP_UGE: | ||||||||
| 5985 | case FCmpInst::FCMP_OGT: | ||||||||
| 5986 | case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; | ||||||||
| 5987 | case FCmpInst::FCMP_ULT: | ||||||||
| 5988 | case FCmpInst::FCMP_ULE: | ||||||||
| 5989 | case FCmpInst::FCMP_OLT: | ||||||||
| 5990 | case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; | ||||||||
| 5991 | } | ||||||||
| 5992 | } | ||||||||
| 5993 | |||||||||
| 5994 | if (isKnownNegation(TrueVal, FalseVal)) { | ||||||||
| 5995 | // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can | ||||||||
| 5996 | // match against either LHS or sext(LHS). | ||||||||
| 5997 | auto MaybeSExtCmpLHS = | ||||||||
| 5998 | m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); | ||||||||
| 5999 | auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); | ||||||||
| 6000 | auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); | ||||||||
| 6001 | if (match(TrueVal, MaybeSExtCmpLHS)) { | ||||||||
| 6002 | // Set the return values. If the compare uses the negated value (-X >s 0), | ||||||||
| 6003 | // swap the return values because the negated value is always 'RHS'. | ||||||||
| 6004 | LHS = TrueVal; | ||||||||
| 6005 | RHS = FalseVal; | ||||||||
| 6006 | if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) | ||||||||
| 6007 | std::swap(LHS, RHS); | ||||||||
| 6008 | |||||||||
| 6009 | // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) | ||||||||
| 6010 | // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) | ||||||||
| 6011 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) | ||||||||
| 6012 | return {SPF_ABS, SPNB_NA, false}; | ||||||||
| 6013 | |||||||||
| 6014 | // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) | ||||||||
| 6015 | if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne)) | ||||||||
| 6016 | return {SPF_ABS, SPNB_NA, false}; | ||||||||
| 6017 | |||||||||
| 6018 | // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) | ||||||||
| 6019 | // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) | ||||||||
| 6020 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) | ||||||||
| 6021 | return {SPF_NABS, SPNB_NA, false}; | ||||||||
| 6022 | } | ||||||||
| 6023 | else if (match(FalseVal, MaybeSExtCmpLHS)) { | ||||||||
| 6024 | // Set the return values. If the compare uses the negated value (-X >s 0), | ||||||||
| 6025 | // swap the return values because the negated value is always 'RHS'. | ||||||||
| 6026 | LHS = FalseVal; | ||||||||
| 6027 | RHS = TrueVal; | ||||||||
| 6028 | if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) | ||||||||
| 6029 | std::swap(LHS, RHS); | ||||||||
| 6030 | |||||||||
| 6031 | // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) | ||||||||
| 6032 | // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) | ||||||||
| 6033 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) | ||||||||
| 6034 | return {SPF_NABS, SPNB_NA, false}; | ||||||||
| 6035 | |||||||||
| 6036 | // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) | ||||||||
| 6037 | // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) | ||||||||
| 6038 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) | ||||||||
| 6039 | return {SPF_ABS, SPNB_NA, false}; | ||||||||
| 6040 | } | ||||||||
| 6041 | } | ||||||||
| 6042 | |||||||||
| 6043 | if (CmpInst::isIntPredicate(Pred)) | ||||||||
| 6044 | return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); | ||||||||
| 6045 | |||||||||
| 6046 | // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar | ||||||||
| 6047 | // may return either -0.0 or 0.0, so fcmp/select pair has stricter | ||||||||
| 6048 | // semantics than minNum. Be conservative in such case. | ||||||||
| 6049 | if (NaNBehavior != SPNB_RETURNS_ANY || | ||||||||
| 6050 | (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && | ||||||||
| 6051 | !isKnownNonZero(CmpRHS))) | ||||||||
| 6052 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 6053 | |||||||||
| 6054 | return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); | ||||||||
| 6055 | } | ||||||||
| 6056 | |||||||||
| 6057 | /// Helps to match a select pattern in case of a type mismatch. | ||||||||
| 6058 | /// | ||||||||
| 6059 | /// The function processes the case when type of true and false values of a | ||||||||
| 6060 | /// select instruction differs from type of the cmp instruction operands because | ||||||||
| 6061 | /// of a cast instruction. The function checks if it is legal to move the cast | ||||||||
| 6062 | /// operation after "select". If yes, it returns the new second value of | ||||||||
| 6063 | /// "select" (with the assumption that cast is moved): | ||||||||
| 6064 | /// 1. As operand of cast instruction when both values of "select" are same cast | ||||||||
| 6065 | /// instructions. | ||||||||
| 6066 | /// 2. As restored constant (by applying reverse cast operation) when the first | ||||||||
| 6067 | /// value of the "select" is a cast operation and the second value is a | ||||||||
| 6068 | /// constant. | ||||||||
| 6069 | /// NOTE: We return only the new second value because the first value could be | ||||||||
| 6070 | /// accessed as operand of cast instruction. | ||||||||
| 6071 | static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, | ||||||||
| 6072 | Instruction::CastOps *CastOp) { | ||||||||
| 6073 | auto *Cast1 = dyn_cast<CastInst>(V1); | ||||||||
| 6074 | if (!Cast1) | ||||||||
| 6075 | return nullptr; | ||||||||
| 6076 | |||||||||
| 6077 | *CastOp = Cast1->getOpcode(); | ||||||||
| 6078 | Type *SrcTy = Cast1->getSrcTy(); | ||||||||
| 6079 | if (auto *Cast2 = dyn_cast<CastInst>(V2)) { | ||||||||
| 6080 | // If V1 and V2 are both the same cast from the same type, look through V1. | ||||||||
| 6081 | if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) | ||||||||
| 6082 | return Cast2->getOperand(0); | ||||||||
| 6083 | return nullptr; | ||||||||
| 6084 | } | ||||||||
| 6085 | |||||||||
| 6086 | auto *C = dyn_cast<Constant>(V2); | ||||||||
| 6087 | if (!C) | ||||||||
| 6088 | return nullptr; | ||||||||
| 6089 | |||||||||
| 6090 | Constant *CastedTo = nullptr; | ||||||||
| 6091 | switch (*CastOp) { | ||||||||
| 6092 | case Instruction::ZExt: | ||||||||
| 6093 | if (CmpI->isUnsigned()) | ||||||||
| 6094 | CastedTo = ConstantExpr::getTrunc(C, SrcTy); | ||||||||
| 6095 | break; | ||||||||
| 6096 | case Instruction::SExt: | ||||||||
| 6097 | if (CmpI->isSigned()) | ||||||||
| 6098 | CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); | ||||||||
| 6099 | break; | ||||||||
| 6100 | case Instruction::Trunc: | ||||||||
| 6101 | Constant *CmpConst; | ||||||||
| 6102 | if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && | ||||||||
| 6103 | CmpConst->getType() == SrcTy) { | ||||||||
| 6104 | // Here we have the following case: | ||||||||
| 6105 | // | ||||||||
| 6106 | // %cond = cmp iN %x, CmpConst | ||||||||
| 6107 | // %tr = trunc iN %x to iK | ||||||||
| 6108 | // %narrowsel = select i1 %cond, iK %t, iK C | ||||||||
| 6109 | // | ||||||||
| 6110 | // We can always move trunc after select operation: | ||||||||
| 6111 | // | ||||||||
| 6112 | // %cond = cmp iN %x, CmpConst | ||||||||
| 6113 | // %widesel = select i1 %cond, iN %x, iN CmpConst | ||||||||
| 6114 | // %tr = trunc iN %widesel to iK | ||||||||
| 6115 | // | ||||||||
| 6116 | // Note that C could be extended in any way because we don't care about | ||||||||
| 6117 | // upper bits after truncation. It can't be abs pattern, because it would | ||||||||
| 6118 | // look like: | ||||||||
| 6119 | // | ||||||||
| 6120 | // select i1 %cond, x, -x. | ||||||||
| 6121 | // | ||||||||
| 6122 | // So only min/max pattern could be matched. Such match requires widened C | ||||||||
| 6123 | // == CmpConst. That is why set widened C = CmpConst, condition trunc | ||||||||
| 6124 | // CmpConst == C is checked below. | ||||||||
| 6125 | CastedTo = CmpConst; | ||||||||
| 6126 | } else { | ||||||||
| 6127 | CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); | ||||||||
| 6128 | } | ||||||||
| 6129 | break; | ||||||||
| 6130 | case Instruction::FPTrunc: | ||||||||
| 6131 | CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); | ||||||||
| 6132 | break; | ||||||||
| 6133 | case Instruction::FPExt: | ||||||||
| 6134 | CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); | ||||||||
| 6135 | break; | ||||||||
| 6136 | case Instruction::FPToUI: | ||||||||
| 6137 | CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); | ||||||||
| 6138 | break; | ||||||||
| 6139 | case Instruction::FPToSI: | ||||||||
| 6140 | CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); | ||||||||
| 6141 | break; | ||||||||
| 6142 | case Instruction::UIToFP: | ||||||||
| 6143 | CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); | ||||||||
| 6144 | break; | ||||||||
| 6145 | case Instruction::SIToFP: | ||||||||
| 6146 | CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); | ||||||||
| 6147 | break; | ||||||||
| 6148 | default: | ||||||||
| 6149 | break; | ||||||||
| 6150 | } | ||||||||
| 6151 | |||||||||
| 6152 | if (!CastedTo) | ||||||||
| 6153 | return nullptr; | ||||||||
| 6154 | |||||||||
| 6155 | // Make sure the cast doesn't lose any information. | ||||||||
| 6156 | Constant *CastedBack = | ||||||||
| 6157 | ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); | ||||||||
| 6158 | if (CastedBack != C) | ||||||||
| 6159 | return nullptr; | ||||||||
| 6160 | |||||||||
| 6161 | return CastedTo; | ||||||||
| 6162 | } | ||||||||
| 6163 | |||||||||
| 6164 | SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, | ||||||||
| 6165 | Instruction::CastOps *CastOp, | ||||||||
| 6166 | unsigned Depth) { | ||||||||
| 6167 | if (Depth >= MaxAnalysisRecursionDepth) | ||||||||
| 6168 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 6169 | |||||||||
| 6170 | SelectInst *SI = dyn_cast<SelectInst>(V); | ||||||||
| 6171 | if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 6172 | |||||||||
| 6173 | CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition()); | ||||||||
| 6174 | if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 6175 | |||||||||
| 6176 | Value *TrueVal = SI->getTrueValue(); | ||||||||
| 6177 | Value *FalseVal = SI->getFalseValue(); | ||||||||
| 6178 | |||||||||
| 6179 | return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS, | ||||||||
| 6180 | CastOp, Depth); | ||||||||
| 6181 | } | ||||||||
| 6182 | |||||||||
| 6183 | SelectPatternResult llvm::matchDecomposedSelectPattern( | ||||||||
| 6184 | CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, | ||||||||
| 6185 | Instruction::CastOps *CastOp, unsigned Depth) { | ||||||||
| 6186 | CmpInst::Predicate Pred = CmpI->getPredicate(); | ||||||||
| 6187 | Value *CmpLHS = CmpI->getOperand(0); | ||||||||
| 6188 | Value *CmpRHS = CmpI->getOperand(1); | ||||||||
| 6189 | FastMathFlags FMF; | ||||||||
| 6190 | if (isa<FPMathOperator>(CmpI)) | ||||||||
| 6191 | FMF = CmpI->getFastMathFlags(); | ||||||||
| 6192 | |||||||||
| 6193 | // Bail out early. | ||||||||
| 6194 | if (CmpI->isEquality()) | ||||||||
| 6195 | return {SPF_UNKNOWN, SPNB_NA, false}; | ||||||||
| 6196 | |||||||||
| 6197 | // Deal with type mismatches. | ||||||||
| 6198 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { | ||||||||
| 6199 | if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { | ||||||||
| 6200 | // If this is a potential fmin/fmax with a cast to integer, then ignore | ||||||||
| 6201 | // -0.0 because there is no corresponding integer value. | ||||||||
| 6202 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) | ||||||||
| 6203 | FMF.setNoSignedZeros(); | ||||||||
| 6204 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, | ||||||||
| 6205 | cast<CastInst>(TrueVal)->getOperand(0), C, | ||||||||
| 6206 | LHS, RHS, Depth); | ||||||||
| 6207 | } | ||||||||
| 6208 | if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { | ||||||||
| 6209 | // If this is a potential fmin/fmax with a cast to integer, then ignore | ||||||||
| 6210 | // -0.0 because there is no corresponding integer value. | ||||||||
| 6211 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) | ||||||||
| 6212 | FMF.setNoSignedZeros(); | ||||||||
| 6213 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, | ||||||||
| 6214 | C, cast<CastInst>(FalseVal)->getOperand(0), | ||||||||
| 6215 | LHS, RHS, Depth); | ||||||||
| 6216 | } | ||||||||
| 6217 | } | ||||||||
| 6218 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, | ||||||||
| 6219 | LHS, RHS, Depth); | ||||||||
| 6220 | } | ||||||||
| 6221 | |||||||||
| 6222 | CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { | ||||||||
| 6223 | if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; | ||||||||
| 6224 | if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; | ||||||||
| 6225 | if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; | ||||||||
| 6226 | if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; | ||||||||
| 6227 | if (SPF == SPF_FMINNUM) | ||||||||
| 6228 | return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; | ||||||||
| 6229 | if (SPF == SPF_FMAXNUM) | ||||||||
| 6230 | return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; | ||||||||
| 6231 | llvm_unreachable("unhandled!")__builtin_unreachable(); | ||||||||
| 6232 | } | ||||||||
| 6233 | |||||||||
| 6234 | SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { | ||||||||
| 6235 | if (SPF == SPF_SMIN) return SPF_SMAX; | ||||||||
| 6236 | if (SPF == SPF_UMIN) return SPF_UMAX; | ||||||||
| 6237 | if (SPF == SPF_SMAX) return SPF_SMIN; | ||||||||
| 6238 | if (SPF == SPF_UMAX) return SPF_UMIN; | ||||||||
| 6239 | llvm_unreachable("unhandled!")__builtin_unreachable(); | ||||||||
| 6240 | } | ||||||||
| 6241 | |||||||||
| 6242 | Intrinsic::ID llvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) { | ||||||||
| 6243 | switch (MinMaxID) { | ||||||||
| 6244 | case Intrinsic::smax: return Intrinsic::smin; | ||||||||
| 6245 | case Intrinsic::smin: return Intrinsic::smax; | ||||||||
| 6246 | case Intrinsic::umax: return Intrinsic::umin; | ||||||||
| 6247 | case Intrinsic::umin: return Intrinsic::umax; | ||||||||
| 6248 | default: llvm_unreachable("Unexpected intrinsic")__builtin_unreachable(); | ||||||||
| 6249 | } | ||||||||
| 6250 | } | ||||||||
| 6251 | |||||||||
| 6252 | CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) { | ||||||||
| 6253 | return getMinMaxPred(getInverseMinMaxFlavor(SPF)); | ||||||||
| 6254 | } | ||||||||
| 6255 | |||||||||
| 6256 | APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) { | ||||||||
| 6257 | switch (SPF) { | ||||||||
| 6258 | case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth); | ||||||||
| 6259 | case SPF_SMIN: return APInt::getSignedMinValue(BitWidth); | ||||||||
| 6260 | case SPF_UMAX: return APInt::getMaxValue(BitWidth); | ||||||||
| 6261 | case SPF_UMIN: return APInt::getMinValue(BitWidth); | ||||||||
| 6262 | default: llvm_unreachable("Unexpected flavor")__builtin_unreachable(); | ||||||||
| 6263 | } | ||||||||
| 6264 | } | ||||||||
| 6265 | |||||||||
| 6266 | std::pair<Intrinsic::ID, bool> | ||||||||
| 6267 | llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) { | ||||||||
| 6268 | // Check if VL contains select instructions that can be folded into a min/max | ||||||||
| 6269 | // vector intrinsic and return the intrinsic if it is possible. | ||||||||
| 6270 | // TODO: Support floating point min/max. | ||||||||
| 6271 | bool AllCmpSingleUse = true; | ||||||||
| 6272 | SelectPatternResult SelectPattern; | ||||||||
| 6273 | SelectPattern.Flavor = SPF_UNKNOWN; | ||||||||
| 6274 | if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) { | ||||||||
| 6275 | Value *LHS, *RHS; | ||||||||
| 6276 | auto CurrentPattern = matchSelectPattern(I, LHS, RHS); | ||||||||
| 6277 | if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) || | ||||||||
| 6278 | CurrentPattern.Flavor == SPF_FMINNUM || | ||||||||
| 6279 | CurrentPattern.Flavor == SPF_FMAXNUM || | ||||||||
| 6280 | !I->getType()->isIntOrIntVectorTy()) | ||||||||
| 6281 | return false; | ||||||||
| 6282 | if (SelectPattern.Flavor != SPF_UNKNOWN && | ||||||||
| 6283 | SelectPattern.Flavor != CurrentPattern.Flavor) | ||||||||
| 6284 | return false; | ||||||||
| 6285 | SelectPattern = CurrentPattern; | ||||||||
| 6286 | AllCmpSingleUse &= | ||||||||
| 6287 | match(I, m_Select(m_OneUse(m_Value()), m_Value(), m_Value())); | ||||||||
| 6288 | return true; | ||||||||
| 6289 | })) { | ||||||||
| 6290 | switch (SelectPattern.Flavor) { | ||||||||
| 6291 | case SPF_SMIN: | ||||||||
| 6292 | return {Intrinsic::smin, AllCmpSingleUse}; | ||||||||
| 6293 | case SPF_UMIN: | ||||||||
| 6294 | return {Intrinsic::umin, AllCmpSingleUse}; | ||||||||
| 6295 | case SPF_SMAX: | ||||||||
| 6296 | return {Intrinsic::smax, AllCmpSingleUse}; | ||||||||
| 6297 | case SPF_UMAX: | ||||||||
| 6298 | return {Intrinsic::umax, AllCmpSingleUse}; | ||||||||
| 6299 | default: | ||||||||
| 6300 | llvm_unreachable("unexpected select pattern flavor")__builtin_unreachable(); | ||||||||
| 6301 | } | ||||||||
| 6302 | } | ||||||||
| 6303 | return {Intrinsic::not_intrinsic, false}; | ||||||||
| 6304 | } | ||||||||
| 6305 | |||||||||
| 6306 | bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, | ||||||||
| 6307 | Value *&Start, Value *&Step) { | ||||||||
| 6308 | // Handle the case of a simple two-predecessor recurrence PHI. | ||||||||
| 6309 | // There's a lot more that could theoretically be done here, but | ||||||||
| 6310 | // this is sufficient to catch some interesting cases. | ||||||||
| 6311 | if (P->getNumIncomingValues() != 2) | ||||||||
| 6312 | return false; | ||||||||
| 6313 | |||||||||
| 6314 | for (unsigned i = 0; i != 2; ++i) { | ||||||||
| 6315 | Value *L = P->getIncomingValue(i); | ||||||||
| 6316 | Value *R = P->getIncomingValue(!i); | ||||||||
| 6317 | Operator *LU = dyn_cast<Operator>(L); | ||||||||
| 6318 | if (!LU) | ||||||||
| 6319 | continue; | ||||||||
| 6320 | unsigned Opcode = LU->getOpcode(); | ||||||||
| 6321 | |||||||||
| 6322 | switch (Opcode) { | ||||||||
| 6323 | default: | ||||||||
| 6324 | continue; | ||||||||
| 6325 | // TODO: Expand list -- xor, div, gep, uaddo, etc.. | ||||||||
| 6326 | case Instruction::LShr: | ||||||||
| 6327 | case Instruction::AShr: | ||||||||
| 6328 | case Instruction::Shl: | ||||||||
| 6329 | case Instruction::Add: | ||||||||
| 6330 | case Instruction::Sub: | ||||||||
| 6331 | case Instruction::And: | ||||||||
| 6332 | case Instruction::Or: | ||||||||
| 6333 | case Instruction::Mul: { | ||||||||
| 6334 | Value *LL = LU->getOperand(0); | ||||||||
| 6335 | Value *LR = LU->getOperand(1); | ||||||||
| 6336 | // Find a recurrence. | ||||||||
| 6337 | if (LL == P) | ||||||||
| 6338 | L = LR; | ||||||||
| 6339 | else if (LR == P) | ||||||||
| 6340 | L = LL; | ||||||||
| 6341 | else | ||||||||
| 6342 | continue; // Check for recurrence with L and R flipped. | ||||||||
| 6343 | |||||||||
| 6344 | break; // Match! | ||||||||
| 6345 | } | ||||||||
| 6346 | }; | ||||||||
| 6347 | |||||||||
| 6348 | // We have matched a recurrence of the form: | ||||||||
| 6349 | // %iv = [R, %entry], [%iv.next, %backedge] | ||||||||
| 6350 | // %iv.next = binop %iv, L | ||||||||
| 6351 | // OR | ||||||||
| 6352 | // %iv = [R, %entry], [%iv.next, %backedge] | ||||||||
| 6353 | // %iv.next = binop L, %iv | ||||||||
| 6354 | BO = cast<BinaryOperator>(LU); | ||||||||
| 6355 | Start = R; | ||||||||
| 6356 | Step = L; | ||||||||
| 6357 | return true; | ||||||||
| 6358 | } | ||||||||
| 6359 | return false; | ||||||||
| 6360 | } | ||||||||
| 6361 | |||||||||
| 6362 | bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P, | ||||||||
| 6363 | Value *&Start, Value *&Step) { | ||||||||
| 6364 | BinaryOperator *BO = nullptr; | ||||||||
| 6365 | P = dyn_cast<PHINode>(I->getOperand(0)); | ||||||||
| 6366 | if (!P) | ||||||||
| 6367 | P = dyn_cast<PHINode>(I->getOperand(1)); | ||||||||
| 6368 | return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I; | ||||||||
| 6369 | } | ||||||||
| 6370 | |||||||||
| 6371 | /// Return true if "icmp Pred LHS RHS" is always true. | ||||||||
| 6372 | static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, | ||||||||
| 6373 | const Value *RHS, const DataLayout &DL, | ||||||||
| 6374 | unsigned Depth) { | ||||||||
| 6375 | assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!")((void)0); | ||||||||
| 6376 | if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) | ||||||||
| 6377 | return true; | ||||||||
| 6378 | |||||||||
| 6379 | switch (Pred) { | ||||||||
| 6380 | default: | ||||||||
| 6381 | return false; | ||||||||
| 6382 | |||||||||
| 6383 | case CmpInst::ICMP_SLE: { | ||||||||
| 6384 | const APInt *C; | ||||||||
| 6385 | |||||||||
| 6386 | // LHS s<= LHS +_{nsw} C if C >= 0 | ||||||||
| 6387 | if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) | ||||||||
| 6388 | return !C->isNegative(); | ||||||||
| 6389 | return false; | ||||||||
| 6390 | } | ||||||||
| 6391 | |||||||||
| 6392 | case CmpInst::ICMP_ULE: { | ||||||||
| 6393 | const APInt *C; | ||||||||
| 6394 | |||||||||
| 6395 | // LHS u<= LHS +_{nuw} C for any C | ||||||||
| 6396 | if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) | ||||||||
| 6397 | return true; | ||||||||
| 6398 | |||||||||
| 6399 | // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) | ||||||||
| 6400 | auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, | ||||||||
| 6401 | const Value *&X, | ||||||||
| 6402 | const APInt *&CA, const APInt *&CB) { | ||||||||
| 6403 | if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && | ||||||||
| 6404 | match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) | ||||||||
| 6405 | return true; | ||||||||
| 6406 | |||||||||
| 6407 | // If X & C == 0 then (X | C) == X +_{nuw} C | ||||||||
| 6408 | if (match(A, m_Or(m_Value(X), m_APInt(CA))) && | ||||||||
| 6409 | match(B, m_Or(m_Specific(X), m_APInt(CB)))) { | ||||||||
| 6410 | KnownBits Known(CA->getBitWidth()); | ||||||||
| 6411 | computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, | ||||||||
| 6412 | /*CxtI*/ nullptr, /*DT*/ nullptr); | ||||||||
| 6413 | if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) | ||||||||
| 6414 | return true; | ||||||||
| 6415 | } | ||||||||
| 6416 | |||||||||
| 6417 | return false; | ||||||||
| 6418 | }; | ||||||||
| 6419 | |||||||||
| 6420 | const Value *X; | ||||||||
| 6421 | const APInt *CLHS, *CRHS; | ||||||||
| 6422 | if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) | ||||||||
| 6423 | return CLHS->ule(*CRHS); | ||||||||
| 6424 | |||||||||
| 6425 | return false; | ||||||||
| 6426 | } | ||||||||
| 6427 | } | ||||||||
| 6428 | } | ||||||||
| 6429 | |||||||||
| 6430 | /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred | ||||||||
| 6431 | /// ALHS ARHS" is true. Otherwise, return None. | ||||||||
| 6432 | static Optional<bool> | ||||||||
| 6433 | isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, | ||||||||
| 6434 | const Value *ARHS, const Value *BLHS, const Value *BRHS, | ||||||||
| 6435 | const DataLayout &DL, unsigned Depth) { | ||||||||
| 6436 | switch (Pred) { | ||||||||
| 6437 | default: | ||||||||
| 6438 | return None; | ||||||||
| 6439 | |||||||||
| 6440 | case CmpInst::ICMP_SLT: | ||||||||
| 6441 | case CmpInst::ICMP_SLE: | ||||||||
| 6442 | if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && | ||||||||
| 6443 | isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) | ||||||||
| 6444 | return true; | ||||||||
| 6445 | return None; | ||||||||
| 6446 | |||||||||
| 6447 | case CmpInst::ICMP_ULT: | ||||||||
| 6448 | case CmpInst::ICMP_ULE: | ||||||||
| 6449 | if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && | ||||||||
| 6450 | isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) | ||||||||
| 6451 | return true; | ||||||||
| 6452 | return None; | ||||||||
| 6453 | } | ||||||||
| 6454 | } | ||||||||
| 6455 | |||||||||
| 6456 | /// Return true if the operands of the two compares match. IsSwappedOps is true | ||||||||
| 6457 | /// when the operands match, but are swapped. | ||||||||
| 6458 | static bool isMatchingOps(const Value *ALHS, const Value *ARHS, | ||||||||
| 6459 | const Value *BLHS, const Value *BRHS, | ||||||||
| 6460 | bool &IsSwappedOps) { | ||||||||
| 6461 | |||||||||
| 6462 | bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); | ||||||||
| 6463 | IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); | ||||||||
| 6464 | return IsMatchingOps || IsSwappedOps; | ||||||||
| 6465 | } | ||||||||
| 6466 | |||||||||
| 6467 | /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true. | ||||||||
| 6468 | /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false. | ||||||||
| 6469 | /// Otherwise, return None if we can't infer anything. | ||||||||
| 6470 | static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred, | ||||||||
| 6471 | CmpInst::Predicate BPred, | ||||||||
| 6472 | bool AreSwappedOps) { | ||||||||
| 6473 | // Canonicalize the predicate as if the operands were not commuted. | ||||||||
| 6474 | if (AreSwappedOps) | ||||||||
| 6475 | BPred = ICmpInst::getSwappedPredicate(BPred); | ||||||||
| 6476 | |||||||||
| 6477 | if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred)) | ||||||||
| 6478 | return true; | ||||||||
| 6479 | if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred)) | ||||||||
| 6480 | return false; | ||||||||
| 6481 | |||||||||
| 6482 | return None; | ||||||||
| 6483 | } | ||||||||
| 6484 | |||||||||
| 6485 | /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true. | ||||||||
| 6486 | /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false. | ||||||||
| 6487 | /// Otherwise, return None if we can't infer anything. | ||||||||
| 6488 | static Optional<bool> | ||||||||
| 6489 | isImpliedCondMatchingImmOperands(CmpInst::Predicate APred, | ||||||||
| 6490 | const ConstantInt *C1, | ||||||||
| 6491 | CmpInst::Predicate BPred, | ||||||||
| 6492 | const ConstantInt *C2) { | ||||||||
| 6493 | ConstantRange DomCR = | ||||||||
| 6494 | ConstantRange::makeExactICmpRegion(APred, C1->getValue()); | ||||||||
| 6495 | ConstantRange CR = ConstantRange::makeExactICmpRegion(BPred, C2->getValue()); | ||||||||
| 6496 | ConstantRange Intersection = DomCR.intersectWith(CR); | ||||||||
| 6497 | ConstantRange Difference = DomCR.difference(CR); | ||||||||
| 6498 | if (Intersection.isEmptySet()) | ||||||||
| 6499 | return false; | ||||||||
| 6500 | if (Difference.isEmptySet()) | ||||||||
| 6501 | return true; | ||||||||
| 6502 | return None; | ||||||||
| 6503 | } | ||||||||
| 6504 | |||||||||
| 6505 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is | ||||||||
| 6506 | /// false. Otherwise, return None if we can't infer anything. | ||||||||
| 6507 | static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS, | ||||||||
| 6508 | CmpInst::Predicate BPred, | ||||||||
| 6509 | const Value *BLHS, const Value *BRHS, | ||||||||
| 6510 | const DataLayout &DL, bool LHSIsTrue, | ||||||||
| 6511 | unsigned Depth) { | ||||||||
| 6512 | Value *ALHS = LHS->getOperand(0); | ||||||||
| 6513 | Value *ARHS = LHS->getOperand(1); | ||||||||
| 6514 | |||||||||
| 6515 | // The rest of the logic assumes the LHS condition is true. If that's not the | ||||||||
| 6516 | // case, invert the predicate to make it so. | ||||||||
| 6517 | CmpInst::Predicate APred = | ||||||||
| 6518 | LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); | ||||||||
| 6519 | |||||||||
| 6520 | // Can we infer anything when the two compares have matching operands? | ||||||||
| 6521 | bool AreSwappedOps; | ||||||||
| 6522 | if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) { | ||||||||
| 6523 | if (Optional<bool> Implication = isImpliedCondMatchingOperands( | ||||||||
| 6524 | APred, BPred, AreSwappedOps)) | ||||||||
| 6525 | return Implication; | ||||||||
| 6526 | // No amount of additional analysis will infer the second condition, so | ||||||||
| 6527 | // early exit. | ||||||||
| 6528 | return None; | ||||||||
| 6529 | } | ||||||||
| 6530 | |||||||||
| 6531 | // Can we infer anything when the LHS operands match and the RHS operands are | ||||||||
| 6532 | // constants (not necessarily matching)? | ||||||||
| 6533 | if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) { | ||||||||
| 6534 | if (Optional<bool> Implication = isImpliedCondMatchingImmOperands( | ||||||||
| 6535 | APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS))) | ||||||||
| 6536 | return Implication; | ||||||||
| 6537 | // No amount of additional analysis will infer the second condition, so | ||||||||
| 6538 | // early exit. | ||||||||
| 6539 | return None; | ||||||||
| 6540 | } | ||||||||
| 6541 | |||||||||
| 6542 | if (APred == BPred) | ||||||||
| 6543 | return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth); | ||||||||
| 6544 | return None; | ||||||||
| 6545 | } | ||||||||
| 6546 | |||||||||
| 6547 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is | ||||||||
| 6548 | /// false. Otherwise, return None if we can't infer anything. We expect the | ||||||||
| 6549 | /// RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select' instruction. | ||||||||
| 6550 | static Optional<bool> | ||||||||
| 6551 | isImpliedCondAndOr(const Instruction *LHS, CmpInst::Predicate RHSPred, | ||||||||
| 6552 | const Value *RHSOp0, const Value *RHSOp1, | ||||||||
| 6553 | const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { | ||||||||
| 6554 | // The LHS must be an 'or', 'and', or a 'select' instruction. | ||||||||
| 6555 | assert((LHS->getOpcode() == Instruction::And ||((void)0) | ||||||||
| 6556 | LHS->getOpcode() == Instruction::Or ||((void)0) | ||||||||
| 6557 | LHS->getOpcode() == Instruction::Select) &&((void)0) | ||||||||
| 6558 | "Expected LHS to be 'and', 'or', or 'select'.")((void)0); | ||||||||
| 6559 | |||||||||
| 6560 | assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit")((void)0); | ||||||||
| 6561 | |||||||||
| 6562 | // If the result of an 'or' is false, then we know both legs of the 'or' are | ||||||||
| 6563 | // false. Similarly, if the result of an 'and' is true, then we know both | ||||||||
| 6564 | // legs of the 'and' are true. | ||||||||
| 6565 | const Value *ALHS, *ARHS; | ||||||||
| 6566 | if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) || | ||||||||
| 6567 | (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) { | ||||||||
| 6568 | // FIXME: Make this non-recursion. | ||||||||
| 6569 | if (Optional<bool> Implication = isImpliedCondition( | ||||||||
| 6570 | ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) | ||||||||
| 6571 | return Implication; | ||||||||
| 6572 | if (Optional<bool> Implication = isImpliedCondition( | ||||||||
| 6573 | ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) | ||||||||
| 6574 | return Implication; | ||||||||
| 6575 | return None; | ||||||||
| 6576 | } | ||||||||
| 6577 | return None; | ||||||||
| 6578 | } | ||||||||
| 6579 | |||||||||
| 6580 | Optional<bool> | ||||||||
| 6581 | llvm::isImpliedCondition(const Value *LHS, CmpInst::Predicate RHSPred, | ||||||||
| 6582 | const Value *RHSOp0, const Value *RHSOp1, | ||||||||
| 6583 | const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { | ||||||||
| 6584 | // Bail out when we hit the limit. | ||||||||
| 6585 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 6586 | return None; | ||||||||
| 6587 | |||||||||
| 6588 | // A mismatch occurs when we compare a scalar cmp to a vector cmp, for | ||||||||
| 6589 | // example. | ||||||||
| 6590 | if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy()) | ||||||||
| 6591 | return None; | ||||||||
| 6592 | |||||||||
| 6593 | Type *OpTy = LHS->getType(); | ||||||||
| 6594 | assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!")((void)0); | ||||||||
| 6595 | |||||||||
| 6596 | // FIXME: Extending the code below to handle vectors. | ||||||||
| 6597 | if (OpTy->isVectorTy()) | ||||||||
| 6598 | return None; | ||||||||
| 6599 | |||||||||
| 6600 | assert(OpTy->isIntegerTy(1) && "implied by above")((void)0); | ||||||||
| 6601 | |||||||||
| 6602 | // Both LHS and RHS are icmps. | ||||||||
| 6603 | const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS); | ||||||||
| 6604 | if (LHSCmp) | ||||||||
| 6605 | return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, | ||||||||
| 6606 | Depth); | ||||||||
| 6607 | |||||||||
| 6608 | /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect | ||||||||
| 6609 | /// the RHS to be an icmp. | ||||||||
| 6610 | /// FIXME: Add support for and/or/select on the RHS. | ||||||||
| 6611 | if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) { | ||||||||
| 6612 | if ((LHSI->getOpcode() == Instruction::And || | ||||||||
| 6613 | LHSI->getOpcode() == Instruction::Or || | ||||||||
| 6614 | LHSI->getOpcode() == Instruction::Select)) | ||||||||
| 6615 | return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, | ||||||||
| 6616 | Depth); | ||||||||
| 6617 | } | ||||||||
| 6618 | return None; | ||||||||
| 6619 | } | ||||||||
| 6620 | |||||||||
| 6621 | Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS, | ||||||||
| 6622 | const DataLayout &DL, bool LHSIsTrue, | ||||||||
| 6623 | unsigned Depth) { | ||||||||
| 6624 | // LHS ==> RHS by definition | ||||||||
| 6625 | if (LHS == RHS) | ||||||||
| 6626 | return LHSIsTrue; | ||||||||
| 6627 | |||||||||
| 6628 | const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS); | ||||||||
| 6629 | if (RHSCmp) | ||||||||
| 6630 | return isImpliedCondition(LHS, RHSCmp->getPredicate(), | ||||||||
| 6631 | RHSCmp->getOperand(0), RHSCmp->getOperand(1), DL, | ||||||||
| 6632 | LHSIsTrue, Depth); | ||||||||
| 6633 | return None; | ||||||||
| 6634 | } | ||||||||
| 6635 | |||||||||
| 6636 | // Returns a pair (Condition, ConditionIsTrue), where Condition is a branch | ||||||||
| 6637 | // condition dominating ContextI or nullptr, if no condition is found. | ||||||||
| 6638 | static std::pair<Value *, bool> | ||||||||
| 6639 | getDomPredecessorCondition(const Instruction *ContextI) { | ||||||||
| 6640 | if (!ContextI || !ContextI->getParent()) | ||||||||
| 6641 | return {nullptr, false}; | ||||||||
| 6642 | |||||||||
| 6643 | // TODO: This is a poor/cheap way to determine dominance. Should we use a | ||||||||
| 6644 | // dominator tree (eg, from a SimplifyQuery) instead? | ||||||||
| 6645 | const BasicBlock *ContextBB = ContextI->getParent(); | ||||||||
| 6646 | const BasicBlock *PredBB = ContextBB->getSinglePredecessor(); | ||||||||
| 6647 | if (!PredBB) | ||||||||
| 6648 | return {nullptr, false}; | ||||||||
| 6649 | |||||||||
| 6650 | // We need a conditional branch in the predecessor. | ||||||||
| 6651 | Value *PredCond; | ||||||||
| 6652 | BasicBlock *TrueBB, *FalseBB; | ||||||||
| 6653 | if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB))) | ||||||||
| 6654 | return {nullptr, false}; | ||||||||
| 6655 | |||||||||
| 6656 | // The branch should get simplified. Don't bother simplifying this condition. | ||||||||
| 6657 | if (TrueBB == FalseBB) | ||||||||
| 6658 | return {nullptr, false}; | ||||||||
| 6659 | |||||||||
| 6660 | assert((TrueBB == ContextBB || FalseBB == ContextBB) &&((void)0) | ||||||||
| 6661 | "Predecessor block does not point to successor?")((void)0); | ||||||||
| 6662 | |||||||||
| 6663 | // Is this condition implied by the predecessor condition? | ||||||||
| 6664 | return {PredCond, TrueBB == ContextBB}; | ||||||||
| 6665 | } | ||||||||
| 6666 | |||||||||
| 6667 | Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond, | ||||||||
| 6668 | const Instruction *ContextI, | ||||||||
| 6669 | const DataLayout &DL) { | ||||||||
| 6670 | assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool")((void)0); | ||||||||
| 6671 | auto PredCond = getDomPredecessorCondition(ContextI); | ||||||||
| 6672 | if (PredCond.first) | ||||||||
| 6673 | return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second); | ||||||||
| 6674 | return None; | ||||||||
| 6675 | } | ||||||||
| 6676 | |||||||||
| 6677 | Optional<bool> llvm::isImpliedByDomCondition(CmpInst::Predicate Pred, | ||||||||
| 6678 | const Value *LHS, const Value *RHS, | ||||||||
| 6679 | const Instruction *ContextI, | ||||||||
| 6680 | const DataLayout &DL) { | ||||||||
| 6681 | auto PredCond = getDomPredecessorCondition(ContextI); | ||||||||
| 6682 | if (PredCond.first) | ||||||||
| 6683 | return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL, | ||||||||
| 6684 | PredCond.second); | ||||||||
| 6685 | return None; | ||||||||
| 6686 | } | ||||||||
| 6687 | |||||||||
| 6688 | static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, | ||||||||
| 6689 | APInt &Upper, const InstrInfoQuery &IIQ) { | ||||||||
| 6690 | unsigned Width = Lower.getBitWidth(); | ||||||||
| 6691 | const APInt *C; | ||||||||
| 6692 | switch (BO.getOpcode()) { | ||||||||
| 6693 | case Instruction::Add: | ||||||||
| 6694 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { | ||||||||
| 6695 | // FIXME: If we have both nuw and nsw, we should reduce the range further. | ||||||||
| 6696 | if (IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(&BO))) { | ||||||||
| 6697 | // 'add nuw x, C' produces [C, UINT_MAX]. | ||||||||
| 6698 | Lower = *C; | ||||||||
| 6699 | } else if (IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(&BO))) { | ||||||||
| 6700 | if (C->isNegative()) { | ||||||||
| 6701 | // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. | ||||||||
| 6702 | Lower = APInt::getSignedMinValue(Width); | ||||||||
| 6703 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; | ||||||||
| 6704 | } else { | ||||||||
| 6705 | // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. | ||||||||
| 6706 | Lower = APInt::getSignedMinValue(Width) + *C; | ||||||||
| 6707 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6708 | } | ||||||||
| 6709 | } | ||||||||
| 6710 | } | ||||||||
| 6711 | break; | ||||||||
| 6712 | |||||||||
| 6713 | case Instruction::And: | ||||||||
| 6714 | if (match(BO.getOperand(1), m_APInt(C))) | ||||||||
| 6715 | // 'and x, C' produces [0, C]. | ||||||||
| 6716 | Upper = *C + 1; | ||||||||
| 6717 | break; | ||||||||
| 6718 | |||||||||
| 6719 | case Instruction::Or: | ||||||||
| 6720 | if (match(BO.getOperand(1), m_APInt(C))) | ||||||||
| 6721 | // 'or x, C' produces [C, UINT_MAX]. | ||||||||
| 6722 | Lower = *C; | ||||||||
| 6723 | break; | ||||||||
| 6724 | |||||||||
| 6725 | case Instruction::AShr: | ||||||||
| 6726 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { | ||||||||
| 6727 | // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. | ||||||||
| 6728 | Lower = APInt::getSignedMinValue(Width).ashr(*C); | ||||||||
| 6729 | Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; | ||||||||
| 6730 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||||||
| 6731 | unsigned ShiftAmount = Width - 1; | ||||||||
| 6732 | if (!C->isNullValue() && IIQ.isExact(&BO)) | ||||||||
| 6733 | ShiftAmount = C->countTrailingZeros(); | ||||||||
| 6734 | if (C->isNegative()) { | ||||||||
| 6735 | // 'ashr C, x' produces [C, C >> (Width-1)] | ||||||||
| 6736 | Lower = *C; | ||||||||
| 6737 | Upper = C->ashr(ShiftAmount) + 1; | ||||||||
| 6738 | } else { | ||||||||
| 6739 | // 'ashr C, x' produces [C >> (Width-1), C] | ||||||||
| 6740 | Lower = C->ashr(ShiftAmount); | ||||||||
| 6741 | Upper = *C + 1; | ||||||||
| 6742 | } | ||||||||
| 6743 | } | ||||||||
| 6744 | break; | ||||||||
| 6745 | |||||||||
| 6746 | case Instruction::LShr: | ||||||||
| 6747 | if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { | ||||||||
| 6748 | // 'lshr x, C' produces [0, UINT_MAX >> C]. | ||||||||
| 6749 | Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1; | ||||||||
| 6750 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||||||
| 6751 | // 'lshr C, x' produces [C >> (Width-1), C]. | ||||||||
| 6752 | unsigned ShiftAmount = Width - 1; | ||||||||
| 6753 | if (!C->isNullValue() && IIQ.isExact(&BO)) | ||||||||
| 6754 | ShiftAmount = C->countTrailingZeros(); | ||||||||
| 6755 | Lower = C->lshr(ShiftAmount); | ||||||||
| 6756 | Upper = *C + 1; | ||||||||
| 6757 | } | ||||||||
| 6758 | break; | ||||||||
| 6759 | |||||||||
| 6760 | case Instruction::Shl: | ||||||||
| 6761 | if (match(BO.getOperand(0), m_APInt(C))) { | ||||||||
| 6762 | if (IIQ.hasNoUnsignedWrap(&BO)) { | ||||||||
| 6763 | // 'shl nuw C, x' produces [C, C << CLZ(C)] | ||||||||
| 6764 | Lower = *C; | ||||||||
| 6765 | Upper = Lower.shl(Lower.countLeadingZeros()) + 1; | ||||||||
| 6766 | } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? | ||||||||
| 6767 | if (C->isNegative()) { | ||||||||
| 6768 | // 'shl nsw C, x' produces [C << CLO(C)-1, C] | ||||||||
| 6769 | unsigned ShiftAmount = C->countLeadingOnes() - 1; | ||||||||
| 6770 | Lower = C->shl(ShiftAmount); | ||||||||
| 6771 | Upper = *C + 1; | ||||||||
| 6772 | } else { | ||||||||
| 6773 | // 'shl nsw C, x' produces [C, C << CLZ(C)-1] | ||||||||
| 6774 | unsigned ShiftAmount = C->countLeadingZeros() - 1; | ||||||||
| 6775 | Lower = *C; | ||||||||
| 6776 | Upper = C->shl(ShiftAmount) + 1; | ||||||||
| 6777 | } | ||||||||
| 6778 | } | ||||||||
| 6779 | } | ||||||||
| 6780 | break; | ||||||||
| 6781 | |||||||||
| 6782 | case Instruction::SDiv: | ||||||||
| 6783 | if (match(BO.getOperand(1), m_APInt(C))) { | ||||||||
| 6784 | APInt IntMin = APInt::getSignedMinValue(Width); | ||||||||
| 6785 | APInt IntMax = APInt::getSignedMaxValue(Width); | ||||||||
| 6786 | if (C->isAllOnesValue()) { | ||||||||
| 6787 | // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] | ||||||||
| 6788 | // where C != -1 and C != 0 and C != 1 | ||||||||
| 6789 | Lower = IntMin + 1; | ||||||||
| 6790 | Upper = IntMax + 1; | ||||||||
| 6791 | } else if (C->countLeadingZeros() < Width - 1) { | ||||||||
| 6792 | // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] | ||||||||
| 6793 | // where C != -1 and C != 0 and C != 1 | ||||||||
| 6794 | Lower = IntMin.sdiv(*C); | ||||||||
| 6795 | Upper = IntMax.sdiv(*C); | ||||||||
| 6796 | if (Lower.sgt(Upper)) | ||||||||
| 6797 | std::swap(Lower, Upper); | ||||||||
| 6798 | Upper = Upper + 1; | ||||||||
| 6799 | assert(Upper != Lower && "Upper part of range has wrapped!")((void)0); | ||||||||
| 6800 | } | ||||||||
| 6801 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||||||
| 6802 | if (C->isMinSignedValue()) { | ||||||||
| 6803 | // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. | ||||||||
| 6804 | Lower = *C; | ||||||||
| 6805 | Upper = Lower.lshr(1) + 1; | ||||||||
| 6806 | } else { | ||||||||
| 6807 | // 'sdiv C, x' produces [-|C|, |C|]. | ||||||||
| 6808 | Upper = C->abs() + 1; | ||||||||
| 6809 | Lower = (-Upper) + 1; | ||||||||
| 6810 | } | ||||||||
| 6811 | } | ||||||||
| 6812 | break; | ||||||||
| 6813 | |||||||||
| 6814 | case Instruction::UDiv: | ||||||||
| 6815 | if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) { | ||||||||
| 6816 | // 'udiv x, C' produces [0, UINT_MAX / C]. | ||||||||
| 6817 | Upper = APInt::getMaxValue(Width).udiv(*C) + 1; | ||||||||
| 6818 | } else if (match(BO.getOperand(0), m_APInt(C))) { | ||||||||
| 6819 | // 'udiv C, x' produces [0, C]. | ||||||||
| 6820 | Upper = *C + 1; | ||||||||
| 6821 | } | ||||||||
| 6822 | break; | ||||||||
| 6823 | |||||||||
| 6824 | case Instruction::SRem: | ||||||||
| 6825 | if (match(BO.getOperand(1), m_APInt(C))) { | ||||||||
| 6826 | // 'srem x, C' produces (-|C|, |C|). | ||||||||
| 6827 | Upper = C->abs(); | ||||||||
| 6828 | Lower = (-Upper) + 1; | ||||||||
| 6829 | } | ||||||||
| 6830 | break; | ||||||||
| 6831 | |||||||||
| 6832 | case Instruction::URem: | ||||||||
| 6833 | if (match(BO.getOperand(1), m_APInt(C))) | ||||||||
| 6834 | // 'urem x, C' produces [0, C). | ||||||||
| 6835 | Upper = *C; | ||||||||
| 6836 | break; | ||||||||
| 6837 | |||||||||
| 6838 | default: | ||||||||
| 6839 | break; | ||||||||
| 6840 | } | ||||||||
| 6841 | } | ||||||||
| 6842 | |||||||||
| 6843 | static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower, | ||||||||
| 6844 | APInt &Upper) { | ||||||||
| 6845 | unsigned Width = Lower.getBitWidth(); | ||||||||
| 6846 | const APInt *C; | ||||||||
| 6847 | switch (II.getIntrinsicID()) { | ||||||||
| 6848 | case Intrinsic::ctpop: | ||||||||
| 6849 | case Intrinsic::ctlz: | ||||||||
| 6850 | case Intrinsic::cttz: | ||||||||
| 6851 | // Maximum of set/clear bits is the bit width. | ||||||||
| 6852 | assert(Lower == 0 && "Expected lower bound to be zero")((void)0); | ||||||||
| 6853 | Upper = Width + 1; | ||||||||
| 6854 | break; | ||||||||
| 6855 | case Intrinsic::uadd_sat: | ||||||||
| 6856 | // uadd.sat(x, C) produces [C, UINT_MAX]. | ||||||||
| 6857 | if (match(II.getOperand(0), m_APInt(C)) || | ||||||||
| 6858 | match(II.getOperand(1), m_APInt(C))) | ||||||||
| 6859 | Lower = *C; | ||||||||
| 6860 | break; | ||||||||
| 6861 | case Intrinsic::sadd_sat: | ||||||||
| 6862 | if (match(II.getOperand(0), m_APInt(C)) || | ||||||||
| 6863 | match(II.getOperand(1), m_APInt(C))) { | ||||||||
| 6864 | if (C->isNegative()) { | ||||||||
| 6865 | // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. | ||||||||
| 6866 | Lower = APInt::getSignedMinValue(Width); | ||||||||
| 6867 | Upper = APInt::getSignedMaxValue(Width) + *C + 1; | ||||||||
| 6868 | } else { | ||||||||
| 6869 | // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. | ||||||||
| 6870 | Lower = APInt::getSignedMinValue(Width) + *C; | ||||||||
| 6871 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6872 | } | ||||||||
| 6873 | } | ||||||||
| 6874 | break; | ||||||||
| 6875 | case Intrinsic::usub_sat: | ||||||||
| 6876 | // usub.sat(C, x) produces [0, C]. | ||||||||
| 6877 | if (match(II.getOperand(0), m_APInt(C))) | ||||||||
| 6878 | Upper = *C + 1; | ||||||||
| 6879 | // usub.sat(x, C) produces [0, UINT_MAX - C]. | ||||||||
| 6880 | else if (match(II.getOperand(1), m_APInt(C))) | ||||||||
| 6881 | Upper = APInt::getMaxValue(Width) - *C + 1; | ||||||||
| 6882 | break; | ||||||||
| 6883 | case Intrinsic::ssub_sat: | ||||||||
| 6884 | if (match(II.getOperand(0), m_APInt(C))) { | ||||||||
| 6885 | if (C->isNegative()) { | ||||||||
| 6886 | // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. | ||||||||
| 6887 | Lower = APInt::getSignedMinValue(Width); | ||||||||
| 6888 | Upper = *C - APInt::getSignedMinValue(Width) + 1; | ||||||||
| 6889 | } else { | ||||||||
| 6890 | // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. | ||||||||
| 6891 | Lower = *C - APInt::getSignedMaxValue(Width); | ||||||||
| 6892 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6893 | } | ||||||||
| 6894 | } else if (match(II.getOperand(1), m_APInt(C))) { | ||||||||
| 6895 | if (C->isNegative()) { | ||||||||
| 6896 | // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: | ||||||||
| 6897 | Lower = APInt::getSignedMinValue(Width) - *C; | ||||||||
| 6898 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6899 | } else { | ||||||||
| 6900 | // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. | ||||||||
| 6901 | Lower = APInt::getSignedMinValue(Width); | ||||||||
| 6902 | Upper = APInt::getSignedMaxValue(Width) - *C + 1; | ||||||||
| 6903 | } | ||||||||
| 6904 | } | ||||||||
| 6905 | break; | ||||||||
| 6906 | case Intrinsic::umin: | ||||||||
| 6907 | case Intrinsic::umax: | ||||||||
| 6908 | case Intrinsic::smin: | ||||||||
| 6909 | case Intrinsic::smax: | ||||||||
| 6910 | if (!match(II.getOperand(0), m_APInt(C)) && | ||||||||
| 6911 | !match(II.getOperand(1), m_APInt(C))) | ||||||||
| 6912 | break; | ||||||||
| 6913 | |||||||||
| 6914 | switch (II.getIntrinsicID()) { | ||||||||
| 6915 | case Intrinsic::umin: | ||||||||
| 6916 | Upper = *C + 1; | ||||||||
| 6917 | break; | ||||||||
| 6918 | case Intrinsic::umax: | ||||||||
| 6919 | Lower = *C; | ||||||||
| 6920 | break; | ||||||||
| 6921 | case Intrinsic::smin: | ||||||||
| 6922 | Lower = APInt::getSignedMinValue(Width); | ||||||||
| 6923 | Upper = *C + 1; | ||||||||
| 6924 | break; | ||||||||
| 6925 | case Intrinsic::smax: | ||||||||
| 6926 | Lower = *C; | ||||||||
| 6927 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6928 | break; | ||||||||
| 6929 | default: | ||||||||
| 6930 | llvm_unreachable("Must be min/max intrinsic")__builtin_unreachable(); | ||||||||
| 6931 | } | ||||||||
| 6932 | break; | ||||||||
| 6933 | case Intrinsic::abs: | ||||||||
| 6934 | // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX], | ||||||||
| 6935 | // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. | ||||||||
| 6936 | if (match(II.getOperand(1), m_One())) | ||||||||
| 6937 | Upper = APInt::getSignedMaxValue(Width) + 1; | ||||||||
| 6938 | else | ||||||||
| 6939 | Upper = APInt::getSignedMinValue(Width) + 1; | ||||||||
| 6940 | break; | ||||||||
| 6941 | default: | ||||||||
| 6942 | break; | ||||||||
| 6943 | } | ||||||||
| 6944 | } | ||||||||
| 6945 | |||||||||
| 6946 | static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, | ||||||||
| 6947 | APInt &Upper, const InstrInfoQuery &IIQ) { | ||||||||
| 6948 | const Value *LHS = nullptr, *RHS = nullptr; | ||||||||
| 6949 | SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); | ||||||||
| 6950 | if (R.Flavor == SPF_UNKNOWN) | ||||||||
| 6951 | return; | ||||||||
| 6952 | |||||||||
| 6953 | unsigned BitWidth = SI.getType()->getScalarSizeInBits(); | ||||||||
| 6954 | |||||||||
| 6955 | if (R.Flavor == SelectPatternFlavor::SPF_ABS) { | ||||||||
| 6956 | // If the negation part of the abs (in RHS) has the NSW flag, | ||||||||
| 6957 | // then the result of abs(X) is [0..SIGNED_MAX], | ||||||||
| 6958 | // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. | ||||||||
| 6959 | Lower = APInt::getNullValue(BitWidth); | ||||||||
| 6960 | if (match(RHS, m_Neg(m_Specific(LHS))) && | ||||||||
| 6961 | IIQ.hasNoSignedWrap(cast<Instruction>(RHS))) | ||||||||
| 6962 | Upper = APInt::getSignedMaxValue(BitWidth) + 1; | ||||||||
| 6963 | else | ||||||||
| 6964 | Upper = APInt::getSignedMinValue(BitWidth) + 1; | ||||||||
| 6965 | return; | ||||||||
| 6966 | } | ||||||||
| 6967 | |||||||||
| 6968 | if (R.Flavor == SelectPatternFlavor::SPF_NABS) { | ||||||||
| 6969 | // The result of -abs(X) is <= 0. | ||||||||
| 6970 | Lower = APInt::getSignedMinValue(BitWidth); | ||||||||
| 6971 | Upper = APInt(BitWidth, 1); | ||||||||
| 6972 | return; | ||||||||
| 6973 | } | ||||||||
| 6974 | |||||||||
| 6975 | const APInt *C; | ||||||||
| 6976 | if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C))) | ||||||||
| 6977 | return; | ||||||||
| 6978 | |||||||||
| 6979 | switch (R.Flavor) { | ||||||||
| 6980 | case SPF_UMIN: | ||||||||
| 6981 | Upper = *C + 1; | ||||||||
| 6982 | break; | ||||||||
| 6983 | case SPF_UMAX: | ||||||||
| 6984 | Lower = *C; | ||||||||
| 6985 | break; | ||||||||
| 6986 | case SPF_SMIN: | ||||||||
| 6987 | Lower = APInt::getSignedMinValue(BitWidth); | ||||||||
| 6988 | Upper = *C + 1; | ||||||||
| 6989 | break; | ||||||||
| 6990 | case SPF_SMAX: | ||||||||
| 6991 | Lower = *C; | ||||||||
| 6992 | Upper = APInt::getSignedMaxValue(BitWidth) + 1; | ||||||||
| 6993 | break; | ||||||||
| 6994 | default: | ||||||||
| 6995 | break; | ||||||||
| 6996 | } | ||||||||
| 6997 | } | ||||||||
| 6998 | |||||||||
| 6999 | ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo, | ||||||||
| 7000 | AssumptionCache *AC, | ||||||||
| 7001 | const Instruction *CtxI, | ||||||||
| 7002 | unsigned Depth) { | ||||||||
| 7003 | assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction")((void)0); | ||||||||
| 7004 | |||||||||
| 7005 | if (Depth == MaxAnalysisRecursionDepth) | ||||||||
| 7006 | return ConstantRange::getFull(V->getType()->getScalarSizeInBits()); | ||||||||
| 7007 | |||||||||
| 7008 | const APInt *C; | ||||||||
| 7009 | if (match(V, m_APInt(C))) | ||||||||
| 7010 | return ConstantRange(*C); | ||||||||
| 7011 | |||||||||
| 7012 | InstrInfoQuery IIQ(UseInstrInfo); | ||||||||
| 7013 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); | ||||||||
| 7014 | APInt Lower = APInt(BitWidth, 0); | ||||||||
| 7015 | APInt Upper = APInt(BitWidth, 0); | ||||||||
| 7016 | if (auto *BO = dyn_cast<BinaryOperator>(V)) | ||||||||
| 7017 | setLimitsForBinOp(*BO, Lower, Upper, IIQ); | ||||||||
| 7018 | else if (auto *II = dyn_cast<IntrinsicInst>(V)) | ||||||||
| 7019 | setLimitsForIntrinsic(*II, Lower, Upper); | ||||||||
| 7020 | else if (auto *SI = dyn_cast<SelectInst>(V)) | ||||||||
| 7021 | setLimitsForSelectPattern(*SI, Lower, Upper, IIQ); | ||||||||
| 7022 | |||||||||
| 7023 | ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper); | ||||||||
| 7024 | |||||||||
| 7025 | if (auto *I = dyn_cast<Instruction>(V)) | ||||||||
| 7026 | if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range)) | ||||||||
| 7027 | CR = CR.intersectWith(getConstantRangeFromMetadata(*Range)); | ||||||||
| 7028 | |||||||||
| 7029 | if (CtxI && AC) { | ||||||||
| 7030 | // Try to restrict the range based on information from assumptions. | ||||||||
| 7031 | for (auto &AssumeVH : AC->assumptionsFor(V)) { | ||||||||
| 7032 | if (!AssumeVH) | ||||||||
| 7033 | continue; | ||||||||
| 7034 | CallInst *I = cast<CallInst>(AssumeVH); | ||||||||
| 7035 | assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&((void)0) | ||||||||
| 7036 | "Got assumption for the wrong function!")((void)0); | ||||||||
| 7037 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&((void)0) | ||||||||
| 7038 | "must be an assume intrinsic")((void)0); | ||||||||
| 7039 | |||||||||
| 7040 | if (!isValidAssumeForContext(I, CtxI, nullptr)) | ||||||||
| 7041 | continue; | ||||||||
| 7042 | Value *Arg = I->getArgOperand(0); | ||||||||
| 7043 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg); | ||||||||
| 7044 | // Currently we just use information from comparisons. | ||||||||
| 7045 | if (!Cmp || Cmp->getOperand(0) != V) | ||||||||
| 7046 | continue; | ||||||||
| 7047 | ConstantRange RHS = computeConstantRange(Cmp->getOperand(1), UseInstrInfo, | ||||||||
| 7048 | AC, I, Depth + 1); | ||||||||
| 7049 | CR = CR.intersectWith( | ||||||||
| 7050 | ConstantRange::makeSatisfyingICmpRegion(Cmp->getPredicate(), RHS)); | ||||||||
| 7051 | } | ||||||||
| 7052 | } | ||||||||
| 7053 | |||||||||
| 7054 | return CR; | ||||||||
| 7055 | } | ||||||||
| 7056 | |||||||||
| 7057 | static Optional<int64_t> | ||||||||
| 7058 | getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) { | ||||||||
| 7059 | // Skip over the first indices. | ||||||||
| 7060 | gep_type_iterator GTI = gep_type_begin(GEP); | ||||||||
| 7061 | for (unsigned i = 1; i != Idx; ++i, ++GTI) | ||||||||
| 7062 | /*skip along*/; | ||||||||
| 7063 | |||||||||
| 7064 | // Compute the offset implied by the rest of the indices. | ||||||||
| 7065 | int64_t Offset = 0; | ||||||||
| 7066 | for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { | ||||||||
| 7067 | ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i)); | ||||||||
| 7068 | if (!OpC) | ||||||||
| 7069 | return None; | ||||||||
| 7070 | if (OpC->isZero()) | ||||||||
| 7071 | continue; // No offset. | ||||||||
| 7072 | |||||||||
| 7073 | // Handle struct indices, which add their field offset to the pointer. | ||||||||
| 7074 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||||||
| 7075 | Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); | ||||||||
| 7076 | continue; | ||||||||
| 7077 | } | ||||||||
| 7078 | |||||||||
| 7079 | // Otherwise, we have a sequential type like an array or fixed-length | ||||||||
| 7080 | // vector. Multiply the index by the ElementSize. | ||||||||
| 7081 | TypeSize Size = DL.getTypeAllocSize(GTI.getIndexedType()); | ||||||||
| 7082 | if (Size.isScalable()) | ||||||||
| 7083 | return None; | ||||||||
| 7084 | Offset += Size.getFixedSize() * OpC->getSExtValue(); | ||||||||
| 7085 | } | ||||||||
| 7086 | |||||||||
| 7087 | return Offset; | ||||||||
| 7088 | } | ||||||||
| 7089 | |||||||||
| 7090 | Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2, | ||||||||
| 7091 | const DataLayout &DL) { | ||||||||
| 7092 | Ptr1 = Ptr1->stripPointerCasts(); | ||||||||
| 7093 | Ptr2 = Ptr2->stripPointerCasts(); | ||||||||
| 7094 | |||||||||
| 7095 | // Handle the trivial case first. | ||||||||
| 7096 | if (Ptr1 == Ptr2) { | ||||||||
| 7097 | return 0; | ||||||||
| 7098 | } | ||||||||
| 7099 | |||||||||
| 7100 | const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1); | ||||||||
| 7101 | const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2); | ||||||||
| 7102 | |||||||||
| 7103 | // If one pointer is a GEP see if the GEP is a constant offset from the base, | ||||||||
| 7104 | // as in "P" and "gep P, 1". | ||||||||
| 7105 | // Also do this iteratively to handle the the following case: | ||||||||
| 7106 | // Ptr_t1 = GEP Ptr1, c1 | ||||||||
| 7107 | // Ptr_t2 = GEP Ptr_t1, c2 | ||||||||
| 7108 | // Ptr2 = GEP Ptr_t2, c3 | ||||||||
| 7109 | // where we will return c1+c2+c3. | ||||||||
| 7110 | // TODO: Handle the case when both Ptr1 and Ptr2 are GEPs of some common base | ||||||||
| 7111 | // -- replace getOffsetFromBase with getOffsetAndBase, check that the bases | ||||||||
| 7112 | // are the same, and return the difference between offsets. | ||||||||
| 7113 | auto getOffsetFromBase = [&DL](const GEPOperator *GEP, | ||||||||
| 7114 | const Value *Ptr) -> Optional<int64_t> { | ||||||||
| 7115 | const GEPOperator *GEP_T = GEP; | ||||||||
| 7116 | int64_t OffsetVal = 0; | ||||||||
| 7117 | bool HasSameBase = false; | ||||||||
| 7118 | while (GEP_T) { | ||||||||
| 7119 | auto Offset = getOffsetFromIndex(GEP_T, 1, DL); | ||||||||
| 7120 | if (!Offset) | ||||||||
| 7121 | return None; | ||||||||
| 7122 | OffsetVal += *Offset; | ||||||||
| 7123 | auto Op0 = GEP_T->getOperand(0)->stripPointerCasts(); | ||||||||
| 7124 | if (Op0 == Ptr) { | ||||||||
| 7125 | HasSameBase = true; | ||||||||
| 7126 | break; | ||||||||
| 7127 | } | ||||||||
| 7128 | GEP_T = dyn_cast<GEPOperator>(Op0); | ||||||||
| 7129 | } | ||||||||
| 7130 | if (!HasSameBase) | ||||||||
| 7131 | return None; | ||||||||
| 7132 | return OffsetVal; | ||||||||
| 7133 | }; | ||||||||
| 7134 | |||||||||
| 7135 | if (GEP1) { | ||||||||
| 7136 | auto Offset = getOffsetFromBase(GEP1, Ptr2); | ||||||||
| 7137 | if (Offset) | ||||||||
| 7138 | return -*Offset; | ||||||||
| 7139 | } | ||||||||
| 7140 | if (GEP2) { | ||||||||
| 7141 | auto Offset = getOffsetFromBase(GEP2, Ptr1); | ||||||||
| 7142 | if (Offset) | ||||||||
| 7143 | return Offset; | ||||||||
| 7144 | } | ||||||||
| 7145 | |||||||||
| 7146 | // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical | ||||||||
| 7147 | // base. After that base, they may have some number of common (and | ||||||||
| 7148 | // potentially variable) indices. After that they handle some constant | ||||||||
| 7149 | // offset, which determines their offset from each other. At this point, we | ||||||||
| 7150 | // handle no other case. | ||||||||
| 7151 | if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0)) | ||||||||
| 7152 | return None; | ||||||||
| 7153 | |||||||||
| 7154 | // Skip any common indices and track the GEP types. | ||||||||
| 7155 | unsigned Idx = 1; | ||||||||
| 7156 | for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx) | ||||||||
| 7157 | if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx)) | ||||||||
| 7158 | break; | ||||||||
| 7159 | |||||||||
| 7160 | auto Offset1 = getOffsetFromIndex(GEP1, Idx, DL); | ||||||||
| 7161 | auto Offset2 = getOffsetFromIndex(GEP2, Idx, DL); | ||||||||
| 7162 | if (!Offset1 || !Offset2) | ||||||||
| 7163 | return None; | ||||||||
| 7164 | return *Offset2 - *Offset1; | ||||||||
| 7165 | } |
| 1 | //===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file exposes the class definitions of all of the subclasses of the |
| 10 | // Instruction class. This is meant to be an easy way to get access to all |
| 11 | // instruction subclasses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_IR_INSTRUCTIONS_H |
| 16 | #define LLVM_IR_INSTRUCTIONS_H |
| 17 | |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/ADT/Bitfields.h" |
| 20 | #include "llvm/ADT/MapVector.h" |
| 21 | #include "llvm/ADT/None.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | #include "llvm/ADT/SmallVector.h" |
| 24 | #include "llvm/ADT/StringRef.h" |
| 25 | #include "llvm/ADT/Twine.h" |
| 26 | #include "llvm/ADT/iterator.h" |
| 27 | #include "llvm/ADT/iterator_range.h" |
| 28 | #include "llvm/IR/Attributes.h" |
| 29 | #include "llvm/IR/BasicBlock.h" |
| 30 | #include "llvm/IR/CallingConv.h" |
| 31 | #include "llvm/IR/CFG.h" |
| 32 | #include "llvm/IR/Constant.h" |
| 33 | #include "llvm/IR/DerivedTypes.h" |
| 34 | #include "llvm/IR/Function.h" |
| 35 | #include "llvm/IR/InstrTypes.h" |
| 36 | #include "llvm/IR/Instruction.h" |
| 37 | #include "llvm/IR/OperandTraits.h" |
| 38 | #include "llvm/IR/Type.h" |
| 39 | #include "llvm/IR/Use.h" |
| 40 | #include "llvm/IR/User.h" |
| 41 | #include "llvm/IR/Value.h" |
| 42 | #include "llvm/Support/AtomicOrdering.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include "llvm/Support/ErrorHandling.h" |
| 45 | #include <cassert> |
| 46 | #include <cstddef> |
| 47 | #include <cstdint> |
| 48 | #include <iterator> |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
| 52 | class APInt; |
| 53 | class ConstantInt; |
| 54 | class DataLayout; |
| 55 | class LLVMContext; |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // AllocaInst Class |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | /// an instruction to allocate memory on the stack |
| 62 | class AllocaInst : public UnaryInstruction { |
| 63 | Type *AllocatedType; |
| 64 | |
| 65 | using AlignmentField = AlignmentBitfieldElementT<0>; |
| 66 | using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>; |
| 67 | using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>; |
| 68 | static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField, |
| 69 | SwiftErrorField>(), |
| 70 | "Bitfields must be contiguous"); |
| 71 | |
| 72 | protected: |
| 73 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 74 | friend class Instruction; |
| 75 | |
| 76 | AllocaInst *cloneImpl() const; |
| 77 | |
| 78 | public: |
| 79 | explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 80 | const Twine &Name, Instruction *InsertBefore); |
| 81 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
| 82 | const Twine &Name, BasicBlock *InsertAtEnd); |
| 83 | |
| 84 | AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
| 85 | Instruction *InsertBefore); |
| 86 | AllocaInst(Type *Ty, unsigned AddrSpace, |
| 87 | const Twine &Name, BasicBlock *InsertAtEnd); |
| 88 | |
| 89 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, |
| 90 | const Twine &Name = "", Instruction *InsertBefore = nullptr); |
| 91 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, |
| 92 | const Twine &Name, BasicBlock *InsertAtEnd); |
| 93 | |
| 94 | /// Return true if there is an allocation size parameter to the allocation |
| 95 | /// instruction that is not 1. |
| 96 | bool isArrayAllocation() const; |
| 97 | |
| 98 | /// Get the number of elements allocated. For a simple allocation of a single |
| 99 | /// element, this will return a constant 1 value. |
| 100 | const Value *getArraySize() const { return getOperand(0); } |
| 101 | Value *getArraySize() { return getOperand(0); } |
| 102 | |
| 103 | /// Overload to return most specific pointer type. |
| 104 | PointerType *getType() const { |
| 105 | return cast<PointerType>(Instruction::getType()); |
| 106 | } |
| 107 | |
| 108 | /// Get allocation size in bits. Returns None if size can't be determined, |
| 109 | /// e.g. in case of a VLA. |
| 110 | Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const; |
| 111 | |
| 112 | /// Return the type that is being allocated by the instruction. |
| 113 | Type *getAllocatedType() const { return AllocatedType; } |
| 114 | /// for use only in special circumstances that need to generically |
| 115 | /// transform a whole instruction (eg: IR linking and vectorization). |
| 116 | void setAllocatedType(Type *Ty) { AllocatedType = Ty; } |
| 117 | |
| 118 | /// Return the alignment of the memory that is being allocated by the |
| 119 | /// instruction. |
| 120 | Align getAlign() const { |
| 121 | return Align(1ULL << getSubclassData<AlignmentField>()); |
| 122 | } |
| 123 | |
| 124 | void setAlignment(Align Align) { |
| 125 | setSubclassData<AlignmentField>(Log2(Align)); |
| 126 | } |
| 127 | |
| 128 | // FIXME: Remove this one transition to Align is over. |
| 129 | unsigned getAlignment() const { return getAlign().value(); } |
| 130 | |
| 131 | /// Return true if this alloca is in the entry block of the function and is a |
| 132 | /// constant size. If so, the code generator will fold it into the |
| 133 | /// prolog/epilog code, so it is basically free. |
| 134 | bool isStaticAlloca() const; |
| 135 | |
| 136 | /// Return true if this alloca is used as an inalloca argument to a call. Such |
| 137 | /// allocas are never considered static even if they are in the entry block. |
| 138 | bool isUsedWithInAlloca() const { |
| 139 | return getSubclassData<UsedWithInAllocaField>(); |
| 140 | } |
| 141 | |
| 142 | /// Specify whether this alloca is used to represent the arguments to a call. |
| 143 | void setUsedWithInAlloca(bool V) { |
| 144 | setSubclassData<UsedWithInAllocaField>(V); |
| 145 | } |
| 146 | |
| 147 | /// Return true if this alloca is used as a swifterror argument to a call. |
| 148 | bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); } |
| 149 | /// Specify whether this alloca is used to represent a swifterror. |
| 150 | void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); } |
| 151 | |
| 152 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 153 | static bool classof(const Instruction *I) { |
| 154 | return (I->getOpcode() == Instruction::Alloca); |
| 155 | } |
| 156 | static bool classof(const Value *V) { |
| 157 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 162 | // method so that subclasses cannot accidentally use it. |
| 163 | template <typename Bitfield> |
| 164 | void setSubclassData(typename Bitfield::Type Value) { |
| 165 | Instruction::setSubclassData<Bitfield>(Value); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | // LoadInst Class |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
| 173 | /// An instruction for reading from memory. This uses the SubclassData field in |
| 174 | /// Value to store whether or not the load is volatile. |
| 175 | class LoadInst : public UnaryInstruction { |
| 176 | using VolatileField = BoolBitfieldElementT<0>; |
| 177 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; |
| 178 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; |
| 179 | static_assert( |
| 180 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), |
| 181 | "Bitfields must be contiguous"); |
| 182 | |
| 183 | void AssertOK(); |
| 184 | |
| 185 | protected: |
| 186 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 187 | friend class Instruction; |
| 188 | |
| 189 | LoadInst *cloneImpl() const; |
| 190 | |
| 191 | public: |
| 192 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, |
| 193 | Instruction *InsertBefore); |
| 194 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 195 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 196 | Instruction *InsertBefore); |
| 197 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 198 | BasicBlock *InsertAtEnd); |
| 199 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 200 | Align Align, Instruction *InsertBefore = nullptr); |
| 201 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 202 | Align Align, BasicBlock *InsertAtEnd); |
| 203 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 204 | Align Align, AtomicOrdering Order, |
| 205 | SyncScope::ID SSID = SyncScope::System, |
| 206 | Instruction *InsertBefore = nullptr); |
| 207 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
| 208 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, |
| 209 | BasicBlock *InsertAtEnd); |
| 210 | |
| 211 | /// Return true if this is a load from a volatile memory location. |
| 212 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
| 213 | |
| 214 | /// Specify whether this is a volatile load or not. |
| 215 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
| 216 | |
| 217 | /// Return the alignment of the access that is being performed. |
| 218 | /// FIXME: Remove this function once transition to Align is over. |
| 219 | /// Use getAlign() instead. |
| 220 | unsigned getAlignment() const { return getAlign().value(); } |
| 221 | |
| 222 | /// Return the alignment of the access that is being performed. |
| 223 | Align getAlign() const { |
| 224 | return Align(1ULL << (getSubclassData<AlignmentField>())); |
| 225 | } |
| 226 | |
| 227 | void setAlignment(Align Align) { |
| 228 | setSubclassData<AlignmentField>(Log2(Align)); |
| 229 | } |
| 230 | |
| 231 | /// Returns the ordering constraint of this load instruction. |
| 232 | AtomicOrdering getOrdering() const { |
| 233 | return getSubclassData<OrderingField>(); |
| 234 | } |
| 235 | /// Sets the ordering constraint of this load instruction. May not be Release |
| 236 | /// or AcquireRelease. |
| 237 | void setOrdering(AtomicOrdering Ordering) { |
| 238 | setSubclassData<OrderingField>(Ordering); |
| 239 | } |
| 240 | |
| 241 | /// Returns the synchronization scope ID of this load instruction. |
| 242 | SyncScope::ID getSyncScopeID() const { |
| 243 | return SSID; |
| 244 | } |
| 245 | |
| 246 | /// Sets the synchronization scope ID of this load instruction. |
| 247 | void setSyncScopeID(SyncScope::ID SSID) { |
| 248 | this->SSID = SSID; |
| 249 | } |
| 250 | |
| 251 | /// Sets the ordering constraint and the synchronization scope ID of this load |
| 252 | /// instruction. |
| 253 | void setAtomic(AtomicOrdering Ordering, |
| 254 | SyncScope::ID SSID = SyncScope::System) { |
| 255 | setOrdering(Ordering); |
| 256 | setSyncScopeID(SSID); |
| 257 | } |
| 258 | |
| 259 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
| 260 | |
| 261 | bool isUnordered() const { |
| 262 | return (getOrdering() == AtomicOrdering::NotAtomic || |
| 263 | getOrdering() == AtomicOrdering::Unordered) && |
| 264 | !isVolatile(); |
| 265 | } |
| 266 | |
| 267 | Value *getPointerOperand() { return getOperand(0); } |
| 268 | const Value *getPointerOperand() const { return getOperand(0); } |
| 269 | static unsigned getPointerOperandIndex() { return 0U; } |
| 270 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
| 271 | |
| 272 | /// Returns the address space of the pointer operand. |
| 273 | unsigned getPointerAddressSpace() const { |
| 274 | return getPointerOperandType()->getPointerAddressSpace(); |
| 275 | } |
| 276 | |
| 277 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 278 | static bool classof(const Instruction *I) { |
| 279 | return I->getOpcode() == Instruction::Load; |
| 280 | } |
| 281 | static bool classof(const Value *V) { |
| 282 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 283 | } |
| 284 | |
| 285 | private: |
| 286 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 287 | // method so that subclasses cannot accidentally use it. |
| 288 | template <typename Bitfield> |
| 289 | void setSubclassData(typename Bitfield::Type Value) { |
| 290 | Instruction::setSubclassData<Bitfield>(Value); |
| 291 | } |
| 292 | |
| 293 | /// The synchronization scope ID of this load instruction. Not quite enough |
| 294 | /// room in SubClassData for everything, so synchronization scope ID gets its |
| 295 | /// own field. |
| 296 | SyncScope::ID SSID; |
| 297 | }; |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | // StoreInst Class |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
| 303 | /// An instruction for storing to memory. |
| 304 | class StoreInst : public Instruction { |
| 305 | using VolatileField = BoolBitfieldElementT<0>; |
| 306 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; |
| 307 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; |
| 308 | static_assert( |
| 309 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), |
| 310 | "Bitfields must be contiguous"); |
| 311 | |
| 312 | void AssertOK(); |
| 313 | |
| 314 | protected: |
| 315 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 316 | friend class Instruction; |
| 317 | |
| 318 | StoreInst *cloneImpl() const; |
| 319 | |
| 320 | public: |
| 321 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); |
| 322 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); |
| 323 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore); |
| 324 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); |
| 325 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
| 326 | Instruction *InsertBefore = nullptr); |
| 327 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
| 328 | BasicBlock *InsertAtEnd); |
| 329 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
| 330 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, |
| 331 | Instruction *InsertBefore = nullptr); |
| 332 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
| 333 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd); |
| 334 | |
| 335 | // allocate space for exactly two operands |
| 336 | void *operator new(size_t S) { return User::operator new(S, 2); } |
| 337 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 338 | |
| 339 | /// Return true if this is a store to a volatile memory location. |
| 340 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
| 341 | |
| 342 | /// Specify whether this is a volatile store or not. |
| 343 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
| 344 | |
| 345 | /// Transparently provide more efficient getOperand methods. |
| 346 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 347 | |
| 348 | /// Return the alignment of the access that is being performed |
| 349 | /// FIXME: Remove this function once transition to Align is over. |
| 350 | /// Use getAlign() instead. |
| 351 | unsigned getAlignment() const { return getAlign().value(); } |
| 352 | |
| 353 | Align getAlign() const { |
| 354 | return Align(1ULL << (getSubclassData<AlignmentField>())); |
| 355 | } |
| 356 | |
| 357 | void setAlignment(Align Align) { |
| 358 | setSubclassData<AlignmentField>(Log2(Align)); |
| 359 | } |
| 360 | |
| 361 | /// Returns the ordering constraint of this store instruction. |
| 362 | AtomicOrdering getOrdering() const { |
| 363 | return getSubclassData<OrderingField>(); |
| 364 | } |
| 365 | |
| 366 | /// Sets the ordering constraint of this store instruction. May not be |
| 367 | /// Acquire or AcquireRelease. |
| 368 | void setOrdering(AtomicOrdering Ordering) { |
| 369 | setSubclassData<OrderingField>(Ordering); |
| 370 | } |
| 371 | |
| 372 | /// Returns the synchronization scope ID of this store instruction. |
| 373 | SyncScope::ID getSyncScopeID() const { |
| 374 | return SSID; |
| 375 | } |
| 376 | |
| 377 | /// Sets the synchronization scope ID of this store instruction. |
| 378 | void setSyncScopeID(SyncScope::ID SSID) { |
| 379 | this->SSID = SSID; |
| 380 | } |
| 381 | |
| 382 | /// Sets the ordering constraint and the synchronization scope ID of this |
| 383 | /// store instruction. |
| 384 | void setAtomic(AtomicOrdering Ordering, |
| 385 | SyncScope::ID SSID = SyncScope::System) { |
| 386 | setOrdering(Ordering); |
| 387 | setSyncScopeID(SSID); |
| 388 | } |
| 389 | |
| 390 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
| 391 | |
| 392 | bool isUnordered() const { |
| 393 | return (getOrdering() == AtomicOrdering::NotAtomic || |
| 394 | getOrdering() == AtomicOrdering::Unordered) && |
| 395 | !isVolatile(); |
| 396 | } |
| 397 | |
| 398 | Value *getValueOperand() { return getOperand(0); } |
| 399 | const Value *getValueOperand() const { return getOperand(0); } |
| 400 | |
| 401 | Value *getPointerOperand() { return getOperand(1); } |
| 402 | const Value *getPointerOperand() const { return getOperand(1); } |
| 403 | static unsigned getPointerOperandIndex() { return 1U; } |
| 404 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
| 405 | |
| 406 | /// Returns the address space of the pointer operand. |
| 407 | unsigned getPointerAddressSpace() const { |
| 408 | return getPointerOperandType()->getPointerAddressSpace(); |
| 409 | } |
| 410 | |
| 411 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 412 | static bool classof(const Instruction *I) { |
| 413 | return I->getOpcode() == Instruction::Store; |
| 414 | } |
| 415 | static bool classof(const Value *V) { |
| 416 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 417 | } |
| 418 | |
| 419 | private: |
| 420 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 421 | // method so that subclasses cannot accidentally use it. |
| 422 | template <typename Bitfield> |
| 423 | void setSubclassData(typename Bitfield::Type Value) { |
| 424 | Instruction::setSubclassData<Bitfield>(Value); |
| 425 | } |
| 426 | |
| 427 | /// The synchronization scope ID of this store instruction. Not quite enough |
| 428 | /// room in SubClassData for everything, so synchronization scope ID gets its |
| 429 | /// own field. |
| 430 | SyncScope::ID SSID; |
| 431 | }; |
| 432 | |
| 433 | template <> |
| 434 | struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { |
| 435 | }; |
| 436 | |
| 437 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits <StoreInst>::op_begin(this); } StoreInst::const_op_iterator StoreInst::op_begin() const { return OperandTraits<StoreInst >::op_begin(const_cast<StoreInst*>(this)); } StoreInst ::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst >::op_end(this); } StoreInst::const_op_iterator StoreInst:: op_end() const { return OperandTraits<StoreInst>::op_end (const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand (unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<StoreInst>::op_begin(const_cast <StoreInst*>(this))[i_nocapture].get()); } void StoreInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<StoreInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned StoreInst::getNumOperands() const { return OperandTraits<StoreInst>::operands(this); } template <int Idx_nocapture> Use &StoreInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &StoreInst::Op() const { return this->OpFrom <Idx_nocapture>(this); } |
| 438 | |
| 439 | //===----------------------------------------------------------------------===// |
| 440 | // FenceInst Class |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
| 443 | /// An instruction for ordering other memory operations. |
| 444 | class FenceInst : public Instruction { |
| 445 | using OrderingField = AtomicOrderingBitfieldElementT<0>; |
| 446 | |
| 447 | void Init(AtomicOrdering Ordering, SyncScope::ID SSID); |
| 448 | |
| 449 | protected: |
| 450 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 451 | friend class Instruction; |
| 452 | |
| 453 | FenceInst *cloneImpl() const; |
| 454 | |
| 455 | public: |
| 456 | // Ordering may only be Acquire, Release, AcquireRelease, or |
| 457 | // SequentiallyConsistent. |
| 458 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 459 | SyncScope::ID SSID = SyncScope::System, |
| 460 | Instruction *InsertBefore = nullptr); |
| 461 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID, |
| 462 | BasicBlock *InsertAtEnd); |
| 463 | |
| 464 | // allocate space for exactly zero operands |
| 465 | void *operator new(size_t S) { return User::operator new(S, 0); } |
| 466 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 467 | |
| 468 | /// Returns the ordering constraint of this fence instruction. |
| 469 | AtomicOrdering getOrdering() const { |
| 470 | return getSubclassData<OrderingField>(); |
| 471 | } |
| 472 | |
| 473 | /// Sets the ordering constraint of this fence instruction. May only be |
| 474 | /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. |
| 475 | void setOrdering(AtomicOrdering Ordering) { |
| 476 | setSubclassData<OrderingField>(Ordering); |
| 477 | } |
| 478 | |
| 479 | /// Returns the synchronization scope ID of this fence instruction. |
| 480 | SyncScope::ID getSyncScopeID() const { |
| 481 | return SSID; |
| 482 | } |
| 483 | |
| 484 | /// Sets the synchronization scope ID of this fence instruction. |
| 485 | void setSyncScopeID(SyncScope::ID SSID) { |
| 486 | this->SSID = SSID; |
| 487 | } |
| 488 | |
| 489 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 490 | static bool classof(const Instruction *I) { |
| 491 | return I->getOpcode() == Instruction::Fence; |
| 492 | } |
| 493 | static bool classof(const Value *V) { |
| 494 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 495 | } |
| 496 | |
| 497 | private: |
| 498 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 499 | // method so that subclasses cannot accidentally use it. |
| 500 | template <typename Bitfield> |
| 501 | void setSubclassData(typename Bitfield::Type Value) { |
| 502 | Instruction::setSubclassData<Bitfield>(Value); |
| 503 | } |
| 504 | |
| 505 | /// The synchronization scope ID of this fence instruction. Not quite enough |
| 506 | /// room in SubClassData for everything, so synchronization scope ID gets its |
| 507 | /// own field. |
| 508 | SyncScope::ID SSID; |
| 509 | }; |
| 510 | |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | // AtomicCmpXchgInst Class |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | |
| 515 | /// An instruction that atomically checks whether a |
| 516 | /// specified value is in a memory location, and, if it is, stores a new value |
| 517 | /// there. The value returned by this instruction is a pair containing the |
| 518 | /// original value as first element, and an i1 indicating success (true) or |
| 519 | /// failure (false) as second element. |
| 520 | /// |
| 521 | class AtomicCmpXchgInst : public Instruction { |
| 522 | void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align, |
| 523 | AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, |
| 524 | SyncScope::ID SSID); |
| 525 | |
| 526 | template <unsigned Offset> |
| 527 | using AtomicOrderingBitfieldElement = |
| 528 | typename Bitfield::Element<AtomicOrdering, Offset, 3, |
| 529 | AtomicOrdering::LAST>; |
| 530 | |
| 531 | protected: |
| 532 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 533 | friend class Instruction; |
| 534 | |
| 535 | AtomicCmpXchgInst *cloneImpl() const; |
| 536 | |
| 537 | public: |
| 538 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, |
| 539 | AtomicOrdering SuccessOrdering, |
| 540 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, |
| 541 | Instruction *InsertBefore = nullptr); |
| 542 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, |
| 543 | AtomicOrdering SuccessOrdering, |
| 544 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, |
| 545 | BasicBlock *InsertAtEnd); |
| 546 | |
| 547 | // allocate space for exactly three operands |
| 548 | void *operator new(size_t S) { return User::operator new(S, 3); } |
| 549 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 550 | |
| 551 | using VolatileField = BoolBitfieldElementT<0>; |
| 552 | using WeakField = BoolBitfieldElementT<VolatileField::NextBit>; |
| 553 | using SuccessOrderingField = |
| 554 | AtomicOrderingBitfieldElementT<WeakField::NextBit>; |
| 555 | using FailureOrderingField = |
| 556 | AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>; |
| 557 | using AlignmentField = |
| 558 | AlignmentBitfieldElementT<FailureOrderingField::NextBit>; |
| 559 | static_assert( |
| 560 | Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField, |
| 561 | FailureOrderingField, AlignmentField>(), |
| 562 | "Bitfields must be contiguous"); |
| 563 | |
| 564 | /// Return the alignment of the memory that is being allocated by the |
| 565 | /// instruction. |
| 566 | Align getAlign() const { |
| 567 | return Align(1ULL << getSubclassData<AlignmentField>()); |
| 568 | } |
| 569 | |
| 570 | void setAlignment(Align Align) { |
| 571 | setSubclassData<AlignmentField>(Log2(Align)); |
| 572 | } |
| 573 | |
| 574 | /// Return true if this is a cmpxchg from a volatile memory |
| 575 | /// location. |
| 576 | /// |
| 577 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
| 578 | |
| 579 | /// Specify whether this is a volatile cmpxchg. |
| 580 | /// |
| 581 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
| 582 | |
| 583 | /// Return true if this cmpxchg may spuriously fail. |
| 584 | bool isWeak() const { return getSubclassData<WeakField>(); } |
| 585 | |
| 586 | void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); } |
| 587 | |
| 588 | /// Transparently provide more efficient getOperand methods. |
| 589 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 590 | |
| 591 | static bool isValidSuccessOrdering(AtomicOrdering Ordering) { |
| 592 | return Ordering != AtomicOrdering::NotAtomic && |
| 593 | Ordering != AtomicOrdering::Unordered; |
| 594 | } |
| 595 | |
| 596 | static bool isValidFailureOrdering(AtomicOrdering Ordering) { |
| 597 | return Ordering != AtomicOrdering::NotAtomic && |
| 598 | Ordering != AtomicOrdering::Unordered && |
| 599 | Ordering != AtomicOrdering::AcquireRelease && |
| 600 | Ordering != AtomicOrdering::Release; |
| 601 | } |
| 602 | |
| 603 | /// Returns the success ordering constraint of this cmpxchg instruction. |
| 604 | AtomicOrdering getSuccessOrdering() const { |
| 605 | return getSubclassData<SuccessOrderingField>(); |
| 606 | } |
| 607 | |
| 608 | /// Sets the success ordering constraint of this cmpxchg instruction. |
| 609 | void setSuccessOrdering(AtomicOrdering Ordering) { |
| 610 | assert(isValidSuccessOrdering(Ordering) &&((void)0) |
| 611 | "invalid CmpXchg success ordering")((void)0); |
| 612 | setSubclassData<SuccessOrderingField>(Ordering); |
| 613 | } |
| 614 | |
| 615 | /// Returns the failure ordering constraint of this cmpxchg instruction. |
| 616 | AtomicOrdering getFailureOrdering() const { |
| 617 | return getSubclassData<FailureOrderingField>(); |
| 618 | } |
| 619 | |
| 620 | /// Sets the failure ordering constraint of this cmpxchg instruction. |
| 621 | void setFailureOrdering(AtomicOrdering Ordering) { |
| 622 | assert(isValidFailureOrdering(Ordering) &&((void)0) |
| 623 | "invalid CmpXchg failure ordering")((void)0); |
| 624 | setSubclassData<FailureOrderingField>(Ordering); |
| 625 | } |
| 626 | |
| 627 | /// Returns a single ordering which is at least as strong as both the |
| 628 | /// success and failure orderings for this cmpxchg. |
| 629 | AtomicOrdering getMergedOrdering() const { |
| 630 | if (getFailureOrdering() == AtomicOrdering::SequentiallyConsistent) |
| 631 | return AtomicOrdering::SequentiallyConsistent; |
| 632 | if (getFailureOrdering() == AtomicOrdering::Acquire) { |
| 633 | if (getSuccessOrdering() == AtomicOrdering::Monotonic) |
| 634 | return AtomicOrdering::Acquire; |
| 635 | if (getSuccessOrdering() == AtomicOrdering::Release) |
| 636 | return AtomicOrdering::AcquireRelease; |
| 637 | } |
| 638 | return getSuccessOrdering(); |
| 639 | } |
| 640 | |
| 641 | /// Returns the synchronization scope ID of this cmpxchg instruction. |
| 642 | SyncScope::ID getSyncScopeID() const { |
| 643 | return SSID; |
| 644 | } |
| 645 | |
| 646 | /// Sets the synchronization scope ID of this cmpxchg instruction. |
| 647 | void setSyncScopeID(SyncScope::ID SSID) { |
| 648 | this->SSID = SSID; |
| 649 | } |
| 650 | |
| 651 | Value *getPointerOperand() { return getOperand(0); } |
| 652 | const Value *getPointerOperand() const { return getOperand(0); } |
| 653 | static unsigned getPointerOperandIndex() { return 0U; } |
| 654 | |
| 655 | Value *getCompareOperand() { return getOperand(1); } |
| 656 | const Value *getCompareOperand() const { return getOperand(1); } |
| 657 | |
| 658 | Value *getNewValOperand() { return getOperand(2); } |
| 659 | const Value *getNewValOperand() const { return getOperand(2); } |
| 660 | |
| 661 | /// Returns the address space of the pointer operand. |
| 662 | unsigned getPointerAddressSpace() const { |
| 663 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
| 664 | } |
| 665 | |
| 666 | /// Returns the strongest permitted ordering on failure, given the |
| 667 | /// desired ordering on success. |
| 668 | /// |
| 669 | /// If the comparison in a cmpxchg operation fails, there is no atomic store |
| 670 | /// so release semantics cannot be provided. So this function drops explicit |
| 671 | /// Release requests from the AtomicOrdering. A SequentiallyConsistent |
| 672 | /// operation would remain SequentiallyConsistent. |
| 673 | static AtomicOrdering |
| 674 | getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { |
| 675 | switch (SuccessOrdering) { |
| 676 | default: |
| 677 | llvm_unreachable("invalid cmpxchg success ordering")__builtin_unreachable(); |
| 678 | case AtomicOrdering::Release: |
| 679 | case AtomicOrdering::Monotonic: |
| 680 | return AtomicOrdering::Monotonic; |
| 681 | case AtomicOrdering::AcquireRelease: |
| 682 | case AtomicOrdering::Acquire: |
| 683 | return AtomicOrdering::Acquire; |
| 684 | case AtomicOrdering::SequentiallyConsistent: |
| 685 | return AtomicOrdering::SequentiallyConsistent; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 690 | static bool classof(const Instruction *I) { |
| 691 | return I->getOpcode() == Instruction::AtomicCmpXchg; |
| 692 | } |
| 693 | static bool classof(const Value *V) { |
| 694 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 695 | } |
| 696 | |
| 697 | private: |
| 698 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 699 | // method so that subclasses cannot accidentally use it. |
| 700 | template <typename Bitfield> |
| 701 | void setSubclassData(typename Bitfield::Type Value) { |
| 702 | Instruction::setSubclassData<Bitfield>(Value); |
| 703 | } |
| 704 | |
| 705 | /// The synchronization scope ID of this cmpxchg instruction. Not quite |
| 706 | /// enough room in SubClassData for everything, so synchronization scope ID |
| 707 | /// gets its own field. |
| 708 | SyncScope::ID SSID; |
| 709 | }; |
| 710 | |
| 711 | template <> |
| 712 | struct OperandTraits<AtomicCmpXchgInst> : |
| 713 | public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { |
| 714 | }; |
| 715 | |
| 716 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() { return OperandTraits<AtomicCmpXchgInst>::op_begin(this ); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst:: op_begin() const { return OperandTraits<AtomicCmpXchgInst> ::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst ::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits <AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst:: const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits <AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst *>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value >( OperandTraits<AtomicCmpXchgInst>::op_begin(const_cast <AtomicCmpXchgInst*>(this))[i_nocapture].get()); } void AtomicCmpXchgInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((void)0); OperandTraits<AtomicCmpXchgInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned AtomicCmpXchgInst ::getNumOperands() const { return OperandTraits<AtomicCmpXchgInst >::operands(this); } template <int Idx_nocapture> Use &AtomicCmpXchgInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & AtomicCmpXchgInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 717 | |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | // AtomicRMWInst Class |
| 720 | //===----------------------------------------------------------------------===// |
| 721 | |
| 722 | /// an instruction that atomically reads a memory location, |
| 723 | /// combines it with another value, and then stores the result back. Returns |
| 724 | /// the old value. |
| 725 | /// |
| 726 | class AtomicRMWInst : public Instruction { |
| 727 | protected: |
| 728 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 729 | friend class Instruction; |
| 730 | |
| 731 | AtomicRMWInst *cloneImpl() const; |
| 732 | |
| 733 | public: |
| 734 | /// This enumeration lists the possible modifications atomicrmw can make. In |
| 735 | /// the descriptions, 'p' is the pointer to the instruction's memory location, |
| 736 | /// 'old' is the initial value of *p, and 'v' is the other value passed to the |
| 737 | /// instruction. These instructions always return 'old'. |
| 738 | enum BinOp : unsigned { |
| 739 | /// *p = v |
| 740 | Xchg, |
| 741 | /// *p = old + v |
| 742 | Add, |
| 743 | /// *p = old - v |
| 744 | Sub, |
| 745 | /// *p = old & v |
| 746 | And, |
| 747 | /// *p = ~(old & v) |
| 748 | Nand, |
| 749 | /// *p = old | v |
| 750 | Or, |
| 751 | /// *p = old ^ v |
| 752 | Xor, |
| 753 | /// *p = old >signed v ? old : v |
| 754 | Max, |
| 755 | /// *p = old <signed v ? old : v |
| 756 | Min, |
| 757 | /// *p = old >unsigned v ? old : v |
| 758 | UMax, |
| 759 | /// *p = old <unsigned v ? old : v |
| 760 | UMin, |
| 761 | |
| 762 | /// *p = old + v |
| 763 | FAdd, |
| 764 | |
| 765 | /// *p = old - v |
| 766 | FSub, |
| 767 | |
| 768 | FIRST_BINOP = Xchg, |
| 769 | LAST_BINOP = FSub, |
| 770 | BAD_BINOP |
| 771 | }; |
| 772 | |
| 773 | private: |
| 774 | template <unsigned Offset> |
| 775 | using AtomicOrderingBitfieldElement = |
| 776 | typename Bitfield::Element<AtomicOrdering, Offset, 3, |
| 777 | AtomicOrdering::LAST>; |
| 778 | |
| 779 | template <unsigned Offset> |
| 780 | using BinOpBitfieldElement = |
| 781 | typename Bitfield::Element<BinOp, Offset, 4, BinOp::LAST_BINOP>; |
| 782 | |
| 783 | public: |
| 784 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, |
| 785 | AtomicOrdering Ordering, SyncScope::ID SSID, |
| 786 | Instruction *InsertBefore = nullptr); |
| 787 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, |
| 788 | AtomicOrdering Ordering, SyncScope::ID SSID, |
| 789 | BasicBlock *InsertAtEnd); |
| 790 | |
| 791 | // allocate space for exactly two operands |
| 792 | void *operator new(size_t S) { return User::operator new(S, 2); } |
| 793 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 794 | |
| 795 | using VolatileField = BoolBitfieldElementT<0>; |
| 796 | using AtomicOrderingField = |
| 797 | AtomicOrderingBitfieldElementT<VolatileField::NextBit>; |
| 798 | using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>; |
| 799 | using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>; |
| 800 | static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField, |
| 801 | OperationField, AlignmentField>(), |
| 802 | "Bitfields must be contiguous"); |
| 803 | |
| 804 | BinOp getOperation() const { return getSubclassData<OperationField>(); } |
| 805 | |
| 806 | static StringRef getOperationName(BinOp Op); |
| 807 | |
| 808 | static bool isFPOperation(BinOp Op) { |
| 809 | switch (Op) { |
| 810 | case AtomicRMWInst::FAdd: |
| 811 | case AtomicRMWInst::FSub: |
| 812 | return true; |
| 813 | default: |
| 814 | return false; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void setOperation(BinOp Operation) { |
| 819 | setSubclassData<OperationField>(Operation); |
| 820 | } |
| 821 | |
| 822 | /// Return the alignment of the memory that is being allocated by the |
| 823 | /// instruction. |
| 824 | Align getAlign() const { |
| 825 | return Align(1ULL << getSubclassData<AlignmentField>()); |
| 826 | } |
| 827 | |
| 828 | void setAlignment(Align Align) { |
| 829 | setSubclassData<AlignmentField>(Log2(Align)); |
| 830 | } |
| 831 | |
| 832 | /// Return true if this is a RMW on a volatile memory location. |
| 833 | /// |
| 834 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
| 835 | |
| 836 | /// Specify whether this is a volatile RMW or not. |
| 837 | /// |
| 838 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
| 839 | |
| 840 | /// Transparently provide more efficient getOperand methods. |
| 841 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 842 | |
| 843 | /// Returns the ordering constraint of this rmw instruction. |
| 844 | AtomicOrdering getOrdering() const { |
| 845 | return getSubclassData<AtomicOrderingField>(); |
| 846 | } |
| 847 | |
| 848 | /// Sets the ordering constraint of this rmw instruction. |
| 849 | void setOrdering(AtomicOrdering Ordering) { |
| 850 | assert(Ordering != AtomicOrdering::NotAtomic &&((void)0) |
| 851 | "atomicrmw instructions can only be atomic.")((void)0); |
| 852 | setSubclassData<AtomicOrderingField>(Ordering); |
| 853 | } |
| 854 | |
| 855 | /// Returns the synchronization scope ID of this rmw instruction. |
| 856 | SyncScope::ID getSyncScopeID() const { |
| 857 | return SSID; |
| 858 | } |
| 859 | |
| 860 | /// Sets the synchronization scope ID of this rmw instruction. |
| 861 | void setSyncScopeID(SyncScope::ID SSID) { |
| 862 | this->SSID = SSID; |
| 863 | } |
| 864 | |
| 865 | Value *getPointerOperand() { return getOperand(0); } |
| 866 | const Value *getPointerOperand() const { return getOperand(0); } |
| 867 | static unsigned getPointerOperandIndex() { return 0U; } |
| 868 | |
| 869 | Value *getValOperand() { return getOperand(1); } |
| 870 | const Value *getValOperand() const { return getOperand(1); } |
| 871 | |
| 872 | /// Returns the address space of the pointer operand. |
| 873 | unsigned getPointerAddressSpace() const { |
| 874 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
| 875 | } |
| 876 | |
| 877 | bool isFloatingPointOperation() const { |
| 878 | return isFPOperation(getOperation()); |
| 879 | } |
| 880 | |
| 881 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 882 | static bool classof(const Instruction *I) { |
| 883 | return I->getOpcode() == Instruction::AtomicRMW; |
| 884 | } |
| 885 | static bool classof(const Value *V) { |
| 886 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 887 | } |
| 888 | |
| 889 | private: |
| 890 | void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align, |
| 891 | AtomicOrdering Ordering, SyncScope::ID SSID); |
| 892 | |
| 893 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 894 | // method so that subclasses cannot accidentally use it. |
| 895 | template <typename Bitfield> |
| 896 | void setSubclassData(typename Bitfield::Type Value) { |
| 897 | Instruction::setSubclassData<Bitfield>(Value); |
| 898 | } |
| 899 | |
| 900 | /// The synchronization scope ID of this rmw instruction. Not quite enough |
| 901 | /// room in SubClassData for everything, so synchronization scope ID gets its |
| 902 | /// own field. |
| 903 | SyncScope::ID SSID; |
| 904 | }; |
| 905 | |
| 906 | template <> |
| 907 | struct OperandTraits<AtomicRMWInst> |
| 908 | : public FixedNumOperandTraits<AtomicRMWInst,2> { |
| 909 | }; |
| 910 | |
| 911 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst ::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits <AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*> (this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end() { return OperandTraits<AtomicRMWInst>::op_end(this); } AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const { return OperandTraits<AtomicRMWInst>::op_end(const_cast <AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand (unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<AtomicRMWInst>::op_begin(const_cast <AtomicRMWInst*>(this))[i_nocapture].get()); } void AtomicRMWInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<AtomicRMWInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned AtomicRMWInst::getNumOperands() const { return OperandTraits<AtomicRMWInst>::operands( this); } template <int Idx_nocapture> Use &AtomicRMWInst ::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &AtomicRMWInst ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } |
| 912 | |
| 913 | //===----------------------------------------------------------------------===// |
| 914 | // GetElementPtrInst Class |
| 915 | //===----------------------------------------------------------------------===// |
| 916 | |
| 917 | // checkGEPType - Simple wrapper function to give a better assertion failure |
| 918 | // message on bad indexes for a gep instruction. |
| 919 | // |
| 920 | inline Type *checkGEPType(Type *Ty) { |
| 921 | assert(Ty && "Invalid GetElementPtrInst indices for type!")((void)0); |
| 922 | return Ty; |
| 923 | } |
| 924 | |
| 925 | /// an instruction for type-safe pointer arithmetic to |
| 926 | /// access elements of arrays and structs |
| 927 | /// |
| 928 | class GetElementPtrInst : public Instruction { |
| 929 | Type *SourceElementType; |
| 930 | Type *ResultElementType; |
| 931 | |
| 932 | GetElementPtrInst(const GetElementPtrInst &GEPI); |
| 933 | |
| 934 | /// Constructors - Create a getelementptr instruction with a base pointer an |
| 935 | /// list of indices. The first ctor can optionally insert before an existing |
| 936 | /// instruction, the second appends the new instruction to the specified |
| 937 | /// BasicBlock. |
| 938 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
| 939 | ArrayRef<Value *> IdxList, unsigned Values, |
| 940 | const Twine &NameStr, Instruction *InsertBefore); |
| 941 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
| 942 | ArrayRef<Value *> IdxList, unsigned Values, |
| 943 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 944 | |
| 945 | void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); |
| 946 | |
| 947 | protected: |
| 948 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 949 | friend class Instruction; |
| 950 | |
| 951 | GetElementPtrInst *cloneImpl() const; |
| 952 | |
| 953 | public: |
| 954 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
| 955 | ArrayRef<Value *> IdxList, |
| 956 | const Twine &NameStr = "", |
| 957 | Instruction *InsertBefore = nullptr) { |
| 958 | unsigned Values = 1 + unsigned(IdxList.size()); |
| 959 | assert(PointeeType && "Must specify element type")((void)0); |
| 960 | assert(cast<PointerType>(Ptr->getType()->getScalarType())((void)0) |
| 961 | ->isOpaqueOrPointeeTypeMatches(PointeeType))((void)0); |
| 962 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
| 963 | NameStr, InsertBefore); |
| 964 | } |
| 965 | |
| 966 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
| 967 | ArrayRef<Value *> IdxList, |
| 968 | const Twine &NameStr, |
| 969 | BasicBlock *InsertAtEnd) { |
| 970 | unsigned Values = 1 + unsigned(IdxList.size()); |
| 971 | assert(PointeeType && "Must specify element type")((void)0); |
| 972 | assert(cast<PointerType>(Ptr->getType()->getScalarType())((void)0) |
| 973 | ->isOpaqueOrPointeeTypeMatches(PointeeType))((void)0); |
| 974 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
| 975 | NameStr, InsertAtEnd); |
| 976 | } |
| 977 | |
| 978 | LLVM_ATTRIBUTE_DEPRECATED(static GetElementPtrInst *CreateInBounds([[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
| 979 | Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr = "",[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
| 980 | Instruction *InsertBefore = nullptr),[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) |
| 981 | "Use the version with explicit element type instead")[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) { |
| 982 | return CreateInBounds( |
| 983 | Ptr->getType()->getScalarType()->getPointerElementType(), Ptr, IdxList, |
| 984 | NameStr, InsertBefore); |
| 985 | } |
| 986 | |
| 987 | /// Create an "inbounds" getelementptr. See the documentation for the |
| 988 | /// "inbounds" flag in LangRef.html for details. |
| 989 | static GetElementPtrInst * |
| 990 | CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, |
| 991 | const Twine &NameStr = "", |
| 992 | Instruction *InsertBefore = nullptr) { |
| 993 | GetElementPtrInst *GEP = |
| 994 | Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); |
| 995 | GEP->setIsInBounds(true); |
| 996 | return GEP; |
| 997 | } |
| 998 | |
| 999 | LLVM_ATTRIBUTE_DEPRECATED(static GetElementPtrInst *CreateInBounds([[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1000 | Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr,[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1001 | BasicBlock *InsertAtEnd),[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 1002 | "Use the version with explicit element type instead")[[deprecated("Use the version with explicit element type instead" )]] static GetElementPtrInst *CreateInBounds( Value *Ptr, ArrayRef <Value *> IdxList, const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 1003 | return CreateInBounds( |
| 1004 | Ptr->getType()->getScalarType()->getPointerElementType(), Ptr, IdxList, |
| 1005 | NameStr, InsertAtEnd); |
| 1006 | } |
| 1007 | |
| 1008 | static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, |
| 1009 | ArrayRef<Value *> IdxList, |
| 1010 | const Twine &NameStr, |
| 1011 | BasicBlock *InsertAtEnd) { |
| 1012 | GetElementPtrInst *GEP = |
| 1013 | Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); |
| 1014 | GEP->setIsInBounds(true); |
| 1015 | return GEP; |
| 1016 | } |
| 1017 | |
| 1018 | /// Transparently provide more efficient getOperand methods. |
| 1019 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 1020 | |
| 1021 | Type *getSourceElementType() const { return SourceElementType; } |
| 1022 | |
| 1023 | void setSourceElementType(Type *Ty) { SourceElementType = Ty; } |
| 1024 | void setResultElementType(Type *Ty) { ResultElementType = Ty; } |
| 1025 | |
| 1026 | Type *getResultElementType() const { |
| 1027 | assert(cast<PointerType>(getType()->getScalarType())((void)0) |
| 1028 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0); |
| 1029 | return ResultElementType; |
| 1030 | } |
| 1031 | |
| 1032 | /// Returns the address space of this instruction's pointer type. |
| 1033 | unsigned getAddressSpace() const { |
| 1034 | // Note that this is always the same as the pointer operand's address space |
| 1035 | // and that is cheaper to compute, so cheat here. |
| 1036 | return getPointerAddressSpace(); |
| 1037 | } |
| 1038 | |
| 1039 | /// Returns the result type of a getelementptr with the given source |
| 1040 | /// element type and indexes. |
| 1041 | /// |
| 1042 | /// Null is returned if the indices are invalid for the specified |
| 1043 | /// source element type. |
| 1044 | static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); |
| 1045 | static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); |
| 1046 | static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); |
| 1047 | |
| 1048 | /// Return the type of the element at the given index of an indexable |
| 1049 | /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})". |
| 1050 | /// |
| 1051 | /// Returns null if the type can't be indexed, or the given index is not |
| 1052 | /// legal for the given type. |
| 1053 | static Type *getTypeAtIndex(Type *Ty, Value *Idx); |
| 1054 | static Type *getTypeAtIndex(Type *Ty, uint64_t Idx); |
| 1055 | |
| 1056 | inline op_iterator idx_begin() { return op_begin()+1; } |
| 1057 | inline const_op_iterator idx_begin() const { return op_begin()+1; } |
| 1058 | inline op_iterator idx_end() { return op_end(); } |
| 1059 | inline const_op_iterator idx_end() const { return op_end(); } |
| 1060 | |
| 1061 | inline iterator_range<op_iterator> indices() { |
| 1062 | return make_range(idx_begin(), idx_end()); |
| 1063 | } |
| 1064 | |
| 1065 | inline iterator_range<const_op_iterator> indices() const { |
| 1066 | return make_range(idx_begin(), idx_end()); |
| 1067 | } |
| 1068 | |
| 1069 | Value *getPointerOperand() { |
| 1070 | return getOperand(0); |
| 1071 | } |
| 1072 | const Value *getPointerOperand() const { |
| 1073 | return getOperand(0); |
| 1074 | } |
| 1075 | static unsigned getPointerOperandIndex() { |
| 1076 | return 0U; // get index for modifying correct operand. |
| 1077 | } |
| 1078 | |
| 1079 | /// Method to return the pointer operand as a |
| 1080 | /// PointerType. |
| 1081 | Type *getPointerOperandType() const { |
| 1082 | return getPointerOperand()->getType(); |
| 1083 | } |
| 1084 | |
| 1085 | /// Returns the address space of the pointer operand. |
| 1086 | unsigned getPointerAddressSpace() const { |
| 1087 | return getPointerOperandType()->getPointerAddressSpace(); |
| 1088 | } |
| 1089 | |
| 1090 | /// Returns the pointer type returned by the GEP |
| 1091 | /// instruction, which may be a vector of pointers. |
| 1092 | static Type *getGEPReturnType(Type *ElTy, Value *Ptr, |
| 1093 | ArrayRef<Value *> IdxList) { |
| 1094 | PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType()); |
| 1095 | unsigned AddrSpace = OrigPtrTy->getAddressSpace(); |
| 1096 | Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList)); |
| 1097 | Type *PtrTy = OrigPtrTy->isOpaque() |
| 1098 | ? PointerType::get(OrigPtrTy->getContext(), AddrSpace) |
| 1099 | : PointerType::get(ResultElemTy, AddrSpace); |
| 1100 | // Vector GEP |
| 1101 | if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) { |
| 1102 | ElementCount EltCount = PtrVTy->getElementCount(); |
| 1103 | return VectorType::get(PtrTy, EltCount); |
| 1104 | } |
| 1105 | for (Value *Index : IdxList) |
| 1106 | if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) { |
| 1107 | ElementCount EltCount = IndexVTy->getElementCount(); |
| 1108 | return VectorType::get(PtrTy, EltCount); |
| 1109 | } |
| 1110 | // Scalar GEP |
| 1111 | return PtrTy; |
| 1112 | } |
| 1113 | |
| 1114 | unsigned getNumIndices() const { // Note: always non-negative |
| 1115 | return getNumOperands() - 1; |
| 1116 | } |
| 1117 | |
| 1118 | bool hasIndices() const { |
| 1119 | return getNumOperands() > 1; |
| 1120 | } |
| 1121 | |
| 1122 | /// Return true if all of the indices of this GEP are |
| 1123 | /// zeros. If so, the result pointer and the first operand have the same |
| 1124 | /// value, just potentially different types. |
| 1125 | bool hasAllZeroIndices() const; |
| 1126 | |
| 1127 | /// Return true if all of the indices of this GEP are |
| 1128 | /// constant integers. If so, the result pointer and the first operand have |
| 1129 | /// a constant offset between them. |
| 1130 | bool hasAllConstantIndices() const; |
| 1131 | |
| 1132 | /// Set or clear the inbounds flag on this GEP instruction. |
| 1133 | /// See LangRef.html for the meaning of inbounds on a getelementptr. |
| 1134 | void setIsInBounds(bool b = true); |
| 1135 | |
| 1136 | /// Determine whether the GEP has the inbounds flag. |
| 1137 | bool isInBounds() const; |
| 1138 | |
| 1139 | /// Accumulate the constant address offset of this GEP if possible. |
| 1140 | /// |
| 1141 | /// This routine accepts an APInt into which it will accumulate the constant |
| 1142 | /// offset of this GEP if the GEP is in fact constant. If the GEP is not |
| 1143 | /// all-constant, it returns false and the value of the offset APInt is |
| 1144 | /// undefined (it is *not* preserved!). The APInt passed into this routine |
| 1145 | /// must be at least as wide as the IntPtr type for the address space of |
| 1146 | /// the base GEP pointer. |
| 1147 | bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; |
| 1148 | bool collectOffset(const DataLayout &DL, unsigned BitWidth, |
| 1149 | MapVector<Value *, APInt> &VariableOffsets, |
| 1150 | APInt &ConstantOffset) const; |
| 1151 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1152 | static bool classof(const Instruction *I) { |
| 1153 | return (I->getOpcode() == Instruction::GetElementPtr); |
| 1154 | } |
| 1155 | static bool classof(const Value *V) { |
| 1156 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1157 | } |
| 1158 | }; |
| 1159 | |
| 1160 | template <> |
| 1161 | struct OperandTraits<GetElementPtrInst> : |
| 1162 | public VariadicOperandTraits<GetElementPtrInst, 1> { |
| 1163 | }; |
| 1164 | |
| 1165 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
| 1166 | ArrayRef<Value *> IdxList, unsigned Values, |
| 1167 | const Twine &NameStr, |
| 1168 | Instruction *InsertBefore) |
| 1169 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
| 1170 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
| 1171 | Values, InsertBefore), |
| 1172 | SourceElementType(PointeeType), |
| 1173 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
| 1174 | assert(cast<PointerType>(getType()->getScalarType())((void)0) |
| 1175 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0); |
| 1176 | init(Ptr, IdxList, NameStr); |
| 1177 | } |
| 1178 | |
| 1179 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
| 1180 | ArrayRef<Value *> IdxList, unsigned Values, |
| 1181 | const Twine &NameStr, |
| 1182 | BasicBlock *InsertAtEnd) |
| 1183 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
| 1184 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
| 1185 | Values, InsertAtEnd), |
| 1186 | SourceElementType(PointeeType), |
| 1187 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
| 1188 | assert(cast<PointerType>(getType()->getScalarType())((void)0) |
| 1189 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))((void)0); |
| 1190 | init(Ptr, IdxList, NameStr); |
| 1191 | } |
| 1192 | |
| 1193 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() { return OperandTraits<GetElementPtrInst>::op_begin(this ); } GetElementPtrInst::const_op_iterator GetElementPtrInst:: op_begin() const { return OperandTraits<GetElementPtrInst> ::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst ::op_iterator GetElementPtrInst::op_end() { return OperandTraits <GetElementPtrInst>::op_end(this); } GetElementPtrInst:: const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits <GetElementPtrInst>::op_end(const_cast<GetElementPtrInst *>(this)); } Value *GetElementPtrInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value >( OperandTraits<GetElementPtrInst>::op_begin(const_cast <GetElementPtrInst*>(this))[i_nocapture].get()); } void GetElementPtrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((void)0); OperandTraits<GetElementPtrInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned GetElementPtrInst ::getNumOperands() const { return OperandTraits<GetElementPtrInst >::operands(this); } template <int Idx_nocapture> Use &GetElementPtrInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & GetElementPtrInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 1194 | |
| 1195 | //===----------------------------------------------------------------------===// |
| 1196 | // ICmpInst Class |
| 1197 | //===----------------------------------------------------------------------===// |
| 1198 | |
| 1199 | /// This instruction compares its operands according to the predicate given |
| 1200 | /// to the constructor. It only operates on integers or pointers. The operands |
| 1201 | /// must be identical types. |
| 1202 | /// Represent an integer comparison operator. |
| 1203 | class ICmpInst: public CmpInst { |
| 1204 | void AssertOK() { |
| 1205 | assert(isIntPredicate() &&((void)0) |
| 1206 | "Invalid ICmp predicate value")((void)0); |
| 1207 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((void)0) |
| 1208 | "Both operands to ICmp instruction are not of the same type!")((void)0); |
| 1209 | // Check that the operands are the right type |
| 1210 | assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||((void)0) |
| 1211 | getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&((void)0) |
| 1212 | "Invalid operand types for ICmp instruction")((void)0); |
| 1213 | } |
| 1214 | |
| 1215 | protected: |
| 1216 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1217 | friend class Instruction; |
| 1218 | |
| 1219 | /// Clone an identical ICmpInst |
| 1220 | ICmpInst *cloneImpl() const; |
| 1221 | |
| 1222 | public: |
| 1223 | /// Constructor with insert-before-instruction semantics. |
| 1224 | ICmpInst( |
| 1225 | Instruction *InsertBefore, ///< Where to insert |
| 1226 | Predicate pred, ///< The predicate to use for the comparison |
| 1227 | Value *LHS, ///< The left-hand-side of the expression |
| 1228 | Value *RHS, ///< The right-hand-side of the expression |
| 1229 | const Twine &NameStr = "" ///< Name of the instruction |
| 1230 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
| 1231 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
| 1232 | InsertBefore) { |
| 1233 | #ifndef NDEBUG1 |
| 1234 | AssertOK(); |
| 1235 | #endif |
| 1236 | } |
| 1237 | |
| 1238 | /// Constructor with insert-at-end semantics. |
| 1239 | ICmpInst( |
| 1240 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
| 1241 | Predicate pred, ///< The predicate to use for the comparison |
| 1242 | Value *LHS, ///< The left-hand-side of the expression |
| 1243 | Value *RHS, ///< The right-hand-side of the expression |
| 1244 | const Twine &NameStr = "" ///< Name of the instruction |
| 1245 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
| 1246 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
| 1247 | &InsertAtEnd) { |
| 1248 | #ifndef NDEBUG1 |
| 1249 | AssertOK(); |
| 1250 | #endif |
| 1251 | } |
| 1252 | |
| 1253 | /// Constructor with no-insertion semantics |
| 1254 | ICmpInst( |
| 1255 | Predicate pred, ///< The predicate to use for the comparison |
| 1256 | Value *LHS, ///< The left-hand-side of the expression |
| 1257 | Value *RHS, ///< The right-hand-side of the expression |
| 1258 | const Twine &NameStr = "" ///< Name of the instruction |
| 1259 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
| 1260 | Instruction::ICmp, pred, LHS, RHS, NameStr) { |
| 1261 | #ifndef NDEBUG1 |
| 1262 | AssertOK(); |
| 1263 | #endif |
| 1264 | } |
| 1265 | |
| 1266 | /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. |
| 1267 | /// @returns the predicate that would be the result if the operand were |
| 1268 | /// regarded as signed. |
| 1269 | /// Return the signed version of the predicate |
| 1270 | Predicate getSignedPredicate() const { |
| 1271 | return getSignedPredicate(getPredicate()); |
| 1272 | } |
| 1273 | |
| 1274 | /// This is a static version that you can use without an instruction. |
| 1275 | /// Return the signed version of the predicate. |
| 1276 | static Predicate getSignedPredicate(Predicate pred); |
| 1277 | |
| 1278 | /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. |
| 1279 | /// @returns the predicate that would be the result if the operand were |
| 1280 | /// regarded as unsigned. |
| 1281 | /// Return the unsigned version of the predicate |
| 1282 | Predicate getUnsignedPredicate() const { |
| 1283 | return getUnsignedPredicate(getPredicate()); |
| 1284 | } |
| 1285 | |
| 1286 | /// This is a static version that you can use without an instruction. |
| 1287 | /// Return the unsigned version of the predicate. |
| 1288 | static Predicate getUnsignedPredicate(Predicate pred); |
| 1289 | |
| 1290 | /// Return true if this predicate is either EQ or NE. This also |
| 1291 | /// tests for commutativity. |
| 1292 | static bool isEquality(Predicate P) { |
| 1293 | return P == ICMP_EQ || P == ICMP_NE; |
| 1294 | } |
| 1295 | |
| 1296 | /// Return true if this predicate is either EQ or NE. This also |
| 1297 | /// tests for commutativity. |
| 1298 | bool isEquality() const { |
| 1299 | return isEquality(getPredicate()); |
| 1300 | } |
| 1301 | |
| 1302 | /// @returns true if the predicate of this ICmpInst is commutative |
| 1303 | /// Determine if this relation is commutative. |
| 1304 | bool isCommutative() const { return isEquality(); } |
| 1305 | |
| 1306 | /// Return true if the predicate is relational (not EQ or NE). |
| 1307 | /// |
| 1308 | bool isRelational() const { |
| 1309 | return !isEquality(); |
| 1310 | } |
| 1311 | |
| 1312 | /// Return true if the predicate is relational (not EQ or NE). |
| 1313 | /// |
| 1314 | static bool isRelational(Predicate P) { |
| 1315 | return !isEquality(P); |
| 1316 | } |
| 1317 | |
| 1318 | /// Return true if the predicate is SGT or UGT. |
| 1319 | /// |
| 1320 | static bool isGT(Predicate P) { |
| 1321 | return P == ICMP_SGT || P == ICMP_UGT; |
| 1322 | } |
| 1323 | |
| 1324 | /// Return true if the predicate is SLT or ULT. |
| 1325 | /// |
| 1326 | static bool isLT(Predicate P) { |
| 1327 | return P == ICMP_SLT || P == ICMP_ULT; |
| 1328 | } |
| 1329 | |
| 1330 | /// Return true if the predicate is SGE or UGE. |
| 1331 | /// |
| 1332 | static bool isGE(Predicate P) { |
| 1333 | return P == ICMP_SGE || P == ICMP_UGE; |
| 1334 | } |
| 1335 | |
| 1336 | /// Return true if the predicate is SLE or ULE. |
| 1337 | /// |
| 1338 | static bool isLE(Predicate P) { |
| 1339 | return P == ICMP_SLE || P == ICMP_ULE; |
| 1340 | } |
| 1341 | |
| 1342 | /// Exchange the two operands to this instruction in such a way that it does |
| 1343 | /// not modify the semantics of the instruction. The predicate value may be |
| 1344 | /// changed to retain the same result if the predicate is order dependent |
| 1345 | /// (e.g. ult). |
| 1346 | /// Swap operands and adjust predicate. |
| 1347 | void swapOperands() { |
| 1348 | setPredicate(getSwappedPredicate()); |
| 1349 | Op<0>().swap(Op<1>()); |
| 1350 | } |
| 1351 | |
| 1352 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1353 | static bool classof(const Instruction *I) { |
| 1354 | return I->getOpcode() == Instruction::ICmp; |
| 1355 | } |
| 1356 | static bool classof(const Value *V) { |
| 1357 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1358 | } |
| 1359 | }; |
| 1360 | |
| 1361 | //===----------------------------------------------------------------------===// |
| 1362 | // FCmpInst Class |
| 1363 | //===----------------------------------------------------------------------===// |
| 1364 | |
| 1365 | /// This instruction compares its operands according to the predicate given |
| 1366 | /// to the constructor. It only operates on floating point values or packed |
| 1367 | /// vectors of floating point values. The operands must be identical types. |
| 1368 | /// Represents a floating point comparison operator. |
| 1369 | class FCmpInst: public CmpInst { |
| 1370 | void AssertOK() { |
| 1371 | assert(isFPPredicate() && "Invalid FCmp predicate value")((void)0); |
| 1372 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((void)0) |
| 1373 | "Both operands to FCmp instruction are not of the same type!")((void)0); |
| 1374 | // Check that the operands are the right type |
| 1375 | assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&((void)0) |
| 1376 | "Invalid operand types for FCmp instruction")((void)0); |
| 1377 | } |
| 1378 | |
| 1379 | protected: |
| 1380 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1381 | friend class Instruction; |
| 1382 | |
| 1383 | /// Clone an identical FCmpInst |
| 1384 | FCmpInst *cloneImpl() const; |
| 1385 | |
| 1386 | public: |
| 1387 | /// Constructor with insert-before-instruction semantics. |
| 1388 | FCmpInst( |
| 1389 | Instruction *InsertBefore, ///< Where to insert |
| 1390 | Predicate pred, ///< The predicate to use for the comparison |
| 1391 | Value *LHS, ///< The left-hand-side of the expression |
| 1392 | Value *RHS, ///< The right-hand-side of the expression |
| 1393 | const Twine &NameStr = "" ///< Name of the instruction |
| 1394 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
| 1395 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
| 1396 | InsertBefore) { |
| 1397 | AssertOK(); |
| 1398 | } |
| 1399 | |
| 1400 | /// Constructor with insert-at-end semantics. |
| 1401 | FCmpInst( |
| 1402 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
| 1403 | Predicate pred, ///< The predicate to use for the comparison |
| 1404 | Value *LHS, ///< The left-hand-side of the expression |
| 1405 | Value *RHS, ///< The right-hand-side of the expression |
| 1406 | const Twine &NameStr = "" ///< Name of the instruction |
| 1407 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
| 1408 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
| 1409 | &InsertAtEnd) { |
| 1410 | AssertOK(); |
| 1411 | } |
| 1412 | |
| 1413 | /// Constructor with no-insertion semantics |
| 1414 | FCmpInst( |
| 1415 | Predicate Pred, ///< The predicate to use for the comparison |
| 1416 | Value *LHS, ///< The left-hand-side of the expression |
| 1417 | Value *RHS, ///< The right-hand-side of the expression |
| 1418 | const Twine &NameStr = "", ///< Name of the instruction |
| 1419 | Instruction *FlagsSource = nullptr |
| 1420 | ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS, |
| 1421 | RHS, NameStr, nullptr, FlagsSource) { |
| 1422 | AssertOK(); |
| 1423 | } |
| 1424 | |
| 1425 | /// @returns true if the predicate of this instruction is EQ or NE. |
| 1426 | /// Determine if this is an equality predicate. |
| 1427 | static bool isEquality(Predicate Pred) { |
| 1428 | return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || |
| 1429 | Pred == FCMP_UNE; |
| 1430 | } |
| 1431 | |
| 1432 | /// @returns true if the predicate of this instruction is EQ or NE. |
| 1433 | /// Determine if this is an equality predicate. |
| 1434 | bool isEquality() const { return isEquality(getPredicate()); } |
| 1435 | |
| 1436 | /// @returns true if the predicate of this instruction is commutative. |
| 1437 | /// Determine if this is a commutative predicate. |
| 1438 | bool isCommutative() const { |
| 1439 | return isEquality() || |
| 1440 | getPredicate() == FCMP_FALSE || |
| 1441 | getPredicate() == FCMP_TRUE || |
| 1442 | getPredicate() == FCMP_ORD || |
| 1443 | getPredicate() == FCMP_UNO; |
| 1444 | } |
| 1445 | |
| 1446 | /// @returns true if the predicate is relational (not EQ or NE). |
| 1447 | /// Determine if this a relational predicate. |
| 1448 | bool isRelational() const { return !isEquality(); } |
| 1449 | |
| 1450 | /// Exchange the two operands to this instruction in such a way that it does |
| 1451 | /// not modify the semantics of the instruction. The predicate value may be |
| 1452 | /// changed to retain the same result if the predicate is order dependent |
| 1453 | /// (e.g. ult). |
| 1454 | /// Swap operands and adjust predicate. |
| 1455 | void swapOperands() { |
| 1456 | setPredicate(getSwappedPredicate()); |
| 1457 | Op<0>().swap(Op<1>()); |
| 1458 | } |
| 1459 | |
| 1460 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1461 | static bool classof(const Instruction *I) { |
| 1462 | return I->getOpcode() == Instruction::FCmp; |
| 1463 | } |
| 1464 | static bool classof(const Value *V) { |
| 1465 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1466 | } |
| 1467 | }; |
| 1468 | |
| 1469 | //===----------------------------------------------------------------------===// |
| 1470 | /// This class represents a function call, abstracting a target |
| 1471 | /// machine's calling convention. This class uses low bit of the SubClassData |
| 1472 | /// field to indicate whether or not this is a tail call. The rest of the bits |
| 1473 | /// hold the calling convention of the call. |
| 1474 | /// |
| 1475 | class CallInst : public CallBase { |
| 1476 | CallInst(const CallInst &CI); |
| 1477 | |
| 1478 | /// Construct a CallInst given a range of arguments. |
| 1479 | /// Construct a CallInst from a range of arguments |
| 1480 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1481 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
| 1482 | Instruction *InsertBefore); |
| 1483 | |
| 1484 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1485 | const Twine &NameStr, Instruction *InsertBefore) |
| 1486 | : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {} |
| 1487 | |
| 1488 | /// Construct a CallInst given a range of arguments. |
| 1489 | /// Construct a CallInst from a range of arguments |
| 1490 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1491 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
| 1492 | BasicBlock *InsertAtEnd); |
| 1493 | |
| 1494 | explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, |
| 1495 | Instruction *InsertBefore); |
| 1496 | |
| 1497 | CallInst(FunctionType *ty, Value *F, const Twine &NameStr, |
| 1498 | BasicBlock *InsertAtEnd); |
| 1499 | |
| 1500 | void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
| 1501 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
| 1502 | void init(FunctionType *FTy, Value *Func, const Twine &NameStr); |
| 1503 | |
| 1504 | /// Compute the number of operands to allocate. |
| 1505 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
| 1506 | // We need one operand for the called function, plus the input operand |
| 1507 | // counts provided. |
| 1508 | return 1 + NumArgs + NumBundleInputs; |
| 1509 | } |
| 1510 | |
| 1511 | protected: |
| 1512 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1513 | friend class Instruction; |
| 1514 | |
| 1515 | CallInst *cloneImpl() const; |
| 1516 | |
| 1517 | public: |
| 1518 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", |
| 1519 | Instruction *InsertBefore = nullptr) { |
| 1520 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); |
| 1521 | } |
| 1522 | |
| 1523 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1524 | const Twine &NameStr, |
| 1525 | Instruction *InsertBefore = nullptr) { |
| 1526 | return new (ComputeNumOperands(Args.size())) |
| 1527 | CallInst(Ty, Func, Args, None, NameStr, InsertBefore); |
| 1528 | } |
| 1529 | |
| 1530 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1531 | ArrayRef<OperandBundleDef> Bundles = None, |
| 1532 | const Twine &NameStr = "", |
| 1533 | Instruction *InsertBefore = nullptr) { |
| 1534 | const int NumOperands = |
| 1535 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
| 1536 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 1537 | |
| 1538 | return new (NumOperands, DescriptorBytes) |
| 1539 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); |
| 1540 | } |
| 1541 | |
| 1542 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, |
| 1543 | BasicBlock *InsertAtEnd) { |
| 1544 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); |
| 1545 | } |
| 1546 | |
| 1547 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1548 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 1549 | return new (ComputeNumOperands(Args.size())) |
| 1550 | CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd); |
| 1551 | } |
| 1552 | |
| 1553 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1554 | ArrayRef<OperandBundleDef> Bundles, |
| 1555 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 1556 | const int NumOperands = |
| 1557 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
| 1558 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 1559 | |
| 1560 | return new (NumOperands, DescriptorBytes) |
| 1561 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); |
| 1562 | } |
| 1563 | |
| 1564 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "", |
| 1565 | Instruction *InsertBefore = nullptr) { |
| 1566 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
| 1567 | InsertBefore); |
| 1568 | } |
| 1569 | |
| 1570 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
| 1571 | ArrayRef<OperandBundleDef> Bundles = None, |
| 1572 | const Twine &NameStr = "", |
| 1573 | Instruction *InsertBefore = nullptr) { |
| 1574 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
| 1575 | NameStr, InsertBefore); |
| 1576 | } |
| 1577 | |
| 1578 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
| 1579 | const Twine &NameStr, |
| 1580 | Instruction *InsertBefore = nullptr) { |
| 1581 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
| 1582 | InsertBefore); |
| 1583 | } |
| 1584 | |
| 1585 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr, |
| 1586 | BasicBlock *InsertAtEnd) { |
| 1587 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
| 1588 | InsertAtEnd); |
| 1589 | } |
| 1590 | |
| 1591 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
| 1592 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 1593 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
| 1594 | InsertAtEnd); |
| 1595 | } |
| 1596 | |
| 1597 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
| 1598 | ArrayRef<OperandBundleDef> Bundles, |
| 1599 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 1600 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
| 1601 | NameStr, InsertAtEnd); |
| 1602 | } |
| 1603 | |
| 1604 | /// Create a clone of \p CI with a different set of operand bundles and |
| 1605 | /// insert it before \p InsertPt. |
| 1606 | /// |
| 1607 | /// The returned call instruction is identical \p CI in every way except that |
| 1608 | /// the operand bundles for the new instruction are set to the operand bundles |
| 1609 | /// in \p Bundles. |
| 1610 | static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, |
| 1611 | Instruction *InsertPt = nullptr); |
| 1612 | |
| 1613 | /// Generate the IR for a call to malloc: |
| 1614 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 1615 | /// possibly multiplied by the array size if the array size is not |
| 1616 | /// constant 1. |
| 1617 | /// 2. Call malloc with that argument. |
| 1618 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 1619 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, |
| 1620 | Type *AllocTy, Value *AllocSize, |
| 1621 | Value *ArraySize = nullptr, |
| 1622 | Function *MallocF = nullptr, |
| 1623 | const Twine &Name = ""); |
| 1624 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 1625 | Type *AllocTy, Value *AllocSize, |
| 1626 | Value *ArraySize = nullptr, |
| 1627 | Function *MallocF = nullptr, |
| 1628 | const Twine &Name = ""); |
| 1629 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, |
| 1630 | Type *AllocTy, Value *AllocSize, |
| 1631 | Value *ArraySize = nullptr, |
| 1632 | ArrayRef<OperandBundleDef> Bundles = None, |
| 1633 | Function *MallocF = nullptr, |
| 1634 | const Twine &Name = ""); |
| 1635 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 1636 | Type *AllocTy, Value *AllocSize, |
| 1637 | Value *ArraySize = nullptr, |
| 1638 | ArrayRef<OperandBundleDef> Bundles = None, |
| 1639 | Function *MallocF = nullptr, |
| 1640 | const Twine &Name = ""); |
| 1641 | /// Generate the IR for a call to the builtin free function. |
| 1642 | static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); |
| 1643 | static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); |
| 1644 | static Instruction *CreateFree(Value *Source, |
| 1645 | ArrayRef<OperandBundleDef> Bundles, |
| 1646 | Instruction *InsertBefore); |
| 1647 | static Instruction *CreateFree(Value *Source, |
| 1648 | ArrayRef<OperandBundleDef> Bundles, |
| 1649 | BasicBlock *InsertAtEnd); |
| 1650 | |
| 1651 | // Note that 'musttail' implies 'tail'. |
| 1652 | enum TailCallKind : unsigned { |
| 1653 | TCK_None = 0, |
| 1654 | TCK_Tail = 1, |
| 1655 | TCK_MustTail = 2, |
| 1656 | TCK_NoTail = 3, |
| 1657 | TCK_LAST = TCK_NoTail |
| 1658 | }; |
| 1659 | |
| 1660 | using TailCallKindField = Bitfield::Element<TailCallKind, 0, 2, TCK_LAST>; |
| 1661 | static_assert( |
| 1662 | Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(), |
| 1663 | "Bitfields must be contiguous"); |
| 1664 | |
| 1665 | TailCallKind getTailCallKind() const { |
| 1666 | return getSubclassData<TailCallKindField>(); |
| 1667 | } |
| 1668 | |
| 1669 | bool isTailCall() const { |
| 1670 | TailCallKind Kind = getTailCallKind(); |
| 1671 | return Kind == TCK_Tail || Kind == TCK_MustTail; |
| 1672 | } |
| 1673 | |
| 1674 | bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; } |
| 1675 | |
| 1676 | bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; } |
| 1677 | |
| 1678 | void setTailCallKind(TailCallKind TCK) { |
| 1679 | setSubclassData<TailCallKindField>(TCK); |
| 1680 | } |
| 1681 | |
| 1682 | void setTailCall(bool IsTc = true) { |
| 1683 | setTailCallKind(IsTc ? TCK_Tail : TCK_None); |
| 1684 | } |
| 1685 | |
| 1686 | /// Return true if the call can return twice |
| 1687 | bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); } |
| 1688 | void setCanReturnTwice() { |
| 1689 | addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice); |
| 1690 | } |
| 1691 | |
| 1692 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1693 | static bool classof(const Instruction *I) { |
| 1694 | return I->getOpcode() == Instruction::Call; |
| 1695 | } |
| 1696 | static bool classof(const Value *V) { |
| 1697 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1698 | } |
| 1699 | |
| 1700 | /// Updates profile metadata by scaling it by \p S / \p T. |
| 1701 | void updateProfWeight(uint64_t S, uint64_t T); |
| 1702 | |
| 1703 | private: |
| 1704 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 1705 | // method so that subclasses cannot accidentally use it. |
| 1706 | template <typename Bitfield> |
| 1707 | void setSubclassData(typename Bitfield::Type Value) { |
| 1708 | Instruction::setSubclassData<Bitfield>(Value); |
| 1709 | } |
| 1710 | }; |
| 1711 | |
| 1712 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1713 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
| 1714 | BasicBlock *InsertAtEnd) |
| 1715 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 1716 | OperandTraits<CallBase>::op_end(this) - |
| 1717 | (Args.size() + CountBundleInputs(Bundles) + 1), |
| 1718 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
| 1719 | InsertAtEnd) { |
| 1720 | init(Ty, Func, Args, Bundles, NameStr); |
| 1721 | } |
| 1722 | |
| 1723 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
| 1724 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
| 1725 | Instruction *InsertBefore) |
| 1726 | : CallBase(Ty->getReturnType(), Instruction::Call, |
| 1727 | OperandTraits<CallBase>::op_end(this) - |
| 1728 | (Args.size() + CountBundleInputs(Bundles) + 1), |
| 1729 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
| 1730 | InsertBefore) { |
| 1731 | init(Ty, Func, Args, Bundles, NameStr); |
| 1732 | } |
| 1733 | |
| 1734 | //===----------------------------------------------------------------------===// |
| 1735 | // SelectInst Class |
| 1736 | //===----------------------------------------------------------------------===// |
| 1737 | |
| 1738 | /// This class represents the LLVM 'select' instruction. |
| 1739 | /// |
| 1740 | class SelectInst : public Instruction { |
| 1741 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
| 1742 | Instruction *InsertBefore) |
| 1743 | : Instruction(S1->getType(), Instruction::Select, |
| 1744 | &Op<0>(), 3, InsertBefore) { |
| 1745 | init(C, S1, S2); |
| 1746 | setName(NameStr); |
| 1747 | } |
| 1748 | |
| 1749 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
| 1750 | BasicBlock *InsertAtEnd) |
| 1751 | : Instruction(S1->getType(), Instruction::Select, |
| 1752 | &Op<0>(), 3, InsertAtEnd) { |
| 1753 | init(C, S1, S2); |
| 1754 | setName(NameStr); |
| 1755 | } |
| 1756 | |
| 1757 | void init(Value *C, Value *S1, Value *S2) { |
| 1758 | assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")((void)0); |
| 1759 | Op<0>() = C; |
| 1760 | Op<1>() = S1; |
| 1761 | Op<2>() = S2; |
| 1762 | } |
| 1763 | |
| 1764 | protected: |
| 1765 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1766 | friend class Instruction; |
| 1767 | |
| 1768 | SelectInst *cloneImpl() const; |
| 1769 | |
| 1770 | public: |
| 1771 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
| 1772 | const Twine &NameStr = "", |
| 1773 | Instruction *InsertBefore = nullptr, |
| 1774 | Instruction *MDFrom = nullptr) { |
| 1775 | SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); |
| 1776 | if (MDFrom) |
| 1777 | Sel->copyMetadata(*MDFrom); |
| 1778 | return Sel; |
| 1779 | } |
| 1780 | |
| 1781 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
| 1782 | const Twine &NameStr, |
| 1783 | BasicBlock *InsertAtEnd) { |
| 1784 | return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); |
| 1785 | } |
| 1786 | |
| 1787 | const Value *getCondition() const { return Op<0>(); } |
| 1788 | const Value *getTrueValue() const { return Op<1>(); } |
| 1789 | const Value *getFalseValue() const { return Op<2>(); } |
| 1790 | Value *getCondition() { return Op<0>(); } |
| 1791 | Value *getTrueValue() { return Op<1>(); } |
| 1792 | Value *getFalseValue() { return Op<2>(); } |
| 1793 | |
| 1794 | void setCondition(Value *V) { Op<0>() = V; } |
| 1795 | void setTrueValue(Value *V) { Op<1>() = V; } |
| 1796 | void setFalseValue(Value *V) { Op<2>() = V; } |
| 1797 | |
| 1798 | /// Swap the true and false values of the select instruction. |
| 1799 | /// This doesn't swap prof metadata. |
| 1800 | void swapValues() { Op<1>().swap(Op<2>()); } |
| 1801 | |
| 1802 | /// Return a string if the specified operands are invalid |
| 1803 | /// for a select operation, otherwise return null. |
| 1804 | static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); |
| 1805 | |
| 1806 | /// Transparently provide more efficient getOperand methods. |
| 1807 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 1808 | |
| 1809 | OtherOps getOpcode() const { |
| 1810 | return static_cast<OtherOps>(Instruction::getOpcode()); |
| 1811 | } |
| 1812 | |
| 1813 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1814 | static bool classof(const Instruction *I) { |
| 1815 | return I->getOpcode() == Instruction::Select; |
| 1816 | } |
| 1817 | static bool classof(const Value *V) { |
| 1818 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1819 | } |
| 1820 | }; |
| 1821 | |
| 1822 | template <> |
| 1823 | struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { |
| 1824 | }; |
| 1825 | |
| 1826 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)SelectInst::op_iterator SelectInst::op_begin() { return OperandTraits <SelectInst>::op_begin(this); } SelectInst::const_op_iterator SelectInst::op_begin() const { return OperandTraits<SelectInst >::op_begin(const_cast<SelectInst*>(this)); } SelectInst ::op_iterator SelectInst::op_end() { return OperandTraits< SelectInst>::op_end(this); } SelectInst::const_op_iterator SelectInst::op_end() const { return OperandTraits<SelectInst >::op_end(const_cast<SelectInst*>(this)); } Value *SelectInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<SelectInst>::op_begin(const_cast <SelectInst*>(this))[i_nocapture].get()); } void SelectInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<SelectInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned SelectInst::getNumOperands() const { return OperandTraits<SelectInst>::operands(this); } template <int Idx_nocapture> Use &SelectInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &SelectInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 1827 | |
| 1828 | //===----------------------------------------------------------------------===// |
| 1829 | // VAArgInst Class |
| 1830 | //===----------------------------------------------------------------------===// |
| 1831 | |
| 1832 | /// This class represents the va_arg llvm instruction, which returns |
| 1833 | /// an argument of the specified type given a va_list and increments that list |
| 1834 | /// |
| 1835 | class VAArgInst : public UnaryInstruction { |
| 1836 | protected: |
| 1837 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1838 | friend class Instruction; |
| 1839 | |
| 1840 | VAArgInst *cloneImpl() const; |
| 1841 | |
| 1842 | public: |
| 1843 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", |
| 1844 | Instruction *InsertBefore = nullptr) |
| 1845 | : UnaryInstruction(Ty, VAArg, List, InsertBefore) { |
| 1846 | setName(NameStr); |
| 1847 | } |
| 1848 | |
| 1849 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr, |
| 1850 | BasicBlock *InsertAtEnd) |
| 1851 | : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { |
| 1852 | setName(NameStr); |
| 1853 | } |
| 1854 | |
| 1855 | Value *getPointerOperand() { return getOperand(0); } |
| 1856 | const Value *getPointerOperand() const { return getOperand(0); } |
| 1857 | static unsigned getPointerOperandIndex() { return 0U; } |
| 1858 | |
| 1859 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1860 | static bool classof(const Instruction *I) { |
| 1861 | return I->getOpcode() == VAArg; |
| 1862 | } |
| 1863 | static bool classof(const Value *V) { |
| 1864 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1865 | } |
| 1866 | }; |
| 1867 | |
| 1868 | //===----------------------------------------------------------------------===// |
| 1869 | // ExtractElementInst Class |
| 1870 | //===----------------------------------------------------------------------===// |
| 1871 | |
| 1872 | /// This instruction extracts a single (scalar) |
| 1873 | /// element from a VectorType value |
| 1874 | /// |
| 1875 | class ExtractElementInst : public Instruction { |
| 1876 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", |
| 1877 | Instruction *InsertBefore = nullptr); |
| 1878 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, |
| 1879 | BasicBlock *InsertAtEnd); |
| 1880 | |
| 1881 | protected: |
| 1882 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1883 | friend class Instruction; |
| 1884 | |
| 1885 | ExtractElementInst *cloneImpl() const; |
| 1886 | |
| 1887 | public: |
| 1888 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
| 1889 | const Twine &NameStr = "", |
| 1890 | Instruction *InsertBefore = nullptr) { |
| 1891 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); |
| 1892 | } |
| 1893 | |
| 1894 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
| 1895 | const Twine &NameStr, |
| 1896 | BasicBlock *InsertAtEnd) { |
| 1897 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); |
| 1898 | } |
| 1899 | |
| 1900 | /// Return true if an extractelement instruction can be |
| 1901 | /// formed with the specified operands. |
| 1902 | static bool isValidOperands(const Value *Vec, const Value *Idx); |
| 1903 | |
| 1904 | Value *getVectorOperand() { return Op<0>(); } |
| 1905 | Value *getIndexOperand() { return Op<1>(); } |
| 1906 | const Value *getVectorOperand() const { return Op<0>(); } |
| 1907 | const Value *getIndexOperand() const { return Op<1>(); } |
| 1908 | |
| 1909 | VectorType *getVectorOperandType() const { |
| 1910 | return cast<VectorType>(getVectorOperand()->getType()); |
| 1911 | } |
| 1912 | |
| 1913 | /// Transparently provide more efficient getOperand methods. |
| 1914 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 1915 | |
| 1916 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1917 | static bool classof(const Instruction *I) { |
| 1918 | return I->getOpcode() == Instruction::ExtractElement; |
| 1919 | } |
| 1920 | static bool classof(const Value *V) { |
| 1921 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1922 | } |
| 1923 | }; |
| 1924 | |
| 1925 | template <> |
| 1926 | struct OperandTraits<ExtractElementInst> : |
| 1927 | public FixedNumOperandTraits<ExtractElementInst, 2> { |
| 1928 | }; |
| 1929 | |
| 1930 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)ExtractElementInst::op_iterator ExtractElementInst::op_begin( ) { return OperandTraits<ExtractElementInst>::op_begin( this); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_begin() const { return OperandTraits<ExtractElementInst >::op_begin(const_cast<ExtractElementInst*>(this)); } ExtractElementInst::op_iterator ExtractElementInst::op_end() { return OperandTraits<ExtractElementInst>::op_end(this ); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_end() const { return OperandTraits<ExtractElementInst >::op_end(const_cast<ExtractElementInst*>(this)); } Value *ExtractElementInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value>( OperandTraits< ExtractElementInst>::op_begin(const_cast<ExtractElementInst *>(this))[i_nocapture].get()); } void ExtractElementInst:: setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void )0); OperandTraits<ExtractElementInst>::op_begin(this)[ i_nocapture] = Val_nocapture; } unsigned ExtractElementInst:: getNumOperands() const { return OperandTraits<ExtractElementInst >::operands(this); } template <int Idx_nocapture> Use &ExtractElementInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ExtractElementInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 1931 | |
| 1932 | //===----------------------------------------------------------------------===// |
| 1933 | // InsertElementInst Class |
| 1934 | //===----------------------------------------------------------------------===// |
| 1935 | |
| 1936 | /// This instruction inserts a single (scalar) |
| 1937 | /// element into a VectorType value |
| 1938 | /// |
| 1939 | class InsertElementInst : public Instruction { |
| 1940 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, |
| 1941 | const Twine &NameStr = "", |
| 1942 | Instruction *InsertBefore = nullptr); |
| 1943 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, |
| 1944 | BasicBlock *InsertAtEnd); |
| 1945 | |
| 1946 | protected: |
| 1947 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 1948 | friend class Instruction; |
| 1949 | |
| 1950 | InsertElementInst *cloneImpl() const; |
| 1951 | |
| 1952 | public: |
| 1953 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
| 1954 | const Twine &NameStr = "", |
| 1955 | Instruction *InsertBefore = nullptr) { |
| 1956 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); |
| 1957 | } |
| 1958 | |
| 1959 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
| 1960 | const Twine &NameStr, |
| 1961 | BasicBlock *InsertAtEnd) { |
| 1962 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); |
| 1963 | } |
| 1964 | |
| 1965 | /// Return true if an insertelement instruction can be |
| 1966 | /// formed with the specified operands. |
| 1967 | static bool isValidOperands(const Value *Vec, const Value *NewElt, |
| 1968 | const Value *Idx); |
| 1969 | |
| 1970 | /// Overload to return most specific vector type. |
| 1971 | /// |
| 1972 | VectorType *getType() const { |
| 1973 | return cast<VectorType>(Instruction::getType()); |
| 1974 | } |
| 1975 | |
| 1976 | /// Transparently provide more efficient getOperand methods. |
| 1977 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 1978 | |
| 1979 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 1980 | static bool classof(const Instruction *I) { |
| 1981 | return I->getOpcode() == Instruction::InsertElement; |
| 1982 | } |
| 1983 | static bool classof(const Value *V) { |
| 1984 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 1985 | } |
| 1986 | }; |
| 1987 | |
| 1988 | template <> |
| 1989 | struct OperandTraits<InsertElementInst> : |
| 1990 | public FixedNumOperandTraits<InsertElementInst, 3> { |
| 1991 | }; |
| 1992 | |
| 1993 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)InsertElementInst::op_iterator InsertElementInst::op_begin() { return OperandTraits<InsertElementInst>::op_begin(this ); } InsertElementInst::const_op_iterator InsertElementInst:: op_begin() const { return OperandTraits<InsertElementInst> ::op_begin(const_cast<InsertElementInst*>(this)); } InsertElementInst ::op_iterator InsertElementInst::op_end() { return OperandTraits <InsertElementInst>::op_end(this); } InsertElementInst:: const_op_iterator InsertElementInst::op_end() const { return OperandTraits <InsertElementInst>::op_end(const_cast<InsertElementInst *>(this)); } Value *InsertElementInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value >( OperandTraits<InsertElementInst>::op_begin(const_cast <InsertElementInst*>(this))[i_nocapture].get()); } void InsertElementInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((void)0); OperandTraits<InsertElementInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned InsertElementInst ::getNumOperands() const { return OperandTraits<InsertElementInst >::operands(this); } template <int Idx_nocapture> Use &InsertElementInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & InsertElementInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 1994 | |
| 1995 | //===----------------------------------------------------------------------===// |
| 1996 | // ShuffleVectorInst Class |
| 1997 | //===----------------------------------------------------------------------===// |
| 1998 | |
| 1999 | constexpr int UndefMaskElem = -1; |
| 2000 | |
| 2001 | /// This instruction constructs a fixed permutation of two |
| 2002 | /// input vectors. |
| 2003 | /// |
| 2004 | /// For each element of the result vector, the shuffle mask selects an element |
| 2005 | /// from one of the input vectors to copy to the result. Non-negative elements |
| 2006 | /// in the mask represent an index into the concatenated pair of input vectors. |
| 2007 | /// UndefMaskElem (-1) specifies that the result element is undefined. |
| 2008 | /// |
| 2009 | /// For scalable vectors, all the elements of the mask must be 0 or -1. This |
| 2010 | /// requirement may be relaxed in the future. |
| 2011 | class ShuffleVectorInst : public Instruction { |
| 2012 | SmallVector<int, 4> ShuffleMask; |
| 2013 | Constant *ShuffleMaskForBitcode; |
| 2014 | |
| 2015 | protected: |
| 2016 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2017 | friend class Instruction; |
| 2018 | |
| 2019 | ShuffleVectorInst *cloneImpl() const; |
| 2020 | |
| 2021 | public: |
| 2022 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 2023 | const Twine &NameStr = "", |
| 2024 | Instruction *InsertBefor = nullptr); |
| 2025 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 2026 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2027 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, |
| 2028 | const Twine &NameStr = "", |
| 2029 | Instruction *InsertBefor = nullptr); |
| 2030 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, |
| 2031 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2032 | |
| 2033 | void *operator new(size_t S) { return User::operator new(S, 2); } |
| 2034 | void operator delete(void *Ptr) { return User::operator delete(Ptr); } |
| 2035 | |
| 2036 | /// Swap the operands and adjust the mask to preserve the semantics |
| 2037 | /// of the instruction. |
| 2038 | void commute(); |
| 2039 | |
| 2040 | /// Return true if a shufflevector instruction can be |
| 2041 | /// formed with the specified operands. |
| 2042 | static bool isValidOperands(const Value *V1, const Value *V2, |
| 2043 | const Value *Mask); |
| 2044 | static bool isValidOperands(const Value *V1, const Value *V2, |
| 2045 | ArrayRef<int> Mask); |
| 2046 | |
| 2047 | /// Overload to return most specific vector type. |
| 2048 | /// |
| 2049 | VectorType *getType() const { |
| 2050 | return cast<VectorType>(Instruction::getType()); |
| 2051 | } |
| 2052 | |
| 2053 | /// Transparently provide more efficient getOperand methods. |
| 2054 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 2055 | |
| 2056 | /// Return the shuffle mask value of this instruction for the given element |
| 2057 | /// index. Return UndefMaskElem if the element is undef. |
| 2058 | int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; } |
| 2059 | |
| 2060 | /// Convert the input shuffle mask operand to a vector of integers. Undefined |
| 2061 | /// elements of the mask are returned as UndefMaskElem. |
| 2062 | static void getShuffleMask(const Constant *Mask, |
| 2063 | SmallVectorImpl<int> &Result); |
| 2064 | |
| 2065 | /// Return the mask for this instruction as a vector of integers. Undefined |
| 2066 | /// elements of the mask are returned as UndefMaskElem. |
| 2067 | void getShuffleMask(SmallVectorImpl<int> &Result) const { |
| 2068 | Result.assign(ShuffleMask.begin(), ShuffleMask.end()); |
| 2069 | } |
| 2070 | |
| 2071 | /// Return the mask for this instruction, for use in bitcode. |
| 2072 | /// |
| 2073 | /// TODO: This is temporary until we decide a new bitcode encoding for |
| 2074 | /// shufflevector. |
| 2075 | Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; } |
| 2076 | |
| 2077 | static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask, |
| 2078 | Type *ResultTy); |
| 2079 | |
| 2080 | void setShuffleMask(ArrayRef<int> Mask); |
| 2081 | |
| 2082 | ArrayRef<int> getShuffleMask() const { return ShuffleMask; } |
| 2083 | |
| 2084 | /// Return true if this shuffle returns a vector with a different number of |
| 2085 | /// elements than its source vectors. |
| 2086 | /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3> |
| 2087 | /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5> |
| 2088 | bool changesLength() const { |
| 2089 | unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType()) |
| 2090 | ->getElementCount() |
| 2091 | .getKnownMinValue(); |
| 2092 | unsigned NumMaskElts = ShuffleMask.size(); |
| 2093 | return NumSourceElts != NumMaskElts; |
| 2094 | } |
| 2095 | |
| 2096 | /// Return true if this shuffle returns a vector with a greater number of |
| 2097 | /// elements than its source vectors. |
| 2098 | /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3> |
| 2099 | bool increasesLength() const { |
| 2100 | unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType()) |
| 2101 | ->getElementCount() |
| 2102 | .getKnownMinValue(); |
| 2103 | unsigned NumMaskElts = ShuffleMask.size(); |
| 2104 | return NumSourceElts < NumMaskElts; |
| 2105 | } |
| 2106 | |
| 2107 | /// Return true if this shuffle mask chooses elements from exactly one source |
| 2108 | /// vector. |
| 2109 | /// Example: <7,5,undef,7> |
| 2110 | /// This assumes that vector operands are the same length as the mask. |
| 2111 | static bool isSingleSourceMask(ArrayRef<int> Mask); |
| 2112 | static bool isSingleSourceMask(const Constant *Mask) { |
| 2113 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2114 | SmallVector<int, 16> MaskAsInts; |
| 2115 | getShuffleMask(Mask, MaskAsInts); |
| 2116 | return isSingleSourceMask(MaskAsInts); |
| 2117 | } |
| 2118 | |
| 2119 | /// Return true if this shuffle chooses elements from exactly one source |
| 2120 | /// vector without changing the length of that vector. |
| 2121 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3> |
| 2122 | /// TODO: Optionally allow length-changing shuffles. |
| 2123 | bool isSingleSource() const { |
| 2124 | return !changesLength() && isSingleSourceMask(ShuffleMask); |
| 2125 | } |
| 2126 | |
| 2127 | /// Return true if this shuffle mask chooses elements from exactly one source |
| 2128 | /// vector without lane crossings. A shuffle using this mask is not |
| 2129 | /// necessarily a no-op because it may change the number of elements from its |
| 2130 | /// input vectors or it may provide demanded bits knowledge via undef lanes. |
| 2131 | /// Example: <undef,undef,2,3> |
| 2132 | static bool isIdentityMask(ArrayRef<int> Mask); |
| 2133 | static bool isIdentityMask(const Constant *Mask) { |
| 2134 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2135 | SmallVector<int, 16> MaskAsInts; |
| 2136 | getShuffleMask(Mask, MaskAsInts); |
| 2137 | return isIdentityMask(MaskAsInts); |
| 2138 | } |
| 2139 | |
| 2140 | /// Return true if this shuffle chooses elements from exactly one source |
| 2141 | /// vector without lane crossings and does not change the number of elements |
| 2142 | /// from its input vectors. |
| 2143 | /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef> |
| 2144 | bool isIdentity() const { |
| 2145 | return !changesLength() && isIdentityMask(ShuffleMask); |
| 2146 | } |
| 2147 | |
| 2148 | /// Return true if this shuffle lengthens exactly one source vector with |
| 2149 | /// undefs in the high elements. |
| 2150 | bool isIdentityWithPadding() const; |
| 2151 | |
| 2152 | /// Return true if this shuffle extracts the first N elements of exactly one |
| 2153 | /// source vector. |
| 2154 | bool isIdentityWithExtract() const; |
| 2155 | |
| 2156 | /// Return true if this shuffle concatenates its 2 source vectors. This |
| 2157 | /// returns false if either input is undefined. In that case, the shuffle is |
| 2158 | /// is better classified as an identity with padding operation. |
| 2159 | bool isConcat() const; |
| 2160 | |
| 2161 | /// Return true if this shuffle mask chooses elements from its source vectors |
| 2162 | /// without lane crossings. A shuffle using this mask would be |
| 2163 | /// equivalent to a vector select with a constant condition operand. |
| 2164 | /// Example: <4,1,6,undef> |
| 2165 | /// This returns false if the mask does not choose from both input vectors. |
| 2166 | /// In that case, the shuffle is better classified as an identity shuffle. |
| 2167 | /// This assumes that vector operands are the same length as the mask |
| 2168 | /// (a length-changing shuffle can never be equivalent to a vector select). |
| 2169 | static bool isSelectMask(ArrayRef<int> Mask); |
| 2170 | static bool isSelectMask(const Constant *Mask) { |
| 2171 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2172 | SmallVector<int, 16> MaskAsInts; |
| 2173 | getShuffleMask(Mask, MaskAsInts); |
| 2174 | return isSelectMask(MaskAsInts); |
| 2175 | } |
| 2176 | |
| 2177 | /// Return true if this shuffle chooses elements from its source vectors |
| 2178 | /// without lane crossings and all operands have the same number of elements. |
| 2179 | /// In other words, this shuffle is equivalent to a vector select with a |
| 2180 | /// constant condition operand. |
| 2181 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3> |
| 2182 | /// This returns false if the mask does not choose from both input vectors. |
| 2183 | /// In that case, the shuffle is better classified as an identity shuffle. |
| 2184 | /// TODO: Optionally allow length-changing shuffles. |
| 2185 | bool isSelect() const { |
| 2186 | return !changesLength() && isSelectMask(ShuffleMask); |
| 2187 | } |
| 2188 | |
| 2189 | /// Return true if this shuffle mask swaps the order of elements from exactly |
| 2190 | /// one source vector. |
| 2191 | /// Example: <7,6,undef,4> |
| 2192 | /// This assumes that vector operands are the same length as the mask. |
| 2193 | static bool isReverseMask(ArrayRef<int> Mask); |
| 2194 | static bool isReverseMask(const Constant *Mask) { |
| 2195 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2196 | SmallVector<int, 16> MaskAsInts; |
| 2197 | getShuffleMask(Mask, MaskAsInts); |
| 2198 | return isReverseMask(MaskAsInts); |
| 2199 | } |
| 2200 | |
| 2201 | /// Return true if this shuffle swaps the order of elements from exactly |
| 2202 | /// one source vector. |
| 2203 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef> |
| 2204 | /// TODO: Optionally allow length-changing shuffles. |
| 2205 | bool isReverse() const { |
| 2206 | return !changesLength() && isReverseMask(ShuffleMask); |
| 2207 | } |
| 2208 | |
| 2209 | /// Return true if this shuffle mask chooses all elements with the same value |
| 2210 | /// as the first element of exactly one source vector. |
| 2211 | /// Example: <4,undef,undef,4> |
| 2212 | /// This assumes that vector operands are the same length as the mask. |
| 2213 | static bool isZeroEltSplatMask(ArrayRef<int> Mask); |
| 2214 | static bool isZeroEltSplatMask(const Constant *Mask) { |
| 2215 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2216 | SmallVector<int, 16> MaskAsInts; |
| 2217 | getShuffleMask(Mask, MaskAsInts); |
| 2218 | return isZeroEltSplatMask(MaskAsInts); |
| 2219 | } |
| 2220 | |
| 2221 | /// Return true if all elements of this shuffle are the same value as the |
| 2222 | /// first element of exactly one source vector without changing the length |
| 2223 | /// of that vector. |
| 2224 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0> |
| 2225 | /// TODO: Optionally allow length-changing shuffles. |
| 2226 | /// TODO: Optionally allow splats from other elements. |
| 2227 | bool isZeroEltSplat() const { |
| 2228 | return !changesLength() && isZeroEltSplatMask(ShuffleMask); |
| 2229 | } |
| 2230 | |
| 2231 | /// Return true if this shuffle mask is a transpose mask. |
| 2232 | /// Transpose vector masks transpose a 2xn matrix. They read corresponding |
| 2233 | /// even- or odd-numbered vector elements from two n-dimensional source |
| 2234 | /// vectors and write each result into consecutive elements of an |
| 2235 | /// n-dimensional destination vector. Two shuffles are necessary to complete |
| 2236 | /// the transpose, one for the even elements and another for the odd elements. |
| 2237 | /// This description closely follows how the TRN1 and TRN2 AArch64 |
| 2238 | /// instructions operate. |
| 2239 | /// |
| 2240 | /// For example, a simple 2x2 matrix can be transposed with: |
| 2241 | /// |
| 2242 | /// ; Original matrix |
| 2243 | /// m0 = < a, b > |
| 2244 | /// m1 = < c, d > |
| 2245 | /// |
| 2246 | /// ; Transposed matrix |
| 2247 | /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 > |
| 2248 | /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 > |
| 2249 | /// |
| 2250 | /// For matrices having greater than n columns, the resulting nx2 transposed |
| 2251 | /// matrix is stored in two result vectors such that one vector contains |
| 2252 | /// interleaved elements from all the even-numbered rows and the other vector |
| 2253 | /// contains interleaved elements from all the odd-numbered rows. For example, |
| 2254 | /// a 2x4 matrix can be transposed with: |
| 2255 | /// |
| 2256 | /// ; Original matrix |
| 2257 | /// m0 = < a, b, c, d > |
| 2258 | /// m1 = < e, f, g, h > |
| 2259 | /// |
| 2260 | /// ; Transposed matrix |
| 2261 | /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 > |
| 2262 | /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 > |
| 2263 | static bool isTransposeMask(ArrayRef<int> Mask); |
| 2264 | static bool isTransposeMask(const Constant *Mask) { |
| 2265 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2266 | SmallVector<int, 16> MaskAsInts; |
| 2267 | getShuffleMask(Mask, MaskAsInts); |
| 2268 | return isTransposeMask(MaskAsInts); |
| 2269 | } |
| 2270 | |
| 2271 | /// Return true if this shuffle transposes the elements of its inputs without |
| 2272 | /// changing the length of the vectors. This operation may also be known as a |
| 2273 | /// merge or interleave. See the description for isTransposeMask() for the |
| 2274 | /// exact specification. |
| 2275 | /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6> |
| 2276 | bool isTranspose() const { |
| 2277 | return !changesLength() && isTransposeMask(ShuffleMask); |
| 2278 | } |
| 2279 | |
| 2280 | /// Return true if this shuffle mask is an extract subvector mask. |
| 2281 | /// A valid extract subvector mask returns a smaller vector from a single |
| 2282 | /// source operand. The base extraction index is returned as well. |
| 2283 | static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, |
| 2284 | int &Index); |
| 2285 | static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, |
| 2286 | int &Index) { |
| 2287 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((void)0); |
| 2288 | // Not possible to express a shuffle mask for a scalable vector for this |
| 2289 | // case. |
| 2290 | if (isa<ScalableVectorType>(Mask->getType())) |
| 2291 | return false; |
| 2292 | SmallVector<int, 16> MaskAsInts; |
| 2293 | getShuffleMask(Mask, MaskAsInts); |
| 2294 | return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index); |
| 2295 | } |
| 2296 | |
| 2297 | /// Return true if this shuffle mask is an extract subvector mask. |
| 2298 | bool isExtractSubvectorMask(int &Index) const { |
| 2299 | // Not possible to express a shuffle mask for a scalable vector for this |
| 2300 | // case. |
| 2301 | if (isa<ScalableVectorType>(getType())) |
| 2302 | return false; |
| 2303 | |
| 2304 | int NumSrcElts = |
| 2305 | cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); |
| 2306 | return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index); |
| 2307 | } |
| 2308 | |
| 2309 | /// Change values in a shuffle permute mask assuming the two vector operands |
| 2310 | /// of length InVecNumElts have swapped position. |
| 2311 | static void commuteShuffleMask(MutableArrayRef<int> Mask, |
| 2312 | unsigned InVecNumElts) { |
| 2313 | for (int &Idx : Mask) { |
| 2314 | if (Idx == -1) |
| 2315 | continue; |
| 2316 | Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; |
| 2317 | assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&((void)0) |
| 2318 | "shufflevector mask index out of range")((void)0); |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 2323 | static bool classof(const Instruction *I) { |
| 2324 | return I->getOpcode() == Instruction::ShuffleVector; |
| 2325 | } |
| 2326 | static bool classof(const Value *V) { |
| 2327 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 2328 | } |
| 2329 | }; |
| 2330 | |
| 2331 | template <> |
| 2332 | struct OperandTraits<ShuffleVectorInst> |
| 2333 | : public FixedNumOperandTraits<ShuffleVectorInst, 2> {}; |
| 2334 | |
| 2335 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)ShuffleVectorInst::op_iterator ShuffleVectorInst::op_begin() { return OperandTraits<ShuffleVectorInst>::op_begin(this ); } ShuffleVectorInst::const_op_iterator ShuffleVectorInst:: op_begin() const { return OperandTraits<ShuffleVectorInst> ::op_begin(const_cast<ShuffleVectorInst*>(this)); } ShuffleVectorInst ::op_iterator ShuffleVectorInst::op_end() { return OperandTraits <ShuffleVectorInst>::op_end(this); } ShuffleVectorInst:: const_op_iterator ShuffleVectorInst::op_end() const { return OperandTraits <ShuffleVectorInst>::op_end(const_cast<ShuffleVectorInst *>(this)); } Value *ShuffleVectorInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value >( OperandTraits<ShuffleVectorInst>::op_begin(const_cast <ShuffleVectorInst*>(this))[i_nocapture].get()); } void ShuffleVectorInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((void)0); OperandTraits<ShuffleVectorInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned ShuffleVectorInst ::getNumOperands() const { return OperandTraits<ShuffleVectorInst >::operands(this); } template <int Idx_nocapture> Use &ShuffleVectorInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ShuffleVectorInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 2336 | |
| 2337 | //===----------------------------------------------------------------------===// |
| 2338 | // ExtractValueInst Class |
| 2339 | //===----------------------------------------------------------------------===// |
| 2340 | |
| 2341 | /// This instruction extracts a struct member or array |
| 2342 | /// element value from an aggregate value. |
| 2343 | /// |
| 2344 | class ExtractValueInst : public UnaryInstruction { |
| 2345 | SmallVector<unsigned, 4> Indices; |
| 2346 | |
| 2347 | ExtractValueInst(const ExtractValueInst &EVI); |
| 2348 | |
| 2349 | /// Constructors - Create a extractvalue instruction with a base aggregate |
| 2350 | /// value and a list of indices. The first ctor can optionally insert before |
| 2351 | /// an existing instruction, the second appends the new instruction to the |
| 2352 | /// specified BasicBlock. |
| 2353 | inline ExtractValueInst(Value *Agg, |
| 2354 | ArrayRef<unsigned> Idxs, |
| 2355 | const Twine &NameStr, |
| 2356 | Instruction *InsertBefore); |
| 2357 | inline ExtractValueInst(Value *Agg, |
| 2358 | ArrayRef<unsigned> Idxs, |
| 2359 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2360 | |
| 2361 | void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); |
| 2362 | |
| 2363 | protected: |
| 2364 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2365 | friend class Instruction; |
| 2366 | |
| 2367 | ExtractValueInst *cloneImpl() const; |
| 2368 | |
| 2369 | public: |
| 2370 | static ExtractValueInst *Create(Value *Agg, |
| 2371 | ArrayRef<unsigned> Idxs, |
| 2372 | const Twine &NameStr = "", |
| 2373 | Instruction *InsertBefore = nullptr) { |
| 2374 | return new |
| 2375 | ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); |
| 2376 | } |
| 2377 | |
| 2378 | static ExtractValueInst *Create(Value *Agg, |
| 2379 | ArrayRef<unsigned> Idxs, |
| 2380 | const Twine &NameStr, |
| 2381 | BasicBlock *InsertAtEnd) { |
| 2382 | return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); |
| 2383 | } |
| 2384 | |
| 2385 | /// Returns the type of the element that would be extracted |
| 2386 | /// with an extractvalue instruction with the specified parameters. |
| 2387 | /// |
| 2388 | /// Null is returned if the indices are invalid for the specified type. |
| 2389 | static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); |
| 2390 | |
| 2391 | using idx_iterator = const unsigned*; |
| 2392 | |
| 2393 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
| 2394 | inline idx_iterator idx_end() const { return Indices.end(); } |
| 2395 | inline iterator_range<idx_iterator> indices() const { |
| 2396 | return make_range(idx_begin(), idx_end()); |
| 2397 | } |
| 2398 | |
| 2399 | Value *getAggregateOperand() { |
| 2400 | return getOperand(0); |
| 2401 | } |
| 2402 | const Value *getAggregateOperand() const { |
| 2403 | return getOperand(0); |
| 2404 | } |
| 2405 | static unsigned getAggregateOperandIndex() { |
| 2406 | return 0U; // get index for modifying correct operand |
| 2407 | } |
| 2408 | |
| 2409 | ArrayRef<unsigned> getIndices() const { |
| 2410 | return Indices; |
| 2411 | } |
| 2412 | |
| 2413 | unsigned getNumIndices() const { |
| 2414 | return (unsigned)Indices.size(); |
| 2415 | } |
| 2416 | |
| 2417 | bool hasIndices() const { |
| 2418 | return true; |
| 2419 | } |
| 2420 | |
| 2421 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 2422 | static bool classof(const Instruction *I) { |
| 2423 | return I->getOpcode() == Instruction::ExtractValue; |
| 2424 | } |
| 2425 | static bool classof(const Value *V) { |
| 2426 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 2427 | } |
| 2428 | }; |
| 2429 | |
| 2430 | ExtractValueInst::ExtractValueInst(Value *Agg, |
| 2431 | ArrayRef<unsigned> Idxs, |
| 2432 | const Twine &NameStr, |
| 2433 | Instruction *InsertBefore) |
| 2434 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
| 2435 | ExtractValue, Agg, InsertBefore) { |
| 2436 | init(Idxs, NameStr); |
| 2437 | } |
| 2438 | |
| 2439 | ExtractValueInst::ExtractValueInst(Value *Agg, |
| 2440 | ArrayRef<unsigned> Idxs, |
| 2441 | const Twine &NameStr, |
| 2442 | BasicBlock *InsertAtEnd) |
| 2443 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
| 2444 | ExtractValue, Agg, InsertAtEnd) { |
| 2445 | init(Idxs, NameStr); |
| 2446 | } |
| 2447 | |
| 2448 | //===----------------------------------------------------------------------===// |
| 2449 | // InsertValueInst Class |
| 2450 | //===----------------------------------------------------------------------===// |
| 2451 | |
| 2452 | /// This instruction inserts a struct field of array element |
| 2453 | /// value into an aggregate value. |
| 2454 | /// |
| 2455 | class InsertValueInst : public Instruction { |
| 2456 | SmallVector<unsigned, 4> Indices; |
| 2457 | |
| 2458 | InsertValueInst(const InsertValueInst &IVI); |
| 2459 | |
| 2460 | /// Constructors - Create a insertvalue instruction with a base aggregate |
| 2461 | /// value, a value to insert, and a list of indices. The first ctor can |
| 2462 | /// optionally insert before an existing instruction, the second appends |
| 2463 | /// the new instruction to the specified BasicBlock. |
| 2464 | inline InsertValueInst(Value *Agg, Value *Val, |
| 2465 | ArrayRef<unsigned> Idxs, |
| 2466 | const Twine &NameStr, |
| 2467 | Instruction *InsertBefore); |
| 2468 | inline InsertValueInst(Value *Agg, Value *Val, |
| 2469 | ArrayRef<unsigned> Idxs, |
| 2470 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2471 | |
| 2472 | /// Constructors - These two constructors are convenience methods because one |
| 2473 | /// and two index insertvalue instructions are so common. |
| 2474 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, |
| 2475 | const Twine &NameStr = "", |
| 2476 | Instruction *InsertBefore = nullptr); |
| 2477 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, |
| 2478 | BasicBlock *InsertAtEnd); |
| 2479 | |
| 2480 | void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 2481 | const Twine &NameStr); |
| 2482 | |
| 2483 | protected: |
| 2484 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2485 | friend class Instruction; |
| 2486 | |
| 2487 | InsertValueInst *cloneImpl() const; |
| 2488 | |
| 2489 | public: |
| 2490 | // allocate space for exactly two operands |
| 2491 | void *operator new(size_t S) { return User::operator new(S, 2); } |
| 2492 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 2493 | |
| 2494 | static InsertValueInst *Create(Value *Agg, Value *Val, |
| 2495 | ArrayRef<unsigned> Idxs, |
| 2496 | const Twine &NameStr = "", |
| 2497 | Instruction *InsertBefore = nullptr) { |
| 2498 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); |
| 2499 | } |
| 2500 | |
| 2501 | static InsertValueInst *Create(Value *Agg, Value *Val, |
| 2502 | ArrayRef<unsigned> Idxs, |
| 2503 | const Twine &NameStr, |
| 2504 | BasicBlock *InsertAtEnd) { |
| 2505 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); |
| 2506 | } |
| 2507 | |
| 2508 | /// Transparently provide more efficient getOperand methods. |
| 2509 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 2510 | |
| 2511 | using idx_iterator = const unsigned*; |
| 2512 | |
| 2513 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
| 2514 | inline idx_iterator idx_end() const { return Indices.end(); } |
| 2515 | inline iterator_range<idx_iterator> indices() const { |
| 2516 | return make_range(idx_begin(), idx_end()); |
| 2517 | } |
| 2518 | |
| 2519 | Value *getAggregateOperand() { |
| 2520 | return getOperand(0); |
| 2521 | } |
| 2522 | const Value *getAggregateOperand() const { |
| 2523 | return getOperand(0); |
| 2524 | } |
| 2525 | static unsigned getAggregateOperandIndex() { |
| 2526 | return 0U; // get index for modifying correct operand |
| 2527 | } |
| 2528 | |
| 2529 | Value *getInsertedValueOperand() { |
| 2530 | return getOperand(1); |
| 2531 | } |
| 2532 | const Value *getInsertedValueOperand() const { |
| 2533 | return getOperand(1); |
| 2534 | } |
| 2535 | static unsigned getInsertedValueOperandIndex() { |
| 2536 | return 1U; // get index for modifying correct operand |
| 2537 | } |
| 2538 | |
| 2539 | ArrayRef<unsigned> getIndices() const { |
| 2540 | return Indices; |
| 2541 | } |
| 2542 | |
| 2543 | unsigned getNumIndices() const { |
| 2544 | return (unsigned)Indices.size(); |
| 2545 | } |
| 2546 | |
| 2547 | bool hasIndices() const { |
| 2548 | return true; |
| 2549 | } |
| 2550 | |
| 2551 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 2552 | static bool classof(const Instruction *I) { |
| 2553 | return I->getOpcode() == Instruction::InsertValue; |
| 2554 | } |
| 2555 | static bool classof(const Value *V) { |
| 2556 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 2557 | } |
| 2558 | }; |
| 2559 | |
| 2560 | template <> |
| 2561 | struct OperandTraits<InsertValueInst> : |
| 2562 | public FixedNumOperandTraits<InsertValueInst, 2> { |
| 2563 | }; |
| 2564 | |
| 2565 | InsertValueInst::InsertValueInst(Value *Agg, |
| 2566 | Value *Val, |
| 2567 | ArrayRef<unsigned> Idxs, |
| 2568 | const Twine &NameStr, |
| 2569 | Instruction *InsertBefore) |
| 2570 | : Instruction(Agg->getType(), InsertValue, |
| 2571 | OperandTraits<InsertValueInst>::op_begin(this), |
| 2572 | 2, InsertBefore) { |
| 2573 | init(Agg, Val, Idxs, NameStr); |
| 2574 | } |
| 2575 | |
| 2576 | InsertValueInst::InsertValueInst(Value *Agg, |
| 2577 | Value *Val, |
| 2578 | ArrayRef<unsigned> Idxs, |
| 2579 | const Twine &NameStr, |
| 2580 | BasicBlock *InsertAtEnd) |
| 2581 | : Instruction(Agg->getType(), InsertValue, |
| 2582 | OperandTraits<InsertValueInst>::op_begin(this), |
| 2583 | 2, InsertAtEnd) { |
| 2584 | init(Agg, Val, Idxs, NameStr); |
| 2585 | } |
| 2586 | |
| 2587 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)InsertValueInst::op_iterator InsertValueInst::op_begin() { return OperandTraits<InsertValueInst>::op_begin(this); } InsertValueInst ::const_op_iterator InsertValueInst::op_begin() const { return OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this)); } InsertValueInst::op_iterator InsertValueInst ::op_end() { return OperandTraits<InsertValueInst>::op_end (this); } InsertValueInst::const_op_iterator InsertValueInst:: op_end() const { return OperandTraits<InsertValueInst>:: op_end(const_cast<InsertValueInst*>(this)); } Value *InsertValueInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<InsertValueInst>::op_begin (const_cast<InsertValueInst*>(this))[i_nocapture].get() ); } void InsertValueInst::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<InsertValueInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertValueInst::getNumOperands() const { return OperandTraits <InsertValueInst>::operands(this); } template <int Idx_nocapture > Use &InsertValueInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &InsertValueInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
| 2588 | |
| 2589 | //===----------------------------------------------------------------------===// |
| 2590 | // PHINode Class |
| 2591 | //===----------------------------------------------------------------------===// |
| 2592 | |
| 2593 | // PHINode - The PHINode class is used to represent the magical mystical PHI |
| 2594 | // node, that can not exist in nature, but can be synthesized in a computer |
| 2595 | // scientist's overactive imagination. |
| 2596 | // |
| 2597 | class PHINode : public Instruction { |
| 2598 | /// The number of operands actually allocated. NumOperands is |
| 2599 | /// the number actually in use. |
| 2600 | unsigned ReservedSpace; |
| 2601 | |
| 2602 | PHINode(const PHINode &PN); |
| 2603 | |
| 2604 | explicit PHINode(Type *Ty, unsigned NumReservedValues, |
| 2605 | const Twine &NameStr = "", |
| 2606 | Instruction *InsertBefore = nullptr) |
| 2607 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), |
| 2608 | ReservedSpace(NumReservedValues) { |
| 2609 | assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")((void)0); |
| 2610 | setName(NameStr); |
| 2611 | allocHungoffUses(ReservedSpace); |
| 2612 | } |
| 2613 | |
| 2614 | PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, |
| 2615 | BasicBlock *InsertAtEnd) |
| 2616 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), |
| 2617 | ReservedSpace(NumReservedValues) { |
| 2618 | assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")((void)0); |
| 2619 | setName(NameStr); |
| 2620 | allocHungoffUses(ReservedSpace); |
| 2621 | } |
| 2622 | |
| 2623 | protected: |
| 2624 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2625 | friend class Instruction; |
| 2626 | |
| 2627 | PHINode *cloneImpl() const; |
| 2628 | |
| 2629 | // allocHungoffUses - this is more complicated than the generic |
| 2630 | // User::allocHungoffUses, because we have to allocate Uses for the incoming |
| 2631 | // values and pointers to the incoming blocks, all in one allocation. |
| 2632 | void allocHungoffUses(unsigned N) { |
| 2633 | User::allocHungoffUses(N, /* IsPhi */ true); |
| 2634 | } |
| 2635 | |
| 2636 | public: |
| 2637 | /// Constructors - NumReservedValues is a hint for the number of incoming |
| 2638 | /// edges that this phi node will have (use 0 if you really have no idea). |
| 2639 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
| 2640 | const Twine &NameStr = "", |
| 2641 | Instruction *InsertBefore = nullptr) { |
| 2642 | return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); |
| 2643 | } |
| 2644 | |
| 2645 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
| 2646 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 2647 | return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); |
| 2648 | } |
| 2649 | |
| 2650 | /// Provide fast operand accessors |
| 2651 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 2652 | |
| 2653 | // Block iterator interface. This provides access to the list of incoming |
| 2654 | // basic blocks, which parallels the list of incoming values. |
| 2655 | |
| 2656 | using block_iterator = BasicBlock **; |
| 2657 | using const_block_iterator = BasicBlock * const *; |
| 2658 | |
| 2659 | block_iterator block_begin() { |
| 2660 | return reinterpret_cast<block_iterator>(op_begin() + ReservedSpace); |
| 2661 | } |
| 2662 | |
| 2663 | const_block_iterator block_begin() const { |
| 2664 | return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace); |
| 2665 | } |
| 2666 | |
| 2667 | block_iterator block_end() { |
| 2668 | return block_begin() + getNumOperands(); |
| 2669 | } |
| 2670 | |
| 2671 | const_block_iterator block_end() const { |
| 2672 | return block_begin() + getNumOperands(); |
| 2673 | } |
| 2674 | |
| 2675 | iterator_range<block_iterator> blocks() { |
| 2676 | return make_range(block_begin(), block_end()); |
| 2677 | } |
| 2678 | |
| 2679 | iterator_range<const_block_iterator> blocks() const { |
| 2680 | return make_range(block_begin(), block_end()); |
| 2681 | } |
| 2682 | |
| 2683 | op_range incoming_values() { return operands(); } |
| 2684 | |
| 2685 | const_op_range incoming_values() const { return operands(); } |
| 2686 | |
| 2687 | /// Return the number of incoming edges |
| 2688 | /// |
| 2689 | unsigned getNumIncomingValues() const { return getNumOperands(); } |
| 2690 | |
| 2691 | /// Return incoming value number x |
| 2692 | /// |
| 2693 | Value *getIncomingValue(unsigned i) const { |
| 2694 | return getOperand(i); |
| 2695 | } |
| 2696 | void setIncomingValue(unsigned i, Value *V) { |
| 2697 | assert(V && "PHI node got a null value!")((void)0); |
| 2698 | assert(getType() == V->getType() &&((void)0) |
| 2699 | "All operands to PHI node must be the same type as the PHI node!")((void)0); |
| 2700 | setOperand(i, V); |
| 2701 | } |
| 2702 | |
| 2703 | static unsigned getOperandNumForIncomingValue(unsigned i) { |
| 2704 | return i; |
| 2705 | } |
| 2706 | |
| 2707 | static unsigned getIncomingValueNumForOperand(unsigned i) { |
| 2708 | return i; |
| 2709 | } |
| 2710 | |
| 2711 | /// Return incoming basic block number @p i. |
| 2712 | /// |
| 2713 | BasicBlock *getIncomingBlock(unsigned i) const { |
| 2714 | return block_begin()[i]; |
| 2715 | } |
| 2716 | |
| 2717 | /// Return incoming basic block corresponding |
| 2718 | /// to an operand of the PHI. |
| 2719 | /// |
| 2720 | BasicBlock *getIncomingBlock(const Use &U) const { |
| 2721 | assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")((void)0); |
| 2722 | return getIncomingBlock(unsigned(&U - op_begin())); |
| 2723 | } |
| 2724 | |
| 2725 | /// Return incoming basic block corresponding |
| 2726 | /// to value use iterator. |
| 2727 | /// |
| 2728 | BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { |
| 2729 | return getIncomingBlock(I.getUse()); |
| 2730 | } |
| 2731 | |
| 2732 | void setIncomingBlock(unsigned i, BasicBlock *BB) { |
| 2733 | assert(BB && "PHI node got a null basic block!")((void)0); |
| 2734 | block_begin()[i] = BB; |
| 2735 | } |
| 2736 | |
| 2737 | /// Replace every incoming basic block \p Old to basic block \p New. |
| 2738 | void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) { |
| 2739 | assert(New && Old && "PHI node got a null basic block!")((void)0); |
| 2740 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
| 2741 | if (getIncomingBlock(Op) == Old) |
| 2742 | setIncomingBlock(Op, New); |
| 2743 | } |
| 2744 | |
| 2745 | /// Add an incoming value to the end of the PHI list |
| 2746 | /// |
| 2747 | void addIncoming(Value *V, BasicBlock *BB) { |
| 2748 | if (getNumOperands() == ReservedSpace) |
| 2749 | growOperands(); // Get more space! |
| 2750 | // Initialize some new operands. |
| 2751 | setNumHungOffUseOperands(getNumOperands() + 1); |
| 2752 | setIncomingValue(getNumOperands() - 1, V); |
| 2753 | setIncomingBlock(getNumOperands() - 1, BB); |
| 2754 | } |
| 2755 | |
| 2756 | /// Remove an incoming value. This is useful if a |
| 2757 | /// predecessor basic block is deleted. The value removed is returned. |
| 2758 | /// |
| 2759 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty |
| 2760 | /// is true), the PHI node is destroyed and any uses of it are replaced with |
| 2761 | /// dummy values. The only time there should be zero incoming values to a PHI |
| 2762 | /// node is when the block is dead, so this strategy is sound. |
| 2763 | /// |
| 2764 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); |
| 2765 | |
| 2766 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { |
| 2767 | int Idx = getBasicBlockIndex(BB); |
| 2768 | assert(Idx >= 0 && "Invalid basic block argument to remove!")((void)0); |
| 2769 | return removeIncomingValue(Idx, DeletePHIIfEmpty); |
| 2770 | } |
| 2771 | |
| 2772 | /// Return the first index of the specified basic |
| 2773 | /// block in the value list for this PHI. Returns -1 if no instance. |
| 2774 | /// |
| 2775 | int getBasicBlockIndex(const BasicBlock *BB) const { |
| 2776 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 2777 | if (block_begin()[i] == BB) |
| 2778 | return i; |
| 2779 | return -1; |
| 2780 | } |
| 2781 | |
| 2782 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { |
| 2783 | int Idx = getBasicBlockIndex(BB); |
| 2784 | assert(Idx >= 0 && "Invalid basic block argument!")((void)0); |
| 2785 | return getIncomingValue(Idx); |
| 2786 | } |
| 2787 | |
| 2788 | /// Set every incoming value(s) for block \p BB to \p V. |
| 2789 | void setIncomingValueForBlock(const BasicBlock *BB, Value *V) { |
| 2790 | assert(BB && "PHI node got a null basic block!")((void)0); |
| 2791 | bool Found = false; |
| 2792 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
| 2793 | if (getIncomingBlock(Op) == BB) { |
| 2794 | Found = true; |
| 2795 | setIncomingValue(Op, V); |
| 2796 | } |
| 2797 | (void)Found; |
| 2798 | assert(Found && "Invalid basic block argument to set!")((void)0); |
| 2799 | } |
| 2800 | |
| 2801 | /// If the specified PHI node always merges together the |
| 2802 | /// same value, return the value, otherwise return null. |
| 2803 | Value *hasConstantValue() const; |
| 2804 | |
| 2805 | /// Whether the specified PHI node always merges |
| 2806 | /// together the same value, assuming undefs are equal to a unique |
| 2807 | /// non-undef value. |
| 2808 | bool hasConstantOrUndefValue() const; |
| 2809 | |
| 2810 | /// If the PHI node is complete which means all of its parent's predecessors |
| 2811 | /// have incoming value in this PHI, return true, otherwise return false. |
| 2812 | bool isComplete() const { |
| 2813 | return llvm::all_of(predecessors(getParent()), |
| 2814 | [this](const BasicBlock *Pred) { |
| 2815 | return getBasicBlockIndex(Pred) >= 0; |
| 2816 | }); |
| 2817 | } |
| 2818 | |
| 2819 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 2820 | static bool classof(const Instruction *I) { |
| 2821 | return I->getOpcode() == Instruction::PHI; |
| 2822 | } |
| 2823 | static bool classof(const Value *V) { |
| 2824 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 2825 | } |
| 2826 | |
| 2827 | private: |
| 2828 | void growOperands(); |
| 2829 | }; |
| 2830 | |
| 2831 | template <> |
| 2832 | struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { |
| 2833 | }; |
| 2834 | |
| 2835 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)PHINode::op_iterator PHINode::op_begin() { return OperandTraits <PHINode>::op_begin(this); } PHINode::const_op_iterator PHINode::op_begin() const { return OperandTraits<PHINode> ::op_begin(const_cast<PHINode*>(this)); } PHINode::op_iterator PHINode::op_end() { return OperandTraits<PHINode>::op_end (this); } PHINode::const_op_iterator PHINode::op_end() const { return OperandTraits<PHINode>::op_end(const_cast<PHINode *>(this)); } Value *PHINode::getOperand(unsigned i_nocapture ) const { ((void)0); return cast_or_null<Value>( OperandTraits <PHINode>::op_begin(const_cast<PHINode*>(this))[i_nocapture ].get()); } void PHINode::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<PHINode>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned PHINode::getNumOperands () const { return OperandTraits<PHINode>::operands(this ); } template <int Idx_nocapture> Use &PHINode::Op( ) { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &PHINode::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 2836 | |
| 2837 | //===----------------------------------------------------------------------===// |
| 2838 | // LandingPadInst Class |
| 2839 | //===----------------------------------------------------------------------===// |
| 2840 | |
| 2841 | //===--------------------------------------------------------------------------- |
| 2842 | /// The landingpad instruction holds all of the information |
| 2843 | /// necessary to generate correct exception handling. The landingpad instruction |
| 2844 | /// cannot be moved from the top of a landing pad block, which itself is |
| 2845 | /// accessible only from the 'unwind' edge of an invoke. This uses the |
| 2846 | /// SubclassData field in Value to store whether or not the landingpad is a |
| 2847 | /// cleanup. |
| 2848 | /// |
| 2849 | class LandingPadInst : public Instruction { |
| 2850 | using CleanupField = BoolBitfieldElementT<0>; |
| 2851 | |
| 2852 | /// The number of operands actually allocated. NumOperands is |
| 2853 | /// the number actually in use. |
| 2854 | unsigned ReservedSpace; |
| 2855 | |
| 2856 | LandingPadInst(const LandingPadInst &LP); |
| 2857 | |
| 2858 | public: |
| 2859 | enum ClauseType { Catch, Filter }; |
| 2860 | |
| 2861 | private: |
| 2862 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 2863 | const Twine &NameStr, Instruction *InsertBefore); |
| 2864 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 2865 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2866 | |
| 2867 | // Allocate space for exactly zero operands. |
| 2868 | void *operator new(size_t S) { return User::operator new(S); } |
| 2869 | |
| 2870 | void growOperands(unsigned Size); |
| 2871 | void init(unsigned NumReservedValues, const Twine &NameStr); |
| 2872 | |
| 2873 | protected: |
| 2874 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2875 | friend class Instruction; |
| 2876 | |
| 2877 | LandingPadInst *cloneImpl() const; |
| 2878 | |
| 2879 | public: |
| 2880 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 2881 | |
| 2882 | /// Constructors - NumReservedClauses is a hint for the number of incoming |
| 2883 | /// clauses that this landingpad will have (use 0 if you really have no idea). |
| 2884 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
| 2885 | const Twine &NameStr = "", |
| 2886 | Instruction *InsertBefore = nullptr); |
| 2887 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
| 2888 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 2889 | |
| 2890 | /// Provide fast operand accessors |
| 2891 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 2892 | |
| 2893 | /// Return 'true' if this landingpad instruction is a |
| 2894 | /// cleanup. I.e., it should be run when unwinding even if its landing pad |
| 2895 | /// doesn't catch the exception. |
| 2896 | bool isCleanup() const { return getSubclassData<CleanupField>(); } |
| 2897 | |
| 2898 | /// Indicate that this landingpad instruction is a cleanup. |
| 2899 | void setCleanup(bool V) { setSubclassData<CleanupField>(V); } |
| 2900 | |
| 2901 | /// Add a catch or filter clause to the landing pad. |
| 2902 | void addClause(Constant *ClauseVal); |
| 2903 | |
| 2904 | /// Get the value of the clause at index Idx. Use isCatch/isFilter to |
| 2905 | /// determine what type of clause this is. |
| 2906 | Constant *getClause(unsigned Idx) const { |
| 2907 | return cast<Constant>(getOperandList()[Idx]); |
| 2908 | } |
| 2909 | |
| 2910 | /// Return 'true' if the clause and index Idx is a catch clause. |
| 2911 | bool isCatch(unsigned Idx) const { |
| 2912 | return !isa<ArrayType>(getOperandList()[Idx]->getType()); |
| 2913 | } |
| 2914 | |
| 2915 | /// Return 'true' if the clause and index Idx is a filter clause. |
| 2916 | bool isFilter(unsigned Idx) const { |
| 2917 | return isa<ArrayType>(getOperandList()[Idx]->getType()); |
| 2918 | } |
| 2919 | |
| 2920 | /// Get the number of clauses for this landing pad. |
| 2921 | unsigned getNumClauses() const { return getNumOperands(); } |
| 2922 | |
| 2923 | /// Grow the size of the operand list to accommodate the new |
| 2924 | /// number of clauses. |
| 2925 | void reserveClauses(unsigned Size) { growOperands(Size); } |
| 2926 | |
| 2927 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 2928 | static bool classof(const Instruction *I) { |
| 2929 | return I->getOpcode() == Instruction::LandingPad; |
| 2930 | } |
| 2931 | static bool classof(const Value *V) { |
| 2932 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 2933 | } |
| 2934 | }; |
| 2935 | |
| 2936 | template <> |
| 2937 | struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { |
| 2938 | }; |
| 2939 | |
| 2940 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)LandingPadInst::op_iterator LandingPadInst::op_begin() { return OperandTraits<LandingPadInst>::op_begin(this); } LandingPadInst ::const_op_iterator LandingPadInst::op_begin() const { return OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this)); } LandingPadInst::op_iterator LandingPadInst ::op_end() { return OperandTraits<LandingPadInst>::op_end (this); } LandingPadInst::const_op_iterator LandingPadInst::op_end () const { return OperandTraits<LandingPadInst>::op_end (const_cast<LandingPadInst*>(this)); } Value *LandingPadInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<LandingPadInst>::op_begin( const_cast<LandingPadInst*>(this))[i_nocapture].get()); } void LandingPadInst::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<LandingPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned LandingPadInst::getNumOperands() const { return OperandTraits <LandingPadInst>::operands(this); } template <int Idx_nocapture > Use &LandingPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &LandingPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
| 2941 | |
| 2942 | //===----------------------------------------------------------------------===// |
| 2943 | // ReturnInst Class |
| 2944 | //===----------------------------------------------------------------------===// |
| 2945 | |
| 2946 | //===--------------------------------------------------------------------------- |
| 2947 | /// Return a value (possibly void), from a function. Execution |
| 2948 | /// does not continue in this function any longer. |
| 2949 | /// |
| 2950 | class ReturnInst : public Instruction { |
| 2951 | ReturnInst(const ReturnInst &RI); |
| 2952 | |
| 2953 | private: |
| 2954 | // ReturnInst constructors: |
| 2955 | // ReturnInst() - 'ret void' instruction |
| 2956 | // ReturnInst( null) - 'ret void' instruction |
| 2957 | // ReturnInst(Value* X) - 'ret X' instruction |
| 2958 | // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I |
| 2959 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I |
| 2960 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B |
| 2961 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B |
| 2962 | // |
| 2963 | // NOTE: If the Value* passed is of type void then the constructor behaves as |
| 2964 | // if it was passed NULL. |
| 2965 | explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, |
| 2966 | Instruction *InsertBefore = nullptr); |
| 2967 | ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); |
| 2968 | explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
| 2969 | |
| 2970 | protected: |
| 2971 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 2972 | friend class Instruction; |
| 2973 | |
| 2974 | ReturnInst *cloneImpl() const; |
| 2975 | |
| 2976 | public: |
| 2977 | static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, |
| 2978 | Instruction *InsertBefore = nullptr) { |
| 2979 | return new(!!retVal) ReturnInst(C, retVal, InsertBefore); |
| 2980 | } |
| 2981 | |
| 2982 | static ReturnInst* Create(LLVMContext &C, Value *retVal, |
| 2983 | BasicBlock *InsertAtEnd) { |
| 2984 | return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); |
| 2985 | } |
| 2986 | |
| 2987 | static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { |
| 2988 | return new(0) ReturnInst(C, InsertAtEnd); |
| 2989 | } |
| 2990 | |
| 2991 | /// Provide fast operand accessors |
| 2992 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 2993 | |
| 2994 | /// Convenience accessor. Returns null if there is no return value. |
| 2995 | Value *getReturnValue() const { |
| 2996 | return getNumOperands() != 0 ? getOperand(0) : nullptr; |
| 2997 | } |
| 2998 | |
| 2999 | unsigned getNumSuccessors() const { return 0; } |
| 3000 | |
| 3001 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 3002 | static bool classof(const Instruction *I) { |
| 3003 | return (I->getOpcode() == Instruction::Ret); |
| 3004 | } |
| 3005 | static bool classof(const Value *V) { |
| 3006 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 3007 | } |
| 3008 | |
| 3009 | private: |
| 3010 | BasicBlock *getSuccessor(unsigned idx) const { |
| 3011 | llvm_unreachable("ReturnInst has no successors!")__builtin_unreachable(); |
| 3012 | } |
| 3013 | |
| 3014 | void setSuccessor(unsigned idx, BasicBlock *B) { |
| 3015 | llvm_unreachable("ReturnInst has no successors!")__builtin_unreachable(); |
| 3016 | } |
| 3017 | }; |
| 3018 | |
| 3019 | template <> |
| 3020 | struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { |
| 3021 | }; |
| 3022 | |
| 3023 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)ReturnInst::op_iterator ReturnInst::op_begin() { return OperandTraits <ReturnInst>::op_begin(this); } ReturnInst::const_op_iterator ReturnInst::op_begin() const { return OperandTraits<ReturnInst >::op_begin(const_cast<ReturnInst*>(this)); } ReturnInst ::op_iterator ReturnInst::op_end() { return OperandTraits< ReturnInst>::op_end(this); } ReturnInst::const_op_iterator ReturnInst::op_end() const { return OperandTraits<ReturnInst >::op_end(const_cast<ReturnInst*>(this)); } Value *ReturnInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<ReturnInst>::op_begin(const_cast <ReturnInst*>(this))[i_nocapture].get()); } void ReturnInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<ReturnInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned ReturnInst::getNumOperands() const { return OperandTraits<ReturnInst>::operands(this); } template <int Idx_nocapture> Use &ReturnInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &ReturnInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 3024 | |
| 3025 | //===----------------------------------------------------------------------===// |
| 3026 | // BranchInst Class |
| 3027 | //===----------------------------------------------------------------------===// |
| 3028 | |
| 3029 | //===--------------------------------------------------------------------------- |
| 3030 | /// Conditional or Unconditional Branch instruction. |
| 3031 | /// |
| 3032 | class BranchInst : public Instruction { |
| 3033 | /// Ops list - Branches are strange. The operands are ordered: |
| 3034 | /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because |
| 3035 | /// they don't have to check for cond/uncond branchness. These are mostly |
| 3036 | /// accessed relative from op_end(). |
| 3037 | BranchInst(const BranchInst &BI); |
| 3038 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): |
| 3039 | // BranchInst(BB *B) - 'br B' |
| 3040 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' |
| 3041 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I |
| 3042 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I |
| 3043 | // BranchInst(BB* B, BB *I) - 'br B' insert at end |
| 3044 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end |
| 3045 | explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); |
| 3046 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 3047 | Instruction *InsertBefore = nullptr); |
| 3048 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); |
| 3049 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 3050 | BasicBlock *InsertAtEnd); |
| 3051 | |
| 3052 | void AssertOK(); |
| 3053 | |
| 3054 | protected: |
| 3055 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 3056 | friend class Instruction; |
| 3057 | |
| 3058 | BranchInst *cloneImpl() const; |
| 3059 | |
| 3060 | public: |
| 3061 | /// Iterator type that casts an operand to a basic block. |
| 3062 | /// |
| 3063 | /// This only makes sense because the successors are stored as adjacent |
| 3064 | /// operands for branch instructions. |
| 3065 | struct succ_op_iterator |
| 3066 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
| 3067 | std::random_access_iterator_tag, BasicBlock *, |
| 3068 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
| 3069 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
| 3070 | |
| 3071 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
| 3072 | BasicBlock *operator->() const { return operator*(); } |
| 3073 | }; |
| 3074 | |
| 3075 | /// The const version of `succ_op_iterator`. |
| 3076 | struct const_succ_op_iterator |
| 3077 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
| 3078 | std::random_access_iterator_tag, |
| 3079 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
| 3080 | const BasicBlock *> { |
| 3081 | explicit const_succ_op_iterator(const_value_op_iterator I) |
| 3082 | : iterator_adaptor_base(I) {} |
| 3083 | |
| 3084 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
| 3085 | const BasicBlock *operator->() const { return operator*(); } |
| 3086 | }; |
| 3087 | |
| 3088 | static BranchInst *Create(BasicBlock *IfTrue, |
| 3089 | Instruction *InsertBefore = nullptr) { |
| 3090 | return new(1) BranchInst(IfTrue, InsertBefore); |
| 3091 | } |
| 3092 | |
| 3093 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
| 3094 | Value *Cond, Instruction *InsertBefore = nullptr) { |
| 3095 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); |
| 3096 | } |
| 3097 | |
| 3098 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { |
| 3099 | return new(1) BranchInst(IfTrue, InsertAtEnd); |
| 3100 | } |
| 3101 | |
| 3102 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
| 3103 | Value *Cond, BasicBlock *InsertAtEnd) { |
| 3104 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); |
| 3105 | } |
| 3106 | |
| 3107 | /// Transparently provide more efficient getOperand methods. |
| 3108 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 3109 | |
| 3110 | bool isUnconditional() const { return getNumOperands() == 1; } |
| 3111 | bool isConditional() const { return getNumOperands() == 3; } |
| 3112 | |
| 3113 | Value *getCondition() const { |
| 3114 | assert(isConditional() && "Cannot get condition of an uncond branch!")((void)0); |
| 3115 | return Op<-3>(); |
| 3116 | } |
| 3117 | |
| 3118 | void setCondition(Value *V) { |
| 3119 | assert(isConditional() && "Cannot set condition of unconditional branch!")((void)0); |
| 3120 | Op<-3>() = V; |
| 3121 | } |
| 3122 | |
| 3123 | unsigned getNumSuccessors() const { return 1+isConditional(); } |
| 3124 | |
| 3125 | BasicBlock *getSuccessor(unsigned i) const { |
| 3126 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!")((void)0); |
| 3127 | return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); |
| 3128 | } |
| 3129 | |
| 3130 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
| 3131 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")((void)0); |
| 3132 | *(&Op<-1>() - idx) = NewSucc; |
| 3133 | } |
| 3134 | |
| 3135 | /// Swap the successors of this branch instruction. |
| 3136 | /// |
| 3137 | /// Swaps the successors of the branch instruction. This also swaps any |
| 3138 | /// branch weight metadata associated with the instruction so that it |
| 3139 | /// continues to map correctly to each operand. |
| 3140 | void swapSuccessors(); |
| 3141 | |
| 3142 | iterator_range<succ_op_iterator> successors() { |
| 3143 | return make_range( |
| 3144 | succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)), |
| 3145 | succ_op_iterator(value_op_end())); |
| 3146 | } |
| 3147 | |
| 3148 | iterator_range<const_succ_op_iterator> successors() const { |
| 3149 | return make_range(const_succ_op_iterator( |
| 3150 | std::next(value_op_begin(), isConditional() ? 1 : 0)), |
| 3151 | const_succ_op_iterator(value_op_end())); |
| 3152 | } |
| 3153 | |
| 3154 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 3155 | static bool classof(const Instruction *I) { |
| 3156 | return (I->getOpcode() == Instruction::Br); |
| 3157 | } |
| 3158 | static bool classof(const Value *V) { |
| 3159 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 3160 | } |
| 3161 | }; |
| 3162 | |
| 3163 | template <> |
| 3164 | struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { |
| 3165 | }; |
| 3166 | |
| 3167 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)BranchInst::op_iterator BranchInst::op_begin() { return OperandTraits <BranchInst>::op_begin(this); } BranchInst::const_op_iterator BranchInst::op_begin() const { return OperandTraits<BranchInst >::op_begin(const_cast<BranchInst*>(this)); } BranchInst ::op_iterator BranchInst::op_end() { return OperandTraits< BranchInst>::op_end(this); } BranchInst::const_op_iterator BranchInst::op_end() const { return OperandTraits<BranchInst >::op_end(const_cast<BranchInst*>(this)); } Value *BranchInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<BranchInst>::op_begin(const_cast <BranchInst*>(this))[i_nocapture].get()); } void BranchInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<BranchInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned BranchInst::getNumOperands() const { return OperandTraits<BranchInst>::operands(this); } template <int Idx_nocapture> Use &BranchInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &BranchInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 3168 | |
| 3169 | //===----------------------------------------------------------------------===// |
| 3170 | // SwitchInst Class |
| 3171 | //===----------------------------------------------------------------------===// |
| 3172 | |
| 3173 | //===--------------------------------------------------------------------------- |
| 3174 | /// Multiway switch |
| 3175 | /// |
| 3176 | class SwitchInst : public Instruction { |
| 3177 | unsigned ReservedSpace; |
| 3178 | |
| 3179 | // Operand[0] = Value to switch on |
| 3180 | // Operand[1] = Default basic block destination |
| 3181 | // Operand[2n ] = Value to match |
| 3182 | // Operand[2n+1] = BasicBlock to go to on match |
| 3183 | SwitchInst(const SwitchInst &SI); |
| 3184 | |
| 3185 | /// Create a new switch instruction, specifying a value to switch on and a |
| 3186 | /// default destination. The number of additional cases can be specified here |
| 3187 | /// to make memory allocation more efficient. This constructor can also |
| 3188 | /// auto-insert before another instruction. |
| 3189 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3190 | Instruction *InsertBefore); |
| 3191 | |
| 3192 | /// Create a new switch instruction, specifying a value to switch on and a |
| 3193 | /// default destination. The number of additional cases can be specified here |
| 3194 | /// to make memory allocation more efficient. This constructor also |
| 3195 | /// auto-inserts at the end of the specified BasicBlock. |
| 3196 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3197 | BasicBlock *InsertAtEnd); |
| 3198 | |
| 3199 | // allocate space for exactly zero operands |
| 3200 | void *operator new(size_t S) { return User::operator new(S); } |
| 3201 | |
| 3202 | void init(Value *Value, BasicBlock *Default, unsigned NumReserved); |
| 3203 | void growOperands(); |
| 3204 | |
| 3205 | protected: |
| 3206 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 3207 | friend class Instruction; |
| 3208 | |
| 3209 | SwitchInst *cloneImpl() const; |
| 3210 | |
| 3211 | public: |
| 3212 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 3213 | |
| 3214 | // -2 |
| 3215 | static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); |
| 3216 | |
| 3217 | template <typename CaseHandleT> class CaseIteratorImpl; |
| 3218 | |
| 3219 | /// A handle to a particular switch case. It exposes a convenient interface |
| 3220 | /// to both the case value and the successor block. |
| 3221 | /// |
| 3222 | /// We define this as a template and instantiate it to form both a const and |
| 3223 | /// non-const handle. |
| 3224 | template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT> |
| 3225 | class CaseHandleImpl { |
| 3226 | // Directly befriend both const and non-const iterators. |
| 3227 | friend class SwitchInst::CaseIteratorImpl< |
| 3228 | CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>; |
| 3229 | |
| 3230 | protected: |
| 3231 | // Expose the switch type we're parameterized with to the iterator. |
| 3232 | using SwitchInstType = SwitchInstT; |
| 3233 | |
| 3234 | SwitchInstT *SI; |
| 3235 | ptrdiff_t Index; |
| 3236 | |
| 3237 | CaseHandleImpl() = default; |
| 3238 | CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {} |
| 3239 | |
| 3240 | public: |
| 3241 | /// Resolves case value for current case. |
| 3242 | ConstantIntT *getCaseValue() const { |
| 3243 | assert((unsigned)Index < SI->getNumCases() &&((void)0) |
| 3244 | "Index out the number of cases.")((void)0); |
| 3245 | return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2)); |
| 3246 | } |
| 3247 | |
| 3248 | /// Resolves successor for current case. |
| 3249 | BasicBlockT *getCaseSuccessor() const { |
| 3250 | assert(((unsigned)Index < SI->getNumCases() ||((void)0) |
| 3251 | (unsigned)Index == DefaultPseudoIndex) &&((void)0) |
| 3252 | "Index out the number of cases.")((void)0); |
| 3253 | return SI->getSuccessor(getSuccessorIndex()); |
| 3254 | } |
| 3255 | |
| 3256 | /// Returns number of current case. |
| 3257 | unsigned getCaseIndex() const { return Index; } |
| 3258 | |
| 3259 | /// Returns successor index for current case successor. |
| 3260 | unsigned getSuccessorIndex() const { |
| 3261 | assert(((unsigned)Index == DefaultPseudoIndex ||((void)0) |
| 3262 | (unsigned)Index < SI->getNumCases()) &&((void)0) |
| 3263 | "Index out the number of cases.")((void)0); |
| 3264 | return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0; |
| 3265 | } |
| 3266 | |
| 3267 | bool operator==(const CaseHandleImpl &RHS) const { |
| 3268 | assert(SI == RHS.SI && "Incompatible operators.")((void)0); |
| 3269 | return Index == RHS.Index; |
| 3270 | } |
| 3271 | }; |
| 3272 | |
| 3273 | using ConstCaseHandle = |
| 3274 | CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>; |
| 3275 | |
| 3276 | class CaseHandle |
| 3277 | : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> { |
| 3278 | friend class SwitchInst::CaseIteratorImpl<CaseHandle>; |
| 3279 | |
| 3280 | public: |
| 3281 | CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {} |
| 3282 | |
| 3283 | /// Sets the new value for current case. |
| 3284 | void setValue(ConstantInt *V) { |
| 3285 | assert((unsigned)Index < SI->getNumCases() &&((void)0) |
| 3286 | "Index out the number of cases.")((void)0); |
| 3287 | SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); |
| 3288 | } |
| 3289 | |
| 3290 | /// Sets the new successor for current case. |
| 3291 | void setSuccessor(BasicBlock *S) { |
| 3292 | SI->setSuccessor(getSuccessorIndex(), S); |
| 3293 | } |
| 3294 | }; |
| 3295 | |
| 3296 | template <typename CaseHandleT> |
| 3297 | class CaseIteratorImpl |
| 3298 | : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>, |
| 3299 | std::random_access_iterator_tag, |
| 3300 | CaseHandleT> { |
| 3301 | using SwitchInstT = typename CaseHandleT::SwitchInstType; |
| 3302 | |
| 3303 | CaseHandleT Case; |
| 3304 | |
| 3305 | public: |
| 3306 | /// Default constructed iterator is in an invalid state until assigned to |
| 3307 | /// a case for a particular switch. |
| 3308 | CaseIteratorImpl() = default; |
| 3309 | |
| 3310 | /// Initializes case iterator for given SwitchInst and for given |
| 3311 | /// case number. |
| 3312 | CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {} |
| 3313 | |
| 3314 | /// Initializes case iterator for given SwitchInst and for given |
| 3315 | /// successor index. |
| 3316 | static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, |
| 3317 | unsigned SuccessorIndex) { |
| 3318 | assert(SuccessorIndex < SI->getNumSuccessors() &&((void)0) |
| 3319 | "Successor index # out of range!")((void)0); |
| 3320 | return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1) |
| 3321 | : CaseIteratorImpl(SI, DefaultPseudoIndex); |
| 3322 | } |
| 3323 | |
| 3324 | /// Support converting to the const variant. This will be a no-op for const |
| 3325 | /// variant. |
| 3326 | operator CaseIteratorImpl<ConstCaseHandle>() const { |
| 3327 | return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index); |
| 3328 | } |
| 3329 | |
| 3330 | CaseIteratorImpl &operator+=(ptrdiff_t N) { |
| 3331 | // Check index correctness after addition. |
| 3332 | // Note: Index == getNumCases() means end(). |
| 3333 | assert(Case.Index + N >= 0 &&((void)0) |
| 3334 | (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&((void)0) |
| 3335 | "Case.Index out the number of cases.")((void)0); |
| 3336 | Case.Index += N; |
| 3337 | return *this; |
| 3338 | } |
| 3339 | CaseIteratorImpl &operator-=(ptrdiff_t N) { |
| 3340 | // Check index correctness after subtraction. |
| 3341 | // Note: Case.Index == getNumCases() means end(). |
| 3342 | assert(Case.Index - N >= 0 &&((void)0) |
| 3343 | (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&((void)0) |
| 3344 | "Case.Index out the number of cases.")((void)0); |
| 3345 | Case.Index -= N; |
| 3346 | return *this; |
| 3347 | } |
| 3348 | ptrdiff_t operator-(const CaseIteratorImpl &RHS) const { |
| 3349 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((void)0); |
| 3350 | return Case.Index - RHS.Case.Index; |
| 3351 | } |
| 3352 | bool operator==(const CaseIteratorImpl &RHS) const { |
| 3353 | return Case == RHS.Case; |
| 3354 | } |
| 3355 | bool operator<(const CaseIteratorImpl &RHS) const { |
| 3356 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((void)0); |
| 3357 | return Case.Index < RHS.Case.Index; |
| 3358 | } |
| 3359 | CaseHandleT &operator*() { return Case; } |
| 3360 | const CaseHandleT &operator*() const { return Case; } |
| 3361 | }; |
| 3362 | |
| 3363 | using CaseIt = CaseIteratorImpl<CaseHandle>; |
| 3364 | using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>; |
| 3365 | |
| 3366 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
| 3367 | unsigned NumCases, |
| 3368 | Instruction *InsertBefore = nullptr) { |
| 3369 | return new SwitchInst(Value, Default, NumCases, InsertBefore); |
| 3370 | } |
| 3371 | |
| 3372 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
| 3373 | unsigned NumCases, BasicBlock *InsertAtEnd) { |
| 3374 | return new SwitchInst(Value, Default, NumCases, InsertAtEnd); |
| 3375 | } |
| 3376 | |
| 3377 | /// Provide fast operand accessors |
| 3378 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 3379 | |
| 3380 | // Accessor Methods for Switch stmt |
| 3381 | Value *getCondition() const { return getOperand(0); } |
| 3382 | void setCondition(Value *V) { setOperand(0, V); } |
| 3383 | |
| 3384 | BasicBlock *getDefaultDest() const { |
| 3385 | return cast<BasicBlock>(getOperand(1)); |
| 3386 | } |
| 3387 | |
| 3388 | void setDefaultDest(BasicBlock *DefaultCase) { |
| 3389 | setOperand(1, reinterpret_cast<Value*>(DefaultCase)); |
| 3390 | } |
| 3391 | |
| 3392 | /// Return the number of 'cases' in this switch instruction, excluding the |
| 3393 | /// default case. |
| 3394 | unsigned getNumCases() const { |
| 3395 | return getNumOperands()/2 - 1; |
| 3396 | } |
| 3397 | |
| 3398 | /// Returns a read/write iterator that points to the first case in the |
| 3399 | /// SwitchInst. |
| 3400 | CaseIt case_begin() { |
| 3401 | return CaseIt(this, 0); |
| 3402 | } |
| 3403 | |
| 3404 | /// Returns a read-only iterator that points to the first case in the |
| 3405 | /// SwitchInst. |
| 3406 | ConstCaseIt case_begin() const { |
| 3407 | return ConstCaseIt(this, 0); |
| 3408 | } |
| 3409 | |
| 3410 | /// Returns a read/write iterator that points one past the last in the |
| 3411 | /// SwitchInst. |
| 3412 | CaseIt case_end() { |
| 3413 | return CaseIt(this, getNumCases()); |
| 3414 | } |
| 3415 | |
| 3416 | /// Returns a read-only iterator that points one past the last in the |
| 3417 | /// SwitchInst. |
| 3418 | ConstCaseIt case_end() const { |
| 3419 | return ConstCaseIt(this, getNumCases()); |
| 3420 | } |
| 3421 | |
| 3422 | /// Iteration adapter for range-for loops. |
| 3423 | iterator_range<CaseIt> cases() { |
| 3424 | return make_range(case_begin(), case_end()); |
| 3425 | } |
| 3426 | |
| 3427 | /// Constant iteration adapter for range-for loops. |
| 3428 | iterator_range<ConstCaseIt> cases() const { |
| 3429 | return make_range(case_begin(), case_end()); |
| 3430 | } |
| 3431 | |
| 3432 | /// Returns an iterator that points to the default case. |
| 3433 | /// Note: this iterator allows to resolve successor only. Attempt |
| 3434 | /// to resolve case value causes an assertion. |
| 3435 | /// Also note, that increment and decrement also causes an assertion and |
| 3436 | /// makes iterator invalid. |
| 3437 | CaseIt case_default() { |
| 3438 | return CaseIt(this, DefaultPseudoIndex); |
| 3439 | } |
| 3440 | ConstCaseIt case_default() const { |
| 3441 | return ConstCaseIt(this, DefaultPseudoIndex); |
| 3442 | } |
| 3443 | |
| 3444 | /// Search all of the case values for the specified constant. If it is |
| 3445 | /// explicitly handled, return the case iterator of it, otherwise return |
| 3446 | /// default case iterator to indicate that it is handled by the default |
| 3447 | /// handler. |
| 3448 | CaseIt findCaseValue(const ConstantInt *C) { |
| 3449 | CaseIt I = llvm::find_if( |
| 3450 | cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; }); |
| 3451 | if (I != case_end()) |
| 3452 | return I; |
| 3453 | |
| 3454 | return case_default(); |
| 3455 | } |
| 3456 | ConstCaseIt findCaseValue(const ConstantInt *C) const { |
| 3457 | ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) { |
| 3458 | return Case.getCaseValue() == C; |
| 3459 | }); |
| 3460 | if (I != case_end()) |
| 3461 | return I; |
| 3462 | |
| 3463 | return case_default(); |
| 3464 | } |
| 3465 | |
| 3466 | /// Finds the unique case value for a given successor. Returns null if the |
| 3467 | /// successor is not found, not unique, or is the default case. |
| 3468 | ConstantInt *findCaseDest(BasicBlock *BB) { |
| 3469 | if (BB == getDefaultDest()) |
| 3470 | return nullptr; |
| 3471 | |
| 3472 | ConstantInt *CI = nullptr; |
| 3473 | for (auto Case : cases()) { |
| 3474 | if (Case.getCaseSuccessor() != BB) |
| 3475 | continue; |
| 3476 | |
| 3477 | if (CI) |
| 3478 | return nullptr; // Multiple cases lead to BB. |
| 3479 | |
| 3480 | CI = Case.getCaseValue(); |
| 3481 | } |
| 3482 | |
| 3483 | return CI; |
| 3484 | } |
| 3485 | |
| 3486 | /// Add an entry to the switch instruction. |
| 3487 | /// Note: |
| 3488 | /// This action invalidates case_end(). Old case_end() iterator will |
| 3489 | /// point to the added case. |
| 3490 | void addCase(ConstantInt *OnVal, BasicBlock *Dest); |
| 3491 | |
| 3492 | /// This method removes the specified case and its successor from the switch |
| 3493 | /// instruction. Note that this operation may reorder the remaining cases at |
| 3494 | /// index idx and above. |
| 3495 | /// Note: |
| 3496 | /// This action invalidates iterators for all cases following the one removed, |
| 3497 | /// including the case_end() iterator. It returns an iterator for the next |
| 3498 | /// case. |
| 3499 | CaseIt removeCase(CaseIt I); |
| 3500 | |
| 3501 | unsigned getNumSuccessors() const { return getNumOperands()/2; } |
| 3502 | BasicBlock *getSuccessor(unsigned idx) const { |
| 3503 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")((void)0); |
| 3504 | return cast<BasicBlock>(getOperand(idx*2+1)); |
| 3505 | } |
| 3506 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
| 3507 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!")((void)0); |
| 3508 | setOperand(idx * 2 + 1, NewSucc); |
| 3509 | } |
| 3510 | |
| 3511 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 3512 | static bool classof(const Instruction *I) { |
| 3513 | return I->getOpcode() == Instruction::Switch; |
| 3514 | } |
| 3515 | static bool classof(const Value *V) { |
| 3516 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 3517 | } |
| 3518 | }; |
| 3519 | |
| 3520 | /// A wrapper class to simplify modification of SwitchInst cases along with |
| 3521 | /// their prof branch_weights metadata. |
| 3522 | class SwitchInstProfUpdateWrapper { |
| 3523 | SwitchInst &SI; |
| 3524 | Optional<SmallVector<uint32_t, 8> > Weights = None; |
| 3525 | bool Changed = false; |
| 3526 | |
| 3527 | protected: |
| 3528 | static MDNode *getProfBranchWeightsMD(const SwitchInst &SI); |
| 3529 | |
| 3530 | MDNode *buildProfBranchWeightsMD(); |
| 3531 | |
| 3532 | void init(); |
| 3533 | |
| 3534 | public: |
| 3535 | using CaseWeightOpt = Optional<uint32_t>; |
| 3536 | SwitchInst *operator->() { return &SI; } |
| 3537 | SwitchInst &operator*() { return SI; } |
| 3538 | operator SwitchInst *() { return &SI; } |
| 3539 | |
| 3540 | SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } |
| 3541 | |
| 3542 | ~SwitchInstProfUpdateWrapper() { |
| 3543 | if (Changed) |
| 3544 | SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD()); |
| 3545 | } |
| 3546 | |
| 3547 | /// Delegate the call to the underlying SwitchInst::removeCase() and remove |
| 3548 | /// correspondent branch weight. |
| 3549 | SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I); |
| 3550 | |
| 3551 | /// Delegate the call to the underlying SwitchInst::addCase() and set the |
| 3552 | /// specified branch weight for the added case. |
| 3553 | void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W); |
| 3554 | |
| 3555 | /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark |
| 3556 | /// this object to not touch the underlying SwitchInst in destructor. |
| 3557 | SymbolTableList<Instruction>::iterator eraseFromParent(); |
| 3558 | |
| 3559 | void setSuccessorWeight(unsigned idx, CaseWeightOpt W); |
| 3560 | CaseWeightOpt getSuccessorWeight(unsigned idx); |
| 3561 | |
| 3562 | static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx); |
| 3563 | }; |
| 3564 | |
| 3565 | template <> |
| 3566 | struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { |
| 3567 | }; |
| 3568 | |
| 3569 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)SwitchInst::op_iterator SwitchInst::op_begin() { return OperandTraits <SwitchInst>::op_begin(this); } SwitchInst::const_op_iterator SwitchInst::op_begin() const { return OperandTraits<SwitchInst >::op_begin(const_cast<SwitchInst*>(this)); } SwitchInst ::op_iterator SwitchInst::op_end() { return OperandTraits< SwitchInst>::op_end(this); } SwitchInst::const_op_iterator SwitchInst::op_end() const { return OperandTraits<SwitchInst >::op_end(const_cast<SwitchInst*>(this)); } Value *SwitchInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<SwitchInst>::op_begin(const_cast <SwitchInst*>(this))[i_nocapture].get()); } void SwitchInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<SwitchInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned SwitchInst::getNumOperands() const { return OperandTraits<SwitchInst>::operands(this); } template <int Idx_nocapture> Use &SwitchInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &SwitchInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 3570 | |
| 3571 | //===----------------------------------------------------------------------===// |
| 3572 | // IndirectBrInst Class |
| 3573 | //===----------------------------------------------------------------------===// |
| 3574 | |
| 3575 | //===--------------------------------------------------------------------------- |
| 3576 | /// Indirect Branch Instruction. |
| 3577 | /// |
| 3578 | class IndirectBrInst : public Instruction { |
| 3579 | unsigned ReservedSpace; |
| 3580 | |
| 3581 | // Operand[0] = Address to jump to |
| 3582 | // Operand[n+1] = n-th destination |
| 3583 | IndirectBrInst(const IndirectBrInst &IBI); |
| 3584 | |
| 3585 | /// Create a new indirectbr instruction, specifying an |
| 3586 | /// Address to jump to. The number of expected destinations can be specified |
| 3587 | /// here to make memory allocation more efficient. This constructor can also |
| 3588 | /// autoinsert before another instruction. |
| 3589 | IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); |
| 3590 | |
| 3591 | /// Create a new indirectbr instruction, specifying an |
| 3592 | /// Address to jump to. The number of expected destinations can be specified |
| 3593 | /// here to make memory allocation more efficient. This constructor also |
| 3594 | /// autoinserts at the end of the specified BasicBlock. |
| 3595 | IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); |
| 3596 | |
| 3597 | // allocate space for exactly zero operands |
| 3598 | void *operator new(size_t S) { return User::operator new(S); } |
| 3599 | |
| 3600 | void init(Value *Address, unsigned NumDests); |
| 3601 | void growOperands(); |
| 3602 | |
| 3603 | protected: |
| 3604 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 3605 | friend class Instruction; |
| 3606 | |
| 3607 | IndirectBrInst *cloneImpl() const; |
| 3608 | |
| 3609 | public: |
| 3610 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 3611 | |
| 3612 | /// Iterator type that casts an operand to a basic block. |
| 3613 | /// |
| 3614 | /// This only makes sense because the successors are stored as adjacent |
| 3615 | /// operands for indirectbr instructions. |
| 3616 | struct succ_op_iterator |
| 3617 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
| 3618 | std::random_access_iterator_tag, BasicBlock *, |
| 3619 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
| 3620 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
| 3621 | |
| 3622 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
| 3623 | BasicBlock *operator->() const { return operator*(); } |
| 3624 | }; |
| 3625 | |
| 3626 | /// The const version of `succ_op_iterator`. |
| 3627 | struct const_succ_op_iterator |
| 3628 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
| 3629 | std::random_access_iterator_tag, |
| 3630 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
| 3631 | const BasicBlock *> { |
| 3632 | explicit const_succ_op_iterator(const_value_op_iterator I) |
| 3633 | : iterator_adaptor_base(I) {} |
| 3634 | |
| 3635 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
| 3636 | const BasicBlock *operator->() const { return operator*(); } |
| 3637 | }; |
| 3638 | |
| 3639 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
| 3640 | Instruction *InsertBefore = nullptr) { |
| 3641 | return new IndirectBrInst(Address, NumDests, InsertBefore); |
| 3642 | } |
| 3643 | |
| 3644 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
| 3645 | BasicBlock *InsertAtEnd) { |
| 3646 | return new IndirectBrInst(Address, NumDests, InsertAtEnd); |
| 3647 | } |
| 3648 | |
| 3649 | /// Provide fast operand accessors. |
| 3650 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 3651 | |
| 3652 | // Accessor Methods for IndirectBrInst instruction. |
| 3653 | Value *getAddress() { return getOperand(0); } |
| 3654 | const Value *getAddress() const { return getOperand(0); } |
| 3655 | void setAddress(Value *V) { setOperand(0, V); } |
| 3656 | |
| 3657 | /// return the number of possible destinations in this |
| 3658 | /// indirectbr instruction. |
| 3659 | unsigned getNumDestinations() const { return getNumOperands()-1; } |
| 3660 | |
| 3661 | /// Return the specified destination. |
| 3662 | BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } |
| 3663 | const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } |
| 3664 | |
| 3665 | /// Add a destination. |
| 3666 | /// |
| 3667 | void addDestination(BasicBlock *Dest); |
| 3668 | |
| 3669 | /// This method removes the specified successor from the |
| 3670 | /// indirectbr instruction. |
| 3671 | void removeDestination(unsigned i); |
| 3672 | |
| 3673 | unsigned getNumSuccessors() const { return getNumOperands()-1; } |
| 3674 | BasicBlock *getSuccessor(unsigned i) const { |
| 3675 | return cast<BasicBlock>(getOperand(i+1)); |
| 3676 | } |
| 3677 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
| 3678 | setOperand(i + 1, NewSucc); |
| 3679 | } |
| 3680 | |
| 3681 | iterator_range<succ_op_iterator> successors() { |
| 3682 | return make_range(succ_op_iterator(std::next(value_op_begin())), |
| 3683 | succ_op_iterator(value_op_end())); |
| 3684 | } |
| 3685 | |
| 3686 | iterator_range<const_succ_op_iterator> successors() const { |
| 3687 | return make_range(const_succ_op_iterator(std::next(value_op_begin())), |
| 3688 | const_succ_op_iterator(value_op_end())); |
| 3689 | } |
| 3690 | |
| 3691 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 3692 | static bool classof(const Instruction *I) { |
| 3693 | return I->getOpcode() == Instruction::IndirectBr; |
| 3694 | } |
| 3695 | static bool classof(const Value *V) { |
| 3696 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 3697 | } |
| 3698 | }; |
| 3699 | |
| 3700 | template <> |
| 3701 | struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { |
| 3702 | }; |
| 3703 | |
| 3704 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)IndirectBrInst::op_iterator IndirectBrInst::op_begin() { return OperandTraits<IndirectBrInst>::op_begin(this); } IndirectBrInst ::const_op_iterator IndirectBrInst::op_begin() const { return OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this)); } IndirectBrInst::op_iterator IndirectBrInst ::op_end() { return OperandTraits<IndirectBrInst>::op_end (this); } IndirectBrInst::const_op_iterator IndirectBrInst::op_end () const { return OperandTraits<IndirectBrInst>::op_end (const_cast<IndirectBrInst*>(this)); } Value *IndirectBrInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<IndirectBrInst>::op_begin( const_cast<IndirectBrInst*>(this))[i_nocapture].get()); } void IndirectBrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<IndirectBrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned IndirectBrInst::getNumOperands() const { return OperandTraits <IndirectBrInst>::operands(this); } template <int Idx_nocapture > Use &IndirectBrInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &IndirectBrInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
| 3705 | |
| 3706 | //===----------------------------------------------------------------------===// |
| 3707 | // InvokeInst Class |
| 3708 | //===----------------------------------------------------------------------===// |
| 3709 | |
| 3710 | /// Invoke instruction. The SubclassData field is used to hold the |
| 3711 | /// calling convention of the call. |
| 3712 | /// |
| 3713 | class InvokeInst : public CallBase { |
| 3714 | /// The number of operands for this call beyond the called function, |
| 3715 | /// arguments, and operand bundles. |
| 3716 | static constexpr int NumExtraOperands = 2; |
| 3717 | |
| 3718 | /// The index from the end of the operand array to the normal destination. |
| 3719 | static constexpr int NormalDestOpEndIdx = -3; |
| 3720 | |
| 3721 | /// The index from the end of the operand array to the unwind destination. |
| 3722 | static constexpr int UnwindDestOpEndIdx = -2; |
| 3723 | |
| 3724 | InvokeInst(const InvokeInst &BI); |
| 3725 | |
| 3726 | /// Construct an InvokeInst given a range of arguments. |
| 3727 | /// |
| 3728 | /// Construct an InvokeInst from a range of arguments |
| 3729 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3730 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3731 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3732 | const Twine &NameStr, Instruction *InsertBefore); |
| 3733 | |
| 3734 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3735 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3736 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3737 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 3738 | |
| 3739 | void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3740 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3741 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
| 3742 | |
| 3743 | /// Compute the number of operands to allocate. |
| 3744 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
| 3745 | // We need one operand for the called function, plus our extra operands and |
| 3746 | // the input operand counts provided. |
| 3747 | return 1 + NumExtraOperands + NumArgs + NumBundleInputs; |
| 3748 | } |
| 3749 | |
| 3750 | protected: |
| 3751 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 3752 | friend class Instruction; |
| 3753 | |
| 3754 | InvokeInst *cloneImpl() const; |
| 3755 | |
| 3756 | public: |
| 3757 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3758 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3759 | const Twine &NameStr, |
| 3760 | Instruction *InsertBefore = nullptr) { |
| 3761 | int NumOperands = ComputeNumOperands(Args.size()); |
| 3762 | return new (NumOperands) |
| 3763 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, |
| 3764 | NameStr, InsertBefore); |
| 3765 | } |
| 3766 | |
| 3767 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3768 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3769 | ArrayRef<OperandBundleDef> Bundles = None, |
| 3770 | const Twine &NameStr = "", |
| 3771 | Instruction *InsertBefore = nullptr) { |
| 3772 | int NumOperands = |
| 3773 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
| 3774 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 3775 | |
| 3776 | return new (NumOperands, DescriptorBytes) |
| 3777 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
| 3778 | NameStr, InsertBefore); |
| 3779 | } |
| 3780 | |
| 3781 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3782 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3783 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 3784 | int NumOperands = ComputeNumOperands(Args.size()); |
| 3785 | return new (NumOperands) |
| 3786 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, |
| 3787 | NameStr, InsertAtEnd); |
| 3788 | } |
| 3789 | |
| 3790 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3791 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3792 | ArrayRef<OperandBundleDef> Bundles, |
| 3793 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 3794 | int NumOperands = |
| 3795 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
| 3796 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 3797 | |
| 3798 | return new (NumOperands, DescriptorBytes) |
| 3799 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
| 3800 | NameStr, InsertAtEnd); |
| 3801 | } |
| 3802 | |
| 3803 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
| 3804 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3805 | const Twine &NameStr, |
| 3806 | Instruction *InsertBefore = nullptr) { |
| 3807 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
| 3808 | IfException, Args, None, NameStr, InsertBefore); |
| 3809 | } |
| 3810 | |
| 3811 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
| 3812 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3813 | ArrayRef<OperandBundleDef> Bundles = None, |
| 3814 | const Twine &NameStr = "", |
| 3815 | Instruction *InsertBefore = nullptr) { |
| 3816 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
| 3817 | IfException, Args, Bundles, NameStr, InsertBefore); |
| 3818 | } |
| 3819 | |
| 3820 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
| 3821 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3822 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 3823 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
| 3824 | IfException, Args, NameStr, InsertAtEnd); |
| 3825 | } |
| 3826 | |
| 3827 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
| 3828 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3829 | ArrayRef<OperandBundleDef> Bundles, |
| 3830 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 3831 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
| 3832 | IfException, Args, Bundles, NameStr, InsertAtEnd); |
| 3833 | } |
| 3834 | |
| 3835 | /// Create a clone of \p II with a different set of operand bundles and |
| 3836 | /// insert it before \p InsertPt. |
| 3837 | /// |
| 3838 | /// The returned invoke instruction is identical to \p II in every way except |
| 3839 | /// that the operand bundles for the new instruction are set to the operand |
| 3840 | /// bundles in \p Bundles. |
| 3841 | static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, |
| 3842 | Instruction *InsertPt = nullptr); |
| 3843 | |
| 3844 | // get*Dest - Return the destination basic blocks... |
| 3845 | BasicBlock *getNormalDest() const { |
| 3846 | return cast<BasicBlock>(Op<NormalDestOpEndIdx>()); |
| 3847 | } |
| 3848 | BasicBlock *getUnwindDest() const { |
| 3849 | return cast<BasicBlock>(Op<UnwindDestOpEndIdx>()); |
| 3850 | } |
| 3851 | void setNormalDest(BasicBlock *B) { |
| 3852 | Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
| 3853 | } |
| 3854 | void setUnwindDest(BasicBlock *B) { |
| 3855 | Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
| 3856 | } |
| 3857 | |
| 3858 | /// Get the landingpad instruction from the landing pad |
| 3859 | /// block (the unwind destination). |
| 3860 | LandingPadInst *getLandingPadInst() const; |
| 3861 | |
| 3862 | BasicBlock *getSuccessor(unsigned i) const { |
| 3863 | assert(i < 2 && "Successor # out of range for invoke!")((void)0); |
| 3864 | return i == 0 ? getNormalDest() : getUnwindDest(); |
| 3865 | } |
| 3866 | |
| 3867 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
| 3868 | assert(i < 2 && "Successor # out of range for invoke!")((void)0); |
| 3869 | if (i == 0) |
| 3870 | setNormalDest(NewSucc); |
| 3871 | else |
| 3872 | setUnwindDest(NewSucc); |
| 3873 | } |
| 3874 | |
| 3875 | unsigned getNumSuccessors() const { return 2; } |
| 3876 | |
| 3877 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 3878 | static bool classof(const Instruction *I) { |
| 3879 | return (I->getOpcode() == Instruction::Invoke); |
| 3880 | } |
| 3881 | static bool classof(const Value *V) { |
| 3882 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 3883 | } |
| 3884 | |
| 3885 | private: |
| 3886 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 3887 | // method so that subclasses cannot accidentally use it. |
| 3888 | template <typename Bitfield> |
| 3889 | void setSubclassData(typename Bitfield::Type Value) { |
| 3890 | Instruction::setSubclassData<Bitfield>(Value); |
| 3891 | } |
| 3892 | }; |
| 3893 | |
| 3894 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3895 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3896 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3897 | const Twine &NameStr, Instruction *InsertBefore) |
| 3898 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
| 3899 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
| 3900 | InsertBefore) { |
| 3901 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
| 3902 | } |
| 3903 | |
| 3904 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
| 3905 | BasicBlock *IfException, ArrayRef<Value *> Args, |
| 3906 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3907 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 3908 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
| 3909 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
| 3910 | InsertAtEnd) { |
| 3911 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
| 3912 | } |
| 3913 | |
| 3914 | //===----------------------------------------------------------------------===// |
| 3915 | // CallBrInst Class |
| 3916 | //===----------------------------------------------------------------------===// |
| 3917 | |
| 3918 | /// CallBr instruction, tracking function calls that may not return control but |
| 3919 | /// instead transfer it to a third location. The SubclassData field is used to |
| 3920 | /// hold the calling convention of the call. |
| 3921 | /// |
| 3922 | class CallBrInst : public CallBase { |
| 3923 | |
| 3924 | unsigned NumIndirectDests; |
| 3925 | |
| 3926 | CallBrInst(const CallBrInst &BI); |
| 3927 | |
| 3928 | /// Construct a CallBrInst given a range of arguments. |
| 3929 | /// |
| 3930 | /// Construct a CallBrInst from a range of arguments |
| 3931 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
| 3932 | ArrayRef<BasicBlock *> IndirectDests, |
| 3933 | ArrayRef<Value *> Args, |
| 3934 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3935 | const Twine &NameStr, Instruction *InsertBefore); |
| 3936 | |
| 3937 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
| 3938 | ArrayRef<BasicBlock *> IndirectDests, |
| 3939 | ArrayRef<Value *> Args, |
| 3940 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 3941 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 3942 | |
| 3943 | void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, |
| 3944 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, |
| 3945 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
| 3946 | |
| 3947 | /// Should the Indirect Destinations change, scan + update the Arg list. |
| 3948 | void updateArgBlockAddresses(unsigned i, BasicBlock *B); |
| 3949 | |
| 3950 | /// Compute the number of operands to allocate. |
| 3951 | static int ComputeNumOperands(int NumArgs, int NumIndirectDests, |
| 3952 | int NumBundleInputs = 0) { |
| 3953 | // We need one operand for the called function, plus our extra operands and |
| 3954 | // the input operand counts provided. |
| 3955 | return 2 + NumIndirectDests + NumArgs + NumBundleInputs; |
| 3956 | } |
| 3957 | |
| 3958 | protected: |
| 3959 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 3960 | friend class Instruction; |
| 3961 | |
| 3962 | CallBrInst *cloneImpl() const; |
| 3963 | |
| 3964 | public: |
| 3965 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
| 3966 | BasicBlock *DefaultDest, |
| 3967 | ArrayRef<BasicBlock *> IndirectDests, |
| 3968 | ArrayRef<Value *> Args, const Twine &NameStr, |
| 3969 | Instruction *InsertBefore = nullptr) { |
| 3970 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
| 3971 | return new (NumOperands) |
| 3972 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, |
| 3973 | NumOperands, NameStr, InsertBefore); |
| 3974 | } |
| 3975 | |
| 3976 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
| 3977 | BasicBlock *DefaultDest, |
| 3978 | ArrayRef<BasicBlock *> IndirectDests, |
| 3979 | ArrayRef<Value *> Args, |
| 3980 | ArrayRef<OperandBundleDef> Bundles = None, |
| 3981 | const Twine &NameStr = "", |
| 3982 | Instruction *InsertBefore = nullptr) { |
| 3983 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
| 3984 | CountBundleInputs(Bundles)); |
| 3985 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 3986 | |
| 3987 | return new (NumOperands, DescriptorBytes) |
| 3988 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
| 3989 | NumOperands, NameStr, InsertBefore); |
| 3990 | } |
| 3991 | |
| 3992 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
| 3993 | BasicBlock *DefaultDest, |
| 3994 | ArrayRef<BasicBlock *> IndirectDests, |
| 3995 | ArrayRef<Value *> Args, const Twine &NameStr, |
| 3996 | BasicBlock *InsertAtEnd) { |
| 3997 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
| 3998 | return new (NumOperands) |
| 3999 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, |
| 4000 | NumOperands, NameStr, InsertAtEnd); |
| 4001 | } |
| 4002 | |
| 4003 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
| 4004 | BasicBlock *DefaultDest, |
| 4005 | ArrayRef<BasicBlock *> IndirectDests, |
| 4006 | ArrayRef<Value *> Args, |
| 4007 | ArrayRef<OperandBundleDef> Bundles, |
| 4008 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 4009 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
| 4010 | CountBundleInputs(Bundles)); |
| 4011 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
| 4012 | |
| 4013 | return new (NumOperands, DescriptorBytes) |
| 4014 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
| 4015 | NumOperands, NameStr, InsertAtEnd); |
| 4016 | } |
| 4017 | |
| 4018 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
| 4019 | ArrayRef<BasicBlock *> IndirectDests, |
| 4020 | ArrayRef<Value *> Args, const Twine &NameStr, |
| 4021 | Instruction *InsertBefore = nullptr) { |
| 4022 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
| 4023 | IndirectDests, Args, NameStr, InsertBefore); |
| 4024 | } |
| 4025 | |
| 4026 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
| 4027 | ArrayRef<BasicBlock *> IndirectDests, |
| 4028 | ArrayRef<Value *> Args, |
| 4029 | ArrayRef<OperandBundleDef> Bundles = None, |
| 4030 | const Twine &NameStr = "", |
| 4031 | Instruction *InsertBefore = nullptr) { |
| 4032 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
| 4033 | IndirectDests, Args, Bundles, NameStr, InsertBefore); |
| 4034 | } |
| 4035 | |
| 4036 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
| 4037 | ArrayRef<BasicBlock *> IndirectDests, |
| 4038 | ArrayRef<Value *> Args, const Twine &NameStr, |
| 4039 | BasicBlock *InsertAtEnd) { |
| 4040 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
| 4041 | IndirectDests, Args, NameStr, InsertAtEnd); |
| 4042 | } |
| 4043 | |
| 4044 | static CallBrInst *Create(FunctionCallee Func, |
| 4045 | BasicBlock *DefaultDest, |
| 4046 | ArrayRef<BasicBlock *> IndirectDests, |
| 4047 | ArrayRef<Value *> Args, |
| 4048 | ArrayRef<OperandBundleDef> Bundles, |
| 4049 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 4050 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
| 4051 | IndirectDests, Args, Bundles, NameStr, InsertAtEnd); |
| 4052 | } |
| 4053 | |
| 4054 | /// Create a clone of \p CBI with a different set of operand bundles and |
| 4055 | /// insert it before \p InsertPt. |
| 4056 | /// |
| 4057 | /// The returned callbr instruction is identical to \p CBI in every way |
| 4058 | /// except that the operand bundles for the new instruction are set to the |
| 4059 | /// operand bundles in \p Bundles. |
| 4060 | static CallBrInst *Create(CallBrInst *CBI, |
| 4061 | ArrayRef<OperandBundleDef> Bundles, |
| 4062 | Instruction *InsertPt = nullptr); |
| 4063 | |
| 4064 | /// Return the number of callbr indirect dest labels. |
| 4065 | /// |
| 4066 | unsigned getNumIndirectDests() const { return NumIndirectDests; } |
| 4067 | |
| 4068 | /// getIndirectDestLabel - Return the i-th indirect dest label. |
| 4069 | /// |
| 4070 | Value *getIndirectDestLabel(unsigned i) const { |
| 4071 | assert(i < getNumIndirectDests() && "Out of bounds!")((void)0); |
| 4072 | return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() + |
| 4073 | 1); |
| 4074 | } |
| 4075 | |
| 4076 | Value *getIndirectDestLabelUse(unsigned i) const { |
| 4077 | assert(i < getNumIndirectDests() && "Out of bounds!")((void)0); |
| 4078 | return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() + |
| 4079 | 1); |
| 4080 | } |
| 4081 | |
| 4082 | // Return the destination basic blocks... |
| 4083 | BasicBlock *getDefaultDest() const { |
| 4084 | return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1)); |
| 4085 | } |
| 4086 | BasicBlock *getIndirectDest(unsigned i) const { |
| 4087 | return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i)); |
| 4088 | } |
| 4089 | SmallVector<BasicBlock *, 16> getIndirectDests() const { |
| 4090 | SmallVector<BasicBlock *, 16> IndirectDests; |
| 4091 | for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i) |
| 4092 | IndirectDests.push_back(getIndirectDest(i)); |
| 4093 | return IndirectDests; |
| 4094 | } |
| 4095 | void setDefaultDest(BasicBlock *B) { |
| 4096 | *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B); |
| 4097 | } |
| 4098 | void setIndirectDest(unsigned i, BasicBlock *B) { |
| 4099 | updateArgBlockAddresses(i, B); |
| 4100 | *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B); |
| 4101 | } |
| 4102 | |
| 4103 | BasicBlock *getSuccessor(unsigned i) const { |
| 4104 | assert(i < getNumSuccessors() + 1 &&((void)0) |
| 4105 | "Successor # out of range for callbr!")((void)0); |
| 4106 | return i == 0 ? getDefaultDest() : getIndirectDest(i - 1); |
| 4107 | } |
| 4108 | |
| 4109 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
| 4110 | assert(i < getNumIndirectDests() + 1 &&((void)0) |
| 4111 | "Successor # out of range for callbr!")((void)0); |
| 4112 | return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc); |
| 4113 | } |
| 4114 | |
| 4115 | unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; } |
| 4116 | |
| 4117 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4118 | static bool classof(const Instruction *I) { |
| 4119 | return (I->getOpcode() == Instruction::CallBr); |
| 4120 | } |
| 4121 | static bool classof(const Value *V) { |
| 4122 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4123 | } |
| 4124 | |
| 4125 | private: |
| 4126 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 4127 | // method so that subclasses cannot accidentally use it. |
| 4128 | template <typename Bitfield> |
| 4129 | void setSubclassData(typename Bitfield::Type Value) { |
| 4130 | Instruction::setSubclassData<Bitfield>(Value); |
| 4131 | } |
| 4132 | }; |
| 4133 | |
| 4134 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
| 4135 | ArrayRef<BasicBlock *> IndirectDests, |
| 4136 | ArrayRef<Value *> Args, |
| 4137 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 4138 | const Twine &NameStr, Instruction *InsertBefore) |
| 4139 | : CallBase(Ty->getReturnType(), Instruction::CallBr, |
| 4140 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
| 4141 | InsertBefore) { |
| 4142 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
| 4143 | } |
| 4144 | |
| 4145 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
| 4146 | ArrayRef<BasicBlock *> IndirectDests, |
| 4147 | ArrayRef<Value *> Args, |
| 4148 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
| 4149 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 4150 | : CallBase(Ty->getReturnType(), Instruction::CallBr, |
| 4151 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
| 4152 | InsertAtEnd) { |
| 4153 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
| 4154 | } |
| 4155 | |
| 4156 | //===----------------------------------------------------------------------===// |
| 4157 | // ResumeInst Class |
| 4158 | //===----------------------------------------------------------------------===// |
| 4159 | |
| 4160 | //===--------------------------------------------------------------------------- |
| 4161 | /// Resume the propagation of an exception. |
| 4162 | /// |
| 4163 | class ResumeInst : public Instruction { |
| 4164 | ResumeInst(const ResumeInst &RI); |
| 4165 | |
| 4166 | explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); |
| 4167 | ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); |
| 4168 | |
| 4169 | protected: |
| 4170 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4171 | friend class Instruction; |
| 4172 | |
| 4173 | ResumeInst *cloneImpl() const; |
| 4174 | |
| 4175 | public: |
| 4176 | static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { |
| 4177 | return new(1) ResumeInst(Exn, InsertBefore); |
| 4178 | } |
| 4179 | |
| 4180 | static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { |
| 4181 | return new(1) ResumeInst(Exn, InsertAtEnd); |
| 4182 | } |
| 4183 | |
| 4184 | /// Provide fast operand accessors |
| 4185 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 4186 | |
| 4187 | /// Convenience accessor. |
| 4188 | Value *getValue() const { return Op<0>(); } |
| 4189 | |
| 4190 | unsigned getNumSuccessors() const { return 0; } |
| 4191 | |
| 4192 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4193 | static bool classof(const Instruction *I) { |
| 4194 | return I->getOpcode() == Instruction::Resume; |
| 4195 | } |
| 4196 | static bool classof(const Value *V) { |
| 4197 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4198 | } |
| 4199 | |
| 4200 | private: |
| 4201 | BasicBlock *getSuccessor(unsigned idx) const { |
| 4202 | llvm_unreachable("ResumeInst has no successors!")__builtin_unreachable(); |
| 4203 | } |
| 4204 | |
| 4205 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
| 4206 | llvm_unreachable("ResumeInst has no successors!")__builtin_unreachable(); |
| 4207 | } |
| 4208 | }; |
| 4209 | |
| 4210 | template <> |
| 4211 | struct OperandTraits<ResumeInst> : |
| 4212 | public FixedNumOperandTraits<ResumeInst, 1> { |
| 4213 | }; |
| 4214 | |
| 4215 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)ResumeInst::op_iterator ResumeInst::op_begin() { return OperandTraits <ResumeInst>::op_begin(this); } ResumeInst::const_op_iterator ResumeInst::op_begin() const { return OperandTraits<ResumeInst >::op_begin(const_cast<ResumeInst*>(this)); } ResumeInst ::op_iterator ResumeInst::op_end() { return OperandTraits< ResumeInst>::op_end(this); } ResumeInst::const_op_iterator ResumeInst::op_end() const { return OperandTraits<ResumeInst >::op_end(const_cast<ResumeInst*>(this)); } Value *ResumeInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<ResumeInst>::op_begin(const_cast <ResumeInst*>(this))[i_nocapture].get()); } void ResumeInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( void)0); OperandTraits<ResumeInst>::op_begin(this)[i_nocapture ] = Val_nocapture; } unsigned ResumeInst::getNumOperands() const { return OperandTraits<ResumeInst>::operands(this); } template <int Idx_nocapture> Use &ResumeInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &ResumeInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } |
| 4216 | |
| 4217 | //===----------------------------------------------------------------------===// |
| 4218 | // CatchSwitchInst Class |
| 4219 | //===----------------------------------------------------------------------===// |
| 4220 | class CatchSwitchInst : public Instruction { |
| 4221 | using UnwindDestField = BoolBitfieldElementT<0>; |
| 4222 | |
| 4223 | /// The number of operands actually allocated. NumOperands is |
| 4224 | /// the number actually in use. |
| 4225 | unsigned ReservedSpace; |
| 4226 | |
| 4227 | // Operand[0] = Outer scope |
| 4228 | // Operand[1] = Unwind block destination |
| 4229 | // Operand[n] = BasicBlock to go to on match |
| 4230 | CatchSwitchInst(const CatchSwitchInst &CSI); |
| 4231 | |
| 4232 | /// Create a new switch instruction, specifying a |
| 4233 | /// default destination. The number of additional handlers can be specified |
| 4234 | /// here to make memory allocation more efficient. |
| 4235 | /// This constructor can also autoinsert before another instruction. |
| 4236 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 4237 | unsigned NumHandlers, const Twine &NameStr, |
| 4238 | Instruction *InsertBefore); |
| 4239 | |
| 4240 | /// Create a new switch instruction, specifying a |
| 4241 | /// default destination. The number of additional handlers can be specified |
| 4242 | /// here to make memory allocation more efficient. |
| 4243 | /// This constructor also autoinserts at the end of the specified BasicBlock. |
| 4244 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
| 4245 | unsigned NumHandlers, const Twine &NameStr, |
| 4246 | BasicBlock *InsertAtEnd); |
| 4247 | |
| 4248 | // allocate space for exactly zero operands |
| 4249 | void *operator new(size_t S) { return User::operator new(S); } |
| 4250 | |
| 4251 | void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); |
| 4252 | void growOperands(unsigned Size); |
| 4253 | |
| 4254 | protected: |
| 4255 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4256 | friend class Instruction; |
| 4257 | |
| 4258 | CatchSwitchInst *cloneImpl() const; |
| 4259 | |
| 4260 | public: |
| 4261 | void operator delete(void *Ptr) { return User::operator delete(Ptr); } |
| 4262 | |
| 4263 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
| 4264 | unsigned NumHandlers, |
| 4265 | const Twine &NameStr = "", |
| 4266 | Instruction *InsertBefore = nullptr) { |
| 4267 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
| 4268 | InsertBefore); |
| 4269 | } |
| 4270 | |
| 4271 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
| 4272 | unsigned NumHandlers, const Twine &NameStr, |
| 4273 | BasicBlock *InsertAtEnd) { |
| 4274 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
| 4275 | InsertAtEnd); |
| 4276 | } |
| 4277 | |
| 4278 | /// Provide fast operand accessors |
| 4279 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 4280 | |
| 4281 | // Accessor Methods for CatchSwitch stmt |
| 4282 | Value *getParentPad() const { return getOperand(0); } |
| 4283 | void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } |
| 4284 | |
| 4285 | // Accessor Methods for CatchSwitch stmt |
| 4286 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } |
| 4287 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
| 4288 | BasicBlock *getUnwindDest() const { |
| 4289 | if (hasUnwindDest()) |
| 4290 | return cast<BasicBlock>(getOperand(1)); |
| 4291 | return nullptr; |
| 4292 | } |
| 4293 | void setUnwindDest(BasicBlock *UnwindDest) { |
| 4294 | assert(UnwindDest)((void)0); |
| 4295 | assert(hasUnwindDest())((void)0); |
| 4296 | setOperand(1, UnwindDest); |
| 4297 | } |
| 4298 | |
| 4299 | /// return the number of 'handlers' in this catchswitch |
| 4300 | /// instruction, except the default handler |
| 4301 | unsigned getNumHandlers() const { |
| 4302 | if (hasUnwindDest()) |
| 4303 | return getNumOperands() - 2; |
| 4304 | return getNumOperands() - 1; |
| 4305 | } |
| 4306 | |
| 4307 | private: |
| 4308 | static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } |
| 4309 | static const BasicBlock *handler_helper(const Value *V) { |
| 4310 | return cast<BasicBlock>(V); |
| 4311 | } |
| 4312 | |
| 4313 | public: |
| 4314 | using DerefFnTy = BasicBlock *(*)(Value *); |
| 4315 | using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>; |
| 4316 | using handler_range = iterator_range<handler_iterator>; |
| 4317 | using ConstDerefFnTy = const BasicBlock *(*)(const Value *); |
| 4318 | using const_handler_iterator = |
| 4319 | mapped_iterator<const_op_iterator, ConstDerefFnTy>; |
| 4320 | using const_handler_range = iterator_range<const_handler_iterator>; |
| 4321 | |
| 4322 | /// Returns an iterator that points to the first handler in CatchSwitchInst. |
| 4323 | handler_iterator handler_begin() { |
| 4324 | op_iterator It = op_begin() + 1; |
| 4325 | if (hasUnwindDest()) |
| 4326 | ++It; |
| 4327 | return handler_iterator(It, DerefFnTy(handler_helper)); |
| 4328 | } |
| 4329 | |
| 4330 | /// Returns an iterator that points to the first handler in the |
| 4331 | /// CatchSwitchInst. |
| 4332 | const_handler_iterator handler_begin() const { |
| 4333 | const_op_iterator It = op_begin() + 1; |
| 4334 | if (hasUnwindDest()) |
| 4335 | ++It; |
| 4336 | return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); |
| 4337 | } |
| 4338 | |
| 4339 | /// Returns a read-only iterator that points one past the last |
| 4340 | /// handler in the CatchSwitchInst. |
| 4341 | handler_iterator handler_end() { |
| 4342 | return handler_iterator(op_end(), DerefFnTy(handler_helper)); |
| 4343 | } |
| 4344 | |
| 4345 | /// Returns an iterator that points one past the last handler in the |
| 4346 | /// CatchSwitchInst. |
| 4347 | const_handler_iterator handler_end() const { |
| 4348 | return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); |
| 4349 | } |
| 4350 | |
| 4351 | /// iteration adapter for range-for loops. |
| 4352 | handler_range handlers() { |
| 4353 | return make_range(handler_begin(), handler_end()); |
| 4354 | } |
| 4355 | |
| 4356 | /// iteration adapter for range-for loops. |
| 4357 | const_handler_range handlers() const { |
| 4358 | return make_range(handler_begin(), handler_end()); |
| 4359 | } |
| 4360 | |
| 4361 | /// Add an entry to the switch instruction... |
| 4362 | /// Note: |
| 4363 | /// This action invalidates handler_end(). Old handler_end() iterator will |
| 4364 | /// point to the added handler. |
| 4365 | void addHandler(BasicBlock *Dest); |
| 4366 | |
| 4367 | void removeHandler(handler_iterator HI); |
| 4368 | |
| 4369 | unsigned getNumSuccessors() const { return getNumOperands() - 1; } |
| 4370 | BasicBlock *getSuccessor(unsigned Idx) const { |
| 4371 | assert(Idx < getNumSuccessors() &&((void)0) |
| 4372 | "Successor # out of range for catchswitch!")((void)0); |
| 4373 | return cast<BasicBlock>(getOperand(Idx + 1)); |
| 4374 | } |
| 4375 | void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { |
| 4376 | assert(Idx < getNumSuccessors() &&((void)0) |
| 4377 | "Successor # out of range for catchswitch!")((void)0); |
| 4378 | setOperand(Idx + 1, NewSucc); |
| 4379 | } |
| 4380 | |
| 4381 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4382 | static bool classof(const Instruction *I) { |
| 4383 | return I->getOpcode() == Instruction::CatchSwitch; |
| 4384 | } |
| 4385 | static bool classof(const Value *V) { |
| 4386 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4387 | } |
| 4388 | }; |
| 4389 | |
| 4390 | template <> |
| 4391 | struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; |
| 4392 | |
| 4393 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)CatchSwitchInst::op_iterator CatchSwitchInst::op_begin() { return OperandTraits<CatchSwitchInst>::op_begin(this); } CatchSwitchInst ::const_op_iterator CatchSwitchInst::op_begin() const { return OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this)); } CatchSwitchInst::op_iterator CatchSwitchInst ::op_end() { return OperandTraits<CatchSwitchInst>::op_end (this); } CatchSwitchInst::const_op_iterator CatchSwitchInst:: op_end() const { return OperandTraits<CatchSwitchInst>:: op_end(const_cast<CatchSwitchInst*>(this)); } Value *CatchSwitchInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<CatchSwitchInst>::op_begin (const_cast<CatchSwitchInst*>(this))[i_nocapture].get() ); } void CatchSwitchInst::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<CatchSwitchInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchSwitchInst::getNumOperands() const { return OperandTraits <CatchSwitchInst>::operands(this); } template <int Idx_nocapture > Use &CatchSwitchInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchSwitchInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
| 4394 | |
| 4395 | //===----------------------------------------------------------------------===// |
| 4396 | // CleanupPadInst Class |
| 4397 | //===----------------------------------------------------------------------===// |
| 4398 | class CleanupPadInst : public FuncletPadInst { |
| 4399 | private: |
| 4400 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
| 4401 | unsigned Values, const Twine &NameStr, |
| 4402 | Instruction *InsertBefore) |
| 4403 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
| 4404 | NameStr, InsertBefore) {} |
| 4405 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
| 4406 | unsigned Values, const Twine &NameStr, |
| 4407 | BasicBlock *InsertAtEnd) |
| 4408 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
| 4409 | NameStr, InsertAtEnd) {} |
| 4410 | |
| 4411 | public: |
| 4412 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None, |
| 4413 | const Twine &NameStr = "", |
| 4414 | Instruction *InsertBefore = nullptr) { |
| 4415 | unsigned Values = 1 + Args.size(); |
| 4416 | return new (Values) |
| 4417 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); |
| 4418 | } |
| 4419 | |
| 4420 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, |
| 4421 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 4422 | unsigned Values = 1 + Args.size(); |
| 4423 | return new (Values) |
| 4424 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); |
| 4425 | } |
| 4426 | |
| 4427 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4428 | static bool classof(const Instruction *I) { |
| 4429 | return I->getOpcode() == Instruction::CleanupPad; |
| 4430 | } |
| 4431 | static bool classof(const Value *V) { |
| 4432 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4433 | } |
| 4434 | }; |
| 4435 | |
| 4436 | //===----------------------------------------------------------------------===// |
| 4437 | // CatchPadInst Class |
| 4438 | //===----------------------------------------------------------------------===// |
| 4439 | class CatchPadInst : public FuncletPadInst { |
| 4440 | private: |
| 4441 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
| 4442 | unsigned Values, const Twine &NameStr, |
| 4443 | Instruction *InsertBefore) |
| 4444 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
| 4445 | NameStr, InsertBefore) {} |
| 4446 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
| 4447 | unsigned Values, const Twine &NameStr, |
| 4448 | BasicBlock *InsertAtEnd) |
| 4449 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
| 4450 | NameStr, InsertAtEnd) {} |
| 4451 | |
| 4452 | public: |
| 4453 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
| 4454 | const Twine &NameStr = "", |
| 4455 | Instruction *InsertBefore = nullptr) { |
| 4456 | unsigned Values = 1 + Args.size(); |
| 4457 | return new (Values) |
| 4458 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); |
| 4459 | } |
| 4460 | |
| 4461 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
| 4462 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
| 4463 | unsigned Values = 1 + Args.size(); |
| 4464 | return new (Values) |
| 4465 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); |
| 4466 | } |
| 4467 | |
| 4468 | /// Convenience accessors |
| 4469 | CatchSwitchInst *getCatchSwitch() const { |
| 4470 | return cast<CatchSwitchInst>(Op<-1>()); |
| 4471 | } |
| 4472 | void setCatchSwitch(Value *CatchSwitch) { |
| 4473 | assert(CatchSwitch)((void)0); |
| 4474 | Op<-1>() = CatchSwitch; |
| 4475 | } |
| 4476 | |
| 4477 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4478 | static bool classof(const Instruction *I) { |
| 4479 | return I->getOpcode() == Instruction::CatchPad; |
| 4480 | } |
| 4481 | static bool classof(const Value *V) { |
| 4482 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4483 | } |
| 4484 | }; |
| 4485 | |
| 4486 | //===----------------------------------------------------------------------===// |
| 4487 | // CatchReturnInst Class |
| 4488 | //===----------------------------------------------------------------------===// |
| 4489 | |
| 4490 | class CatchReturnInst : public Instruction { |
| 4491 | CatchReturnInst(const CatchReturnInst &RI); |
| 4492 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); |
| 4493 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); |
| 4494 | |
| 4495 | void init(Value *CatchPad, BasicBlock *BB); |
| 4496 | |
| 4497 | protected: |
| 4498 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4499 | friend class Instruction; |
| 4500 | |
| 4501 | CatchReturnInst *cloneImpl() const; |
| 4502 | |
| 4503 | public: |
| 4504 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
| 4505 | Instruction *InsertBefore = nullptr) { |
| 4506 | assert(CatchPad)((void)0); |
| 4507 | assert(BB)((void)0); |
| 4508 | return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); |
| 4509 | } |
| 4510 | |
| 4511 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
| 4512 | BasicBlock *InsertAtEnd) { |
| 4513 | assert(CatchPad)((void)0); |
| 4514 | assert(BB)((void)0); |
| 4515 | return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); |
| 4516 | } |
| 4517 | |
| 4518 | /// Provide fast operand accessors |
| 4519 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 4520 | |
| 4521 | /// Convenience accessors. |
| 4522 | CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } |
| 4523 | void setCatchPad(CatchPadInst *CatchPad) { |
| 4524 | assert(CatchPad)((void)0); |
| 4525 | Op<0>() = CatchPad; |
| 4526 | } |
| 4527 | |
| 4528 | BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } |
| 4529 | void setSuccessor(BasicBlock *NewSucc) { |
| 4530 | assert(NewSucc)((void)0); |
| 4531 | Op<1>() = NewSucc; |
| 4532 | } |
| 4533 | unsigned getNumSuccessors() const { return 1; } |
| 4534 | |
| 4535 | /// Get the parentPad of this catchret's catchpad's catchswitch. |
| 4536 | /// The successor block is implicitly a member of this funclet. |
| 4537 | Value *getCatchSwitchParentPad() const { |
| 4538 | return getCatchPad()->getCatchSwitch()->getParentPad(); |
| 4539 | } |
| 4540 | |
| 4541 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4542 | static bool classof(const Instruction *I) { |
| 4543 | return (I->getOpcode() == Instruction::CatchRet); |
| 4544 | } |
| 4545 | static bool classof(const Value *V) { |
| 4546 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4547 | } |
| 4548 | |
| 4549 | private: |
| 4550 | BasicBlock *getSuccessor(unsigned Idx) const { |
| 4551 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((void)0); |
| 4552 | return getSuccessor(); |
| 4553 | } |
| 4554 | |
| 4555 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
| 4556 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((void)0); |
| 4557 | setSuccessor(B); |
| 4558 | } |
| 4559 | }; |
| 4560 | |
| 4561 | template <> |
| 4562 | struct OperandTraits<CatchReturnInst> |
| 4563 | : public FixedNumOperandTraits<CatchReturnInst, 2> {}; |
| 4564 | |
| 4565 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)CatchReturnInst::op_iterator CatchReturnInst::op_begin() { return OperandTraits<CatchReturnInst>::op_begin(this); } CatchReturnInst ::const_op_iterator CatchReturnInst::op_begin() const { return OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this)); } CatchReturnInst::op_iterator CatchReturnInst ::op_end() { return OperandTraits<CatchReturnInst>::op_end (this); } CatchReturnInst::const_op_iterator CatchReturnInst:: op_end() const { return OperandTraits<CatchReturnInst>:: op_end(const_cast<CatchReturnInst*>(this)); } Value *CatchReturnInst ::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null <Value>( OperandTraits<CatchReturnInst>::op_begin (const_cast<CatchReturnInst*>(this))[i_nocapture].get() ); } void CatchReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture) { ((void)0); OperandTraits<CatchReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchReturnInst::getNumOperands() const { return OperandTraits <CatchReturnInst>::operands(this); } template <int Idx_nocapture > Use &CatchReturnInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchReturnInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } |
| 4566 | |
| 4567 | //===----------------------------------------------------------------------===// |
| 4568 | // CleanupReturnInst Class |
| 4569 | //===----------------------------------------------------------------------===// |
| 4570 | |
| 4571 | class CleanupReturnInst : public Instruction { |
| 4572 | using UnwindDestField = BoolBitfieldElementT<0>; |
| 4573 | |
| 4574 | private: |
| 4575 | CleanupReturnInst(const CleanupReturnInst &RI); |
| 4576 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
| 4577 | Instruction *InsertBefore = nullptr); |
| 4578 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
| 4579 | BasicBlock *InsertAtEnd); |
| 4580 | |
| 4581 | void init(Value *CleanupPad, BasicBlock *UnwindBB); |
| 4582 | |
| 4583 | protected: |
| 4584 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4585 | friend class Instruction; |
| 4586 | |
| 4587 | CleanupReturnInst *cloneImpl() const; |
| 4588 | |
| 4589 | public: |
| 4590 | static CleanupReturnInst *Create(Value *CleanupPad, |
| 4591 | BasicBlock *UnwindBB = nullptr, |
| 4592 | Instruction *InsertBefore = nullptr) { |
| 4593 | assert(CleanupPad)((void)0); |
| 4594 | unsigned Values = 1; |
| 4595 | if (UnwindBB) |
| 4596 | ++Values; |
| 4597 | return new (Values) |
| 4598 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); |
| 4599 | } |
| 4600 | |
| 4601 | static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, |
| 4602 | BasicBlock *InsertAtEnd) { |
| 4603 | assert(CleanupPad)((void)0); |
| 4604 | unsigned Values = 1; |
| 4605 | if (UnwindBB) |
| 4606 | ++Values; |
| 4607 | return new (Values) |
| 4608 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); |
| 4609 | } |
| 4610 | |
| 4611 | /// Provide fast operand accessors |
| 4612 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; |
| 4613 | |
| 4614 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } |
| 4615 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
| 4616 | |
| 4617 | /// Convenience accessor. |
| 4618 | CleanupPadInst *getCleanupPad() const { |
| 4619 | return cast<CleanupPadInst>(Op<0>()); |
| 4620 | } |
| 4621 | void setCleanupPad(CleanupPadInst *CleanupPad) { |
| 4622 | assert(CleanupPad)((void)0); |
| 4623 | Op<0>() = CleanupPad; |
| 4624 | } |
| 4625 | |
| 4626 | unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } |
| 4627 | |
| 4628 | BasicBlock *getUnwindDest() const { |
| 4629 | return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; |
| 4630 | } |
| 4631 | void setUnwindDest(BasicBlock *NewDest) { |
| 4632 | assert(NewDest)((void)0); |
| 4633 | assert(hasUnwindDest())((void)0); |
| 4634 | Op<1>() = NewDest; |
| 4635 | } |
| 4636 | |
| 4637 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4638 | static bool classof(const Instruction *I) { |
| 4639 | return (I->getOpcode() == Instruction::CleanupRet); |
| 4640 | } |
| 4641 | static bool classof(const Value *V) { |
| 4642 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4643 | } |
| 4644 | |
| 4645 | private: |
| 4646 | BasicBlock *getSuccessor(unsigned Idx) const { |
| 4647 | assert(Idx == 0)((void)0); |
| 4648 | return getUnwindDest(); |
| 4649 | } |
| 4650 | |
| 4651 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
| 4652 | assert(Idx == 0)((void)0); |
| 4653 | setUnwindDest(B); |
| 4654 | } |
| 4655 | |
| 4656 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
| 4657 | // method so that subclasses cannot accidentally use it. |
| 4658 | template <typename Bitfield> |
| 4659 | void setSubclassData(typename Bitfield::Type Value) { |
| 4660 | Instruction::setSubclassData<Bitfield>(Value); |
| 4661 | } |
| 4662 | }; |
| 4663 | |
| 4664 | template <> |
| 4665 | struct OperandTraits<CleanupReturnInst> |
| 4666 | : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; |
| 4667 | |
| 4668 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)CleanupReturnInst::op_iterator CleanupReturnInst::op_begin() { return OperandTraits<CleanupReturnInst>::op_begin(this ); } CleanupReturnInst::const_op_iterator CleanupReturnInst:: op_begin() const { return OperandTraits<CleanupReturnInst> ::op_begin(const_cast<CleanupReturnInst*>(this)); } CleanupReturnInst ::op_iterator CleanupReturnInst::op_end() { return OperandTraits <CleanupReturnInst>::op_end(this); } CleanupReturnInst:: const_op_iterator CleanupReturnInst::op_end() const { return OperandTraits <CleanupReturnInst>::op_end(const_cast<CleanupReturnInst *>(this)); } Value *CleanupReturnInst::getOperand(unsigned i_nocapture) const { ((void)0); return cast_or_null<Value >( OperandTraits<CleanupReturnInst>::op_begin(const_cast <CleanupReturnInst*>(this))[i_nocapture].get()); } void CleanupReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((void)0); OperandTraits<CleanupReturnInst>::op_begin (this)[i_nocapture] = Val_nocapture; } unsigned CleanupReturnInst ::getNumOperands() const { return OperandTraits<CleanupReturnInst >::operands(this); } template <int Idx_nocapture> Use &CleanupReturnInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & CleanupReturnInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } |
| 4669 | |
| 4670 | //===----------------------------------------------------------------------===// |
| 4671 | // UnreachableInst Class |
| 4672 | //===----------------------------------------------------------------------===// |
| 4673 | |
| 4674 | //===--------------------------------------------------------------------------- |
| 4675 | /// This function has undefined behavior. In particular, the |
| 4676 | /// presence of this instruction indicates some higher level knowledge that the |
| 4677 | /// end of the block cannot be reached. |
| 4678 | /// |
| 4679 | class UnreachableInst : public Instruction { |
| 4680 | protected: |
| 4681 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4682 | friend class Instruction; |
| 4683 | |
| 4684 | UnreachableInst *cloneImpl() const; |
| 4685 | |
| 4686 | public: |
| 4687 | explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); |
| 4688 | explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
| 4689 | |
| 4690 | // allocate space for exactly zero operands |
| 4691 | void *operator new(size_t S) { return User::operator new(S, 0); } |
| 4692 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
| 4693 | |
| 4694 | unsigned getNumSuccessors() const { return 0; } |
| 4695 | |
| 4696 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4697 | static bool classof(const Instruction *I) { |
| 4698 | return I->getOpcode() == Instruction::Unreachable; |
| 4699 | } |
| 4700 | static bool classof(const Value *V) { |
| 4701 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4702 | } |
| 4703 | |
| 4704 | private: |
| 4705 | BasicBlock *getSuccessor(unsigned idx) const { |
| 4706 | llvm_unreachable("UnreachableInst has no successors!")__builtin_unreachable(); |
| 4707 | } |
| 4708 | |
| 4709 | void setSuccessor(unsigned idx, BasicBlock *B) { |
| 4710 | llvm_unreachable("UnreachableInst has no successors!")__builtin_unreachable(); |
| 4711 | } |
| 4712 | }; |
| 4713 | |
| 4714 | //===----------------------------------------------------------------------===// |
| 4715 | // TruncInst Class |
| 4716 | //===----------------------------------------------------------------------===// |
| 4717 | |
| 4718 | /// This class represents a truncation of integer types. |
| 4719 | class TruncInst : public CastInst { |
| 4720 | protected: |
| 4721 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4722 | friend class Instruction; |
| 4723 | |
| 4724 | /// Clone an identical TruncInst |
| 4725 | TruncInst *cloneImpl() const; |
| 4726 | |
| 4727 | public: |
| 4728 | /// Constructor with insert-before-instruction semantics |
| 4729 | TruncInst( |
| 4730 | Value *S, ///< The value to be truncated |
| 4731 | Type *Ty, ///< The (smaller) type to truncate to |
| 4732 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4733 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4734 | ); |
| 4735 | |
| 4736 | /// Constructor with insert-at-end-of-block semantics |
| 4737 | TruncInst( |
| 4738 | Value *S, ///< The value to be truncated |
| 4739 | Type *Ty, ///< The (smaller) type to truncate to |
| 4740 | const Twine &NameStr, ///< A name for the new instruction |
| 4741 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4742 | ); |
| 4743 | |
| 4744 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4745 | static bool classof(const Instruction *I) { |
| 4746 | return I->getOpcode() == Trunc; |
| 4747 | } |
| 4748 | static bool classof(const Value *V) { |
| 4749 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4750 | } |
| 4751 | }; |
| 4752 | |
| 4753 | //===----------------------------------------------------------------------===// |
| 4754 | // ZExtInst Class |
| 4755 | //===----------------------------------------------------------------------===// |
| 4756 | |
| 4757 | /// This class represents zero extension of integer types. |
| 4758 | class ZExtInst : public CastInst { |
| 4759 | protected: |
| 4760 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4761 | friend class Instruction; |
| 4762 | |
| 4763 | /// Clone an identical ZExtInst |
| 4764 | ZExtInst *cloneImpl() const; |
| 4765 | |
| 4766 | public: |
| 4767 | /// Constructor with insert-before-instruction semantics |
| 4768 | ZExtInst( |
| 4769 | Value *S, ///< The value to be zero extended |
| 4770 | Type *Ty, ///< The type to zero extend to |
| 4771 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4772 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4773 | ); |
| 4774 | |
| 4775 | /// Constructor with insert-at-end semantics. |
| 4776 | ZExtInst( |
| 4777 | Value *S, ///< The value to be zero extended |
| 4778 | Type *Ty, ///< The type to zero extend to |
| 4779 | const Twine &NameStr, ///< A name for the new instruction |
| 4780 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4781 | ); |
| 4782 | |
| 4783 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4784 | static bool classof(const Instruction *I) { |
| 4785 | return I->getOpcode() == ZExt; |
| 4786 | } |
| 4787 | static bool classof(const Value *V) { |
| 4788 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4789 | } |
| 4790 | }; |
| 4791 | |
| 4792 | //===----------------------------------------------------------------------===// |
| 4793 | // SExtInst Class |
| 4794 | //===----------------------------------------------------------------------===// |
| 4795 | |
| 4796 | /// This class represents a sign extension of integer types. |
| 4797 | class SExtInst : public CastInst { |
| 4798 | protected: |
| 4799 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4800 | friend class Instruction; |
| 4801 | |
| 4802 | /// Clone an identical SExtInst |
| 4803 | SExtInst *cloneImpl() const; |
| 4804 | |
| 4805 | public: |
| 4806 | /// Constructor with insert-before-instruction semantics |
| 4807 | SExtInst( |
| 4808 | Value *S, ///< The value to be sign extended |
| 4809 | Type *Ty, ///< The type to sign extend to |
| 4810 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4811 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4812 | ); |
| 4813 | |
| 4814 | /// Constructor with insert-at-end-of-block semantics |
| 4815 | SExtInst( |
| 4816 | Value *S, ///< The value to be sign extended |
| 4817 | Type *Ty, ///< The type to sign extend to |
| 4818 | const Twine &NameStr, ///< A name for the new instruction |
| 4819 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4820 | ); |
| 4821 | |
| 4822 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4823 | static bool classof(const Instruction *I) { |
| 4824 | return I->getOpcode() == SExt; |
| 4825 | } |
| 4826 | static bool classof(const Value *V) { |
| 4827 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4828 | } |
| 4829 | }; |
| 4830 | |
| 4831 | //===----------------------------------------------------------------------===// |
| 4832 | // FPTruncInst Class |
| 4833 | //===----------------------------------------------------------------------===// |
| 4834 | |
| 4835 | /// This class represents a truncation of floating point types. |
| 4836 | class FPTruncInst : public CastInst { |
| 4837 | protected: |
| 4838 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4839 | friend class Instruction; |
| 4840 | |
| 4841 | /// Clone an identical FPTruncInst |
| 4842 | FPTruncInst *cloneImpl() const; |
| 4843 | |
| 4844 | public: |
| 4845 | /// Constructor with insert-before-instruction semantics |
| 4846 | FPTruncInst( |
| 4847 | Value *S, ///< The value to be truncated |
| 4848 | Type *Ty, ///< The type to truncate to |
| 4849 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4850 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4851 | ); |
| 4852 | |
| 4853 | /// Constructor with insert-before-instruction semantics |
| 4854 | FPTruncInst( |
| 4855 | Value *S, ///< The value to be truncated |
| 4856 | Type *Ty, ///< The type to truncate to |
| 4857 | const Twine &NameStr, ///< A name for the new instruction |
| 4858 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4859 | ); |
| 4860 | |
| 4861 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4862 | static bool classof(const Instruction *I) { |
| 4863 | return I->getOpcode() == FPTrunc; |
| 4864 | } |
| 4865 | static bool classof(const Value *V) { |
| 4866 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4867 | } |
| 4868 | }; |
| 4869 | |
| 4870 | //===----------------------------------------------------------------------===// |
| 4871 | // FPExtInst Class |
| 4872 | //===----------------------------------------------------------------------===// |
| 4873 | |
| 4874 | /// This class represents an extension of floating point types. |
| 4875 | class FPExtInst : public CastInst { |
| 4876 | protected: |
| 4877 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4878 | friend class Instruction; |
| 4879 | |
| 4880 | /// Clone an identical FPExtInst |
| 4881 | FPExtInst *cloneImpl() const; |
| 4882 | |
| 4883 | public: |
| 4884 | /// Constructor with insert-before-instruction semantics |
| 4885 | FPExtInst( |
| 4886 | Value *S, ///< The value to be extended |
| 4887 | Type *Ty, ///< The type to extend to |
| 4888 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4889 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4890 | ); |
| 4891 | |
| 4892 | /// Constructor with insert-at-end-of-block semantics |
| 4893 | FPExtInst( |
| 4894 | Value *S, ///< The value to be extended |
| 4895 | Type *Ty, ///< The type to extend to |
| 4896 | const Twine &NameStr, ///< A name for the new instruction |
| 4897 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4898 | ); |
| 4899 | |
| 4900 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4901 | static bool classof(const Instruction *I) { |
| 4902 | return I->getOpcode() == FPExt; |
| 4903 | } |
| 4904 | static bool classof(const Value *V) { |
| 4905 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4906 | } |
| 4907 | }; |
| 4908 | |
| 4909 | //===----------------------------------------------------------------------===// |
| 4910 | // UIToFPInst Class |
| 4911 | //===----------------------------------------------------------------------===// |
| 4912 | |
| 4913 | /// This class represents a cast unsigned integer to floating point. |
| 4914 | class UIToFPInst : public CastInst { |
| 4915 | protected: |
| 4916 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4917 | friend class Instruction; |
| 4918 | |
| 4919 | /// Clone an identical UIToFPInst |
| 4920 | UIToFPInst *cloneImpl() const; |
| 4921 | |
| 4922 | public: |
| 4923 | /// Constructor with insert-before-instruction semantics |
| 4924 | UIToFPInst( |
| 4925 | Value *S, ///< The value to be converted |
| 4926 | Type *Ty, ///< The type to convert to |
| 4927 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4928 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4929 | ); |
| 4930 | |
| 4931 | /// Constructor with insert-at-end-of-block semantics |
| 4932 | UIToFPInst( |
| 4933 | Value *S, ///< The value to be converted |
| 4934 | Type *Ty, ///< The type to convert to |
| 4935 | const Twine &NameStr, ///< A name for the new instruction |
| 4936 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4937 | ); |
| 4938 | |
| 4939 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4940 | static bool classof(const Instruction *I) { |
| 4941 | return I->getOpcode() == UIToFP; |
| 4942 | } |
| 4943 | static bool classof(const Value *V) { |
| 4944 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4945 | } |
| 4946 | }; |
| 4947 | |
| 4948 | //===----------------------------------------------------------------------===// |
| 4949 | // SIToFPInst Class |
| 4950 | //===----------------------------------------------------------------------===// |
| 4951 | |
| 4952 | /// This class represents a cast from signed integer to floating point. |
| 4953 | class SIToFPInst : public CastInst { |
| 4954 | protected: |
| 4955 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4956 | friend class Instruction; |
| 4957 | |
| 4958 | /// Clone an identical SIToFPInst |
| 4959 | SIToFPInst *cloneImpl() const; |
| 4960 | |
| 4961 | public: |
| 4962 | /// Constructor with insert-before-instruction semantics |
| 4963 | SIToFPInst( |
| 4964 | Value *S, ///< The value to be converted |
| 4965 | Type *Ty, ///< The type to convert to |
| 4966 | const Twine &NameStr = "", ///< A name for the new instruction |
| 4967 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 4968 | ); |
| 4969 | |
| 4970 | /// Constructor with insert-at-end-of-block semantics |
| 4971 | SIToFPInst( |
| 4972 | Value *S, ///< The value to be converted |
| 4973 | Type *Ty, ///< The type to convert to |
| 4974 | const Twine &NameStr, ///< A name for the new instruction |
| 4975 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 4976 | ); |
| 4977 | |
| 4978 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 4979 | static bool classof(const Instruction *I) { |
| 4980 | return I->getOpcode() == SIToFP; |
| 4981 | } |
| 4982 | static bool classof(const Value *V) { |
| 4983 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 4984 | } |
| 4985 | }; |
| 4986 | |
| 4987 | //===----------------------------------------------------------------------===// |
| 4988 | // FPToUIInst Class |
| 4989 | //===----------------------------------------------------------------------===// |
| 4990 | |
| 4991 | /// This class represents a cast from floating point to unsigned integer |
| 4992 | class FPToUIInst : public CastInst { |
| 4993 | protected: |
| 4994 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 4995 | friend class Instruction; |
| 4996 | |
| 4997 | /// Clone an identical FPToUIInst |
| 4998 | FPToUIInst *cloneImpl() const; |
| 4999 | |
| 5000 | public: |
| 5001 | /// Constructor with insert-before-instruction semantics |
| 5002 | FPToUIInst( |
| 5003 | Value *S, ///< The value to be converted |
| 5004 | Type *Ty, ///< The type to convert to |
| 5005 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5006 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5007 | ); |
| 5008 | |
| 5009 | /// Constructor with insert-at-end-of-block semantics |
| 5010 | FPToUIInst( |
| 5011 | Value *S, ///< The value to be converted |
| 5012 | Type *Ty, ///< The type to convert to |
| 5013 | const Twine &NameStr, ///< A name for the new instruction |
| 5014 | BasicBlock *InsertAtEnd ///< Where to insert the new instruction |
| 5015 | ); |
| 5016 | |
| 5017 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5018 | static bool classof(const Instruction *I) { |
| 5019 | return I->getOpcode() == FPToUI; |
| 5020 | } |
| 5021 | static bool classof(const Value *V) { |
| 5022 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5023 | } |
| 5024 | }; |
| 5025 | |
| 5026 | //===----------------------------------------------------------------------===// |
| 5027 | // FPToSIInst Class |
| 5028 | //===----------------------------------------------------------------------===// |
| 5029 | |
| 5030 | /// This class represents a cast from floating point to signed integer. |
| 5031 | class FPToSIInst : public CastInst { |
| 5032 | protected: |
| 5033 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5034 | friend class Instruction; |
| 5035 | |
| 5036 | /// Clone an identical FPToSIInst |
| 5037 | FPToSIInst *cloneImpl() const; |
| 5038 | |
| 5039 | public: |
| 5040 | /// Constructor with insert-before-instruction semantics |
| 5041 | FPToSIInst( |
| 5042 | Value *S, ///< The value to be converted |
| 5043 | Type *Ty, ///< The type to convert to |
| 5044 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5045 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5046 | ); |
| 5047 | |
| 5048 | /// Constructor with insert-at-end-of-block semantics |
| 5049 | FPToSIInst( |
| 5050 | Value *S, ///< The value to be converted |
| 5051 | Type *Ty, ///< The type to convert to |
| 5052 | const Twine &NameStr, ///< A name for the new instruction |
| 5053 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 5054 | ); |
| 5055 | |
| 5056 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5057 | static bool classof(const Instruction *I) { |
| 5058 | return I->getOpcode() == FPToSI; |
| 5059 | } |
| 5060 | static bool classof(const Value *V) { |
| 5061 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5062 | } |
| 5063 | }; |
| 5064 | |
| 5065 | //===----------------------------------------------------------------------===// |
| 5066 | // IntToPtrInst Class |
| 5067 | //===----------------------------------------------------------------------===// |
| 5068 | |
| 5069 | /// This class represents a cast from an integer to a pointer. |
| 5070 | class IntToPtrInst : public CastInst { |
| 5071 | public: |
| 5072 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5073 | friend class Instruction; |
| 5074 | |
| 5075 | /// Constructor with insert-before-instruction semantics |
| 5076 | IntToPtrInst( |
| 5077 | Value *S, ///< The value to be converted |
| 5078 | Type *Ty, ///< The type to convert to |
| 5079 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5080 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5081 | ); |
| 5082 | |
| 5083 | /// Constructor with insert-at-end-of-block semantics |
| 5084 | IntToPtrInst( |
| 5085 | Value *S, ///< The value to be converted |
| 5086 | Type *Ty, ///< The type to convert to |
| 5087 | const Twine &NameStr, ///< A name for the new instruction |
| 5088 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 5089 | ); |
| 5090 | |
| 5091 | /// Clone an identical IntToPtrInst. |
| 5092 | IntToPtrInst *cloneImpl() const; |
| 5093 | |
| 5094 | /// Returns the address space of this instruction's pointer type. |
| 5095 | unsigned getAddressSpace() const { |
| 5096 | return getType()->getPointerAddressSpace(); |
| 5097 | } |
| 5098 | |
| 5099 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5100 | static bool classof(const Instruction *I) { |
| 5101 | return I->getOpcode() == IntToPtr; |
| 5102 | } |
| 5103 | static bool classof(const Value *V) { |
| 5104 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5105 | } |
| 5106 | }; |
| 5107 | |
| 5108 | //===----------------------------------------------------------------------===// |
| 5109 | // PtrToIntInst Class |
| 5110 | //===----------------------------------------------------------------------===// |
| 5111 | |
| 5112 | /// This class represents a cast from a pointer to an integer. |
| 5113 | class PtrToIntInst : public CastInst { |
| 5114 | protected: |
| 5115 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5116 | friend class Instruction; |
| 5117 | |
| 5118 | /// Clone an identical PtrToIntInst. |
| 5119 | PtrToIntInst *cloneImpl() const; |
| 5120 | |
| 5121 | public: |
| 5122 | /// Constructor with insert-before-instruction semantics |
| 5123 | PtrToIntInst( |
| 5124 | Value *S, ///< The value to be converted |
| 5125 | Type *Ty, ///< The type to convert to |
| 5126 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5127 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5128 | ); |
| 5129 | |
| 5130 | /// Constructor with insert-at-end-of-block semantics |
| 5131 | PtrToIntInst( |
| 5132 | Value *S, ///< The value to be converted |
| 5133 | Type *Ty, ///< The type to convert to |
| 5134 | const Twine &NameStr, ///< A name for the new instruction |
| 5135 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 5136 | ); |
| 5137 | |
| 5138 | /// Gets the pointer operand. |
| 5139 | Value *getPointerOperand() { return getOperand(0); } |
| 5140 | /// Gets the pointer operand. |
| 5141 | const Value *getPointerOperand() const { return getOperand(0); } |
| 5142 | /// Gets the operand index of the pointer operand. |
| 5143 | static unsigned getPointerOperandIndex() { return 0U; } |
| 5144 | |
| 5145 | /// Returns the address space of the pointer operand. |
| 5146 | unsigned getPointerAddressSpace() const { |
| 5147 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
| 5148 | } |
| 5149 | |
| 5150 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5151 | static bool classof(const Instruction *I) { |
| 5152 | return I->getOpcode() == PtrToInt; |
| 5153 | } |
| 5154 | static bool classof(const Value *V) { |
| 5155 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5156 | } |
| 5157 | }; |
| 5158 | |
| 5159 | //===----------------------------------------------------------------------===// |
| 5160 | // BitCastInst Class |
| 5161 | //===----------------------------------------------------------------------===// |
| 5162 | |
| 5163 | /// This class represents a no-op cast from one type to another. |
| 5164 | class BitCastInst : public CastInst { |
| 5165 | protected: |
| 5166 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5167 | friend class Instruction; |
| 5168 | |
| 5169 | /// Clone an identical BitCastInst. |
| 5170 | BitCastInst *cloneImpl() const; |
| 5171 | |
| 5172 | public: |
| 5173 | /// Constructor with insert-before-instruction semantics |
| 5174 | BitCastInst( |
| 5175 | Value *S, ///< The value to be casted |
| 5176 | Type *Ty, ///< The type to casted to |
| 5177 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5178 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5179 | ); |
| 5180 | |
| 5181 | /// Constructor with insert-at-end-of-block semantics |
| 5182 | BitCastInst( |
| 5183 | Value *S, ///< The value to be casted |
| 5184 | Type *Ty, ///< The type to casted to |
| 5185 | const Twine &NameStr, ///< A name for the new instruction |
| 5186 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 5187 | ); |
| 5188 | |
| 5189 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5190 | static bool classof(const Instruction *I) { |
| 5191 | return I->getOpcode() == BitCast; |
| 5192 | } |
| 5193 | static bool classof(const Value *V) { |
| 5194 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5195 | } |
| 5196 | }; |
| 5197 | |
| 5198 | //===----------------------------------------------------------------------===// |
| 5199 | // AddrSpaceCastInst Class |
| 5200 | //===----------------------------------------------------------------------===// |
| 5201 | |
| 5202 | /// This class represents a conversion between pointers from one address space |
| 5203 | /// to another. |
| 5204 | class AddrSpaceCastInst : public CastInst { |
| 5205 | protected: |
| 5206 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5207 | friend class Instruction; |
| 5208 | |
| 5209 | /// Clone an identical AddrSpaceCastInst. |
| 5210 | AddrSpaceCastInst *cloneImpl() const; |
| 5211 | |
| 5212 | public: |
| 5213 | /// Constructor with insert-before-instruction semantics |
| 5214 | AddrSpaceCastInst( |
| 5215 | Value *S, ///< The value to be casted |
| 5216 | Type *Ty, ///< The type to casted to |
| 5217 | const Twine &NameStr = "", ///< A name for the new instruction |
| 5218 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
| 5219 | ); |
| 5220 | |
| 5221 | /// Constructor with insert-at-end-of-block semantics |
| 5222 | AddrSpaceCastInst( |
| 5223 | Value *S, ///< The value to be casted |
| 5224 | Type *Ty, ///< The type to casted to |
| 5225 | const Twine &NameStr, ///< A name for the new instruction |
| 5226 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
| 5227 | ); |
| 5228 | |
| 5229 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5230 | static bool classof(const Instruction *I) { |
| 5231 | return I->getOpcode() == AddrSpaceCast; |
| 5232 | } |
| 5233 | static bool classof(const Value *V) { |
| 5234 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5235 | } |
| 5236 | |
| 5237 | /// Gets the pointer operand. |
| 5238 | Value *getPointerOperand() { |
| 5239 | return getOperand(0); |
| 5240 | } |
| 5241 | |
| 5242 | /// Gets the pointer operand. |
| 5243 | const Value *getPointerOperand() const { |
| 5244 | return getOperand(0); |
| 5245 | } |
| 5246 | |
| 5247 | /// Gets the operand index of the pointer operand. |
| 5248 | static unsigned getPointerOperandIndex() { |
| 5249 | return 0U; |
| 5250 | } |
| 5251 | |
| 5252 | /// Returns the address space of the pointer operand. |
| 5253 | unsigned getSrcAddressSpace() const { |
| 5254 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
| 5255 | } |
| 5256 | |
| 5257 | /// Returns the address space of the result. |
| 5258 | unsigned getDestAddressSpace() const { |
| 5259 | return getType()->getPointerAddressSpace(); |
| 5260 | } |
| 5261 | }; |
| 5262 | |
| 5263 | /// A helper function that returns the pointer operand of a load or store |
| 5264 | /// instruction. Returns nullptr if not load or store. |
| 5265 | inline const Value *getLoadStorePointerOperand(const Value *V) { |
| 5266 | if (auto *Load = dyn_cast<LoadInst>(V)) |
| 5267 | return Load->getPointerOperand(); |
| 5268 | if (auto *Store = dyn_cast<StoreInst>(V)) |
| 5269 | return Store->getPointerOperand(); |
| 5270 | return nullptr; |
| 5271 | } |
| 5272 | inline Value *getLoadStorePointerOperand(Value *V) { |
| 5273 | return const_cast<Value *>( |
| 5274 | getLoadStorePointerOperand(static_cast<const Value *>(V))); |
| 5275 | } |
| 5276 | |
| 5277 | /// A helper function that returns the pointer operand of a load, store |
| 5278 | /// or GEP instruction. Returns nullptr if not load, store, or GEP. |
| 5279 | inline const Value *getPointerOperand(const Value *V) { |
| 5280 | if (auto *Ptr = getLoadStorePointerOperand(V)) |
| 5281 | return Ptr; |
| 5282 | if (auto *Gep = dyn_cast<GetElementPtrInst>(V)) |
| 5283 | return Gep->getPointerOperand(); |
| 5284 | return nullptr; |
| 5285 | } |
| 5286 | inline Value *getPointerOperand(Value *V) { |
| 5287 | return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V))); |
| 5288 | } |
| 5289 | |
| 5290 | /// A helper function that returns the alignment of load or store instruction. |
| 5291 | inline Align getLoadStoreAlignment(Value *I) { |
| 5292 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0) |
| 5293 | "Expected Load or Store instruction")((void)0); |
| 5294 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 5295 | return LI->getAlign(); |
| 5296 | return cast<StoreInst>(I)->getAlign(); |
| 5297 | } |
| 5298 | |
| 5299 | /// A helper function that returns the address space of the pointer operand of |
| 5300 | /// load or store instruction. |
| 5301 | inline unsigned getLoadStoreAddressSpace(Value *I) { |
| 5302 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0) |
| 5303 | "Expected Load or Store instruction")((void)0); |
| 5304 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 5305 | return LI->getPointerAddressSpace(); |
| 5306 | return cast<StoreInst>(I)->getPointerAddressSpace(); |
| 5307 | } |
| 5308 | |
| 5309 | /// A helper function that returns the type of a load or store instruction. |
| 5310 | inline Type *getLoadStoreType(Value *I) { |
| 5311 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&((void)0) |
| 5312 | "Expected Load or Store instruction")((void)0); |
| 5313 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 5314 | return LI->getType(); |
| 5315 | return cast<StoreInst>(I)->getValueOperand()->getType(); |
| 5316 | } |
| 5317 | |
| 5318 | //===----------------------------------------------------------------------===// |
| 5319 | // FreezeInst Class |
| 5320 | //===----------------------------------------------------------------------===// |
| 5321 | |
| 5322 | /// This class represents a freeze function that returns random concrete |
| 5323 | /// value if an operand is either a poison value or an undef value |
| 5324 | class FreezeInst : public UnaryInstruction { |
| 5325 | protected: |
| 5326 | // Note: Instruction needs to be a friend here to call cloneImpl. |
| 5327 | friend class Instruction; |
| 5328 | |
| 5329 | /// Clone an identical FreezeInst |
| 5330 | FreezeInst *cloneImpl() const; |
| 5331 | |
| 5332 | public: |
| 5333 | explicit FreezeInst(Value *S, |
| 5334 | const Twine &NameStr = "", |
| 5335 | Instruction *InsertBefore = nullptr); |
| 5336 | FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd); |
| 5337 | |
| 5338 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
| 5339 | static inline bool classof(const Instruction *I) { |
| 5340 | return I->getOpcode() == Freeze; |
| 5341 | } |
| 5342 | static inline bool classof(const Value *V) { |
| 5343 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
| 5344 | } |
| 5345 | }; |
| 5346 | |
| 5347 | } // end namespace llvm |
| 5348 | |
| 5349 | #endif // LLVM_IR_INSTRUCTIONS_H |
| 1 | //===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | // | |||
| 9 | // This file contains types to represent alignments. | |||
| 10 | // They are instrumented to guarantee some invariants are preserved and prevent | |||
| 11 | // invalid manipulations. | |||
| 12 | // | |||
| 13 | // - Align represents an alignment in bytes, it is always set and always a valid | |||
| 14 | // power of two, its minimum value is 1 which means no alignment requirements. | |||
| 15 | // | |||
| 16 | // - MaybeAlign is an optional type, it may be undefined or set. When it's set | |||
| 17 | // you can get the underlying Align type by using the getValue() method. | |||
| 18 | // | |||
| 19 | //===----------------------------------------------------------------------===// | |||
| 20 | ||||
| 21 | #ifndef LLVM_SUPPORT_ALIGNMENT_H_ | |||
| 22 | #define LLVM_SUPPORT_ALIGNMENT_H_ | |||
| 23 | ||||
| 24 | #include "llvm/ADT/Optional.h" | |||
| 25 | #include "llvm/Support/MathExtras.h" | |||
| 26 | #include <cassert> | |||
| 27 | #ifndef NDEBUG1 | |||
| 28 | #include <string> | |||
| 29 | #endif // NDEBUG | |||
| 30 | ||||
| 31 | namespace llvm { | |||
| 32 | ||||
| 33 | #define ALIGN_CHECK_ISPOSITIVE(decl) \ | |||
| 34 | assert(decl > 0 && (#decl " should be defined"))((void)0) | |||
| 35 | ||||
| 36 | /// This struct is a compact representation of a valid (non-zero power of two) | |||
| 37 | /// alignment. | |||
| 38 | /// It is suitable for use as static global constants. | |||
| 39 | struct Align { | |||
| 40 | private: | |||
| 41 | uint8_t ShiftValue = 0; /// The log2 of the required alignment. | |||
| 42 | /// ShiftValue is less than 64 by construction. | |||
| 43 | ||||
| 44 | friend struct MaybeAlign; | |||
| 45 | friend unsigned Log2(Align); | |||
| 46 | friend bool operator==(Align Lhs, Align Rhs); | |||
| 47 | friend bool operator!=(Align Lhs, Align Rhs); | |||
| 48 | friend bool operator<=(Align Lhs, Align Rhs); | |||
| 49 | friend bool operator>=(Align Lhs, Align Rhs); | |||
| 50 | friend bool operator<(Align Lhs, Align Rhs); | |||
| 51 | friend bool operator>(Align Lhs, Align Rhs); | |||
| 52 | friend unsigned encode(struct MaybeAlign A); | |||
| 53 | friend struct MaybeAlign decodeMaybeAlign(unsigned Value); | |||
| 54 | ||||
| 55 | /// A trivial type to allow construction of constexpr Align. | |||
| 56 | /// This is currently needed to workaround a bug in GCC 5.3 which prevents | |||
| 57 | /// definition of constexpr assign operators. | |||
| 58 | /// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic | |||
| 59 | /// FIXME: Remove this, make all assign operators constexpr and introduce user | |||
| 60 | /// defined literals when we don't have to support GCC 5.3 anymore. | |||
| 61 | /// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain | |||
| 62 | struct LogValue { | |||
| 63 | uint8_t Log; | |||
| 64 | }; | |||
| 65 | ||||
| 66 | public: | |||
| 67 | /// Default is byte-aligned. | |||
| 68 | constexpr Align() = default; | |||
| 69 | /// Do not perform checks in case of copy/move construct/assign, because the | |||
| 70 | /// checks have been performed when building `Other`. | |||
| 71 | constexpr Align(const Align &Other) = default; | |||
| 72 | constexpr Align(Align &&Other) = default; | |||
| 73 | Align &operator=(const Align &Other) = default; | |||
| 74 | Align &operator=(Align &&Other) = default; | |||
| 75 | ||||
| 76 | explicit Align(uint64_t Value) { | |||
| 77 | assert(Value > 0 && "Value must not be 0")((void)0); | |||
| 78 | assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2")((void)0); | |||
| 79 | ShiftValue = Log2_64(Value); | |||
| 80 | assert(ShiftValue < 64 && "Broken invariant")((void)0); | |||
| 81 | } | |||
| 82 | ||||
| 83 | /// This is a hole in the type system and should not be abused. | |||
| 84 | /// Needed to interact with C for instance. | |||
| 85 | uint64_t value() const { return uint64_t(1) << ShiftValue; } | |||
| ||||
| 86 | ||||
| 87 | /// Allow constructions of constexpr Align. | |||
| 88 | template <size_t kValue> constexpr static LogValue Constant() { | |||
| 89 | return LogValue{static_cast<uint8_t>(CTLog2<kValue>())}; | |||
| 90 | } | |||
| 91 | ||||
| 92 | /// Allow constructions of constexpr Align from types. | |||
| 93 | /// Compile time equivalent to Align(alignof(T)). | |||
| 94 | template <typename T> constexpr static LogValue Of() { | |||
| 95 | return Constant<std::alignment_of<T>::value>(); | |||
| 96 | } | |||
| 97 | ||||
| 98 | /// Constexpr constructor from LogValue type. | |||
| 99 | constexpr Align(LogValue CA) : ShiftValue(CA.Log) {} | |||
| 100 | }; | |||
| 101 | ||||
| 102 | /// Treats the value 0 as a 1, so Align is always at least 1. | |||
| 103 | inline Align assumeAligned(uint64_t Value) { | |||
| 104 | return Value ? Align(Value) : Align(); | |||
| 105 | } | |||
| 106 | ||||
| 107 | /// This struct is a compact representation of a valid (power of two) or | |||
| 108 | /// undefined (0) alignment. | |||
| 109 | struct MaybeAlign : public llvm::Optional<Align> { | |||
| 110 | private: | |||
| 111 | using UP = llvm::Optional<Align>; | |||
| 112 | ||||
| 113 | public: | |||
| 114 | /// Default is undefined. | |||
| 115 | MaybeAlign() = default; | |||
| 116 | /// Do not perform checks in case of copy/move construct/assign, because the | |||
| 117 | /// checks have been performed when building `Other`. | |||
| 118 | MaybeAlign(const MaybeAlign &Other) = default; | |||
| 119 | MaybeAlign &operator=(const MaybeAlign &Other) = default; | |||
| 120 | MaybeAlign(MaybeAlign &&Other) = default; | |||
| 121 | MaybeAlign &operator=(MaybeAlign &&Other) = default; | |||
| 122 | ||||
| 123 | /// Use llvm::Optional<Align> constructor. | |||
| 124 | using UP::UP; | |||
| 125 | ||||
| 126 | explicit MaybeAlign(uint64_t Value) { | |||
| 127 | assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&((void)0) | |||
| 128 | "Alignment is neither 0 nor a power of 2")((void)0); | |||
| 129 | if (Value) | |||
| 130 | emplace(Value); | |||
| 131 | } | |||
| 132 | ||||
| 133 | /// For convenience, returns a valid alignment or 1 if undefined. | |||
| 134 | Align valueOrOne() const { return hasValue() ? getValue() : Align(); } | |||
| 135 | }; | |||
| 136 | ||||
| 137 | /// Checks that SizeInBytes is a multiple of the alignment. | |||
| 138 | inline bool isAligned(Align Lhs, uint64_t SizeInBytes) { | |||
| 139 | return SizeInBytes % Lhs.value() == 0; | |||
| 140 | } | |||
| 141 | ||||
| 142 | /// Checks that Addr is a multiple of the alignment. | |||
| 143 | inline bool isAddrAligned(Align Lhs, const void *Addr) { | |||
| 144 | return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr)); | |||
| 145 | } | |||
| 146 | ||||
| 147 | /// Returns a multiple of A needed to store `Size` bytes. | |||
| 148 | inline uint64_t alignTo(uint64_t Size, Align A) { | |||
| 149 | const uint64_t Value = A.value(); | |||
| 150 | // The following line is equivalent to `(Size + Value - 1) / Value * Value`. | |||
| 151 | ||||
| 152 | // The division followed by a multiplication can be thought of as a right | |||
| 153 | // shift followed by a left shift which zeros out the extra bits produced in | |||
| 154 | // the bump; `~(Value - 1)` is a mask where all those bits being zeroed out | |||
| 155 | // are just zero. | |||
| 156 | ||||
| 157 | // Most compilers can generate this code but the pattern may be missed when | |||
| 158 | // multiple functions gets inlined. | |||
| 159 | return (Size + Value - 1) & ~(Value - 1U); | |||
| 160 | } | |||
| 161 | ||||
| 162 | /// If non-zero \p Skew is specified, the return value will be a minimal integer | |||
| 163 | /// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for | |||
| 164 | /// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p | |||
| 165 | /// Skew mod \p A'. | |||
| 166 | /// | |||
| 167 | /// Examples: | |||
| 168 | /// \code | |||
| 169 | /// alignTo(5, Align(8), 7) = 7 | |||
| 170 | /// alignTo(17, Align(8), 1) = 17 | |||
| 171 | /// alignTo(~0LL, Align(8), 3) = 3 | |||
| 172 | /// \endcode | |||
| 173 | inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) { | |||
| 174 | const uint64_t Value = A.value(); | |||
| 175 | Skew %= Value; | |||
| 176 | return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew; | |||
| 177 | } | |||
| 178 | ||||
| 179 | /// Returns a multiple of A needed to store `Size` bytes. | |||
| 180 | /// Returns `Size` if current alignment is undefined. | |||
| 181 | inline uint64_t alignTo(uint64_t Size, MaybeAlign A) { | |||
| 182 | return A ? alignTo(Size, A.getValue()) : Size; | |||
| 183 | } | |||
| 184 | ||||
| 185 | /// Aligns `Addr` to `Alignment` bytes, rounding up. | |||
| 186 | inline uintptr_t alignAddr(const void *Addr, Align Alignment) { | |||
| 187 | uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr); | |||
| 188 | assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=((void)0) | |||
| 189 | ArithAddr &&((void)0) | |||
| 190 | "Overflow")((void)0); | |||
| 191 | return alignTo(ArithAddr, Alignment); | |||
| 192 | } | |||
| 193 | ||||
| 194 | /// Returns the offset to the next integer (mod 2**64) that is greater than | |||
| 195 | /// or equal to \p Value and is a multiple of \p Align. | |||
| 196 | inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) { | |||
| 197 | return alignTo(Value, Alignment) - Value; | |||
| 198 | } | |||
| 199 | ||||
| 200 | /// Returns the necessary adjustment for aligning `Addr` to `Alignment` | |||
| 201 | /// bytes, rounding up. | |||
| 202 | inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) { | |||
| 203 | return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment); | |||
| 204 | } | |||
| 205 | ||||
| 206 | /// Returns the log2 of the alignment. | |||
| 207 | inline unsigned Log2(Align A) { return A.ShiftValue; } | |||
| 208 | ||||
| 209 | /// Returns the alignment that satisfies both alignments. | |||
| 210 | /// Same semantic as MinAlign. | |||
| 211 | inline Align commonAlignment(Align A, Align B) { return std::min(A, B); } | |||
| 212 | ||||
| 213 | /// Returns the alignment that satisfies both alignments. | |||
| 214 | /// Same semantic as MinAlign. | |||
| 215 | inline Align commonAlignment(Align A, uint64_t Offset) { | |||
| 216 | return Align(MinAlign(A.value(), Offset)); | |||
| 217 | } | |||
| 218 | ||||
| 219 | /// Returns the alignment that satisfies both alignments. | |||
| 220 | /// Same semantic as MinAlign. | |||
| 221 | inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) { | |||
| 222 | return A && B ? commonAlignment(*A, *B) : A ? A : B; | |||
| 223 | } | |||
| 224 | ||||
| 225 | /// Returns the alignment that satisfies both alignments. | |||
| 226 | /// Same semantic as MinAlign. | |||
| 227 | inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) { | |||
| 228 | return MaybeAlign(MinAlign((*A).value(), Offset)); | |||
| 229 | } | |||
| 230 | ||||
| 231 | /// Returns a representation of the alignment that encodes undefined as 0. | |||
| 232 | inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; } | |||
| 233 | ||||
| 234 | /// Dual operation of the encode function above. | |||
| 235 | inline MaybeAlign decodeMaybeAlign(unsigned Value) { | |||
| 236 | if (Value == 0) | |||
| 237 | return MaybeAlign(); | |||
| 238 | Align Out; | |||
| 239 | Out.ShiftValue = Value - 1; | |||
| 240 | return Out; | |||
| 241 | } | |||
| 242 | ||||
| 243 | /// Returns a representation of the alignment, the encoded value is positive by | |||
| 244 | /// definition. | |||
| 245 | inline unsigned encode(Align A) { return encode(MaybeAlign(A)); } | |||
| 246 | ||||
| 247 | /// Comparisons between Align and scalars. Rhs must be positive. | |||
| 248 | inline bool operator==(Align Lhs, uint64_t Rhs) { | |||
| 249 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 250 | return Lhs.value() == Rhs; | |||
| 251 | } | |||
| 252 | inline bool operator!=(Align Lhs, uint64_t Rhs) { | |||
| 253 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 254 | return Lhs.value() != Rhs; | |||
| 255 | } | |||
| 256 | inline bool operator<=(Align Lhs, uint64_t Rhs) { | |||
| 257 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 258 | return Lhs.value() <= Rhs; | |||
| 259 | } | |||
| 260 | inline bool operator>=(Align Lhs, uint64_t Rhs) { | |||
| 261 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 262 | return Lhs.value() >= Rhs; | |||
| 263 | } | |||
| 264 | inline bool operator<(Align Lhs, uint64_t Rhs) { | |||
| 265 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 266 | return Lhs.value() < Rhs; | |||
| 267 | } | |||
| 268 | inline bool operator>(Align Lhs, uint64_t Rhs) { | |||
| 269 | ALIGN_CHECK_ISPOSITIVE(Rhs); | |||
| 270 | return Lhs.value() > Rhs; | |||
| 271 | } | |||
| 272 | ||||
| 273 | /// Comparisons between MaybeAlign and scalars. | |||
| 274 | inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) { | |||
| 275 | return Lhs ? (*Lhs).value() == Rhs : Rhs == 0; | |||
| 276 | } | |||
| 277 | inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) { | |||
| 278 | return Lhs ? (*Lhs).value() != Rhs : Rhs != 0; | |||
| 279 | } | |||
| 280 | ||||
| 281 | /// Comparisons operators between Align. | |||
| 282 | inline bool operator==(Align Lhs, Align Rhs) { | |||
| 283 | return Lhs.ShiftValue == Rhs.ShiftValue; | |||
| 284 | } | |||
| 285 | inline bool operator!=(Align Lhs, Align Rhs) { | |||
| 286 | return Lhs.ShiftValue != Rhs.ShiftValue; | |||
| 287 | } | |||
| 288 | inline bool operator<=(Align Lhs, Align Rhs) { | |||
| 289 | return Lhs.ShiftValue <= Rhs.ShiftValue; | |||
| 290 | } | |||
| 291 | inline bool operator>=(Align Lhs, Align Rhs) { | |||
| 292 | return Lhs.ShiftValue >= Rhs.ShiftValue; | |||
| 293 | } | |||
| 294 | inline bool operator<(Align Lhs, Align Rhs) { | |||
| 295 | return Lhs.ShiftValue < Rhs.ShiftValue; | |||
| 296 | } | |||
| 297 | inline bool operator>(Align Lhs, Align Rhs) { | |||
| 298 | return Lhs.ShiftValue > Rhs.ShiftValue; | |||
| 299 | } | |||
| 300 | ||||
| 301 | // Don't allow relational comparisons with MaybeAlign. | |||
| 302 | bool operator<=(Align Lhs, MaybeAlign Rhs) = delete; | |||
| 303 | bool operator>=(Align Lhs, MaybeAlign Rhs) = delete; | |||
| 304 | bool operator<(Align Lhs, MaybeAlign Rhs) = delete; | |||
| 305 | bool operator>(Align Lhs, MaybeAlign Rhs) = delete; | |||
| 306 | ||||
| 307 | bool operator<=(MaybeAlign Lhs, Align Rhs) = delete; | |||
| 308 | bool operator>=(MaybeAlign Lhs, Align Rhs) = delete; | |||
| 309 | bool operator<(MaybeAlign Lhs, Align Rhs) = delete; | |||
| 310 | bool operator>(MaybeAlign Lhs, Align Rhs) = delete; | |||
| 311 | ||||
| 312 | bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; | |||
| 313 | bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; | |||
| 314 | bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; | |||
| 315 | bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; | |||
| 316 | ||||
| 317 | inline Align operator*(Align Lhs, uint64_t Rhs) { | |||
| 318 | assert(Rhs > 0 && "Rhs must be positive")((void)0); | |||
| 319 | return Align(Lhs.value() * Rhs); | |||
| 320 | } | |||
| 321 | ||||
| 322 | inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) { | |||
| 323 | assert(Rhs > 0 && "Rhs must be positive")((void)0); | |||
| 324 | return Lhs ? Lhs.getValue() * Rhs : MaybeAlign(); | |||
| 325 | } | |||
| 326 | ||||
| 327 | inline Align operator/(Align Lhs, uint64_t Divisor) { | |||
| 328 | assert(llvm::isPowerOf2_64(Divisor) &&((void)0) | |||
| 329 | "Divisor must be positive and a power of 2")((void)0); | |||
| 330 | assert(Lhs != 1 && "Can't halve byte alignment")((void)0); | |||
| 331 | return Align(Lhs.value() / Divisor); | |||
| 332 | } | |||
| 333 | ||||
| 334 | inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) { | |||
| 335 | assert(llvm::isPowerOf2_64(Divisor) &&((void)0) | |||
| 336 | "Divisor must be positive and a power of 2")((void)0); | |||
| 337 | return Lhs ? Lhs.getValue() / Divisor : MaybeAlign(); | |||
| 338 | } | |||
| 339 | ||||
| 340 | inline Align max(MaybeAlign Lhs, Align Rhs) { | |||
| 341 | return Lhs && *Lhs > Rhs ? *Lhs : Rhs; | |||
| 342 | } | |||
| 343 | ||||
| 344 | inline Align max(Align Lhs, MaybeAlign Rhs) { | |||
| 345 | return Rhs && *Rhs > Lhs ? *Rhs : Lhs; | |||
| 346 | } | |||
| 347 | ||||
| 348 | #ifndef NDEBUG1 | |||
| 349 | // For usage in LLVM_DEBUG macros. | |||
| 350 | inline std::string DebugStr(const Align &A) { | |||
| 351 | return std::to_string(A.value()); | |||
| 352 | } | |||
| 353 | // For usage in LLVM_DEBUG macros. | |||
| 354 | inline std::string DebugStr(const MaybeAlign &MA) { | |||
| 355 | if (MA) | |||
| 356 | return std::to_string(MA->value()); | |||
| 357 | return "None"; | |||
| 358 | } | |||
| 359 | #endif // NDEBUG | |||
| 360 | ||||
| 361 | #undef ALIGN_CHECK_ISPOSITIVE | |||
| 362 | ||||
| 363 | } // namespace llvm | |||
| 364 | ||||
| 365 | #endif // LLVM_SUPPORT_ALIGNMENT_H_ |
| 1 | //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains some functions that are useful for math stuff. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_MATHEXTRAS_H |
| 14 | #define LLVM_SUPPORT_MATHEXTRAS_H |
| 15 | |
| 16 | #include "llvm/Support/Compiler.h" |
| 17 | #include <cassert> |
| 18 | #include <climits> |
| 19 | #include <cmath> |
| 20 | #include <cstdint> |
| 21 | #include <cstring> |
| 22 | #include <limits> |
| 23 | #include <type_traits> |
| 24 | |
| 25 | #ifdef __ANDROID_NDK__ |
| 26 | #include <android/api-level.h> |
| 27 | #endif |
| 28 | |
| 29 | #ifdef _MSC_VER |
| 30 | // Declare these intrinsics manually rather including intrin.h. It's very |
| 31 | // expensive, and MathExtras.h is popular. |
| 32 | // #include <intrin.h> |
| 33 | extern "C" { |
| 34 | unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); |
| 35 | unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); |
| 36 | unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); |
| 37 | unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | namespace llvm { |
| 42 | |
| 43 | /// The behavior an operation has on an input of 0. |
| 44 | enum ZeroBehavior { |
| 45 | /// The returned value is undefined. |
| 46 | ZB_Undefined, |
| 47 | /// The returned value is numeric_limits<T>::max() |
| 48 | ZB_Max, |
| 49 | /// The returned value is numeric_limits<T>::digits |
| 50 | ZB_Width |
| 51 | }; |
| 52 | |
| 53 | /// Mathematical constants. |
| 54 | namespace numbers { |
| 55 | // TODO: Track C++20 std::numbers. |
| 56 | // TODO: Favor using the hexadecimal FP constants (requires C++17). |
| 57 | constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113 |
| 58 | egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620 |
| 59 | ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162 |
| 60 | ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392 |
| 61 | log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0) |
| 62 | log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2) |
| 63 | pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796 |
| 64 | inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541 |
| 65 | sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161 |
| 66 | inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197 |
| 67 | sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219 |
| 68 | inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1) |
| 69 | sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194 |
| 70 | inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1) |
| 71 | phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622 |
| 72 | constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113 |
| 73 | egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620 |
| 74 | ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162 |
| 75 | ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392 |
| 76 | log2ef = 1.44269504F, // (0x1.715476P+0) |
| 77 | log10ef = .434294482F, // (0x1.bcb7b2P-2) |
| 78 | pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796 |
| 79 | inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541 |
| 80 | sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161 |
| 81 | inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197 |
| 82 | sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193 |
| 83 | inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1) |
| 84 | sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194 |
| 85 | inv_sqrt3f = .577350269F, // (0x1.279a74P-1) |
| 86 | phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622 |
| 87 | } // namespace numbers |
| 88 | |
| 89 | namespace detail { |
| 90 | template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter { |
| 91 | static unsigned count(T Val, ZeroBehavior) { |
| 92 | if (!Val) |
| 93 | return std::numeric_limits<T>::digits; |
| 94 | if (Val & 0x1) |
| 95 | return 0; |
| 96 | |
| 97 | // Bisection method. |
| 98 | unsigned ZeroBits = 0; |
| 99 | T Shift = std::numeric_limits<T>::digits >> 1; |
| 100 | T Mask = std::numeric_limits<T>::max() >> Shift; |
| 101 | while (Shift) { |
| 102 | if ((Val & Mask) == 0) { |
| 103 | Val >>= Shift; |
| 104 | ZeroBits |= Shift; |
| 105 | } |
| 106 | Shift >>= 1; |
| 107 | Mask >>= Shift; |
| 108 | } |
| 109 | return ZeroBits; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | #if defined(__GNUC__4) || defined(_MSC_VER) |
| 114 | template <typename T> struct TrailingZerosCounter<T, 4> { |
| 115 | static unsigned count(T Val, ZeroBehavior ZB) { |
| 116 | if (ZB != ZB_Undefined && Val == 0) |
| 117 | return 32; |
| 118 | |
| 119 | #if __has_builtin(__builtin_ctz)1 || defined(__GNUC__4) |
| 120 | return __builtin_ctz(Val); |
| 121 | #elif defined(_MSC_VER) |
| 122 | unsigned long Index; |
| 123 | _BitScanForward(&Index, Val); |
| 124 | return Index; |
| 125 | #endif |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | #if !defined(_MSC_VER) || defined(_M_X64) |
| 130 | template <typename T> struct TrailingZerosCounter<T, 8> { |
| 131 | static unsigned count(T Val, ZeroBehavior ZB) { |
| 132 | if (ZB != ZB_Undefined && Val == 0) |
| 133 | return 64; |
| 134 | |
| 135 | #if __has_builtin(__builtin_ctzll)1 || defined(__GNUC__4) |
| 136 | return __builtin_ctzll(Val); |
| 137 | #elif defined(_MSC_VER) |
| 138 | unsigned long Index; |
| 139 | _BitScanForward64(&Index, Val); |
| 140 | return Index; |
| 141 | #endif |
| 142 | } |
| 143 | }; |
| 144 | #endif |
| 145 | #endif |
| 146 | } // namespace detail |
| 147 | |
| 148 | /// Count number of 0's from the least significant bit to the most |
| 149 | /// stopping at the first 1. |
| 150 | /// |
| 151 | /// Only unsigned integral types are allowed. |
| 152 | /// |
| 153 | /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are |
| 154 | /// valid arguments. |
| 155 | template <typename T> |
| 156 | unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) { |
| 157 | static_assert(std::numeric_limits<T>::is_integer && |
| 158 | !std::numeric_limits<T>::is_signed, |
| 159 | "Only unsigned integral types are allowed."); |
| 160 | return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB); |
| 161 | } |
| 162 | |
| 163 | namespace detail { |
| 164 | template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter { |
| 165 | static unsigned count(T Val, ZeroBehavior) { |
| 166 | if (!Val) |
| 167 | return std::numeric_limits<T>::digits; |
| 168 | |
| 169 | // Bisection method. |
| 170 | unsigned ZeroBits = 0; |
| 171 | for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) { |
| 172 | T Tmp = Val >> Shift; |
| 173 | if (Tmp) |
| 174 | Val = Tmp; |
| 175 | else |
| 176 | ZeroBits |= Shift; |
| 177 | } |
| 178 | return ZeroBits; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | #if defined(__GNUC__4) || defined(_MSC_VER) |
| 183 | template <typename T> struct LeadingZerosCounter<T, 4> { |
| 184 | static unsigned count(T Val, ZeroBehavior ZB) { |
| 185 | if (ZB != ZB_Undefined && Val == 0) |
| 186 | return 32; |
| 187 | |
| 188 | #if __has_builtin(__builtin_clz)1 || defined(__GNUC__4) |
| 189 | return __builtin_clz(Val); |
| 190 | #elif defined(_MSC_VER) |
| 191 | unsigned long Index; |
| 192 | _BitScanReverse(&Index, Val); |
| 193 | return Index ^ 31; |
| 194 | #endif |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | #if !defined(_MSC_VER) || defined(_M_X64) |
| 199 | template <typename T> struct LeadingZerosCounter<T, 8> { |
| 200 | static unsigned count(T Val, ZeroBehavior ZB) { |
| 201 | if (ZB != ZB_Undefined && Val == 0) |
| 202 | return 64; |
| 203 | |
| 204 | #if __has_builtin(__builtin_clzll)1 || defined(__GNUC__4) |
| 205 | return __builtin_clzll(Val); |
| 206 | #elif defined(_MSC_VER) |
| 207 | unsigned long Index; |
| 208 | _BitScanReverse64(&Index, Val); |
| 209 | return Index ^ 63; |
| 210 | #endif |
| 211 | } |
| 212 | }; |
| 213 | #endif |
| 214 | #endif |
| 215 | } // namespace detail |
| 216 | |
| 217 | /// Count number of 0's from the most significant bit to the least |
| 218 | /// stopping at the first 1. |
| 219 | /// |
| 220 | /// Only unsigned integral types are allowed. |
| 221 | /// |
| 222 | /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are |
| 223 | /// valid arguments. |
| 224 | template <typename T> |
| 225 | unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) { |
| 226 | static_assert(std::numeric_limits<T>::is_integer && |
| 227 | !std::numeric_limits<T>::is_signed, |
| 228 | "Only unsigned integral types are allowed."); |
| 229 | return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB); |
| 230 | } |
| 231 | |
| 232 | /// Get the index of the first set bit starting from the least |
| 233 | /// significant bit. |
| 234 | /// |
| 235 | /// Only unsigned integral types are allowed. |
| 236 | /// |
| 237 | /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are |
| 238 | /// valid arguments. |
| 239 | template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) { |
| 240 | if (ZB == ZB_Max && Val == 0) |
| 241 | return std::numeric_limits<T>::max(); |
| 242 | |
| 243 | return countTrailingZeros(Val, ZB_Undefined); |
| 244 | } |
| 245 | |
| 246 | /// Create a bitmask with the N right-most bits set to 1, and all other |
| 247 | /// bits set to 0. Only unsigned types are allowed. |
| 248 | template <typename T> T maskTrailingOnes(unsigned N) { |
| 249 | static_assert(std::is_unsigned<T>::value, "Invalid type!"); |
| 250 | const unsigned Bits = CHAR_BIT8 * sizeof(T); |
| 251 | assert(N <= Bits && "Invalid bit index")((void)0); |
| 252 | return N == 0 ? 0 : (T(-1) >> (Bits - N)); |
| 253 | } |
| 254 | |
| 255 | /// Create a bitmask with the N left-most bits set to 1, and all other |
| 256 | /// bits set to 0. Only unsigned types are allowed. |
| 257 | template <typename T> T maskLeadingOnes(unsigned N) { |
| 258 | return ~maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
| 259 | } |
| 260 | |
| 261 | /// Create a bitmask with the N right-most bits set to 0, and all other |
| 262 | /// bits set to 1. Only unsigned types are allowed. |
| 263 | template <typename T> T maskTrailingZeros(unsigned N) { |
| 264 | return maskLeadingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
| 265 | } |
| 266 | |
| 267 | /// Create a bitmask with the N left-most bits set to 0, and all other |
| 268 | /// bits set to 1. Only unsigned types are allowed. |
| 269 | template <typename T> T maskLeadingZeros(unsigned N) { |
| 270 | return maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
| 271 | } |
| 272 | |
| 273 | /// Get the index of the last set bit starting from the least |
| 274 | /// significant bit. |
| 275 | /// |
| 276 | /// Only unsigned integral types are allowed. |
| 277 | /// |
| 278 | /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are |
| 279 | /// valid arguments. |
| 280 | template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) { |
| 281 | if (ZB == ZB_Max && Val == 0) |
| 282 | return std::numeric_limits<T>::max(); |
| 283 | |
| 284 | // Use ^ instead of - because both gcc and llvm can remove the associated ^ |
| 285 | // in the __builtin_clz intrinsic on x86. |
| 286 | return countLeadingZeros(Val, ZB_Undefined) ^ |
| 287 | (std::numeric_limits<T>::digits - 1); |
| 288 | } |
| 289 | |
| 290 | /// Macro compressed bit reversal table for 256 bits. |
| 291 | /// |
| 292 | /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
| 293 | static const unsigned char BitReverseTable256[256] = { |
| 294 | #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64 |
| 295 | #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16) |
| 296 | #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4) |
| 297 | R6(0), R6(2), R6(1), R6(3) |
| 298 | #undef R2 |
| 299 | #undef R4 |
| 300 | #undef R6 |
| 301 | }; |
| 302 | |
| 303 | /// Reverse the bits in \p Val. |
| 304 | template <typename T> |
| 305 | T reverseBits(T Val) { |
| 306 | unsigned char in[sizeof(Val)]; |
| 307 | unsigned char out[sizeof(Val)]; |
| 308 | std::memcpy(in, &Val, sizeof(Val)); |
| 309 | for (unsigned i = 0; i < sizeof(Val); ++i) |
| 310 | out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]]; |
| 311 | std::memcpy(&Val, out, sizeof(Val)); |
| 312 | return Val; |
| 313 | } |
| 314 | |
| 315 | #if __has_builtin(__builtin_bitreverse8)1 |
| 316 | template<> |
| 317 | inline uint8_t reverseBits<uint8_t>(uint8_t Val) { |
| 318 | return __builtin_bitreverse8(Val); |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | #if __has_builtin(__builtin_bitreverse16)1 |
| 323 | template<> |
| 324 | inline uint16_t reverseBits<uint16_t>(uint16_t Val) { |
| 325 | return __builtin_bitreverse16(Val); |
| 326 | } |
| 327 | #endif |
| 328 | |
| 329 | #if __has_builtin(__builtin_bitreverse32)1 |
| 330 | template<> |
| 331 | inline uint32_t reverseBits<uint32_t>(uint32_t Val) { |
| 332 | return __builtin_bitreverse32(Val); |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | #if __has_builtin(__builtin_bitreverse64)1 |
| 337 | template<> |
| 338 | inline uint64_t reverseBits<uint64_t>(uint64_t Val) { |
| 339 | return __builtin_bitreverse64(Val); |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | // NOTE: The following support functions use the _32/_64 extensions instead of |
| 344 | // type overloading so that signed and unsigned integers can be used without |
| 345 | // ambiguity. |
| 346 | |
| 347 | /// Return the high 32 bits of a 64 bit value. |
| 348 | constexpr inline uint32_t Hi_32(uint64_t Value) { |
| 349 | return static_cast<uint32_t>(Value >> 32); |
| 350 | } |
| 351 | |
| 352 | /// Return the low 32 bits of a 64 bit value. |
| 353 | constexpr inline uint32_t Lo_32(uint64_t Value) { |
| 354 | return static_cast<uint32_t>(Value); |
| 355 | } |
| 356 | |
| 357 | /// Make a 64-bit integer from a high / low pair of 32-bit integers. |
| 358 | constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) { |
| 359 | return ((uint64_t)High << 32) | (uint64_t)Low; |
| 360 | } |
| 361 | |
| 362 | /// Checks if an integer fits into the given bit width. |
| 363 | template <unsigned N> constexpr inline bool isInt(int64_t x) { |
| 364 | return N >= 64 || (-(INT64_C(1)1LL<<(N-1)) <= x && x < (INT64_C(1)1LL<<(N-1))); |
| 365 | } |
| 366 | // Template specializations to get better code for common cases. |
| 367 | template <> constexpr inline bool isInt<8>(int64_t x) { |
| 368 | return static_cast<int8_t>(x) == x; |
| 369 | } |
| 370 | template <> constexpr inline bool isInt<16>(int64_t x) { |
| 371 | return static_cast<int16_t>(x) == x; |
| 372 | } |
| 373 | template <> constexpr inline bool isInt<32>(int64_t x) { |
| 374 | return static_cast<int32_t>(x) == x; |
| 375 | } |
| 376 | |
| 377 | /// Checks if a signed integer is an N bit number shifted left by S. |
| 378 | template <unsigned N, unsigned S> |
| 379 | constexpr inline bool isShiftedInt(int64_t x) { |
| 380 | static_assert( |
| 381 | N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number."); |
| 382 | static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide."); |
| 383 | return isInt<N + S>(x) && (x % (UINT64_C(1)1ULL << S) == 0); |
| 384 | } |
| 385 | |
| 386 | /// Checks if an unsigned integer fits into the given bit width. |
| 387 | /// |
| 388 | /// This is written as two functions rather than as simply |
| 389 | /// |
| 390 | /// return N >= 64 || X < (UINT64_C(1) << N); |
| 391 | /// |
| 392 | /// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting |
| 393 | /// left too many places. |
| 394 | template <unsigned N> |
| 395 | constexpr inline std::enable_if_t<(N < 64), bool> isUInt(uint64_t X) { |
| 396 | static_assert(N > 0, "isUInt<0> doesn't make sense"); |
| 397 | return X < (UINT64_C(1)1ULL << (N)); |
| 398 | } |
| 399 | template <unsigned N> |
| 400 | constexpr inline std::enable_if_t<N >= 64, bool> isUInt(uint64_t) { |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | // Template specializations to get better code for common cases. |
| 405 | template <> constexpr inline bool isUInt<8>(uint64_t x) { |
| 406 | return static_cast<uint8_t>(x) == x; |
| 407 | } |
| 408 | template <> constexpr inline bool isUInt<16>(uint64_t x) { |
| 409 | return static_cast<uint16_t>(x) == x; |
| 410 | } |
| 411 | template <> constexpr inline bool isUInt<32>(uint64_t x) { |
| 412 | return static_cast<uint32_t>(x) == x; |
| 413 | } |
| 414 | |
| 415 | /// Checks if a unsigned integer is an N bit number shifted left by S. |
| 416 | template <unsigned N, unsigned S> |
| 417 | constexpr inline bool isShiftedUInt(uint64_t x) { |
| 418 | static_assert( |
| 419 | N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)"); |
| 420 | static_assert(N + S <= 64, |
| 421 | "isShiftedUInt<N, S> with N + S > 64 is too wide."); |
| 422 | // Per the two static_asserts above, S must be strictly less than 64. So |
| 423 | // 1 << S is not undefined behavior. |
| 424 | return isUInt<N + S>(x) && (x % (UINT64_C(1)1ULL << S) == 0); |
| 425 | } |
| 426 | |
| 427 | /// Gets the maximum value for a N-bit unsigned integer. |
| 428 | inline uint64_t maxUIntN(uint64_t N) { |
| 429 | assert(N > 0 && N <= 64 && "integer width out of range")((void)0); |
| 430 | |
| 431 | // uint64_t(1) << 64 is undefined behavior, so we can't do |
| 432 | // (uint64_t(1) << N) - 1 |
| 433 | // without checking first that N != 64. But this works and doesn't have a |
| 434 | // branch. |
| 435 | return UINT64_MAX0xffffffffffffffffULL >> (64 - N); |
| 436 | } |
| 437 | |
| 438 | /// Gets the minimum value for a N-bit signed integer. |
| 439 | inline int64_t minIntN(int64_t N) { |
| 440 | assert(N > 0 && N <= 64 && "integer width out of range")((void)0); |
| 441 | |
| 442 | return UINT64_C(1)1ULL + ~(UINT64_C(1)1ULL << (N - 1)); |
| 443 | } |
| 444 | |
| 445 | /// Gets the maximum value for a N-bit signed integer. |
| 446 | inline int64_t maxIntN(int64_t N) { |
| 447 | assert(N > 0 && N <= 64 && "integer width out of range")((void)0); |
| 448 | |
| 449 | // This relies on two's complement wraparound when N == 64, so we convert to |
| 450 | // int64_t only at the very end to avoid UB. |
| 451 | return (UINT64_C(1)1ULL << (N - 1)) - 1; |
| 452 | } |
| 453 | |
| 454 | /// Checks if an unsigned integer fits into the given (dynamic) bit width. |
| 455 | inline bool isUIntN(unsigned N, uint64_t x) { |
| 456 | return N >= 64 || x <= maxUIntN(N); |
| 457 | } |
| 458 | |
| 459 | /// Checks if an signed integer fits into the given (dynamic) bit width. |
| 460 | inline bool isIntN(unsigned N, int64_t x) { |
| 461 | return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N)); |
| 462 | } |
| 463 | |
| 464 | /// Return true if the argument is a non-empty sequence of ones starting at the |
| 465 | /// least significant bit with the remainder zero (32 bit version). |
| 466 | /// Ex. isMask_32(0x0000FFFFU) == true. |
| 467 | constexpr inline bool isMask_32(uint32_t Value) { |
| 468 | return Value && ((Value + 1) & Value) == 0; |
| 469 | } |
| 470 | |
| 471 | /// Return true if the argument is a non-empty sequence of ones starting at the |
| 472 | /// least significant bit with the remainder zero (64 bit version). |
| 473 | constexpr inline bool isMask_64(uint64_t Value) { |
| 474 | return Value && ((Value + 1) & Value) == 0; |
| 475 | } |
| 476 | |
| 477 | /// Return true if the argument contains a non-empty sequence of ones with the |
| 478 | /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true. |
| 479 | constexpr inline bool isShiftedMask_32(uint32_t Value) { |
| 480 | return Value && isMask_32((Value - 1) | Value); |
| 481 | } |
| 482 | |
| 483 | /// Return true if the argument contains a non-empty sequence of ones with the |
| 484 | /// remainder zero (64 bit version.) |
| 485 | constexpr inline bool isShiftedMask_64(uint64_t Value) { |
| 486 | return Value && isMask_64((Value - 1) | Value); |
| 487 | } |
| 488 | |
| 489 | /// Return true if the argument is a power of two > 0. |
| 490 | /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) |
| 491 | constexpr inline bool isPowerOf2_32(uint32_t Value) { |
| 492 | return Value && !(Value & (Value - 1)); |
| 493 | } |
| 494 | |
| 495 | /// Return true if the argument is a power of two > 0 (64 bit edition.) |
| 496 | constexpr inline bool isPowerOf2_64(uint64_t Value) { |
| 497 | return Value && !(Value & (Value - 1)); |
| 498 | } |
| 499 | |
| 500 | /// Count the number of ones from the most significant bit to the first |
| 501 | /// zero bit. |
| 502 | /// |
| 503 | /// Ex. countLeadingOnes(0xFF0FFF00) == 8. |
| 504 | /// Only unsigned integral types are allowed. |
| 505 | /// |
| 506 | /// \param ZB the behavior on an input of all ones. Only ZB_Width and |
| 507 | /// ZB_Undefined are valid arguments. |
| 508 | template <typename T> |
| 509 | unsigned countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) { |
| 510 | static_assert(std::numeric_limits<T>::is_integer && |
| 511 | !std::numeric_limits<T>::is_signed, |
| 512 | "Only unsigned integral types are allowed."); |
| 513 | return countLeadingZeros<T>(~Value, ZB); |
| 514 | } |
| 515 | |
| 516 | /// Count the number of ones from the least significant bit to the first |
| 517 | /// zero bit. |
| 518 | /// |
| 519 | /// Ex. countTrailingOnes(0x00FF00FF) == 8. |
| 520 | /// Only unsigned integral types are allowed. |
| 521 | /// |
| 522 | /// \param ZB the behavior on an input of all ones. Only ZB_Width and |
| 523 | /// ZB_Undefined are valid arguments. |
| 524 | template <typename T> |
| 525 | unsigned countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) { |
| 526 | static_assert(std::numeric_limits<T>::is_integer && |
| 527 | !std::numeric_limits<T>::is_signed, |
| 528 | "Only unsigned integral types are allowed."); |
| 529 | return countTrailingZeros<T>(~Value, ZB); |
| 530 | } |
| 531 | |
| 532 | namespace detail { |
| 533 | template <typename T, std::size_t SizeOfT> struct PopulationCounter { |
| 534 | static unsigned count(T Value) { |
| 535 | // Generic version, forward to 32 bits. |
| 536 | static_assert(SizeOfT <= 4, "Not implemented!"); |
| 537 | #if defined(__GNUC__4) |
| 538 | return __builtin_popcount(Value); |
| 539 | #else |
| 540 | uint32_t v = Value; |
| 541 | v = v - ((v >> 1) & 0x55555555); |
| 542 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); |
| 543 | return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; |
| 544 | #endif |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | template <typename T> struct PopulationCounter<T, 8> { |
| 549 | static unsigned count(T Value) { |
| 550 | #if defined(__GNUC__4) |
| 551 | return __builtin_popcountll(Value); |
| 552 | #else |
| 553 | uint64_t v = Value; |
| 554 | v = v - ((v >> 1) & 0x5555555555555555ULL); |
| 555 | v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL); |
| 556 | v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL; |
| 557 | return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56); |
| 558 | #endif |
| 559 | } |
| 560 | }; |
| 561 | } // namespace detail |
| 562 | |
| 563 | /// Count the number of set bits in a value. |
| 564 | /// Ex. countPopulation(0xF000F000) = 8 |
| 565 | /// Returns 0 if the word is zero. |
| 566 | template <typename T> |
| 567 | inline unsigned countPopulation(T Value) { |
| 568 | static_assert(std::numeric_limits<T>::is_integer && |
| 569 | !std::numeric_limits<T>::is_signed, |
| 570 | "Only unsigned integral types are allowed."); |
| 571 | return detail::PopulationCounter<T, sizeof(T)>::count(Value); |
| 572 | } |
| 573 | |
| 574 | /// Compile time Log2. |
| 575 | /// Valid only for positive powers of two. |
| 576 | template <size_t kValue> constexpr inline size_t CTLog2() { |
| 577 | static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue), |
| 578 | "Value is not a valid power of 2"); |
| 579 | return 1 + CTLog2<kValue / 2>(); |
| 580 | } |
| 581 | |
| 582 | template <> constexpr inline size_t CTLog2<1>() { return 0; } |
| 583 | |
| 584 | /// Return the log base 2 of the specified value. |
| 585 | inline double Log2(double Value) { |
| 586 | #if defined(__ANDROID_API__) && __ANDROID_API__ < 18 |
| 587 | return __builtin_log(Value) / __builtin_log(2.0); |
| 588 | #else |
| 589 | return log2(Value); |
| 590 | #endif |
| 591 | } |
| 592 | |
| 593 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. |
| 594 | /// (32 bit edition.) |
| 595 | /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2 |
| 596 | inline unsigned Log2_32(uint32_t Value) { |
| 597 | return 31 - countLeadingZeros(Value); |
| 598 | } |
| 599 | |
| 600 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. |
| 601 | /// (64 bit edition.) |
| 602 | inline unsigned Log2_64(uint64_t Value) { |
| 603 | return 63 - countLeadingZeros(Value); |
| 604 | } |
| 605 | |
| 606 | /// Return the ceil log base 2 of the specified value, 32 if the value is zero. |
| 607 | /// (32 bit edition). |
| 608 | /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3 |
| 609 | inline unsigned Log2_32_Ceil(uint32_t Value) { |
| 610 | return 32 - countLeadingZeros(Value - 1); |
| 611 | } |
| 612 | |
| 613 | /// Return the ceil log base 2 of the specified value, 64 if the value is zero. |
| 614 | /// (64 bit edition.) |
| 615 | inline unsigned Log2_64_Ceil(uint64_t Value) { |
| 616 | return 64 - countLeadingZeros(Value - 1); |
| 617 | } |
| 618 | |
| 619 | /// Return the greatest common divisor of the values using Euclid's algorithm. |
| 620 | template <typename T> |
| 621 | inline T greatestCommonDivisor(T A, T B) { |
| 622 | while (B) { |
| 623 | T Tmp = B; |
| 624 | B = A % B; |
| 625 | A = Tmp; |
| 626 | } |
| 627 | return A; |
| 628 | } |
| 629 | |
| 630 | inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) { |
| 631 | return greatestCommonDivisor<uint64_t>(A, B); |
| 632 | } |
| 633 | |
| 634 | /// This function takes a 64-bit integer and returns the bit equivalent double. |
| 635 | inline double BitsToDouble(uint64_t Bits) { |
| 636 | double D; |
| 637 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); |
| 638 | memcpy(&D, &Bits, sizeof(Bits)); |
| 639 | return D; |
| 640 | } |
| 641 | |
| 642 | /// This function takes a 32-bit integer and returns the bit equivalent float. |
| 643 | inline float BitsToFloat(uint32_t Bits) { |
| 644 | float F; |
| 645 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); |
| 646 | memcpy(&F, &Bits, sizeof(Bits)); |
| 647 | return F; |
| 648 | } |
| 649 | |
| 650 | /// This function takes a double and returns the bit equivalent 64-bit integer. |
| 651 | /// Note that copying doubles around changes the bits of NaNs on some hosts, |
| 652 | /// notably x86, so this routine cannot be used if these bits are needed. |
| 653 | inline uint64_t DoubleToBits(double Double) { |
| 654 | uint64_t Bits; |
| 655 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); |
| 656 | memcpy(&Bits, &Double, sizeof(Double)); |
| 657 | return Bits; |
| 658 | } |
| 659 | |
| 660 | /// This function takes a float and returns the bit equivalent 32-bit integer. |
| 661 | /// Note that copying floats around changes the bits of NaNs on some hosts, |
| 662 | /// notably x86, so this routine cannot be used if these bits are needed. |
| 663 | inline uint32_t FloatToBits(float Float) { |
| 664 | uint32_t Bits; |
| 665 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); |
| 666 | memcpy(&Bits, &Float, sizeof(Float)); |
| 667 | return Bits; |
| 668 | } |
| 669 | |
| 670 | /// A and B are either alignments or offsets. Return the minimum alignment that |
| 671 | /// may be assumed after adding the two together. |
| 672 | constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) { |
| 673 | // The largest power of 2 that divides both A and B. |
| 674 | // |
| 675 | // Replace "-Value" by "1+~Value" in the following commented code to avoid |
| 676 | // MSVC warning C4146 |
| 677 | // return (A | B) & -(A | B); |
| 678 | return (A | B) & (1 + ~(A | B)); |
| 679 | } |
| 680 | |
| 681 | /// Returns the next power of two (in 64-bits) that is strictly greater than A. |
| 682 | /// Returns zero on overflow. |
| 683 | inline uint64_t NextPowerOf2(uint64_t A) { |
| 684 | A |= (A >> 1); |
| 685 | A |= (A >> 2); |
| 686 | A |= (A >> 4); |
| 687 | A |= (A >> 8); |
| 688 | A |= (A >> 16); |
| 689 | A |= (A >> 32); |
| 690 | return A + 1; |
| 691 | } |
| 692 | |
| 693 | /// Returns the power of two which is less than or equal to the given value. |
| 694 | /// Essentially, it is a floor operation across the domain of powers of two. |
| 695 | inline uint64_t PowerOf2Floor(uint64_t A) { |
| 696 | if (!A) return 0; |
| 697 | return 1ull << (63 - countLeadingZeros(A, ZB_Undefined)); |
| 698 | } |
| 699 | |
| 700 | /// Returns the power of two which is greater than or equal to the given value. |
| 701 | /// Essentially, it is a ceil operation across the domain of powers of two. |
| 702 | inline uint64_t PowerOf2Ceil(uint64_t A) { |
| 703 | if (!A) |
| 704 | return 0; |
| 705 | return NextPowerOf2(A - 1); |
| 706 | } |
| 707 | |
| 708 | /// Returns the next integer (mod 2**64) that is greater than or equal to |
| 709 | /// \p Value and is a multiple of \p Align. \p Align must be non-zero. |
| 710 | /// |
| 711 | /// If non-zero \p Skew is specified, the return value will be a minimal |
| 712 | /// integer that is greater than or equal to \p Value and equal to |
| 713 | /// \p Align * N + \p Skew for some integer N. If \p Skew is larger than |
| 714 | /// \p Align, its value is adjusted to '\p Skew mod \p Align'. |
| 715 | /// |
| 716 | /// Examples: |
| 717 | /// \code |
| 718 | /// alignTo(5, 8) = 8 |
| 719 | /// alignTo(17, 8) = 24 |
| 720 | /// alignTo(~0LL, 8) = 0 |
| 721 | /// alignTo(321, 255) = 510 |
| 722 | /// |
| 723 | /// alignTo(5, 8, 7) = 7 |
| 724 | /// alignTo(17, 8, 1) = 17 |
| 725 | /// alignTo(~0LL, 8, 3) = 3 |
| 726 | /// alignTo(321, 255, 42) = 552 |
| 727 | /// \endcode |
| 728 | inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { |
| 729 | assert(Align != 0u && "Align can't be 0.")((void)0); |
| 730 | Skew %= Align; |
| 731 | return (Value + Align - 1 - Skew) / Align * Align + Skew; |
| 732 | } |
| 733 | |
| 734 | /// Returns the next integer (mod 2**64) that is greater than or equal to |
| 735 | /// \p Value and is a multiple of \c Align. \c Align must be non-zero. |
| 736 | template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) { |
| 737 | static_assert(Align != 0u, "Align must be non-zero"); |
| 738 | return (Value + Align - 1) / Align * Align; |
| 739 | } |
| 740 | |
| 741 | /// Returns the integer ceil(Numerator / Denominator). |
| 742 | inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) { |
| 743 | return alignTo(Numerator, Denominator) / Denominator; |
| 744 | } |
| 745 | |
| 746 | /// Returns the integer nearest(Numerator / Denominator). |
| 747 | inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) { |
| 748 | return (Numerator + (Denominator / 2)) / Denominator; |
| 749 | } |
| 750 | |
| 751 | /// Returns the largest uint64_t less than or equal to \p Value and is |
| 752 | /// \p Skew mod \p Align. \p Align must be non-zero |
| 753 | inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { |
| 754 | assert(Align != 0u && "Align can't be 0.")((void)0); |
| 755 | Skew %= Align; |
| 756 | return (Value - Skew) / Align * Align + Skew; |
| 757 | } |
| 758 | |
| 759 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. |
| 760 | /// Requires 0 < B <= 32. |
| 761 | template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) { |
| 762 | static_assert(B > 0, "Bit width can't be 0."); |
| 763 | static_assert(B <= 32, "Bit width out of range."); |
| 764 | return int32_t(X << (32 - B)) >> (32 - B); |
| 765 | } |
| 766 | |
| 767 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. |
| 768 | /// Requires 0 < B <= 32. |
| 769 | inline int32_t SignExtend32(uint32_t X, unsigned B) { |
| 770 | assert(B > 0 && "Bit width can't be 0.")((void)0); |
| 771 | assert(B <= 32 && "Bit width out of range.")((void)0); |
| 772 | return int32_t(X << (32 - B)) >> (32 - B); |
| 773 | } |
| 774 | |
| 775 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. |
| 776 | /// Requires 0 < B <= 64. |
| 777 | template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) { |
| 778 | static_assert(B > 0, "Bit width can't be 0."); |
| 779 | static_assert(B <= 64, "Bit width out of range."); |
| 780 | return int64_t(x << (64 - B)) >> (64 - B); |
| 781 | } |
| 782 | |
| 783 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. |
| 784 | /// Requires 0 < B <= 64. |
| 785 | inline int64_t SignExtend64(uint64_t X, unsigned B) { |
| 786 | assert(B > 0 && "Bit width can't be 0.")((void)0); |
| 787 | assert(B <= 64 && "Bit width out of range.")((void)0); |
| 788 | return int64_t(X << (64 - B)) >> (64 - B); |
| 789 | } |
| 790 | |
| 791 | /// Subtract two unsigned integers, X and Y, of type T and return the absolute |
| 792 | /// value of the result. |
| 793 | template <typename T> |
| 794 | std::enable_if_t<std::is_unsigned<T>::value, T> AbsoluteDifference(T X, T Y) { |
| 795 | return X > Y ? (X - Y) : (Y - X); |
| 796 | } |
| 797 | |
| 798 | /// Add two unsigned integers, X and Y, of type T. Clamp the result to the |
| 799 | /// maximum representable value of T on overflow. ResultOverflowed indicates if |
| 800 | /// the result is larger than the maximum representable value of type T. |
| 801 | template <typename T> |
| 802 | std::enable_if_t<std::is_unsigned<T>::value, T> |
| 803 | SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) { |
| 804 | bool Dummy; |
| 805 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
| 806 | // Hacker's Delight, p. 29 |
| 807 | T Z = X + Y; |
| 808 | Overflowed = (Z < X || Z < Y); |
| 809 | if (Overflowed) |
| 810 | return std::numeric_limits<T>::max(); |
| 811 | else |
| 812 | return Z; |
| 813 | } |
| 814 | |
| 815 | /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the |
| 816 | /// maximum representable value of T on overflow. ResultOverflowed indicates if |
| 817 | /// the result is larger than the maximum representable value of type T. |
| 818 | template <typename T> |
| 819 | std::enable_if_t<std::is_unsigned<T>::value, T> |
| 820 | SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) { |
| 821 | bool Dummy; |
| 822 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
| 823 | |
| 824 | // Hacker's Delight, p. 30 has a different algorithm, but we don't use that |
| 825 | // because it fails for uint16_t (where multiplication can have undefined |
| 826 | // behavior due to promotion to int), and requires a division in addition |
| 827 | // to the multiplication. |
| 828 | |
| 829 | Overflowed = false; |
| 830 | |
| 831 | // Log2(Z) would be either Log2Z or Log2Z + 1. |
| 832 | // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z |
| 833 | // will necessarily be less than Log2Max as desired. |
| 834 | int Log2Z = Log2_64(X) + Log2_64(Y); |
| 835 | const T Max = std::numeric_limits<T>::max(); |
| 836 | int Log2Max = Log2_64(Max); |
| 837 | if (Log2Z < Log2Max) { |
| 838 | return X * Y; |
| 839 | } |
| 840 | if (Log2Z > Log2Max) { |
| 841 | Overflowed = true; |
| 842 | return Max; |
| 843 | } |
| 844 | |
| 845 | // We're going to use the top bit, and maybe overflow one |
| 846 | // bit past it. Multiply all but the bottom bit then add |
| 847 | // that on at the end. |
| 848 | T Z = (X >> 1) * Y; |
| 849 | if (Z & ~(Max >> 1)) { |
| 850 | Overflowed = true; |
| 851 | return Max; |
| 852 | } |
| 853 | Z <<= 1; |
| 854 | if (X & 1) |
| 855 | return SaturatingAdd(Z, Y, ResultOverflowed); |
| 856 | |
| 857 | return Z; |
| 858 | } |
| 859 | |
| 860 | /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to |
| 861 | /// the product. Clamp the result to the maximum representable value of T on |
| 862 | /// overflow. ResultOverflowed indicates if the result is larger than the |
| 863 | /// maximum representable value of type T. |
| 864 | template <typename T> |
| 865 | std::enable_if_t<std::is_unsigned<T>::value, T> |
| 866 | SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) { |
| 867 | bool Dummy; |
| 868 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
| 869 | |
| 870 | T Product = SaturatingMultiply(X, Y, &Overflowed); |
| 871 | if (Overflowed) |
| 872 | return Product; |
| 873 | |
| 874 | return SaturatingAdd(A, Product, &Overflowed); |
| 875 | } |
| 876 | |
| 877 | /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC. |
| 878 | extern const float huge_valf; |
| 879 | |
| 880 | |
| 881 | /// Add two signed integers, computing the two's complement truncated result, |
| 882 | /// returning true if overflow occured. |
| 883 | template <typename T> |
| 884 | std::enable_if_t<std::is_signed<T>::value, T> AddOverflow(T X, T Y, T &Result) { |
| 885 | #if __has_builtin(__builtin_add_overflow)1 |
| 886 | return __builtin_add_overflow(X, Y, &Result); |
| 887 | #else |
| 888 | // Perform the unsigned addition. |
| 889 | using U = std::make_unsigned_t<T>; |
| 890 | const U UX = static_cast<U>(X); |
| 891 | const U UY = static_cast<U>(Y); |
| 892 | const U UResult = UX + UY; |
| 893 | |
| 894 | // Convert to signed. |
| 895 | Result = static_cast<T>(UResult); |
| 896 | |
| 897 | // Adding two positive numbers should result in a positive number. |
| 898 | if (X > 0 && Y > 0) |
| 899 | return Result <= 0; |
| 900 | // Adding two negatives should result in a negative number. |
| 901 | if (X < 0 && Y < 0) |
| 902 | return Result >= 0; |
| 903 | return false; |
| 904 | #endif |
| 905 | } |
| 906 | |
| 907 | /// Subtract two signed integers, computing the two's complement truncated |
| 908 | /// result, returning true if an overflow ocurred. |
| 909 | template <typename T> |
| 910 | std::enable_if_t<std::is_signed<T>::value, T> SubOverflow(T X, T Y, T &Result) { |
| 911 | #if __has_builtin(__builtin_sub_overflow)1 |
| 912 | return __builtin_sub_overflow(X, Y, &Result); |
| 913 | #else |
| 914 | // Perform the unsigned addition. |
| 915 | using U = std::make_unsigned_t<T>; |
| 916 | const U UX = static_cast<U>(X); |
| 917 | const U UY = static_cast<U>(Y); |
| 918 | const U UResult = UX - UY; |
| 919 | |
| 920 | // Convert to signed. |
| 921 | Result = static_cast<T>(UResult); |
| 922 | |
| 923 | // Subtracting a positive number from a negative results in a negative number. |
| 924 | if (X <= 0 && Y > 0) |
| 925 | return Result >= 0; |
| 926 | // Subtracting a negative number from a positive results in a positive number. |
| 927 | if (X >= 0 && Y < 0) |
| 928 | return Result <= 0; |
| 929 | return false; |
| 930 | #endif |
| 931 | } |
| 932 | |
| 933 | /// Multiply two signed integers, computing the two's complement truncated |
| 934 | /// result, returning true if an overflow ocurred. |
| 935 | template <typename T> |
| 936 | std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) { |
| 937 | // Perform the unsigned multiplication on absolute values. |
| 938 | using U = std::make_unsigned_t<T>; |
| 939 | const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X); |
| 940 | const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y); |
| 941 | const U UResult = UX * UY; |
| 942 | |
| 943 | // Convert to signed. |
| 944 | const bool IsNegative = (X < 0) ^ (Y < 0); |
| 945 | Result = IsNegative ? (0 - UResult) : UResult; |
| 946 | |
| 947 | // If any of the args was 0, result is 0 and no overflow occurs. |
| 948 | if (UX == 0 || UY == 0) |
| 949 | return false; |
| 950 | |
| 951 | // UX and UY are in [1, 2^n], where n is the number of digits. |
| 952 | // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for |
| 953 | // positive) divided by an argument compares to the other. |
| 954 | if (IsNegative) |
| 955 | return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY; |
| 956 | else |
| 957 | return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY; |
| 958 | } |
| 959 | |
| 960 | } // End llvm namespace |
| 961 | |
| 962 | #endif |