clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Local.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/Utils/Local.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/Local.h" |
| 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/DenseMapInfo.h" |
| 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/Hashing.h" |
| 20 | #include "llvm/ADT/None.h" |
| 21 | #include "llvm/ADT/Optional.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | #include "llvm/ADT/SetVector.h" |
| 24 | #include "llvm/ADT/SmallPtrSet.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Analysis/AssumeBundleQueries.h" |
| 28 | #include "llvm/Analysis/ConstantFolding.h" |
| 29 | #include "llvm/Analysis/DomTreeUpdater.h" |
| 30 | #include "llvm/Analysis/EHPersonalities.h" |
| 31 | #include "llvm/Analysis/InstructionSimplify.h" |
| 32 | #include "llvm/Analysis/LazyValueInfo.h" |
| 33 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 34 | #include "llvm/Analysis/MemorySSAUpdater.h" |
| 35 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 36 | #include "llvm/Analysis/ValueTracking.h" |
| 37 | #include "llvm/Analysis/VectorUtils.h" |
| 38 | #include "llvm/BinaryFormat/Dwarf.h" |
| 39 | #include "llvm/IR/Argument.h" |
| 40 | #include "llvm/IR/Attributes.h" |
| 41 | #include "llvm/IR/BasicBlock.h" |
| 42 | #include "llvm/IR/CFG.h" |
| 43 | #include "llvm/IR/Constant.h" |
| 44 | #include "llvm/IR/ConstantRange.h" |
| 45 | #include "llvm/IR/Constants.h" |
| 46 | #include "llvm/IR/DIBuilder.h" |
| 47 | #include "llvm/IR/DataLayout.h" |
| 48 | #include "llvm/IR/DebugInfoMetadata.h" |
| 49 | #include "llvm/IR/DebugLoc.h" |
| 50 | #include "llvm/IR/DerivedTypes.h" |
| 51 | #include "llvm/IR/Dominators.h" |
| 52 | #include "llvm/IR/Function.h" |
| 53 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 54 | #include "llvm/IR/GlobalObject.h" |
| 55 | #include "llvm/IR/IRBuilder.h" |
| 56 | #include "llvm/IR/InstrTypes.h" |
| 57 | #include "llvm/IR/Instruction.h" |
| 58 | #include "llvm/IR/Instructions.h" |
| 59 | #include "llvm/IR/IntrinsicInst.h" |
| 60 | #include "llvm/IR/Intrinsics.h" |
| 61 | #include "llvm/IR/LLVMContext.h" |
| 62 | #include "llvm/IR/MDBuilder.h" |
| 63 | #include "llvm/IR/Metadata.h" |
| 64 | #include "llvm/IR/Module.h" |
| 65 | #include "llvm/IR/Operator.h" |
| 66 | #include "llvm/IR/PatternMatch.h" |
| 67 | #include "llvm/IR/PseudoProbe.h" |
| 68 | #include "llvm/IR/Type.h" |
| 69 | #include "llvm/IR/Use.h" |
| 70 | #include "llvm/IR/User.h" |
| 71 | #include "llvm/IR/Value.h" |
| 72 | #include "llvm/IR/ValueHandle.h" |
| 73 | #include "llvm/Support/Casting.h" |
| 74 | #include "llvm/Support/Debug.h" |
| 75 | #include "llvm/Support/ErrorHandling.h" |
| 76 | #include "llvm/Support/KnownBits.h" |
| 77 | #include "llvm/Support/raw_ostream.h" |
| 78 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 79 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 80 | #include <algorithm> |
| 81 | #include <cassert> |
| 82 | #include <climits> |
| 83 | #include <cstdint> |
| 84 | #include <iterator> |
| 85 | #include <map> |
| 86 | #include <utility> |
| 87 | |
| 88 | using namespace llvm; |
| 89 | using namespace llvm::PatternMatch; |
| 90 | |
| 91 | #define DEBUG_TYPE "local" |
| 92 | |
| 93 | STATISTIC(NumRemoved, "Number of unreachable basic blocks removed"); |
| 94 | STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd"); |
| 95 | |
| 96 | static cl::opt<bool> PHICSEDebugHash( |
| 97 | "phicse-debug-hash", |
| 98 | #ifdef EXPENSIVE_CHECKS |
| 99 | cl::init(true), |
| 100 | #else |
| 101 | cl::init(false), |
| 102 | #endif |
| 103 | cl::Hidden, |
| 104 | cl::desc("Perform extra assertion checking to verify that PHINodes's hash " |
| 105 | "function is well-behaved w.r.t. its isEqual predicate")); |
| 106 | |
| 107 | static cl::opt<unsigned> PHICSENumPHISmallSize( |
| 108 | "phicse-num-phi-smallsize", cl::init(32), cl::Hidden, |
| 109 | cl::desc( |
| 110 | "When the basic block contains not more than this number of PHI nodes, " |
| 111 | "perform a (faster!) exhaustive search instead of set-driven one.")); |
| 112 | |
| 113 | |
| 114 | |
| 115 | static const unsigned BitPartRecursionMaxDepth = 48; |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, |
| 129 | const TargetLibraryInfo *TLI, |
| 130 | DomTreeUpdater *DTU) { |
| 131 | Instruction *T = BB->getTerminator(); |
| 132 | IRBuilder<> Builder(T); |
| 133 | |
| 134 | |
| 135 | if (auto *BI = dyn_cast<BranchInst>(T)) { |
| 136 | if (BI->isUnconditional()) return false; |
| 137 | |
| 138 | BasicBlock *Dest1 = BI->getSuccessor(0); |
| 139 | BasicBlock *Dest2 = BI->getSuccessor(1); |
| 140 | |
| 141 | if (Dest2 == Dest1) { |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 148 | Dest1->removePredecessor(BI->getParent()); |
| 149 | |
| 150 | |
| 151 | BranchInst *NewBI = Builder.CreateBr(Dest1); |
| 152 | |
| 153 | |
| 154 | NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg, |
| 155 | LLVMContext::MD_annotation}); |
| 156 | |
| 157 | Value *Cond = BI->getCondition(); |
| 158 | BI->eraseFromParent(); |
| 159 | if (DeleteDeadConditions) |
| 160 | RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { |
| 165 | |
| 166 | |
| 167 | BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; |
| 168 | BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; |
| 169 | |
| 170 | |
| 171 | |
| 172 | OldDest->removePredecessor(BB); |
| 173 | |
| 174 | |
| 175 | BranchInst *NewBI = Builder.CreateBr(Destination); |
| 176 | |
| 177 | |
| 178 | NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg, |
| 179 | LLVMContext::MD_annotation}); |
| 180 | |
| 181 | BI->eraseFromParent(); |
| 182 | if (DTU) |
| 183 | DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}}); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (auto *SI = dyn_cast<SwitchInst>(T)) { |
| 191 | |
| 192 | |
| 193 | auto *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 194 | BasicBlock *DefaultDest = SI->getDefaultDest(); |
| 195 | BasicBlock *TheOnlyDest = DefaultDest; |
| 196 | |
| 197 | |
| 198 | if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) && |
| 199 | SI->getNumCases() > 0) { |
| 200 | TheOnlyDest = SI->case_begin()->getCaseSuccessor(); |
| 201 | } |
| 202 | |
| 203 | bool Changed = false; |
| 204 | |
| 205 | |
| 206 | for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) { |
| 207 | |
| 208 | if (i->getCaseValue() == CI) { |
| 209 | TheOnlyDest = i->getCaseSuccessor(); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | if (i->getCaseSuccessor() == DefaultDest) { |
| 216 | MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); |
| 217 | unsigned NCases = SI->getNumCases(); |
| 218 | |
| 219 | |
| 220 | if (NCases > 1 && MD && MD->getNumOperands() == 2 + NCases) { |
| 221 | |
| 222 | SmallVector<uint32_t, 8> Weights; |
| 223 | for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; |
| 224 | ++MD_i) { |
| 225 | auto *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i)); |
| 226 | Weights.push_back(CI->getValue().getZExtValue()); |
| 227 | } |
| 228 | |
| 229 | unsigned idx = i->getCaseIndex(); |
| 230 | Weights[0] += Weights[idx+1]; |
| 231 | |
| 232 | std::swap(Weights[idx+1], Weights.back()); |
| 233 | Weights.pop_back(); |
| 234 | SI->setMetadata(LLVMContext::MD_prof, |
| 235 | MDBuilder(BB->getContext()). |
| 236 | createBranchWeights(Weights)); |
| 237 | } |
| 238 | |
| 239 | BasicBlock *ParentBB = SI->getParent(); |
| 240 | DefaultDest->removePredecessor(ParentBB); |
| 241 | i = SI->removeCase(i); |
| 242 | e = SI->case_end(); |
| 243 | Changed = true; |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | if (i->getCaseSuccessor() != TheOnlyDest) |
| 251 | TheOnlyDest = nullptr; |
| 252 | |
| 253 | |
| 254 | ++i; |
| 255 | } |
| 256 | |
| 257 | if (CI && !TheOnlyDest) { |
| 258 | |
| 259 | |
| 260 | TheOnlyDest = SI->getDefaultDest(); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | if (TheOnlyDest) { |
| 266 | |
| 267 | Builder.CreateBr(TheOnlyDest); |
| 268 | BasicBlock *BB = SI->getParent(); |
| 269 | |
| 270 | SmallSet<BasicBlock *, 8> RemovedSuccessors; |
| 271 | |
| 272 | |
| 273 | BasicBlock *SuccToKeep = TheOnlyDest; |
| 274 | for (BasicBlock *Succ : successors(SI)) { |
| 275 | if (DTU && Succ != TheOnlyDest) |
| 276 | RemovedSuccessors.insert(Succ); |
| 277 | |
| 278 | if (Succ == SuccToKeep) { |
| 279 | SuccToKeep = nullptr; |
| 280 | } else { |
| 281 | Succ->removePredecessor(BB); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | Value *Cond = SI->getCondition(); |
| 287 | SI->eraseFromParent(); |
| 288 | if (DeleteDeadConditions) |
| 289 | RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); |
| 290 | if (DTU) { |
| 291 | std::vector<DominatorTree::UpdateType> Updates; |
| 292 | Updates.reserve(RemovedSuccessors.size()); |
| 293 | for (auto *RemovedSuccessor : RemovedSuccessors) |
| 294 | Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor}); |
| 295 | DTU->applyUpdates(Updates); |
| 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | if (SI->getNumCases() == 1) { |
| 301 | |
| 302 | |
| 303 | auto FirstCase = *SI->case_begin(); |
| 304 | Value *Cond = Builder.CreateICmpEQ(SI->getCondition(), |
| 305 | FirstCase.getCaseValue(), "cond"); |
| 306 | |
| 307 | |
| 308 | BranchInst *NewBr = Builder.CreateCondBr(Cond, |
| 309 | FirstCase.getCaseSuccessor(), |
| 310 | SI->getDefaultDest()); |
| 311 | MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); |
| 312 | if (MD && MD->getNumOperands() == 3) { |
| 313 | ConstantInt *SICase = |
| 314 | mdconst::dyn_extract<ConstantInt>(MD->getOperand(2)); |
| 315 | ConstantInt *SIDef = |
| 316 | mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); |
| 317 | assert(SICase && SIDef); |
| 318 | |
| 319 | NewBr->setMetadata(LLVMContext::MD_prof, |
| 320 | MDBuilder(BB->getContext()). |
| 321 | createBranchWeights(SICase->getValue().getZExtValue(), |
| 322 | SIDef->getValue().getZExtValue())); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit); |
| 327 | if (MakeImplicitMD) |
| 328 | NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD); |
| 329 | |
| 330 | |
| 331 | SI->eraseFromParent(); |
| 332 | return true; |
| 333 | } |
| 334 | return Changed; |
| 335 | } |
| 336 | |
| 337 | if (auto *IBI = dyn_cast<IndirectBrInst>(T)) { |
| 338 | |
| 339 | if (auto *BA = |
| 340 | dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { |
| 341 | BasicBlock *TheOnlyDest = BA->getBasicBlock(); |
| 342 | SmallSet<BasicBlock *, 8> RemovedSuccessors; |
| 343 | |
| 344 | |
| 345 | Builder.CreateBr(TheOnlyDest); |
| 346 | |
| 347 | BasicBlock *SuccToKeep = TheOnlyDest; |
| 348 | for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { |
| 349 | BasicBlock *DestBB = IBI->getDestination(i); |
| 350 | if (DTU && DestBB != TheOnlyDest) |
| 351 | RemovedSuccessors.insert(DestBB); |
| 352 | if (IBI->getDestination(i) == SuccToKeep) { |
| 353 | SuccToKeep = nullptr; |
| 354 | } else { |
| 355 | DestBB->removePredecessor(BB); |
| 356 | } |
| 357 | } |
| 358 | Value *Address = IBI->getAddress(); |
| 359 | IBI->eraseFromParent(); |
| 360 | if (DeleteDeadConditions) |
| 361 | |
| 362 | RecursivelyDeleteTriviallyDeadInstructions(Address, TLI); |
| 363 | |
| 364 | |
| 365 | |
| 366 | if (BA->use_empty()) |
| 367 | BA->destroyConstant(); |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | if (SuccToKeep) { |
| 373 | BB->getTerminator()->eraseFromParent(); |
| 374 | new UnreachableInst(BB->getContext(), BB); |
| 375 | } |
| 376 | |
| 377 | if (DTU) { |
| 378 | std::vector<DominatorTree::UpdateType> Updates; |
| 379 | Updates.reserve(RemovedSuccessors.size()); |
| 380 | for (auto *RemovedSuccessor : RemovedSuccessors) |
| 381 | Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor}); |
| 382 | DTU->applyUpdates(Updates); |
| 383 | } |
| 384 | return true; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | bool llvm::isInstructionTriviallyDead(Instruction *I, |
| 399 | const TargetLibraryInfo *TLI) { |
| 400 | if (!I->use_empty()) |
| 401 | return false; |
| 402 | return wouldInstructionBeTriviallyDead(I, TLI); |
| 403 | } |
| 404 | |
| 405 | bool llvm::wouldInstructionBeTriviallyDead(Instruction *I, |
| 406 | const TargetLibraryInfo *TLI) { |
| 407 | if (I->isTerminator()) |
| 408 | return false; |
| 409 | |
| 410 | |
| 411 | |
| 412 | if (I->isEHPad()) |
| 413 | return false; |
| 414 | |
| 415 | |
| 416 | |
| 417 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { |
| 418 | if (DDI->getAddress()) |
| 419 | return false; |
| 420 | return true; |
| 421 | } |
| 422 | if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { |
| 423 | if (DVI->hasArgList() || DVI->getValue(0)) |
| 424 | return false; |
| 425 | return true; |
| 426 | } |
| 427 | if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(I)) { |
| 428 | if (DLI->getLabel()) |
| 429 | return false; |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | if (!I->willReturn()) |
| 434 | return false; |
| 435 | |
| 436 | if (!I->mayHaveSideEffects()) |
| 437 | return true; |
| 438 | |
| 439 | |
| 440 | |
| 441 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 442 | |
| 443 | if (II->getIntrinsicID() == Intrinsic::stacksave || |
| 444 | II->getIntrinsicID() == Intrinsic::launder_invariant_group) |
| 445 | return true; |
| 446 | |
| 447 | if (II->isLifetimeStartOrEnd()) { |
| 448 | auto *Arg = II->getArgOperand(1); |
| 449 | |
| 450 | if (isa<UndefValue>(Arg)) |
| 451 | return true; |
| 452 | |
| 453 | |
| 454 | if (isa<AllocaInst>(Arg) || isa<GlobalValue>(Arg) || isa<Argument>(Arg)) |
| 455 | return llvm::all_of(Arg->uses(), [](Use &Use) { |
| 456 | if (IntrinsicInst *IntrinsicUse = |
| 457 | dyn_cast<IntrinsicInst>(Use.getUser())) |
| 458 | return IntrinsicUse->isLifetimeStartOrEnd(); |
| 459 | return false; |
| 460 | }); |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | if ((II->getIntrinsicID() == Intrinsic::assume && |
| 469 | isAssumeWithEmptyBundle(cast<AssumeInst>(*II))) || |
| 470 | II->getIntrinsicID() == Intrinsic::experimental_guard) { |
| 471 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0))) |
| 472 | return !Cond->isZero(); |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I)) { |
| 478 | Optional<fp::ExceptionBehavior> ExBehavior = FPI->getExceptionBehavior(); |
| 479 | return ExBehavior.getValue() != fp::ebStrict; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (isAllocLikeFn(I, TLI)) |
| 484 | return true; |
| 485 | |
| 486 | if (CallInst *CI = isFreeCall(I, TLI)) |
| 487 | if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) |
| 488 | return C->isNullValue() || isa<UndefValue>(C); |
| 489 | |
| 490 | if (auto *Call = dyn_cast<CallBase>(I)) |
| 491 | if (isMathLibCallNoop(Call, TLI)) |
| 492 | return true; |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(I)) { |
| 499 | Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior(); |
| 500 | if (!EB || *EB == fp::ExceptionBehavior::ebIgnore) |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | bool llvm::RecursivelyDeleteTriviallyDeadInstructions( |
| 512 | Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU, |
| 513 | std::function<void(Value *)> AboutToDeleteCallback) { |
| 514 | Instruction *I = dyn_cast<Instruction>(V); |
| 515 | if (!I || !isInstructionTriviallyDead(I, TLI)) |
| 516 | return false; |
| 517 | |
| 518 | SmallVector<WeakTrackingVH, 16> DeadInsts; |
| 519 | DeadInsts.push_back(I); |
| 520 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU, |
| 521 | AboutToDeleteCallback); |
| 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive( |
| 527 | SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI, |
| 528 | MemorySSAUpdater *MSSAU, |
| 529 | std::function<void(Value *)> AboutToDeleteCallback) { |
| 530 | unsigned S = 0, E = DeadInsts.size(), Alive = 0; |
| 531 | for (; S != E; ++S) { |
| 532 | auto *I = cast<Instruction>(DeadInsts[S]); |
| 533 | if (!isInstructionTriviallyDead(I)) { |
| 534 | DeadInsts[S] = nullptr; |
| 535 | ++Alive; |
| 536 | } |
| 537 | } |
| 538 | if (Alive == E) |
| 539 | return false; |
| 540 | RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU, |
| 541 | AboutToDeleteCallback); |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | void llvm::RecursivelyDeleteTriviallyDeadInstructions( |
| 546 | SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI, |
| 547 | MemorySSAUpdater *MSSAU, |
| 548 | std::function<void(Value *)> AboutToDeleteCallback) { |
| 549 | |
| 550 | while (!DeadInsts.empty()) { |
| 551 | Value *V = DeadInsts.pop_back_val(); |
| 552 | Instruction *I = cast_or_null<Instruction>(V); |
| 553 | if (!I) |
| 554 | continue; |
| 555 | assert(isInstructionTriviallyDead(I, TLI) && |
| 556 | "Live instruction found in dead worklist!"); |
| 557 | assert(I->use_empty() && "Instructions with uses are not dead."); |
| 558 | |
| 559 | |
| 560 | salvageDebugInfo(*I); |
| 561 | |
| 562 | if (AboutToDeleteCallback) |
| 563 | AboutToDeleteCallback(I); |
| 564 | |
| 565 | |
| 566 | |
| 567 | for (Use &OpU : I->operands()) { |
| 568 | Value *OpV = OpU.get(); |
| 569 | OpU.set(nullptr); |
| 570 | |
| 571 | if (!OpV->use_empty()) |
| 572 | continue; |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) |
| 578 | if (isInstructionTriviallyDead(OpI, TLI)) |
| 579 | DeadInsts.push_back(OpI); |
| 580 | } |
| 581 | if (MSSAU) |
| 582 | MSSAU->removeMemoryAccess(I); |
| 583 | |
| 584 | I->eraseFromParent(); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | bool llvm::replaceDbgUsesWithUndef(Instruction *I) { |
| 589 | SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; |
| 590 | findDbgUsers(DbgUsers, I); |
| 591 | for (auto *DII : DbgUsers) { |
| 592 | Value *Undef = UndefValue::get(I->getType()); |
| 593 | DII->replaceVariableLocationOp(I, Undef); |
| 594 | } |
| 595 | return !DbgUsers.empty(); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | static bool areAllUsesEqual(Instruction *I) { |
| 603 | Value::user_iterator UI = I->user_begin(); |
| 604 | Value::user_iterator UE = I->user_end(); |
| 605 | if (UI == UE) |
| 606 | return true; |
| 607 | |
| 608 | User *TheUse = *UI; |
| 609 | for (++UI; UI != UE; ++UI) { |
| 610 | if (*UI != TheUse) |
| 611 | return false; |
| 612 | } |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, |
| 622 | const TargetLibraryInfo *TLI, |
| 623 | llvm::MemorySSAUpdater *MSSAU) { |
| 624 | SmallPtrSet<Instruction*, 4> Visited; |
| 625 | for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects(); |
| 626 | I = cast<Instruction>(*I->user_begin())) { |
| 627 | if (I->use_empty()) |
| 628 | return RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU); |
| 629 | |
| 630 | |
| 631 | |
| 632 | if (!Visited.insert(I).second) { |
| 633 | |
| 634 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 635 | (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU); |
| 636 | return true; |
| 637 | } |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | static bool |
| 643 | simplifyAndDCEInstruction(Instruction *I, |
| 644 | SmallSetVector<Instruction *, 16> &WorkList, |
| 645 | const DataLayout &DL, |
| 646 | const TargetLibraryInfo *TLI) { |
| 647 | if (isInstructionTriviallyDead(I, TLI)) { |
| 648 | salvageDebugInfo(*I); |
| 649 | |
| 650 | |
| 651 | |
| 652 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 653 | Value *OpV = I->getOperand(i); |
| 654 | I->setOperand(i, nullptr); |
| 655 | |
| 656 | if (!OpV->use_empty() || I == OpV) |
| 657 | continue; |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) |
| 663 | if (isInstructionTriviallyDead(OpI, TLI)) |
| 664 | WorkList.insert(OpI); |
| 665 | } |
| 666 | |
| 667 | I->eraseFromParent(); |
| 668 | |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | if (Value *SimpleV = SimplifyInstruction(I, DL)) { |
| 673 | |
| 674 | |
| 675 | for (User *U : I->users()) { |
| 676 | if (U != I) { |
| 677 | WorkList.insert(cast<Instruction>(U)); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | |
| 682 | bool Changed = false; |
| 683 | if (!I->use_empty()) { |
| 684 | I->replaceAllUsesWith(SimpleV); |
| 685 | Changed = true; |
| 686 | } |
| 687 | if (isInstructionTriviallyDead(I, TLI)) { |
| 688 | I->eraseFromParent(); |
| 689 | Changed = true; |
| 690 | } |
| 691 | return Changed; |
| 692 | } |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, |
| 702 | const TargetLibraryInfo *TLI) { |
| 703 | bool MadeChange = false; |
| 704 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 705 | |
| 706 | #ifndef NDEBUG |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | AssertingVH<Instruction> TerminatorVH(&BB->back()); |
| 712 | #endif |
| 713 | |
| 714 | SmallSetVector<Instruction *, 16> WorkList; |
| 715 | |
| 716 | |
| 717 | |
| 718 | for (BasicBlock::iterator BI = BB->begin(), E = std::prev(BB->end()); |
| 719 | BI != E;) { |
| 720 | assert(!BI->isTerminator()); |
| 721 | Instruction *I = &*BI; |
| 722 | ++BI; |
| 723 | |
| 724 | |
| 725 | |
| 726 | if (!WorkList.count(I)) |
| 727 | MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI); |
| 728 | } |
| 729 | |
| 730 | while (!WorkList.empty()) { |
| 731 | Instruction *I = WorkList.pop_back_val(); |
| 732 | MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI); |
| 733 | } |
| 734 | return MadeChange; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, |
| 742 | DomTreeUpdater *DTU) { |
| 743 | |
| 744 | |
| 745 | while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { |
| 746 | Value *NewVal = PN->getIncomingValue(0); |
| 747 | |
| 748 | if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); |
| 749 | PN->replaceAllUsesWith(NewVal); |
| 750 | PN->eraseFromParent(); |
| 751 | } |
| 752 | |
| 753 | BasicBlock *PredBB = DestBB->getSinglePredecessor(); |
| 754 | assert(PredBB && "Block doesn't have a single predecessor!"); |
| 755 | |
| 756 | bool ReplaceEntryBB = PredBB->isEntryBlock(); |
| 757 | |
| 758 | |
| 759 | |
| 760 | SmallVector<DominatorTree::UpdateType, 32> Updates; |
| 761 | |
| 762 | if (DTU) { |
| 763 | SmallPtrSet<BasicBlock *, 2> PredsOfPredBB(pred_begin(PredBB), |
| 764 | pred_end(PredBB)); |
| 765 | Updates.reserve(Updates.size() + 2 * PredsOfPredBB.size() + 1); |
| 766 | for (BasicBlock *PredOfPredBB : PredsOfPredBB) |
| 767 | |
| 768 | if (PredOfPredBB != PredBB) |
| 769 | Updates.push_back({DominatorTree::Insert, PredOfPredBB, DestBB}); |
| 770 | for (BasicBlock *PredOfPredBB : PredsOfPredBB) |
| 771 | Updates.push_back({DominatorTree::Delete, PredOfPredBB, PredBB}); |
| 772 | Updates.push_back({DominatorTree::Delete, PredBB, DestBB}); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | |
| 777 | if (DestBB->hasAddressTaken()) { |
| 778 | BlockAddress *BA = BlockAddress::get(DestBB); |
| 779 | Constant *Replacement = |
| 780 | ConstantInt::get(Type::getInt32Ty(BA->getContext()), 1); |
| 781 | BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, |
| 782 | BA->getType())); |
| 783 | BA->destroyConstant(); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | PredBB->replaceAllUsesWith(DestBB); |
| 788 | |
| 789 | |
| 790 | PredBB->getTerminator()->eraseFromParent(); |
| 791 | DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); |
| 792 | new UnreachableInst(PredBB->getContext(), PredBB); |
| 793 | |
| 794 | |
| 795 | |
| 796 | if (ReplaceEntryBB) |
| 797 | DestBB->moveAfter(PredBB); |
| 798 | |
| 799 | if (DTU) { |
| 800 | assert(PredBB->getInstList().size() == 1 && |
| 801 | isa<UnreachableInst>(PredBB->getTerminator()) && |
| 802 | "The successor list of PredBB isn't empty before " |
| 803 | "applying corresponding DTU updates."); |
| 804 | DTU->applyUpdatesPermissive(Updates); |
| 805 | DTU->deleteBB(PredBB); |
| 806 | |
| 807 | |
| 808 | if (ReplaceEntryBB && DTU->hasDomTree()) { |
| 809 | |
| 810 | |
| 811 | |
| 812 | DTU->recalculate(*(DestBB->getParent())); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | else { |
| 817 | PredBB->eraseFromParent(); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
| 822 | |
| 823 | static bool CanMergeValues(Value *First, Value *Second) { |
| 824 | return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
| 832 | assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); |
| 833 | |
| 834 | LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " |
| 835 | << Succ->getName() << "\n"); |
| 836 | |
| 837 | |
| 838 | if (Succ->getSinglePredecessor()) return true; |
| 839 | |
| 840 | |
| 841 | SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); |
| 842 | |
| 843 | |
| 844 | |
| 845 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 846 | PHINode *PN = cast<PHINode>(I); |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); |
| 852 | if (BBPN && BBPN->getParent() == BB) { |
| 853 | for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { |
| 854 | BasicBlock *IBB = PN->getIncomingBlock(PI); |
| 855 | if (BBPreds.count(IBB) && |
| 856 | !CanMergeValues(BBPN->getIncomingValueForBlock(IBB), |
| 857 | PN->getIncomingValue(PI))) { |
| 858 | LLVM_DEBUG(dbgs() |
| 859 | << "Can't fold, phi node " << PN->getName() << " in " |
| 860 | << Succ->getName() << " is conflicting with " |
| 861 | << BBPN->getName() << " with regard to common predecessor " |
| 862 | << IBB->getName() << "\n"); |
| 863 | return false; |
| 864 | } |
| 865 | } |
| 866 | } else { |
| 867 | Value* Val = PN->getIncomingValueForBlock(BB); |
| 868 | for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { |
| 869 | |
| 870 | |
| 871 | |
| 872 | BasicBlock *IBB = PN->getIncomingBlock(PI); |
| 873 | if (BBPreds.count(IBB) && |
| 874 | !CanMergeValues(Val, PN->getIncomingValue(PI))) { |
| 875 | LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() |
| 876 | << " in " << Succ->getName() |
| 877 | << " is conflicting with regard to common " |
| 878 | << "predecessor " << IBB->getName() << "\n"); |
| 879 | return false; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return true; |
| 886 | } |
| 887 | |
| 888 | using PredBlockVector = SmallVector<BasicBlock *, 16>; |
| 889 | using IncomingValueMap = DenseMap<BasicBlock *, Value *>; |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB, |
| 904 | IncomingValueMap &IncomingValues) { |
| 905 | if (!isa<UndefValue>(OldVal)) { |
| 906 | assert((!IncomingValues.count(BB) || |
| 907 | IncomingValues.find(BB)->second == OldVal) && |
| 908 | "Expected OldVal to match incoming value from BB!"); |
| 909 | |
| 910 | IncomingValues.insert(std::make_pair(BB, OldVal)); |
| 911 | return OldVal; |
| 912 | } |
| 913 | |
| 914 | IncomingValueMap::const_iterator It = IncomingValues.find(BB); |
| 915 | if (It != IncomingValues.end()) return It->second; |
| 916 | |
| 917 | return OldVal; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | static void gatherIncomingValuesToPhi(PHINode *PN, |
| 929 | IncomingValueMap &IncomingValues) { |
| 930 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 931 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 932 | Value *V = PN->getIncomingValue(i); |
| 933 | |
| 934 | if (!isa<UndefValue>(V)) |
| 935 | IncomingValues.insert(std::make_pair(BB, V)); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | static void replaceUndefValuesInPhi(PHINode *PN, |
| 945 | const IncomingValueMap &IncomingValues) { |
| 946 | SmallVector<unsigned> TrueUndefOps; |
| 947 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 948 | Value *V = PN->getIncomingValue(i); |
| 949 | |
| 950 | if (!isa<UndefValue>(V)) continue; |
| 951 | |
| 952 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 953 | IncomingValueMap::const_iterator It = IncomingValues.find(BB); |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | if (It == IncomingValues.end()) { |
| 960 | TrueUndefOps.push_back(i); |
| 961 | continue; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | |
| 966 | PN->setIncomingValue(i, It->second); |
| 967 | } |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | unsigned PoisonCount = count_if(TrueUndefOps, [&](unsigned i) { |
| 973 | return isa<PoisonValue>(PN->getIncomingValue(i)); |
| 974 | }); |
| 975 | if (PoisonCount != 0 && PoisonCount != TrueUndefOps.size()) { |
| 976 | for (unsigned i : TrueUndefOps) |
| 977 | PN->setIncomingValue(i, UndefValue::get(PN->getType())); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB, |
| 989 | const PredBlockVector &BBPreds, |
| 990 | PHINode *PN) { |
| 991 | Value *OldVal = PN->removeIncomingValue(BB, false); |
| 992 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 993 | |
| 994 | IncomingValueMap IncomingValues; |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | gatherIncomingValuesToPhi(PN, IncomingValues); |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { |
| 1010 | PHINode *OldValPN = cast<PHINode>(OldVal); |
| 1011 | for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) { |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | BasicBlock *PredBB = OldValPN->getIncomingBlock(i); |
| 1018 | Value *PredVal = OldValPN->getIncomingValue(i); |
| 1019 | Value *Selected = selectIncomingValueForBlock(PredVal, PredBB, |
| 1020 | IncomingValues); |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | PN->addIncoming(Selected, PredBB); |
| 1025 | } |
| 1026 | } else { |
| 1027 | for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) { |
| 1028 | |
| 1029 | |
| 1030 | BasicBlock *PredBB = BBPreds[i]; |
| 1031 | Value *Selected = selectIncomingValueForBlock(OldVal, PredBB, |
| 1032 | IncomingValues); |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | PN->addIncoming(Selected, PredBB); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | replaceUndefValuesInPhi(PN, IncomingValues); |
| 1041 | } |
| 1042 | |
| 1043 | bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, |
| 1044 | DomTreeUpdater *DTU) { |
| 1045 | assert(BB != &BB->getParent()->getEntryBlock() && |
| 1046 | "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); |
| 1047 | |
| 1048 | |
| 1049 | BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); |
| 1050 | if (BB == Succ) return false; |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | if (!Succ->getSinglePredecessor()) { |
| 1069 | BasicBlock::iterator BBI = BB->begin(); |
| 1070 | while (isa<PHINode>(*BBI)) { |
| 1071 | for (Use &U : BBI->uses()) { |
| 1072 | if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) { |
| 1073 | if (PN->getIncomingBlock(U) != BB) |
| 1074 | return false; |
| 1075 | } else { |
| 1076 | return false; |
| 1077 | } |
| 1078 | } |
| 1079 | ++BBI; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | for (BasicBlock *PredBB : predecessors(BB)) { |
| 1086 | if (auto *CBI = dyn_cast<CallBrInst>(PredBB->getTerminator())) { |
| 1087 | if (Succ == CBI->getDefaultDest()) |
| 1088 | return false; |
| 1089 | for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) |
| 1090 | if (Succ == CBI->getIndirectDest(i)) |
| 1091 | return false; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); |
| 1096 | |
| 1097 | SmallVector<DominatorTree::UpdateType, 32> Updates; |
| 1098 | if (DTU) { |
| 1099 | |
| 1100 | SmallPtrSet<BasicBlock *, 8> PredsOfBB(pred_begin(BB), pred_end(BB)); |
| 1101 | SmallPtrSet<BasicBlock *, 8> PredsOfSucc(pred_begin(Succ), pred_end(Succ)); |
| 1102 | Updates.reserve(Updates.size() + 2 * PredsOfBB.size() + 1); |
| 1103 | for (auto *PredOfBB : PredsOfBB) |
| 1104 | |
| 1105 | if (!PredsOfSucc.contains(PredOfBB)) |
| 1106 | Updates.push_back({DominatorTree::Insert, PredOfBB, Succ}); |
| 1107 | for (auto *PredOfBB : PredsOfBB) |
| 1108 | Updates.push_back({DominatorTree::Delete, PredOfBB, BB}); |
| 1109 | Updates.push_back({DominatorTree::Delete, BB, Succ}); |
| 1110 | } |
| 1111 | |
| 1112 | if (isa<PHINode>(Succ->begin())) { |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB)); |
| 1117 | |
| 1118 | |
| 1119 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 1120 | PHINode *PN = cast<PHINode>(I); |
| 1121 | |
| 1122 | redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | if (Succ->getSinglePredecessor()) { |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | BB->getTerminator()->eraseFromParent(); |
| 1132 | Succ->getInstList().splice(Succ->getFirstNonPHI()->getIterator(), |
| 1133 | BB->getInstList()); |
| 1134 | } else { |
| 1135 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 1136 | |
| 1137 | assert(PN->use_empty() && "There shouldn't be any uses here!"); |
| 1138 | PN->eraseFromParent(); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | unsigned LoopMDKind = BB->getContext().getMDKindID("llvm.loop"); |
| 1145 | Instruction *TI = BB->getTerminator(); |
| 1146 | if (TI) |
| 1147 | if (MDNode *LoopMD = TI->getMetadata(LoopMDKind)) |
| 1148 | for (BasicBlock *Pred : predecessors(BB)) |
| 1149 | Pred->getTerminator()->setMetadata(LoopMDKind, LoopMD); |
| 1150 | |
| 1151 | |
| 1152 | BB->replaceAllUsesWith(Succ); |
| 1153 | if (!Succ->hasName()) Succ->takeName(BB); |
| 1154 | |
| 1155 | |
| 1156 | if (BB->getTerminator()) |
| 1157 | BB->getInstList().pop_back(); |
| 1158 | new UnreachableInst(BB->getContext(), BB); |
| 1159 | assert(succ_empty(BB) && "The successor list of BB isn't empty before " |
| 1160 | "applying corresponding DTU updates."); |
| 1161 | |
| 1162 | if (DTU) |
| 1163 | DTU->applyUpdates(Updates); |
| 1164 | |
| 1165 | DeleteDeadBlock(BB, DTU); |
| 1166 | |
| 1167 | return true; |
| 1168 | } |
| 1169 | |
| 1170 | static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) { |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | bool Changed = false; |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I);) { |
| 1181 | ++I; |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) { |
| 1186 | if (!DuplicatePN->isIdenticalToWhenDefined(PN)) |
| 1187 | continue; |
| 1188 | |
| 1189 | ++NumPHICSEs; |
| 1190 | DuplicatePN->replaceAllUsesWith(PN); |
| 1191 | DuplicatePN->eraseFromParent(); |
| 1192 | Changed = true; |
| 1193 | |
| 1194 | |
| 1195 | I = BB->begin(); |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | return Changed; |
| 1200 | } |
| 1201 | |
| 1202 | static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) { |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | struct PHIDenseMapInfo { |
| 1208 | static PHINode *getEmptyKey() { |
| 1209 | return DenseMapInfo<PHINode *>::getEmptyKey(); |
| 1210 | } |
| 1211 | |
| 1212 | static PHINode *getTombstoneKey() { |
| 1213 | return DenseMapInfo<PHINode *>::getTombstoneKey(); |
| 1214 | } |
| 1215 | |
| 1216 | static bool isSentinel(PHINode *PN) { |
| 1217 | return PN == getEmptyKey() || PN == getTombstoneKey(); |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | static unsigned getHashValueImpl(PHINode *PN) { |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | return static_cast<unsigned>(hash_combine( |
| 1227 | hash_combine_range(PN->value_op_begin(), PN->value_op_end()), |
| 1228 | hash_combine_range(PN->block_begin(), PN->block_end()))); |
| 1229 | } |
| 1230 | |
| 1231 | static unsigned getHashValue(PHINode *PN) { |
| 1232 | #ifndef NDEBUG |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | if (PHICSEDebugHash) |
| 1238 | return 0; |
| 1239 | #endif |
| 1240 | return getHashValueImpl(PN); |
| 1241 | } |
| 1242 | |
| 1243 | static bool isEqualImpl(PHINode *LHS, PHINode *RHS) { |
| 1244 | if (isSentinel(LHS) || isSentinel(RHS)) |
| 1245 | return LHS == RHS; |
| 1246 | return LHS->isIdenticalTo(RHS); |
| 1247 | } |
| 1248 | |
| 1249 | static bool isEqual(PHINode *LHS, PHINode *RHS) { |
| 1250 | |
| 1251 | |
| 1252 | bool Result = isEqualImpl(LHS, RHS); |
| 1253 | assert(!Result || (isSentinel(LHS) && LHS == RHS) || |
| 1254 | getHashValueImpl(LHS) == getHashValueImpl(RHS)); |
| 1255 | return Result; |
| 1256 | } |
| 1257 | }; |
| 1258 | |
| 1259 | |
| 1260 | DenseSet<PHINode *, PHIDenseMapInfo> PHISet; |
| 1261 | PHISet.reserve(4 * PHICSENumPHISmallSize); |
| 1262 | |
| 1263 | |
| 1264 | bool Changed = false; |
| 1265 | for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) { |
| 1266 | auto Inserted = PHISet.insert(PN); |
| 1267 | if (!Inserted.second) { |
| 1268 | |
| 1269 | ++NumPHICSEs; |
| 1270 | PN->replaceAllUsesWith(*Inserted.first); |
| 1271 | PN->eraseFromParent(); |
| 1272 | Changed = true; |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | PHISet.clear(); |
| 1277 | I = BB->begin(); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | return Changed; |
| 1282 | } |
| 1283 | |
| 1284 | bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { |
| 1285 | if ( |
| 1286 | #ifndef NDEBUG |
| 1287 | !PHICSEDebugHash && |
| 1288 | #endif |
| 1289 | hasNItemsOrLess(BB->phis(), PHICSENumPHISmallSize)) |
| 1290 | return EliminateDuplicatePHINodesNaiveImpl(BB); |
| 1291 | return EliminateDuplicatePHINodesSetBasedImpl(BB); |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | static Align tryEnforceAlignment(Value *V, Align PrefAlign, |
| 1302 | const DataLayout &DL) { |
| 1303 | V = V->stripPointerCasts(); |
| 1304 | |
| 1305 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | Align CurrentAlign = AI->getAlign(); |
| 1312 | if (PrefAlign <= CurrentAlign) |
| 1313 | return CurrentAlign; |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | if (DL.exceedsNaturalStackAlignment(PrefAlign)) |
| 1318 | return CurrentAlign; |
| 1319 | AI->setAlignment(PrefAlign); |
| 1320 | return PrefAlign; |
| 1321 | } |
| 1322 | |
| 1323 | if (auto *GO = dyn_cast<GlobalObject>(V)) { |
| 1324 | |
| 1325 | Align CurrentAlign = GO->getPointerAlignment(DL); |
| 1326 | if (PrefAlign <= CurrentAlign) |
| 1327 | return CurrentAlign; |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | if (!GO->canIncreaseAlignment()) |
| 1334 | return CurrentAlign; |
| 1335 | |
| 1336 | GO->setAlignment(PrefAlign); |
| 1337 | return PrefAlign; |
| 1338 | } |
| 1339 | |
| 1340 | return Align(1); |
| 1341 | } |
| 1342 | |
| 1343 | Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, |
| 1344 | const DataLayout &DL, |
| 1345 | const Instruction *CxtI, |
| 1346 | AssumptionCache *AC, |
| 1347 | const DominatorTree *DT) { |
| 1348 | assert(V->getType()->isPointerTy() && |
| 1349 | "getOrEnforceKnownAlignment expects a pointer!"); |
| 1350 | |
| 1351 | KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT); |
| 1352 | unsigned TrailZ = Known.countMinTrailingZeros(); |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent); |
| 1358 | |
| 1359 | Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ)); |
| 1360 | |
| 1361 | if (PrefAlign && *PrefAlign > Alignment) |
| 1362 | Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL)); |
| 1363 | |
| 1364 | |
| 1365 | return Alignment; |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | static bool PhiHasDebugValue(DILocalVariable *DIVar, |
| 1374 | DIExpression *DIExpr, |
| 1375 | PHINode *APN) { |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | SmallVector<DbgValueInst *, 1> DbgValues; |
| 1380 | findDbgValues(DbgValues, APN); |
| 1381 | for (auto *DVI : DbgValues) { |
| 1382 | assert(is_contained(DVI->getValues(), APN)); |
| 1383 | if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr)) |
| 1384 | return true; |
| 1385 | } |
| 1386 | return false; |
| 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) { |
| 1399 | const DataLayout &DL = DII->getModule()->getDataLayout(); |
| 1400 | TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy); |
| 1401 | if (Optional<uint64_t> FragmentSize = DII->getFragmentSizeInBits()) { |
| 1402 | assert(!ValueSize.isScalable() && |
| 1403 | "Fragments don't work on scalable types."); |
| 1404 | return ValueSize.getFixedSize() >= *FragmentSize; |
| 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | if (DII->isAddressOfVariable()) { |
| 1410 | |
| 1411 | assert(DII->getNumVariableLocationOps() == 1 && |
| 1412 | "address of variable must have exactly 1 location operand."); |
| 1413 | if (auto *AI = |
| 1414 | dyn_cast_or_null<AllocaInst>(DII->getVariableLocationOp(0))) { |
| 1415 | if (Optional<TypeSize> FragmentSize = AI->getAllocationSizeInBits(DL)) { |
| 1416 | assert(ValueSize.isScalable() == FragmentSize->isScalable() && |
| 1417 | "Both sizes should agree on the scalable flag."); |
| 1418 | return TypeSize::isKnownGE(ValueSize, *FragmentSize); |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | static DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII, Instruction *Src) { |
| 1431 | |
| 1432 | const DebugLoc &DeclareLoc = DII->getDebugLoc(); |
| 1433 | MDNode *Scope = DeclareLoc.getScope(); |
| 1434 | DILocation *InlinedAt = DeclareLoc.getInlinedAt(); |
| 1435 | |
| 1436 | return DILocation::get(DII->getContext(), 0, 0, Scope, InlinedAt); |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, |
| 1442 | StoreInst *SI, DIBuilder &Builder) { |
| 1443 | assert(DII->isAddressOfVariable()); |
| 1444 | auto *DIVar = DII->getVariable(); |
| 1445 | assert(DIVar && "Missing variable"); |
| 1446 | auto *DIExpr = DII->getExpression(); |
| 1447 | Value *DV = SI->getValueOperand(); |
| 1448 | |
| 1449 | DebugLoc NewLoc = getDebugValueLoc(DII, SI); |
| 1450 | |
| 1451 | if (!valueCoversEntireFragment(DV->getType(), DII)) { |
| 1452 | |
| 1453 | |
| 1454 | LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " |
| 1455 | << *DII << '\n'); |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | DV = UndefValue::get(DV->getType()); |
| 1460 | Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, |
| 1470 | LoadInst *LI, DIBuilder &Builder) { |
| 1471 | auto *DIVar = DII->getVariable(); |
| 1472 | auto *DIExpr = DII->getExpression(); |
| 1473 | assert(DIVar && "Missing variable"); |
| 1474 | |
| 1475 | if (!valueCoversEntireFragment(LI->getType(), DII)) { |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " |
| 1480 | << *DII << '\n'); |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | Instruction *DbgValue = Builder.insertDbgValueIntrinsic( |
| 1491 | LI, DIVar, DIExpr, NewLoc, (Instruction *)nullptr); |
| 1492 | DbgValue->insertAfter(LI); |
| 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, |
| 1498 | PHINode *APN, DIBuilder &Builder) { |
| 1499 | auto *DIVar = DII->getVariable(); |
| 1500 | auto *DIExpr = DII->getExpression(); |
| 1501 | assert(DIVar && "Missing variable"); |
| 1502 | |
| 1503 | if (PhiHasDebugValue(DIVar, DIExpr, APN)) |
| 1504 | return; |
| 1505 | |
| 1506 | if (!valueCoversEntireFragment(APN->getType(), DII)) { |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " |
| 1511 | << *DII << '\n'); |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | BasicBlock *BB = APN->getParent(); |
| 1516 | auto InsertionPt = BB->getFirstInsertionPt(); |
| 1517 | |
| 1518 | DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | if (InsertionPt != BB->end()) |
| 1524 | Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, NewLoc, &*InsertionPt); |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | static bool isArray(AllocaInst *AI) { |
| 1529 | return AI->isArrayAllocation() || |
| 1530 | (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy()); |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | static bool isStructure(AllocaInst *AI) { |
| 1535 | return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy(); |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | bool llvm::LowerDbgDeclare(Function &F) { |
| 1541 | bool Changed = false; |
| 1542 | DIBuilder DIB(*F.getParent(), false); |
| 1543 | SmallVector<DbgDeclareInst *, 4> Dbgs; |
| 1544 | for (auto &FI : F) |
| 1545 | for (Instruction &BI : FI) |
| 1546 | if (auto DDI = dyn_cast<DbgDeclareInst>(&BI)) |
| 1547 | Dbgs.push_back(DDI); |
| 1548 | |
| 1549 | if (Dbgs.empty()) |
| 1550 | return Changed; |
| 1551 | |
| 1552 | for (auto &I : Dbgs) { |
| 1553 | DbgDeclareInst *DDI = I; |
| 1554 | AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | if (!AI || isArray(AI) || isStructure(AI)) |
| 1562 | continue; |
| 1563 | |
| 1564 | |
| 1565 | if (llvm::any_of(AI->users(), [](User *U) -> bool { |
| 1566 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) |
| 1567 | return LI->isVolatile(); |
| 1568 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) |
| 1569 | return SI->isVolatile(); |
| 1570 | return false; |
| 1571 | })) |
| 1572 | continue; |
| 1573 | |
| 1574 | SmallVector<const Value *, 8> WorkList; |
| 1575 | WorkList.push_back(AI); |
| 1576 | while (!WorkList.empty()) { |
| 1577 | const Value *V = WorkList.pop_back_val(); |
| 1578 | for (auto &AIUse : V->uses()) { |
| 1579 | User *U = AIUse.getUser(); |
| 1580 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 1581 | if (AIUse.getOperandNo() == 1) |
| 1582 | ConvertDebugDeclareToDebugValue(DDI, SI, DIB); |
| 1583 | } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
| 1584 | ConvertDebugDeclareToDebugValue(DDI, LI, DIB); |
| 1585 | } else if (CallInst *CI = dyn_cast<CallInst>(U)) { |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | if (!CI->isLifetimeStartOrEnd()) { |
| 1590 | DebugLoc NewLoc = getDebugValueLoc(DDI, nullptr); |
| 1591 | auto *DerefExpr = |
| 1592 | DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref); |
| 1593 | DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr, |
| 1594 | NewLoc, CI); |
| 1595 | } |
| 1596 | } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) { |
| 1597 | if (BI->getType()->isPointerTy()) |
| 1598 | WorkList.push_back(BI); |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | DDI->eraseFromParent(); |
| 1603 | Changed = true; |
| 1604 | } |
| 1605 | |
| 1606 | if (Changed) |
| 1607 | for (BasicBlock &BB : F) |
| 1608 | RemoveRedundantDbgInstrs(&BB); |
| 1609 | |
| 1610 | return Changed; |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | void llvm::insertDebugValuesForPHIs(BasicBlock *BB, |
| 1615 | SmallVectorImpl<PHINode *> &InsertedPHIs) { |
| 1616 | assert(BB && "No BasicBlock to clone dbg.value(s) from."); |
| 1617 | if (InsertedPHIs.size() == 0) |
| 1618 | return; |
| 1619 | |
| 1620 | |
| 1621 | ValueToValueMapTy DbgValueMap; |
| 1622 | for (auto &I : *BB) { |
| 1623 | if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) { |
| 1624 | for (Value *V : DbgII->location_ops()) |
| 1625 | if (auto *Loc = dyn_cast_or_null<PHINode>(V)) |
| 1626 | DbgValueMap.insert({Loc, DbgII}); |
| 1627 | } |
| 1628 | } |
| 1629 | if (DbgValueMap.size() == 0) |
| 1630 | return; |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | MapVector<std::pair<BasicBlock *, DbgVariableIntrinsic *>, |
| 1637 | DbgVariableIntrinsic *> |
| 1638 | NewDbgValueMap; |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | for (auto PHI : InsertedPHIs) { |
| 1645 | BasicBlock *Parent = PHI->getParent(); |
| 1646 | |
| 1647 | if (Parent->getFirstNonPHI()->isEHPad()) |
| 1648 | continue; |
| 1649 | for (auto VI : PHI->operand_values()) { |
| 1650 | auto V = DbgValueMap.find(VI); |
| 1651 | if (V != DbgValueMap.end()) { |
| 1652 | auto *DbgII = cast<DbgVariableIntrinsic>(V->second); |
| 1653 | auto NewDI = NewDbgValueMap.find({Parent, DbgII}); |
| 1654 | if (NewDI == NewDbgValueMap.end()) { |
| 1655 | auto *NewDbgII = cast<DbgVariableIntrinsic>(DbgII->clone()); |
| 1656 | NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first; |
| 1657 | } |
| 1658 | DbgVariableIntrinsic *NewDbgII = NewDI->second; |
| 1659 | |
| 1660 | |
| 1661 | if (is_contained(NewDbgII->location_ops(), VI)) |
| 1662 | NewDbgII->replaceVariableLocationOp(VI, PHI); |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | for (auto DI : NewDbgValueMap) { |
| 1668 | BasicBlock *Parent = DI.first.first; |
| 1669 | auto *NewDbgII = DI.second; |
| 1670 | auto InsertionPt = Parent->getFirstInsertionPt(); |
| 1671 | assert(InsertionPt != Parent->end() && "Ill-formed basic block"); |
| 1672 | NewDbgII->insertBefore(&*InsertionPt); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, |
| 1677 | DIBuilder &Builder, uint8_t DIExprFlags, |
| 1678 | int Offset) { |
| 1679 | auto DbgAddrs = FindDbgAddrUses(Address); |
| 1680 | for (DbgVariableIntrinsic *DII : DbgAddrs) { |
| 1681 | const DebugLoc &Loc = DII->getDebugLoc(); |
| 1682 | auto *DIVar = DII->getVariable(); |
| 1683 | auto *DIExpr = DII->getExpression(); |
| 1684 | assert(DIVar && "Missing variable"); |
| 1685 | DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset); |
| 1686 | |
| 1687 | |
| 1688 | Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, DII); |
| 1689 | DII->eraseFromParent(); |
| 1690 | } |
| 1691 | return !DbgAddrs.empty(); |
| 1692 | } |
| 1693 | |
| 1694 | static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress, |
| 1695 | DIBuilder &Builder, int Offset) { |
| 1696 | const DebugLoc &Loc = DVI->getDebugLoc(); |
| 1697 | auto *DIVar = DVI->getVariable(); |
| 1698 | auto *DIExpr = DVI->getExpression(); |
| 1699 | assert(DIVar && "Missing variable"); |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | if (!DIExpr || DIExpr->getNumElements() < 1 || |
| 1705 | DIExpr->getElement(0) != dwarf::DW_OP_deref) |
| 1706 | return; |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | if (Offset) |
| 1711 | DIExpr = DIExpression::prepend(DIExpr, 0, Offset); |
| 1712 | |
| 1713 | Builder.insertDbgValueIntrinsic(NewAddress, DIVar, DIExpr, Loc, DVI); |
| 1714 | DVI->eraseFromParent(); |
| 1715 | } |
| 1716 | |
| 1717 | void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress, |
| 1718 | DIBuilder &Builder, int Offset) { |
| 1719 | if (auto *L = LocalAsMetadata::getIfExists(AI)) |
| 1720 | if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L)) |
| 1721 | for (Use &U : llvm::make_early_inc_range(MDV->uses())) |
| 1722 | if (auto *DVI = dyn_cast<DbgValueInst>(U.getUser())) |
| 1723 | replaceOneDbgValueForAlloca(DVI, NewAllocaAddress, Builder, Offset); |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | void llvm::salvageDebugInfo(Instruction &I) { |
| 1729 | SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; |
| 1730 | findDbgUsers(DbgUsers, &I); |
| 1731 | salvageDebugInfoForDbgValues(I, DbgUsers); |
| 1732 | } |
| 1733 | |
| 1734 | void llvm::salvageDebugInfoForDbgValues( |
| 1735 | Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers) { |
| 1736 | |
| 1737 | |
| 1738 | const unsigned MaxDebugArgs = 16; |
| 1739 | bool Salvaged = false; |
| 1740 | |
| 1741 | for (auto *DII : DbgUsers) { |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | bool StackValue = isa<DbgValueInst>(DII); |
| 1746 | auto DIILocation = DII->location_ops(); |
| 1747 | assert( |
| 1748 | is_contained(DIILocation, &I) && |
| 1749 | "DbgVariableIntrinsic must use salvaged instruction as its location"); |
| 1750 | SmallVector<Value *, 4> AdditionalValues; |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | DIExpression *SalvagedExpr = DII->getExpression(); |
| 1756 | auto LocItr = find(DIILocation, &I); |
| 1757 | while (SalvagedExpr && LocItr != DIILocation.end()) { |
| 1758 | unsigned LocNo = std::distance(DIILocation.begin(), LocItr); |
| 1759 | SalvagedExpr = salvageDebugInfoImpl(I, SalvagedExpr, StackValue, LocNo, |
| 1760 | AdditionalValues); |
| 1761 | LocItr = std::find(++LocItr, DIILocation.end(), &I); |
| 1762 | } |
| 1763 | |
| 1764 | |
| 1765 | if (!SalvagedExpr) |
| 1766 | break; |
| 1767 | |
| 1768 | DII->replaceVariableLocationOp(&I, I.getOperand(0)); |
| 1769 | if (AdditionalValues.empty()) { |
| 1770 | DII->setExpression(SalvagedExpr); |
| 1771 | } else if (isa<DbgValueInst>(DII) && |
| 1772 | DII->getNumVariableLocationOps() + AdditionalValues.size() <= |
| 1773 | MaxDebugArgs) { |
| 1774 | DII->addVariableLocationOps(AdditionalValues, SalvagedExpr); |
| 1775 | } else { |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | Value *Undef = UndefValue::get(I.getOperand(0)->getType()); |
| 1781 | DII->replaceVariableLocationOp(I.getOperand(0), Undef); |
| 1782 | } |
| 1783 | LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); |
| 1784 | Salvaged = true; |
| 1785 | } |
| 1786 | |
| 1787 | if (Salvaged) |
| 1788 | return; |
| 1789 | |
| 1790 | for (auto *DII : DbgUsers) { |
| 1791 | Value *Undef = UndefValue::get(I.getType()); |
| 1792 | DII->replaceVariableLocationOp(&I, Undef); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | bool getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL, |
| 1797 | uint64_t CurrentLocOps, |
| 1798 | SmallVectorImpl<uint64_t> &Opcodes, |
| 1799 | SmallVectorImpl<Value *> &AdditionalValues) { |
| 1800 | unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace()); |
| 1801 | |
| 1802 | MapVector<Value *, APInt> VariableOffsets; |
| 1803 | APInt ConstantOffset(BitWidth, 0); |
| 1804 | if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) |
| 1805 | return false; |
| 1806 | if (!VariableOffsets.empty() && !CurrentLocOps) { |
| 1807 | Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0}); |
| 1808 | CurrentLocOps = 1; |
| 1809 | } |
| 1810 | for (auto Offset : VariableOffsets) { |
| 1811 | AdditionalValues.push_back(Offset.first); |
| 1812 | assert(Offset.second.isStrictlyPositive() && |
| 1813 | "Expected strictly positive multiplier for offset."); |
| 1814 | Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu, |
| 1815 | Offset.second.getZExtValue(), dwarf::DW_OP_mul, |
| 1816 | dwarf::DW_OP_plus}); |
| 1817 | } |
| 1818 | DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue()); |
| 1819 | return true; |
| 1820 | } |
| 1821 | |
| 1822 | uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) { |
| 1823 | switch (Opcode) { |
| 1824 | case Instruction::Add: |
| 1825 | return dwarf::DW_OP_plus; |
| 1826 | case Instruction::Sub: |
| 1827 | return dwarf::DW_OP_minus; |
| 1828 | case Instruction::Mul: |
| 1829 | return dwarf::DW_OP_mul; |
| 1830 | case Instruction::SDiv: |
| 1831 | return dwarf::DW_OP_div; |
| 1832 | case Instruction::SRem: |
| 1833 | return dwarf::DW_OP_mod; |
| 1834 | case Instruction::Or: |
| 1835 | return dwarf::DW_OP_or; |
| 1836 | case Instruction::And: |
| 1837 | return dwarf::DW_OP_and; |
| 1838 | case Instruction::Xor: |
| 1839 | return dwarf::DW_OP_xor; |
| 1840 | case Instruction::Shl: |
| 1841 | return dwarf::DW_OP_shl; |
| 1842 | case Instruction::LShr: |
| 1843 | return dwarf::DW_OP_shr; |
| 1844 | case Instruction::AShr: |
| 1845 | return dwarf::DW_OP_shra; |
| 1846 | default: |
| 1847 | |
| 1848 | return 0; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | bool getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps, |
| 1853 | SmallVectorImpl<uint64_t> &Opcodes, |
| 1854 | SmallVectorImpl<Value *> &AdditionalValues) { |
| 1855 | |
| 1856 | auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1)); |
| 1857 | |
| 1858 | if (ConstInt && ConstInt->getBitWidth() > 64) |
| 1859 | return false; |
| 1860 | |
| 1861 | Instruction::BinaryOps BinOpcode = BI->getOpcode(); |
| 1862 | |
| 1863 | if (ConstInt) { |
| 1864 | uint64_t Val = ConstInt->getSExtValue(); |
| 1865 | |
| 1866 | |
| 1867 | if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) { |
| 1868 | uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val); |
| 1869 | DIExpression::appendOffset(Opcodes, Offset); |
| 1870 | return true; |
| 1871 | } |
| 1872 | Opcodes.append({dwarf::DW_OP_constu, Val}); |
| 1873 | } else { |
| 1874 | if (!CurrentLocOps) { |
| 1875 | Opcodes.append({dwarf::DW_OP_LLVM_arg, 0}); |
| 1876 | CurrentLocOps = 1; |
| 1877 | } |
| 1878 | Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps}); |
| 1879 | AdditionalValues.push_back(BI->getOperand(1)); |
| 1880 | } |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode); |
| 1885 | if (!DwarfBinOp) |
| 1886 | return false; |
| 1887 | Opcodes.push_back(DwarfBinOp); |
| 1888 | |
| 1889 | return true; |
| 1890 | } |
| 1891 | |
| 1892 | DIExpression * |
| 1893 | llvm::salvageDebugInfoImpl(Instruction &I, DIExpression *SrcDIExpr, |
| 1894 | bool WithStackValue, unsigned LocNo, |
| 1895 | SmallVectorImpl<Value *> &AdditionalValues) { |
| 1896 | uint64_t CurrentLocOps = SrcDIExpr->getNumLocationOperands(); |
| 1897 | auto &M = *I.getModule(); |
| 1898 | auto &DL = M.getDataLayout(); |
| 1899 | |
| 1900 | |
| 1901 | auto doSalvage = [&](SmallVectorImpl<uint64_t> &Ops) -> DIExpression * { |
| 1902 | DIExpression *DIExpr = SrcDIExpr; |
| 1903 | if (!Ops.empty()) { |
| 1904 | DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, LocNo, WithStackValue); |
| 1905 | } |
| 1906 | return DIExpr; |
| 1907 | }; |
| 1908 | |
| 1909 | |
| 1910 | auto applyOps = [&](ArrayRef<uint64_t> Opcodes) { |
| 1911 | SmallVector<uint64_t, 8> Ops(Opcodes.begin(), Opcodes.end()); |
| 1912 | return doSalvage(Ops); |
| 1913 | }; |
| 1914 | |
| 1915 | if (auto *CI = dyn_cast<CastInst>(&I)) { |
| 1916 | |
| 1917 | if (CI->isNoopCast(DL)) |
| 1918 | return SrcDIExpr; |
| 1919 | |
| 1920 | Type *Type = CI->getType(); |
| 1921 | |
| 1922 | if (Type->isVectorTy() || |
| 1923 | !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I))) |
| 1924 | return nullptr; |
| 1925 | |
| 1926 | Value *FromValue = CI->getOperand(0); |
| 1927 | unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits(); |
| 1928 | unsigned ToTypeBitSize = Type->getScalarSizeInBits(); |
| 1929 | |
| 1930 | return applyOps(DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize, |
| 1931 | isa<SExtInst>(&I))); |
| 1932 | } |
| 1933 | |
| 1934 | SmallVector<uint64_t, 8> Ops; |
| 1935 | if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1936 | if (getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues)) |
| 1937 | return doSalvage(Ops); |
| 1938 | } else if (auto *BI = dyn_cast<BinaryOperator>(&I)) { |
| 1939 | if (getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues)) |
| 1940 | return doSalvage(Ops); |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | return nullptr; |
| 1946 | } |
| 1947 | |
| 1948 | |
| 1949 | using DbgValReplacement = Optional<DIExpression *>; |
| 1950 | |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | static bool rewriteDebugUsers( |
| 1955 | Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT, |
| 1956 | function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr) { |
| 1957 | |
| 1958 | SmallVector<DbgVariableIntrinsic *, 1> Users; |
| 1959 | findDbgUsers(Users, &From); |
| 1960 | if (Users.empty()) |
| 1961 | return false; |
| 1962 | |
| 1963 | |
| 1964 | bool Changed = false; |
| 1965 | SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage; |
| 1966 | if (isa<Instruction>(&To)) { |
| 1967 | bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint; |
| 1968 | |
| 1969 | for (auto *DII : Users) { |
| 1970 | |
| 1971 | |
| 1972 | if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) { |
| 1973 | LLVM_DEBUG(dbgs() << "MOVE: " << *DII << '\n'); |
| 1974 | DII->moveAfter(&DomPoint); |
| 1975 | Changed = true; |
| 1976 | |
| 1977 | |
| 1978 | |
| 1979 | } else if (!DT.dominates(&DomPoint, DII)) { |
| 1980 | UndefOrSalvage.insert(DII); |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | |
| 1986 | for (auto *DII : Users) { |
| 1987 | if (UndefOrSalvage.count(DII)) |
| 1988 | continue; |
| 1989 | |
| 1990 | DbgValReplacement DVR = RewriteExpr(*DII); |
| 1991 | if (!DVR) |
| 1992 | continue; |
| 1993 | |
| 1994 | DII->replaceVariableLocationOp(&From, &To); |
| 1995 | DII->setExpression(*DVR); |
| 1996 | LLVM_DEBUG(dbgs() << "REWRITE: " << *DII << '\n'); |
| 1997 | Changed = true; |
| 1998 | } |
| 1999 | |
| 2000 | if (!UndefOrSalvage.empty()) { |
| 2001 | |
| 2002 | salvageDebugInfo(From); |
| 2003 | Changed = true; |
| 2004 | } |
| 2005 | |
| 2006 | return Changed; |
| 2007 | } |
| 2008 | |
| 2009 | |
| 2010 | |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | |
| 2015 | |
| 2016 | static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy, |
| 2017 | Type *ToTy) { |
| 2018 | |
| 2019 | if (FromTy == ToTy) |
| 2020 | return true; |
| 2021 | |
| 2022 | |
| 2023 | if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) { |
| 2024 | bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy); |
| 2025 | bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) && |
| 2026 | !DL.isNonIntegralPointerType(ToTy); |
| 2027 | return SameSize && LosslessConversion; |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | return false; |
| 2032 | } |
| 2033 | |
| 2034 | bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To, |
| 2035 | Instruction &DomPoint, DominatorTree &DT) { |
| 2036 | |
| 2037 | if (!From.isUsedByMetadata()) |
| 2038 | return false; |
| 2039 | |
| 2040 | assert(&From != &To && "Can't replace something with itself"); |
| 2041 | |
| 2042 | Type *FromTy = From.getType(); |
| 2043 | Type *ToTy = To.getType(); |
| 2044 | |
| 2045 | auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { |
| 2046 | return DII.getExpression(); |
| 2047 | }; |
| 2048 | |
| 2049 | |
| 2050 | Module &M = *From.getModule(); |
| 2051 | const DataLayout &DL = M.getDataLayout(); |
| 2052 | if (isBitCastSemanticsPreserving(DL, FromTy, ToTy)) |
| 2053 | return rewriteDebugUsers(From, To, DomPoint, DT, Identity); |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) { |
| 2058 | uint64_t FromBits = FromTy->getPrimitiveSizeInBits(); |
| 2059 | uint64_t ToBits = ToTy->getPrimitiveSizeInBits(); |
| 2060 | assert(FromBits != ToBits && "Unexpected no-op conversion"); |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | if (FromBits < ToBits) |
| 2065 | return rewriteDebugUsers(From, To, DomPoint, DT, Identity); |
| 2066 | |
| 2067 | |
| 2068 | |
| 2069 | auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { |
| 2070 | DILocalVariable *Var = DII.getVariable(); |
| 2071 | |
| 2072 | |
| 2073 | auto Signedness = Var->getSignedness(); |
| 2074 | if (!Signedness) |
| 2075 | return None; |
| 2076 | |
| 2077 | bool Signed = *Signedness == DIBasicType::Signedness::Signed; |
| 2078 | return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits, |
| 2079 | Signed); |
| 2080 | }; |
| 2081 | return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt); |
| 2082 | } |
| 2083 | |
| 2084 | |
| 2085 | return false; |
| 2086 | } |
| 2087 | |
| 2088 | std::pair<unsigned, unsigned> |
| 2089 | llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) { |
| 2090 | unsigned NumDeadInst = 0; |
| 2091 | unsigned NumDeadDbgInst = 0; |
| 2092 | |
| 2093 | |
| 2094 | Instruction *EndInst = BB->getTerminator(); |
| 2095 | while (EndInst != &BB->front()) { |
| 2096 | |
| 2097 | Instruction *Inst = &*--EndInst->getIterator(); |
| 2098 | if (!Inst->use_empty() && !Inst->getType()->isTokenTy()) |
| 2099 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
| 2100 | if (Inst->isEHPad() || Inst->getType()->isTokenTy()) { |
| 2101 | EndInst = Inst; |
| 2102 | continue; |
| 2103 | } |
| 2104 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 2105 | ++NumDeadDbgInst; |
| 2106 | else |
| 2107 | ++NumDeadInst; |
| 2108 | Inst->eraseFromParent(); |
| 2109 | } |
| 2110 | return {NumDeadInst, NumDeadDbgInst}; |
| 2111 | } |
| 2112 | |
| 2113 | unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA, |
| 2114 | DomTreeUpdater *DTU, |
| 2115 | MemorySSAUpdater *MSSAU) { |
| 2116 | BasicBlock *BB = I->getParent(); |
| 2117 | |
| 2118 | if (MSSAU) |
| 2119 | MSSAU->changeToUnreachable(I); |
| 2120 | |
| 2121 | SmallSet<BasicBlock *, 8> UniqueSuccessors; |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | for (BasicBlock *Successor : successors(BB)) { |
| 2126 | Successor->removePredecessor(BB, PreserveLCSSA); |
| 2127 | if (DTU) |
| 2128 | UniqueSuccessors.insert(Successor); |
| 2129 | } |
| 2130 | auto *UI = new UnreachableInst(I->getContext(), I); |
| 2131 | UI->setDebugLoc(I->getDebugLoc()); |
| 2132 | |
| 2133 | |
| 2134 | unsigned NumInstrsRemoved = 0; |
| 2135 | BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end(); |
| 2136 | while (BBI != BBE) { |
| 2137 | if (!BBI->use_empty()) |
| 2138 | BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); |
| 2139 | BB->getInstList().erase(BBI++); |
| 2140 | ++NumInstrsRemoved; |
| 2141 | } |
| 2142 | if (DTU) { |
| 2143 | SmallVector<DominatorTree::UpdateType, 8> Updates; |
| 2144 | Updates.reserve(UniqueSuccessors.size()); |
| 2145 | for (BasicBlock *UniqueSuccessor : UniqueSuccessors) |
| 2146 | Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor}); |
| 2147 | DTU->applyUpdates(Updates); |
| 2148 | } |
| 2149 | return NumInstrsRemoved; |
| 2150 | } |
| 2151 | |
| 2152 | CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) { |
| 2153 | SmallVector<Value *, 8> Args(II->args()); |
| 2154 | SmallVector<OperandBundleDef, 1> OpBundles; |
| 2155 | II->getOperandBundlesAsDefs(OpBundles); |
| 2156 | CallInst *NewCall = CallInst::Create(II->getFunctionType(), |
| 2157 | II->getCalledOperand(), Args, OpBundles); |
| 2158 | NewCall->setCallingConv(II->getCallingConv()); |
| 2159 | NewCall->setAttributes(II->getAttributes()); |
| 2160 | NewCall->setDebugLoc(II->getDebugLoc()); |
| 2161 | NewCall->copyMetadata(*II); |
| 2162 | |
| 2163 | |
| 2164 | uint64_t TotalWeight; |
| 2165 | if (NewCall->extractProfTotalWeight(TotalWeight)) { |
| 2166 | |
| 2167 | MDBuilder MDB(NewCall->getContext()); |
| 2168 | auto NewWeights = uint32_t(TotalWeight) != TotalWeight |
| 2169 | ? nullptr |
| 2170 | : MDB.createBranchWeights({uint32_t(TotalWeight)}); |
| 2171 | NewCall->setMetadata(LLVMContext::MD_prof, NewWeights); |
| 2172 | } |
| 2173 | |
| 2174 | return NewCall; |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | void llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) { |
| 2179 | CallInst *NewCall = createCallMatchingInvoke(II); |
| 2180 | NewCall->takeName(II); |
| 2181 | NewCall->insertBefore(II); |
| 2182 | II->replaceAllUsesWith(NewCall); |
| 2183 | |
| 2184 | |
| 2185 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 2186 | BranchInst::Create(NormalDestBB, II); |
| 2187 | |
| 2188 | |
| 2189 | BasicBlock *BB = II->getParent(); |
| 2190 | BasicBlock *UnwindDestBB = II->getUnwindDest(); |
| 2191 | UnwindDestBB->removePredecessor(BB); |
| 2192 | II->eraseFromParent(); |
| 2193 | if (DTU) |
| 2194 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); |
| 2195 | } |
| 2196 | |
| 2197 | BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI, |
| 2198 | BasicBlock *UnwindEdge, |
| 2199 | DomTreeUpdater *DTU) { |
| 2200 | BasicBlock *BB = CI->getParent(); |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | BasicBlock *Split = SplitBlock(BB, CI, DTU, nullptr, nullptr, |
| 2205 | CI->getName() + ".noexc"); |
| 2206 | |
| 2207 | |
| 2208 | BB->getInstList().pop_back(); |
| 2209 | |
| 2210 | |
| 2211 | SmallVector<Value *, 8> InvokeArgs(CI->args()); |
| 2212 | SmallVector<OperandBundleDef, 1> OpBundles; |
| 2213 | |
| 2214 | CI->getOperandBundlesAsDefs(OpBundles); |
| 2215 | |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | InvokeInst *II = |
| 2221 | InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split, |
| 2222 | UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB); |
| 2223 | II->setDebugLoc(CI->getDebugLoc()); |
| 2224 | II->setCallingConv(CI->getCallingConv()); |
| 2225 | II->setAttributes(CI->getAttributes()); |
| 2226 | |
| 2227 | if (DTU) |
| 2228 | DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}}); |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | CI->replaceAllUsesWith(II); |
| 2233 | |
| 2234 | |
| 2235 | Split->getInstList().pop_front(); |
| 2236 | return Split; |
| 2237 | } |
| 2238 | |
| 2239 | static bool markAliveBlocks(Function &F, |
| 2240 | SmallPtrSetImpl<BasicBlock *> &Reachable, |
| 2241 | DomTreeUpdater *DTU = nullptr) { |
| 2242 | SmallVector<BasicBlock*, 128> Worklist; |
| 2243 | BasicBlock *BB = &F.front(); |
| 2244 | Worklist.push_back(BB); |
| 2245 | Reachable.insert(BB); |
| 2246 | bool Changed = false; |
| 2247 | do { |
| 2248 | BB = Worklist.pop_back_val(); |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | for (Instruction &I : *BB) { |
| 2254 | if (auto *CI = dyn_cast<CallInst>(&I)) { |
| 2255 | Value *Callee = CI->getCalledOperand(); |
| 2256 | |
| 2257 | if (Function *F = dyn_cast<Function>(Callee)) { |
| 2258 | auto IntrinsicID = F->getIntrinsicID(); |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | if (IntrinsicID == Intrinsic::assume) { |
| 2264 | if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) { |
| 2265 | |
| 2266 | changeToUnreachable(CI, false, DTU); |
| 2267 | Changed = true; |
| 2268 | break; |
| 2269 | } |
| 2270 | } else if (IntrinsicID == Intrinsic::experimental_guard) { |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | |
| 2277 | |
| 2278 | |
| 2279 | |
| 2280 | if (match(CI->getArgOperand(0), m_Zero())) |
| 2281 | if (!isa<UnreachableInst>(CI->getNextNode())) { |
| 2282 | changeToUnreachable(CI->getNextNode(), false, DTU); |
| 2283 | Changed = true; |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | } else if ((isa<ConstantPointerNull>(Callee) && |
| 2288 | !NullPointerIsDefined(CI->getFunction())) || |
| 2289 | isa<UndefValue>(Callee)) { |
| 2290 | changeToUnreachable(CI, false, DTU); |
| 2291 | Changed = true; |
| 2292 | break; |
| 2293 | } |
| 2294 | if (CI->doesNotReturn() && !CI->isMustTailCall()) { |
| 2295 | |
| 2296 | |
| 2297 | |
| 2298 | if (!isa<UnreachableInst>(CI->getNextNode())) { |
| 2299 | |
| 2300 | changeToUnreachable(CI->getNextNode(), false, DTU); |
| 2301 | Changed = true; |
| 2302 | } |
| 2303 | break; |
| 2304 | } |
| 2305 | } else if (auto *SI = dyn_cast<StoreInst>(&I)) { |
| 2306 | |
| 2307 | |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | if (SI->isVolatile()) continue; |
| 2312 | |
| 2313 | Value *Ptr = SI->getOperand(1); |
| 2314 | |
| 2315 | if (isa<UndefValue>(Ptr) || |
| 2316 | (isa<ConstantPointerNull>(Ptr) && |
| 2317 | !NullPointerIsDefined(SI->getFunction(), |
| 2318 | SI->getPointerAddressSpace()))) { |
| 2319 | changeToUnreachable(SI, false, DTU); |
| 2320 | Changed = true; |
| 2321 | break; |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | Instruction *Terminator = BB->getTerminator(); |
| 2327 | if (auto *II = dyn_cast<InvokeInst>(Terminator)) { |
| 2328 | |
| 2329 | Value *Callee = II->getCalledOperand(); |
| 2330 | if ((isa<ConstantPointerNull>(Callee) && |
| 2331 | !NullPointerIsDefined(BB->getParent())) || |
| 2332 | isa<UndefValue>(Callee)) { |
| 2333 | changeToUnreachable(II, false, DTU); |
| 2334 | Changed = true; |
| 2335 | } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) { |
| 2336 | if (II->use_empty() && II->onlyReadsMemory()) { |
| 2337 | |
| 2338 | BasicBlock *NormalDestBB = II->getNormalDest(); |
| 2339 | BasicBlock *UnwindDestBB = II->getUnwindDest(); |
| 2340 | BranchInst::Create(NormalDestBB, II); |
| 2341 | UnwindDestBB->removePredecessor(II->getParent()); |
| 2342 | II->eraseFromParent(); |
| 2343 | if (DTU) |
| 2344 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); |
| 2345 | } else |
| 2346 | changeToCall(II, DTU); |
| 2347 | Changed = true; |
| 2348 | } |
| 2349 | } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) { |
| 2350 | |
| 2351 | struct CatchPadDenseMapInfo { |
| 2352 | static CatchPadInst *getEmptyKey() { |
| 2353 | return DenseMapInfo<CatchPadInst *>::getEmptyKey(); |
| 2354 | } |
| 2355 | |
| 2356 | static CatchPadInst *getTombstoneKey() { |
| 2357 | return DenseMapInfo<CatchPadInst *>::getTombstoneKey(); |
| 2358 | } |
| 2359 | |
| 2360 | static unsigned getHashValue(CatchPadInst *CatchPad) { |
| 2361 | return static_cast<unsigned>(hash_combine_range( |
| 2362 | CatchPad->value_op_begin(), CatchPad->value_op_end())); |
| 2363 | } |
| 2364 | |
| 2365 | static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) { |
| 2366 | if (LHS == getEmptyKey() || LHS == getTombstoneKey() || |
| 2367 | RHS == getEmptyKey() || RHS == getTombstoneKey()) |
| 2368 | return LHS == RHS; |
| 2369 | return LHS->isIdenticalTo(RHS); |
| 2370 | } |
| 2371 | }; |
| 2372 | |
| 2373 | SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases; |
| 2374 | |
| 2375 | SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4, |
| 2376 | CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>> |
| 2377 | HandlerSet; |
| 2378 | detail::DenseSetEmpty Empty; |
| 2379 | for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(), |
| 2380 | E = CatchSwitch->handler_end(); |
| 2381 | I != E; ++I) { |
| 2382 | BasicBlock *HandlerBB = *I; |
| 2383 | if (DTU) |
| 2384 | ++NumPerSuccessorCases[HandlerBB]; |
| 2385 | auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI()); |
| 2386 | if (!HandlerSet.insert({CatchPad, Empty}).second) { |
| 2387 | if (DTU) |
| 2388 | --NumPerSuccessorCases[HandlerBB]; |
| 2389 | CatchSwitch->removeHandler(I); |
| 2390 | --I; |
| 2391 | --E; |
| 2392 | Changed = true; |
| 2393 | } |
| 2394 | } |
| 2395 | if (DTU) { |
| 2396 | std::vector<DominatorTree::UpdateType> Updates; |
| 2397 | for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases) |
| 2398 | if (I.second == 0) |
| 2399 | Updates.push_back({DominatorTree::Delete, BB, I.first}); |
| 2400 | DTU->applyUpdates(Updates); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU); |
| 2405 | for (BasicBlock *Successor : successors(BB)) |
| 2406 | if (Reachable.insert(Successor).second) |
| 2407 | Worklist.push_back(Successor); |
| 2408 | } while (!Worklist.empty()); |
| 2409 | return Changed; |
| 2410 | } |
| 2411 | |
| 2412 | void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) { |
| 2413 | Instruction *TI = BB->getTerminator(); |
| 2414 | |
| 2415 | if (auto *II = dyn_cast<InvokeInst>(TI)) { |
| 2416 | changeToCall(II, DTU); |
| 2417 | return; |
| 2418 | } |
| 2419 | |
| 2420 | Instruction *NewTI; |
| 2421 | BasicBlock *UnwindDest; |
| 2422 | |
| 2423 | if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) { |
| 2424 | NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI); |
| 2425 | UnwindDest = CRI->getUnwindDest(); |
| 2426 | } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { |
| 2427 | auto *NewCatchSwitch = CatchSwitchInst::Create( |
| 2428 | CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(), |
| 2429 | CatchSwitch->getName(), CatchSwitch); |
| 2430 | for (BasicBlock *PadBB : CatchSwitch->handlers()) |
| 2431 | NewCatchSwitch->addHandler(PadBB); |
| 2432 | |
| 2433 | NewTI = NewCatchSwitch; |
| 2434 | UnwindDest = CatchSwitch->getUnwindDest(); |
| 2435 | } else { |
| 2436 | llvm_unreachable("Could not find unwind successor"); |
| 2437 | } |
| 2438 | |
| 2439 | NewTI->takeName(TI); |
| 2440 | NewTI->setDebugLoc(TI->getDebugLoc()); |
| 2441 | UnwindDest->removePredecessor(BB); |
| 2442 | TI->replaceAllUsesWith(NewTI); |
| 2443 | TI->eraseFromParent(); |
| 2444 | if (DTU) |
| 2445 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}}); |
| 2446 | } |
| 2447 | |
| 2448 | |
| 2449 | |
| 2450 | |
| 2451 | bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU, |
| 2452 | MemorySSAUpdater *MSSAU) { |
| 2453 | SmallPtrSet<BasicBlock *, 16> Reachable; |
| 2454 | bool Changed = markAliveBlocks(F, Reachable, DTU); |
| 2455 | |
| 2456 | |
| 2457 | if (Reachable.size() == F.size()) |
| 2458 | return Changed; |
| 2459 | |
| 2460 | assert(Reachable.size() < F.size()); |
| 2461 | |
| 2462 | |
| 2463 | SmallSetVector<BasicBlock *, 8> BlocksToRemove; |
| 2464 | for (BasicBlock &BB : F) { |
| 2465 | |
| 2466 | if (Reachable.count(&BB)) |
| 2467 | continue; |
| 2468 | |
| 2469 | if (DTU && DTU->isBBPendingDeletion(&BB)) |
| 2470 | continue; |
| 2471 | BlocksToRemove.insert(&BB); |
| 2472 | } |
| 2473 | |
| 2474 | if (BlocksToRemove.empty()) |
| 2475 | return Changed; |
| 2476 | |
| 2477 | Changed = true; |
| 2478 | NumRemoved += BlocksToRemove.size(); |
| 2479 | |
| 2480 | if (MSSAU) |
| 2481 | MSSAU->removeBlocks(BlocksToRemove); |
| 2482 | |
| 2483 | DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU); |
| 2484 | |
| 2485 | return Changed; |
| 2486 | } |
| 2487 | |
| 2488 | void llvm::combineMetadata(Instruction *K, const Instruction *J, |
| 2489 | ArrayRef<unsigned> KnownIDs, bool DoesKMove) { |
| 2490 | SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; |
| 2491 | K->dropUnknownNonDebugMetadata(KnownIDs); |
| 2492 | K->getAllMetadataOtherThanDebugLoc(Metadata); |
| 2493 | for (const auto &MD : Metadata) { |
| 2494 | unsigned Kind = MD.first; |
| 2495 | MDNode *JMD = J->getMetadata(Kind); |
| 2496 | MDNode *KMD = MD.second; |
| 2497 | |
| 2498 | switch (Kind) { |
| 2499 | default: |
| 2500 | K->setMetadata(Kind, nullptr); |
| 2501 | break; |
| 2502 | case LLVMContext::MD_dbg: |
| 2503 | llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg"); |
| 2504 | case LLVMContext::MD_tbaa: |
| 2505 | K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD)); |
| 2506 | break; |
| 2507 | case LLVMContext::MD_alias_scope: |
| 2508 | K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD)); |
| 2509 | break; |
| 2510 | case LLVMContext::MD_noalias: |
| 2511 | case LLVMContext::MD_mem_parallel_loop_access: |
| 2512 | K->setMetadata(Kind, MDNode::intersect(JMD, KMD)); |
| 2513 | break; |
| 2514 | case LLVMContext::MD_access_group: |
| 2515 | K->setMetadata(LLVMContext::MD_access_group, |
| 2516 | intersectAccessGroups(K, J)); |
| 2517 | break; |
| 2518 | case LLVMContext::MD_range: |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | if (DoesKMove) |
| 2523 | |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD)); |
| 2528 | break; |
| 2529 | case LLVMContext::MD_fpmath: |
| 2530 | K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); |
| 2531 | break; |
| 2532 | case LLVMContext::MD_invariant_load: |
| 2533 | |
| 2534 | K->setMetadata(Kind, JMD); |
| 2535 | break; |
| 2536 | case LLVMContext::MD_nonnull: |
| 2537 | |
| 2538 | if (DoesKMove) |
| 2539 | K->setMetadata(Kind, JMD); |
| 2540 | break; |
| 2541 | case LLVMContext::MD_invariant_group: |
| 2542 | |
| 2543 | break; |
| 2544 | case LLVMContext::MD_align: |
| 2545 | K->setMetadata(Kind, |
| 2546 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); |
| 2547 | break; |
| 2548 | case LLVMContext::MD_dereferenceable: |
| 2549 | case LLVMContext::MD_dereferenceable_or_null: |
| 2550 | K->setMetadata(Kind, |
| 2551 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); |
| 2552 | break; |
| 2553 | case LLVMContext::MD_preserve_access_index: |
| 2554 | |
| 2555 | break; |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | |
| 2563 | |
| 2564 | if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group)) |
| 2565 | if (isa<LoadInst>(K) || isa<StoreInst>(K)) |
| 2566 | K->setMetadata(LLVMContext::MD_invariant_group, JMD); |
| 2567 | } |
| 2568 | |
| 2569 | void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J, |
| 2570 | bool KDominatesJ) { |
| 2571 | unsigned KnownIDs[] = { |
| 2572 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 2573 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 2574 | LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull, |
| 2575 | LLVMContext::MD_invariant_group, LLVMContext::MD_align, |
| 2576 | LLVMContext::MD_dereferenceable, |
| 2577 | LLVMContext::MD_dereferenceable_or_null, |
| 2578 | LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; |
| 2579 | combineMetadata(K, J, KnownIDs, KDominatesJ); |
| 2580 | } |
| 2581 | |
| 2582 | void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) { |
| 2583 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
| 2584 | Source.getAllMetadata(MD); |
| 2585 | MDBuilder MDB(Dest.getContext()); |
| 2586 | Type *NewType = Dest.getType(); |
| 2587 | const DataLayout &DL = Source.getModule()->getDataLayout(); |
| 2588 | for (const auto &MDPair : MD) { |
| 2589 | unsigned ID = MDPair.first; |
| 2590 | MDNode *N = MDPair.second; |
| 2591 | |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | |
| 2596 | |
| 2597 | |
| 2598 | switch (ID) { |
| 2599 | case LLVMContext::MD_dbg: |
| 2600 | case LLVMContext::MD_tbaa: |
| 2601 | case LLVMContext::MD_prof: |
| 2602 | case LLVMContext::MD_fpmath: |
| 2603 | case LLVMContext::MD_tbaa_struct: |
| 2604 | case LLVMContext::MD_invariant_load: |
| 2605 | case LLVMContext::MD_alias_scope: |
| 2606 | case LLVMContext::MD_noalias: |
| 2607 | case LLVMContext::MD_nontemporal: |
| 2608 | case LLVMContext::MD_mem_parallel_loop_access: |
| 2609 | case LLVMContext::MD_access_group: |
| 2610 | |
| 2611 | Dest.setMetadata(ID, N); |
| 2612 | break; |
| 2613 | |
| 2614 | case LLVMContext::MD_nonnull: |
| 2615 | copyNonnullMetadata(Source, N, Dest); |
| 2616 | break; |
| 2617 | |
| 2618 | case LLVMContext::MD_align: |
| 2619 | case LLVMContext::MD_dereferenceable: |
| 2620 | case LLVMContext::MD_dereferenceable_or_null: |
| 2621 | |
| 2622 | if (NewType->isPointerTy()) |
| 2623 | Dest.setMetadata(ID, N); |
| 2624 | break; |
| 2625 | |
| 2626 | case LLVMContext::MD_range: |
| 2627 | copyRangeMetadata(DL, Source, N, Dest); |
| 2628 | break; |
| 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) { |
| 2634 | auto *ReplInst = dyn_cast<Instruction>(Repl); |
| 2635 | if (!ReplInst) |
| 2636 | return; |
| 2637 | |
| 2638 | |
| 2639 | |
| 2640 | |
| 2641 | |
| 2642 | |
| 2643 | |
| 2644 | if (!isa<LoadInst>(I)) |
| 2645 | ReplInst->andIRFlags(I); |
| 2646 | |
| 2647 | |
| 2648 | |
| 2649 | |
| 2650 | |
| 2651 | |
| 2652 | |
| 2653 | |
| 2654 | |
| 2655 | |
| 2656 | static const unsigned KnownIDs[] = { |
| 2657 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 2658 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 2659 | LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, |
| 2660 | LLVMContext::MD_invariant_group, LLVMContext::MD_nonnull, |
| 2661 | LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; |
| 2662 | combineMetadata(ReplInst, I, KnownIDs, false); |
| 2663 | } |
| 2664 | |
| 2665 | template <typename RootType, typename DominatesFn> |
| 2666 | static unsigned replaceDominatedUsesWith(Value *From, Value *To, |
| 2667 | const RootType &Root, |
| 2668 | const DominatesFn &Dominates) { |
| 2669 | assert(From->getType() == To->getType()); |
| 2670 | |
| 2671 | unsigned Count = 0; |
| 2672 | for (Value::use_iterator UI = From->use_begin(), UE = From->use_end(); |
| 2673 | UI != UE;) { |
| 2674 | Use &U = *UI++; |
| 2675 | if (!Dominates(Root, U)) |
| 2676 | continue; |
| 2677 | U.set(To); |
| 2678 | LLVM_DEBUG(dbgs() << "Replace dominated use of '" << From->getName() |
| 2679 | << "' as " << *To << " in " << *U << "\n"); |
| 2680 | ++Count; |
| 2681 | } |
| 2682 | return Count; |
| 2683 | } |
| 2684 | |
| 2685 | unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) { |
| 2686 | assert(From->getType() == To->getType()); |
| 2687 | auto *BB = From->getParent(); |
| 2688 | unsigned Count = 0; |
| 2689 | |
| 2690 | for (Value::use_iterator UI = From->use_begin(), UE = From->use_end(); |
| 2691 | UI != UE;) { |
| 2692 | Use &U = *UI++; |
| 2693 | auto *I = cast<Instruction>(U.getUser()); |
| 2694 | if (I->getParent() == BB) |
| 2695 | continue; |
| 2696 | U.set(To); |
| 2697 | ++Count; |
| 2698 | } |
| 2699 | return Count; |
| 2700 | } |
| 2701 | |
| 2702 | unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, |
| 2703 | DominatorTree &DT, |
| 2704 | const BasicBlockEdge &Root) { |
| 2705 | auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) { |
| 2706 | return DT.dominates(Root, U); |
| 2707 | }; |
| 2708 | return ::replaceDominatedUsesWith(From, To, Root, Dominates); |
| 2709 | } |
| 2710 | |
| 2711 | unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, |
| 2712 | DominatorTree &DT, |
| 2713 | const BasicBlock *BB) { |
| 2714 | auto Dominates = [&DT](const BasicBlock *BB, const Use &U) { |
| 2715 | return DT.dominates(BB, U); |
| 2716 | }; |
| 2717 | return ::replaceDominatedUsesWith(From, To, BB, Dominates); |
| 2718 | } |
| 2719 | |
| 2720 | bool llvm::callsGCLeafFunction(const CallBase *Call, |
| 2721 | const TargetLibraryInfo &TLI) { |
| 2722 | |
| 2723 | if (Call->hasFnAttr("gc-leaf-function")) |
| 2724 | return true; |
| 2725 | if (const Function *F = Call->getCalledFunction()) { |
| 2726 | if (F->hasFnAttribute("gc-leaf-function")) |
| 2727 | return true; |
| 2728 | |
| 2729 | if (auto IID = F->getIntrinsicID()) { |
| 2730 | |
| 2731 | return IID != Intrinsic::experimental_gc_statepoint && |
| 2732 | IID != Intrinsic::experimental_deoptimize && |
| 2733 | IID != Intrinsic::memcpy_element_unordered_atomic && |
| 2734 | IID != Intrinsic::memmove_element_unordered_atomic; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | |
| 2740 | |
| 2741 | LibFunc LF; |
| 2742 | if (TLI.getLibFunc(*Call, LF)) { |
| 2743 | return TLI.has(LF); |
| 2744 | } |
| 2745 | |
| 2746 | return false; |
| 2747 | } |
| 2748 | |
| 2749 | void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, |
| 2750 | LoadInst &NewLI) { |
| 2751 | auto *NewTy = NewLI.getType(); |
| 2752 | |
| 2753 | |
| 2754 | if (NewTy->isPointerTy()) { |
| 2755 | NewLI.setMetadata(LLVMContext::MD_nonnull, N); |
| 2756 | return; |
| 2757 | } |
| 2758 | |
| 2759 | |
| 2760 | |
| 2761 | if (!NewTy->isIntegerTy()) |
| 2762 | return; |
| 2763 | |
| 2764 | MDBuilder MDB(NewLI.getContext()); |
| 2765 | const Value *Ptr = OldLI.getPointerOperand(); |
| 2766 | auto *ITy = cast<IntegerType>(NewTy); |
| 2767 | auto *NullInt = ConstantExpr::getPtrToInt( |
| 2768 | ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); |
| 2769 | auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); |
| 2770 | NewLI.setMetadata(LLVMContext::MD_range, |
| 2771 | MDB.createRange(NonNullInt, NullInt)); |
| 2772 | } |
| 2773 | |
| 2774 | void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, |
| 2775 | MDNode *N, LoadInst &NewLI) { |
| 2776 | auto *NewTy = NewLI.getType(); |
| 2777 | |
| 2778 | |
| 2779 | |
| 2780 | |
| 2781 | |
| 2782 | if (!NewTy->isPointerTy()) |
| 2783 | return; |
| 2784 | |
| 2785 | unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy); |
| 2786 | if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) { |
| 2787 | MDNode *NN = MDNode::get(OldLI.getContext(), None); |
| 2788 | NewLI.setMetadata(LLVMContext::MD_nonnull, NN); |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | void llvm::dropDebugUsers(Instruction &I) { |
| 2793 | SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; |
| 2794 | findDbgUsers(DbgUsers, &I); |
| 2795 | for (auto *DII : DbgUsers) |
| 2796 | DII->eraseFromParent(); |
| 2797 | } |
| 2798 | |
| 2799 | void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, |
| 2800 | BasicBlock *BB) { |
| 2801 | |
| 2802 | |
| 2803 | |
| 2804 | |
| 2805 | |
| 2806 | |
| 2807 | |
| 2808 | |
| 2809 | |
| 2810 | |
| 2811 | |
| 2812 | |
| 2813 | |
| 2814 | |
| 2815 | |
| 2816 | |
| 2817 | |
| 2818 | |
| 2819 | |
| 2820 | |
| 2821 | |
| 2822 | |
| 2823 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) { |
| 2824 | Instruction *I = &*II; |
| 2825 | I->dropUndefImplyingAttrsAndUnknownMetadata(); |
| 2826 | if (I->isUsedByMetadata()) |
| 2827 | dropDebugUsers(*I); |
| 2828 | if (I->isDebugOrPseudoInst()) { |
| 2829 | |
| 2830 | II = I->eraseFromParent(); |
| 2831 | continue; |
| 2832 | } |
| 2833 | I->setDebugLoc(InsertPt->getDebugLoc()); |
| 2834 | ++II; |
| 2835 | } |
| 2836 | DomBlock->getInstList().splice(InsertPt->getIterator(), BB->getInstList(), |
| 2837 | BB->begin(), |
| 2838 | BB->getTerminator()->getIterator()); |
| 2839 | } |
| 2840 | |
| 2841 | namespace { |
| 2842 | |
| 2843 | |
| 2844 | |
| 2845 | struct BitPart { |
| 2846 | BitPart(Value *P, unsigned BW) : Provider(P) { |
| 2847 | Provenance.resize(BW); |
| 2848 | } |
| 2849 | |
| 2850 | |
| 2851 | Value *Provider; |
| 2852 | |
| 2853 | |
| 2854 | |
| 2855 | SmallVector<int8_t, 32> Provenance; |
| 2856 | |
| 2857 | enum { Unset = -1 }; |
| 2858 | }; |
| 2859 | |
| 2860 | } |
| 2861 | |
| 2862 | |
| 2863 | |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | |
| 2868 | |
| 2869 | |
| 2870 | |
| 2871 | |
| 2872 | |
| 2873 | |
| 2874 | |
| 2875 | |
| 2876 | |
| 2877 | |
| 2878 | |
| 2879 | |
| 2880 | |
| 2881 | |
| 2882 | |
| 2883 | |
| 2884 | |
| 2885 | |
| 2886 | |
| 2887 | |
| 2888 | |
| 2889 | static const Optional<BitPart> & |
| 2890 | collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, |
| 2891 | std::map<Value *, Optional<BitPart>> &BPS, int Depth, |
| 2892 | bool &FoundRoot) { |
| 2893 | auto I = BPS.find(V); |
| 2894 | if (I != BPS.end()) |
| 2895 | return I->second; |
| 2896 | |
| 2897 | auto &Result = BPS[V] = None; |
| 2898 | auto BitWidth = V->getType()->getScalarSizeInBits(); |
| 2899 | |
| 2900 | |
| 2901 | if (BitWidth > 128) |
| 2902 | return Result; |
| 2903 | |
| 2904 | |
| 2905 | if (Depth == BitPartRecursionMaxDepth) { |
| 2906 | LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n"); |
| 2907 | return Result; |
| 2908 | } |
| 2909 | |
| 2910 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 2911 | Value *X, *Y; |
| 2912 | const APInt *C; |
| 2913 | |
| 2914 | |
| 2915 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) { |
| 2916 | |
| 2917 | const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 2918 | Depth + 1, FoundRoot); |
| 2919 | if (!A || !A->Provider) |
| 2920 | return Result; |
| 2921 | |
| 2922 | const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, |
| 2923 | Depth + 1, FoundRoot); |
| 2924 | if (!B || A->Provider != B->Provider) |
| 2925 | return Result; |
| 2926 | |
| 2927 | |
| 2928 | Result = BitPart(A->Provider, BitWidth); |
| 2929 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) { |
| 2930 | if (A->Provenance[BitIdx] != BitPart::Unset && |
| 2931 | B->Provenance[BitIdx] != BitPart::Unset && |
| 2932 | A->Provenance[BitIdx] != B->Provenance[BitIdx]) |
| 2933 | return Result = None; |
| 2934 | |
| 2935 | if (A->Provenance[BitIdx] == BitPart::Unset) |
| 2936 | Result->Provenance[BitIdx] = B->Provenance[BitIdx]; |
| 2937 | else |
| 2938 | Result->Provenance[BitIdx] = A->Provenance[BitIdx]; |
| 2939 | } |
| 2940 | |
| 2941 | return Result; |
| 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) { |
| 2946 | const APInt &BitShift = *C; |
| 2947 | |
| 2948 | |
| 2949 | if (BitShift.uge(BitWidth)) |
| 2950 | return Result; |
| 2951 | |
| 2952 | |
| 2953 | if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0) |
| 2954 | return Result; |
| 2955 | |
| 2956 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 2957 | Depth + 1, FoundRoot); |
| 2958 | if (!Res) |
| 2959 | return Result; |
| 2960 | Result = Res; |
| 2961 | |
| 2962 | |
| 2963 | auto &P = Result->Provenance; |
| 2964 | if (I->getOpcode() == Instruction::Shl) { |
| 2965 | P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end()); |
| 2966 | P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset); |
| 2967 | } else { |
| 2968 | P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue())); |
| 2969 | P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset); |
| 2970 | } |
| 2971 | |
| 2972 | return Result; |
| 2973 | } |
| 2974 | |
| 2975 | |
| 2976 | |
| 2977 | if (match(V, m_And(m_Value(X), m_APInt(C)))) { |
| 2978 | const APInt &AndMask = *C; |
| 2979 | |
| 2980 | |
| 2981 | |
| 2982 | unsigned NumMaskedBits = AndMask.countPopulation(); |
| 2983 | if (!MatchBitReversals && (NumMaskedBits % 8) != 0) |
| 2984 | return Result; |
| 2985 | |
| 2986 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 2987 | Depth + 1, FoundRoot); |
| 2988 | if (!Res) |
| 2989 | return Result; |
| 2990 | Result = Res; |
| 2991 | |
| 2992 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
| 2993 | |
| 2994 | if (AndMask[BitIdx] == 0) |
| 2995 | Result->Provenance[BitIdx] = BitPart::Unset; |
| 2996 | return Result; |
| 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | if (match(V, m_ZExt(m_Value(X)))) { |
| 3001 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 3002 | Depth + 1, FoundRoot); |
| 3003 | if (!Res) |
| 3004 | return Result; |
| 3005 | |
| 3006 | Result = BitPart(Res->Provider, BitWidth); |
| 3007 | auto NarrowBitWidth = X->getType()->getScalarSizeInBits(); |
| 3008 | for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx) |
| 3009 | Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; |
| 3010 | for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx) |
| 3011 | Result->Provenance[BitIdx] = BitPart::Unset; |
| 3012 | return Result; |
| 3013 | } |
| 3014 | |
| 3015 | |
| 3016 | if (match(V, m_Trunc(m_Value(X)))) { |
| 3017 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 3018 | Depth + 1, FoundRoot); |
| 3019 | if (!Res) |
| 3020 | return Result; |
| 3021 | |
| 3022 | Result = BitPart(Res->Provider, BitWidth); |
| 3023 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
| 3024 | Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; |
| 3025 | return Result; |
| 3026 | } |
| 3027 | |
| 3028 | |
| 3029 | |
| 3030 | if (match(V, m_BitReverse(m_Value(X)))) { |
| 3031 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 3032 | Depth + 1, FoundRoot); |
| 3033 | if (!Res) |
| 3034 | return Result; |
| 3035 | |
| 3036 | Result = BitPart(Res->Provider, BitWidth); |
| 3037 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
| 3038 | Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx]; |
| 3039 | return Result; |
| 3040 | } |
| 3041 | |
| 3042 | |
| 3043 | if (match(V, m_BSwap(m_Value(X)))) { |
| 3044 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 3045 | Depth + 1, FoundRoot); |
| 3046 | if (!Res) |
| 3047 | return Result; |
| 3048 | |
| 3049 | unsigned ByteWidth = BitWidth / 8; |
| 3050 | Result = BitPart(Res->Provider, BitWidth); |
| 3051 | for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) { |
| 3052 | unsigned ByteBitOfs = ByteIdx * 8; |
| 3053 | for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx) |
| 3054 | Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] = |
| 3055 | Res->Provenance[ByteBitOfs + BitIdx]; |
| 3056 | } |
| 3057 | return Result; |
| 3058 | } |
| 3059 | |
| 3060 | |
| 3061 | |
| 3062 | |
| 3063 | |
| 3064 | if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) || |
| 3065 | match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) { |
| 3066 | |
| 3067 | unsigned ModAmt = C->urem(BitWidth); |
| 3068 | if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr) |
| 3069 | ModAmt = BitWidth - ModAmt; |
| 3070 | |
| 3071 | |
| 3072 | if (!MatchBitReversals && (ModAmt % 8) != 0) |
| 3073 | return Result; |
| 3074 | |
| 3075 | |
| 3076 | const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
| 3077 | Depth + 1, FoundRoot); |
| 3078 | if (!LHS || !LHS->Provider) |
| 3079 | return Result; |
| 3080 | |
| 3081 | const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, |
| 3082 | Depth + 1, FoundRoot); |
| 3083 | if (!RHS || LHS->Provider != RHS->Provider) |
| 3084 | return Result; |
| 3085 | |
| 3086 | unsigned StartBitRHS = BitWidth - ModAmt; |
| 3087 | Result = BitPart(LHS->Provider, BitWidth); |
| 3088 | for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx) |
| 3089 | Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx]; |
| 3090 | for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx) |
| 3091 | Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS]; |
| 3092 | return Result; |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | |
| 3098 | if (FoundRoot) |
| 3099 | return Result; |
| 3100 | |
| 3101 | |
| 3102 | |
| 3103 | FoundRoot = true; |
| 3104 | Result = BitPart(V, BitWidth); |
| 3105 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
| 3106 | Result->Provenance[BitIdx] = BitIdx; |
| 3107 | return Result; |
| 3108 | } |
| 3109 | |
| 3110 | static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To, |
| 3111 | unsigned BitWidth) { |
| 3112 | if (From % 8 != To % 8) |
| 3113 | return false; |
| 3114 | |
| 3115 | From >>= 3; |
| 3116 | To >>= 3; |
| 3117 | BitWidth >>= 3; |
| 3118 | return From == BitWidth - To - 1; |
| 3119 | } |
| 3120 | |
| 3121 | static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To, |
| 3122 | unsigned BitWidth) { |
| 3123 | return From == BitWidth - To - 1; |
| 3124 | } |
| 3125 | |
| 3126 | bool llvm::recognizeBSwapOrBitReverseIdiom( |
| 3127 | Instruction *I, bool MatchBSwaps, bool MatchBitReversals, |
| 3128 | SmallVectorImpl<Instruction *> &InsertedInsts) { |
| 3129 | if (!match(I, m_Or(m_Value(), m_Value())) && |
| 3130 | !match(I, m_FShl(m_Value(), m_Value(), m_Value())) && |
| 3131 | !match(I, m_FShr(m_Value(), m_Value(), m_Value()))) |
| 3132 | return false; |
| 3133 | if (!MatchBSwaps && !MatchBitReversals) |
| 3134 | return false; |
| 3135 | Type *ITy = I->getType(); |
| 3136 | if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128) |
| 3137 | return false; |
| 3138 | |
| 3139 | Type *DemandedTy = ITy; |
| 3140 | if (I->hasOneUse()) |
| 3141 | if (auto *Trunc = dyn_cast<TruncInst>(I->user_back())) |
| 3142 | DemandedTy = Trunc->getType(); |
| 3143 | |
| 3144 | |
| 3145 | bool FoundRoot = false; |
| 3146 | std::map<Value *, Optional<BitPart>> BPS; |
| 3147 | const auto &Res = |
| 3148 | collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot); |
| 3149 | if (!Res) |
| 3150 | return false; |
| 3151 | ArrayRef<int8_t> BitProvenance = Res->Provenance; |
| 3152 | assert(all_of(BitProvenance, |
| 3153 | [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) && |
| 3154 | "Illegal bit provenance index"); |
| 3155 | |
| 3156 | |
| 3157 | if (BitProvenance.back() == BitPart::Unset) { |
| 3158 | while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset) |
| 3159 | BitProvenance = BitProvenance.drop_back(); |
| 3160 | if (BitProvenance.empty()) |
| 3161 | return false; |
| 3162 | DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size()); |
| 3163 | if (auto *IVecTy = dyn_cast<VectorType>(ITy)) |
| 3164 | DemandedTy = VectorType::get(DemandedTy, IVecTy); |
| 3165 | } |
| 3166 | |
| 3167 | |
| 3168 | unsigned DemandedBW = DemandedTy->getScalarSizeInBits(); |
| 3169 | if (DemandedBW > ITy->getScalarSizeInBits()) |
| 3170 | return false; |
| 3171 | |
| 3172 | |
| 3173 | |
| 3174 | APInt DemandedMask = APInt::getAllOnesValue(DemandedBW); |
| 3175 | bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0; |
| 3176 | bool OKForBitReverse = MatchBitReversals; |
| 3177 | for (unsigned BitIdx = 0; |
| 3178 | (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) { |
| 3179 | if (BitProvenance[BitIdx] == BitPart::Unset) { |
| 3180 | DemandedMask.clearBit(BitIdx); |
| 3181 | continue; |
| 3182 | } |
| 3183 | OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx, |
| 3184 | DemandedBW); |
| 3185 | OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx], |
| 3186 | BitIdx, DemandedBW); |
| 3187 | } |
| 3188 | |
| 3189 | Intrinsic::ID Intrin; |
| 3190 | if (OKForBSwap) |
| 3191 | Intrin = Intrinsic::bswap; |
| 3192 | else if (OKForBitReverse) |
| 3193 | Intrin = Intrinsic::bitreverse; |
| 3194 | else |
| 3195 | return false; |
| 3196 | |
| 3197 | Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, DemandedTy); |
| 3198 | Value *Provider = Res->Provider; |
| 3199 | |
| 3200 | |
| 3201 | if (DemandedTy != Provider->getType()) { |
| 3202 | auto *Trunc = |
| 3203 | CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I); |
| 3204 | InsertedInsts.push_back(Trunc); |
| 3205 | Provider = Trunc; |
| 3206 | } |
| 3207 | |
| 3208 | Instruction *Result = CallInst::Create(F, Provider, "rev", I); |
| 3209 | InsertedInsts.push_back(Result); |
| 3210 | |
| 3211 | if (!DemandedMask.isAllOnesValue()) { |
| 3212 | auto *Mask = ConstantInt::get(DemandedTy, DemandedMask); |
| 3213 | Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I); |
| 3214 | InsertedInsts.push_back(Result); |
| 3215 | } |
| 3216 | |
| 3217 | |
| 3218 | if (ITy != Result->getType()) { |
| 3219 | auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I); |
| 3220 | InsertedInsts.push_back(ExtInst); |
| 3221 | } |
| 3222 | |
| 3223 | return true; |
| 3224 | } |
| 3225 | |
| 3226 | |
| 3227 | |
| 3228 | |
| 3229 | |
| 3230 | |
| 3231 | void llvm::maybeMarkSanitizerLibraryCallNoBuiltin( |
| 3232 | CallInst *CI, const TargetLibraryInfo *TLI) { |
| 3233 | Function *F = CI->getCalledFunction(); |
| 3234 | LibFunc Func; |
| 3235 | if (F && !F->hasLocalLinkage() && F->hasName() && |
| 3236 | TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) && |
| 3237 | !F->doesNotAccessMemory()) |
| 3238 | CI->addAttribute(AttributeList::FunctionIndex, Attribute::NoBuiltin); |
| 3239 | } |
| 3240 | |
| 3241 | bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) { |
| 3242 | |
| 3243 | if (I->getOperand(OpIdx)->getType()->isMetadataTy()) |
| 3244 | return false; |
| 3245 | |
| 3246 | |
| 3247 | if (!isa<Constant>(I->getOperand(OpIdx))) |
| 3248 | return true; |
| 3249 | |
| 3250 | switch (I->getOpcode()) { |
| 3251 | default: |
| 3252 | return true; |
| 3253 | case Instruction::Call: |
| 3254 | case Instruction::Invoke: { |
| 3255 | const auto &CB = cast<CallBase>(*I); |
| 3256 | |
| 3257 | |
| 3258 | if (CB.isInlineAsm()) |
| 3259 | return false; |
| 3260 | |
| 3261 | |
| 3262 | |
| 3263 | if (CB.isBundleOperand(OpIdx)) |
| 3264 | return false; |
| 3265 | |
| 3266 | if (OpIdx < CB.getNumArgOperands()) { |
| 3267 | |
| 3268 | |
| 3269 | if (isa<IntrinsicInst>(CB) && |
| 3270 | OpIdx >= CB.getFunctionType()->getNumParams()) { |
| 3271 | |
| 3272 | return CB.getIntrinsicID() == Intrinsic::experimental_stackmap; |
| 3273 | } |
| 3274 | |
| 3275 | |
| 3276 | |
| 3277 | if (CB.getIntrinsicID() == Intrinsic::gcroot) |
| 3278 | return false; |
| 3279 | |
| 3280 | |
| 3281 | return !CB.paramHasAttr(OpIdx, Attribute::ImmArg); |
| 3282 | } |
| 3283 | |
| 3284 | |
| 3285 | |
| 3286 | return !isa<IntrinsicInst>(CB); |
| 3287 | } |
| 3288 | case Instruction::ShuffleVector: |
| 3289 | |
| 3290 | return OpIdx != 2; |
| 3291 | case Instruction::Switch: |
| 3292 | case Instruction::ExtractValue: |
| 3293 | |
| 3294 | return OpIdx == 0; |
| 3295 | case Instruction::InsertValue: |
| 3296 | |
| 3297 | return OpIdx < 2; |
| 3298 | case Instruction::Alloca: |
| 3299 | |
| 3300 | |
| 3301 | |
| 3302 | return !cast<AllocaInst>(I)->isStaticAlloca(); |
| 3303 | case Instruction::GetElementPtr: |
| 3304 | if (OpIdx == 0) |
| 3305 | return true; |
| 3306 | gep_type_iterator It = gep_type_begin(I); |
| 3307 | for (auto E = std::next(It, OpIdx); It != E; ++It) |
| 3308 | if (It.isStruct()) |
| 3309 | return false; |
| 3310 | return true; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | Value *llvm::invertCondition(Value *Condition) { |
| 3315 | |
| 3316 | if (Constant *C = dyn_cast<Constant>(Condition)) |
| 1 | Assuming 'Condition' is not a 'Constant' | |
|
| |
| 3317 | return ConstantExpr::getNot(C); |
| 3318 | |
| 3319 | |
| 3320 | Value *NotCondition; |
| 3321 | if (match(Condition, m_Not(m_Value(NotCondition)))) |
| 3 | | Calling 'match<llvm::Value, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::bind_ty<llvm::Value>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 30, true>>' | |
|
| 12 | | Returning from 'match<llvm::Value, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::bind_ty<llvm::Value>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 30, true>>' | |
|
| |
| 3322 | return NotCondition; |
| 3323 | |
| 3324 | BasicBlock *Parent = nullptr; |
| 14 | | 'Parent' initialized to a null pointer value | |
|
| 3325 | Instruction *Inst = dyn_cast<Instruction>(Condition); |
| 15 | | Assuming 'Condition' is not a 'Instruction' | |
|
| 3326 | if (Inst) |
| |
| 3327 | Parent = Inst->getParent(); |
| 3328 | else if (Argument *Arg = dyn_cast<Argument>(Condition)) |
| 17 | | Assuming 'Condition' is not a 'Argument' | |
|
| |
| 3329 | Parent = &Arg->getParent()->getEntryBlock(); |
| 3330 | assert(Parent && "Unsupported condition to invert"); |
| 3331 | |
| 3332 | |
| 3333 | for (User *U : Condition->users()) |
| 3334 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 3335 | if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) |
| 3336 | return I; |
| 3337 | |
| 3338 | |
| 3339 | auto *Inverted = |
| 3340 | BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv"); |
| 3341 | if (Inst && !isa<PHINode>(Inst)) |
| 3342 | Inverted->insertAfter(Inst); |
| 3343 | else |
| 3344 | Inverted->insertBefore(&*Parent->getFirstInsertionPt()); |
| 19 | | Called C++ object pointer is null |
|
| 3345 | return Inverted; |
| 3346 | } |
| 3347 | |
| 3348 | bool llvm::inferAttributesFromOthers(Function &F) { |
| 3349 | |
| 3350 | |
| 3351 | |
| 3352 | bool Changed = false; |
| 3353 | |
| 3354 | if (!F.hasFnAttribute(Attribute::NoSync) && |
| 3355 | F.doesNotAccessMemory() && !F.isConvergent()) { |
| 3356 | F.setNoSync(); |
| 3357 | Changed = true; |
| 3358 | } |
| 3359 | |
| 3360 | |
| 3361 | if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) { |
| 3362 | F.setDoesNotFreeMemory(); |
| 3363 | Changed = true; |
| 3364 | } |
| 3365 | |
| 3366 | |
| 3367 | if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) { |
| 3368 | F.setMustProgress(); |
| 3369 | Changed = true; |
| 3370 | } |
| 3371 | |
| 3372 | |
| 3373 | |
| 3374 | |
| 3375 | return Changed; |
| 3376 | } |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #ifndef LLVM_IR_PATTERNMATCH_H |
| 29 | #define LLVM_IR_PATTERNMATCH_H |
| 30 | |
| 31 | #include "llvm/ADT/APFloat.h" |
| 32 | #include "llvm/ADT/APInt.h" |
| 33 | #include "llvm/IR/Constant.h" |
| 34 | #include "llvm/IR/Constants.h" |
| 35 | #include "llvm/IR/DataLayout.h" |
| 36 | #include "llvm/IR/InstrTypes.h" |
| 37 | #include "llvm/IR/Instruction.h" |
| 38 | #include "llvm/IR/Instructions.h" |
| 39 | #include "llvm/IR/IntrinsicInst.h" |
| 40 | #include "llvm/IR/Intrinsics.h" |
| 41 | #include "llvm/IR/Operator.h" |
| 42 | #include "llvm/IR/Value.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include <cstdint> |
| 45 | |
| 46 | namespace llvm { |
| 47 | namespace PatternMatch { |
| 48 | |
| 49 | template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { |
| 50 | return const_cast<Pattern &>(P).match(V); |
| 4 | | Calling 'BinaryOp_match::match' | |
|
| 10 | | Returning from 'BinaryOp_match::match' | |
|
| 11 | | Returning zero, which participates in a condition later | |
|
| 51 | } |
| 52 | |
| 53 | template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) { |
| 54 | return const_cast<Pattern &>(P).match(Mask); |
| 55 | } |
| 56 | |
| 57 | template <typename SubPattern_t> struct OneUse_match { |
| 58 | SubPattern_t SubPattern; |
| 59 | |
| 60 | OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} |
| 61 | |
| 62 | template <typename OpTy> bool match(OpTy *V) { |
| 63 | return V->hasOneUse() && SubPattern.match(V); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { |
| 68 | return SubPattern; |
| 69 | } |
| 70 | |
| 71 | template <typename Class> struct class_match { |
| 72 | template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | inline class_match<Value> m_Value() { return class_match<Value>(); } |
| 77 | |
| 78 | |
| 79 | inline class_match<UnaryOperator> m_UnOp() { |
| 80 | return class_match<UnaryOperator>(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | inline class_match<BinaryOperator> m_BinOp() { |
| 85 | return class_match<BinaryOperator>(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } |
| 90 | |
| 91 | struct undef_match { |
| 92 | static bool check(const Value *V) { |
| 93 | if (isa<UndefValue>(V)) |
| 94 | return true; |
| 95 | |
| 96 | const auto *CA = dyn_cast<ConstantAggregate>(V); |
| 97 | if (!CA) |
| 98 | return false; |
| 99 | |
| 100 | SmallPtrSet<const ConstantAggregate *, 8> Seen; |
| 101 | SmallVector<const ConstantAggregate *, 8> Worklist; |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | auto CheckValue = [&](const ConstantAggregate *CA) { |
| 107 | for (const Value *Op : CA->operand_values()) { |
| 108 | if (isa<UndefValue>(Op)) |
| 109 | continue; |
| 110 | |
| 111 | const auto *CA = dyn_cast<ConstantAggregate>(Op); |
| 112 | if (!CA) |
| 113 | return false; |
| 114 | if (Seen.insert(CA).second) |
| 115 | Worklist.emplace_back(CA); |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | }; |
| 120 | |
| 121 | if (!CheckValue(CA)) |
| 122 | return false; |
| 123 | |
| 124 | while (!Worklist.empty()) { |
| 125 | if (!CheckValue(Worklist.pop_back_val())) |
| 126 | return false; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | template <typename ITy> bool match(ITy *V) { return check(V); } |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | inline auto m_Undef() { return undef_match(); } |
| 137 | |
| 138 | |
| 139 | inline class_match<PoisonValue> m_Poison() { return class_match<PoisonValue>(); } |
| 140 | |
| 141 | |
| 142 | inline class_match<Constant> m_Constant() { return class_match<Constant>(); } |
| 143 | |
| 144 | |
| 145 | inline class_match<ConstantInt> m_ConstantInt() { |
| 146 | return class_match<ConstantInt>(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | inline class_match<ConstantFP> m_ConstantFP() { |
| 151 | return class_match<ConstantFP>(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | inline class_match<ConstantExpr> m_ConstantExpr() { |
| 156 | return class_match<ConstantExpr>(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | inline class_match<BasicBlock> m_BasicBlock() { |
| 161 | return class_match<BasicBlock>(); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | template <typename Ty> struct match_unless { |
| 166 | Ty M; |
| 167 | |
| 168 | match_unless(const Ty &Matcher) : M(Matcher) {} |
| 169 | |
| 170 | template <typename ITy> bool match(ITy *V) { return !M.match(V); } |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) { |
| 175 | return match_unless<Ty>(M); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | template <typename LTy, typename RTy> struct match_combine_or { |
| 180 | LTy L; |
| 181 | RTy R; |
| 182 | |
| 183 | match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
| 184 | |
| 185 | template <typename ITy> bool match(ITy *V) { |
| 186 | if (L.match(V)) |
| 187 | return true; |
| 188 | if (R.match(V)) |
| 189 | return true; |
| 190 | return false; |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | template <typename LTy, typename RTy> struct match_combine_and { |
| 195 | LTy L; |
| 196 | RTy R; |
| 197 | |
| 198 | match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
| 199 | |
| 200 | template <typename ITy> bool match(ITy *V) { |
| 201 | if (L.match(V)) |
| 202 | if (R.match(V)) |
| 203 | return true; |
| 204 | return false; |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | template <typename LTy, typename RTy> |
| 210 | inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { |
| 211 | return match_combine_or<LTy, RTy>(L, R); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | template <typename LTy, typename RTy> |
| 216 | inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { |
| 217 | return match_combine_and<LTy, RTy>(L, R); |
| 218 | } |
| 219 | |
| 220 | struct apint_match { |
| 221 | const APInt *&Res; |
| 222 | bool AllowUndef; |
| 223 | |
| 224 | apint_match(const APInt *&Res, bool AllowUndef) |
| 225 | : Res(Res), AllowUndef(AllowUndef) {} |
| 226 | |
| 227 | template <typename ITy> bool match(ITy *V) { |
| 228 | if (auto *CI = dyn_cast<ConstantInt>(V)) { |
| 229 | Res = &CI->getValue(); |
| 230 | return true; |
| 231 | } |
| 232 | if (V->getType()->isVectorTy()) |
| 233 | if (const auto *C = dyn_cast<Constant>(V)) |
| 234 | if (auto *CI = dyn_cast_or_null<ConstantInt>( |
| 235 | C->getSplatValue(AllowUndef))) { |
| 236 | Res = &CI->getValue(); |
| 237 | return true; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | |
| 244 | |
| 245 | struct apfloat_match { |
| 246 | const APFloat *&Res; |
| 247 | bool AllowUndef; |
| 248 | |
| 249 | apfloat_match(const APFloat *&Res, bool AllowUndef) |
| 250 | : Res(Res), AllowUndef(AllowUndef) {} |
| 251 | |
| 252 | template <typename ITy> bool match(ITy *V) { |
| 253 | if (auto *CI = dyn_cast<ConstantFP>(V)) { |
| 254 | Res = &CI->getValueAPF(); |
| 255 | return true; |
| 256 | } |
| 257 | if (V->getType()->isVectorTy()) |
| 258 | if (const auto *C = dyn_cast<Constant>(V)) |
| 259 | if (auto *CI = dyn_cast_or_null<ConstantFP>( |
| 260 | C->getSplatValue(AllowUndef))) { |
| 261 | Res = &CI->getValueAPF(); |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | |
| 269 | |
| 270 | inline apint_match m_APInt(const APInt *&Res) { |
| 271 | |
| 272 | return apint_match(Res, false); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | inline apint_match m_APIntAllowUndef(const APInt *&Res) { |
| 277 | return apint_match(Res, true); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | inline apint_match m_APIntForbidUndef(const APInt *&Res) { |
| 282 | return apint_match(Res, false); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | inline apfloat_match m_APFloat(const APFloat *&Res) { |
| 288 | |
| 289 | return apfloat_match(Res, false); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) { |
| 294 | return apfloat_match(Res, true); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) { |
| 299 | return apfloat_match(Res, false); |
| 300 | } |
| 301 | |
| 302 | template <int64_t Val> struct constantint_match { |
| 303 | template <typename ITy> bool match(ITy *V) { |
| 304 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { |
| 305 | const APInt &CIV = CI->getValue(); |
| 306 | if (Val >= 0) |
| 307 | return CIV == static_cast<uint64_t>(Val); |
| 308 | |
| 309 | |
| 310 | |
| 311 | return -CIV == -Val; |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { |
| 319 | return constantint_match<Val>(); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | template <typename Predicate, typename ConstantVal> |
| 326 | struct cstval_pred_ty : public Predicate { |
| 327 | template <typename ITy> bool match(ITy *V) { |
| 328 | if (const auto *CV = dyn_cast<ConstantVal>(V)) |
| 329 | return this->isValue(CV->getValue()); |
| 330 | if (const auto *VTy = dyn_cast<VectorType>(V->getType())) { |
| 331 | if (const auto *C = dyn_cast<Constant>(V)) { |
| 332 | if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue())) |
| 333 | return this->isValue(CV->getValue()); |
| 334 | |
| 335 | |
| 336 | auto *FVTy = dyn_cast<FixedVectorType>(VTy); |
| 337 | if (!FVTy) |
| 338 | return false; |
| 339 | |
| 340 | |
| 341 | unsigned NumElts = FVTy->getNumElements(); |
| 342 | assert(NumElts != 0 && "Constant vector with no elements?"); |
| 343 | bool HasNonUndefElements = false; |
| 344 | for (unsigned i = 0; i != NumElts; ++i) { |
| 345 | Constant *Elt = C->getAggregateElement(i); |
| 346 | if (!Elt) |
| 347 | return false; |
| 348 | if (isa<UndefValue>(Elt)) |
| 349 | continue; |
| 350 | auto *CV = dyn_cast<ConstantVal>(Elt); |
| 351 | if (!CV || !this->isValue(CV->getValue())) |
| 352 | return false; |
| 353 | HasNonUndefElements = true; |
| 354 | } |
| 355 | return HasNonUndefElements; |
| 356 | } |
| 357 | } |
| 358 | return false; |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | |
| 363 | template <typename Predicate> |
| 364 | using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>; |
| 365 | |
| 366 | |
| 367 | template <typename Predicate> |
| 368 | using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>; |
| 369 | |
| 370 | |
| 371 | |
| 372 | template <typename Predicate> struct api_pred_ty : public Predicate { |
| 373 | const APInt *&Res; |
| 374 | |
| 375 | api_pred_ty(const APInt *&R) : Res(R) {} |
| 376 | |
| 377 | template <typename ITy> bool match(ITy *V) { |
| 378 | if (const auto *CI = dyn_cast<ConstantInt>(V)) |
| 379 | if (this->isValue(CI->getValue())) { |
| 380 | Res = &CI->getValue(); |
| 381 | return true; |
| 382 | } |
| 383 | if (V->getType()->isVectorTy()) |
| 384 | if (const auto *C = dyn_cast<Constant>(V)) |
| 385 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) |
| 386 | if (this->isValue(CI->getValue())) { |
| 387 | Res = &CI->getValue(); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | }; |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | template <typename Predicate> struct apf_pred_ty : public Predicate { |
| 399 | const APFloat *&Res; |
| 400 | |
| 401 | apf_pred_ty(const APFloat *&R) : Res(R) {} |
| 402 | |
| 403 | template <typename ITy> bool match(ITy *V) { |
| 404 | if (const auto *CI = dyn_cast<ConstantFP>(V)) |
| 405 | if (this->isValue(CI->getValue())) { |
| 406 | Res = &CI->getValue(); |
| 407 | return true; |
| 408 | } |
| 409 | if (V->getType()->isVectorTy()) |
| 410 | if (const auto *C = dyn_cast<Constant>(V)) |
| 411 | if (auto *CI = dyn_cast_or_null<ConstantFP>( |
| 412 | C->getSplatValue( true))) |
| 413 | if (this->isValue(CI->getValue())) { |
| 414 | Res = &CI->getValue(); |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | return false; |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | struct is_any_apint { |
| 432 | bool isValue(const APInt &C) { return true; } |
| 433 | }; |
| 434 | |
| 435 | |
| 436 | inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() { |
| 437 | return cst_pred_ty<is_any_apint>(); |
| 438 | } |
| 439 | |
| 440 | struct is_all_ones { |
| 441 | bool isValue(const APInt &C) { return C.isAllOnesValue(); } |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | inline cst_pred_ty<is_all_ones> m_AllOnes() { |
| 446 | return cst_pred_ty<is_all_ones>(); |
| 447 | } |
| 448 | |
| 449 | struct is_maxsignedvalue { |
| 450 | bool isValue(const APInt &C) { return C.isMaxSignedValue(); } |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | |
| 455 | inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { |
| 456 | return cst_pred_ty<is_maxsignedvalue>(); |
| 457 | } |
| 458 | inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { |
| 459 | return V; |
| 460 | } |
| 461 | |
| 462 | struct is_negative { |
| 463 | bool isValue(const APInt &C) { return C.isNegative(); } |
| 464 | }; |
| 465 | |
| 466 | |
| 467 | inline cst_pred_ty<is_negative> m_Negative() { |
| 468 | return cst_pred_ty<is_negative>(); |
| 469 | } |
| 470 | inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { |
| 471 | return V; |
| 472 | } |
| 473 | |
| 474 | struct is_nonnegative { |
| 475 | bool isValue(const APInt &C) { return C.isNonNegative(); } |
| 476 | }; |
| 477 | |
| 478 | |
| 479 | inline cst_pred_ty<is_nonnegative> m_NonNegative() { |
| 480 | return cst_pred_ty<is_nonnegative>(); |
| 481 | } |
| 482 | inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { |
| 483 | return V; |
| 484 | } |
| 485 | |
| 486 | struct is_strictlypositive { |
| 487 | bool isValue(const APInt &C) { return C.isStrictlyPositive(); } |
| 488 | }; |
| 489 | |
| 490 | |
| 491 | inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { |
| 492 | return cst_pred_ty<is_strictlypositive>(); |
| 493 | } |
| 494 | inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { |
| 495 | return V; |
| 496 | } |
| 497 | |
| 498 | struct is_nonpositive { |
| 499 | bool isValue(const APInt &C) { return C.isNonPositive(); } |
| 500 | }; |
| 501 | |
| 502 | |
| 503 | inline cst_pred_ty<is_nonpositive> m_NonPositive() { |
| 504 | return cst_pred_ty<is_nonpositive>(); |
| 505 | } |
| 506 | inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } |
| 507 | |
| 508 | struct is_one { |
| 509 | bool isValue(const APInt &C) { return C.isOneValue(); } |
| 510 | }; |
| 511 | |
| 512 | |
| 513 | inline cst_pred_ty<is_one> m_One() { |
| 514 | return cst_pred_ty<is_one>(); |
| 515 | } |
| 516 | |
| 517 | struct is_zero_int { |
| 518 | bool isValue(const APInt &C) { return C.isNullValue(); } |
| 519 | }; |
| 520 | |
| 521 | |
| 522 | inline cst_pred_ty<is_zero_int> m_ZeroInt() { |
| 523 | return cst_pred_ty<is_zero_int>(); |
| 524 | } |
| 525 | |
| 526 | struct is_zero { |
| 527 | template <typename ITy> bool match(ITy *V) { |
| 528 | auto *C = dyn_cast<Constant>(V); |
| 529 | |
| 530 | return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); |
| 531 | } |
| 532 | }; |
| 533 | |
| 534 | |
| 535 | inline is_zero m_Zero() { |
| 536 | return is_zero(); |
| 537 | } |
| 538 | |
| 539 | struct is_power2 { |
| 540 | bool isValue(const APInt &C) { return C.isPowerOf2(); } |
| 541 | }; |
| 542 | |
| 543 | |
| 544 | inline cst_pred_ty<is_power2> m_Power2() { |
| 545 | return cst_pred_ty<is_power2>(); |
| 546 | } |
| 547 | inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { |
| 548 | return V; |
| 549 | } |
| 550 | |
| 551 | struct is_negated_power2 { |
| 552 | bool isValue(const APInt &C) { return (-C).isPowerOf2(); } |
| 553 | }; |
| 554 | |
| 555 | |
| 556 | inline cst_pred_ty<is_negated_power2> m_NegatedPower2() { |
| 557 | return cst_pred_ty<is_negated_power2>(); |
| 558 | } |
| 559 | inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) { |
| 560 | return V; |
| 561 | } |
| 562 | |
| 563 | struct is_power2_or_zero { |
| 564 | bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } |
| 565 | }; |
| 566 | |
| 567 | |
| 568 | inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { |
| 569 | return cst_pred_ty<is_power2_or_zero>(); |
| 570 | } |
| 571 | inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { |
| 572 | return V; |
| 573 | } |
| 574 | |
| 575 | struct is_sign_mask { |
| 576 | bool isValue(const APInt &C) { return C.isSignMask(); } |
| 577 | }; |
| 578 | |
| 579 | |
| 580 | inline cst_pred_ty<is_sign_mask> m_SignMask() { |
| 581 | return cst_pred_ty<is_sign_mask>(); |
| 582 | } |
| 583 | |
| 584 | struct is_lowbit_mask { |
| 585 | bool isValue(const APInt &C) { return C.isMask(); } |
| 586 | }; |
| 587 | |
| 588 | |
| 589 | inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { |
| 590 | return cst_pred_ty<is_lowbit_mask>(); |
| 591 | } |
| 592 | |
| 593 | struct icmp_pred_with_threshold { |
| 594 | ICmpInst::Predicate Pred; |
| 595 | const APInt *Thr; |
| 596 | bool isValue(const APInt &C) { |
| 597 | switch (Pred) { |
| 598 | case ICmpInst::Predicate::ICMP_EQ: |
| 599 | return C.eq(*Thr); |
| 600 | case ICmpInst::Predicate::ICMP_NE: |
| 601 | return C.ne(*Thr); |
| 602 | case ICmpInst::Predicate::ICMP_UGT: |
| 603 | return C.ugt(*Thr); |
| 604 | case ICmpInst::Predicate::ICMP_UGE: |
| 605 | return C.uge(*Thr); |
| 606 | case ICmpInst::Predicate::ICMP_ULT: |
| 607 | return C.ult(*Thr); |
| 608 | case ICmpInst::Predicate::ICMP_ULE: |
| 609 | return C.ule(*Thr); |
| 610 | case ICmpInst::Predicate::ICMP_SGT: |
| 611 | return C.sgt(*Thr); |
| 612 | case ICmpInst::Predicate::ICMP_SGE: |
| 613 | return C.sge(*Thr); |
| 614 | case ICmpInst::Predicate::ICMP_SLT: |
| 615 | return C.slt(*Thr); |
| 616 | case ICmpInst::Predicate::ICMP_SLE: |
| 617 | return C.sle(*Thr); |
| 618 | default: |
| 619 | llvm_unreachable("Unhandled ICmp predicate"); |
| 620 | } |
| 621 | } |
| 622 | }; |
| 623 | |
| 624 | |
| 625 | inline cst_pred_ty<icmp_pred_with_threshold> |
| 626 | m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { |
| 627 | cst_pred_ty<icmp_pred_with_threshold> P; |
| 628 | P.Pred = Predicate; |
| 629 | P.Thr = &Threshold; |
| 630 | return P; |
| 631 | } |
| 632 | |
| 633 | struct is_nan { |
| 634 | bool isValue(const APFloat &C) { return C.isNaN(); } |
| 635 | }; |
| 636 | |
| 637 | |
| 638 | inline cstfp_pred_ty<is_nan> m_NaN() { |
| 639 | return cstfp_pred_ty<is_nan>(); |
| 640 | } |
| 641 | |
| 642 | struct is_nonnan { |
| 643 | bool isValue(const APFloat &C) { return !C.isNaN(); } |
| 644 | }; |
| 645 | |
| 646 | |
| 647 | inline cstfp_pred_ty<is_nonnan> m_NonNaN() { |
| 648 | return cstfp_pred_ty<is_nonnan>(); |
| 649 | } |
| 650 | |
| 651 | struct is_inf { |
| 652 | bool isValue(const APFloat &C) { return C.isInfinity(); } |
| 653 | }; |
| 654 | |
| 655 | |
| 656 | inline cstfp_pred_ty<is_inf> m_Inf() { |
| 657 | return cstfp_pred_ty<is_inf>(); |
| 658 | } |
| 659 | |
| 660 | struct is_noninf { |
| 661 | bool isValue(const APFloat &C) { return !C.isInfinity(); } |
| 662 | }; |
| 663 | |
| 664 | |
| 665 | inline cstfp_pred_ty<is_noninf> m_NonInf() { |
| 666 | return cstfp_pred_ty<is_noninf>(); |
| 667 | } |
| 668 | |
| 669 | struct is_finite { |
| 670 | bool isValue(const APFloat &C) { return C.isFinite(); } |
| 671 | }; |
| 672 | |
| 673 | |
| 674 | inline cstfp_pred_ty<is_finite> m_Finite() { |
| 675 | return cstfp_pred_ty<is_finite>(); |
| 676 | } |
| 677 | inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; } |
| 678 | |
| 679 | struct is_finitenonzero { |
| 680 | bool isValue(const APFloat &C) { return C.isFiniteNonZero(); } |
| 681 | }; |
| 682 | |
| 683 | |
| 684 | inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() { |
| 685 | return cstfp_pred_ty<is_finitenonzero>(); |
| 686 | } |
| 687 | inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) { |
| 688 | return V; |
| 689 | } |
| 690 | |
| 691 | struct is_any_zero_fp { |
| 692 | bool isValue(const APFloat &C) { return C.isZero(); } |
| 693 | }; |
| 694 | |
| 695 | |
| 696 | inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { |
| 697 | return cstfp_pred_ty<is_any_zero_fp>(); |
| 698 | } |
| 699 | |
| 700 | struct is_pos_zero_fp { |
| 701 | bool isValue(const APFloat &C) { return C.isPosZero(); } |
| 702 | }; |
| 703 | |
| 704 | |
| 705 | inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { |
| 706 | return cstfp_pred_ty<is_pos_zero_fp>(); |
| 707 | } |
| 708 | |
| 709 | struct is_neg_zero_fp { |
| 710 | bool isValue(const APFloat &C) { return C.isNegZero(); } |
| 711 | }; |
| 712 | |
| 713 | |
| 714 | inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { |
| 715 | return cstfp_pred_ty<is_neg_zero_fp>(); |
| 716 | } |
| 717 | |
| 718 | struct is_non_zero_fp { |
| 719 | bool isValue(const APFloat &C) { return C.isNonZero(); } |
| 720 | }; |
| 721 | |
| 722 | |
| 723 | inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() { |
| 724 | return cstfp_pred_ty<is_non_zero_fp>(); |
| 725 | } |
| 726 | |
| 727 | |
| 728 | |
| 729 | template <typename Class> struct bind_ty { |
| 730 | Class *&VR; |
| 731 | |
| 732 | bind_ty(Class *&V) : VR(V) {} |
| 733 | |
| 734 | template <typename ITy> bool match(ITy *V) { |
| 735 | if (auto *CV = dyn_cast<Class>(V)) { |
| 736 | VR = CV; |
| 737 | return true; |
| 738 | } |
| 739 | return false; |
| 740 | } |
| 741 | }; |
| 742 | |
| 743 | |
| 744 | inline bind_ty<Value> m_Value(Value *&V) { return V; } |
| 745 | inline bind_ty<const Value> m_Value(const Value *&V) { return V; } |
| 746 | |
| 747 | |
| 748 | inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } |
| 749 | |
| 750 | inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; } |
| 751 | |
| 752 | inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } |
| 753 | |
| 754 | inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; } |
| 755 | inline bind_ty<const WithOverflowInst> |
| 756 | m_WithOverflowInst(const WithOverflowInst *&I) { |
| 757 | return I; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } |
| 762 | |
| 763 | |
| 764 | inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } |
| 765 | |
| 766 | |
| 767 | inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } |
| 768 | |
| 769 | |
| 770 | inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; } |
| 771 | |
| 772 | |
| 773 | inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; } |
| 774 | inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) { |
| 775 | return V; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | inline match_combine_and<class_match<Constant>, |
| 780 | match_unless<class_match<ConstantExpr>>> |
| 781 | m_ImmConstant() { |
| 782 | return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr())); |
| 783 | } |
| 784 | |
| 785 | |
| 786 | inline match_combine_and<bind_ty<Constant>, |
| 787 | match_unless<class_match<ConstantExpr>>> |
| 788 | m_ImmConstant(Constant *&C) { |
| 789 | return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr())); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | struct specificval_ty { |
| 794 | const Value *Val; |
| 795 | |
| 796 | specificval_ty(const Value *V) : Val(V) {} |
| 797 | |
| 798 | template <typename ITy> bool match(ITy *V) { return V == Val; } |
| 799 | }; |
| 800 | |
| 801 | |
| 802 | inline specificval_ty m_Specific(const Value *V) { return V; } |
| 803 | |
| 804 | |
| 805 | |
| 806 | template <typename Class> struct deferredval_ty { |
| 807 | Class *const &Val; |
| 808 | |
| 809 | deferredval_ty(Class *const &V) : Val(V) {} |
| 810 | |
| 811 | template <typename ITy> bool match(ITy *const V) { return V == Val; } |
| 812 | }; |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } |
| 821 | inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { |
| 822 | return V; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | |
| 827 | struct specific_fpval { |
| 828 | double Val; |
| 829 | |
| 830 | specific_fpval(double V) : Val(V) {} |
| 831 | |
| 832 | template <typename ITy> bool match(ITy *V) { |
| 833 | if (const auto *CFP = dyn_cast<ConstantFP>(V)) |
| 834 | return CFP->isExactlyValue(Val); |
| 835 | if (V->getType()->isVectorTy()) |
| 836 | if (const auto *C = dyn_cast<Constant>(V)) |
| 837 | if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) |
| 838 | return CFP->isExactlyValue(Val); |
| 839 | return false; |
| 840 | } |
| 841 | }; |
| 842 | |
| 843 | |
| 844 | |
| 845 | inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } |
| 846 | |
| 847 | |
| 848 | inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } |
| 849 | |
| 850 | struct bind_const_intval_ty { |
| 851 | uint64_t &VR; |
| 852 | |
| 853 | bind_const_intval_ty(uint64_t &V) : VR(V) {} |
| 854 | |
| 855 | template <typename ITy> bool match(ITy *V) { |
| 856 | if (const auto *CV = dyn_cast<ConstantInt>(V)) |
| 857 | if (CV->getValue().ule(UINT64_MAX)) { |
| 858 | VR = CV->getZExtValue(); |
| 859 | return true; |
| 860 | } |
| 861 | return false; |
| 862 | } |
| 863 | }; |
| 864 | |
| 865 | |
| 866 | |
| 867 | template <bool AllowUndefs> |
| 868 | struct specific_intval { |
| 869 | APInt Val; |
| 870 | |
| 871 | specific_intval(APInt V) : Val(std::move(V)) {} |
| 872 | |
| 873 | template <typename ITy> bool match(ITy *V) { |
| 874 | const auto *CI = dyn_cast<ConstantInt>(V); |
| 875 | if (!CI && V->getType()->isVectorTy()) |
| 876 | if (const auto *C = dyn_cast<Constant>(V)) |
| 877 | CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs)); |
| 878 | |
| 879 | return CI && APInt::isSameValue(CI->getValue(), Val); |
| 880 | } |
| 881 | }; |
| 882 | |
| 883 | |
| 884 | |
| 885 | inline specific_intval<false> m_SpecificInt(APInt V) { |
| 886 | return specific_intval<false>(std::move(V)); |
| 887 | } |
| 888 | |
| 889 | inline specific_intval<false> m_SpecificInt(uint64_t V) { |
| 890 | return m_SpecificInt(APInt(64, V)); |
| 891 | } |
| 892 | |
| 893 | inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) { |
| 894 | return specific_intval<true>(std::move(V)); |
| 895 | } |
| 896 | |
| 897 | inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) { |
| 898 | return m_SpecificIntAllowUndef(APInt(64, V)); |
| 899 | } |
| 900 | |
| 901 | |
| 902 | |
| 903 | inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } |
| 904 | |
| 905 | |
| 906 | struct specific_bbval { |
| 907 | BasicBlock *Val; |
| 908 | |
| 909 | specific_bbval(BasicBlock *Val) : Val(Val) {} |
| 910 | |
| 911 | template <typename ITy> bool match(ITy *V) { |
| 912 | const auto *BB = dyn_cast<BasicBlock>(V); |
| 913 | return BB && BB == Val; |
| 914 | } |
| 915 | }; |
| 916 | |
| 917 | |
| 918 | inline specific_bbval m_SpecificBB(BasicBlock *BB) { |
| 919 | return specific_bbval(BB); |
| 920 | } |
| 921 | |
| 922 | |
| 923 | inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) { |
| 924 | return BB; |
| 925 | } |
| 926 | inline deferredval_ty<const BasicBlock> |
| 927 | m_Deferred(const BasicBlock *const &BB) { |
| 928 | return BB; |
| 929 | } |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | template <typename LHS_t, typename RHS_t, bool Commutable = false> |
| 935 | struct AnyBinaryOp_match { |
| 936 | LHS_t L; |
| 937 | RHS_t R; |
| 938 | |
| 939 | |
| 940 | |
| 941 | AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
| 942 | |
| 943 | template <typename OpTy> bool match(OpTy *V) { |
| 944 | if (auto *I = dyn_cast<BinaryOperator>(V)) |
| 945 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || |
| 946 | (Commutable && L.match(I->getOperand(1)) && |
| 947 | R.match(I->getOperand(0))); |
| 948 | return false; |
| 949 | } |
| 950 | }; |
| 951 | |
| 952 | template <typename LHS, typename RHS> |
| 953 | inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { |
| 954 | return AnyBinaryOp_match<LHS, RHS>(L, R); |
| 955 | } |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | template <typename OP_t> struct AnyUnaryOp_match { |
| 962 | OP_t X; |
| 963 | |
| 964 | AnyUnaryOp_match(const OP_t &X) : X(X) {} |
| 965 | |
| 966 | template <typename OpTy> bool match(OpTy *V) { |
| 967 | if (auto *I = dyn_cast<UnaryOperator>(V)) |
| 968 | return X.match(I->getOperand(0)); |
| 969 | return false; |
| 970 | } |
| 971 | }; |
| 972 | |
| 973 | template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) { |
| 974 | return AnyUnaryOp_match<OP_t>(X); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | template <typename LHS_t, typename RHS_t, unsigned Opcode, |
| 982 | bool Commutable = false> |
| 983 | struct BinaryOp_match { |
| 984 | LHS_t L; |
| 985 | RHS_t R; |
| 986 | |
| 987 | |
| 988 | |
| 989 | BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
| 990 | |
| 991 | template <typename OpTy> bool match(OpTy *V) { |
| 992 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
| 5 | | Assuming the condition is false | |
|
| |
| 993 | auto *I = cast<BinaryOperator>(V); |
| 994 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || |
| 995 | (Commutable && L.match(I->getOperand(1)) && |
| 996 | R.match(I->getOperand(0))); |
| 997 | } |
| 998 | if (auto *CE = dyn_cast<ConstantExpr>(V)) |
| 7 | | Assuming 'V' is not a 'ConstantExpr' | |
|
| |
| 999 | return CE->getOpcode() == Opcode && |
| 1000 | ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) || |
| 1001 | (Commutable && L.match(CE->getOperand(1)) && |
| 1002 | R.match(CE->getOperand(0)))); |
| 1003 | return false; |
| 9 | | Returning zero, which participates in a condition later | |
|
| 1004 | } |
| 1005 | }; |
| 1006 | |
| 1007 | template <typename LHS, typename RHS> |
| 1008 | inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, |
| 1009 | const RHS &R) { |
| 1010 | return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); |
| 1011 | } |
| 1012 | |
| 1013 | template <typename LHS, typename RHS> |
| 1014 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, |
| 1015 | const RHS &R) { |
| 1016 | return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); |
| 1017 | } |
| 1018 | |
| 1019 | template <typename LHS, typename RHS> |
| 1020 | inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, |
| 1021 | const RHS &R) { |
| 1022 | return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); |
| 1023 | } |
| 1024 | |
| 1025 | template <typename LHS, typename RHS> |
| 1026 | inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, |
| 1027 | const RHS &R) { |
| 1028 | return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); |
| 1029 | } |
| 1030 | |
| 1031 | template <typename Op_t> struct FNeg_match { |
| 1032 | Op_t X; |
| 1033 | |
| 1034 | FNeg_match(const Op_t &Op) : X(Op) {} |
| 1035 | template <typename OpTy> bool match(OpTy *V) { |
| 1036 | auto *FPMO = dyn_cast<FPMathOperator>(V); |
| 1037 | if (!FPMO) return false; |
| 1038 | |
| 1039 | if (FPMO->getOpcode() == Instruction::FNeg) |
| 1040 | return X.match(FPMO->getOperand(0)); |
| 1041 | |
| 1042 | if (FPMO->getOpcode() == Instruction::FSub) { |
| 1043 | if (FPMO->hasNoSignedZeros()) { |
| 1044 | |
| 1045 | if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) |
| 1046 | return false; |
| 1047 | } else { |
| 1048 | |
| 1049 | if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | return X.match(FPMO->getOperand(1)); |
| 1054 | } |
| 1055 | |
| 1056 | return false; |
| 1057 | } |
| 1058 | }; |
| 1059 | |
| 1060 | |
| 1061 | template <typename OpTy> |
| 1062 | inline FNeg_match<OpTy> |
| 1063 | m_FNeg(const OpTy &X) { |
| 1064 | return FNeg_match<OpTy>(X); |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | template <typename RHS> |
| 1069 | inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> |
| 1070 | m_FNegNSZ(const RHS &X) { |
| 1071 | return m_FSub(m_AnyZeroFP(), X); |
| 1072 | } |
| 1073 | |
| 1074 | template <typename LHS, typename RHS> |
| 1075 | inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, |
| 1076 | const RHS &R) { |
| 1077 | return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); |
| 1078 | } |
| 1079 | |
| 1080 | template <typename LHS, typename RHS> |
| 1081 | inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, |
| 1082 | const RHS &R) { |
| 1083 | return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); |
| 1084 | } |
| 1085 | |
| 1086 | template <typename LHS, typename RHS> |
| 1087 | inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, |
| 1088 | const RHS &R) { |
| 1089 | return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); |
| 1090 | } |
| 1091 | |
| 1092 | template <typename LHS, typename RHS> |
| 1093 | inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, |
| 1094 | const RHS &R) { |
| 1095 | return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); |
| 1096 | } |
| 1097 | |
| 1098 | template <typename LHS, typename RHS> |
| 1099 | inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, |
| 1100 | const RHS &R) { |
| 1101 | return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); |
| 1102 | } |
| 1103 | |
| 1104 | template <typename LHS, typename RHS> |
| 1105 | inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, |
| 1106 | const RHS &R) { |
| 1107 | return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); |
| 1108 | } |
| 1109 | |
| 1110 | template <typename LHS, typename RHS> |
| 1111 | inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, |
| 1112 | const RHS &R) { |
| 1113 | return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); |
| 1114 | } |
| 1115 | |
| 1116 | template <typename LHS, typename RHS> |
| 1117 | inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, |
| 1118 | const RHS &R) { |
| 1119 | return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); |
| 1120 | } |
| 1121 | |
| 1122 | template <typename LHS, typename RHS> |
| 1123 | inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, |
| 1124 | const RHS &R) { |
| 1125 | return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); |
| 1126 | } |
| 1127 | |
| 1128 | template <typename LHS, typename RHS> |
| 1129 | inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, |
| 1130 | const RHS &R) { |
| 1131 | return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); |
| 1132 | } |
| 1133 | |
| 1134 | template <typename LHS, typename RHS> |
| 1135 | inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, |
| 1136 | const RHS &R) { |
| 1137 | return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); |
| 1138 | } |
| 1139 | |
| 1140 | template <typename LHS, typename RHS> |
| 1141 | inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, |
| 1142 | const RHS &R) { |
| 1143 | return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); |
| 1144 | } |
| 1145 | |
| 1146 | template <typename LHS, typename RHS> |
| 1147 | inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, |
| 1148 | const RHS &R) { |
| 1149 | return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); |
| 1150 | } |
| 1151 | |
| 1152 | template <typename LHS, typename RHS> |
| 1153 | inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, |
| 1154 | const RHS &R) { |
| 1155 | return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); |
| 1156 | } |
| 1157 | |
| 1158 | template <typename LHS_t, typename RHS_t, unsigned Opcode, |
| 1159 | unsigned WrapFlags = 0> |
| 1160 | struct OverflowingBinaryOp_match { |
| 1161 | LHS_t L; |
| 1162 | RHS_t R; |
| 1163 | |
| 1164 | OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) |
| 1165 | : L(LHS), R(RHS) {} |
| 1166 | |
| 1167 | template <typename OpTy> bool match(OpTy *V) { |
| 1168 | if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { |
| 1169 | if (Op->getOpcode() != Opcode) |
| 1170 | return false; |
| 1171 | if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) && |
| 1172 | !Op->hasNoUnsignedWrap()) |
| 1173 | return false; |
| 1174 | if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) && |
| 1175 | !Op->hasNoSignedWrap()) |
| 1176 | return false; |
| 1177 | return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1)); |
| 1178 | } |
| 1179 | return false; |
| 1180 | } |
| 1181 | }; |
| 1182 | |
| 1183 | template <typename LHS, typename RHS> |
| 1184 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
| 1185 | OverflowingBinaryOperator::NoSignedWrap> |
| 1186 | m_NSWAdd(const LHS &L, const RHS &R) { |
| 1187 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
| 1188 | OverflowingBinaryOperator::NoSignedWrap>( |
| 1189 | L, R); |
| 1190 | } |
| 1191 | template <typename LHS, typename RHS> |
| 1192 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
| 1193 | OverflowingBinaryOperator::NoSignedWrap> |
| 1194 | m_NSWSub(const LHS &L, const RHS &R) { |
| 1195 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
| 1196 | OverflowingBinaryOperator::NoSignedWrap>( |
| 1197 | L, R); |
| 1198 | } |
| 1199 | template <typename LHS, typename RHS> |
| 1200 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
| 1201 | OverflowingBinaryOperator::NoSignedWrap> |
| 1202 | m_NSWMul(const LHS &L, const RHS &R) { |
| 1203 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
| 1204 | OverflowingBinaryOperator::NoSignedWrap>( |
| 1205 | L, R); |
| 1206 | } |
| 1207 | template <typename LHS, typename RHS> |
| 1208 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
| 1209 | OverflowingBinaryOperator::NoSignedWrap> |
| 1210 | m_NSWShl(const LHS &L, const RHS &R) { |
| 1211 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
| 1212 | OverflowingBinaryOperator::NoSignedWrap>( |
| 1213 | L, R); |
| 1214 | } |
| 1215 | |
| 1216 | template <typename LHS, typename RHS> |
| 1217 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
| 1218 | OverflowingBinaryOperator::NoUnsignedWrap> |
| 1219 | m_NUWAdd(const LHS &L, const RHS &R) { |
| 1220 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
| 1221 | OverflowingBinaryOperator::NoUnsignedWrap>( |
| 1222 | L, R); |
| 1223 | } |
| 1224 | template <typename LHS, typename RHS> |
| 1225 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
| 1226 | OverflowingBinaryOperator::NoUnsignedWrap> |
| 1227 | m_NUWSub(const LHS &L, const RHS &R) { |
| 1228 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
| 1229 | OverflowingBinaryOperator::NoUnsignedWrap>( |
| 1230 | L, R); |
| 1231 | } |
| 1232 | template <typename LHS, typename RHS> |
| 1233 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
| 1234 | OverflowingBinaryOperator::NoUnsignedWrap> |
| 1235 | m_NUWMul(const LHS &L, const RHS &R) { |
| 1236 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
| 1237 | OverflowingBinaryOperator::NoUnsignedWrap>( |
| 1238 | L, R); |
| 1239 | } |
| 1240 | template <typename LHS, typename RHS> |
| 1241 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
| 1242 | OverflowingBinaryOperator::NoUnsignedWrap> |
| 1243 | m_NUWShl(const LHS &L, const RHS &R) { |
| 1244 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
| 1245 | OverflowingBinaryOperator::NoUnsignedWrap>( |
| 1246 | L, R); |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | template <typename LHS_t, typename RHS_t, typename Predicate> |
| 1253 | struct BinOpPred_match : Predicate { |
| 1254 | LHS_t L; |
| 1255 | RHS_t R; |
| 1256 | |
| 1257 | BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
| 1258 | |
| 1259 | template <typename OpTy> bool match(OpTy *V) { |
| 1260 | if (auto *I = dyn_cast<Instruction>(V)) |
| 1261 | return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && |
| 1262 | R.match(I->getOperand(1)); |
| 1263 | if (auto *CE = dyn_cast<ConstantExpr>(V)) |
| 1264 | return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && |
| 1265 | R.match(CE->getOperand(1)); |
| 1266 | return false; |
| 1267 | } |
| 1268 | }; |
| 1269 | |
| 1270 | struct is_shift_op { |
| 1271 | bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } |
| 1272 | }; |
| 1273 | |
| 1274 | struct is_right_shift_op { |
| 1275 | bool isOpType(unsigned Opcode) { |
| 1276 | return Opcode == Instruction::LShr || Opcode == Instruction::AShr; |
| 1277 | } |
| 1278 | }; |
| 1279 | |
| 1280 | struct is_logical_shift_op { |
| 1281 | bool isOpType(unsigned Opcode) { |
| 1282 | return Opcode == Instruction::LShr || Opcode == Instruction::Shl; |
| 1283 | } |
| 1284 | }; |
| 1285 | |
| 1286 | struct is_bitwiselogic_op { |
| 1287 | bool isOpType(unsigned Opcode) { |
| 1288 | return Instruction::isBitwiseLogicOp(Opcode); |
| 1289 | } |
| 1290 | }; |
| 1291 | |
| 1292 | struct is_idiv_op { |
| 1293 | bool isOpType(unsigned Opcode) { |
| 1294 | return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; |
| 1295 | } |
| 1296 | }; |
| 1297 | |
| 1298 | struct is_irem_op { |
| 1299 | bool isOpType(unsigned Opcode) { |
| 1300 | return Opcode == Instruction::SRem || Opcode == Instruction::URem; |
| 1301 | } |
| 1302 | }; |
| 1303 | |
| 1304 | |
| 1305 | template <typename LHS, typename RHS> |
| 1306 | inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, |
| 1307 | const RHS &R) { |
| 1308 | return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | template <typename LHS, typename RHS> |
| 1313 | inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, |
| 1314 | const RHS &R) { |
| 1315 | return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | template <typename LHS, typename RHS> |
| 1320 | inline BinOpPred_match<LHS, RHS, is_logical_shift_op> |
| 1321 | m_LogicalShift(const LHS &L, const RHS &R) { |
| 1322 | return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | template <typename LHS, typename RHS> |
| 1327 | inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> |
| 1328 | m_BitwiseLogic(const LHS &L, const RHS &R) { |
| 1329 | return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | template <typename LHS, typename RHS> |
| 1334 | inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, |
| 1335 | const RHS &R) { |
| 1336 | return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | template <typename LHS, typename RHS> |
| 1341 | inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L, |
| 1342 | const RHS &R) { |
| 1343 | return BinOpPred_match<LHS, RHS, is_irem_op>(L, R); |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | template <typename SubPattern_t> struct Exact_match { |
| 1350 | SubPattern_t SubPattern; |
| 1351 | |
| 1352 | Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} |
| 1353 | |
| 1354 | template <typename OpTy> bool match(OpTy *V) { |
| 1355 | if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) |
| 1356 | return PEO->isExact() && SubPattern.match(V); |
| 1357 | return false; |
| 1358 | } |
| 1359 | }; |
| 1360 | |
| 1361 | template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { |
| 1362 | return SubPattern; |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy, |
| 1370 | bool Commutable = false> |
| 1371 | struct CmpClass_match { |
| 1372 | PredicateTy &Predicate; |
| 1373 | LHS_t L; |
| 1374 | RHS_t R; |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) |
| 1379 | : Predicate(Pred), L(LHS), R(RHS) {} |
| 1380 | |
| 1381 | template <typename OpTy> bool match(OpTy *V) { |
| 1382 | if (auto *I = dyn_cast<Class>(V)) { |
| 1383 | if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { |
| 1384 | Predicate = I->getPredicate(); |
| 1385 | return true; |
| 1386 | } else if (Commutable && L.match(I->getOperand(1)) && |
| 1387 | R.match(I->getOperand(0))) { |
| 1388 | Predicate = I->getSwappedPredicate(); |
| 1389 | return true; |
| 1390 | } |
| 1391 | } |
| 1392 | return false; |
| 1393 | } |
| 1394 | }; |
| 1395 | |
| 1396 | template <typename LHS, typename RHS> |
| 1397 | inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate> |
| 1398 | m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
| 1399 | return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R); |
| 1400 | } |
| 1401 | |
| 1402 | template <typename LHS, typename RHS> |
| 1403 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> |
| 1404 | m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
| 1405 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R); |
| 1406 | } |
| 1407 | |
| 1408 | template <typename LHS, typename RHS> |
| 1409 | inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> |
| 1410 | m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
| 1411 | return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R); |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | template <typename T0, unsigned Opcode> struct OneOps_match { |
| 1420 | T0 Op1; |
| 1421 | |
| 1422 | OneOps_match(const T0 &Op1) : Op1(Op1) {} |
| 1423 | |
| 1424 | template <typename OpTy> bool match(OpTy *V) { |
| 1425 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
| 1426 | auto *I = cast<Instruction>(V); |
| 1427 | return Op1.match(I->getOperand(0)); |
| 1428 | } |
| 1429 | return false; |
| 1430 | } |
| 1431 | }; |
| 1432 | |
| 1433 | |
| 1434 | template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { |
| 1435 | T0 Op1; |
| 1436 | T1 Op2; |
| 1437 | |
| 1438 | TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} |
| 1439 | |
| 1440 | template <typename OpTy> bool match(OpTy *V) { |
| 1441 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
| 1442 | auto *I = cast<Instruction>(V); |
| 1443 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); |
| 1444 | } |
| 1445 | return false; |
| 1446 | } |
| 1447 | }; |
| 1448 | |
| 1449 | |
| 1450 | template <typename T0, typename T1, typename T2, unsigned Opcode> |
| 1451 | struct ThreeOps_match { |
| 1452 | T0 Op1; |
| 1453 | T1 Op2; |
| 1454 | T2 Op3; |
| 1455 | |
| 1456 | ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) |
| 1457 | : Op1(Op1), Op2(Op2), Op3(Op3) {} |
| 1458 | |
| 1459 | template <typename OpTy> bool match(OpTy *V) { |
| 1460 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
| 1461 | auto *I = cast<Instruction>(V); |
| 1462 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && |
| 1463 | Op3.match(I->getOperand(2)); |
| 1464 | } |
| 1465 | return false; |
| 1466 | } |
| 1467 | }; |
| 1468 | |
| 1469 | |
| 1470 | template <typename Cond, typename LHS, typename RHS> |
| 1471 | inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> |
| 1472 | m_Select(const Cond &C, const LHS &L, const RHS &R) { |
| 1473 | return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | template <int64_t L, int64_t R, typename Cond> |
| 1479 | inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, |
| 1480 | Instruction::Select> |
| 1481 | m_SelectCst(const Cond &C) { |
| 1482 | return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); |
| 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | template <typename OpTy> |
| 1487 | inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) { |
| 1488 | return OneOps_match<OpTy, Instruction::Freeze>(Op); |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | template <typename Val_t, typename Elt_t, typename Idx_t> |
| 1493 | inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> |
| 1494 | m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { |
| 1495 | return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( |
| 1496 | Val, Elt, Idx); |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | template <typename Val_t, typename Idx_t> |
| 1501 | inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> |
| 1502 | m_ExtractElt(const Val_t &Val, const Idx_t &Idx) { |
| 1503 | return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | template <typename T0, typename T1, typename T2> struct Shuffle_match { |
| 1508 | T0 Op1; |
| 1509 | T1 Op2; |
| 1510 | T2 Mask; |
| 1511 | |
| 1512 | Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask) |
| 1513 | : Op1(Op1), Op2(Op2), Mask(Mask) {} |
| 1514 | |
| 1515 | template <typename OpTy> bool match(OpTy *V) { |
| 1516 | if (auto *I = dyn_cast<ShuffleVectorInst>(V)) { |
| 1517 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && |
| 1518 | Mask.match(I->getShuffleMask()); |
| 1519 | } |
| 1520 | return false; |
| 1521 | } |
| 1522 | }; |
| 1523 | |
| 1524 | struct m_Mask { |
| 1525 | ArrayRef<int> &MaskRef; |
| 1526 | m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} |
| 1527 | bool match(ArrayRef<int> Mask) { |
| 1528 | MaskRef = Mask; |
| 1529 | return true; |
| 1530 | } |
| 1531 | }; |
| 1532 | |
| 1533 | struct m_ZeroMask { |
| 1534 | bool match(ArrayRef<int> Mask) { |
| 1535 | return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; }); |
| 1536 | } |
| 1537 | }; |
| 1538 | |
| 1539 | struct m_SpecificMask { |
| 1540 | ArrayRef<int> &MaskRef; |
| 1541 | m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} |
| 1542 | bool match(ArrayRef<int> Mask) { return MaskRef == Mask; } |
| 1543 | }; |
| 1544 | |
| 1545 | struct m_SplatOrUndefMask { |
| 1546 | int &SplatIndex; |
| 1547 | m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {} |
| 1548 | bool match(ArrayRef<int> Mask) { |
| 1549 | auto First = find_if(Mask, [](int Elem) { return Elem != -1; }); |
| 1550 | if (First == Mask.end()) |
| 1551 | return false; |
| 1552 | SplatIndex = *First; |
| 1553 | return all_of(Mask, |
| 1554 | [First](int Elem) { return Elem == *First || Elem == -1; }); |
| 1555 | } |
| 1556 | }; |
| 1557 | |
| 1558 | |
| 1559 | template <typename V1_t, typename V2_t> |
| 1560 | inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector> |
| 1561 | m_Shuffle(const V1_t &v1, const V2_t &v2) { |
| 1562 | return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2); |
| 1563 | } |
| 1564 | |
| 1565 | template <typename V1_t, typename V2_t, typename Mask_t> |
| 1566 | inline Shuffle_match<V1_t, V2_t, Mask_t> |
| 1567 | m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) { |
| 1568 | return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask); |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | template <typename OpTy> |
| 1573 | inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { |
| 1574 | return OneOps_match<OpTy, Instruction::Load>(Op); |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | template <typename ValueOpTy, typename PointerOpTy> |
| 1579 | inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> |
| 1580 | m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { |
| 1581 | return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, |
| 1582 | PointerOp); |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | template <typename Op_t, unsigned Opcode> struct CastClass_match { |
| 1590 | Op_t Op; |
| 1591 | |
| 1592 | CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} |
| 1593 | |
| 1594 | template <typename OpTy> bool match(OpTy *V) { |
| 1595 | if (auto *O = dyn_cast<Operator>(V)) |
| 1596 | return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); |
| 1597 | return false; |
| 1598 | } |
| 1599 | }; |
| 1600 | |
| 1601 | |
| 1602 | template <typename OpTy> |
| 1603 | inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) { |
| 1604 | return CastClass_match<OpTy, Instruction::BitCast>(Op); |
| 1605 | } |
| 1606 | |
| 1607 | |
| 1608 | template <typename OpTy> |
| 1609 | inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) { |
| 1610 | return CastClass_match<OpTy, Instruction::PtrToInt>(Op); |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | template <typename OpTy> |
| 1615 | inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) { |
| 1616 | return CastClass_match<OpTy, Instruction::IntToPtr>(Op); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | template <typename OpTy> |
| 1621 | inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { |
| 1622 | return CastClass_match<OpTy, Instruction::Trunc>(Op); |
| 1623 | } |
| 1624 | |
| 1625 | template <typename OpTy> |
| 1626 | inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy> |
| 1627 | m_TruncOrSelf(const OpTy &Op) { |
| 1628 | return m_CombineOr(m_Trunc(Op), Op); |
| 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | template <typename OpTy> |
| 1633 | inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { |
| 1634 | return CastClass_match<OpTy, Instruction::SExt>(Op); |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | template <typename OpTy> |
| 1639 | inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { |
| 1640 | return CastClass_match<OpTy, Instruction::ZExt>(Op); |
| 1641 | } |
| 1642 | |
| 1643 | template <typename OpTy> |
| 1644 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy> |
| 1645 | m_ZExtOrSelf(const OpTy &Op) { |
| 1646 | return m_CombineOr(m_ZExt(Op), Op); |
| 1647 | } |
| 1648 | |
| 1649 | template <typename OpTy> |
| 1650 | inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy> |
| 1651 | m_SExtOrSelf(const OpTy &Op) { |
| 1652 | return m_CombineOr(m_SExt(Op), Op); |
| 1653 | } |
| 1654 | |
| 1655 | template <typename OpTy> |
| 1656 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, |
| 1657 | CastClass_match<OpTy, Instruction::SExt>> |
| 1658 | m_ZExtOrSExt(const OpTy &Op) { |
| 1659 | return m_CombineOr(m_ZExt(Op), m_SExt(Op)); |
| 1660 | } |
| 1661 | |
| 1662 | template <typename OpTy> |
| 1663 | inline match_combine_or< |
| 1664 | match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, |
| 1665 | CastClass_match<OpTy, Instruction::SExt>>, |
| 1666 | OpTy> |
| 1667 | m_ZExtOrSExtOrSelf(const OpTy &Op) { |
| 1668 | return m_CombineOr(m_ZExtOrSExt(Op), Op); |
| 1669 | } |
| 1670 | |
| 1671 | template <typename OpTy> |
| 1672 | inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { |
| 1673 | return CastClass_match<OpTy, Instruction::UIToFP>(Op); |
| 1674 | } |
| 1675 | |
| 1676 | template <typename OpTy> |
| 1677 | inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { |
| 1678 | return CastClass_match<OpTy, Instruction::SIToFP>(Op); |
| 1679 | } |
| 1680 | |
| 1681 | template <typename OpTy> |
| 1682 | inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) { |
| 1683 | return CastClass_match<OpTy, Instruction::FPToUI>(Op); |
| 1684 | } |
| 1685 | |
| 1686 | template <typename OpTy> |
| 1687 | inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) { |
| 1688 | return CastClass_match<OpTy, Instruction::FPToSI>(Op); |
| 1689 | } |
| 1690 | |
| 1691 | template <typename OpTy> |
| 1692 | inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) { |
| 1693 | return CastClass_match<OpTy, Instruction::FPTrunc>(Op); |
| 1694 | } |
| 1695 | |
| 1696 | template <typename OpTy> |
| 1697 | inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { |
| 1698 | return CastClass_match<OpTy, Instruction::FPExt>(Op); |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | struct br_match { |
| 1706 | BasicBlock *&Succ; |
| 1707 | |
| 1708 | br_match(BasicBlock *&Succ) : Succ(Succ) {} |
| 1709 | |
| 1710 | template <typename OpTy> bool match(OpTy *V) { |
| 1711 | if (auto *BI = dyn_cast<BranchInst>(V)) |
| 1712 | if (BI->isUnconditional()) { |
| 1713 | Succ = BI->getSuccessor(0); |
| 1714 | return true; |
| 1715 | } |
| 1716 | return false; |
| 1717 | } |
| 1718 | }; |
| 1719 | |
| 1720 | inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } |
| 1721 | |
| 1722 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> |
| 1723 | struct brc_match { |
| 1724 | Cond_t Cond; |
| 1725 | TrueBlock_t T; |
| 1726 | FalseBlock_t F; |
| 1727 | |
| 1728 | brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f) |
| 1729 | : Cond(C), T(t), F(f) {} |
| 1730 | |
| 1731 | template <typename OpTy> bool match(OpTy *V) { |
| 1732 | if (auto *BI = dyn_cast<BranchInst>(V)) |
| 1733 | if (BI->isConditional() && Cond.match(BI->getCondition())) |
| 1734 | return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1)); |
| 1735 | return false; |
| 1736 | } |
| 1737 | }; |
| 1738 | |
| 1739 | template <typename Cond_t> |
| 1740 | inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>> |
| 1741 | m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { |
| 1742 | return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>( |
| 1743 | C, m_BasicBlock(T), m_BasicBlock(F)); |
| 1744 | } |
| 1745 | |
| 1746 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> |
| 1747 | inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t> |
| 1748 | m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) { |
| 1749 | return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F); |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | |
| 1756 | template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, |
| 1757 | bool Commutable = false> |
| 1758 | struct MaxMin_match { |
| 1759 | using PredType = Pred_t; |
| 1760 | LHS_t L; |
| 1761 | RHS_t R; |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
| 1766 | |
| 1767 | template <typename OpTy> bool match(OpTy *V) { |
| 1768 | if (auto *II = dyn_cast<IntrinsicInst>(V)) { |
| 1769 | Intrinsic::ID IID = II->getIntrinsicID(); |
| 1770 | if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) || |
| 1771 | (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) || |
| 1772 | (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) || |
| 1773 | (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) { |
| 1774 | Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); |
| 1775 | return (L.match(LHS) && R.match(RHS)) || |
| 1776 | (Commutable && L.match(RHS) && R.match(LHS)); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | auto *SI = dyn_cast<SelectInst>(V); |
| 1781 | if (!SI) |
| 1782 | return false; |
| 1783 | auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); |
| 1784 | if (!Cmp) |
| 1785 | return false; |
| 1786 | |
| 1787 | |
| 1788 | auto *TrueVal = SI->getTrueValue(); |
| 1789 | auto *FalseVal = SI->getFalseValue(); |
| 1790 | auto *LHS = Cmp->getOperand(0); |
| 1791 | auto *RHS = Cmp->getOperand(1); |
| 1792 | if ((TrueVal != LHS || FalseVal != RHS) && |
| 1793 | (TrueVal != RHS || FalseVal != LHS)) |
| 1794 | return false; |
| 1795 | typename CmpInst_t::Predicate Pred = |
| 1796 | LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); |
| 1797 | |
| 1798 | if (!Pred_t::match(Pred)) |
| 1799 | return false; |
| 1800 | |
| 1801 | return (L.match(LHS) && R.match(RHS)) || |
| 1802 | (Commutable && L.match(RHS) && R.match(LHS)); |
| 1803 | } |
| 1804 | }; |
| 1805 | |
| 1806 | |
| 1807 | struct smax_pred_ty { |
| 1808 | static bool match(ICmpInst::Predicate Pred) { |
| 1809 | return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; |
| 1810 | } |
| 1811 | }; |
| 1812 | |
| 1813 | |
| 1814 | struct smin_pred_ty { |
| 1815 | static bool match(ICmpInst::Predicate Pred) { |
| 1816 | return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; |
| 1817 | } |
| 1818 | }; |
| 1819 | |
| 1820 | |
| 1821 | struct umax_pred_ty { |
| 1822 | static bool match(ICmpInst::Predicate Pred) { |
| 1823 | return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; |
| 1824 | } |
| 1825 | }; |
| 1826 | |
| 1827 | |
| 1828 | struct umin_pred_ty { |
| 1829 | static bool match(ICmpInst::Predicate Pred) { |
| 1830 | return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; |
| 1831 | } |
| 1832 | }; |
| 1833 | |
| 1834 | |
| 1835 | struct ofmax_pred_ty { |
| 1836 | static bool match(FCmpInst::Predicate Pred) { |
| 1837 | return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; |
| 1838 | } |
| 1839 | }; |
| 1840 | |
| 1841 | |
| 1842 | struct ofmin_pred_ty { |
| 1843 | static bool match(FCmpInst::Predicate Pred) { |
| 1844 | return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; |
| 1845 | } |
| 1846 | }; |
| 1847 | |
| 1848 | |
| 1849 | struct ufmax_pred_ty { |
| 1850 | static bool match(FCmpInst::Predicate Pred) { |
| 1851 | return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; |
| 1852 | } |
| 1853 | }; |
| 1854 | |
| 1855 | |
| 1856 | struct ufmin_pred_ty { |
| 1857 | static bool match(FCmpInst::Predicate Pred) { |
| 1858 | return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; |
| 1859 | } |
| 1860 | }; |
| 1861 | |
| 1862 | template <typename LHS, typename RHS> |
| 1863 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, |
| 1864 | const RHS &R) { |
| 1865 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); |
| 1866 | } |
| 1867 | |
| 1868 | template <typename LHS, typename RHS> |
| 1869 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, |
| 1870 | const RHS &R) { |
| 1871 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); |
| 1872 | } |
| 1873 | |
| 1874 | template <typename LHS, typename RHS> |
| 1875 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, |
| 1876 | const RHS &R) { |
| 1877 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); |
| 1878 | } |
| 1879 | |
| 1880 | template <typename LHS, typename RHS> |
| 1881 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, |
| 1882 | const RHS &R) { |
| 1883 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); |
| 1884 | } |
| 1885 | |
| 1886 | template <typename LHS, typename RHS> |
| 1887 | inline match_combine_or< |
| 1888 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>, |
| 1889 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>, |
| 1890 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>, |
| 1891 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>> |
| 1892 | m_MaxOrMin(const LHS &L, const RHS &R) { |
| 1893 | return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)), |
| 1894 | m_CombineOr(m_UMax(L, R), m_UMin(L, R))); |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | template <typename LHS, typename RHS> |
| 1907 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, |
| 1908 | const RHS &R) { |
| 1909 | return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); |
| 1910 | } |
| 1911 | |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | template <typename LHS, typename RHS> |
| 1922 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, |
| 1923 | const RHS &R) { |
| 1924 | return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | template <typename LHS, typename RHS> |
| 1937 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> |
| 1938 | m_UnordFMax(const LHS &L, const RHS &R) { |
| 1939 | return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); |
| 1940 | } |
| 1941 | |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | template <typename LHS, typename RHS> |
| 1952 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> |
| 1953 | m_UnordFMin(const LHS &L, const RHS &R) { |
| 1954 | return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); |
| 1955 | } |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | template <typename LHS_t, typename RHS_t, typename Sum_t> |
| 1963 | struct UAddWithOverflow_match { |
| 1964 | LHS_t L; |
| 1965 | RHS_t R; |
| 1966 | Sum_t S; |
| 1967 | |
| 1968 | UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) |
| 1969 | : L(L), R(R), S(S) {} |
| 1970 | |
| 1971 | template <typename OpTy> bool match(OpTy *V) { |
| 1972 | Value *ICmpLHS, *ICmpRHS; |
| 1973 | ICmpInst::Predicate Pred; |
| 1974 | if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) |
| 1975 | return false; |
| 1976 | |
| 1977 | Value *AddLHS, *AddRHS; |
| 1978 | auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); |
| 1979 | |
| 1980 | |
| 1981 | if (Pred == ICmpInst::ICMP_ULT) |
| 1982 | if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) |
| 1983 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); |
| 1984 | |
| 1985 | |
| 1986 | if (Pred == ICmpInst::ICMP_UGT) |
| 1987 | if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) |
| 1988 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); |
| 1989 | |
| 1990 | Value *Op1; |
| 1991 | auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes())); |
| 1992 | |
| 1993 | if (Pred == ICmpInst::ICMP_ULT) { |
| 1994 | if (XorExpr.match(ICmpLHS)) |
| 1995 | return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS); |
| 1996 | } |
| 1997 | |
| 1998 | if (Pred == ICmpInst::ICMP_UGT) { |
| 1999 | if (XorExpr.match(ICmpRHS)) |
| 2000 | return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS); |
| 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | if (Pred == ICmpInst::ICMP_EQ) { |
| 2005 | |
| 2006 | |
| 2007 | if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && |
| 2008 | (m_One().match(AddLHS) || m_One().match(AddRHS))) |
| 2009 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); |
| 2010 | |
| 2011 | |
| 2012 | if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && |
| 2013 | (m_One().match(AddLHS) || m_One().match(AddRHS))) |
| 2014 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); |
| 2015 | } |
| 2016 | |
| 2017 | return false; |
| 2018 | } |
| 2019 | }; |
| 2020 | |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | |
| 2025 | template <typename LHS_t, typename RHS_t, typename Sum_t> |
| 2026 | UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> |
| 2027 | m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { |
| 2028 | return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); |
| 2029 | } |
| 2030 | |
| 2031 | template <typename Opnd_t> struct Argument_match { |
| 2032 | unsigned OpI; |
| 2033 | Opnd_t Val; |
| 2034 | |
| 2035 | Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} |
| 2036 | |
| 2037 | template <typename OpTy> bool match(OpTy *V) { |
| 2038 | |
| 2039 | if (const auto *CI = dyn_cast<CallInst>(V)) |
| 2040 | return Val.match(CI->getArgOperand(OpI)); |
| 2041 | return false; |
| 2042 | } |
| 2043 | }; |
| 2044 | |
| 2045 | |
| 2046 | template <unsigned OpI, typename Opnd_t> |
| 2047 | inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { |
| 2048 | return Argument_match<Opnd_t>(OpI, Op); |
| 2049 | } |
| 2050 | |
| 2051 | |
| 2052 | struct IntrinsicID_match { |
| 2053 | unsigned ID; |
| 2054 | |
| 2055 | IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} |
| 2056 | |
| 2057 | template <typename OpTy> bool match(OpTy *V) { |
| 2058 | if (const auto *CI = dyn_cast<CallInst>(V)) |
| 2059 | if (const auto *F = CI->getCalledFunction()) |
| 2060 | return F->getIntrinsicID() == ID; |
| 2061 | return false; |
| 2062 | } |
| 2063 | }; |
| 2064 | |
| 2065 | |
| 2066 | |
| 2067 | |
| 2068 | |
| 2069 | template <typename T0 = void, typename T1 = void, typename T2 = void, |
| 2070 | typename T3 = void, typename T4 = void, typename T5 = void, |
| 2071 | typename T6 = void, typename T7 = void, typename T8 = void, |
| 2072 | typename T9 = void, typename T10 = void> |
| 2073 | struct m_Intrinsic_Ty; |
| 2074 | template <typename T0> struct m_Intrinsic_Ty<T0> { |
| 2075 | using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; |
| 2076 | }; |
| 2077 | template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { |
| 2078 | using Ty = |
| 2079 | match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; |
| 2080 | }; |
| 2081 | template <typename T0, typename T1, typename T2> |
| 2082 | struct m_Intrinsic_Ty<T0, T1, T2> { |
| 2083 | using Ty = |
| 2084 | match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, |
| 2085 | Argument_match<T2>>; |
| 2086 | }; |
| 2087 | template <typename T0, typename T1, typename T2, typename T3> |
| 2088 | struct m_Intrinsic_Ty<T0, T1, T2, T3> { |
| 2089 | using Ty = |
| 2090 | match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, |
| 2091 | Argument_match<T3>>; |
| 2092 | }; |
| 2093 | |
| 2094 | template <typename T0, typename T1, typename T2, typename T3, typename T4> |
| 2095 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> { |
| 2096 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty, |
| 2097 | Argument_match<T4>>; |
| 2098 | }; |
| 2099 | |
| 2100 | template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5> |
| 2101 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> { |
| 2102 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty, |
| 2103 | Argument_match<T5>>; |
| 2104 | }; |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { |
| 2109 | return IntrinsicID_match(IntrID); |
| 2110 | } |
| 2111 | |
| 2112 | |
| 2113 | template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> |
| 2114 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty |
| 2115 | m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, |
| 2116 | const Opnd3 &Op3) { |
| 2117 | return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3); |
| 2118 | } |
| 2119 | |
| 2120 | template <Intrinsic::ID IntrID, typename T0> |
| 2121 | inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { |
| 2122 | return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); |
| 2123 | } |
| 2124 | |
| 2125 | template <Intrinsic::ID IntrID, typename T0, typename T1> |
| 2126 | inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, |
| 2127 | const T1 &Op1) { |
| 2128 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); |
| 2129 | } |
| 2130 | |
| 2131 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> |
| 2132 | inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty |
| 2133 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { |
| 2134 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); |
| 2135 | } |
| 2136 | |
| 2137 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
| 2138 | typename T3> |
| 2139 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty |
| 2140 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { |
| 2141 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); |
| 2142 | } |
| 2143 | |
| 2144 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
| 2145 | typename T3, typename T4> |
| 2146 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty |
| 2147 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, |
| 2148 | const T4 &Op4) { |
| 2149 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3), |
| 2150 | m_Argument<4>(Op4)); |
| 2151 | } |
| 2152 | |
| 2153 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
| 2154 | typename T3, typename T4, typename T5> |
| 2155 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty |
| 2156 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, |
| 2157 | const T4 &Op4, const T5 &Op5) { |
| 2158 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4), |
| 2159 | m_Argument<5>(Op5)); |
| 2160 | } |
| 2161 | |
| 2162 | |
| 2163 | template <typename Opnd0> |
| 2164 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { |
| 2165 | return m_Intrinsic<Intrinsic::bitreverse>(Op0); |
| 2166 | } |
| 2167 | |
| 2168 | template <typename Opnd0> |
| 2169 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { |
| 2170 | return m_Intrinsic<Intrinsic::bswap>(Op0); |
| 2171 | } |
| 2172 | |
| 2173 | template <typename Opnd0> |
| 2174 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { |
| 2175 | return m_Intrinsic<Intrinsic::fabs>(Op0); |
| 2176 | } |
| 2177 | |
| 2178 | template <typename Opnd0> |
| 2179 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { |
| 2180 | return m_Intrinsic<Intrinsic::canonicalize>(Op0); |
| 2181 | } |
| 2182 | |
| 2183 | template <typename Opnd0, typename Opnd1> |
| 2184 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, |
| 2185 | const Opnd1 &Op1) { |
| 2186 | return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); |
| 2187 | } |
| 2188 | |
| 2189 | template <typename Opnd0, typename Opnd1> |
| 2190 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, |
| 2191 | const Opnd1 &Op1) { |
| 2192 | return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); |
| 2193 | } |
| 2194 | |
| 2195 | template <typename Opnd0, typename Opnd1, typename Opnd2> |
| 2196 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty |
| 2197 | m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { |
| 2198 | return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2); |
| 2199 | } |
| 2200 | |
| 2201 | template <typename Opnd0, typename Opnd1, typename Opnd2> |
| 2202 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty |
| 2203 | m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { |
| 2204 | return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2); |
| 2205 | } |
| 2206 | |
| 2207 | |
| 2208 | |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | template <typename LHS, typename RHS> |
| 2213 | inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { |
| 2214 | return AnyBinaryOp_match<LHS, RHS, true>(L, R); |
| 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | template <typename LHS, typename RHS> |
| 2220 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> |
| 2221 | m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
| 2222 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, |
| 2223 | R); |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | template <typename LHS, typename RHS> |
| 2228 | inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, |
| 2229 | const RHS &R) { |
| 2230 | return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); |
| 2231 | } |
| 2232 | |
| 2233 | |
| 2234 | template <typename LHS, typename RHS> |
| 2235 | inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, |
| 2236 | const RHS &R) { |
| 2237 | return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); |
| 2238 | } |
| 2239 | |
| 2240 | |
| 2241 | template <typename LHS, typename RHS> |
| 2242 | inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, |
| 2243 | const RHS &R) { |
| 2244 | return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); |
| 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | template <typename LHS, typename RHS> |
| 2249 | inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, |
| 2250 | const RHS &R) { |
| 2251 | return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); |
| 2252 | } |
| 2253 | |
| 2254 | |
| 2255 | template <typename LHS, typename RHS> |
| 2256 | inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, |
| 2257 | const RHS &R) { |
| 2258 | return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); |
| 2259 | } |
| 2260 | |
| 2261 | |
| 2262 | template <typename ValTy> |
| 2263 | inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> |
| 2264 | m_Neg(const ValTy &V) { |
| 2265 | return m_Sub(m_ZeroInt(), V); |
| 2266 | } |
| 2267 | |
| 2268 | |
| 2269 | template <typename ValTy> |
| 2270 | inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, |
| 2271 | Instruction::Sub, |
| 2272 | OverflowingBinaryOperator::NoSignedWrap> |
| 2273 | m_NSWNeg(const ValTy &V) { |
| 2274 | return m_NSWSub(m_ZeroInt(), V); |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | template <typename ValTy> |
| 2279 | inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true> |
| 2280 | m_Not(const ValTy &V) { |
| 2281 | return m_c_Xor(V, m_AllOnes()); |
| 2282 | } |
| 2283 | |
| 2284 | |
| 2285 | template <typename LHS, typename RHS> |
| 2286 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> |
| 2287 | m_c_SMin(const LHS &L, const RHS &R) { |
| 2288 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); |
| 2289 | } |
| 2290 | |
| 2291 | template <typename LHS, typename RHS> |
| 2292 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> |
| 2293 | m_c_SMax(const LHS &L, const RHS &R) { |
| 2294 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); |
| 2295 | } |
| 2296 | |
| 2297 | template <typename LHS, typename RHS> |
| 2298 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> |
| 2299 | m_c_UMin(const LHS &L, const RHS &R) { |
| 2300 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); |
| 2301 | } |
| 2302 | |
| 2303 | template <typename LHS, typename RHS> |
| 2304 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> |
| 2305 | m_c_UMax(const LHS &L, const RHS &R) { |
| 2306 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); |
| 2307 | } |
| 2308 | |
| 2309 | template <typename LHS, typename RHS> |
| 2310 | inline match_combine_or< |
| 2311 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>, |
| 2312 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>, |
| 2313 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>, |
| 2314 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>> |
| 2315 | m_c_MaxOrMin(const LHS &L, const RHS &R) { |
| 2316 | return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)), |
| 2317 | m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R))); |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | template <typename LHS, typename RHS> |
| 2322 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> |
| 2323 | m_c_FAdd(const LHS &L, const RHS &R) { |
| 2324 | return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); |
| 2325 | } |
| 2326 | |
| 2327 | |
| 2328 | template <typename LHS, typename RHS> |
| 2329 | inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> |
| 2330 | m_c_FMul(const LHS &L, const RHS &R) { |
| 2331 | return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); |
| 2332 | } |
| 2333 | |
| 2334 | template <typename Opnd_t> struct Signum_match { |
| 2335 | Opnd_t Val; |
| 2336 | Signum_match(const Opnd_t &V) : Val(V) {} |
| 2337 | |
| 2338 | template <typename OpTy> bool match(OpTy *V) { |
| 2339 | unsigned TypeSize = V->getType()->getScalarSizeInBits(); |
| 2340 | if (TypeSize == 0) |
| 2341 | return false; |
| 2342 | |
| 2343 | unsigned ShiftWidth = TypeSize - 1; |
| 2344 | Value *OpL = nullptr, *OpR = nullptr; |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | |
| 2356 | auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); |
| 2357 | auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); |
| 2358 | auto Signum = m_Or(LHS, RHS); |
| 2359 | |
| 2360 | return Signum.match(V) && OpL == OpR && Val.match(OpL); |
| 2361 | } |
| 2362 | }; |
| 2363 | |
| 2364 | |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | |
| 2370 | template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { |
| 2371 | return Signum_match<Val_t>(V); |
| 2372 | } |
| 2373 | |
| 2374 | template <int Ind, typename Opnd_t> struct ExtractValue_match { |
| 2375 | Opnd_t Val; |
| 2376 | ExtractValue_match(const Opnd_t &V) : Val(V) {} |
| 2377 | |
| 2378 | template <typename OpTy> bool match(OpTy *V) { |
| 2379 | if (auto *I = dyn_cast<ExtractValueInst>(V)) { |
| 2380 | |
| 2381 | if (Ind != -1 && |
| 2382 | !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind)) |
| 2383 | return false; |
| 2384 | return Val.match(I->getAggregateOperand()); |
| 2385 | } |
| 2386 | return false; |
| 2387 | } |
| 2388 | }; |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | template <int Ind, typename Val_t> |
| 2393 | inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) { |
| 2394 | return ExtractValue_match<Ind, Val_t>(V); |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | template <typename Val_t> |
| 2400 | inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) { |
| 2401 | return ExtractValue_match<-1, Val_t>(V); |
| 2402 | } |
| 2403 | |
| 2404 | |
| 2405 | template <int Ind, typename T0, typename T1> struct InsertValue_match { |
| 2406 | T0 Op0; |
| 2407 | T1 Op1; |
| 2408 | |
| 2409 | InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {} |
| 2410 | |
| 2411 | template <typename OpTy> bool match(OpTy *V) { |
| 2412 | if (auto *I = dyn_cast<InsertValueInst>(V)) { |
| 2413 | return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) && |
| 2414 | I->getNumIndices() == 1 && Ind == I->getIndices()[0]; |
| 2415 | } |
| 2416 | return false; |
| 2417 | } |
| 2418 | }; |
| 2419 | |
| 2420 | |
| 2421 | template <int Ind, typename Val_t, typename Elt_t> |
| 2422 | inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val, |
| 2423 | const Elt_t &Elt) { |
| 2424 | return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt); |
| 2425 | } |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | |
| 2431 | struct VScaleVal_match { |
| 2432 | const DataLayout &DL; |
| 2433 | VScaleVal_match(const DataLayout &DL) : DL(DL) {} |
| 2434 | |
| 2435 | template <typename ITy> bool match(ITy *V) { |
| 2436 | if (m_Intrinsic<Intrinsic::vscale>().match(V)) |
| 2437 | return true; |
| 2438 | |
| 2439 | Value *Ptr; |
| 2440 | if (m_PtrToInt(m_Value(Ptr)).match(V)) { |
| 2441 | if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { |
| 2442 | auto *DerefTy = GEP->getSourceElementType(); |
| 2443 | if (GEP->getNumIndices() == 1 && isa<ScalableVectorType>(DerefTy) && |
| 2444 | m_Zero().match(GEP->getPointerOperand()) && |
| 2445 | m_SpecificInt(1).match(GEP->idx_begin()->get()) && |
| 2446 | DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8) |
| 2447 | return true; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | return false; |
| 2452 | } |
| 2453 | }; |
| 2454 | |
| 2455 | inline VScaleVal_match m_VScale(const DataLayout &DL) { |
| 2456 | return VScaleVal_match(DL); |
| 2457 | } |
| 2458 | |
| 2459 | template <typename LHS, typename RHS, unsigned Opcode> |
| 2460 | struct LogicalOp_match { |
| 2461 | LHS L; |
| 2462 | RHS R; |
| 2463 | |
| 2464 | LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {} |
| 2465 | |
| 2466 | template <typename T> bool match(T *V) { |
| 2467 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 2468 | if (!I->getType()->isIntOrIntVectorTy(1)) |
| 2469 | return false; |
| 2470 | |
| 2471 | if (I->getOpcode() == Opcode && L.match(I->getOperand(0)) && |
| 2472 | R.match(I->getOperand(1))) |
| 2473 | return true; |
| 2474 | |
| 2475 | if (auto *SI = dyn_cast<SelectInst>(I)) { |
| 2476 | if (Opcode == Instruction::And) { |
| 2477 | if (const auto *C = dyn_cast<Constant>(SI->getFalseValue())) |
| 2478 | if (C->isNullValue() && L.match(SI->getCondition()) && |
| 2479 | R.match(SI->getTrueValue())) |
| 2480 | return true; |
| 2481 | } else { |
| 2482 | assert(Opcode == Instruction::Or); |
| 2483 | if (const auto *C = dyn_cast<Constant>(SI->getTrueValue())) |
| 2484 | if (C->isOneValue() && L.match(SI->getCondition()) && |
| 2485 | R.match(SI->getFalseValue())) |
| 2486 | return true; |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | return false; |
| 2492 | } |
| 2493 | }; |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | template <typename LHS, typename RHS> |
| 2498 | inline LogicalOp_match<LHS, RHS, Instruction::And> |
| 2499 | m_LogicalAnd(const LHS &L, const RHS &R) { |
| 2500 | return LogicalOp_match<LHS, RHS, Instruction::And>(L, R); |
| 2501 | } |
| 2502 | |
| 2503 | |
| 2504 | inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); } |
| 2505 | |
| 2506 | |
| 2507 | |
| 2508 | template <typename LHS, typename RHS> |
| 2509 | inline LogicalOp_match<LHS, RHS, Instruction::Or> |
| 2510 | m_LogicalOr(const LHS &L, const RHS &R) { |
| 2511 | return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R); |
| 2512 | } |
| 2513 | |
| 2514 | |
| 2515 | inline auto m_LogicalOr() { |
| 2516 | return m_LogicalOr(m_Value(), m_Value()); |
| 2517 | } |
| 2518 | |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | #endif // LLVM_IR_PATTERNMATCH_H |