clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Transforms/Utils -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Transforms/Utils -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/Transforms/Utils -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Transforms/Utils -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/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 | Value *Op0 = nullptr; |
1756 | DIExpression *SalvagedExpr = DII->getExpression(); |
1757 | auto LocItr = find(DIILocation, &I); |
1758 | while (SalvagedExpr && LocItr != DIILocation.end()) { |
1759 | SmallVector<uint64_t, 16> Ops; |
1760 | unsigned LocNo = std::distance(DIILocation.begin(), LocItr); |
1761 | uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands(); |
1762 | Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues); |
1763 | if (!Op0) |
1764 | break; |
1765 | SalvagedExpr = |
1766 | DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue); |
1767 | LocItr = std::find(++LocItr, DIILocation.end(), &I); |
1768 | } |
1769 | |
1770 | |
1771 | if (!Op0) |
1772 | break; |
1773 | |
1774 | DII->replaceVariableLocationOp(&I, Op0); |
1775 | if (AdditionalValues.empty()) { |
1776 | DII->setExpression(SalvagedExpr); |
1777 | } else if (isa<DbgValueInst>(DII) && |
1778 | DII->getNumVariableLocationOps() + AdditionalValues.size() <= |
1779 | MaxDebugArgs) { |
1780 | DII->addVariableLocationOps(AdditionalValues, SalvagedExpr); |
1781 | } else { |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | Value *Undef = UndefValue::get(I.getOperand(0)->getType()); |
1787 | DII->replaceVariableLocationOp(I.getOperand(0), Undef); |
1788 | } |
1789 | LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); |
1790 | Salvaged = true; |
1791 | } |
1792 | |
1793 | if (Salvaged) |
1794 | return; |
1795 | |
1796 | for (auto *DII : DbgUsers) { |
1797 | Value *Undef = UndefValue::get(I.getType()); |
1798 | DII->replaceVariableLocationOp(&I, Undef); |
1799 | } |
1800 | } |
1801 | |
1802 | Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL, |
1803 | uint64_t CurrentLocOps, |
1804 | SmallVectorImpl<uint64_t> &Opcodes, |
1805 | SmallVectorImpl<Value *> &AdditionalValues) { |
1806 | unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace()); |
1807 | |
1808 | MapVector<Value *, APInt> VariableOffsets; |
1809 | APInt ConstantOffset(BitWidth, 0); |
1810 | if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) |
1811 | return nullptr; |
1812 | if (!VariableOffsets.empty() && !CurrentLocOps) { |
1813 | Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0}); |
1814 | CurrentLocOps = 1; |
1815 | } |
1816 | for (auto Offset : VariableOffsets) { |
1817 | AdditionalValues.push_back(Offset.first); |
1818 | assert(Offset.second.isStrictlyPositive() && |
1819 | "Expected strictly positive multiplier for offset."); |
1820 | Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu, |
1821 | Offset.second.getZExtValue(), dwarf::DW_OP_mul, |
1822 | dwarf::DW_OP_plus}); |
1823 | } |
1824 | DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue()); |
1825 | return GEP->getOperand(0); |
1826 | } |
1827 | |
1828 | uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) { |
1829 | switch (Opcode) { |
1830 | case Instruction::Add: |
1831 | return dwarf::DW_OP_plus; |
1832 | case Instruction::Sub: |
1833 | return dwarf::DW_OP_minus; |
1834 | case Instruction::Mul: |
1835 | return dwarf::DW_OP_mul; |
1836 | case Instruction::SDiv: |
1837 | return dwarf::DW_OP_div; |
1838 | case Instruction::SRem: |
1839 | return dwarf::DW_OP_mod; |
1840 | case Instruction::Or: |
1841 | return dwarf::DW_OP_or; |
1842 | case Instruction::And: |
1843 | return dwarf::DW_OP_and; |
1844 | case Instruction::Xor: |
1845 | return dwarf::DW_OP_xor; |
1846 | case Instruction::Shl: |
1847 | return dwarf::DW_OP_shl; |
1848 | case Instruction::LShr: |
1849 | return dwarf::DW_OP_shr; |
1850 | case Instruction::AShr: |
1851 | return dwarf::DW_OP_shra; |
1852 | default: |
1853 | |
1854 | return 0; |
1855 | } |
1856 | } |
1857 | |
1858 | Value *getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps, |
1859 | SmallVectorImpl<uint64_t> &Opcodes, |
1860 | SmallVectorImpl<Value *> &AdditionalValues) { |
1861 | |
1862 | auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1)); |
1863 | |
1864 | if (ConstInt && ConstInt->getBitWidth() > 64) |
1865 | return nullptr; |
1866 | |
1867 | Instruction::BinaryOps BinOpcode = BI->getOpcode(); |
1868 | |
1869 | if (ConstInt) { |
1870 | uint64_t Val = ConstInt->getSExtValue(); |
1871 | |
1872 | |
1873 | if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) { |
1874 | uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val); |
1875 | DIExpression::appendOffset(Opcodes, Offset); |
1876 | return BI->getOperand(0); |
1877 | } |
1878 | Opcodes.append({dwarf::DW_OP_constu, Val}); |
1879 | } else { |
1880 | if (!CurrentLocOps) { |
1881 | Opcodes.append({dwarf::DW_OP_LLVM_arg, 0}); |
1882 | CurrentLocOps = 1; |
1883 | } |
1884 | Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps}); |
1885 | AdditionalValues.push_back(BI->getOperand(1)); |
1886 | } |
1887 | |
1888 | |
1889 | |
1890 | uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode); |
1891 | if (!DwarfBinOp) |
1892 | return nullptr; |
1893 | Opcodes.push_back(DwarfBinOp); |
1894 | return BI->getOperand(0); |
1895 | } |
1896 | |
1897 | Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, |
1898 | SmallVectorImpl<uint64_t> &Ops, |
1899 | SmallVectorImpl<Value *> &AdditionalValues) { |
1900 | auto &M = *I.getModule(); |
1901 | auto &DL = M.getDataLayout(); |
1902 | |
1903 | if (auto *CI = dyn_cast<CastInst>(&I)) { |
1904 | Value *FromValue = CI->getOperand(0); |
1905 | |
1906 | if (CI->isNoopCast(DL)) { |
1907 | return FromValue; |
1908 | } |
1909 | |
1910 | Type *Type = CI->getType(); |
1911 | |
1912 | if (Type->isVectorTy() || |
1913 | !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I))) |
1914 | return nullptr; |
1915 | |
1916 | unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits(); |
1917 | unsigned ToTypeBitSize = Type->getScalarSizeInBits(); |
1918 | |
1919 | auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize, |
1920 | isa<SExtInst>(&I)); |
1921 | Ops.append(ExtOps.begin(), ExtOps.end()); |
1922 | return FromValue; |
1923 | } |
1924 | |
1925 | if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) |
1926 | return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues); |
1927 | else if (auto *BI = dyn_cast<BinaryOperator>(&I)) { |
1928 | return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues); |
1929 | } |
1930 | |
1931 | |
1932 | |
1933 | return nullptr; |
1934 | } |
1935 | |
1936 | |
1937 | using DbgValReplacement = Optional<DIExpression *>; |
1938 | |
1939 | |
1940 | |
1941 | |
1942 | static bool rewriteDebugUsers( |
1943 | Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT, |
1944 | function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr) { |
1945 | |
1946 | SmallVector<DbgVariableIntrinsic *, 1> Users; |
1947 | findDbgUsers(Users, &From); |
1948 | if (Users.empty()) |
1949 | return false; |
1950 | |
1951 | |
1952 | bool Changed = false; |
1953 | SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage; |
1954 | if (isa<Instruction>(&To)) { |
1955 | bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint; |
1956 | |
1957 | for (auto *DII : Users) { |
1958 | |
1959 | |
1960 | if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) { |
1961 | LLVM_DEBUG(dbgs() << "MOVE: " << *DII << '\n'); |
1962 | DII->moveAfter(&DomPoint); |
1963 | Changed = true; |
1964 | |
1965 | |
1966 | |
1967 | } else if (!DT.dominates(&DomPoint, DII)) { |
1968 | UndefOrSalvage.insert(DII); |
1969 | } |
1970 | } |
1971 | } |
1972 | |
1973 | |
1974 | for (auto *DII : Users) { |
1975 | if (UndefOrSalvage.count(DII)) |
1976 | continue; |
1977 | |
1978 | DbgValReplacement DVR = RewriteExpr(*DII); |
1979 | if (!DVR) |
1980 | continue; |
1981 | |
1982 | DII->replaceVariableLocationOp(&From, &To); |
1983 | DII->setExpression(*DVR); |
1984 | LLVM_DEBUG(dbgs() << "REWRITE: " << *DII << '\n'); |
1985 | Changed = true; |
1986 | } |
1987 | |
1988 | if (!UndefOrSalvage.empty()) { |
1989 | |
1990 | salvageDebugInfo(From); |
1991 | Changed = true; |
1992 | } |
1993 | |
1994 | return Changed; |
1995 | } |
1996 | |
1997 | |
1998 | |
1999 | |
2000 | |
2001 | |
2002 | |
2003 | |
2004 | static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy, |
2005 | Type *ToTy) { |
2006 | |
2007 | if (FromTy == ToTy) |
2008 | return true; |
2009 | |
2010 | |
2011 | if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) { |
2012 | bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy); |
2013 | bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) && |
2014 | !DL.isNonIntegralPointerType(ToTy); |
2015 | return SameSize && LosslessConversion; |
2016 | } |
2017 | |
2018 | |
2019 | return false; |
2020 | } |
2021 | |
2022 | bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To, |
2023 | Instruction &DomPoint, DominatorTree &DT) { |
2024 | |
2025 | if (!From.isUsedByMetadata()) |
2026 | return false; |
2027 | |
2028 | assert(&From != &To && "Can't replace something with itself"); |
2029 | |
2030 | Type *FromTy = From.getType(); |
2031 | Type *ToTy = To.getType(); |
2032 | |
2033 | auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { |
2034 | return DII.getExpression(); |
2035 | }; |
2036 | |
2037 | |
2038 | Module &M = *From.getModule(); |
2039 | const DataLayout &DL = M.getDataLayout(); |
2040 | if (isBitCastSemanticsPreserving(DL, FromTy, ToTy)) |
2041 | return rewriteDebugUsers(From, To, DomPoint, DT, Identity); |
2042 | |
2043 | |
2044 | |
2045 | if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) { |
2046 | uint64_t FromBits = FromTy->getPrimitiveSizeInBits(); |
2047 | uint64_t ToBits = ToTy->getPrimitiveSizeInBits(); |
2048 | assert(FromBits != ToBits && "Unexpected no-op conversion"); |
2049 | |
2050 | |
2051 | |
2052 | if (FromBits < ToBits) |
2053 | return rewriteDebugUsers(From, To, DomPoint, DT, Identity); |
2054 | |
2055 | |
2056 | |
2057 | auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { |
2058 | DILocalVariable *Var = DII.getVariable(); |
2059 | |
2060 | |
2061 | auto Signedness = Var->getSignedness(); |
2062 | if (!Signedness) |
2063 | return None; |
2064 | |
2065 | bool Signed = *Signedness == DIBasicType::Signedness::Signed; |
2066 | return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits, |
2067 | Signed); |
2068 | }; |
2069 | return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt); |
2070 | } |
2071 | |
2072 | |
2073 | return false; |
2074 | } |
2075 | |
2076 | std::pair<unsigned, unsigned> |
2077 | llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) { |
2078 | unsigned NumDeadInst = 0; |
2079 | unsigned NumDeadDbgInst = 0; |
2080 | |
2081 | |
2082 | Instruction *EndInst = BB->getTerminator(); |
2083 | while (EndInst != &BB->front()) { |
2084 | |
2085 | Instruction *Inst = &*--EndInst->getIterator(); |
2086 | if (!Inst->use_empty() && !Inst->getType()->isTokenTy()) |
2087 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
2088 | if (Inst->isEHPad() || Inst->getType()->isTokenTy()) { |
2089 | EndInst = Inst; |
2090 | continue; |
2091 | } |
2092 | if (isa<DbgInfoIntrinsic>(Inst)) |
2093 | ++NumDeadDbgInst; |
2094 | else |
2095 | ++NumDeadInst; |
2096 | Inst->eraseFromParent(); |
2097 | } |
2098 | return {NumDeadInst, NumDeadDbgInst}; |
2099 | } |
2100 | |
2101 | unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA, |
2102 | DomTreeUpdater *DTU, |
2103 | MemorySSAUpdater *MSSAU) { |
2104 | BasicBlock *BB = I->getParent(); |
2105 | |
2106 | if (MSSAU) |
2107 | MSSAU->changeToUnreachable(I); |
2108 | |
2109 | SmallSet<BasicBlock *, 8> UniqueSuccessors; |
2110 | |
2111 | |
2112 | |
2113 | for (BasicBlock *Successor : successors(BB)) { |
2114 | Successor->removePredecessor(BB, PreserveLCSSA); |
2115 | if (DTU) |
2116 | UniqueSuccessors.insert(Successor); |
2117 | } |
2118 | auto *UI = new UnreachableInst(I->getContext(), I); |
2119 | UI->setDebugLoc(I->getDebugLoc()); |
2120 | |
2121 | |
2122 | unsigned NumInstrsRemoved = 0; |
2123 | BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end(); |
2124 | while (BBI != BBE) { |
2125 | if (!BBI->use_empty()) |
2126 | BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); |
2127 | BB->getInstList().erase(BBI++); |
2128 | ++NumInstrsRemoved; |
2129 | } |
2130 | if (DTU) { |
2131 | SmallVector<DominatorTree::UpdateType, 8> Updates; |
2132 | Updates.reserve(UniqueSuccessors.size()); |
2133 | for (BasicBlock *UniqueSuccessor : UniqueSuccessors) |
2134 | Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor}); |
2135 | DTU->applyUpdates(Updates); |
2136 | } |
2137 | return NumInstrsRemoved; |
2138 | } |
2139 | |
2140 | CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) { |
2141 | SmallVector<Value *, 8> Args(II->args()); |
2142 | SmallVector<OperandBundleDef, 1> OpBundles; |
2143 | II->getOperandBundlesAsDefs(OpBundles); |
2144 | CallInst *NewCall = CallInst::Create(II->getFunctionType(), |
2145 | II->getCalledOperand(), Args, OpBundles); |
2146 | NewCall->setCallingConv(II->getCallingConv()); |
2147 | NewCall->setAttributes(II->getAttributes()); |
2148 | NewCall->setDebugLoc(II->getDebugLoc()); |
2149 | NewCall->copyMetadata(*II); |
2150 | |
2151 | |
2152 | uint64_t TotalWeight; |
2153 | if (NewCall->extractProfTotalWeight(TotalWeight)) { |
2154 | |
2155 | MDBuilder MDB(NewCall->getContext()); |
2156 | auto NewWeights = uint32_t(TotalWeight) != TotalWeight |
2157 | ? nullptr |
2158 | : MDB.createBranchWeights({uint32_t(TotalWeight)}); |
2159 | NewCall->setMetadata(LLVMContext::MD_prof, NewWeights); |
2160 | } |
2161 | |
2162 | return NewCall; |
2163 | } |
2164 | |
2165 | |
2166 | void llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) { |
2167 | CallInst *NewCall = createCallMatchingInvoke(II); |
2168 | NewCall->takeName(II); |
2169 | NewCall->insertBefore(II); |
2170 | II->replaceAllUsesWith(NewCall); |
2171 | |
2172 | |
2173 | BasicBlock *NormalDestBB = II->getNormalDest(); |
2174 | BranchInst::Create(NormalDestBB, II); |
2175 | |
2176 | |
2177 | BasicBlock *BB = II->getParent(); |
2178 | BasicBlock *UnwindDestBB = II->getUnwindDest(); |
2179 | UnwindDestBB->removePredecessor(BB); |
2180 | II->eraseFromParent(); |
2181 | if (DTU) |
2182 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); |
2183 | } |
2184 | |
2185 | BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI, |
2186 | BasicBlock *UnwindEdge, |
2187 | DomTreeUpdater *DTU) { |
2188 | BasicBlock *BB = CI->getParent(); |
2189 | |
2190 | |
2191 | |
2192 | BasicBlock *Split = SplitBlock(BB, CI, DTU, nullptr, nullptr, |
2193 | CI->getName() + ".noexc"); |
2194 | |
2195 | |
2196 | BB->getInstList().pop_back(); |
2197 | |
2198 | |
2199 | SmallVector<Value *, 8> InvokeArgs(CI->args()); |
2200 | SmallVector<OperandBundleDef, 1> OpBundles; |
2201 | |
2202 | CI->getOperandBundlesAsDefs(OpBundles); |
2203 | |
2204 | |
2205 | |
2206 | |
2207 | |
2208 | InvokeInst *II = |
2209 | InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split, |
2210 | UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB); |
2211 | II->setDebugLoc(CI->getDebugLoc()); |
2212 | II->setCallingConv(CI->getCallingConv()); |
2213 | II->setAttributes(CI->getAttributes()); |
2214 | |
2215 | if (DTU) |
2216 | DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}}); |
2217 | |
2218 | |
2219 | |
2220 | CI->replaceAllUsesWith(II); |
2221 | |
2222 | |
2223 | Split->getInstList().pop_front(); |
2224 | return Split; |
2225 | } |
2226 | |
2227 | static bool markAliveBlocks(Function &F, |
2228 | SmallPtrSetImpl<BasicBlock *> &Reachable, |
2229 | DomTreeUpdater *DTU = nullptr) { |
2230 | SmallVector<BasicBlock*, 128> Worklist; |
2231 | BasicBlock *BB = &F.front(); |
2232 | Worklist.push_back(BB); |
2233 | Reachable.insert(BB); |
2234 | bool Changed = false; |
2235 | do { |
2236 | BB = Worklist.pop_back_val(); |
2237 | |
2238 | |
2239 | |
2240 | |
2241 | for (Instruction &I : *BB) { |
2242 | if (auto *CI = dyn_cast<CallInst>(&I)) { |
2243 | Value *Callee = CI->getCalledOperand(); |
2244 | |
2245 | if (Function *F = dyn_cast<Function>(Callee)) { |
2246 | auto IntrinsicID = F->getIntrinsicID(); |
2247 | |
2248 | |
2249 | |
2250 | |
2251 | if (IntrinsicID == Intrinsic::assume) { |
2252 | if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) { |
2253 | |
2254 | changeToUnreachable(CI, false, DTU); |
2255 | Changed = true; |
2256 | break; |
2257 | } |
2258 | } else if (IntrinsicID == Intrinsic::experimental_guard) { |
2259 | |
2260 | |
2261 | |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | if (match(CI->getArgOperand(0), m_Zero())) |
2269 | if (!isa<UnreachableInst>(CI->getNextNode())) { |
2270 | changeToUnreachable(CI->getNextNode(), false, DTU); |
2271 | Changed = true; |
2272 | break; |
2273 | } |
2274 | } |
2275 | } else if ((isa<ConstantPointerNull>(Callee) && |
2276 | !NullPointerIsDefined(CI->getFunction())) || |
2277 | isa<UndefValue>(Callee)) { |
2278 | changeToUnreachable(CI, false, DTU); |
2279 | Changed = true; |
2280 | break; |
2281 | } |
2282 | if (CI->doesNotReturn() && !CI->isMustTailCall()) { |
2283 | |
2284 | |
2285 | |
2286 | if (!isa<UnreachableInst>(CI->getNextNode())) { |
2287 | |
2288 | changeToUnreachable(CI->getNextNode(), false, DTU); |
2289 | Changed = true; |
2290 | } |
2291 | break; |
2292 | } |
2293 | } else if (auto *SI = dyn_cast<StoreInst>(&I)) { |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | |
2299 | if (SI->isVolatile()) continue; |
2300 | |
2301 | Value *Ptr = SI->getOperand(1); |
2302 | |
2303 | if (isa<UndefValue>(Ptr) || |
2304 | (isa<ConstantPointerNull>(Ptr) && |
2305 | !NullPointerIsDefined(SI->getFunction(), |
2306 | SI->getPointerAddressSpace()))) { |
2307 | changeToUnreachable(SI, false, DTU); |
2308 | Changed = true; |
2309 | break; |
2310 | } |
2311 | } |
2312 | } |
2313 | |
2314 | Instruction *Terminator = BB->getTerminator(); |
2315 | if (auto *II = dyn_cast<InvokeInst>(Terminator)) { |
2316 | |
2317 | Value *Callee = II->getCalledOperand(); |
2318 | if ((isa<ConstantPointerNull>(Callee) && |
2319 | !NullPointerIsDefined(BB->getParent())) || |
2320 | isa<UndefValue>(Callee)) { |
2321 | changeToUnreachable(II, false, DTU); |
2322 | Changed = true; |
2323 | } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) { |
2324 | if (II->use_empty() && II->onlyReadsMemory()) { |
2325 | |
2326 | BasicBlock *NormalDestBB = II->getNormalDest(); |
2327 | BasicBlock *UnwindDestBB = II->getUnwindDest(); |
2328 | BranchInst::Create(NormalDestBB, II); |
2329 | UnwindDestBB->removePredecessor(II->getParent()); |
2330 | II->eraseFromParent(); |
2331 | if (DTU) |
2332 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); |
2333 | } else |
2334 | changeToCall(II, DTU); |
2335 | Changed = true; |
2336 | } |
2337 | } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) { |
2338 | |
2339 | struct CatchPadDenseMapInfo { |
2340 | static CatchPadInst *getEmptyKey() { |
2341 | return DenseMapInfo<CatchPadInst *>::getEmptyKey(); |
2342 | } |
2343 | |
2344 | static CatchPadInst *getTombstoneKey() { |
2345 | return DenseMapInfo<CatchPadInst *>::getTombstoneKey(); |
2346 | } |
2347 | |
2348 | static unsigned getHashValue(CatchPadInst *CatchPad) { |
2349 | return static_cast<unsigned>(hash_combine_range( |
2350 | CatchPad->value_op_begin(), CatchPad->value_op_end())); |
2351 | } |
2352 | |
2353 | static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) { |
2354 | if (LHS == getEmptyKey() || LHS == getTombstoneKey() || |
2355 | RHS == getEmptyKey() || RHS == getTombstoneKey()) |
2356 | return LHS == RHS; |
2357 | return LHS->isIdenticalTo(RHS); |
2358 | } |
2359 | }; |
2360 | |
2361 | SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases; |
2362 | |
2363 | SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4, |
2364 | CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>> |
2365 | HandlerSet; |
2366 | detail::DenseSetEmpty Empty; |
2367 | for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(), |
2368 | E = CatchSwitch->handler_end(); |
2369 | I != E; ++I) { |
2370 | BasicBlock *HandlerBB = *I; |
2371 | if (DTU) |
2372 | ++NumPerSuccessorCases[HandlerBB]; |
2373 | auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI()); |
2374 | if (!HandlerSet.insert({CatchPad, Empty}).second) { |
2375 | if (DTU) |
2376 | --NumPerSuccessorCases[HandlerBB]; |
2377 | CatchSwitch->removeHandler(I); |
2378 | --I; |
2379 | --E; |
2380 | Changed = true; |
2381 | } |
2382 | } |
2383 | if (DTU) { |
2384 | std::vector<DominatorTree::UpdateType> Updates; |
2385 | for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases) |
2386 | if (I.second == 0) |
2387 | Updates.push_back({DominatorTree::Delete, BB, I.first}); |
2388 | DTU->applyUpdates(Updates); |
2389 | } |
2390 | } |
2391 | |
2392 | Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU); |
2393 | for (BasicBlock *Successor : successors(BB)) |
2394 | if (Reachable.insert(Successor).second) |
2395 | Worklist.push_back(Successor); |
2396 | } while (!Worklist.empty()); |
2397 | return Changed; |
2398 | } |
2399 | |
2400 | void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) { |
2401 | Instruction *TI = BB->getTerminator(); |
2402 | |
2403 | if (auto *II = dyn_cast<InvokeInst>(TI)) { |
2404 | changeToCall(II, DTU); |
2405 | return; |
2406 | } |
2407 | |
2408 | Instruction *NewTI; |
2409 | BasicBlock *UnwindDest; |
2410 | |
2411 | if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) { |
2412 | NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI); |
2413 | UnwindDest = CRI->getUnwindDest(); |
2414 | } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { |
2415 | auto *NewCatchSwitch = CatchSwitchInst::Create( |
2416 | CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(), |
2417 | CatchSwitch->getName(), CatchSwitch); |
2418 | for (BasicBlock *PadBB : CatchSwitch->handlers()) |
2419 | NewCatchSwitch->addHandler(PadBB); |
2420 | |
2421 | NewTI = NewCatchSwitch; |
2422 | UnwindDest = CatchSwitch->getUnwindDest(); |
2423 | } else { |
2424 | llvm_unreachable("Could not find unwind successor"); |
2425 | } |
2426 | |
2427 | NewTI->takeName(TI); |
2428 | NewTI->setDebugLoc(TI->getDebugLoc()); |
2429 | UnwindDest->removePredecessor(BB); |
2430 | TI->replaceAllUsesWith(NewTI); |
2431 | TI->eraseFromParent(); |
2432 | if (DTU) |
2433 | DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}}); |
2434 | } |
2435 | |
2436 | |
2437 | |
2438 | |
2439 | bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU, |
2440 | MemorySSAUpdater *MSSAU) { |
2441 | SmallPtrSet<BasicBlock *, 16> Reachable; |
2442 | bool Changed = markAliveBlocks(F, Reachable, DTU); |
2443 | |
2444 | |
2445 | if (Reachable.size() == F.size()) |
2446 | return Changed; |
2447 | |
2448 | assert(Reachable.size() < F.size()); |
2449 | |
2450 | |
2451 | SmallSetVector<BasicBlock *, 8> BlocksToRemove; |
2452 | for (BasicBlock &BB : F) { |
2453 | |
2454 | if (Reachable.count(&BB)) |
2455 | continue; |
2456 | |
2457 | if (DTU && DTU->isBBPendingDeletion(&BB)) |
2458 | continue; |
2459 | BlocksToRemove.insert(&BB); |
2460 | } |
2461 | |
2462 | if (BlocksToRemove.empty()) |
2463 | return Changed; |
2464 | |
2465 | Changed = true; |
2466 | NumRemoved += BlocksToRemove.size(); |
2467 | |
2468 | if (MSSAU) |
2469 | MSSAU->removeBlocks(BlocksToRemove); |
2470 | |
2471 | DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU); |
2472 | |
2473 | return Changed; |
2474 | } |
2475 | |
2476 | void llvm::combineMetadata(Instruction *K, const Instruction *J, |
2477 | ArrayRef<unsigned> KnownIDs, bool DoesKMove) { |
2478 | SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; |
2479 | K->dropUnknownNonDebugMetadata(KnownIDs); |
2480 | K->getAllMetadataOtherThanDebugLoc(Metadata); |
2481 | for (const auto &MD : Metadata) { |
2482 | unsigned Kind = MD.first; |
2483 | MDNode *JMD = J->getMetadata(Kind); |
2484 | MDNode *KMD = MD.second; |
2485 | |
2486 | switch (Kind) { |
2487 | default: |
2488 | K->setMetadata(Kind, nullptr); |
2489 | break; |
2490 | case LLVMContext::MD_dbg: |
2491 | llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg"); |
2492 | case LLVMContext::MD_tbaa: |
2493 | K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD)); |
2494 | break; |
2495 | case LLVMContext::MD_alias_scope: |
2496 | K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD)); |
2497 | break; |
2498 | case LLVMContext::MD_noalias: |
2499 | case LLVMContext::MD_mem_parallel_loop_access: |
2500 | K->setMetadata(Kind, MDNode::intersect(JMD, KMD)); |
2501 | break; |
2502 | case LLVMContext::MD_access_group: |
2503 | K->setMetadata(LLVMContext::MD_access_group, |
2504 | intersectAccessGroups(K, J)); |
2505 | break; |
2506 | case LLVMContext::MD_range: |
2507 | |
2508 | |
2509 | |
2510 | if (DoesKMove) |
2511 | |
2512 | |
2513 | |
2514 | |
2515 | K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD)); |
2516 | break; |
2517 | case LLVMContext::MD_fpmath: |
2518 | K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); |
2519 | break; |
2520 | case LLVMContext::MD_invariant_load: |
2521 | |
2522 | K->setMetadata(Kind, JMD); |
2523 | break; |
2524 | case LLVMContext::MD_nonnull: |
2525 | |
2526 | if (DoesKMove) |
2527 | K->setMetadata(Kind, JMD); |
2528 | break; |
2529 | case LLVMContext::MD_invariant_group: |
2530 | |
2531 | break; |
2532 | case LLVMContext::MD_align: |
2533 | K->setMetadata(Kind, |
2534 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); |
2535 | break; |
2536 | case LLVMContext::MD_dereferenceable: |
2537 | case LLVMContext::MD_dereferenceable_or_null: |
2538 | K->setMetadata(Kind, |
2539 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); |
2540 | break; |
2541 | case LLVMContext::MD_preserve_access_index: |
2542 | |
2543 | break; |
2544 | } |
2545 | } |
2546 | |
2547 | |
2548 | |
2549 | |
2550 | |
2551 | |
2552 | if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group)) |
2553 | if (isa<LoadInst>(K) || isa<StoreInst>(K)) |
2554 | K->setMetadata(LLVMContext::MD_invariant_group, JMD); |
2555 | } |
2556 | |
2557 | void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J, |
2558 | bool KDominatesJ) { |
2559 | unsigned KnownIDs[] = { |
2560 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
2561 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
2562 | LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull, |
2563 | LLVMContext::MD_invariant_group, LLVMContext::MD_align, |
2564 | LLVMContext::MD_dereferenceable, |
2565 | LLVMContext::MD_dereferenceable_or_null, |
2566 | LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; |
2567 | combineMetadata(K, J, KnownIDs, KDominatesJ); |
2568 | } |
2569 | |
2570 | void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) { |
2571 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
2572 | Source.getAllMetadata(MD); |
2573 | MDBuilder MDB(Dest.getContext()); |
2574 | Type *NewType = Dest.getType(); |
2575 | const DataLayout &DL = Source.getModule()->getDataLayout(); |
2576 | for (const auto &MDPair : MD) { |
2577 | unsigned ID = MDPair.first; |
2578 | MDNode *N = MDPair.second; |
2579 | |
2580 | |
2581 | |
2582 | |
2583 | |
2584 | |
2585 | |
2586 | switch (ID) { |
2587 | case LLVMContext::MD_dbg: |
2588 | case LLVMContext::MD_tbaa: |
2589 | case LLVMContext::MD_prof: |
2590 | case LLVMContext::MD_fpmath: |
2591 | case LLVMContext::MD_tbaa_struct: |
2592 | case LLVMContext::MD_invariant_load: |
2593 | case LLVMContext::MD_alias_scope: |
2594 | case LLVMContext::MD_noalias: |
2595 | case LLVMContext::MD_nontemporal: |
2596 | case LLVMContext::MD_mem_parallel_loop_access: |
2597 | case LLVMContext::MD_access_group: |
2598 | |
2599 | Dest.setMetadata(ID, N); |
2600 | break; |
2601 | |
2602 | case LLVMContext::MD_nonnull: |
2603 | copyNonnullMetadata(Source, N, Dest); |
2604 | break; |
2605 | |
2606 | case LLVMContext::MD_align: |
2607 | case LLVMContext::MD_dereferenceable: |
2608 | case LLVMContext::MD_dereferenceable_or_null: |
2609 | |
2610 | if (NewType->isPointerTy()) |
2611 | Dest.setMetadata(ID, N); |
2612 | break; |
2613 | |
2614 | case LLVMContext::MD_range: |
2615 | copyRangeMetadata(DL, Source, N, Dest); |
2616 | break; |
2617 | } |
2618 | } |
2619 | } |
2620 | |
2621 | void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) { |
2622 | auto *ReplInst = dyn_cast<Instruction>(Repl); |
2623 | if (!ReplInst) |
2624 | return; |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | |
2630 | |
2631 | |
2632 | if (!isa<LoadInst>(I)) |
2633 | ReplInst->andIRFlags(I); |
2634 | |
2635 | |
2636 | |
2637 | |
2638 | |
2639 | |
2640 | |
2641 | |
2642 | |
2643 | |
2644 | static const unsigned KnownIDs[] = { |
2645 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
2646 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
2647 | LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, |
2648 | LLVMContext::MD_invariant_group, LLVMContext::MD_nonnull, |
2649 | LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; |
2650 | combineMetadata(ReplInst, I, KnownIDs, false); |
2651 | } |
2652 | |
2653 | template <typename RootType, typename DominatesFn> |
2654 | static unsigned replaceDominatedUsesWith(Value *From, Value *To, |
2655 | const RootType &Root, |
2656 | const DominatesFn &Dominates) { |
2657 | assert(From->getType() == To->getType()); |
2658 | |
2659 | unsigned Count = 0; |
2660 | for (Value::use_iterator UI = From->use_begin(), UE = From->use_end(); |
2661 | UI != UE;) { |
2662 | Use &U = *UI++; |
2663 | if (!Dominates(Root, U)) |
2664 | continue; |
2665 | U.set(To); |
2666 | LLVM_DEBUG(dbgs() << "Replace dominated use of '" << From->getName() |
2667 | << "' as " << *To << " in " << *U << "\n"); |
2668 | ++Count; |
2669 | } |
2670 | return Count; |
2671 | } |
2672 | |
2673 | unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) { |
2674 | assert(From->getType() == To->getType()); |
2675 | auto *BB = From->getParent(); |
2676 | unsigned Count = 0; |
2677 | |
2678 | for (Value::use_iterator UI = From->use_begin(), UE = From->use_end(); |
2679 | UI != UE;) { |
2680 | Use &U = *UI++; |
2681 | auto *I = cast<Instruction>(U.getUser()); |
2682 | if (I->getParent() == BB) |
2683 | continue; |
2684 | U.set(To); |
2685 | ++Count; |
2686 | } |
2687 | return Count; |
2688 | } |
2689 | |
2690 | unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, |
2691 | DominatorTree &DT, |
2692 | const BasicBlockEdge &Root) { |
2693 | auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) { |
2694 | return DT.dominates(Root, U); |
2695 | }; |
2696 | return ::replaceDominatedUsesWith(From, To, Root, Dominates); |
2697 | } |
2698 | |
2699 | unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, |
2700 | DominatorTree &DT, |
2701 | const BasicBlock *BB) { |
2702 | auto Dominates = [&DT](const BasicBlock *BB, const Use &U) { |
2703 | return DT.dominates(BB, U); |
2704 | }; |
2705 | return ::replaceDominatedUsesWith(From, To, BB, Dominates); |
2706 | } |
2707 | |
2708 | bool llvm::callsGCLeafFunction(const CallBase *Call, |
2709 | const TargetLibraryInfo &TLI) { |
2710 | |
2711 | if (Call->hasFnAttr("gc-leaf-function")) |
2712 | return true; |
2713 | if (const Function *F = Call->getCalledFunction()) { |
2714 | if (F->hasFnAttribute("gc-leaf-function")) |
2715 | return true; |
2716 | |
2717 | if (auto IID = F->getIntrinsicID()) { |
2718 | |
2719 | return IID != Intrinsic::experimental_gc_statepoint && |
2720 | IID != Intrinsic::experimental_deoptimize && |
2721 | IID != Intrinsic::memcpy_element_unordered_atomic && |
2722 | IID != Intrinsic::memmove_element_unordered_atomic; |
2723 | } |
2724 | } |
2725 | |
2726 | |
2727 | |
2728 | |
2729 | LibFunc LF; |
2730 | if (TLI.getLibFunc(*Call, LF)) { |
2731 | return TLI.has(LF); |
2732 | } |
2733 | |
2734 | return false; |
2735 | } |
2736 | |
2737 | void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, |
2738 | LoadInst &NewLI) { |
2739 | auto *NewTy = NewLI.getType(); |
2740 | |
2741 | |
2742 | if (NewTy->isPointerTy()) { |
2743 | NewLI.setMetadata(LLVMContext::MD_nonnull, N); |
2744 | return; |
2745 | } |
2746 | |
2747 | |
2748 | |
2749 | if (!NewTy->isIntegerTy()) |
2750 | return; |
2751 | |
2752 | MDBuilder MDB(NewLI.getContext()); |
2753 | const Value *Ptr = OldLI.getPointerOperand(); |
2754 | auto *ITy = cast<IntegerType>(NewTy); |
2755 | auto *NullInt = ConstantExpr::getPtrToInt( |
2756 | ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); |
2757 | auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); |
2758 | NewLI.setMetadata(LLVMContext::MD_range, |
2759 | MDB.createRange(NonNullInt, NullInt)); |
2760 | } |
2761 | |
2762 | void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, |
2763 | MDNode *N, LoadInst &NewLI) { |
2764 | auto *NewTy = NewLI.getType(); |
2765 | |
2766 | |
2767 | |
2768 | |
2769 | |
2770 | if (!NewTy->isPointerTy()) |
2771 | return; |
2772 | |
2773 | unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy); |
2774 | if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) { |
2775 | MDNode *NN = MDNode::get(OldLI.getContext(), None); |
2776 | NewLI.setMetadata(LLVMContext::MD_nonnull, NN); |
2777 | } |
2778 | } |
2779 | |
2780 | void llvm::dropDebugUsers(Instruction &I) { |
2781 | SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; |
2782 | findDbgUsers(DbgUsers, &I); |
2783 | for (auto *DII : DbgUsers) |
2784 | DII->eraseFromParent(); |
2785 | } |
2786 | |
2787 | void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, |
2788 | BasicBlock *BB) { |
2789 | |
2790 | |
2791 | |
2792 | |
2793 | |
2794 | |
2795 | |
2796 | |
2797 | |
2798 | |
2799 | |
2800 | |
2801 | |
2802 | |
2803 | |
2804 | |
2805 | |
2806 | |
2807 | |
2808 | |
2809 | |
2810 | |
2811 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) { |
2812 | Instruction *I = &*II; |
2813 | I->dropUndefImplyingAttrsAndUnknownMetadata(); |
2814 | if (I->isUsedByMetadata()) |
2815 | dropDebugUsers(*I); |
2816 | if (I->isDebugOrPseudoInst()) { |
2817 | |
2818 | II = I->eraseFromParent(); |
2819 | continue; |
2820 | } |
2821 | I->setDebugLoc(InsertPt->getDebugLoc()); |
2822 | ++II; |
2823 | } |
2824 | DomBlock->getInstList().splice(InsertPt->getIterator(), BB->getInstList(), |
2825 | BB->begin(), |
2826 | BB->getTerminator()->getIterator()); |
2827 | } |
2828 | |
2829 | namespace { |
2830 | |
2831 | |
2832 | |
2833 | struct BitPart { |
2834 | BitPart(Value *P, unsigned BW) : Provider(P) { |
2835 | Provenance.resize(BW); |
2836 | } |
2837 | |
2838 | |
2839 | Value *Provider; |
2840 | |
2841 | |
2842 | |
2843 | SmallVector<int8_t, 32> Provenance; |
2844 | |
2845 | enum { Unset = -1 }; |
2846 | }; |
2847 | |
2848 | } |
2849 | |
2850 | |
2851 | |
2852 | |
2853 | |
2854 | |
2855 | |
2856 | |
2857 | |
2858 | |
2859 | |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | |
2867 | |
2868 | |
2869 | |
2870 | |
2871 | |
2872 | |
2873 | |
2874 | |
2875 | |
2876 | |
2877 | static const Optional<BitPart> & |
2878 | collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, |
2879 | std::map<Value *, Optional<BitPart>> &BPS, int Depth, |
2880 | bool &FoundRoot) { |
2881 | auto I = BPS.find(V); |
2882 | if (I != BPS.end()) |
2883 | return I->second; |
2884 | |
2885 | auto &Result = BPS[V] = None; |
2886 | auto BitWidth = V->getType()->getScalarSizeInBits(); |
2887 | |
2888 | |
2889 | if (BitWidth > 128) |
2890 | return Result; |
2891 | |
2892 | |
2893 | if (Depth == BitPartRecursionMaxDepth) { |
2894 | LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n"); |
2895 | return Result; |
2896 | } |
2897 | |
2898 | if (auto *I = dyn_cast<Instruction>(V)) { |
2899 | Value *X, *Y; |
2900 | const APInt *C; |
2901 | |
2902 | |
2903 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) { |
2904 | |
2905 | const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
2906 | Depth + 1, FoundRoot); |
2907 | if (!A || !A->Provider) |
2908 | return Result; |
2909 | |
2910 | const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, |
2911 | Depth + 1, FoundRoot); |
2912 | if (!B || A->Provider != B->Provider) |
2913 | return Result; |
2914 | |
2915 | |
2916 | Result = BitPart(A->Provider, BitWidth); |
2917 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) { |
2918 | if (A->Provenance[BitIdx] != BitPart::Unset && |
2919 | B->Provenance[BitIdx] != BitPart::Unset && |
2920 | A->Provenance[BitIdx] != B->Provenance[BitIdx]) |
2921 | return Result = None; |
2922 | |
2923 | if (A->Provenance[BitIdx] == BitPart::Unset) |
2924 | Result->Provenance[BitIdx] = B->Provenance[BitIdx]; |
2925 | else |
2926 | Result->Provenance[BitIdx] = A->Provenance[BitIdx]; |
2927 | } |
2928 | |
2929 | return Result; |
2930 | } |
2931 | |
2932 | |
2933 | if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) { |
2934 | const APInt &BitShift = *C; |
2935 | |
2936 | |
2937 | if (BitShift.uge(BitWidth)) |
2938 | return Result; |
2939 | |
2940 | |
2941 | if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0) |
2942 | return Result; |
2943 | |
2944 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
2945 | Depth + 1, FoundRoot); |
2946 | if (!Res) |
2947 | return Result; |
2948 | Result = Res; |
2949 | |
2950 | |
2951 | auto &P = Result->Provenance; |
2952 | if (I->getOpcode() == Instruction::Shl) { |
2953 | P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end()); |
2954 | P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset); |
2955 | } else { |
2956 | P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue())); |
2957 | P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset); |
2958 | } |
2959 | |
2960 | return Result; |
2961 | } |
2962 | |
2963 | |
2964 | |
2965 | if (match(V, m_And(m_Value(X), m_APInt(C)))) { |
2966 | const APInt &AndMask = *C; |
2967 | |
2968 | |
2969 | |
2970 | unsigned NumMaskedBits = AndMask.countPopulation(); |
2971 | if (!MatchBitReversals && (NumMaskedBits % 8) != 0) |
2972 | return Result; |
2973 | |
2974 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
2975 | Depth + 1, FoundRoot); |
2976 | if (!Res) |
2977 | return Result; |
2978 | Result = Res; |
2979 | |
2980 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
2981 | |
2982 | if (AndMask[BitIdx] == 0) |
2983 | Result->Provenance[BitIdx] = BitPart::Unset; |
2984 | return Result; |
2985 | } |
2986 | |
2987 | |
2988 | if (match(V, m_ZExt(m_Value(X)))) { |
2989 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
2990 | Depth + 1, FoundRoot); |
2991 | if (!Res) |
2992 | return Result; |
2993 | |
2994 | Result = BitPart(Res->Provider, BitWidth); |
2995 | auto NarrowBitWidth = X->getType()->getScalarSizeInBits(); |
2996 | for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx) |
2997 | Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; |
2998 | for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx) |
2999 | Result->Provenance[BitIdx] = BitPart::Unset; |
3000 | return Result; |
3001 | } |
3002 | |
3003 | |
3004 | if (match(V, m_Trunc(m_Value(X)))) { |
3005 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
3006 | Depth + 1, FoundRoot); |
3007 | if (!Res) |
3008 | return Result; |
3009 | |
3010 | Result = BitPart(Res->Provider, BitWidth); |
3011 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
3012 | Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; |
3013 | return Result; |
3014 | } |
3015 | |
3016 | |
3017 | |
3018 | if (match(V, m_BitReverse(m_Value(X)))) { |
3019 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
3020 | Depth + 1, FoundRoot); |
3021 | if (!Res) |
3022 | return Result; |
3023 | |
3024 | Result = BitPart(Res->Provider, BitWidth); |
3025 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
3026 | Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx]; |
3027 | return Result; |
3028 | } |
3029 | |
3030 | |
3031 | if (match(V, m_BSwap(m_Value(X)))) { |
3032 | const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
3033 | Depth + 1, FoundRoot); |
3034 | if (!Res) |
3035 | return Result; |
3036 | |
3037 | unsigned ByteWidth = BitWidth / 8; |
3038 | Result = BitPart(Res->Provider, BitWidth); |
3039 | for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) { |
3040 | unsigned ByteBitOfs = ByteIdx * 8; |
3041 | for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx) |
3042 | Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] = |
3043 | Res->Provenance[ByteBitOfs + BitIdx]; |
3044 | } |
3045 | return Result; |
3046 | } |
3047 | |
3048 | |
3049 | |
3050 | |
3051 | |
3052 | if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) || |
3053 | match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) { |
3054 | |
3055 | unsigned ModAmt = C->urem(BitWidth); |
3056 | if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr) |
3057 | ModAmt = BitWidth - ModAmt; |
3058 | |
3059 | |
3060 | if (!MatchBitReversals && (ModAmt % 8) != 0) |
3061 | return Result; |
3062 | |
3063 | |
3064 | const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, |
3065 | Depth + 1, FoundRoot); |
3066 | if (!LHS || !LHS->Provider) |
3067 | return Result; |
3068 | |
3069 | const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, |
3070 | Depth + 1, FoundRoot); |
3071 | if (!RHS || LHS->Provider != RHS->Provider) |
3072 | return Result; |
3073 | |
3074 | unsigned StartBitRHS = BitWidth - ModAmt; |
3075 | Result = BitPart(LHS->Provider, BitWidth); |
3076 | for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx) |
3077 | Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx]; |
3078 | for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx) |
3079 | Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS]; |
3080 | return Result; |
3081 | } |
3082 | } |
3083 | |
3084 | |
3085 | |
3086 | if (FoundRoot) |
3087 | return Result; |
3088 | |
3089 | |
3090 | |
3091 | FoundRoot = true; |
3092 | Result = BitPart(V, BitWidth); |
3093 | for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) |
3094 | Result->Provenance[BitIdx] = BitIdx; |
3095 | return Result; |
3096 | } |
3097 | |
3098 | static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To, |
3099 | unsigned BitWidth) { |
3100 | if (From % 8 != To % 8) |
3101 | return false; |
3102 | |
3103 | From >>= 3; |
3104 | To >>= 3; |
3105 | BitWidth >>= 3; |
3106 | return From == BitWidth - To - 1; |
3107 | } |
3108 | |
3109 | static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To, |
3110 | unsigned BitWidth) { |
3111 | return From == BitWidth - To - 1; |
3112 | } |
3113 | |
3114 | bool llvm::recognizeBSwapOrBitReverseIdiom( |
3115 | Instruction *I, bool MatchBSwaps, bool MatchBitReversals, |
3116 | SmallVectorImpl<Instruction *> &InsertedInsts) { |
3117 | if (!match(I, m_Or(m_Value(), m_Value())) && |
3118 | !match(I, m_FShl(m_Value(), m_Value(), m_Value())) && |
3119 | !match(I, m_FShr(m_Value(), m_Value(), m_Value()))) |
3120 | return false; |
3121 | if (!MatchBSwaps && !MatchBitReversals) |
| 1 | Assuming 'MatchBSwaps' is true | |
|
3122 | return false; |
3123 | Type *ITy = I->getType(); |
3124 | if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128) |
| 2 | | Assuming the condition is false | |
|
| |
3125 | return false; |
3126 | |
3127 | Type *DemandedTy = ITy; |
3128 | if (I->hasOneUse()) |
| |
3129 | if (auto *Trunc = dyn_cast<TruncInst>(I->user_back())) |
3130 | DemandedTy = Trunc->getType(); |
3131 | |
3132 | |
3133 | bool FoundRoot = false; |
3134 | std::map<Value *, Optional<BitPart>> BPS; |
3135 | const auto &Res = |
3136 | collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot); |
3137 | if (!Res) |
| 5 | | Assuming the condition is false | |
|
| |
3138 | return false; |
3139 | ArrayRef<int8_t> BitProvenance = Res->Provenance; |
3140 | assert(all_of(BitProvenance, |
3141 | [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) && |
3142 | "Illegal bit provenance index"); |
3143 | |
3144 | |
3145 | if (BitProvenance.back() == BitPart::Unset) { |
| 7 | | Assuming the condition is false | |
|
| |
3146 | while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset) |
3147 | BitProvenance = BitProvenance.drop_back(); |
3148 | if (BitProvenance.empty()) |
3149 | return false; |
3150 | DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size()); |
3151 | if (auto *IVecTy = dyn_cast<VectorType>(ITy)) |
3152 | DemandedTy = VectorType::get(DemandedTy, IVecTy); |
3153 | } |
3154 | |
3155 | |
3156 | unsigned DemandedBW = DemandedTy->getScalarSizeInBits(); |
3157 | if (DemandedBW > ITy->getScalarSizeInBits()) |
| 9 | | Assuming the condition is false | |
|
| |
3158 | return false; |
3159 | |
3160 | |
3161 | |
3162 | APInt DemandedMask = APInt::getAllOnesValue(DemandedBW); |
3163 | bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0; |
| 11 | | Assuming the condition is true | |
|
3164 | bool OKForBitReverse = MatchBitReversals; |
3165 | for (unsigned BitIdx = 0; |
3166 | (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) { |
| 12 | | Assuming 'BitIdx' is >= 'DemandedBW' | |
|
3167 | if (BitProvenance[BitIdx] == BitPart::Unset) { |
3168 | DemandedMask.clearBit(BitIdx); |
3169 | continue; |
3170 | } |
3171 | OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx, |
3172 | DemandedBW); |
3173 | OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx], |
3174 | BitIdx, DemandedBW); |
3175 | } |
3176 | |
3177 | Intrinsic::ID Intrin; |
3178 | if (OKForBSwap) |
| |
3179 | Intrin = Intrinsic::bswap; |
3180 | else if (OKForBitReverse) |
3181 | Intrin = Intrinsic::bitreverse; |
3182 | else |
3183 | return false; |
3184 | |
3185 | Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, DemandedTy); |
3186 | Value *Provider = Res->Provider; |
3187 | |
3188 | |
3189 | if (DemandedTy != Provider->getType()) { |
| 14 | | Assuming the condition is false | |
|
| |
3190 | auto *Trunc = |
3191 | CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I); |
3192 | InsertedInsts.push_back(Trunc); |
3193 | Provider = Trunc; |
3194 | } |
3195 | |
3196 | Instruction *Result = CallInst::Create(F, Provider, "rev", I); |
3197 | InsertedInsts.push_back(Result); |
3198 | |
3199 | if (!DemandedMask.isAllOnesValue()) { |
| 16 | | Calling 'APInt::isAllOnesValue' | |
|
3200 | auto *Mask = ConstantInt::get(DemandedTy, DemandedMask); |
3201 | Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I); |
3202 | InsertedInsts.push_back(Result); |
3203 | } |
3204 | |
3205 | |
3206 | if (ITy != Result->getType()) { |
3207 | auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I); |
3208 | InsertedInsts.push_back(ExtInst); |
3209 | } |
3210 | |
3211 | return true; |
3212 | } |
3213 | |
3214 | |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | void llvm::maybeMarkSanitizerLibraryCallNoBuiltin( |
3220 | CallInst *CI, const TargetLibraryInfo *TLI) { |
3221 | Function *F = CI->getCalledFunction(); |
3222 | LibFunc Func; |
3223 | if (F && !F->hasLocalLinkage() && F->hasName() && |
3224 | TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) && |
3225 | !F->doesNotAccessMemory()) |
3226 | CI->addFnAttr(Attribute::NoBuiltin); |
3227 | } |
3228 | |
3229 | bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) { |
3230 | |
3231 | if (I->getOperand(OpIdx)->getType()->isMetadataTy()) |
3232 | return false; |
3233 | |
3234 | |
3235 | if (!isa<Constant>(I->getOperand(OpIdx))) |
3236 | return true; |
3237 | |
3238 | switch (I->getOpcode()) { |
3239 | default: |
3240 | return true; |
3241 | case Instruction::Call: |
3242 | case Instruction::Invoke: { |
3243 | const auto &CB = cast<CallBase>(*I); |
3244 | |
3245 | |
3246 | if (CB.isInlineAsm()) |
3247 | return false; |
3248 | |
3249 | |
3250 | |
3251 | if (CB.isBundleOperand(OpIdx)) |
3252 | return false; |
3253 | |
3254 | if (OpIdx < CB.getNumArgOperands()) { |
3255 | |
3256 | |
3257 | if (isa<IntrinsicInst>(CB) && |
3258 | OpIdx >= CB.getFunctionType()->getNumParams()) { |
3259 | |
3260 | return CB.getIntrinsicID() == Intrinsic::experimental_stackmap; |
3261 | } |
3262 | |
3263 | |
3264 | |
3265 | if (CB.getIntrinsicID() == Intrinsic::gcroot) |
3266 | return false; |
3267 | |
3268 | |
3269 | return !CB.paramHasAttr(OpIdx, Attribute::ImmArg); |
3270 | } |
3271 | |
3272 | |
3273 | |
3274 | return !isa<IntrinsicInst>(CB); |
3275 | } |
3276 | case Instruction::ShuffleVector: |
3277 | |
3278 | return OpIdx != 2; |
3279 | case Instruction::Switch: |
3280 | case Instruction::ExtractValue: |
3281 | |
3282 | return OpIdx == 0; |
3283 | case Instruction::InsertValue: |
3284 | |
3285 | return OpIdx < 2; |
3286 | case Instruction::Alloca: |
3287 | |
3288 | |
3289 | |
3290 | return !cast<AllocaInst>(I)->isStaticAlloca(); |
3291 | case Instruction::GetElementPtr: |
3292 | if (OpIdx == 0) |
3293 | return true; |
3294 | gep_type_iterator It = gep_type_begin(I); |
3295 | for (auto E = std::next(It, OpIdx); It != E; ++It) |
3296 | if (It.isStruct()) |
3297 | return false; |
3298 | return true; |
3299 | } |
3300 | } |
3301 | |
3302 | Value *llvm::invertCondition(Value *Condition) { |
3303 | |
3304 | if (Constant *C = dyn_cast<Constant>(Condition)) |
3305 | return ConstantExpr::getNot(C); |
3306 | |
3307 | |
3308 | Value *NotCondition; |
3309 | if (match(Condition, m_Not(m_Value(NotCondition)))) |
3310 | return NotCondition; |
3311 | |
3312 | BasicBlock *Parent = nullptr; |
3313 | Instruction *Inst = dyn_cast<Instruction>(Condition); |
3314 | if (Inst) |
3315 | Parent = Inst->getParent(); |
3316 | else if (Argument *Arg = dyn_cast<Argument>(Condition)) |
3317 | Parent = &Arg->getParent()->getEntryBlock(); |
3318 | assert(Parent && "Unsupported condition to invert"); |
3319 | |
3320 | |
3321 | for (User *U : Condition->users()) |
3322 | if (Instruction *I = dyn_cast<Instruction>(U)) |
3323 | if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) |
3324 | return I; |
3325 | |
3326 | |
3327 | auto *Inverted = |
3328 | BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv"); |
3329 | if (Inst && !isa<PHINode>(Inst)) |
3330 | Inverted->insertAfter(Inst); |
3331 | else |
3332 | Inverted->insertBefore(&*Parent->getFirstInsertionPt()); |
3333 | return Inverted; |
3334 | } |
3335 | |
3336 | bool llvm::inferAttributesFromOthers(Function &F) { |
3337 | |
3338 | |
3339 | |
3340 | bool Changed = false; |
3341 | |
3342 | if (!F.hasFnAttribute(Attribute::NoSync) && |
3343 | F.doesNotAccessMemory() && !F.isConvergent()) { |
3344 | F.setNoSync(); |
3345 | Changed = true; |
3346 | } |
3347 | |
3348 | |
3349 | if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) { |
3350 | F.setDoesNotFreeMemory(); |
3351 | Changed = true; |
3352 | } |
3353 | |
3354 | |
3355 | if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) { |
3356 | F.setMustProgress(); |
3357 | Changed = true; |
3358 | } |
3359 | |
3360 | |
3361 | |
3362 | |
3363 | return Changed; |
3364 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #ifndef LLVM_ADT_APINT_H |
16 | #define LLVM_ADT_APINT_H |
17 | |
18 | #include "llvm/Support/Compiler.h" |
19 | #include "llvm/Support/MathExtras.h" |
20 | #include <cassert> |
21 | #include <climits> |
22 | #include <cstring> |
23 | #include <utility> |
24 | |
25 | namespace llvm { |
26 | class FoldingSetNodeID; |
27 | class StringRef; |
28 | class hash_code; |
29 | class raw_ostream; |
30 | |
31 | template <typename T> class SmallVectorImpl; |
32 | template <typename T> class ArrayRef; |
33 | template <typename T> class Optional; |
34 | template <typename T> struct DenseMapInfo; |
35 | |
36 | class APInt; |
37 | |
38 | inline APInt operator-(APInt); |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | class LLVM_NODISCARD APInt { |
71 | public: |
72 | typedef uint64_t WordType; |
73 | |
74 | |
75 | enum : unsigned { |
76 | |
77 | APINT_WORD_SIZE = sizeof(WordType), |
78 | |
79 | APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT |
80 | }; |
81 | |
82 | enum class Rounding { |
83 | DOWN, |
84 | TOWARD_ZERO, |
85 | UP, |
86 | }; |
87 | |
88 | static constexpr WordType WORDTYPE_MAX = ~WordType(0); |
89 | |
90 | private: |
91 | |
92 | |
93 | union { |
94 | uint64_t VAL; |
95 | uint64_t *pVal; |
96 | } U; |
97 | |
98 | unsigned BitWidth; |
99 | |
100 | friend struct DenseMapInfo<APInt>; |
101 | |
102 | friend class APSInt; |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { |
109 | U.pVal = val; |
110 | } |
111 | |
112 | |
113 | |
114 | |
115 | static unsigned whichWord(unsigned bitPosition) { |
116 | return bitPosition / APINT_BITS_PER_WORD; |
117 | } |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | static unsigned whichBit(unsigned bitPosition) { |
124 | return bitPosition % APINT_BITS_PER_WORD; |
125 | } |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | static uint64_t maskBit(unsigned bitPosition) { |
134 | return 1ULL << whichBit(bitPosition); |
135 | } |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | APInt &clearUnusedBits() { |
144 | |
145 | unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1; |
146 | |
147 | |
148 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); |
149 | if (isSingleWord()) |
150 | U.VAL &= mask; |
151 | else |
152 | U.pVal[getNumWords() - 1] &= mask; |
153 | return *this; |
154 | } |
155 | |
156 | |
157 | |
158 | uint64_t getWord(unsigned bitPosition) const { |
159 | return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; |
160 | } |
161 | |
162 | |
163 | |
164 | |
165 | void reallocate(unsigned NewBitWidth); |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | void fromString(unsigned numBits, StringRef str, uint8_t radix); |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | static void divide(const WordType *LHS, unsigned lhsWords, |
188 | const WordType *RHS, unsigned rhsWords, WordType *Quotient, |
189 | WordType *Remainder); |
190 | |
191 | |
192 | void initSlowCase(uint64_t val, bool isSigned); |
193 | |
194 | |
195 | void initFromArray(ArrayRef<uint64_t> array); |
196 | |
197 | |
198 | void initSlowCase(const APInt &that); |
199 | |
200 | |
201 | void shlSlowCase(unsigned ShiftAmt); |
202 | |
203 | |
204 | void lshrSlowCase(unsigned ShiftAmt); |
205 | |
206 | |
207 | void ashrSlowCase(unsigned ShiftAmt); |
208 | |
209 | |
210 | void AssignSlowCase(const APInt &RHS); |
211 | |
212 | |
213 | bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY; |
214 | |
215 | |
216 | unsigned countLeadingZerosSlowCase() const LLVM_READONLY; |
217 | |
218 | |
219 | unsigned countLeadingOnesSlowCase() const LLVM_READONLY; |
220 | |
221 | |
222 | unsigned countTrailingZerosSlowCase() const LLVM_READONLY; |
223 | |
224 | |
225 | unsigned countTrailingOnesSlowCase() const LLVM_READONLY; |
226 | |
227 | |
228 | unsigned countPopulationSlowCase() const LLVM_READONLY; |
229 | |
230 | |
231 | bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY; |
232 | |
233 | |
234 | bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY; |
235 | |
236 | |
237 | void setBitsSlowCase(unsigned loBit, unsigned hiBit); |
238 | |
239 | |
240 | void flipAllBitsSlowCase(); |
241 | |
242 | |
243 | void AndAssignSlowCase(const APInt& RHS); |
244 | |
245 | |
246 | void OrAssignSlowCase(const APInt& RHS); |
247 | |
248 | |
249 | void XorAssignSlowCase(const APInt& RHS); |
250 | |
251 | |
252 | |
253 | int compare(const APInt &RHS) const LLVM_READONLY; |
254 | |
255 | |
256 | |
257 | int compareSigned(const APInt &RHS) const LLVM_READONLY; |
258 | |
259 | public: |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | APInt(unsigned numBits, uint64_t val, bool isSigned = false) |
274 | : BitWidth(numBits) { |
275 | assert(BitWidth && "bitwidth too small"); |
276 | if (isSingleWord()) { |
277 | U.VAL = val; |
278 | clearUnusedBits(); |
279 | } else { |
280 | initSlowCase(val, isSigned); |
281 | } |
282 | } |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); |
292 | |
293 | |
294 | |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | APInt(unsigned numBits, StringRef str, uint8_t radix); |
314 | |
315 | |
316 | |
317 | APInt(const APInt &that) : BitWidth(that.BitWidth) { |
318 | if (isSingleWord()) |
319 | U.VAL = that.U.VAL; |
320 | else |
321 | initSlowCase(that); |
322 | } |
323 | |
324 | |
325 | APInt(APInt &&that) : BitWidth(that.BitWidth) { |
326 | memcpy(&U, &that.U, sizeof(U)); |
327 | that.BitWidth = 0; |
328 | } |
329 | |
330 | |
331 | ~APInt() { |
332 | if (needsCleanup()) |
333 | delete[] U.pVal; |
334 | } |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | explicit APInt() : BitWidth(1) { U.VAL = 0; } |
342 | |
343 | |
344 | bool needsCleanup() const { return !isSingleWord(); } |
345 | |
346 | |
347 | |
348 | void Profile(FoldingSetNodeID &id) const; |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } |
| 18 | | Returning the value 1, which participates in a condition later | |
|
358 | |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | bool isNegative() const { return (*this)[BitWidth - 1]; } |
365 | |
366 | |
367 | |
368 | |
369 | bool isNonNegative() const { return !isNegative(); } |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | bool isSignBitSet() const { return (*this)[BitWidth-1]; } |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | bool isSignBitClear() const { return !isSignBitSet(); } |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } |
392 | |
393 | |
394 | |
395 | |
396 | bool isNonPositive() const { return !isStrictlyPositive(); } |
397 | |
398 | |
399 | |
400 | |
401 | bool isAllOnesValue() const { |
402 | if (isSingleWord()) |
| 17 | | Calling 'APInt::isSingleWord' | |
|
| 19 | | Returning from 'APInt::isSingleWord' | |
|
| |
403 | return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); |
| 21 | | The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::APInt::WordType' |
|
404 | return countTrailingOnesSlowCase() == BitWidth; |
405 | } |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | bool isNullValue() const { return !*this; } |
412 | |
413 | |
414 | |
415 | |
416 | bool isOneValue() const { |
417 | if (isSingleWord()) |
418 | return U.VAL == 1; |
419 | return countLeadingZerosSlowCase() == BitWidth - 1; |
420 | } |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | bool isMaxValue() const { return isAllOnesValue(); } |
427 | |
428 | |
429 | |
430 | |
431 | |
432 | bool isMaxSignedValue() const { |
433 | if (isSingleWord()) |
434 | return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); |
435 | return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; |
436 | } |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | bool isMinValue() const { return isNullValue(); } |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | bool isMinSignedValue() const { |
449 | if (isSingleWord()) |
450 | return U.VAL == (WordType(1) << (BitWidth - 1)); |
451 | return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; |
452 | } |
453 | |
454 | |
455 | bool isIntN(unsigned N) const { |
456 | assert(N && "N == 0 ???"); |
457 | return getActiveBits() <= N; |
458 | } |
459 | |
460 | |
461 | bool isSignedIntN(unsigned N) const { |
462 | assert(N && "N == 0 ???"); |
463 | return getMinSignedBits() <= N; |
464 | } |
465 | |
466 | |
467 | |
468 | |
469 | bool isPowerOf2() const { |
470 | if (isSingleWord()) |
471 | return isPowerOf2_64(U.VAL); |
472 | return countPopulationSlowCase() == 1; |
473 | } |
474 | |
475 | |
476 | |
477 | |
478 | bool isSignMask() const { return isMinSignedValue(); } |
479 | |
480 | |
481 | |
482 | |
483 | bool getBoolValue() const { return !!*this; } |
484 | |
485 | |
486 | |
487 | uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX) const { |
488 | return ugt(Limit) ? Limit : getZExtValue(); |
489 | } |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | bool isSplat(unsigned SplatSizeInBits) const; |
497 | |
498 | |
499 | |
500 | bool isMask(unsigned numBits) const { |
501 | assert(numBits != 0 && "numBits must be non-zero"); |
502 | assert(numBits <= BitWidth && "numBits out of range"); |
503 | if (isSingleWord()) |
504 | return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); |
505 | unsigned Ones = countTrailingOnesSlowCase(); |
506 | return (numBits == Ones) && |
507 | ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
508 | } |
509 | |
510 | |
511 | |
512 | |
513 | bool isMask() const { |
514 | if (isSingleWord()) |
515 | return isMask_64(U.VAL); |
516 | unsigned Ones = countTrailingOnesSlowCase(); |
517 | return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
518 | } |
519 | |
520 | |
521 | |
522 | bool isShiftedMask() const { |
523 | if (isSingleWord()) |
524 | return isShiftedMask_64(U.VAL); |
525 | unsigned Ones = countPopulationSlowCase(); |
526 | unsigned LeadZ = countLeadingZerosSlowCase(); |
527 | return (Ones + LeadZ + countTrailingZeros()) == BitWidth; |
528 | } |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | static APInt getMaxValue(unsigned numBits) { |
536 | return getAllOnesValue(numBits); |
537 | } |
538 | |
539 | |
540 | static APInt getSignedMaxValue(unsigned numBits) { |
541 | APInt API = getAllOnesValue(numBits); |
542 | API.clearBit(numBits - 1); |
543 | return API; |
544 | } |
545 | |
546 | |
547 | static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } |
548 | |
549 | |
550 | static APInt getSignedMinValue(unsigned numBits) { |
551 | APInt API(numBits, 0); |
552 | API.setBit(numBits - 1); |
553 | return API; |
554 | } |
555 | |
556 | |
557 | |
558 | |
559 | |
560 | static APInt getSignMask(unsigned BitWidth) { |
561 | return getSignedMinValue(BitWidth); |
562 | } |
563 | |
564 | |
565 | |
566 | |
567 | static APInt getAllOnesValue(unsigned numBits) { |
568 | return APInt(numBits, WORDTYPE_MAX, true); |
569 | } |
570 | |
571 | |
572 | |
573 | |
574 | static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); } |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | |
581 | |
582 | APInt getHiBits(unsigned numBits) const; |
583 | |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | |
590 | APInt getLoBits(unsigned numBits) const; |
591 | |
592 | |
593 | static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { |
594 | APInt Res(numBits, 0); |
595 | Res.setBit(BitNo); |
596 | return Res; |
597 | } |
598 | |
599 | |
600 | |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | |
611 | |
612 | static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { |
613 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
614 | APInt Res(numBits, 0); |
615 | Res.setBits(loBit, hiBit); |
616 | return Res; |
617 | } |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, |
626 | unsigned hiBit) { |
627 | APInt Res(numBits, 0); |
628 | Res.setBitsWithWrap(loBit, hiBit); |
629 | return Res; |
630 | } |
631 | |
632 | |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { |
644 | APInt Res(numBits, 0); |
645 | Res.setBitsFrom(loBit); |
646 | return Res; |
647 | } |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | |
654 | |
655 | static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { |
656 | APInt Res(numBits, 0); |
657 | Res.setHighBits(hiBitsSet); |
658 | return Res; |
659 | } |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { |
668 | APInt Res(numBits, 0); |
669 | Res.setLowBits(loBitsSet); |
670 | return Res; |
671 | } |
672 | |
673 | |
674 | static APInt getSplat(unsigned NewLen, const APInt &V); |
675 | |
676 | |
677 | |
678 | static bool isSameValue(const APInt &I1, const APInt &I2) { |
679 | if (I1.getBitWidth() == I2.getBitWidth()) |
680 | return I1 == I2; |
681 | |
682 | if (I1.getBitWidth() > I2.getBitWidth()) |
683 | return I1 == I2.zext(I1.getBitWidth()); |
684 | |
685 | return I1.zext(I2.getBitWidth()) == I2; |
686 | } |
687 | |
688 | |
689 | friend hash_code hash_value(const APInt &Arg); |
690 | |
691 | |
692 | |
693 | |
694 | const uint64_t *getRawData() const { |
695 | if (isSingleWord()) |
696 | return &U.VAL; |
697 | return &U.pVal[0]; |
698 | } |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | |
708 | |
709 | APInt operator++(int) { |
710 | APInt API(*this); |
711 | ++(*this); |
712 | return API; |
713 | } |
714 | |
715 | |
716 | |
717 | |
718 | APInt &operator++(); |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | APInt operator--(int) { |
726 | APInt API(*this); |
727 | --(*this); |
728 | return API; |
729 | } |
730 | |
731 | |
732 | |
733 | |
734 | APInt &operator--(); |
735 | |
736 | |
737 | |
738 | |
739 | |
740 | |
741 | bool operator!() const { |
742 | if (isSingleWord()) |
743 | return U.VAL == 0; |
744 | return countLeadingZerosSlowCase() == BitWidth; |
745 | } |
746 | |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | |
754 | APInt &operator=(const APInt &RHS) { |
755 | |
756 | if (isSingleWord() && RHS.isSingleWord()) { |
757 | U.VAL = RHS.U.VAL; |
758 | BitWidth = RHS.BitWidth; |
759 | return clearUnusedBits(); |
760 | } |
761 | |
762 | AssignSlowCase(RHS); |
763 | return *this; |
764 | } |
765 | |
766 | |
767 | APInt &operator=(APInt &&that) { |
768 | #ifdef EXPENSIVE_CHECKS |
769 | |
770 | if (this == &that) |
771 | return *this; |
772 | #endif |
773 | assert(this != &that && "Self-move not supported"); |
774 | if (!isSingleWord()) |
775 | delete[] U.pVal; |
776 | |
777 | |
778 | |
779 | memcpy(&U, &that.U, sizeof(U)); |
780 | |
781 | BitWidth = that.BitWidth; |
782 | that.BitWidth = 0; |
783 | |
784 | return *this; |
785 | } |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | |
794 | APInt &operator=(uint64_t RHS) { |
795 | if (isSingleWord()) { |
796 | U.VAL = RHS; |
797 | return clearUnusedBits(); |
798 | } |
799 | U.pVal[0] = RHS; |
800 | memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
801 | return *this; |
802 | } |
803 | |
804 | |
805 | |
806 | |
807 | |
808 | |
809 | |
810 | APInt &operator&=(const APInt &RHS) { |
811 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
812 | if (isSingleWord()) |
813 | U.VAL &= RHS.U.VAL; |
814 | else |
815 | AndAssignSlowCase(RHS); |
816 | return *this; |
817 | } |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | |
824 | APInt &operator&=(uint64_t RHS) { |
825 | if (isSingleWord()) { |
826 | U.VAL &= RHS; |
827 | return *this; |
828 | } |
829 | U.pVal[0] &= RHS; |
830 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
831 | return *this; |
832 | } |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | |
840 | APInt &operator|=(const APInt &RHS) { |
841 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
842 | if (isSingleWord()) |
843 | U.VAL |= RHS.U.VAL; |
844 | else |
845 | OrAssignSlowCase(RHS); |
846 | return *this; |
847 | } |
848 | |
849 | |
850 | |
851 | |
852 | |
853 | |
854 | APInt &operator|=(uint64_t RHS) { |
855 | if (isSingleWord()) { |
856 | U.VAL |= RHS; |
857 | return clearUnusedBits(); |
858 | } |
859 | U.pVal[0] |= RHS; |
860 | return *this; |
861 | } |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | |
868 | |
869 | APInt &operator^=(const APInt &RHS) { |
870 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
871 | if (isSingleWord()) |
872 | U.VAL ^= RHS.U.VAL; |
873 | else |
874 | XorAssignSlowCase(RHS); |
875 | return *this; |
876 | } |
877 | |
878 | |
879 | |
880 | |
881 | |
882 | |
883 | APInt &operator^=(uint64_t RHS) { |
884 | if (isSingleWord()) { |
885 | U.VAL ^= RHS; |
886 | return clearUnusedBits(); |
887 | } |
888 | U.pVal[0] ^= RHS; |
889 | return *this; |
890 | } |
891 | |
892 | |
893 | |
894 | |
895 | |
896 | |
897 | APInt &operator*=(const APInt &RHS); |
898 | APInt &operator*=(uint64_t RHS); |
899 | |
900 | |
901 | |
902 | |
903 | |
904 | |
905 | APInt &operator+=(const APInt &RHS); |
906 | APInt &operator+=(uint64_t RHS); |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | APInt &operator-=(const APInt &RHS); |
914 | APInt &operator-=(uint64_t RHS); |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | APInt &operator<<=(unsigned ShiftAmt) { |
922 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
923 | if (isSingleWord()) { |
924 | if (ShiftAmt == BitWidth) |
925 | U.VAL = 0; |
926 | else |
927 | U.VAL <<= ShiftAmt; |
928 | return clearUnusedBits(); |
929 | } |
930 | shlSlowCase(ShiftAmt); |
931 | return *this; |
932 | } |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | APInt &operator<<=(const APInt &ShiftAmt); |
940 | |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | |
948 | APInt operator*(const APInt &RHS) const; |
949 | |
950 | |
951 | |
952 | |
953 | APInt operator<<(unsigned Bits) const { return shl(Bits); } |
954 | |
955 | |
956 | |
957 | |
958 | APInt operator<<(const APInt &Bits) const { return shl(Bits); } |
959 | |
960 | |
961 | |
962 | |
963 | APInt ashr(unsigned ShiftAmt) const { |
964 | APInt R(*this); |
965 | R.ashrInPlace(ShiftAmt); |
966 | return R; |
967 | } |
968 | |
969 | |
970 | void ashrInPlace(unsigned ShiftAmt) { |
971 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
972 | if (isSingleWord()) { |
973 | int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); |
974 | if (ShiftAmt == BitWidth) |
975 | U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); |
976 | else |
977 | U.VAL = SExtVAL >> ShiftAmt; |
978 | clearUnusedBits(); |
979 | return; |
980 | } |
981 | ashrSlowCase(ShiftAmt); |
982 | } |
983 | |
984 | |
985 | |
986 | |
987 | APInt lshr(unsigned shiftAmt) const { |
988 | APInt R(*this); |
989 | R.lshrInPlace(shiftAmt); |
990 | return R; |
991 | } |
992 | |
993 | |
994 | void lshrInPlace(unsigned ShiftAmt) { |
995 | assert(ShiftAmt <= BitWidth && "Invalid shift amount"); |
996 | if (isSingleWord()) { |
997 | if (ShiftAmt == BitWidth) |
998 | U.VAL = 0; |
999 | else |
1000 | U.VAL >>= ShiftAmt; |
1001 | return; |
1002 | } |
1003 | lshrSlowCase(ShiftAmt); |
1004 | } |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | APInt shl(unsigned shiftAmt) const { |
1010 | APInt R(*this); |
1011 | R <<= shiftAmt; |
1012 | return R; |
1013 | } |
1014 | |
1015 | |
1016 | APInt rotl(unsigned rotateAmt) const; |
1017 | |
1018 | |
1019 | APInt rotr(unsigned rotateAmt) const; |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | APInt ashr(const APInt &ShiftAmt) const { |
1025 | APInt R(*this); |
1026 | R.ashrInPlace(ShiftAmt); |
1027 | return R; |
1028 | } |
1029 | |
1030 | |
1031 | void ashrInPlace(const APInt &shiftAmt); |
1032 | |
1033 | |
1034 | |
1035 | |
1036 | APInt lshr(const APInt &ShiftAmt) const { |
1037 | APInt R(*this); |
1038 | R.lshrInPlace(ShiftAmt); |
1039 | return R; |
1040 | } |
1041 | |
1042 | |
1043 | void lshrInPlace(const APInt &ShiftAmt); |
1044 | |
1045 | |
1046 | |
1047 | |
1048 | APInt shl(const APInt &ShiftAmt) const { |
1049 | APInt R(*this); |
1050 | R <<= ShiftAmt; |
1051 | return R; |
1052 | } |
1053 | |
1054 | |
1055 | APInt rotl(const APInt &rotateAmt) const; |
1056 | |
1057 | |
1058 | APInt rotr(const APInt &rotateAmt) const; |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | APInt udiv(const APInt &RHS) const; |
1068 | APInt udiv(uint64_t RHS) const; |
1069 | |
1070 | |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | APInt sdiv(const APInt &RHS) const; |
1076 | APInt sdiv(int64_t RHS) const; |
1077 | |
1078 | |
1079 | |
1080 | |
1081 | |
1082 | |
1083 | |
1084 | |
1085 | |
1086 | |
1087 | APInt urem(const APInt &RHS) const; |
1088 | uint64_t urem(uint64_t RHS) const; |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | APInt srem(const APInt &RHS) const; |
1094 | int64_t srem(int64_t RHS) const; |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | |
1101 | |
1102 | |
1103 | static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1104 | APInt &Remainder); |
1105 | static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
1106 | uint64_t &Remainder); |
1107 | |
1108 | static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1109 | APInt &Remainder); |
1110 | static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, |
1111 | int64_t &Remainder); |
1112 | |
1113 | |
1114 | APInt sadd_ov(const APInt &RHS, bool &Overflow) const; |
1115 | APInt uadd_ov(const APInt &RHS, bool &Overflow) const; |
1116 | APInt ssub_ov(const APInt &RHS, bool &Overflow) const; |
1117 | APInt usub_ov(const APInt &RHS, bool &Overflow) const; |
1118 | APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; |
1119 | APInt smul_ov(const APInt &RHS, bool &Overflow) const; |
1120 | APInt umul_ov(const APInt &RHS, bool &Overflow) const; |
1121 | APInt sshl_ov(const APInt &Amt, bool &Overflow) const; |
1122 | APInt ushl_ov(const APInt &Amt, bool &Overflow) const; |
1123 | |
1124 | |
1125 | APInt sadd_sat(const APInt &RHS) const; |
1126 | APInt uadd_sat(const APInt &RHS) const; |
1127 | APInt ssub_sat(const APInt &RHS) const; |
1128 | APInt usub_sat(const APInt &RHS) const; |
1129 | APInt smul_sat(const APInt &RHS) const; |
1130 | APInt umul_sat(const APInt &RHS) const; |
1131 | APInt sshl_sat(const APInt &RHS) const; |
1132 | APInt ushl_sat(const APInt &RHS) const; |
1133 | |
1134 | |
1135 | |
1136 | |
1137 | bool operator[](unsigned bitPosition) const { |
1138 | assert(bitPosition < getBitWidth() && "Bit position out of bounds!"); |
1139 | return (maskBit(bitPosition) & getWord(bitPosition)) != 0; |
1140 | } |
1141 | |
1142 | |
1143 | |
1144 | |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | |
1150 | bool operator==(const APInt &RHS) const { |
1151 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths"); |
1152 | if (isSingleWord()) |
1153 | return U.VAL == RHS.U.VAL; |
1154 | return EqualSlowCase(RHS); |
1155 | } |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | bool operator==(uint64_t Val) const { |
1164 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; |
1165 | } |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | bool eq(const APInt &RHS) const { return (*this) == RHS; } |
1174 | |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | bool operator!=(uint64_t Val) const { return !((*this) == Val); } |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | bool ne(const APInt &RHS) const { return !((*this) == RHS); } |
1198 | |
1199 | |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | |
1205 | bool ult(const APInt &RHS) const { return compare(RHS) < 0; } |
1206 | |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | bool ult(uint64_t RHS) const { |
1214 | |
1215 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; |
1216 | } |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | bool slt(int64_t RHS) const { |
1233 | return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative() |
1234 | : getSExtValue() < RHS; |
1235 | } |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } |
1244 | |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | bool ule(uint64_t RHS) const { return !ugt(RHS); } |
1252 | |
1253 | |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } |
1260 | |
1261 | |
1262 | |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | bool sle(uint64_t RHS) const { return !sgt(RHS); } |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | bool ugt(const APInt &RHS) const { return !ule(RHS); } |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | bool ugt(uint64_t RHS) const { |
1284 | |
1285 | return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; |
1286 | } |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | |
1292 | |
1293 | |
1294 | bool sgt(const APInt &RHS) const { return !sle(RHS); } |
1295 | |
1296 | |
1297 | |
1298 | |
1299 | |
1300 | |
1301 | |
1302 | bool sgt(int64_t RHS) const { |
1303 | return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative() |
1304 | : getSExtValue() > RHS; |
1305 | } |
1306 | |
1307 | |
1308 | |
1309 | |
1310 | |
1311 | |
1312 | |
1313 | bool uge(const APInt &RHS) const { return !ult(RHS); } |
1314 | |
1315 | |
1316 | |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | bool uge(uint64_t RHS) const { return !ult(RHS); } |
1322 | |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | bool sge(const APInt &RHS) const { return !slt(RHS); } |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | bool sge(int64_t RHS) const { return !slt(RHS); } |
1338 | |
1339 | |
1340 | |
1341 | bool intersects(const APInt &RHS) const { |
1342 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1343 | if (isSingleWord()) |
1344 | return (U.VAL & RHS.U.VAL) != 0; |
1345 | return intersectsSlowCase(RHS); |
1346 | } |
1347 | |
1348 | |
1349 | bool isSubsetOf(const APInt &RHS) const { |
1350 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
1351 | if (isSingleWord()) |
1352 | return (U.VAL & ~RHS.U.VAL) == 0; |
1353 | return isSubsetOfSlowCase(RHS); |
1354 | } |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | |
1360 | |
1361 | |
1362 | |
1363 | |
1364 | APInt trunc(unsigned width) const; |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | |
1370 | APInt truncUSat(unsigned width) const; |
1371 | |
1372 | |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | APInt truncSSat(unsigned width) const; |
1378 | |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | APInt sext(unsigned width) const; |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | APInt zext(unsigned width) const; |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | APInt sextOrTrunc(unsigned width) const; |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | APInt zextOrTrunc(unsigned width) const; |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | APInt truncOrSelf(unsigned width) const; |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | APInt sextOrSelf(unsigned width) const; |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | APInt zextOrSelf(unsigned width) const; |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | void setAllBits() { |
1430 | if (isSingleWord()) |
1431 | U.VAL = WORDTYPE_MAX; |
1432 | else |
1433 | |
1434 | memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); |
1435 | |
1436 | clearUnusedBits(); |
1437 | } |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | void setBit(unsigned BitPosition) { |
1443 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
1444 | WordType Mask = maskBit(BitPosition); |
1445 | if (isSingleWord()) |
1446 | U.VAL |= Mask; |
1447 | else |
1448 | U.pVal[whichWord(BitPosition)] |= Mask; |
1449 | } |
1450 | |
1451 | |
1452 | void setSignBit() { |
1453 | setBit(BitWidth - 1); |
1454 | } |
1455 | |
1456 | |
1457 | void setBitVal(unsigned BitPosition, bool BitValue) { |
1458 | if (BitValue) |
1459 | setBit(BitPosition); |
1460 | else |
1461 | clearBit(BitPosition); |
1462 | } |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | void setBitsWithWrap(unsigned loBit, unsigned hiBit) { |
1469 | assert(hiBit <= BitWidth && "hiBit out of range"); |
1470 | assert(loBit <= BitWidth && "loBit out of range"); |
1471 | if (loBit < hiBit) { |
1472 | setBits(loBit, hiBit); |
1473 | return; |
1474 | } |
1475 | setLowBits(hiBit); |
1476 | setHighBits(BitWidth - loBit); |
1477 | } |
1478 | |
1479 | |
1480 | |
1481 | void setBits(unsigned loBit, unsigned hiBit) { |
1482 | assert(hiBit <= BitWidth && "hiBit out of range"); |
1483 | assert(loBit <= BitWidth && "loBit out of range"); |
1484 | assert(loBit <= hiBit && "loBit greater than hiBit"); |
1485 | if (loBit == hiBit) |
1486 | return; |
1487 | if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { |
1488 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); |
1489 | mask <<= loBit; |
1490 | if (isSingleWord()) |
1491 | U.VAL |= mask; |
1492 | else |
1493 | U.pVal[0] |= mask; |
1494 | } else { |
1495 | setBitsSlowCase(loBit, hiBit); |
1496 | } |
1497 | } |
1498 | |
1499 | |
1500 | void setBitsFrom(unsigned loBit) { |
1501 | return setBits(loBit, BitWidth); |
1502 | } |
1503 | |
1504 | |
1505 | void setLowBits(unsigned loBits) { |
1506 | return setBits(0, loBits); |
1507 | } |
1508 | |
1509 | |
1510 | void setHighBits(unsigned hiBits) { |
1511 | return setBits(BitWidth - hiBits, BitWidth); |
1512 | } |
1513 | |
1514 | |
1515 | void clearAllBits() { |
1516 | if (isSingleWord()) |
1517 | U.VAL = 0; |
1518 | else |
1519 | memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); |
1520 | } |
1521 | |
1522 | |
1523 | |
1524 | |
1525 | void clearBit(unsigned BitPosition) { |
1526 | assert(BitPosition < BitWidth && "BitPosition out of range"); |
1527 | WordType Mask = ~maskBit(BitPosition); |
1528 | if (isSingleWord()) |
1529 | U.VAL &= Mask; |
1530 | else |
1531 | U.pVal[whichWord(BitPosition)] &= Mask; |
1532 | } |
1533 | |
1534 | |
1535 | void clearLowBits(unsigned loBits) { |
1536 | assert(loBits <= BitWidth && "More bits than bitwidth"); |
1537 | APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits); |
1538 | *this &= Keep; |
1539 | } |
1540 | |
1541 | |
1542 | void clearSignBit() { |
1543 | clearBit(BitWidth - 1); |
1544 | } |
1545 | |
1546 | |
1547 | void flipAllBits() { |
1548 | if (isSingleWord()) { |
1549 | U.VAL ^= WORDTYPE_MAX; |
1550 | clearUnusedBits(); |
1551 | } else { |
1552 | flipAllBitsSlowCase(); |
1553 | } |
1554 | } |
1555 | |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | void flipBit(unsigned bitPosition); |
1561 | |
1562 | |
1563 | void negate() { |
1564 | flipAllBits(); |
1565 | ++(*this); |
1566 | } |
1567 | |
1568 | |
1569 | void insertBits(const APInt &SubBits, unsigned bitPosition); |
1570 | void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits); |
1571 | |
1572 | |
1573 | APInt extractBits(unsigned numBits, unsigned bitPosition) const; |
1574 | uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const; |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | unsigned getBitWidth() const { return BitWidth; } |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | unsigned getNumWords() const { return getNumWords(BitWidth); } |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | |
1596 | static unsigned getNumWords(unsigned BitWidth) { |
1597 | return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
1598 | } |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | |
1605 | unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); } |
1606 | |
1607 | |
1608 | |
1609 | |
1610 | |
1611 | unsigned getActiveWords() const { |
1612 | unsigned numActiveBits = getActiveBits(); |
1613 | return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; |
1614 | } |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | |
1623 | |
1624 | unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; } |
1625 | |
1626 | |
1627 | |
1628 | |
1629 | |
1630 | |
1631 | uint64_t getZExtValue() const { |
1632 | if (isSingleWord()) |
1633 | return U.VAL; |
1634 | assert(getActiveBits() <= 64 && "Too many bits for uint64_t"); |
1635 | return U.pVal[0]; |
1636 | } |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | |
1643 | int64_t getSExtValue() const { |
1644 | if (isSingleWord()) |
1645 | return SignExtend64(U.VAL, BitWidth); |
1646 | assert(getMinSignedBits() <= 64 && "Too many bits for int64_t"); |
1647 | return int64_t(U.pVal[0]); |
1648 | } |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | static unsigned getBitsNeeded(StringRef str, uint8_t radix); |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | unsigned countLeadingZeros() const { |
1665 | if (isSingleWord()) { |
1666 | unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; |
1667 | return llvm::countLeadingZeros(U.VAL) - unusedBits; |
1668 | } |
1669 | return countLeadingZerosSlowCase(); |
1670 | } |
1671 | |
1672 | |
1673 | |
1674 | |
1675 | |
1676 | |
1677 | |
1678 | |
1679 | |
1680 | unsigned countLeadingOnes() const { |
1681 | if (isSingleWord()) |
1682 | return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); |
1683 | return countLeadingOnesSlowCase(); |
1684 | } |
1685 | |
1686 | |
1687 | |
1688 | unsigned getNumSignBits() const { |
1689 | return isNegative() ? countLeadingOnes() : countLeadingZeros(); |
1690 | } |
1691 | |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | |
1697 | |
1698 | |
1699 | |
1700 | unsigned countTrailingZeros() const { |
1701 | if (isSingleWord()) { |
1702 | unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL); |
1703 | return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros); |
1704 | } |
1705 | return countTrailingZerosSlowCase(); |
1706 | } |
1707 | |
1708 | |
1709 | |
1710 | |
1711 | |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | unsigned countTrailingOnes() const { |
1717 | if (isSingleWord()) |
1718 | return llvm::countTrailingOnes(U.VAL); |
1719 | return countTrailingOnesSlowCase(); |
1720 | } |
1721 | |
1722 | |
1723 | |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | unsigned countPopulation() const { |
1729 | if (isSingleWord()) |
1730 | return llvm::countPopulation(U.VAL); |
1731 | return countPopulationSlowCase(); |
1732 | } |
1733 | |
1734 | |
1735 | |
1736 | |
1737 | void print(raw_ostream &OS, bool isSigned) const; |
1738 | |
1739 | |
1740 | |
1741 | void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, |
1742 | bool formatAsCLiteral = false) const; |
1743 | |
1744 | |
1745 | |
1746 | void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1747 | toString(Str, Radix, false, false); |
1748 | } |
1749 | |
1750 | |
1751 | |
1752 | void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1753 | toString(Str, Radix, true, false); |
1754 | } |
1755 | |
1756 | |
1757 | APInt byteSwap() const; |
1758 | |
1759 | |
1760 | |
1761 | APInt reverseBits() const; |
1762 | |
1763 | |
1764 | double roundToDouble(bool isSigned) const; |
1765 | |
1766 | |
1767 | double roundToDouble() const { return roundToDouble(false); } |
1768 | |
1769 | |
1770 | double signedRoundToDouble() const { return roundToDouble(true); } |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | double bitsToDouble() const { |
1778 | return BitsToDouble(getWord(0)); |
1779 | } |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | float bitsToFloat() const { |
1787 | return BitsToFloat(static_cast<uint32_t>(getWord(0))); |
1788 | } |
1789 | |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | static APInt doubleToBits(double V) { |
1795 | return APInt(sizeof(double) * CHAR_BIT, DoubleToBits(V)); |
1796 | } |
1797 | |
1798 | |
1799 | |
1800 | |
1801 | |
1802 | static APInt floatToBits(float V) { |
1803 | return APInt(sizeof(float) * CHAR_BIT, FloatToBits(V)); |
1804 | } |
1805 | |
1806 | |
1807 | |
1808 | |
1809 | |
1810 | |
1811 | unsigned logBase2() const { return getActiveBits() - 1; } |
1812 | |
1813 | |
1814 | unsigned ceilLogBase2() const { |
1815 | APInt temp(*this); |
1816 | --temp; |
1817 | return temp.getActiveBits(); |
1818 | } |
1819 | |
1820 | |
1821 | |
1822 | |
1823 | |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | |
1829 | unsigned nearestLogBase2() const { |
1830 | |
1831 | |
1832 | |
1833 | if (BitWidth == 1) |
1834 | return U.VAL - 1; |
1835 | |
1836 | |
1837 | if (isNullValue()) |
1838 | return UINT32_MAX; |
1839 | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | |
1845 | unsigned lg = logBase2(); |
1846 | return lg + unsigned((*this)[lg - 1]); |
1847 | } |
1848 | |
1849 | |
1850 | |
1851 | int32_t exactLogBase2() const { |
1852 | if (!isPowerOf2()) |
1853 | return -1; |
1854 | return logBase2(); |
1855 | } |
1856 | |
1857 | |
1858 | APInt sqrt() const; |
1859 | |
1860 | |
1861 | |
1862 | |
1863 | APInt abs() const { |
1864 | if (isNegative()) |
1865 | return -(*this); |
1866 | return *this; |
1867 | } |
1868 | |
1869 | |
1870 | APInt multiplicativeInverse(const APInt &modulo) const; |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | struct ms; |
1878 | ms magic() const; |
1879 | |
1880 | |
1881 | struct mu; |
1882 | mu magicu(unsigned LeadingZeros = 0) const; |
1883 | |
1884 | |
1885 | |
1886 | |
1887 | |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | static void tcSet(WordType *, WordType, unsigned); |
1897 | |
1898 | |
1899 | static void tcAssign(WordType *, const WordType *, unsigned); |
1900 | |
1901 | |
1902 | static bool tcIsZero(const WordType *, unsigned); |
1903 | |
1904 | |
1905 | static int tcExtractBit(const WordType *, unsigned bit); |
1906 | |
1907 | |
1908 | |
1909 | |
1910 | |
1911 | static void tcExtract(WordType *, unsigned dstCount, |
1912 | const WordType *, unsigned srcBits, |
1913 | unsigned srcLSB); |
1914 | |
1915 | |
1916 | static void tcSetBit(WordType *, unsigned bit); |
1917 | |
1918 | |
1919 | static void tcClearBit(WordType *, unsigned bit); |
1920 | |
1921 | |
1922 | |
1923 | static unsigned tcLSB(const WordType *, unsigned n); |
1924 | static unsigned tcMSB(const WordType *parts, unsigned n); |
1925 | |
1926 | |
1927 | static void tcNegate(WordType *, unsigned); |
1928 | |
1929 | |
1930 | static WordType tcAdd(WordType *, const WordType *, |
1931 | WordType carry, unsigned); |
1932 | |
1933 | static WordType tcAddPart(WordType *, WordType, unsigned); |
1934 | |
1935 | |
1936 | static WordType tcSubtract(WordType *, const WordType *, |
1937 | WordType carry, unsigned); |
1938 | |
1939 | static WordType tcSubtractPart(WordType *, WordType, unsigned); |
1940 | |
1941 | |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | static int tcMultiplyPart(WordType *dst, const WordType *src, |
1952 | WordType multiplier, WordType carry, |
1953 | unsigned srcParts, unsigned dstParts, |
1954 | bool add); |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | |
1960 | static int tcMultiply(WordType *, const WordType *, const WordType *, |
1961 | unsigned); |
1962 | |
1963 | |
1964 | |
1965 | static void tcFullMultiply(WordType *, const WordType *, |
1966 | const WordType *, unsigned, unsigned); |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | |
1976 | |
1977 | static int tcDivide(WordType *lhs, const WordType *rhs, |
1978 | WordType *remainder, WordType *scratch, |
1979 | unsigned parts); |
1980 | |
1981 | |
1982 | |
1983 | static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); |
1984 | |
1985 | |
1986 | |
1987 | static void tcShiftRight(WordType *, unsigned Words, unsigned Count); |
1988 | |
1989 | |
1990 | static void tcAnd(WordType *, const WordType *, unsigned); |
1991 | static void tcOr(WordType *, const WordType *, unsigned); |
1992 | static void tcXor(WordType *, const WordType *, unsigned); |
1993 | static void tcComplement(WordType *, unsigned); |
1994 | |
1995 | |
1996 | static int tcCompare(const WordType *, const WordType *, unsigned); |
1997 | |
1998 | |
1999 | static WordType tcIncrement(WordType *dst, unsigned parts) { |
2000 | return tcAddPart(dst, 1, parts); |
2001 | } |
2002 | |
2003 | |
2004 | static WordType tcDecrement(WordType *dst, unsigned parts) { |
2005 | return tcSubtractPart(dst, 1, parts); |
2006 | } |
2007 | |
2008 | |
2009 | static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); |
2010 | |
2011 | |
2012 | void dump() const; |
2013 | |
2014 | |
2015 | }; |
2016 | |
2017 | |
2018 | struct APInt::ms { |
2019 | APInt m; |
2020 | unsigned s; |
2021 | }; |
2022 | |
2023 | |
2024 | struct APInt::mu { |
2025 | APInt m; |
2026 | bool a; |
2027 | unsigned s; |
2028 | }; |
2029 | |
2030 | inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } |
2031 | |
2032 | inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } |
2033 | |
2034 | |
2035 | |
2036 | |
2037 | inline APInt operator~(APInt v) { |
2038 | v.flipAllBits(); |
2039 | return v; |
2040 | } |
2041 | |
2042 | inline APInt operator&(APInt a, const APInt &b) { |
2043 | a &= b; |
2044 | return a; |
2045 | } |
2046 | |
2047 | inline APInt operator&(const APInt &a, APInt &&b) { |
2048 | b &= a; |
2049 | return std::move(b); |
2050 | } |
2051 | |
2052 | inline APInt operator&(APInt a, uint64_t RHS) { |
2053 | a &= RHS; |
2054 | return a; |
2055 | } |
2056 | |
2057 | inline APInt operator&(uint64_t LHS, APInt b) { |
2058 | b &= LHS; |
2059 | return b; |
2060 | } |
2061 | |
2062 | inline APInt operator|(APInt a, const APInt &b) { |
2063 | a |= b; |
2064 | return a; |
2065 | } |
2066 | |
2067 | inline APInt operator|(const APInt &a, APInt &&b) { |
2068 | b |= a; |
2069 | return std::move(b); |
2070 | } |
2071 | |
2072 | inline APInt operator|(APInt a, uint64_t RHS) { |
2073 | a |= RHS; |
2074 | return a; |
2075 | } |
2076 | |
2077 | inline APInt operator|(uint64_t LHS, APInt b) { |
2078 | b |= LHS; |
2079 | return b; |
2080 | } |
2081 | |
2082 | inline APInt operator^(APInt a, const APInt &b) { |
2083 | a ^= b; |
2084 | return a; |
2085 | } |
2086 | |
2087 | inline APInt operator^(const APInt &a, APInt &&b) { |
2088 | b ^= a; |
2089 | return std::move(b); |
2090 | } |
2091 | |
2092 | inline APInt operator^(APInt a, uint64_t RHS) { |
2093 | a ^= RHS; |
2094 | return a; |
2095 | } |
2096 | |
2097 | inline APInt operator^(uint64_t LHS, APInt b) { |
2098 | b ^= LHS; |
2099 | return b; |
2100 | } |
2101 | |
2102 | inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { |
2103 | I.print(OS, true); |
2104 | return OS; |
2105 | } |
2106 | |
2107 | inline APInt operator-(APInt v) { |
2108 | v.negate(); |
2109 | return v; |
2110 | } |
2111 | |
2112 | inline APInt operator+(APInt a, const APInt &b) { |
2113 | a += b; |
2114 | return a; |
2115 | } |
2116 | |
2117 | inline APInt operator+(const APInt &a, APInt &&b) { |
2118 | b += a; |
2119 | return std::move(b); |
2120 | } |
2121 | |
2122 | inline APInt operator+(APInt a, uint64_t RHS) { |
2123 | a += RHS; |
2124 | return a; |
2125 | } |
2126 | |
2127 | inline APInt operator+(uint64_t LHS, APInt b) { |
2128 | b += LHS; |
2129 | return b; |
2130 | } |
2131 | |
2132 | inline APInt operator-(APInt a, const APInt &b) { |
2133 | a -= b; |
2134 | return a; |
2135 | } |
2136 | |
2137 | inline APInt operator-(const APInt &a, APInt &&b) { |
2138 | b.negate(); |
2139 | b += a; |
2140 | return std::move(b); |
2141 | } |
2142 | |
2143 | inline APInt operator-(APInt a, uint64_t RHS) { |
2144 | a -= RHS; |
2145 | return a; |
2146 | } |
2147 | |
2148 | inline APInt operator-(uint64_t LHS, APInt b) { |
2149 | b.negate(); |
2150 | b += LHS; |
2151 | return b; |
2152 | } |
2153 | |
2154 | inline APInt operator*(APInt a, uint64_t RHS) { |
2155 | a *= RHS; |
2156 | return a; |
2157 | } |
2158 | |
2159 | inline APInt operator*(uint64_t LHS, APInt b) { |
2160 | b *= LHS; |
2161 | return b; |
2162 | } |
2163 | |
2164 | |
2165 | namespace APIntOps { |
2166 | |
2167 | |
2168 | inline const APInt &smin(const APInt &A, const APInt &B) { |
2169 | return A.slt(B) ? A : B; |
2170 | } |
2171 | |
2172 | |
2173 | inline const APInt &smax(const APInt &A, const APInt &B) { |
2174 | return A.sgt(B) ? A : B; |
2175 | } |
2176 | |
2177 | |
2178 | inline const APInt &umin(const APInt &A, const APInt &B) { |
2179 | return A.ult(B) ? A : B; |
2180 | } |
2181 | |
2182 | |
2183 | inline const APInt &umax(const APInt &A, const APInt &B) { |
2184 | return A.ugt(B) ? A : B; |
2185 | } |
2186 | |
2187 | |
2188 | |
2189 | |
2190 | |
2191 | |
2192 | |
2193 | APInt GreatestCommonDivisor(APInt A, APInt B); |
2194 | |
2195 | |
2196 | |
2197 | |
2198 | inline double RoundAPIntToDouble(const APInt &APIVal) { |
2199 | return APIVal.roundToDouble(); |
2200 | } |
2201 | |
2202 | |
2203 | |
2204 | |
2205 | inline double RoundSignedAPIntToDouble(const APInt &APIVal) { |
2206 | return APIVal.signedRoundToDouble(); |
2207 | } |
2208 | |
2209 | |
2210 | inline float RoundAPIntToFloat(const APInt &APIVal) { |
2211 | return float(RoundAPIntToDouble(APIVal)); |
2212 | } |
2213 | |
2214 | |
2215 | |
2216 | |
2217 | inline float RoundSignedAPIntToFloat(const APInt &APIVal) { |
2218 | return float(APIVal.signedRoundToDouble()); |
2219 | } |
2220 | |
2221 | |
2222 | |
2223 | |
2224 | APInt RoundDoubleToAPInt(double Double, unsigned width); |
2225 | |
2226 | |
2227 | |
2228 | |
2229 | inline APInt RoundFloatToAPInt(float Float, unsigned width) { |
2230 | return RoundDoubleToAPInt(double(Float), width); |
2231 | } |
2232 | |
2233 | |
2234 | APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2235 | |
2236 | |
2237 | APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2238 | |
2239 | |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | |
2248 | |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | |
2255 | |
2256 | |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | |
2269 | |
2270 | |
2271 | |
2272 | Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
2273 | unsigned RangeWidth); |
2274 | |
2275 | |
2276 | |
2277 | Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A, |
2278 | const APInt &B); |
2279 | |
2280 | } |
2281 | |
2282 | |
2283 | |
2284 | hash_code hash_value(const APInt &Arg); |
2285 | |
2286 | |
2287 | |
2288 | void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes); |
2289 | |
2290 | |
2291 | |
2292 | void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes); |
2293 | |
2294 | |
2295 | template <> struct DenseMapInfo<APInt> { |
2296 | static inline APInt getEmptyKey() { |
2297 | APInt V(nullptr, 0); |
2298 | V.U.VAL = 0; |
2299 | return V; |
2300 | } |
2301 | |
2302 | static inline APInt getTombstoneKey() { |
2303 | APInt V(nullptr, 0); |
2304 | V.U.VAL = 1; |
2305 | return V; |
2306 | } |
2307 | |
2308 | static unsigned getHashValue(const APInt &Key); |
2309 | |
2310 | static bool isEqual(const APInt &LHS, const APInt &RHS) { |
2311 | return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; |
2312 | } |
2313 | }; |
2314 | |
2315 | } |
2316 | |
2317 | #endif |