LLVM: lib/Transforms/Scalar/ConstantHoisting.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
65#include
66#include
67#include
68#include
69
70using namespace llvm;
72
73#define DEBUG_TYPE "consthoist"
74
75STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
76STATISTIC(NumConstantsRebased, "Number of constants rebased");
77
80 cl::desc("Enable the use of the block frequency analysis to reduce the "
81 "chance to execute const materialization more frequently than "
82 "without hoisting."));
83
86 cl::desc("Try hoisting constant gep expressions"));
87
90 cl::desc("Do not rebase if number of dependent constants of a Base is less "
91 "than this number."),
93
94namespace {
95
96
97class ConstantHoistingLegacyPass : public FunctionPass {
98public:
99 static char ID;
100
103 }
104
106
107 StringRef getPassName() const override { return "Constant Hoisting"; }
108
109 void getAnalysisUsage(AnalysisUsage &AU) const override {
112 AU.addRequired();
113 AU.addRequired();
114 AU.addRequired();
115 AU.addRequired();
116 }
117
118private:
119 ConstantHoistingPass Impl;
120};
121
122}
123
124char ConstantHoistingLegacyPass::ID = 0;
125
127 "Constant Hoisting", false, false)
134
136 return new ConstantHoistingLegacyPass();
137}
138
139
140bool ConstantHoistingLegacyPass::runOnFunction(Function &Fn) {
141 if (skipFunction(Fn))
142 return false;
143
144 LLVM_DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n");
146
147 bool MadeChange =
148 Impl.runImpl(Fn, getAnalysis().getTTI(Fn),
149 getAnalysis().getDomTree(),
151 ? &getAnalysis().getBFI()
152 : nullptr,
154 &getAnalysis().getPSI());
155
156 LLVM_DEBUG(dbgs() << "********** End Constant Hoisting **********\n");
157
158 return MadeChange;
159}
160
161void ConstantHoistingPass::collectMatInsertPts(
163 SmallVectorImplBasicBlock::iterator &MatInsertPts) const {
164 for (const RebasedConstantInfo &RCI : RebasedConstants)
165 for (const ConstantUser &U : RCI.Uses)
166 MatInsertPts.emplace_back(findMatInsertPt(U.Inst, U.OpndIdx));
167}
168
169
171 unsigned Idx) const {
172
173
174 if (Idx != ~0U) {
177 if (CastInst->isCast())
178 return CastInst->getIterator();
179 }
180
181
184
185
186
187 assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!");
188 BasicBlock *InsertionBlock = nullptr;
190 InsertionBlock = cast(Inst)->getIncomingBlock(Idx);
191 if (!InsertionBlock->isEHPad()) {
193 }
194 } else {
195 InsertionBlock = Inst->getParent();
196 }
197
198
199
200
201 auto *IDom = DT->getNode(InsertionBlock)->getIDom();
202 while (IDom->getBlock()->isEHPad()) {
203 assert(Entry != IDom->getBlock() && "eh pad in entry block");
204 IDom = IDom->getIDom();
205 }
206
207 return IDom->getBlock()->getTerminator()->getIterator();
208}
209
210
211
212
216 assert(!BBs.count(Entry) && "Assume Entry is not in BBs");
217
219
220
221
223 for (auto *BB : BBs) {
224
226 continue;
227 Path.clear();
228
229
231
233 do {
234 Path.insert(Node);
237 break;
238 }
240 "Entry doens't dominate current Node");
243
244
245
247 continue;
248
249
251 }
252
253
254
255 unsigned Idx = 0;
258 while (Idx != Orders.size()) {
261 if (Candidates.count(ChildDomNode->getBlock()))
262 Orders.push_back(ChildDomNode->getBlock());
263 }
264 }
265
266
267 using InsertPtsCostPair =
269
270
271
275 bool NodeInBBs = BBs.count(Node);
276 auto &[InsertPts, InsertPtsFreq] = InsertPtsMap[Node];
277
278
279 if (Node == Entry) {
282 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1))
284 else
286 break;
287 }
288
290
291
292 auto &[ParentInsertPts, ParentPtsFreq] = InsertPtsMap[Parent];
293
294
295
296
297
298
299 if (NodeInBBs ||
300 (->isEHPad() &&
302 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1)))) {
303 ParentInsertPts.insert(Node);
305 } else {
306 ParentInsertPts.insert_range(InsertPts);
307 ParentPtsFreq += InsertPtsFreq;
308 }
309 }
310}
311
312
313SetVectorBasicBlock::iterator
314ConstantHoistingPass::findConstantInsertionPoint(
315 const ConstantInfo &ConstInfo,
318
319 SetVector<BasicBlock *> BBs;
320 SetVectorBasicBlock::iterator InsertPts;
321
323 BBs.insert(MatInsertPt->getParent());
324
325 if (BBs.count(Entry)) {
327 return InsertPts;
328 }
329
330 if (BFI) {
332 for (BasicBlock *BB : BBs)
333 InsertPts.insert(BB->getFirstInsertionPt());
334 return InsertPts;
335 }
336
337 while (BBs.size() >= 2) {
339 BB1 = BBs.pop_back_val();
340 BB2 = BBs.pop_back_val();
341 BB = DT->findNearestCommonDominator(BB1, BB2);
342 if (BB == Entry) {
344 return InsertPts;
345 }
346 BBs.insert(BB);
347 }
348 assert((BBs.size() == 1) && "Expected only one element.");
349 Instruction &FirstInst = (*BBs.begin())->front();
350 InsertPts.insert(findMatInsertPt(&FirstInst));
351 return InsertPts;
352}
353
354
355
356
357
358
359
360void ConstantHoistingPass::collectConstantCandidates(
361 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx,
362 ConstantInt *ConstInt) {
364 return;
365
367
368
373 else
377
378
380 ConstCandMapType::iterator Itr;
382 ConstPtrUnionType Cand = ConstInt;
383 std::tie(Itr, Inserted) = ConstCandMap.try_emplace(Cand);
384 if (Inserted) {
385 ConstIntCandVec.push_back(ConstantCandidate(ConstInt));
386 Itr->second = ConstIntCandVec.size() - 1;
387 }
388 ConstIntCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
390 << "Collect constant " << *ConstInt << " from " << *Inst
391 << " with cost " << Cost << '\n';
392 else dbgs() << "Collect constant " << *ConstInt
393 << " indirectly from " << *Inst << " via "
395 << '\n';);
396 }
397}
398
399
400void ConstantHoistingPass::collectConstantCandidates(
401 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx,
402 ConstantExpr *ConstExpr) {
403
405 return;
406
408 if (!BaseGV)
409 return;
410
411
413 IntegerType *OffsetTy = DL->getIndexType(*Ctx, GVPtrTy->getAddressSpace());
414 APInt Offset(DL->getTypeSizeInBits(OffsetTy), 0, true);
416
417
418
419
420
421 if (!GEPO->isInBounds())
422 return;
423
424 if (!GEPO->accumulateConstantOffset(*DL, Offset))
425 return;
426
427 if (.isIntN(32))
428 return;
429
430
431
432
433
437 ConstCandVecType &ExprCandVec = ConstGEPCandMap[BaseGV];
438 ConstCandMapType::iterator Itr;
440 ConstPtrUnionType Cand = ConstExpr;
441 std::tie(Itr, Inserted) = ConstCandMap.try_emplace(Cand);
442 if (Inserted) {
443 ExprCandVec.push_back(ConstantCandidate(
444 ConstantInt::get(Type::getInt32Ty(*Ctx), Offset.getLimitedValue()),
445 ConstExpr));
446 Itr->second = ExprCandVec.size() - 1;
447 }
448 ExprCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
449}
450
451
452void ConstantHoistingPass::collectConstantCandidates(
453 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx) {
455
456
458 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
459 return;
460 }
461
462
464
465
466 if (!CastInst->isCast())
467 return;
468
470
471
472 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
473 return;
474 }
475 }
476
477
479
481 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstExpr);
482
483
484 if (!ConstExpr->isCast())
485 return;
486
488
489
490 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
491 return;
492 }
493 }
494}
495
496
497
498void ConstantHoistingPass::collectConstantCandidates(
499 ConstCandMapType &ConstCandMap, Instruction *Inst) {
500
502 return;
503
504
505 for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) {
506
507
508
509
510
512 collectConstantCandidates(ConstCandMap, Inst, Idx);
513 }
514 }
515}
516
517
518
519void ConstantHoistingPass::collectConstantCandidates(Function &Fn) {
520 ConstCandMapType ConstCandMap;
521 for (BasicBlock &BB : Fn) {
522
523 if (!DT->isReachableFromEntry(&BB))
524 continue;
525 for (Instruction &Inst : BB)
527 collectConstantCandidates(ConstCandMap, &Inst);
528 }
529}
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554unsigned
555ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S,
556 ConstCandVecType::iterator E,
557 ConstCandVecType::iterator &MaxCostItr) {
558 unsigned NumUses = 0;
559
560 if (!OptForSize || std::distance(S,E) > 100) {
561 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
562 NumUses += ConstCand->Uses.size();
563 if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost)
564 MaxCostItr = ConstCand;
565 }
566 return NumUses;
567 }
568
569 LLVM_DEBUG(dbgs() << "== Maximize constants in range ==\n");
571 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
572 auto Value = ConstCand->ConstInt->getValue();
573 Type *Ty = ConstCand->ConstInt->getType();
575 NumUses += ConstCand->Uses.size();
576 LLVM_DEBUG(dbgs() << "= Constant: " << ConstCand->ConstInt->getValue()
577 << "\n");
578
579 for (auto User : ConstCand->Uses) {
580 unsigned Opcode = User.Inst->getOpcode();
581 unsigned OpndIdx = User.OpndIdx;
585
586 for (auto C2 = S; C2 != E; ++C2) {
587 APInt Diff = C2->ConstInt->getValue() - ConstCand->ConstInt->getValue();
590 Cost -= ImmCosts;
592 << "has penalty: " << ImmCosts << "\n"
593 << "Adjusted cost: " << Cost << "\n");
594 }
595 }
597 if (Cost > MaxCost) {
598 MaxCost = Cost;
599 MaxCostItr = ConstCand;
600 LLVM_DEBUG(dbgs() << "New candidate: " << MaxCostItr->ConstInt->getValue()
601 << "\n");
602 }
603 }
604 return NumUses;
605}
606
607
608
609void ConstantHoistingPass::findAndMakeBaseConstant(
610 ConstCandVecType::iterator S, ConstCandVecType::iterator E,
611 SmallVectorImplconsthoist::ConstantInfo &ConstInfoVec) {
612 auto MaxCostItr = S;
613 unsigned NumUses = maximizeConstantsInRange(S, E, MaxCostItr);
614
615
616 if (NumUses <= 1)
617 return;
618
619 ConstantInt *ConstInt = MaxCostItr->ConstInt;
620 ConstantExpr *ConstExpr = MaxCostItr->ConstExpr;
621 ConstantInfo ConstInfo;
622 ConstInfo.BaseInt = ConstInt;
623 ConstInfo.BaseExpr = ConstExpr;
625
626
627 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
628 APInt Diff = ConstCand->ConstInt->getValue() - ConstInt->getValue();
629 Constant *Offset = Diff == 0 ? nullptr : ConstantInt::get(Ty, Diff);
630 Type *ConstTy =
631 ConstCand->ConstExpr ? ConstCand->ConstExpr->getType() : nullptr;
633 RebasedConstantInfo(std::move(ConstCand->Uses), Offset, ConstTy));
634 }
635 ConstInfoVec.push_back(std::move(ConstInfo));
636}
637
638
639
640void ConstantHoistingPass::findBaseConstants(GlobalVariable *BaseGV) {
641
642
643 ConstCandVecType &ConstCandVec = BaseGV ?
644 ConstGEPCandMap[BaseGV] : ConstIntCandVec;
645 ConstInfoVecType &ConstInfoVec = BaseGV ?
646 ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
647
648
650 const ConstantCandidate &RHS) {
652 return LHS.ConstInt->getBitWidth() < RHS.ConstInt->getBitWidth();
653 return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue());
654 });
655
656
657
658 auto MinValItr = ConstCandVec.begin();
659 for (auto CC = std::next(ConstCandVec.begin()), E = ConstCandVec.end();
660 CC != E; ++CC) {
661 if (MinValItr->ConstInt->getType() == CC->ConstInt->getType()) {
662 Type *MemUseValTy = nullptr;
663 for (auto &U : CC->Uses) {
664 auto *UI = U.Inst;
666 MemUseValTy = LI->getType();
667 break;
669
670 if (SI->getPointerOperand() == SI->getOperand(U.OpndIdx)) {
671 MemUseValTy = SI->getValueOperand()->getType();
672 break;
673 }
674 }
675 }
676
677
678 APInt Diff = CC->ConstInt->getValue() - MinValItr->ConstInt->getValue();
681
682
684 nullptr, Diff.getSExtValue(),
685 true, 0)))
686 continue;
687 }
688
689
690 findAndMakeBaseConstant(MinValItr, CC, ConstInfoVec);
691
692 MinValItr = CC;
693 }
694
695 findAndMakeBaseConstant(MinValItr, ConstCandVec.end(), ConstInfoVec);
696}
697
698
699
700
701
702
703
706
707
708
709
710
711
712 BasicBlock *IncomingBB = PHI->getIncomingBlock(Idx);
713 for (unsigned i = 0; i < Idx; ++i) {
714 if (PHI->getIncomingBlock(i) == IncomingBB) {
715 Value *IncomingVal = PHI->getIncomingValue(i);
717 return false;
718 }
719 }
720 }
721
723 return true;
724}
725
726
727
728void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
729 UserAdjustment *Adj) {
731
732
733 if (!Adj->Offset && Adj->Ty && Adj->Ty != Base->getType())
734 Adj->Offset = ConstantInt::get(Type::getInt32Ty(*Ctx), 0);
735
736 if (Adj->Offset) {
737 if (Adj->Ty) {
738
740 "mat_gep", Adj->MatInsertPt);
741
742 Mat = new BitCastInst(Mat, Adj->Ty, "mat_bitcast",
743 Adj->MatInsertPt->getIterator());
744 } else
745
746 Mat =
748 "const_mat", Adj->MatInsertPt->getIterator());
749
750 LLVM_DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
751 << " + " << *Adj->Offset << ") in BB "
752 << Mat->getParent()->getName() << '\n'
753 << *Mat << '\n');
754 Mat->setDebugLoc(Adj->User.Inst->getDebugLoc());
755 }
756 Value *Opnd = Adj->User.Inst->getOperand(Adj->User.OpndIdx);
757
758
760 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
761 if ((Adj->User.Inst, Adj->User.OpndIdx, Mat) && Adj->Offset)
763 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
764 return;
765 }
766
767
769 assert(CastInst->isCast() && "Expected an cast instruction!");
770
771
772 Instruction *&ClonedCastInst = ClonedCastMap[CastInst];
773 if (!ClonedCastInst) {
774 ClonedCastInst = CastInst->clone();
776 ClonedCastInst->insertAfter(CastInst->getIterator());
777
778 ClonedCastInst->setDebugLoc(CastInst->getDebugLoc());
779 LLVM_DEBUG(dbgs() << "Clone instruction: " << *CastInst << '\n'
780 << "To : " << *ClonedCastInst << '\n');
781 }
782
783 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
784 updateOperand(Adj->User.Inst, Adj->User.OpndIdx, ClonedCastInst);
785 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
786 return;
787 }
788
789
792
793 updateOperand(Adj->User.Inst, Adj->User.OpndIdx, Mat);
794 return;
795 }
796
797
798 assert(ConstExpr->isCast() && "ConstExpr should be a cast");
800 ConstExprInst->insertBefore(Adj->MatInsertPt);
802
803
804 ConstExprInst->setDebugLoc(Adj->User.Inst->getDebugLoc());
805
806 LLVM_DEBUG(dbgs() << "Create instruction: " << *ConstExprInst << '\n'
807 << "From : " << *ConstExpr << '\n');
808 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
809 if ((Adj->User.Inst, Adj->User.OpndIdx, ConstExprInst)) {
811 if (Adj->Offset)
813 }
814 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
815 return;
816 }
817}
818
819
820
821bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
822 bool MadeChange = false;
823 SmallVectorImplconsthoist::ConstantInfo &ConstInfoVec =
824 BaseGV ? ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
825 for (const consthoist::ConstantInfo &ConstInfo : ConstInfoVec) {
827 collectMatInsertPts(ConstInfo.RebasedConstants, MatInsertPts);
828 SetVectorBasicBlock::iterator IPSet =
829 findConstantInsertionPoint(ConstInfo, MatInsertPts);
830
831 if (IPSet.empty())
832 continue;
833
834 unsigned UsesNum = 0;
835 unsigned ReBasesNum = 0;
836 unsigned NotRebasedNum = 0;
838
839 UsesNum = 0;
841 unsigned MatCtr = 0;
843 UsesNum += RCI.Uses.size();
844 for (auto const &U : RCI.Uses) {
846 BasicBlock *OrigMatInsertBB = MatInsertPt->getParent();
847
848
849 if (IPSet.size() == 1 ||
850 DT->dominates(IP->getParent(), OrigMatInsertBB))
851 ToBeRebased.emplace_back(RCI.Offset, RCI.Ty, MatInsertPt, U);
852 }
853 }
854
855
856
858 NotRebasedNum += ToBeRebased.size();
859 continue;
860 }
861
862
864
866 assert(BaseGV && "A base constant expression must have an base GV");
868 Base = new BitCastInst(ConstInfo.BaseExpr, Ty, "const", IP);
869 } else {
871 Base = new BitCastInst(ConstInfo.BaseInt, Ty, "const", IP);
872 }
873
874 Base->setDebugLoc(IP->getDebugLoc());
875
877 << ") to BB " << IP->getParent()->getName() << '\n'
878 << *Base << '\n');
879
880
881 for (UserAdjustment &R : ToBeRebased) {
882 emitBaseConstants(Base, &R);
883 ReBasesNum++;
884
886 Base->getDebugLoc(), R.User.Inst->getDebugLoc()));
887 }
888 assert(->use_empty() && "The use list is empty!?");
890 "All uses should be instructions.");
891 }
892 (void)UsesNum;
893 (void)ReBasesNum;
894 (void)NotRebasedNum;
895
896 assert(UsesNum == (ReBasesNum + NotRebasedNum) &&
897 "Not all uses are rebased");
898
899 NumConstantsHoisted++;
900
901
902
904
905 MadeChange = true;
906 }
907 return MadeChange;
908}
909
910
911
912void ConstantHoistingPass::deleteDeadCastInst() const {
913 for (auto const &I : ClonedCastMap)
914 if (I.first->use_empty())
915 I.first->eraseFromParent();
916}
917
918
922 this->TTI = &TTI;
923 this->DT = &DT;
924 this->BFI = BFI;
927 this->Entry = &Entry;
928 this->PSI = PSI;
931
932
933 collectConstantCandidates(Fn);
934
935
936
937 if (!ConstIntCandVec.empty())
938 findBaseConstants(nullptr);
939 for (const auto &MapEntry : ConstGEPCandMap)
940 if (!MapEntry.second.empty())
941 findBaseConstants(MapEntry.first);
942
943
944
945 bool MadeChange = false;
946 if (!ConstIntInfoVec.empty())
947 MadeChange = emitBaseConstants(nullptr);
948 for (const auto &MapEntry : ConstGEPInfoMap)
949 if (!MapEntry.second.empty())
950 MadeChange |= emitBaseConstants(MapEntry.first);
951
952
953
954 deleteDeadCastInst();
955
957
958 return MadeChange;
959}
960
967 : nullptr;
970 if ((F, TTI, DT, BFI, F.getEntryBlock(), PSI))
972
975 return PA;
976}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool updateOperand(Instruction *Inst, unsigned Idx, Instruction *Mat)
Updates the operand at Idx in instruction Inst with the result of instruction Mat.
Definition ConstantHoisting.cpp:704
static void findBestInsertionSet(DominatorTree &DT, BlockFrequencyInfo &BFI, BasicBlock *Entry, SetVector< BasicBlock * > &BBs)
Given BBs as input, find another set of BBs which collectively dominates BBs and have the minimal sum...
Definition ConstantHoisting.cpp:213
static cl::opt< unsigned > MinNumOfDependentToRebase("consthoist-min-num-to-rebase", cl::desc("Do not rebase if number of dependent constants of a Base is less " "than this number."), cl::init(0), cl::Hidden)
static cl::opt< bool > ConstHoistWithBlockFrequency("consthoist-with-block-frequency", cl::init(true), cl::Hidden, cl::desc("Enable the use of the block frequency analysis to reduce the " "chance to execute const materialization more frequently than " "without hoisting."))
static cl::opt< bool > ConstHoistGEP("consthoist-gep", cl::init(false), cl::Hidden, cl::desc("Try hoisting constant gep expressions"))
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This pass exposes codegen information to IR-level passes.
unsigned getBitWidth() const
Return the number of bits in the APInt.
int64_t getSExtValue() const
Get sign extended value.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM Basic Block Representation.
InstListType::iterator iterator
Instruction iterators...
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Analysis pass which computes BlockFrequencyInfo.
Legacy analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM_ABI BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Represents analyses that only rely on functions' control flow.
LLVM_ABI bool isCast() const
Return true if this is a convert constant expression.
LLVM_ABI Instruction * getAsInstruction() const
Returns an Instruction which implements the same operation as this ConstantExpr.
bool runImpl(Function &F, TargetTransformInfo &TTI, DominatorTree &DT, BlockFrequencyInfo *BFI, BasicBlock &Entry, ProfileSummaryInfo *PSI)
Optimize expensive integer constants in the given function.
Definition ConstantHoisting.cpp:919
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition ConstantHoisting.cpp:961
IntegerType * getIntegerType() const
Variant of the getType() method to always return an IntegerType, which reduces the amount of casting ...
const APInt & getValue() const
Return the constant as an APInt value reference.
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
iterator_range< iterator > children()
DomTreeNodeBase * getIDom() const
Analysis pass which computes a DominatorTree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
FunctionPass class - This class is used to implement most global optimizations.
const BasicBlock & getEntryBlock() const
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
PointerType * getType() const
Global values are always pointers.
CostType getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
A vector that has set insertion semantics.
void insert_range(Range &&R)
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
void clear()
Completely clear the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
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.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace=0, Instruction *I=nullptr, int64_t ScalableOffset=0) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
LLVM_ABI InstructionCost getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty, TargetCostKind CostKind) const
LLVM_ABI InstructionCost getIntImmCostInst(unsigned Opc, unsigned Idx, const APInt &Imm, Type *Ty, TargetCostKind CostKind, Instruction *Inst=nullptr) const
Return the expected cost of materialization for the given integer immediate of the specified type for...
@ TCK_SizeAndLatency
The weighted sum of size and latency.
LLVM_ABI bool isLegalAddImmediate(int64_t Imm) const
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
LLVM_ABI bool preferToKeepConstantsAttached(const Instruction &Inst, const Function &Fn) const
It can be advantageous to detach complex constants from their uses to make their generation cheaper.
LLVM_ABI InstructionCost getIntImmCodeSizeCost(unsigned Opc, unsigned Idx, const APInt &Imm, Type *Ty) const
Return the expected cost for the given integer when optimising for size.
@ TCC_Basic
The cost of a typical 'add' instruction.
bool isVectorTy() const
True if this is an instance of VectorType.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
A private "module" namespace for types and utilities used by ConstantHoisting.
SmallVector< RebasedConstantInfo, 4 > RebasedConstantListType
@ User
could "use" a pointer
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
void stable_sort(R &&Range)
LLVM_ABI FunctionPass * createConstantHoistingPass()
Definition ConstantHoisting.cpp:135
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI void initializeConstantHoistingLegacyPassPass(PassRegistry &)
LLVM_ABI bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)
Given an instruction, is it legal to set operand OpIdx to a non-constant value?
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
RebasedConstantListType RebasedConstants