LLVM 24.0.0git
NaryReassociate.cpp
Go to the documentation of this file.
1//===- NaryReassociate.cpp - Reassociate n-ary expressions ----------------===//
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 pass reassociates n-ary add expressions and eliminates the redundancy
10// exposed by the reassociation.
11//
12// A motivating example:
13//
14// void foo(int a, int b) {
15// bar(a + b);
16// bar((a + 2) + b);
17// }
18//
19// An ideal compiler should reassociate (a + 2) + b to (a + b) + 2 and simplify
20// the above code to
21//
22// int t = a + b;
23// bar(t);
24// bar(t + 2);
25//
26// However, the Reassociate pass is unable to do that because it processes each
27// instruction individually and believes (a + 2) + b is the best form according
28// to its rank system.
29//
30// To address this limitation, NaryReassociate reassociates an expression in a
31// form that reuses existing instructions. As a result, NaryReassociate can
32// reassociate (a + 2) + b in the example to (a + b) + 2 because it detects that
33// (a + b) is computed before.
34//
35// NaryReassociate works as follows. For every instruction in the form of (a +
36// b) + c, it checks whether a + c or b + c is already computed by a dominating
37// instruction. If so, it then reassociates (a + b) + c into (a + c) + b or (b +
38// c) + a and removes the redundancy accordingly. To efficiently look up whether
39// an expression is computed before, we store each instruction seen and its SCEV
40// into an SCEV-to-instruction map.
41//
42// Although the algorithm pattern-matches only ternary additions, it
43// automatically handles many >3-ary expressions by walking through the function
44// in the depth-first order. For example, given
45//
46// (a + c) + d
47// ((a + b) + c) + d
48//
49// NaryReassociate first rewrites (a + b) + c to (a + c) + b, and then rewrites
50// ((a + c) + b) + d into ((a + c) + d) + b.
51//
52// Finally, the above dominator-based algorithm may need to be run multiple
53// iterations before emitting optimal code. One source of this need is that we
54// only split an operand when it is used only once. The above algorithm can
55// eliminate an instruction and decrease the usage count of its operands. As a
56// result, an instruction that previously had multiple uses may become a
57// single-use instruction and thus eligible for split consideration. For
58// example,
59//
60// ac = a + c
61// ab = a + b
62// abc = ab + c
63// ab2 = ab + b
64// ab2c = ab2 + c
65//
66// In the first iteration, we cannot reassociate abc to ac+b because ab is used
67// twice. However, we can reassociate ab2c to abc+b in the first iteration. As a
68// result, ab2 becomes dead and ab will be used only once in the second
69// iteration.
70//
71// Limitations and TODO items:
72//
73// 1) We only considers n-ary adds and muls for now. This should be extended
74// and generalized.
75//
76//===----------------------------------------------------------------------===//
77
87#include "llvm/IR/BasicBlock.h"
88#include "llvm/IR/Constants.h"
89#include "llvm/IR/DataLayout.h"
91#include "llvm/IR/Dominators.h"
92#include "llvm/IR/Function.h"
94#include "llvm/IR/IRBuilder.h"
95#include "llvm/IR/InstrTypes.h"
96#include "llvm/IR/Instruction.h"
98#include "llvm/IR/Module.h"
99#include "llvm/IR/Operator.h"
100#include "llvm/IR/PatternMatch.h"
101#include "llvm/IR/Type.h"
102#include "llvm/IR/Value.h"
103#include "llvm/IR/ValueHandle.h"
105#include "llvm/Pass.h"
106#include "llvm/Support/Casting.h"
111#include <cassert>
112#include <cstdint>
113
114using namespace llvm;
115using namespace PatternMatch;
116
117#define DEBUG_TYPE "nary-reassociate"
118
119namespace {
120
121class NaryReassociateLegacyPass : public FunctionPass {
122public:
123 static char ID;
124
125 NaryReassociateLegacyPass() : FunctionPass(ID) {
127 }
128
129 bool doInitialization(Module &M) override {
130 return false;
131 }
132
133 bool runOnFunction(Function &F) override;
134
135 void getAnalysisUsage(AnalysisUsage &AU) const override {
143 AU.setPreservesCFG();
144 }
145
146private:
148};
149
150} // end anonymous namespace
151
152char NaryReassociateLegacyPass::ID = 0;
153
154INITIALIZE_PASS_BEGIN(NaryReassociateLegacyPass, "nary-reassociate",
155 "Nary reassociation", false, false)
161INITIALIZE_PASS_END(NaryReassociateLegacyPass, "nary-reassociate",
162 "Nary reassociation", false, false)
163
165 return new NaryReassociateLegacyPass();
166}
167
168bool NaryReassociateLegacyPass::runOnFunction(Function &F) {
169 if (skipFunction(F))
170 return false;
171
172 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
173 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
174 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
175 auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
176 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
177
178 return Impl.runImpl(F, AC, DT, SE, TLI, TTI);
179}
180
183 auto *AC = &AM.getResult<AssumptionAnalysis>(F);
184 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
185 auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
186 auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
187 auto *TTI = &AM.getResult<TargetIRAnalysis>(F);
188
189 if (!runImpl(F, AC, DT, SE, TLI, TTI))
190 return PreservedAnalyses::all();
191
195 return PA;
196}
197
200 TargetLibraryInfo *TLI_,
201 TargetTransformInfo *TTI_) {
202 AC = AC_;
203 DT = DT_;
204 SE = SE_;
205 TLI = TLI_;
206 TTI = TTI_;
207 DL = &F.getDataLayout();
208
209 bool Changed = false, ChangedInThisIteration;
210 do {
211 ChangedInThisIteration = doOneIteration(F);
212 Changed |= ChangedInThisIteration;
213 } while (ChangedInThisIteration);
214 return Changed;
215}
216
217bool NaryReassociatePass::doOneIteration(Function &F) {
218 bool Changed = false;
219 SeenExprs.clear();
220 // Process the basic blocks in a depth first traversal of the dominator
221 // tree. This order ensures that all bases of a candidate are in Candidates
222 // when we process it.
224 for (const auto Node : depth_first(DT)) {
225 BasicBlock *BB = Node->getBlock();
226 for (Instruction &OrigI : *BB) {
227 SCEVUse OrigSCEV = nullptr;
228 if (Instruction *NewI = tryReassociate(&OrigI, OrigSCEV)) {
229 Changed = true;
230 OrigI.replaceAllUsesWith(NewI);
231
232 // Add 'OrigI' to the list of dead instructions.
233 DeadInsts.push_back(WeakTrackingVH(&OrigI));
234 // Add the rewritten instruction to SeenExprs; the original
235 // instruction is deleted.
236 SCEVUse NewSCEV = SE->getSCEV(NewI);
237 SeenExprs[NewSCEV].push_back(WeakTrackingVH(NewI));
238
239 // Ideally, NewSCEV should equal OldSCEV because tryReassociate(I)
240 // is equivalent to I. However, ScalarEvolution::getSCEV may
241 // weaken nsw causing NewSCEV not to equal OldSCEV. For example,
242 // suppose we reassociate
243 // I = &a[sext(i +nsw j)] // assuming sizeof(a[0]) = 4
244 // to
245 // NewI = &a[sext(i)] + sext(j).
246 //
247 // ScalarEvolution computes
248 // getSCEV(I) = a + 4 * sext(i + j)
249 // getSCEV(newI) = a + 4 * sext(i) + 4 * sext(j)
250 // which are different SCEVs.
251 //
252 // To alleviate this issue of ScalarEvolution not always capturing
253 // equivalence, we add I to SeenExprs[OldSCEV] as well so that we can
254 // map both SCEV before and after tryReassociate(I) to I.
255 //
256 // This improvement is exercised in @reassociate_gep_nsw in
257 // nary-gep.ll.
258 if (NewSCEV != OrigSCEV)
259 SeenExprs[OrigSCEV].push_back(WeakTrackingVH(NewI));
260 } else if (OrigSCEV)
261 SeenExprs[OrigSCEV].push_back(WeakTrackingVH(&OrigI));
262 }
263 }
264 // Delete all dead instructions from 'DeadInsts'.
265 // Please note ScalarEvolution is updated along the way.
267 DeadInsts, TLI, nullptr, [this](Value *V) { SE->forgetValue(V); });
268
269 return Changed;
270}
271
272Instruction *NaryReassociatePass::tryReassociate(Instruction *I,
273 SCEVUse &OrigSCEV) {
274
275 if (!SE->isSCEVable(I->getType()))
276 return nullptr;
277
278 switch (I->getOpcode()) {
279 case Instruction::Add:
280 case Instruction::Mul:
281 OrigSCEV = SE->getSCEV(I);
282 return tryReassociateBinaryOp(cast<BinaryOperator>(I));
283 case Instruction::GetElementPtr:
284 OrigSCEV = SE->getSCEV(I);
285 return tryReassociateGEP(cast<GetElementPtrInst>(I));
286 default:
287 break;
288 }
289
290 // Try to match signed/unsigned Min/Max.
291 if (match(I, m_MaxOrMin(m_Value(), m_Value()))) {
292 OrigSCEV = SE->getSCEV(I);
294 tryReassociateMinOrMax(cast<IntrinsicInst>(I)));
295 }
296
297 return nullptr;
298}
299
301 const TargetTransformInfo *TTI) {
302 SmallVector<const Value *, 4> Indices(GEP->indices());
303 return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(),
305}
306
307Instruction *NaryReassociatePass::tryReassociateGEP(GetElementPtrInst *GEP) {
308 // Not worth reassociating GEP if it is foldable.
309 if (isGEPFoldable(GEP, TTI))
310 return nullptr;
311
313 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
314 if (GTI.isSequential()) {
315 if (auto *NewGEP = tryReassociateGEPAtIndex(GEP, I - 1,
316 GTI.getIndexedType())) {
317 return NewGEP;
318 }
319 }
320 }
321 return nullptr;
322}
323
324bool NaryReassociatePass::requiresSignExtension(Value *Index,
325 GetElementPtrInst *GEP) {
326 unsigned IndexSizeInBits =
327 DL->getIndexSizeInBits(GEP->getType()->getPointerAddressSpace());
328 return cast<IntegerType>(Index->getType())->getBitWidth() < IndexSizeInBits;
329}
330
331GetElementPtrInst *
332NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
333 unsigned I, Type *IndexedType) {
334 SimplifyQuery SQ(*DL, DT, AC, GEP);
335 Value *IndexToSplit = GEP->getOperand(I + 1);
336 if (SExtInst *SExt = dyn_cast<SExtInst>(IndexToSplit)) {
337 IndexToSplit = SExt->getOperand(0);
338 } else if (ZExtInst *ZExt = dyn_cast<ZExtInst>(IndexToSplit)) {
339 // zext can be treated as sext if the source is non-negative.
340 if (isKnownNonNegative(ZExt->getOperand(0), SQ))
341 IndexToSplit = ZExt->getOperand(0);
342 }
343
344 if (AddOperator *AO = dyn_cast<AddOperator>(IndexToSplit)) {
345 // If the I-th index needs sext and the underlying add is not equipped with
346 // nsw, we cannot split the add because
347 // sext(LHS + RHS) != sext(LHS) + sext(RHS).
348 if (requiresSignExtension(IndexToSplit, GEP) &&
349 computeOverflowForSignedAdd(AO, SQ) != OverflowResult::NeverOverflows)
350 return nullptr;
351
352 Value *LHS = AO->getOperand(0), *RHS = AO->getOperand(1);
353 // IndexToSplit = LHS + RHS.
354 if (auto *NewGEP = tryReassociateGEPAtIndex(GEP, I, LHS, RHS, IndexedType))
355 return NewGEP;
356 // Symmetrically, try IndexToSplit = RHS + LHS.
357 if (LHS != RHS) {
358 if (auto *NewGEP =
359 tryReassociateGEPAtIndex(GEP, I, RHS, LHS, IndexedType))
360 return NewGEP;
361 }
362 }
363 return nullptr;
364}
365
366GetElementPtrInst *
367NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
368 unsigned I, Value *LHS,
369 Value *RHS, Type *IndexedType) {
370 // Look for GEP's closest dominator that has the same SCEV as GEP except that
371 // the I-th index is replaced with LHS.
372 SmallVector<SCEVUse, 4> IndexExprs;
373 for (Use &Index : GEP->indices())
374 IndexExprs.push_back(SE->getSCEV(Index));
375 // Replace the I-th index with LHS.
376 IndexExprs[I] = SE->getSCEV(LHS);
377 Type *GEPArgType = SE->getEffectiveSCEVType(GEP->getOperand(I)->getType());
378 Type *LHSType = SE->getEffectiveSCEVType(LHS->getType());
379 size_t LHSSize = DL->getTypeSizeInBits(LHSType).getFixedValue();
380 size_t GEPArgSize = DL->getTypeSizeInBits(GEPArgType).getFixedValue();
381 if (isKnownNonNegative(LHS, SimplifyQuery(*DL, DT, AC, GEP)) &&
382 LHSSize < GEPArgSize) {
383 // Zero-extend LHS if it is non-negative. InstCombine canonicalizes sext to
384 // zext if the source operand is proved non-negative. We should do that
385 // consistently so that CandidateExpr more likely appears before. See
386 // @reassociate_gep_assume for an example of this canonicalization.
387 IndexExprs[I] = SE->getZeroExtendExpr(IndexExprs[I], GEPArgType);
388 }
389 SCEVUse CandidateExpr = SE->getGEPExpr(cast<GEPOperator>(GEP), IndexExprs);
390
391 Value *Candidate = findClosestMatchingDominator(CandidateExpr, GEP);
392 if (Candidate == nullptr)
393 return nullptr;
394
395 IRBuilder<> Builder(GEP);
396 // Candidate should have the same pointer type as GEP.
397 assert(Candidate->getType() == GEP->getType());
398
399 // NewGEP = (char *)Candidate + RHS * sizeof(IndexedType)
400 uint64_t IndexedSize = DL->getTypeAllocSize(IndexedType);
401 Type *ElementType = GEP->getResultElementType();
402 uint64_t ElementSize = DL->getTypeAllocSize(ElementType);
403 // Another less rare case: because I is not necessarily the last index of the
404 // GEP, the size of the type at the I-th index (IndexedSize) is not
405 // necessarily divisible by ElementSize. For example,
406 //
407 // #pragma pack(1)
408 // struct S {
409 // int a[3];
410 // int64 b[8];
411 // };
412 // #pragma pack()
413 //
414 // sizeof(S) = 100 is indivisible by sizeof(int64) = 8.
415 //
416 // TODO: bail out on this case for now. We could emit uglygep.
417 if (ElementSize == 0 || IndexedSize % ElementSize != 0)
418 return nullptr;
419
420 // NewGEP = &Candidate[RHS * (sizeof(IndexedType) / sizeof(Candidate[0])));
421 Type *PtrIdxTy = DL->getIndexType(GEP->getType());
422 if (RHS->getType() != PtrIdxTy)
423 RHS = Builder.CreateSExtOrTrunc(RHS, PtrIdxTy);
424 if (IndexedSize != ElementSize) {
425 RHS = Builder.CreateMul(
426 RHS, ConstantInt::get(PtrIdxTy, IndexedSize / ElementSize));
427 }
428 GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(
429 Builder.CreateGEP(GEP->getResultElementType(), Candidate, RHS));
430 NewGEP->setIsInBounds(GEP->isInBounds());
431 NewGEP->takeName(GEP);
432 return NewGEP;
433}
434
435Instruction *NaryReassociatePass::tryReassociateBinaryOp(BinaryOperator *I) {
436 Value *LHS = I->getOperand(0), *RHS = I->getOperand(1);
437 // There is no need to reassociate 0.
438 if (SE->getSCEV(I)->isZero())
439 return nullptr;
440 if (auto *NewI = tryReassociateBinaryOp(LHS, RHS, I))
441 return NewI;
442 if (auto *NewI = tryReassociateBinaryOp(RHS, LHS, I))
443 return NewI;
444 return nullptr;
445}
446
447Instruction *NaryReassociatePass::tryReassociateBinaryOp(Value *LHS, Value *RHS,
448 BinaryOperator *I) {
449 Value *A = nullptr, *B = nullptr;
450 // To be conservative, we reassociate I only when it is the only user of (A op
451 // B).
452 if (LHS->hasOneUse() && matchTernaryOp(I, LHS, A, B)) {
453 // I = (A op B) op RHS
454 // = (A op RHS) op B or (B op RHS) op A
455 SCEVUse AExpr = SE->getSCEV(A), BExpr = SE->getSCEV(B);
456 SCEVUse RHSExpr = SE->getSCEV(RHS);
457 if (BExpr != RHSExpr) {
458 if (auto *NewI =
459 tryReassociatedBinaryOp(getBinarySCEV(I, AExpr, RHSExpr), B, I))
460 return NewI;
461 }
462 if (AExpr != RHSExpr) {
463 if (auto *NewI =
464 tryReassociatedBinaryOp(getBinarySCEV(I, BExpr, RHSExpr), A, I))
465 return NewI;
466 }
467 }
468 return nullptr;
469}
470
471Instruction *NaryReassociatePass::tryReassociatedBinaryOp(SCEVUse LHSExpr,
472 Value *RHS,
473 BinaryOperator *I) {
474 // Look for the closest dominator LHS of I that computes LHSExpr, and replace
475 // I with LHS op RHS.
476 auto *LHS = findClosestMatchingDominator(LHSExpr, I);
477 if (LHS == nullptr)
478 return nullptr;
479
480 Instruction *NewI = nullptr;
481 switch (I->getOpcode()) {
482 case Instruction::Add:
483 NewI = BinaryOperator::CreateAdd(LHS, RHS, "", I->getIterator());
484 break;
485 case Instruction::Mul:
486 NewI = BinaryOperator::CreateMul(LHS, RHS, "", I->getIterator());
487 break;
488 default:
489 llvm_unreachable("Unexpected instruction.");
490 }
491 NewI->setDebugLoc(I->getDebugLoc());
492 NewI->takeName(I);
493 return NewI;
494}
495
496bool NaryReassociatePass::matchTernaryOp(BinaryOperator *I, Value *V,
497 Value *&Op1, Value *&Op2) {
498 switch (I->getOpcode()) {
499 case Instruction::Add:
500 return match(V, m_Add(m_Value(Op1), m_Value(Op2)));
501 case Instruction::Mul:
502 return match(V, m_Mul(m_Value(Op1), m_Value(Op2)));
503 default:
504 llvm_unreachable("Unexpected instruction.");
505 }
506 return false;
507}
508
509SCEVUse NaryReassociatePass::getBinarySCEV(BinaryOperator *I, SCEVUse LHS,
510 SCEVUse RHS) {
511 switch (I->getOpcode()) {
512 case Instruction::Add:
513 return SE->getAddExpr(LHS, RHS);
514 case Instruction::Mul:
515 return SE->getMulExpr(LHS, RHS);
516 default:
517 llvm_unreachable("Unexpected instruction.");
518 }
519 return nullptr;
520}
521
523NaryReassociatePass::findClosestMatchingDominator(SCEVUse CandidateExpr,
524 Instruction *Dominatee) {
525 auto Pos = SeenExprs.find(CandidateExpr);
526 if (Pos == SeenExprs.end())
527 return nullptr;
528
529 auto &Candidates = Pos->second;
530 // Because we process the basic blocks in pre-order of the dominator tree, a
531 // candidate that doesn't dominate the current instruction won't dominate any
532 // future instruction either. Therefore, we pop it out of the stack. This
533 // optimization makes the algorithm O(n).
534 while (!Candidates.empty()) {
535 // Candidates stores WeakTrackingVHs, so a candidate can be nullptr if it's
536 // removed during rewriting.
537 if (Value *Candidate = Candidates.pop_back_val()) {
538 Instruction *CandidateInstruction = cast<Instruction>(Candidate);
539 if (!DT->dominates(CandidateInstruction, Dominatee))
540 continue;
541
542 // Make sure that the instruction is safe to reuse without introducing
543 // poison.
544 SmallVector<Instruction *> DropPoisonGeneratingInsts;
545 if (!SE->canReuseInstruction(CandidateExpr, CandidateInstruction,
546 DropPoisonGeneratingInsts))
547 continue;
548
549 for (Instruction *I : DropPoisonGeneratingInsts)
550 I->dropPoisonGeneratingAnnotations();
551
552 return CandidateInstruction;
553 }
554 }
555 return nullptr;
556}
557
559 switch (IntrinID) {
560 case Intrinsic::smax:
561 return scSMaxExpr;
562 case Intrinsic::umax:
563 return scUMaxExpr;
564 case Intrinsic::smin:
565 return scSMinExpr;
566 case Intrinsic::umin:
567 return scUMinExpr;
568 default:
569 llvm_unreachable("Can't convert MinMax pattern to SCEV type");
570 return scUnknown;
571 }
572}
573
574Value *NaryReassociatePass::tryReassociateMinOrMax(IntrinsicInst *I) {
575 Value *LHS = I->getArgOperand(0);
576 Value *RHS = I->getArgOperand(1);
577 if (auto *RHSI = dyn_cast<IntrinsicInst>(RHS);
578 RHSI && RHSI->getIntrinsicID() == I->getIntrinsicID())
579 std::swap(LHS, RHS);
580 auto *LHSI = dyn_cast<IntrinsicInst>(LHS);
581 if (!LHSI || LHSI->getIntrinsicID() != I->getIntrinsicID())
582 return nullptr;
583
584 Value *A = LHSI->getArgOperand(0), *B = LHSI->getArgOperand(1);
585
586 if (LHS->hasNUsesOrMore(3) ||
587 // The optimization is profitable only if LHS can be removed in the end.
588 // In other words LHS should be used (directly or indirectly) by I only.
589 llvm::any_of(LHS->users(), [&](auto *U) {
590 return U != I && !(U->hasOneUser() && *U->users().begin() == I);
591 }))
592 return nullptr;
593
594 auto tryCombination = [&](Value *A, SCEVUse AExpr, Value *B, SCEVUse BExpr,
595 Value *C, SCEVUse CExpr) -> Value * {
596 SmallVector<SCEVUse, 2> Ops1{BExpr, AExpr};
597 SCEVTypes SCEVType = convertToSCEVType(I->getIntrinsicID());
598 SCEVUse R1Expr = SE->getMinMaxExpr(SCEVType, Ops1);
599
600 Instruction *R1MinMax = findClosestMatchingDominator(R1Expr, I);
601
602 if (!R1MinMax)
603 return nullptr;
604
605 LLVM_DEBUG(dbgs() << "NARY: Found common sub-expr: " << *R1MinMax << "\n");
606
607 SmallVector<SCEVUse, 2> Ops2{SE->getUnknown(C), SE->getUnknown(R1MinMax)};
608 SCEVUse R2Expr = SE->getMinMaxExpr(SCEVType, Ops2);
609
610 SCEVExpander Expander(*SE, "nary-reassociate");
611 Value *NewMinMax = Expander.expandCodeFor(R2Expr, I->getType(), I);
612 NewMinMax->setName(Twine(I->getName()).concat(".nary"));
613
614 LLVM_DEBUG(dbgs() << "NARY: Deleting: " << *I << "\n"
615 << "NARY: Inserting: " << *NewMinMax << "\n");
616 return NewMinMax;
617 };
618
619 SCEVUse AExpr = SE->getSCEV(A);
620 SCEVUse BExpr = SE->getSCEV(B);
621 SCEVUse RHSExpr = SE->getSCEV(RHS);
622
623 if (BExpr != RHSExpr) {
624 // Try (A op RHS) op B
625 if (auto *NewMinMax = tryCombination(A, AExpr, RHS, RHSExpr, B, BExpr))
626 return NewMinMax;
627 }
628
629 if (AExpr != RHSExpr) {
630 // Try (RHS op B) op A
631 if (auto *NewMinMax = tryCombination(RHS, RHSExpr, B, BExpr, A, AExpr))
632 return NewMinMax;
633 }
634
635 return nullptr;
636}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static bool isGEPFoldable(GetElementPtrInst *GEP, const TargetTransformInfo *TTI)
static SCEVTypes convertToSCEVType(Intrinsic::ID IntrinID)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
LLVM_ABI void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
LLVM_ABI bool runImpl(Function &F, AssumptionCache *AC_, DominatorTree *DT_, ScalarEvolution *SE_, TargetLibraryInfo *TLI_, TargetTransformInfo *TTI_)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCC_Free
Expected to fold away in lowering.
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition Value.cpp:155
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
Value handle that is nullable, but tries to track the Value.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_MaxOrMin(const Opnd0 &Op0, const Opnd1 &Op1)
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:68
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI FunctionPass * createNaryReassociatePass()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1746
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
generic_gep_type_iterator<> gep_type_iterator
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI void initializeNaryReassociateLegacyPassPass(PassRegistry &)
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructionsPermissive(SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow instructions that are not...
Definition Local.cpp:550
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
gep_type_iterator gep_type_begin(const User *GEP)
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
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.
SCEVUseT< const SCEV * > SCEVUse
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880