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
66#include
67#include
68#include
69#include
70
71using namespace llvm;
73
74#define DEBUG_TYPE "consthoist"
75
76STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
77STATISTIC(NumConstantsRebased, "Number of constants rebased");
78
81 cl::desc("Enable the use of the block frequency analysis to reduce the "
82 "chance to execute const materialization more frequently than "
83 "without hoisting."));
84
87 cl::desc("Try hoisting constant gep expressions"));
88
91 cl::desc("Do not rebase if number of dependent constants of a Base is less "
92 "than this number."),
94
95namespace {
96
97
98class ConstantHoistingLegacyPass : public FunctionPass {
99public:
100 static char ID;
101
104 }
105
107
109
117 }
118
119private:
121};
122
123}
124
125char ConstantHoistingLegacyPass::ID = 0;
126
128 "Constant Hoisting", false, false)
135
137 return new ConstantHoistingLegacyPass();
138}
139
140
141bool ConstantHoistingLegacyPass::runOnFunction(Function &Fn) {
142 if (skipFunction(Fn))
143 return false;
144
145 LLVM_DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n");
147
148 bool MadeChange =
149 Impl.runImpl(Fn, getAnalysis().getTTI(Fn),
150 getAnalysis().getDomTree(),
152 ? &getAnalysis().getBFI()
153 : nullptr,
155 &getAnalysis().getPSI());
156
157 LLVM_DEBUG(dbgs() << "********** End Constant Hoisting **********\n");
158
159 return MadeChange;
160}
161
162void ConstantHoistingPass::collectMatInsertPts(
167 MatInsertPts.emplace_back(findMatInsertPt(U.Inst, U.OpndIdx));
168}
169
170
172 unsigned Idx) const {
173
174
175 if (Idx != ~0U) {
177 if (auto CastInst = dyn_cast(Opnd))
180 }
181
182
183 if (!isa(Inst) && !Inst->isEHPad())
185
186
187
188 assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!");
189 BasicBlock *InsertionBlock = nullptr;
190 if (Idx != ~0U && isa(Inst)) {
191 InsertionBlock = cast(Inst)->getIncomingBlock(Idx);
192 if (!InsertionBlock->isEHPad()) {
194 }
195 } else {
196 InsertionBlock = Inst->getParent();
197 }
198
199
200
201
203 while (IDom->getBlock()->isEHPad()) {
204 assert(Entry != IDom->getBlock() && "eh pad in entry block");
205 IDom = IDom->getIDom();
206 }
207
208 return IDom->getBlock()->getTerminator()->getIterator();
209}
210
211
212
213
217 assert(!BBs.count(Entry) && "Assume Entry is not in BBs");
218
220
221
222
224 for (auto *BB : BBs) {
225
227 continue;
228 Path.clear();
229
230
232
234 do {
235 Path.insert(Node);
238 break;
239 }
241 "Entry doens't dominate current Node");
244
245
246
248 continue;
249
250
251 Candidates.insert(Path.begin(), Path.end());
252 }
253
254
255
256 unsigned Idx = 0;
259 while (Idx != Orders.size()) {
262 if (Candidates.count(ChildDomNode->getBlock()))
263 Orders.push_back(ChildDomNode->getBlock());
264 }
265 }
266
267
268 using InsertPtsCostPair =
270
271
272
276 bool NodeInBBs = BBs.count(Node);
277 auto &InsertPts = InsertPtsMap[Node].first;
279
280
281 if (Node == Entry) {
283 if (InsertPtsFreq > BFI.getBlockFreq(Node) ||
284 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1))
286 else
287 BBs.insert(InsertPts.begin(), InsertPts.end());
288 break;
289 }
290
292
293
294 auto &ParentInsertPts = InsertPtsMap[Parent].first;
295 BlockFrequency &ParentPtsFreq = InsertPtsMap[Parent].second;
296
297
298
299
300
301
302 if (NodeInBBs ||
303 (->isEHPad() &&
304 (InsertPtsFreq > BFI.getBlockFreq(Node) ||
305 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1)))) {
307 ParentPtsFreq += BFI.getBlockFreq(Node);
308 } else {
309 ParentInsertPts.insert(InsertPts.begin(), InsertPts.end());
310 ParentPtsFreq += InsertPtsFreq;
311 }
312 }
313}
314
315
317ConstantHoistingPass::findConstantInsertionPoint(
321
324
326 BBs.insert(MatInsertPt->getParent());
327
328 if (BBs.count(Entry)) {
330 return InsertPts;
331 }
332
333 if (BFI) {
336 InsertPts.insert(BB->getFirstInsertionPt());
337 return InsertPts;
338 }
339
340 while (BBs.size() >= 2) {
342 BB1 = BBs.pop_back_val();
343 BB2 = BBs.pop_back_val();
345 if (BB == Entry) {
347 return InsertPts;
348 }
350 }
351 assert((BBs.size() == 1) && "Expected only one element.");
352 Instruction &FirstInst = (*BBs.begin())->front();
353 InsertPts.insert(findMatInsertPt(&FirstInst));
354 return InsertPts;
355}
356
357
358
359
360
361
362
363void ConstantHoistingPass::collectConstantCandidates(
364 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx,
367 return;
368
370
371
372 if (auto IntrInst = dyn_cast(Inst))
376 else
380
381
385 ConstPtrUnionType Cand = ConstInt;
386 std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(Cand, 0));
387 if (Inserted) {
389 Itr->second = ConstIntCandVec.size() - 1;
390 }
391 ConstIntCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
393 << "Collect constant " << *ConstInt << " from " << *Inst
394 << " with cost " << Cost << '\n';
395 else dbgs() << "Collect constant " << *ConstInt
396 << " indirectly from " << *Inst << " via "
398 << '\n';);
399 }
400}
401
402
403void ConstantHoistingPass::collectConstantCandidates(
404 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx,
406
408 return;
409
411 if (!BaseGV)
412 return;
413
414
418 auto *GEPO = cast(ConstExpr);
419
420
421
422
423
424 if (!GEPO->isInBounds())
425 return;
426
427 if (!GEPO->accumulateConstantOffset(*DL, Offset))
428 return;
429
430 if (.isIntN(32))
431 return;
432
433
434
435
436
440 ConstCandVecType &ExprCandVec = ConstGEPCandMap[BaseGV];
443 ConstPtrUnionType Cand = ConstExpr;
444 std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(Cand, 0));
445 if (Inserted) {
448 ConstExpr));
449 Itr->second = ExprCandVec.size() - 1;
450 }
451 ExprCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
452}
453
454
455void ConstantHoistingPass::collectConstantCandidates(
456 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx) {
458
459
460 if (auto ConstInt = dyn_cast(Opnd)) {
461 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
462 return;
463 }
464
465
466 if (auto CastInst = dyn_cast(Opnd)) {
467
468
470 return;
471
472 if (auto *ConstInt = dyn_cast(CastInst->getOperand(0))) {
473
474
475 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
476 return;
477 }
478 }
479
480
481 if (auto ConstExpr = dyn_cast(Opnd)) {
482
483 if (ConstHoistGEP && isa(ConstExpr))
484 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstExpr);
485
486
487 if (!ConstExpr->isCast())
488 return;
489
490 if (auto ConstInt = dyn_cast(ConstExpr->getOperand(0))) {
491
492
493 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
494 return;
495 }
496 }
497}
498
499
500
501void ConstantHoistingPass::collectConstantCandidates(
502 ConstCandMapType &ConstCandMap, Instruction *Inst) {
503
505 return;
506
507
509
510
511
512
513
515 collectConstantCandidates(ConstCandMap, Inst, Idx);
516 }
517 }
518}
519
520
521
522void ConstantHoistingPass::collectConstantCandidates(Function &Fn) {
523 ConstCandMapType ConstCandMap;
525
527 continue;
530 collectConstantCandidates(ConstCandMap, &Inst);
531 }
532}
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557unsigned
558ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S,
559 ConstCandVecType::iterator E,
560 ConstCandVecType::iterator &MaxCostItr) {
561 unsigned NumUses = 0;
562
563 if (!OptForSize || std::distance(S,E) > 100) {
564 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
565 NumUses += ConstCand->Uses.size();
566 if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost)
567 MaxCostItr = ConstCand;
568 }
569 return NumUses;
570 }
571
572 LLVM_DEBUG(dbgs() << "== Maximize constants in range ==\n");
574 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
575 auto Value = ConstCand->ConstInt->getValue();
576 Type *Ty = ConstCand->ConstInt->getType();
578 NumUses += ConstCand->Uses.size();
579 LLVM_DEBUG(dbgs() << "= Constant: " << ConstCand->ConstInt->getValue()
580 << "\n");
581
582 for (auto User : ConstCand->Uses) {
583 unsigned Opcode = User.Inst->getOpcode();
584 unsigned OpndIdx = User.OpndIdx;
588
589 for (auto C2 = S; C2 != E; ++C2) {
590 APInt Diff = C2->ConstInt->getValue() - ConstCand->ConstInt->getValue();
593 Cost -= ImmCosts;
595 << "has penalty: " << ImmCosts << "\n"
596 << "Adjusted cost: " << Cost << "\n");
597 }
598 }
600 if (Cost > MaxCost) {
601 MaxCost = Cost;
602 MaxCostItr = ConstCand;
603 LLVM_DEBUG(dbgs() << "New candidate: " << MaxCostItr->ConstInt->getValue()
604 << "\n");
605 }
606 }
607 return NumUses;
608}
609
610
611
612void ConstantHoistingPass::findAndMakeBaseConstant(
613 ConstCandVecType::iterator S, ConstCandVecType::iterator E,
615 auto MaxCostItr = S;
616 unsigned NumUses = maximizeConstantsInRange(S, E, MaxCostItr);
617
618
619 if (NumUses <= 1)
620 return;
621
622 ConstantInt *ConstInt = MaxCostItr->ConstInt;
623 ConstantExpr *ConstExpr = MaxCostItr->ConstExpr;
625 ConstInfo.BaseInt = ConstInt;
626 ConstInfo.BaseExpr = ConstExpr;
628
629
630 for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
631 APInt Diff = ConstCand->ConstInt->getValue() - ConstInt->getValue();
632 Constant *Offset = Diff == 0 ? nullptr : ConstantInt::get(Ty, Diff);
633 Type *ConstTy =
634 ConstCand->ConstExpr ? ConstCand->ConstExpr->getType() : nullptr;
637 }
638 ConstInfoVec.push_back(std::move(ConstInfo));
639}
640
641
642
643void ConstantHoistingPass::findBaseConstants(GlobalVariable *BaseGV) {
644
645
646 ConstCandVecType &ConstCandVec = BaseGV ?
647 ConstGEPCandMap[BaseGV] : ConstIntCandVec;
648 ConstInfoVecType &ConstInfoVec = BaseGV ?
649 ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
650
651
655 return LHS.ConstInt->getBitWidth() < RHS.ConstInt->getBitWidth();
656 return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue());
657 });
658
659
660
661 auto MinValItr = ConstCandVec.begin();
662 for (auto CC = std::next(ConstCandVec.begin()), E = ConstCandVec.end();
664 if (MinValItr->ConstInt->getType() == CC->ConstInt->getType()) {
665 Type *MemUseValTy = nullptr;
666 for (auto &U : CC->Uses) {
667 auto *UI = U.Inst;
668 if (LoadInst *LI = dyn_cast(UI)) {
669 MemUseValTy = LI->getType();
670 break;
671 } else if (StoreInst *SI = dyn_cast(UI)) {
672
673 if (SI->getPointerOperand() == SI->getOperand(U.OpndIdx)) {
674 MemUseValTy = SI->getValueOperand()->getType();
675 break;
676 }
677 }
678 }
679
680
681 APInt Diff = CC->ConstInt->getValue() - MinValItr->ConstInt->getValue();
684
685
687 nullptr, Diff.getSExtValue(),
688 true, 0)))
689 continue;
690 }
691
692
693 findAndMakeBaseConstant(MinValItr, CC, ConstInfoVec);
694
695 MinValItr = CC;
696 }
697
698 findAndMakeBaseConstant(MinValItr, ConstCandVec.end(), ConstInfoVec);
699}
700
701
702
703
704
705
706
708 if (auto PHI = dyn_cast(Inst)) {
709
710
711
712
713
714
716 for (unsigned i = 0; i < Idx; ++i) {
717 if (PHI->getIncomingBlock(i) == IncomingBB) {
718 Value *IncomingVal = PHI->getIncomingValue(i);
720 return false;
721 }
722 }
723 }
724
726 return true;
727}
728
729
730
731void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
732 UserAdjustment *Adj) {
734
735
736 if (!Adj->Offset && Adj->Ty && Adj->Ty != Base->getType())
738
739 if (Adj->Offset) {
740 if (Adj->Ty) {
741
743 "mat_gep", Adj->MatInsertPt);
744
745 Mat = new BitCastInst(Mat, Adj->Ty, "mat_bitcast",
746 Adj->MatInsertPt->getIterator());
747 } else
748
749 Mat =
751 "const_mat", Adj->MatInsertPt->getIterator());
752
753 LLVM_DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0)
754 << " + " << *Adj->Offset << ") in BB "
755 << Mat->getParent()->getName() << '\n'
756 << *Mat << '\n');
757 Mat->setDebugLoc(Adj->User.Inst->getDebugLoc());
758 }
759 Value *Opnd = Adj->User.Inst->getOperand(Adj->User.OpndIdx);
760
761
762 if (isa(Opnd)) {
763 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
764 if ((Adj->User.Inst, Adj->User.OpndIdx, Mat) && Adj->Offset)
766 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
767 return;
768 }
769
770
771 if (auto CastInst = dyn_cast(Opnd)) {
773
774
776 if (!ClonedCastInst) {
780
783 << "To : " << *ClonedCastInst << '\n');
784 }
785
786 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
787 updateOperand(Adj->User.Inst, Adj->User.OpndIdx, ClonedCastInst);
788 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
789 return;
790 }
791
792
793 if (auto ConstExpr = dyn_cast(Opnd)) {
794 if (isa(ConstExpr)) {
795
796 updateOperand(Adj->User.Inst, Adj->User.OpndIdx, Mat);
797 return;
798 }
799
800
801 assert(ConstExpr->isCast() && "ConstExpr should be a cast");
803 ConstExprInst->insertBefore(Adj->MatInsertPt);
805
806
807 ConstExprInst->setDebugLoc(Adj->User.Inst->getDebugLoc());
808
809 LLVM_DEBUG(dbgs() << "Create instruction: " << *ConstExprInst << '\n'
810 << "From : " << *ConstExpr << '\n');
811 LLVM_DEBUG(dbgs() << "Update: " << *Adj->User.Inst << '\n');
812 if ((Adj->User.Inst, Adj->User.OpndIdx, ConstExprInst)) {
814 if (Adj->Offset)
816 }
817 LLVM_DEBUG(dbgs() << "To : " << *Adj->User.Inst << '\n');
818 return;
819 }
820}
821
822
823
824bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
825 bool MadeChange = false;
827 BaseGV ? ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
830 collectMatInsertPts(ConstInfo.RebasedConstants, MatInsertPts);
832 findConstantInsertionPoint(ConstInfo, MatInsertPts);
833
834 if (IPSet.empty())
835 continue;
836
837 unsigned UsesNum = 0;
838 unsigned ReBasesNum = 0;
839 unsigned NotRebasedNum = 0;
841
842 UsesNum = 0;
844 unsigned MatCtr = 0;
846 UsesNum += RCI.Uses.size();
847 for (auto const &U : RCI.Uses) {
850
851
852 if (IPSet.size() == 1 ||
853 DT->dominates(IP->getParent(), OrigMatInsertBB))
854 ToBeRebased.emplace_back(RCI.Offset, RCI.Ty, MatInsertPt, U);
855 }
856 }
857
858
859
861 NotRebasedNum += ToBeRebased.size();
862 continue;
863 }
864
865
867
869 assert(BaseGV && "A base constant expression must have an base GV");
872 } else {
875 }
876
877 Base->setDebugLoc(IP->getDebugLoc());
878
880 << ") to BB " << IP->getParent()->getName() << '\n'
881 << *Base << '\n');
882
883
884 for (UserAdjustment &R : ToBeRebased) {
885 emitBaseConstants(Base, &R);
886 ReBasesNum++;
887
889 Base->getDebugLoc(), R.User.Inst->getDebugLoc()));
890 }
891 assert(->use_empty() && "The use list is empty!?");
892 assert(isa(Base->user_back()) &&
893 "All uses should be instructions.");
894 }
895 (void)UsesNum;
896 (void)ReBasesNum;
897 (void)NotRebasedNum;
898
899 assert(UsesNum == (ReBasesNum + NotRebasedNum) &&
900 "Not all uses are rebased");
901
902 NumConstantsHoisted++;
903
904
905
907
908 MadeChange = true;
909 }
910 return MadeChange;
911}
912
913
914
915void ConstantHoistingPass::deleteDeadCastInst() const {
916 for (auto const &I : ClonedCastMap)
917 if (I.first->use_empty())
918 I.first->eraseFromParent();
919}
920
921
925 this->TTI = &TTI;
926 this->DT = &DT;
927 this->BFI = BFI;
930 this->Entry = &Entry;
931 this->PSI = PSI;
934
935
936 collectConstantCandidates(Fn);
937
938
939
940 if (!ConstIntCandVec.empty())
941 findBaseConstants(nullptr);
942 for (const auto &MapEntry : ConstGEPCandMap)
943 if (!MapEntry.second.empty())
944 findBaseConstants(MapEntry.first);
945
946
947
948 bool MadeChange = false;
949 if (!ConstIntInfoVec.empty())
950 MadeChange = emitBaseConstants(nullptr);
951 for (const auto &MapEntry : ConstGEPInfoMap)
952 if (!MapEntry.second.empty())
953 MadeChange |= emitBaseConstants(MapEntry.first);
954
955
956
957 deleteDeadCastInst();
958
960
961 return MadeChange;
962}
963
970 : nullptr;
973 if ((F, TTI, DT, BFI, F.getEntryBlock(), PSI))
975
978 return PA;
979}
This file implements a class to represent arbitrary precision integral constant values and operations...
static bool updateOperand(Instruction *Inst, unsigned Idx, Instruction *Mat)
Updates the operand at Idx in instruction Inst with the result of instruction Mat.
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...
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...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Class for arbitrary precision integers.
unsigned getBitWidth() const
Return the number of bits in the APInt.
int64_t getSExtValue() const
Get sign extended value.
A container for analyses that lazily runs them and caches their results.
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()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const Function * getParent() const
Return the enclosing method, or null if none.
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 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.
This class represents a no-op cast from one type to another.
Analysis pass which computes BlockFrequencyInfo.
Legacy analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Represents analyses that only rely on functions' control flow.
This is the base class for all instructions that perform data casts.
A constant value that is initialized with an expression using other constant values.
bool isCast() const
Return true if this is a convert constant expression.
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.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the shared class of boolean and integer constants.
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.
This is an important base class in LLVM.
static DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
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.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
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.
std::optional< CostType > getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
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.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
Class to represent integer types.
An instruction for reading from memory.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
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.
void 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 clear()
Completely clear the SetVector.
size_type count(const key_type &key) const
Count the number of elements of a given key in 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.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
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...
InstructionCost getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty, TargetCostKind CostKind) const
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.
bool isLegalAddImmediate(int64_t Imm) const
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
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.
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.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
static IntegerType * getInt8Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
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.
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.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
FunctionPass * createConstantHoistingPass()
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)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)
Given an instruction, is it legal to set operand OpIdx to a non-constant value?
void initializeConstantHoistingLegacyPassPass(PassRegistry &)
Keeps track of a constant candidate and its uses.
A base constant and all its rebased constants.
RebasedConstantListType RebasedConstants
Keeps track of the user of a constant and the operand index where the constant is used.
This represents a constant that has been rebased with respect to a base constant.