LLVM 24.0.0git
GuardWidening.cpp
Go to the documentation of this file.
1//===- GuardWidening.cpp - ---- Guard widening ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the guard widening pass. The semantics of the
10// @llvm.experimental.guard intrinsic lets LLVM transform it so that it fails
11// more often that it did before the transform. This optimization is called
12// "widening" and can be used hoist and common runtime checks in situations like
13// these:
14//
15// %cmp0 = 7 u< Length
16// call @llvm.experimental.guard(i1 %cmp0) [ "deopt"(...) ]
17// call @unknown_side_effects()
18// %cmp1 = 9 u< Length
19// call @llvm.experimental.guard(i1 %cmp1) [ "deopt"(...) ]
20// ...
21//
22// =>
23//
24// %cmp0 = 9 u< Length
25// call @llvm.experimental.guard(i1 %cmp0) [ "deopt"(...) ]
26// call @unknown_side_effects()
27// ...
28//
29// If %cmp0 is false, @llvm.experimental.guard will "deoptimize" back to a
30// generic implementation of the same function, which will have the correct
31// semantics from that point onward. It is always _legal_ to deoptimize (so
32// replacing %cmp0 with false is "correct"), though it may not always be
33// profitable to do so.
34//
35// NB! This pass is a work in progress. It hasn't been tuned to be "production
36// ready" yet. It is known to have quadriatic running time and will not scale
37// to large numbers of guards
38//
39//===----------------------------------------------------------------------===//
40
42#include "ScalarOptions.h"
43#include "llvm/ADT/DenseMap.h"
45#include "llvm/ADT/Statistic.h"
53#include "llvm/IR/Dominators.h"
54#include "llvm/IR/IRBuilder.h"
57#include "llvm/Support/Debug.h"
62#include <functional>
63
64using namespace llvm;
65
66#define DEBUG_TYPE "guard-widening"
67
68STATISTIC(GuardsEliminated, "Number of eliminated guards");
69STATISTIC(CondBranchEliminated, "Number of eliminated conditional branches");
70STATISTIC(FreezeAdded, "Number of freeze instruction introduced");
71
72// Get the condition of \p I. It can either be a guard or a conditional branch.
75 assert(GI->getIntrinsicID() == Intrinsic::experimental_guard &&
76 "Bad guard intrinsic?");
77 return GI->getArgOperand(0);
78 }
79 Value *Cond, *WC;
80 BasicBlock *IfTrueBB, *IfFalseBB;
81 if (parseWidenableBranch(I, Cond, WC, IfTrueBB, IfFalseBB))
82 return Cond;
83
84 return cast<CondBrInst>(I)->getCondition();
85}
86
87// Set the condition for \p I to \p NewCond. \p I can either be a guard or a
88// conditional branch.
89static void setCondition(Instruction *I, Value *NewCond) {
91 assert(GI->getIntrinsicID() == Intrinsic::experimental_guard &&
92 "Bad guard intrinsic?");
93 GI->setArgOperand(0, NewCond);
94 return;
95 }
96 cast<CondBrInst>(I)->setCondition(NewCond);
97}
98
99// Eliminates the guard instruction properly.
100static void eliminateGuard(Instruction *GuardInst, MemorySSAUpdater *MSSAU) {
101 GuardInst->eraseFromParent();
102 if (MSSAU)
103 MSSAU->removeMemoryAccess(GuardInst);
104 ++GuardsEliminated;
105}
106
107/// Find a point at which the widened condition of \p Guard should be inserted.
108/// When it is represented as intrinsic call, we can do it right before the call
109/// instruction. However, when we are dealing with widenable branch, we must
110/// account for the following situation: widening should not turn a
111/// loop-invariant condition into a loop-variant. It means that if
112/// widenable.condition() call is invariant (w.r.t. any loop), the new wide
113/// condition should stay invariant. Otherwise there can be a miscompile, like
114/// the one described at https://github.com/llvm/llvm-project/issues/60234. The
115/// safest way to do it is to expand the new condition at WC's block.
116static std::optional<BasicBlock::iterator>
118 if (isGuard(WCOrGuard))
119 return WCOrGuard->getIterator();
120 if (auto WC = extractWidenableCondition(WCOrGuard))
121 return cast<Instruction>(WC)->getIterator();
122 return std::nullopt;
123}
124
125namespace {
126
127class GuardWideningImpl {
128 DominatorTree &DT;
129 PostDominatorTree *PDT;
130 LoopInfo &LI;
131 AssumptionCache &AC;
132 MemorySSAUpdater *MSSAU;
133
134 /// Together, these describe the region of interest. This might be all of
135 /// the blocks within a function, or only a given loop's blocks and preheader.
136 DomTreeNode *Root;
137 std::function<bool(BasicBlock*)> BlockFilter;
138
139 /// The set of guards and conditional branches whose conditions have been
140 /// widened into dominating guards.
141 SmallVector<Instruction *, 16> EliminatedGuardsAndBranches;
142
143 /// The set of guards which have been widened to include conditions to other
144 /// guards.
145 DenseSet<Instruction *> WidenedGuards;
146
147 /// Try to eliminate instruction \p Instr by widening it into an earlier
148 /// dominating guard. \p DFSI is the DFS iterator on the dominator tree that
149 /// is currently visiting the block containing \p Guard, and \p GuardsPerBlock
150 /// maps BasicBlocks to the set of guards seen in that block.
151 bool eliminateInstrViaWidening(
152 Instruction *Instr, const df_iterator<DomTreeNode *> &DFSI,
153 const DenseMap<BasicBlock *, SmallVector<Instruction *, 8>>
154 &GuardsPerBlock);
155
156 /// Used to keep track of which widening potential is more effective.
157 enum WideningScore {
158 /// Don't widen.
159 WS_IllegalOrNegative,
160
161 /// Widening is performance neutral as far as the cycles spent in check
162 /// conditions goes (but can still help, e.g., code layout, having less
163 /// deopt state).
164 WS_Neutral,
165
166 /// Widening is profitable.
167 WS_Positive,
168
169 /// Widening is very profitable. Not significantly different from \c
170 /// WS_Positive, except by the order.
171 WS_VeryPositive
172 };
173
174 static StringRef scoreTypeToString(WideningScore WS);
175
176 /// Compute the score for widening the condition in \p DominatedInstr
177 /// into \p WideningPoint.
178 WideningScore computeWideningScore(Instruction *DominatedInstr,
179 Instruction *ToWiden,
180 BasicBlock::iterator WideningPoint,
181 SmallVectorImpl<Value *> &ChecksToHoist,
182 SmallVectorImpl<Value *> &ChecksToWiden);
183
184 /// Helper to check if \p V can be hoisted to \p InsertPos.
185 bool canBeHoistedTo(const Value *V, BasicBlock::iterator InsertPos) const {
186 SmallPtrSet<const Instruction *, 8> Visited;
187 return canBeHoistedTo(V, InsertPos, Visited);
188 }
189
190 bool canBeHoistedTo(const Value *V, BasicBlock::iterator InsertPos,
191 SmallPtrSetImpl<const Instruction *> &Visited) const;
192
193 bool canBeHoistedTo(const SmallVectorImpl<Value *> &Checks,
194 BasicBlock::iterator InsertPos) const {
195 return all_of(Checks,
196 [&](const Value *V) { return canBeHoistedTo(V, InsertPos); });
197 }
198 /// Helper to hoist \p V to \p InsertPos. Guaranteed to succeed if \c
199 /// canBeHoistedTo returned true.
200 void makeAvailableAt(Value *V, BasicBlock::iterator InsertPos) const;
201
202 void makeAvailableAt(const SmallVectorImpl<Value *> &Checks,
203 BasicBlock::iterator InsertPos) const {
204 for (Value *V : Checks)
205 makeAvailableAt(V, InsertPos);
206 }
207
208 /// Common helper used by \c widenGuard and \c isWideningCondProfitable. Try
209 /// to generate an expression computing the logical AND of \p ChecksToHoist
210 /// and \p ChecksToWiden. Return true if the expression computing the AND is
211 /// only as expensive as computing one of the set of expressions. If \p
212 /// InsertPt is true then actually generate the resulting expression, make it
213 /// available at \p InsertPt and return it in \p Result (else no change to the
214 /// IR is made).
215 std::optional<Value *>
216 mergeChecks(SmallVectorImpl<Value *> &ChecksToHoist,
217 SmallVectorImpl<Value *> &ChecksToWiden,
218 std::optional<BasicBlock::iterator> InsertPt);
219
220 /// Generate the logical AND of \p ChecksToHoist and \p OldCondition and make
221 /// it available at InsertPt
222 Value *hoistChecks(SmallVectorImpl<Value *> &ChecksToHoist,
223 Value *OldCondition, BasicBlock::iterator InsertPt);
224
225 /// Adds freeze to Orig and push it as far as possible very aggressively.
226 /// Also replaces all uses of frozen instruction with frozen version.
227 Value *freezeAndPush(Value *Orig, BasicBlock::iterator InsertPt);
228
229 /// Represents a range check of the form \c Base + \c Offset u< \c Length,
230 /// with the constraint that \c Length is not negative. \c CheckInst is the
231 /// pre-existing instruction in the IR that computes the result of this range
232 /// check.
233 class RangeCheck {
234 const Value *Base;
235 const ConstantInt *Offset;
236 const Value *Length;
237 ICmpInst *CheckInst;
238
239 public:
240 explicit RangeCheck(const Value *Base, const ConstantInt *Offset,
241 const Value *Length, ICmpInst *CheckInst)
242 : Base(Base), Offset(Offset), Length(Length), CheckInst(CheckInst) {}
243
244 void setBase(const Value *NewBase) { Base = NewBase; }
245 void setOffset(const ConstantInt *NewOffset) { Offset = NewOffset; }
246
247 const Value *getBase() const { return Base; }
248 const ConstantInt *getOffset() const { return Offset; }
249 const APInt &getOffsetValue() const { return getOffset()->getValue(); }
250 const Value *getLength() const { return Length; };
251 ICmpInst *getCheckInst() const { return CheckInst; }
252
253 void print(raw_ostream &OS, bool PrintTypes = false) {
254 OS << "Base: ";
255 Base->printAsOperand(OS, PrintTypes);
256 OS << " Offset: ";
257 Offset->printAsOperand(OS, PrintTypes);
258 OS << " Length: ";
259 Length->printAsOperand(OS, PrintTypes);
260 }
261
262 LLVM_DUMP_METHOD void dump() {
263 print(dbgs());
264 dbgs() << "\n";
265 }
266 };
267
268 /// Parse \p ToParse into a conjunction (logical-and) of range checks; and
269 /// append them to \p Checks. Returns true on success, may clobber \c Checks
270 /// on failure.
271 bool parseRangeChecks(SmallVectorImpl<Value *> &ToParse,
272 SmallVectorImpl<RangeCheck> &Checks) {
273 for (auto CheckCond : ToParse) {
274 if (!parseRangeChecks(CheckCond, Checks))
275 return false;
276 }
277 return true;
278 }
279
280 bool parseRangeChecks(Value *CheckCond, SmallVectorImpl<RangeCheck> &Checks);
281
282 /// Combine the checks in \p Checks into a smaller set of checks and append
283 /// them into \p CombinedChecks. Return true on success (i.e. all of checks
284 /// in \p Checks were combined into \p CombinedChecks). Clobbers \p Checks
285 /// and \p CombinedChecks on success and on failure.
286 bool combineRangeChecks(SmallVectorImpl<RangeCheck> &Checks,
287 SmallVectorImpl<RangeCheck> &CombinedChecks) const;
288
289 /// Can we compute the logical AND of \p ChecksToHoist and \p ChecksToWiden
290 /// for the price of computing only one of the set of expressions?
291 bool isWideningCondProfitable(SmallVectorImpl<Value *> &ChecksToHoist,
292 SmallVectorImpl<Value *> &ChecksToWiden) {
293 return mergeChecks(ChecksToHoist, ChecksToWiden, /*InsertPt=*/std::nullopt)
294 .has_value();
295 }
296
297 /// Widen \p ChecksToWiden to fail if any of \p ChecksToHoist is false
298 void widenGuard(SmallVectorImpl<Value *> &ChecksToHoist,
299 SmallVectorImpl<Value *> &ChecksToWiden,
300 Instruction *ToWiden) {
301 auto InsertPt = findInsertionPointForWideCondition(ToWiden);
302 auto MergedCheck = mergeChecks(ChecksToHoist, ChecksToWiden, InsertPt);
303 Value *Result = MergedCheck ? *MergedCheck
304 : hoistChecks(ChecksToHoist,
305 getCondition(ToWiden), *InsertPt);
306
307 if (isGuardAsWidenableBranch(ToWiden)) {
309 return;
310 }
311 setCondition(ToWiden, Result);
312 }
313
314public:
315 explicit GuardWideningImpl(DominatorTree &DT, PostDominatorTree *PDT,
316 LoopInfo &LI, AssumptionCache &AC,
317 MemorySSAUpdater *MSSAU, DomTreeNode *Root,
318 std::function<bool(BasicBlock *)> BlockFilter)
319 : DT(DT), PDT(PDT), LI(LI), AC(AC), MSSAU(MSSAU), Root(Root),
320 BlockFilter(BlockFilter) {}
321
322 /// The entry point for this pass.
323 bool run();
324};
325} // namespace
326
327static bool isSupportedGuardInstruction(const Instruction *Insn) {
328 if (isGuard(Insn))
329 return true;
330 if (ScalarOptions::Global.guard_widening_widen_branch_guards &&
332 return true;
333 return false;
334}
335
336bool GuardWideningImpl::run() {
337 DenseMap<BasicBlock *, SmallVector<Instruction *, 8>> GuardsInBlock;
338 bool Changed = false;
339 for (auto DFI = df_begin(Root), DFE = df_end(Root);
340 DFI != DFE; ++DFI) {
341 auto *BB = (*DFI)->getBlock();
342 if (!BlockFilter(BB))
343 continue;
344
345 auto &CurrentList = GuardsInBlock[BB];
346
347 for (auto &I : *BB)
349 CurrentList.push_back(cast<Instruction>(&I));
350
351 for (auto *II : CurrentList)
352 Changed |= eliminateInstrViaWidening(II, DFI, GuardsInBlock);
353 }
354
355 assert(EliminatedGuardsAndBranches.empty() || Changed);
356 for (auto *I : EliminatedGuardsAndBranches)
357 if (!WidenedGuards.count(I)) {
358 assert(isa<ConstantInt>(getCondition(I)) && "Should be!");
360 eliminateGuard(I, MSSAU);
361 else {
363 "Eliminated something other than guard or branch?");
364 ++CondBranchEliminated;
365 }
366 }
367
368 return Changed;
369}
370
371bool GuardWideningImpl::eliminateInstrViaWidening(
372 Instruction *Instr, const df_iterator<DomTreeNode *> &DFSI,
373 const DenseMap<BasicBlock *, SmallVector<Instruction *, 8>>
374 &GuardsInBlock) {
375 SmallVector<Value *> ChecksToHoist;
376 parseWidenableGuard(Instr, ChecksToHoist);
377 // Ignore trivial true or false conditions. These instructions will be
378 // trivially eliminated by any cleanup pass. Do not erase them because other
379 // guards can possibly be widened into them.
380 if (ChecksToHoist.empty() ||
381 (ChecksToHoist.size() == 1 && isa<ConstantInt>(ChecksToHoist.front())))
382 return false;
383
384 Instruction *BestSoFar = nullptr;
385 auto BestScoreSoFar = WS_IllegalOrNegative;
386
387 // In the set of dominating guards, find the one we can merge GuardInst with
388 // for the most profit.
389 for (unsigned i = 0, e = DFSI.getPathLength(); i != e; ++i) {
390 auto *CurBB = DFSI.getPath(i)->getBlock();
391 if (!BlockFilter(CurBB))
392 break;
393 assert(GuardsInBlock.count(CurBB) && "Must have been populated by now!");
394 const auto &GuardsInCurBB = GuardsInBlock.find(CurBB)->second;
395
396 auto I = GuardsInCurBB.begin();
397 auto E = Instr->getParent() == CurBB ? find(GuardsInCurBB, Instr)
398 : GuardsInCurBB.end();
399
400#ifndef NDEBUG
401 {
402 unsigned Index = 0;
403 for (auto &I : *CurBB) {
404 if (Index == GuardsInCurBB.size())
405 break;
406 if (GuardsInCurBB[Index] == &I)
407 Index++;
408 }
409 assert(Index == GuardsInCurBB.size() &&
410 "Guards expected to be in order!");
411 }
412#endif
413
414 assert((i == (e - 1)) == (Instr->getParent() == CurBB) && "Bad DFS?");
415
416 for (auto *Candidate : make_range(I, E)) {
417 auto WideningPoint = findInsertionPointForWideCondition(Candidate);
418 if (!WideningPoint)
419 continue;
420 SmallVector<Value *> CandidateChecks;
421 parseWidenableGuard(Candidate, CandidateChecks);
422 auto Score = computeWideningScore(Instr, Candidate, *WideningPoint,
423 ChecksToHoist, CandidateChecks);
424 LLVM_DEBUG(dbgs() << "Score between " << *Instr << " and " << *Candidate
425 << " is " << scoreTypeToString(Score) << "\n");
426 if (Score > BestScoreSoFar) {
427 BestScoreSoFar = Score;
428 BestSoFar = Candidate;
429 }
430 }
431 }
432
433 if (BestScoreSoFar == WS_IllegalOrNegative) {
434 LLVM_DEBUG(dbgs() << "Did not eliminate guard " << *Instr << "\n");
435 return false;
436 }
437
438 assert(BestSoFar != Instr && "Should have never visited same guard!");
439 assert(DT.dominates(BestSoFar, Instr) && "Should be!");
440
441 LLVM_DEBUG(dbgs() << "Widening " << *Instr << " into " << *BestSoFar
442 << " with score " << scoreTypeToString(BestScoreSoFar)
443 << "\n");
444 SmallVector<Value *> ChecksToWiden;
445 parseWidenableGuard(BestSoFar, ChecksToWiden);
446 widenGuard(ChecksToHoist, ChecksToWiden, BestSoFar);
447 auto NewGuardCondition = ConstantInt::getTrue(Instr->getContext());
448 setCondition(Instr, NewGuardCondition);
449 EliminatedGuardsAndBranches.push_back(Instr);
450 WidenedGuards.insert(BestSoFar);
451 return true;
452}
453
454GuardWideningImpl::WideningScore GuardWideningImpl::computeWideningScore(
455 Instruction *DominatedInstr, Instruction *ToWiden,
456 BasicBlock::iterator WideningPoint, SmallVectorImpl<Value *> &ChecksToHoist,
457 SmallVectorImpl<Value *> &ChecksToWiden) {
458 Loop *DominatedInstrLoop = LI.getLoopFor(DominatedInstr->getParent());
459 Loop *DominatingGuardLoop = LI.getLoopFor(WideningPoint->getParent());
460 bool HoistingOutOfLoop = false;
461
462 if (DominatingGuardLoop != DominatedInstrLoop) {
463 // Be conservative and don't widen into a sibling loop. TODO: If the
464 // sibling is colder, we should consider allowing this.
465 if (DominatingGuardLoop &&
466 !DominatingGuardLoop->contains(DominatedInstrLoop))
467 return WS_IllegalOrNegative;
468
469 HoistingOutOfLoop = true;
470 }
471
472 if (!canBeHoistedTo(ChecksToHoist, WideningPoint))
473 return WS_IllegalOrNegative;
474 // Further in the GuardWideningImpl::hoistChecks the entire condition might be
475 // widened, not the parsed list of checks. So we need to check the possibility
476 // of that condition hoisting.
477 if (!canBeHoistedTo(getCondition(ToWiden), WideningPoint))
478 return WS_IllegalOrNegative;
479
480 // If the guard was conditional executed, it may never be reached
481 // dynamically. There are two potential downsides to hoisting it out of the
482 // conditionally executed region: 1) we may spuriously deopt without need and
483 // 2) we have the extra cost of computing the guard condition in the common
484 // case. At the moment, we really only consider the second in our heuristic
485 // here. TODO: evaluate cost model for spurious deopt
486 // NOTE: As written, this also lets us hoist right over another guard which
487 // is essentially just another spelling for control flow.
488 if (isWideningCondProfitable(ChecksToHoist, ChecksToWiden))
489 return HoistingOutOfLoop ? WS_VeryPositive : WS_Positive;
490
491 if (HoistingOutOfLoop)
492 return WS_Positive;
493
494 // For a given basic block \p BB, return its successor which is guaranteed or
495 // highly likely will be taken as its successor.
496 auto GetLikelySuccessor = [](const BasicBlock * BB)->const BasicBlock * {
497 if (auto *UniqueSucc = BB->getUniqueSuccessor())
498 return UniqueSucc;
499 auto *Term = BB->getTerminator();
500 Value *Cond = nullptr;
501 const BasicBlock *IfTrue = nullptr, *IfFalse = nullptr;
502 using namespace PatternMatch;
503 if (!match(Term, m_Br(m_Value(Cond), m_BasicBlock(IfTrue),
504 m_BasicBlock(IfFalse))))
505 return nullptr;
506 // For constant conditions, only one dynamical successor is possible
507 if (auto *ConstCond = dyn_cast<ConstantInt>(Cond))
508 return ConstCond->isAllOnesValue() ? IfTrue : IfFalse;
509 // If one of successors ends with deopt, another one is likely.
510 if (IfFalse->getPostdominatingDeoptimizeCall())
511 return IfTrue;
513 return IfFalse;
514 // TODO: Use branch frequency metatada to allow hoisting through non-deopt
515 // branches?
516 return nullptr;
517 };
518
519 // Returns true if we might be hoisting above explicit control flow into a
520 // considerably hotter block. Note that this completely ignores implicit
521 // control flow (guards, calls which throw, etc...). That choice appears
522 // arbitrary (we assume that implicit control flow exits are all rare).
523 auto MaybeHoistingToHotterBlock = [&]() {
524 const auto *DominatingBlock = WideningPoint->getParent();
525 const auto *DominatedBlock = DominatedInstr->getParent();
526
527 // Descend as low as we can, always taking the likely successor.
528 assert(DT.isReachableFromEntry(DominatingBlock) && "Unreached code");
529 assert(DT.isReachableFromEntry(DominatedBlock) && "Unreached code");
530 assert(DT.dominates(DominatingBlock, DominatedBlock) && "No dominance");
531 while (DominatedBlock != DominatingBlock) {
532 auto *LikelySucc = GetLikelySuccessor(DominatingBlock);
533 // No likely successor?
534 if (!LikelySucc)
535 break;
536 // Only go down the dominator tree.
537 if (!DT.properlyDominates(DominatingBlock, LikelySucc))
538 break;
539 DominatingBlock = LikelySucc;
540 }
541
542 // Found?
543 if (DominatedBlock == DominatingBlock)
544 return false;
545 // We followed the likely successor chain and went past the dominated
546 // block. It means that the dominated guard is in dead/very cold code.
547 if (!DT.dominates(DominatingBlock, DominatedBlock))
548 return true;
549 // TODO: diamond, triangle cases
550 if (!PDT)
551 return true;
552 return !PDT->dominates(DominatedBlock, DominatingBlock);
553 };
554
555 return MaybeHoistingToHotterBlock() ? WS_IllegalOrNegative : WS_Neutral;
556}
557
558bool GuardWideningImpl::canBeHoistedTo(
559 const Value *V, BasicBlock::iterator Loc,
560 SmallPtrSetImpl<const Instruction *> &Visited) const {
561 auto *Inst = dyn_cast<Instruction>(V);
562 if (!Inst || DT.dominates(Inst, Loc) || Visited.count(Inst))
563 return true;
564
565 if (!isSafeToSpeculativelyExecute(Inst, Loc, &AC, &DT) ||
566 Inst->mayReadFromMemory())
567 return false;
568
569 Visited.insert(Inst);
570
571 // We only want to go _up_ the dominance chain when recursing.
572 assert(!isa<PHINode>(Loc) &&
573 "PHIs should return false for isSafeToSpeculativelyExecute");
574 assert(DT.isReachableFromEntry(Inst->getParent()) &&
575 "We did a DFS from the block entry!");
576 return all_of(Inst->operands(),
577 [&](Value *Op) { return canBeHoistedTo(Op, Loc, Visited); });
578}
579
580void GuardWideningImpl::makeAvailableAt(Value *V,
581 BasicBlock::iterator Loc) const {
582 auto *Inst = dyn_cast<Instruction>(V);
583 if (!Inst || DT.dominates(Inst, Loc))
584 return;
585
586 assert(isSafeToSpeculativelyExecute(Inst, Loc, &AC, &DT) &&
587 !Inst->mayReadFromMemory() &&
588 "Should've checked with canBeHoistedTo!");
589
590 for (Value *Op : Inst->operands())
591 makeAvailableAt(Op, Loc);
592
593 Inst->moveBefore(*Loc->getParent(), Loc);
594}
595
596// Return Instruction before which we can insert freeze for the value V as close
597// to def as possible. If there is no place to add freeze, return empty.
598static std::optional<BasicBlock::iterator>
600 auto *I = dyn_cast<Instruction>(V);
601 if (!I)
602 return DT.getRoot()->getFirstNonPHIOrDbgOrAlloca()->getIterator();
603
604 std::optional<BasicBlock::iterator> Res = I->getInsertionPointAfterDef();
605 // If there is no place to add freeze - return nullptr.
606 if (!Res || !DT.dominates(I, &**Res))
607 return std::nullopt;
608
609 Instruction *ResInst = &**Res;
610
611 // If there is a User dominated by original I, then it should be dominated
612 // by Freeze instruction as well.
613 if (any_of(I->users(), [&](User *U) {
614 Instruction *User = cast<Instruction>(U);
615 return ResInst != User && DT.dominates(I, User) &&
616 !DT.dominates(ResInst, User);
617 }))
618 return std::nullopt;
619 return Res;
620}
621
622Value *GuardWideningImpl::freezeAndPush(Value *Orig,
623 BasicBlock::iterator InsertPt) {
624 if (isGuaranteedNotToBePoison(Orig, nullptr, InsertPt, &DT))
625 return Orig;
626 std::optional<BasicBlock::iterator> InsertPtAtDef =
627 getFreezeInsertPt(Orig, DT);
628 if (!InsertPtAtDef) {
629 FreezeInst *FI = new FreezeInst(Orig, "gw.freeze");
630 FI->insertBefore(*InsertPt->getParent(), InsertPt);
631 return FI;
632 }
633 if (isa<Constant>(Orig) || isa<GlobalValue>(Orig)) {
634 BasicBlock::iterator InsertPt = *InsertPtAtDef;
635 FreezeInst *FI = new FreezeInst(Orig, "gw.freeze");
636 FI->insertBefore(*InsertPt->getParent(), InsertPt);
637 return FI;
638 }
639
640 SmallPtrSet<Value *, 16> Visited;
641 SmallVector<Value *, 16> Worklist;
642 SmallPtrSet<Instruction *, 16> DropPoisonFlags;
643 SmallVector<Value *, 16> NeedFreeze;
644 DenseMap<Value *, FreezeInst *> CacheOfFreezes;
645
646 // A bit overloaded data structures. Visited contains constant/GV
647 // if we already met it. In this case CacheOfFreezes has a freeze if it is
648 // required.
649 auto handleConstantOrGlobal = [&](Use &U) {
650 Value *Def = U.get();
651 if (!isa<Constant>(Def) && !isa<GlobalValue>(Def))
652 return false;
653
654 if (Visited.insert(Def).second) {
655 if (isGuaranteedNotToBePoison(Def, nullptr, InsertPt, &DT))
656 return true;
657 BasicBlock::iterator InsertPt = *getFreezeInsertPt(Def, DT);
658 FreezeInst *FI = new FreezeInst(Def, Def->getName() + ".gw.fr");
659 FI->insertBefore(*InsertPt->getParent(), InsertPt);
660 CacheOfFreezes[Def] = FI;
661 }
662
663 if (auto It = CacheOfFreezes.find(Def); It != CacheOfFreezes.end())
664 U.set(It->second);
665 return true;
666 };
667
668 Worklist.push_back(Orig);
669 while (!Worklist.empty()) {
670 Value *V = Worklist.pop_back_val();
671 if (!Visited.insert(V).second)
672 continue;
673
674 if (isGuaranteedNotToBePoison(V, nullptr, InsertPt, &DT))
675 continue;
676
679 /*ConsiderFlagsAndMetadata*/ false)) {
680 NeedFreeze.push_back(V);
681 continue;
682 }
683 // Check all operands. If for any of them we cannot insert Freeze,
684 // stop here. Otherwise, iterate.
685 if (any_of(I->operands(), [&](Value *Op) {
686 return isa<Instruction>(Op) && !getFreezeInsertPt(Op, DT);
687 })) {
688 NeedFreeze.push_back(I);
689 continue;
690 }
691 DropPoisonFlags.insert(I);
692 for (Use &U : I->operands())
693 if (!handleConstantOrGlobal(U))
694 Worklist.push_back(U.get());
695 }
696 for (Instruction *I : DropPoisonFlags)
697 I->dropPoisonGeneratingAnnotations();
698
699 Value *Result = Orig;
700 for (Value *V : NeedFreeze) {
701 BasicBlock::iterator FreezeInsertPt = *getFreezeInsertPt(V, DT);
702 FreezeInst *FI = new FreezeInst(V, V->getName() + ".gw.fr");
703 FI->insertBefore(*FreezeInsertPt->getParent(), FreezeInsertPt);
704 ++FreezeAdded;
705 if (V == Orig)
706 Result = FI;
707 V->replaceUsesWithIf(
708 FI, [&](const Use & U)->bool { return U.getUser() != FI; });
709 }
710
711 return Result;
712}
713
714std::optional<Value *>
715GuardWideningImpl::mergeChecks(SmallVectorImpl<Value *> &ChecksToHoist,
716 SmallVectorImpl<Value *> &ChecksToWiden,
717 std::optional<BasicBlock::iterator> InsertPt) {
718 using namespace llvm::PatternMatch;
719
720 Value *Result = nullptr;
721 {
722 // L >u C0 && L >u C1 -> L >u max(C0, C1)
723 ConstantInt *RHS0, *RHS1;
724 Value *LHS;
725 CmpPredicate Pred0, Pred1;
726 // TODO: Support searching for pairs to merge from both whole lists of
727 // ChecksToHoist and ChecksToWiden.
728 if (ChecksToWiden.size() == 1 && ChecksToHoist.size() == 1 &&
729 match(ChecksToWiden.front(),
730 m_ICmp(Pred0, m_Value(LHS), m_ConstantInt(RHS0))) &&
731 match(ChecksToHoist.front(),
732 m_ICmp(Pred1, m_Specific(LHS), m_ConstantInt(RHS1)))) {
733
734 ConstantRange CR0 =
736 ConstantRange CR1 =
738
739 // Given what we're doing here and the semantics of guards, it would
740 // be correct to use a subset intersection, but that may be too
741 // aggressive in cases we care about.
742 if (std::optional<ConstantRange> Intersect =
743 CR0.exactIntersectWith(CR1)) {
744 APInt NewRHSAP;
746 if (Intersect->getEquivalentICmp(Pred, NewRHSAP)) {
747 if (InsertPt) {
748 ConstantInt *NewRHS =
749 ConstantInt::get((*InsertPt)->getContext(), NewRHSAP);
750 assert(canBeHoistedTo(LHS, *InsertPt) && "must be");
751 makeAvailableAt(LHS, *InsertPt);
752 Result = new ICmpInst(*InsertPt, Pred, LHS, NewRHS, "wide.chk");
753 }
754 return Result;
755 }
756 }
757 }
758 }
759
760 {
762 if (parseRangeChecks(ChecksToWiden, Checks) &&
763 parseRangeChecks(ChecksToHoist, Checks) &&
764 combineRangeChecks(Checks, CombinedChecks)) {
765 if (InsertPt) {
766 for (auto &RC : CombinedChecks) {
767 makeAvailableAt(RC.getCheckInst(), *InsertPt);
768 if (Result)
769 Result = BinaryOperator::CreateAnd(RC.getCheckInst(), Result, "",
770 *InsertPt);
771 else
772 Result = RC.getCheckInst();
773 }
774 assert(Result && "Failed to find result value");
775 Result->setName("wide.chk");
776 Result = freezeAndPush(Result, *InsertPt);
777 }
778 return Result;
779 }
780 }
781 // We were not able to compute ChecksToHoist AND ChecksToWiden for the price
782 // of one.
783 return std::nullopt;
784}
785
786Value *GuardWideningImpl::hoistChecks(SmallVectorImpl<Value *> &ChecksToHoist,
787 Value *OldCondition,
788 BasicBlock::iterator InsertPt) {
789 assert(!ChecksToHoist.empty());
790 IRBuilder<> Builder(InsertPt);
791 makeAvailableAt(ChecksToHoist, InsertPt);
792 makeAvailableAt(OldCondition, InsertPt);
793 Value *Result = Builder.CreateAnd(ChecksToHoist);
794 Result = freezeAndPush(Result, InsertPt);
795 Result = Builder.CreateAnd(OldCondition, Result);
796 Result->setName("wide.chk");
797 return Result;
798}
799
800bool GuardWideningImpl::parseRangeChecks(
801 Value *CheckCond, SmallVectorImpl<GuardWideningImpl::RangeCheck> &Checks) {
802 using namespace llvm::PatternMatch;
803
804 auto *IC = dyn_cast<ICmpInst>(CheckCond);
805 if (!IC || !IC->getOperand(0)->getType()->isIntegerTy() ||
806 (IC->getPredicate() != ICmpInst::ICMP_ULT &&
807 IC->getPredicate() != ICmpInst::ICMP_UGT))
808 return false;
809
810 const Value *CmpLHS = IC->getOperand(0), *CmpRHS = IC->getOperand(1);
811 if (IC->getPredicate() == ICmpInst::ICMP_UGT)
812 std::swap(CmpLHS, CmpRHS);
813
814 auto &DL = IC->getDataLayout();
815
816 GuardWideningImpl::RangeCheck Check(
817 CmpLHS, cast<ConstantInt>(ConstantInt::getNullValue(CmpRHS->getType())),
818 CmpRHS, IC);
819
820 if (!isKnownNonNegative(Check.getLength(), DL))
821 return false;
822
823 // What we have in \c Check now is a correct interpretation of \p CheckCond.
824 // Try to see if we can move some constant offsets into the \c Offset field.
825
826 bool Changed;
827 auto &Ctx = CheckCond->getContext();
828
829 do {
830 Value *OpLHS;
831 ConstantInt *OpRHS;
832 Changed = false;
833
834#ifndef NDEBUG
835 auto *BaseInst = dyn_cast<Instruction>(Check.getBase());
836 assert((!BaseInst || DT.isReachableFromEntry(BaseInst->getParent())) &&
837 "Unreachable instruction?");
838#endif
839
840 if (match(Check.getBase(), m_Add(m_Value(OpLHS), m_ConstantInt(OpRHS)))) {
841 Check.setBase(OpLHS);
842 APInt NewOffset = Check.getOffsetValue() + OpRHS->getValue();
843 Check.setOffset(ConstantInt::get(Ctx, NewOffset));
844 Changed = true;
845 } else if (match(Check.getBase(),
846 m_Or(m_Value(OpLHS), m_ConstantInt(OpRHS)))) {
847 KnownBits Known = computeKnownBits(OpLHS, DL);
848 if ((OpRHS->getValue() & Known.Zero) == OpRHS->getValue()) {
849 Check.setBase(OpLHS);
850 APInt NewOffset = Check.getOffsetValue() + OpRHS->getValue();
851 Check.setOffset(ConstantInt::get(Ctx, NewOffset));
852 Changed = true;
853 }
854 }
855 } while (Changed);
856
857 Checks.push_back(Check);
858 return true;
859}
860
861bool GuardWideningImpl::combineRangeChecks(
862 SmallVectorImpl<GuardWideningImpl::RangeCheck> &Checks,
863 SmallVectorImpl<GuardWideningImpl::RangeCheck> &RangeChecksOut) const {
864 unsigned OldCount = Checks.size();
865 while (!Checks.empty()) {
866 // Pick all of the range checks with a specific base and length, and try to
867 // merge them.
868 const Value *CurrentBase = Checks.front().getBase();
869 const Value *CurrentLength = Checks.front().getLength();
870
872
873 auto IsCurrentCheck = [&](GuardWideningImpl::RangeCheck &RC) {
874 return RC.getBase() == CurrentBase && RC.getLength() == CurrentLength;
875 };
876
877 copy_if(Checks, std::back_inserter(CurrentChecks), IsCurrentCheck);
878 erase_if(Checks, IsCurrentCheck);
879
880 assert(CurrentChecks.size() != 0 && "We know we have at least one!");
881
882 if (CurrentChecks.size() < 3) {
883 llvm::append_range(RangeChecksOut, CurrentChecks);
884 continue;
885 }
886
887 // CurrentChecks.size() will typically be 3 here, but so far there has been
888 // no need to hard-code that fact.
889
890 llvm::sort(CurrentChecks, [&](const GuardWideningImpl::RangeCheck &LHS,
891 const GuardWideningImpl::RangeCheck &RHS) {
892 return LHS.getOffsetValue().slt(RHS.getOffsetValue());
893 });
894
895 // Note: std::sort should not invalidate the ChecksStart iterator.
896
897 const ConstantInt *MinOffset = CurrentChecks.front().getOffset();
898 const ConstantInt *MaxOffset = CurrentChecks.back().getOffset();
899
900 unsigned BitWidth = MaxOffset->getValue().getBitWidth();
901 if ((MaxOffset->getValue() - MinOffset->getValue())
903 return false;
904
905 APInt MaxDiff = MaxOffset->getValue() - MinOffset->getValue();
906 const APInt &HighOffset = MaxOffset->getValue();
907 auto OffsetOK = [&](const GuardWideningImpl::RangeCheck &RC) {
908 return (HighOffset - RC.getOffsetValue()).ult(MaxDiff);
909 };
910
911 if (MaxDiff.isMinValue() || !all_of(drop_begin(CurrentChecks), OffsetOK))
912 return false;
913
914 // We have a series of f+1 checks as:
915 //
916 // I+k_0 u< L ... Chk_0
917 // I+k_1 u< L ... Chk_1
918 // ...
919 // I+k_f u< L ... Chk_f
920 //
921 // with forall i in [0,f]: k_f-k_i u< k_f-k_0 ... Precond_0
922 // k_f-k_0 u< INT_MIN+k_f ... Precond_1
923 // k_f != k_0 ... Precond_2
924 //
925 // Claim:
926 // Chk_0 AND Chk_f implies all the other checks
927 //
928 // Informal proof sketch:
929 //
930 // We will show that the integer range [I+k_0,I+k_f] does not unsigned-wrap
931 // (i.e. going from I+k_0 to I+k_f does not cross the -1,0 boundary) and
932 // thus I+k_f is the greatest unsigned value in that range.
933 //
934 // This combined with Ckh_(f+1) shows that everything in that range is u< L.
935 // Via Precond_0 we know that all of the indices in Chk_0 through Chk_(f+1)
936 // lie in [I+k_0,I+k_f], this proving our claim.
937 //
938 // To see that [I+k_0,I+k_f] is not a wrapping range, note that there are
939 // two possibilities: I+k_0 u< I+k_f or I+k_0 >u I+k_f (they can't be equal
940 // since k_0 != k_f). In the former case, [I+k_0,I+k_f] is not a wrapping
941 // range by definition, and the latter case is impossible:
942 //
943 // 0-----I+k_f---I+k_0----L---INT_MAX,INT_MIN------------------(-1)
944 // xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
945 //
946 // For Chk_0 to succeed, we'd have to have k_f-k_0 (the range highlighted
947 // with 'x' above) to be at least >u INT_MIN.
948
949 RangeChecksOut.emplace_back(CurrentChecks.front());
950 RangeChecksOut.emplace_back(CurrentChecks.back());
951 }
952
953 assert(RangeChecksOut.size() <= OldCount && "We pessimized!");
954 return RangeChecksOut.size() != OldCount;
955}
956
957#ifndef NDEBUG
958StringRef GuardWideningImpl::scoreTypeToString(WideningScore WS) {
959 switch (WS) {
960 case WS_IllegalOrNegative:
961 return "IllegalOrNegative";
962 case WS_Neutral:
963 return "Neutral";
964 case WS_Positive:
965 return "Positive";
966 case WS_VeryPositive:
967 return "VeryPositive";
968 }
969
970 llvm_unreachable("Fully covered switch above!");
971}
972#endif
973
976 // Avoid requesting analyses if there are no guards or widenable conditions.
977 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
978 F.getParent(), Intrinsic::experimental_guard);
979 bool HasIntrinsicGuards = GuardDecl && !GuardDecl->use_empty();
981 F.getParent(), Intrinsic::experimental_widenable_condition);
982 bool HasWidenableConditions = WCDecl && !WCDecl->use_empty();
983 if (!HasIntrinsicGuards && !HasWidenableConditions)
984 return PreservedAnalyses::all();
985 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
986 auto &LI = AM.getResult<LoopAnalysis>(F);
987 auto &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
988 auto &AC = AM.getResult<AssumptionAnalysis>(F);
989 auto *MSSAA = AM.getCachedResult<MemorySSAAnalysis>(F);
990 std::unique_ptr<MemorySSAUpdater> MSSAU;
991 if (MSSAA)
992 MSSAU = std::make_unique<MemorySSAUpdater>(&MSSAA->getMSSA());
993 if (!GuardWideningImpl(DT, &PDT, LI, AC, MSSAU ? MSSAU.get() : nullptr,
994 DT.getRootNode(), [](BasicBlock *) { return true; })
995 .run())
996 return PreservedAnalyses::all();
997
1001 return PA;
1002}
1003
1006 LPMUpdater &U) {
1007 BasicBlock *RootBB = L.getLoopPredecessor();
1008 if (!RootBB)
1009 RootBB = L.getHeader();
1010 auto BlockFilter = [&](BasicBlock *BB) {
1011 return BB == RootBB || L.contains(BB);
1012 };
1013 std::unique_ptr<MemorySSAUpdater> MSSAU;
1014 if (AR.MSSA)
1015 MSSAU = std::make_unique<MemorySSAUpdater>(AR.MSSA);
1016 if (!GuardWideningImpl(AR.DT, nullptr, AR.LI, AR.AC,
1017 MSSAU ? MSSAU.get() : nullptr, AR.DT.getNode(RootBB),
1018 BlockFilter)
1019 .run())
1020 return PreservedAnalyses::all();
1021
1022 auto PA = getLoopPassPreservedAnalyses();
1023 if (AR.MSSA)
1024 PA.preserve<MemorySSAAnalysis>();
1025 return PA;
1026}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:686
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define Check(C,...)
static bool isSupportedGuardInstruction(const Instruction *Insn)
static void eliminateGuard(Instruction *GuardInst, MemorySSAUpdater *MSSAU)
static std::optional< BasicBlock::iterator > findInsertionPointForWideCondition(Instruction *WCOrGuard)
Find a point at which the widened condition of Guard should be inserted.
static Value * getCondition(Instruction *I)
static void setCondition(Instruction *I, Value *NewCond)
static std::optional< BasicBlock::iterator > getFreezeInsertPt(Value *V, const DominatorTree &DT)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition APInt.h:413
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
LLVM_ABI const CallInst * getPostdominatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize that is present either in current ...
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI std::optional< ConstantRange > exactIntersectWith(const ConstantRange &CR) const
Intersect the two ranges and return the result if it can be represented exactly, otherwise return std...
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:763
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:767
iterator end()
Definition DenseMap.h:687
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A wrapper class for inspecting calls to intrinsic functions.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:922
LLVM_ABI void removeMemoryAccess(MemoryAccess *, bool OptimizePhis=false)
Remove a MemoryAccess from MemorySSA, including updating all definitions and uses.
Analysis pass which computes a PostDominatorTree.
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
LLVM Value Representation.
Definition Value.h:75
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:187
unsigned getPathLength() const
Return the length of the path from the entry node to the current node, counting both nodes.
NodeRef getPath(unsigned n) const
Return the n'th node in the path from the entry node to the current node.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
auto m_Value()
Match an arbitrary value and ignore it.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
iterator end() const
Definition BasicBlock.h:89
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
@ Known
Known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
df_iterator< T > df_begin(const T &G)
LLVM_ABI Value * extractWidenableCondition(const User *U)
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI void parseWidenableGuard(const User *U, llvm::SmallVectorImpl< Value * > &Checks)
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P)
Provide wrappers to std::copy_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1807
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI bool parseWidenableBranch(const User *U, Value *&Condition, Value *&WidenableCondition, BasicBlock *&IfTrueBB, BasicBlock *&IfFalseBB)
If U is widenable branch looking like: cond = ... wc = call i1 @llvm.experimental....
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isGuardAsWidenableBranch(const User *U)
Returns true iff U has semantics of a guard expressed in a form of a widenable conditional branch to ...
LLVM_ABI void setWidenableBranchCond(CondBrInst *WidenableBR, Value *Cond)
Given a branch we know is widenable (defined per Analysis/GuardUtils.h), set it's condition such that...
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2208
df_iterator< T > df_end(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...