LLVM 24.0.0git
HexagonCommonGEP.cpp
Go to the documentation of this file.
1//===- HexagonCommonGEP.cpp -----------------------------------------------===//
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#include "Hexagon.h"
10
11#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/FoldingSet.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/BasicBlock.h"
21#include "llvm/IR/Constant.h"
22#include "llvm/IR/Constants.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/Function.h"
26#include "llvm/IR/Instruction.h"
28#include "llvm/IR/Type.h"
29#include "llvm/IR/Use.h"
30#include "llvm/IR/User.h"
31#include "llvm/IR/Value.h"
32#include "llvm/IR/Verifier.h"
34#include "llvm/Pass.h"
39#include "llvm/Support/Debug.h"
42#include <cassert>
43#include <cstddef>
44#include <cstdint>
45#include <iterator>
46#include <map>
47#include <set>
48#include <utility>
49#include <vector>
50
51#define DEBUG_TYPE "commgep"
52
53using namespace llvm;
54
55static cl::opt<bool> OptSpeculate("commgep-speculate", cl::init(true),
57
58static cl::opt<bool> OptEnableInv("commgep-inv", cl::init(true), cl::Hidden);
59
60static cl::opt<bool> OptEnableConst("commgep-const", cl::init(true),
62
63namespace {
64
65 struct GepNode;
66 using NodeSet = std::set<GepNode *>;
67 using NodeToValueMap = std::map<GepNode *, Value *>;
68 using NodeVect = std::vector<GepNode *>;
69 using NodeChildrenMap = std::map<GepNode *, NodeVect>;
70 using UseSet = SetVector<Use *>;
71 using NodeToUsesMap = std::map<GepNode *, UseSet>;
72
73 // Numbering map for gep nodes. Used to keep track of ordering for
74 // gep nodes.
75 struct NodeOrdering {
76 NodeOrdering() = default;
77
78 void insert(const GepNode *N) { Map.insert(std::make_pair(N, ++LastNum)); }
79 void clear() { Map.clear(); }
80
81 bool operator()(const GepNode *N1, const GepNode *N2) const {
82 auto F1 = Map.find(N1), F2 = Map.find(N2);
83 assert(F1 != Map.end() && F2 != Map.end());
84 return F1->second < F2->second;
85 }
86
87 private:
88 std::map<const GepNode *, unsigned> Map;
89 unsigned LastNum = 0;
90 };
91
92 class HexagonCommonGEP : public FunctionPass {
93 public:
94 static char ID;
95
96 HexagonCommonGEP() : FunctionPass(ID) {}
97
98 bool runOnFunction(Function &F) override;
99 StringRef getPassName() const override { return "Hexagon Common GEP"; }
100
101 void getAnalysisUsage(AnalysisUsage &AU) const override {
102 AU.addRequired<DominatorTreeWrapperPass>();
103 AU.addPreserved<DominatorTreeWrapperPass>();
104 AU.addRequired<PostDominatorTreeWrapperPass>();
105 AU.addPreserved<PostDominatorTreeWrapperPass>();
106 AU.addRequired<LoopInfoWrapperPass>();
107 AU.addPreserved<LoopInfoWrapperPass>();
108 FunctionPass::getAnalysisUsage(AU);
109 }
110
111 private:
112 using ValueToNodeMap = std::map<Value *, GepNode *>;
113 using ValueVect = std::vector<Value *>;
114 using NodeToValuesMap = std::map<GepNode *, ValueVect>;
115
116 void getBlockTraversalOrder(BasicBlock *Root, ValueVect &Order);
117 bool isHandledGepForm(GetElementPtrInst *GepI);
118 void processGepInst(GetElementPtrInst *GepI, ValueToNodeMap &NM);
119 void collect();
120 void common();
121
122 BasicBlock *recalculatePlacement(GepNode *Node, NodeChildrenMap &NCM,
123 NodeToValueMap &Loc);
124 BasicBlock *recalculatePlacementRec(GepNode *Node, NodeChildrenMap &NCM,
125 NodeToValueMap &Loc);
126 bool isInvariantIn(Value *Val, Loop *L);
127 bool isInvariantIn(GepNode *Node, Loop *L);
128 bool isInMainPath(BasicBlock *B, Loop *L);
129 BasicBlock *adjustForInvariance(GepNode *Node, NodeChildrenMap &NCM,
130 NodeToValueMap &Loc);
131 void separateChainForNode(GepNode *Node, Use *U, NodeToValueMap &Loc);
132 void separateConstantChains(GepNode *Node, NodeChildrenMap &NCM,
133 NodeToValueMap &Loc);
134 void computeNodePlacement(NodeToValueMap &Loc);
135
136 Value *fabricateGEP(NodeVect &NA, BasicBlock::iterator At,
137 BasicBlock *LocB);
138 void getAllUsersForNode(GepNode *Node, ValueVect &Values,
139 NodeChildrenMap &NCM);
140 void materialize(NodeToValueMap &Loc);
141
142 void removeDeadCode();
143
144 NodeVect Nodes;
145 NodeToUsesMap Uses;
146 NodeOrdering NodeOrder; // Node ordering, for deterministic behavior.
147 SpecificBumpPtrAllocator<GepNode> *Mem;
148 LLVMContext *Ctx;
149 LoopInfo *LI;
150 DominatorTree *DT;
151 PostDominatorTree *PDT;
152 Function *Fn;
153 };
154
155} // end anonymous namespace
156
157char HexagonCommonGEP::ID = 0;
158
159INITIALIZE_PASS_BEGIN(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
160 false, false)
164INITIALIZE_PASS_END(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
166
167namespace {
168
169 struct GepNode {
170 enum {
171 None = 0,
172 Root = 0x01,
173 Internal = 0x02,
174 Used = 0x04,
175 InBounds = 0x08,
176 Pointer = 0x10, // See note below.
177 };
178 // Note: GEP indices generally traverse nested types, and so a GepNode
179 // (representing a single index) can be associated with some composite
180 // type. The exception is the GEP input, which is a pointer, and not
181 // a composite type (at least not in the sense of having sub-types).
182 // Also, the corresponding index plays a different role as well: it is
183 // simply added to the input pointer. Since pointer types are becoming
184 // opaque (i.e. are no longer going to include the pointee type), the
185 // two pieces of information (1) the fact that it's a pointer, and
186 // (2) the pointee type, need to be stored separately. The pointee type
187 // will be stored in the PTy member, while the fact that the node
188 // operates on a pointer will be reflected by the flag "Pointer".
189
191 union {
194 };
195 Value *Idx = nullptr;
196 Type *PTy = nullptr; // Type indexed by this node. For pointer nodes
197 // this is the "pointee" type, and indexing a
198 // pointer does not change the type.
199
200 GepNode() : Parent(nullptr) {}
201 GepNode(const GepNode *N) : Flags(N->Flags), Idx(N->Idx), PTy(N->PTy) {
202 if (Flags & Root)
203 BaseVal = N->BaseVal;
204 else
205 Parent = N->Parent;
206 }
207
208 friend raw_ostream &operator<< (raw_ostream &OS, const GepNode &GN);
209 };
210
212 OS << "{ {";
213 bool Comma = false;
214 if (GN.Flags & GepNode::Root) {
215 OS << "root";
216 Comma = true;
217 }
218 if (GN.Flags & GepNode::Internal) {
219 if (Comma)
220 OS << ',';
221 OS << "internal";
222 Comma = true;
223 }
224 if (GN.Flags & GepNode::Used) {
225 if (Comma)
226 OS << ',';
227 OS << "used";
228 }
229 if (GN.Flags & GepNode::InBounds) {
230 if (Comma)
231 OS << ',';
232 OS << "inbounds";
233 }
234 if (GN.Flags & GepNode::Pointer) {
235 if (Comma)
236 OS << ',';
237 OS << "pointer";
238 }
239 OS << "} ";
240 if (GN.Flags & GepNode::Root)
241 OS << "BaseVal:" << GN.BaseVal->getName() << '(' << GN.BaseVal << ')';
242 else
243 OS << "Parent:" << GN.Parent;
244
245 OS << " Idx:";
247 OS << CI->getValue().getSExtValue();
248 else if (GN.Idx->hasName())
249 OS << GN.Idx->getName();
250 else
251 OS << "<anon> =" << *GN.Idx;
252
253 OS << " PTy:";
254 if (GN.PTy->isStructTy()) {
256 if (!STy->isLiteral())
257 OS << GN.PTy->getStructName();
258 else
259 OS << "<anon-struct>:" << *STy;
260 }
261 else
262 OS << *GN.PTy;
263 OS << " }";
264 return OS;
265 }
266
267 template <typename NodeContainer>
268 void dump_node_container(raw_ostream &OS, const NodeContainer &S) {
269 using const_iterator = typename NodeContainer::const_iterator;
270
271 for (const_iterator I = S.begin(), E = S.end(); I != E; ++I)
272 OS << *I << ' ' << **I << '\n';
273 }
274
275 [[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS, const NodeVect &S);
276 raw_ostream &operator<< (raw_ostream &OS, const NodeVect &S) {
277 dump_node_container(OS, S);
278 return OS;
279 }
280
281 [[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS,
282 const NodeToUsesMap &M);
283 raw_ostream &operator<< (raw_ostream &OS, const NodeToUsesMap &M){
284 for (const auto &I : M) {
285 const UseSet &Us = I.second;
286 OS << I.first << " -> #" << Us.size() << '{';
287 for (const Use *U : Us) {
288 User *R = U->getUser();
289 if (R->hasName())
290 OS << ' ' << R->getName();
291 else
292 OS << " <?>(" << *R << ')';
293 }
294 OS << " }\n";
295 }
296 return OS;
297 }
298
299 struct in_set {
300 in_set(const NodeSet &S) : NS(S) {}
301
302 bool operator() (GepNode *N) const {
303 return NS.find(N) != NS.end();
304 }
305
306 private:
307 const NodeSet &NS;
308 };
309
310} // end anonymous namespace
311
312inline void *operator new(size_t, SpecificBumpPtrAllocator<GepNode> &A) {
313 return A.Allocate();
314}
315
316void HexagonCommonGEP::getBlockTraversalOrder(BasicBlock *Root,
317 ValueVect &Order) {
318 // Compute block ordering for a typical DT-based traversal of the flow
319 // graph: "before visiting a block, all of its dominators must have been
320 // visited".
321
322 Order.push_back(Root);
323 for (auto *DTN : children<DomTreeNode*>(DT->getNode(Root)))
324 getBlockTraversalOrder(DTN->getBlock(), Order);
325}
326
327bool HexagonCommonGEP::isHandledGepForm(GetElementPtrInst *GepI) {
328 // No vector GEPs.
329 if (!GepI->getType()->isPointerTy())
330 return false;
331 // No GEPs without any indices. (Is this possible?)
332 if (GepI->indices().empty())
333 return false;
334 return true;
335}
336
337void HexagonCommonGEP::processGepInst(GetElementPtrInst *GepI,
338 ValueToNodeMap &NM) {
339 LLVM_DEBUG(dbgs() << "Visiting GEP: " << *GepI << '\n');
340 GepNode *N = new (*Mem) GepNode;
341 Value *PtrOp = GepI->getPointerOperand();
342 uint32_t InBounds = GepI->isInBounds() ? GepNode::InBounds : 0;
343 ValueToNodeMap::iterator F = NM.find(PtrOp);
344 if (F == NM.end()) {
345 N->BaseVal = PtrOp;
346 N->Flags |= GepNode::Root | InBounds;
347 } else {
348 // If PtrOp was a GEP instruction, it must have already been processed.
349 // The ValueToNodeMap entry for it is the last gep node in the generated
350 // chain. Link to it here.
351 N->Parent = F->second;
352 }
353 N->PTy = GepI->getSourceElementType();
354 N->Flags |= GepNode::Pointer;
355 N->Idx = *GepI->idx_begin();
356
357 // Collect the list of users of this GEP instruction. Will add it to the
358 // last node created for it.
359 UseSet Us;
360 for (Instruction::user_iterator UI = GepI->user_begin(),
361 UE = GepI->user_end();
362 UI != UE; ++UI) {
363 // Check if this gep is used by anything other than other geps that
364 // we will process.
365 if (isa<GetElementPtrInst>(*UI)) {
366 GetElementPtrInst *UserG = cast<GetElementPtrInst>(*UI);
367 if (isHandledGepForm(UserG))
368 continue;
369 }
370 Us.insert(&UI.getUse());
371 }
372 Nodes.push_back(N);
373 NodeOrder.insert(N);
374
375 // Skip the first index operand, since it was already handled above. This
376 // dereferences the pointer operand.
377 GepNode *PN = N;
378 Type *PtrTy = GepI->getSourceElementType();
379 for (Use &U : llvm::drop_begin(GepI->indices())) {
380 Value *Op = U;
381 GepNode *Nx = new (*Mem) GepNode;
382 Nx->Parent = PN; // Link Nx to the previous node.
383 Nx->Flags |= GepNode::Internal | InBounds;
384 Nx->PTy = PtrTy;
385 Nx->Idx = Op;
386 Nodes.push_back(Nx);
387 NodeOrder.insert(Nx);
388 PN = Nx;
389
391 }
392
393 // After last node has been created, update the use information.
394 if (!Us.empty()) {
395 PN->Flags |= GepNode::Used;
396 Uses[PN].insert_range(Us);
397 }
398
399 // Link the last node with the originating GEP instruction. This is to
400 // help with linking chained GEP instructions.
401 NM.insert(std::make_pair(GepI, PN));
402}
403
404void HexagonCommonGEP::collect() {
405 // Establish depth-first traversal order of the dominator tree.
406 ValueVect BO;
407 getBlockTraversalOrder(&Fn->front(), BO);
408
409 // The creation of gep nodes requires DT-traversal. When processing a GEP
410 // instruction that uses another GEP instruction as the base pointer, the
411 // gep node for the base pointer should already exist.
412 ValueToNodeMap NM;
413 for (Value *I : BO) {
415 for (Instruction &J : *B)
416 if (auto *GepI = dyn_cast<GetElementPtrInst>(&J))
417 if (isHandledGepForm(GepI))
418 processGepInst(GepI, NM);
419 }
420
421 LLVM_DEBUG(dbgs() << "Gep nodes after initial collection:\n" << Nodes);
422}
423
424static void invert_find_roots(const NodeVect &Nodes, NodeChildrenMap &NCM,
425 NodeVect &Roots) {
426 for (GepNode *N : Nodes) {
427 if (N->Flags & GepNode::Root) {
428 Roots.push_back(N);
429 continue;
430 }
431 GepNode *PN = N->Parent;
432 NCM[PN].push_back(N);
433 }
434}
435
436static void nodes_for_root(GepNode *Root, NodeChildrenMap &NCM,
437 NodeSet &Nodes) {
438 NodeVect Work;
439 Work.push_back(Root);
440 Nodes.insert(Root);
441
442 while (!Work.empty()) {
443 NodeVect::iterator First = Work.begin();
444 GepNode *N = *First;
445 Work.erase(First);
446 NodeChildrenMap::iterator CF = NCM.find(N);
447 if (CF != NCM.end()) {
448 llvm::append_range(Work, CF->second);
449 Nodes.insert(CF->second.begin(), CF->second.end());
450 }
451 }
452}
453
454namespace {
455
456 using NodeSymRel = std::set<NodeSet>;
457 using NodePair = std::pair<GepNode *, GepNode *>;
458 using NodePairSet = std::set<NodePair>;
459
460} // end anonymous namespace
461
462static const NodeSet *node_class(GepNode *N, NodeSymRel &Rel) {
463 for (const NodeSet &S : Rel)
464 if (S.count(N))
465 return &S;
466 return nullptr;
467}
468
469 // Create an ordered pair of GepNode pointers. The pair will be used in
470 // determining equality. The only purpose of the ordering is to eliminate
471 // duplication due to the commutativity of equality/non-equality.
472static NodePair node_pair(GepNode *N1, GepNode *N2) {
473 uintptr_t P1 = reinterpret_cast<uintptr_t>(N1);
474 uintptr_t P2 = reinterpret_cast<uintptr_t>(N2);
475 if (P1 <= P2)
476 return std::make_pair(N1, N2);
477 return std::make_pair(N2, N1);
478}
479
480static unsigned node_hash(GepNode *N) {
481 // Include everything except flags and parent.
483 ID.AddPointer(N->Idx);
484 ID.AddPointer(N->PTy);
485 return ID.computeHash();
486}
487
488static bool node_eq(GepNode *N1, GepNode *N2, NodePairSet &Eq,
489 NodePairSet &Ne) {
490 // Don't cache the result for nodes with different hashes. The hash
491 // comparison is fast enough.
492 if (node_hash(N1) != node_hash(N2))
493 return false;
494
495 NodePair NP = node_pair(N1, N2);
496 NodePairSet::iterator FEq = Eq.find(NP);
497 if (FEq != Eq.end())
498 return true;
499 NodePairSet::iterator FNe = Ne.find(NP);
500 if (FNe != Ne.end())
501 return false;
502 // Not previously compared.
503 bool Root1 = N1->Flags & GepNode::Root;
504 uint32_t CmpFlags = GepNode::Root | GepNode::Pointer;
505 bool Different = (N1->Flags & CmpFlags) != (N2->Flags & CmpFlags);
506 NodePair P = node_pair(N1, N2);
507 // If the root/pointer flags have different values, the nodes are
508 // different.
509 // If both nodes are root nodes, but their base pointers differ,
510 // they are different.
511 if (Different || (Root1 && N1->BaseVal != N2->BaseVal)) {
512 Ne.insert(P);
513 return false;
514 }
515 // Here the root/pointer flags are identical, and for root nodes the
516 // base pointers are equal, so the root nodes are equal.
517 // For non-root nodes, compare their parent nodes.
518 if (Root1 || node_eq(N1->Parent, N2->Parent, Eq, Ne)) {
519 Eq.insert(P);
520 return true;
521 }
522 return false;
523}
524
525void HexagonCommonGEP::common() {
526 // The essence of this commoning is finding gep nodes that are equal.
527 // To do this we need to compare all pairs of nodes. To save time,
528 // first, partition the set of all nodes into sets of potentially equal
529 // nodes, and then compare pairs from within each partition.
530 using NodeSetMap = std::map<unsigned, NodeSet>;
531 NodeSetMap MaybeEq;
532
533 for (GepNode *N : Nodes) {
534 unsigned H = node_hash(N);
535 MaybeEq[H].insert(N);
536 }
537
538 // Compute the equivalence relation for the gep nodes. Use two caches,
539 // one for equality and the other for non-equality.
540 NodeSymRel EqRel; // Equality relation (as set of equivalence classes).
541 NodePairSet Eq, Ne; // Caches.
542 for (auto &I : MaybeEq) {
543 NodeSet &S = I.second;
544 for (NodeSet::iterator NI = S.begin(), NE = S.end(); NI != NE; ++NI) {
545 GepNode *N = *NI;
546 // If node already has a class, then the class must have been created
547 // in a prior iteration of this loop. Since equality is transitive,
548 // nothing more will be added to that class, so skip it.
549 if (node_class(N, EqRel))
550 continue;
551
552 // Create a new class candidate now.
553 NodeSet C;
554 for (NodeSet::iterator NJ = std::next(NI); NJ != NE; ++NJ)
555 if (node_eq(N, *NJ, Eq, Ne))
556 C.insert(*NJ);
557 // If Tmp is empty, N would be the only element in it. Don't bother
558 // creating a class for it then.
559 if (!C.empty()) {
560 C.insert(N); // Finalize the set before adding it to the relation.
561 std::pair<NodeSymRel::iterator, bool> Ins = EqRel.insert(C);
562 (void)Ins;
563 assert(Ins.second && "Cannot add a class");
564 }
565 }
566 }
567
568 LLVM_DEBUG({
569 dbgs() << "Gep node equality:\n";
570 for (NodePairSet::iterator I = Eq.begin(), E = Eq.end(); I != E; ++I)
571 dbgs() << "{ " << I->first << ", " << I->second << " }\n";
572
573 dbgs() << "Gep equivalence classes:\n";
574 for (const NodeSet &S : EqRel) {
575 dbgs() << '{';
576 for (NodeSet::const_iterator J = S.begin(), F = S.end(); J != F; ++J) {
577 if (J != S.begin())
578 dbgs() << ',';
579 dbgs() << ' ' << *J;
580 }
581 dbgs() << " }\n";
582 }
583 });
584
585 // Create a projection from a NodeSet to the minimal element in it.
586 using ProjMap = std::map<const NodeSet *, GepNode *>;
587 ProjMap PM;
588 for (const NodeSet &S : EqRel) {
589 GepNode *Min = *llvm::min_element(S, NodeOrder);
590 std::pair<ProjMap::iterator,bool> Ins = PM.insert(std::make_pair(&S, Min));
591 (void)Ins;
592 assert(Ins.second && "Cannot add minimal element");
593
594 // Update the min element's flags, and user list.
595 uint32_t Flags = 0;
596 UseSet &MinUs = Uses[Min];
597 for (GepNode *N : S) {
598 uint32_t NF = N->Flags;
599 // If N is used, append all original values of N to the list of
600 // original values of Min.
601 if (NF & GepNode::Used) {
602 auto &U = Uses[N];
603 MinUs.insert_range(U);
604 }
605 Flags |= NF;
606 }
607 if (MinUs.empty())
608 Uses.erase(Min);
609
610 // The collected flags should include all the flags from the min element.
611 assert((Min->Flags & Flags) == Min->Flags);
612 Min->Flags = Flags;
613 }
614
615 // Commoning: for each non-root gep node, replace "Parent" with the
616 // selected (minimum) node from the corresponding equivalence class.
617 // If a given parent does not have an equivalence class, leave it
618 // unchanged (it means that it's the only element in its class).
619 for (GepNode *N : Nodes) {
620 if (N->Flags & GepNode::Root)
621 continue;
622 const NodeSet *PC = node_class(N->Parent, EqRel);
623 if (!PC)
624 continue;
625 ProjMap::iterator F = PM.find(PC);
626 if (F == PM.end())
627 continue;
628 // Found a replacement, use it.
629 GepNode *Rep = F->second;
630 N->Parent = Rep;
631 }
632
633 LLVM_DEBUG(dbgs() << "Gep nodes after commoning:\n" << Nodes);
634
635 // Finally, erase the nodes that are no longer used.
636 NodeSet Erase;
637 for (GepNode *N : Nodes) {
638 const NodeSet *PC = node_class(N, EqRel);
639 if (!PC)
640 continue;
641 ProjMap::iterator F = PM.find(PC);
642 if (F == PM.end())
643 continue;
644 if (N == F->second)
645 continue;
646 // Node for removal.
647 Erase.insert(N);
648 }
649 erase_if(Nodes, in_set(Erase));
650
651 LLVM_DEBUG(dbgs() << "Gep nodes after post-commoning cleanup:\n" << Nodes);
652}
653
654template <typename T>
656 LLVM_DEBUG({
657 dbgs() << "NCD of {";
658 for (typename T::iterator I = Blocks.begin(), E = Blocks.end(); I != E;
659 ++I) {
660 if (!*I)
661 continue;
663 dbgs() << ' ' << B->getName();
664 }
665 dbgs() << " }\n";
666 });
667
668 // Allow null basic blocks in Blocks. In such cases, return nullptr.
669 typename T::iterator I = Blocks.begin(), E = Blocks.end();
670 if (I == E || !*I)
671 return nullptr;
673 while (++I != E) {
675 Dom = B ? DT->findNearestCommonDominator(Dom, B) : nullptr;
676 if (!Dom)
677 return nullptr;
678 }
679 LLVM_DEBUG(dbgs() << "computed:" << Dom->getName() << '\n');
680 return Dom;
681}
682
683template <typename T>
685 // If two blocks, A and B, dominate a block C, then A dominates B,
686 // or B dominates A.
687 typename T::iterator I = Blocks.begin(), E = Blocks.end();
688 // Find the first non-null block.
689 while (I != E && !*I)
690 ++I;
691 if (I == E)
692 return DT->getRoot();
693 BasicBlock *DomB = cast<BasicBlock>(*I);
694 while (++I != E) {
695 if (!*I)
696 continue;
698 if (DT->dominates(B, DomB))
699 continue;
700 if (!DT->dominates(DomB, B))
701 return nullptr;
702 DomB = B;
703 }
704 return DomB;
705}
706
707// Find the first use in B of any value from Values. If no such use,
708// return B->end().
709template <typename T>
711 BasicBlock::iterator FirstUse = B->end(), BEnd = B->end();
712
713 using iterator = typename T::iterator;
714
715 for (iterator I = Values.begin(), E = Values.end(); I != E; ++I) {
716 Value *V = *I;
717 // If V is used in a PHI node, the use belongs to the incoming block,
718 // not the block with the PHI node. In the incoming block, the use
719 // would be considered as being at the end of it, so it cannot
720 // influence the position of the first use (which is assumed to be
721 // at the end to start with).
722 if (isa<PHINode>(V))
723 continue;
724 if (!isa<Instruction>(V))
725 continue;
727 if (In->getParent() != B)
728 continue;
729 BasicBlock::iterator It = In->getIterator();
730 if (std::distance(FirstUse, BEnd) < std::distance(It, BEnd))
731 FirstUse = It;
732 }
733 return FirstUse;
734}
735
736static bool is_empty(const BasicBlock *B) {
737 return B->empty() || (&*B->begin() == B->getTerminator());
738}
739
740BasicBlock *HexagonCommonGEP::recalculatePlacement(GepNode *Node,
741 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
742 LLVM_DEBUG(dbgs() << "Loc for node:" << Node << '\n');
743 // Recalculate the placement for Node, assuming that the locations of
744 // its children in Loc are valid.
745 // Return nullptr if there is no valid placement for Node (for example, it
746 // uses an index value that is not available at the location required
747 // to dominate all children, etc.).
748
749 // Find the nearest common dominator for:
750 // - all users, if the node is used, and
751 // - all children.
752 ValueVect Bs;
753 if (Node->Flags & GepNode::Used) {
754 // Append all blocks with uses of the original values to the
755 // block vector Bs.
756 NodeToUsesMap::iterator UF = Uses.find(Node);
757 assert(UF != Uses.end() && "Used node with no use information");
758 UseSet &Us = UF->second;
759 for (Use *U : Us) {
760 User *R = U->getUser();
761 if (!isa<Instruction>(R))
762 continue;
764 ? cast<PHINode>(R)->getIncomingBlock(*U)
765 : cast<Instruction>(R)->getParent();
766 Bs.push_back(PB);
767 }
768 }
769 // Append the location of each child.
770 NodeChildrenMap::iterator CF = NCM.find(Node);
771 if (CF != NCM.end()) {
772 NodeVect &Cs = CF->second;
773 for (GepNode *CN : Cs) {
774 NodeToValueMap::iterator LF = Loc.find(CN);
775 // If the child is only used in GEP instructions (i.e. is not used in
776 // non-GEP instructions), the nearest dominator computed for it may
777 // have been null. In such case it won't have a location available.
778 if (LF == Loc.end())
779 continue;
780 Bs.push_back(LF->second);
781 }
782 }
783
784 BasicBlock *DomB = nearest_common_dominator(DT, Bs);
785 if (!DomB)
786 return nullptr;
787 // Check if the index used by Node dominates the computed dominator.
789 if (IdxI && !DT->dominates(IdxI->getParent(), DomB))
790 return nullptr;
791
792 // Avoid putting nodes into empty blocks.
793 while (is_empty(DomB)) {
794 DomTreeNode *N = (*DT)[DomB]->getIDom();
795 if (!N)
796 break;
797 DomB = N->getBlock();
798 }
799
800 // Otherwise, DomB is fine. Update the location map.
801 Loc[Node] = DomB;
802 return DomB;
803}
804
805BasicBlock *HexagonCommonGEP::recalculatePlacementRec(GepNode *Node,
806 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
807 LLVM_DEBUG(dbgs() << "LocRec begin for node:" << Node << '\n');
808 // Recalculate the placement of Node, after recursively recalculating the
809 // placements of all its children.
810 NodeChildrenMap::iterator CF = NCM.find(Node);
811 if (CF != NCM.end()) {
812 NodeVect &Cs = CF->second;
813 for (GepNode *C : Cs)
814 recalculatePlacementRec(C, NCM, Loc);
815 }
816 BasicBlock *LB = recalculatePlacement(Node, NCM, Loc);
817 LLVM_DEBUG(dbgs() << "LocRec end for node:" << Node << '\n');
818 return LB;
819}
820
821bool HexagonCommonGEP::isInvariantIn(Value *Val, Loop *L) {
822 if (isa<Constant>(Val) || isa<Argument>(Val))
823 return true;
825 if (!In)
826 return false;
827 BasicBlock *HdrB = L->getHeader(), *DefB = In->getParent();
828 return DT->properlyDominates(DefB, HdrB);
829}
830
831bool HexagonCommonGEP::isInvariantIn(GepNode *Node, Loop *L) {
832 if (Node->Flags & GepNode::Root)
833 if (!isInvariantIn(Node->BaseVal, L))
834 return false;
835 return isInvariantIn(Node->Idx, L);
836}
837
838bool HexagonCommonGEP::isInMainPath(BasicBlock *B, Loop *L) {
839 BasicBlock *HB = L->getHeader();
840 BasicBlock *LB = L->getLoopLatch();
841 // B must post-dominate the loop header or dominate the loop latch.
842 if (PDT->dominates(B, HB))
843 return true;
844 if (LB && DT->dominates(B, LB))
845 return true;
846 return false;
847}
848
850 if (BasicBlock *PH = L->getLoopPreheader())
851 return PH;
852 if (!OptSpeculate)
853 return nullptr;
854 DomTreeNode *DN = DT->getNode(L->getHeader());
855 if (!DN)
856 return nullptr;
857 return DN->getIDom()->getBlock();
858}
859
860BasicBlock *HexagonCommonGEP::adjustForInvariance(GepNode *Node,
861 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
862 // Find the "topmost" location for Node: it must be dominated by both,
863 // its parent (or the BaseVal, if it's a root node), and by the index
864 // value.
865 ValueVect Bs;
866 if (Node->Flags & GepNode::Root) {
867 if (Instruction *PIn = dyn_cast<Instruction>(Node->BaseVal))
868 Bs.push_back(PIn->getParent());
869 } else {
870 Bs.push_back(Loc[Node->Parent]);
871 }
872 if (Instruction *IIn = dyn_cast<Instruction>(Node->Idx))
873 Bs.push_back(IIn->getParent());
874 BasicBlock *TopB = nearest_common_dominatee(DT, Bs);
875
876 // Traverse the loop nest upwards until we find a loop in which Node
877 // is no longer invariant, or until we get to the upper limit of Node's
878 // placement. The traversal will also stop when a suitable "preheader"
879 // cannot be found for a given loop. The "preheader" may actually be
880 // a regular block outside of the loop (i.e. not guarded), in which case
881 // the Node will be speculated.
882 // For nodes that are not in the main path of the containing loop (i.e.
883 // are not executed in each iteration), do not move them out of the loop.
884 BasicBlock *LocB = cast_or_null<BasicBlock>(Loc[Node]);
885 if (LocB) {
886 Loop *Lp = LI->getLoopFor(LocB);
887 while (Lp) {
888 if (!isInvariantIn(Node, Lp) || !isInMainPath(LocB, Lp))
889 break;
890 BasicBlock *NewLoc = preheader(DT, Lp);
891 if (!NewLoc || !DT->dominates(TopB, NewLoc))
892 break;
893 Lp = Lp->getParentLoop();
894 LocB = NewLoc;
895 }
896 }
897 Loc[Node] = LocB;
898
899 // Recursively compute the locations of all children nodes.
900 NodeChildrenMap::iterator CF = NCM.find(Node);
901 if (CF != NCM.end()) {
902 NodeVect &Cs = CF->second;
903 for (GepNode *C : Cs)
904 adjustForInvariance(C, NCM, Loc);
905 }
906 return LocB;
907}
908
909namespace {
910
911 struct LocationAsBlock {
912 LocationAsBlock(const NodeToValueMap &L) : Map(L) {}
913
914 const NodeToValueMap &Map;
915 };
916
917 [[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS,
918 const LocationAsBlock &Loc) {
919 for (const auto &I : Loc.Map) {
920 OS << I.first << " -> ";
921 if (BasicBlock *B = cast_or_null<BasicBlock>(I.second))
922 OS << B->getName() << '(' << B << ')';
923 else
924 OS << "<null-block>";
925 OS << '\n';
926 }
927 return OS;
928 }
929
930 inline bool is_constant(GepNode *N) {
931 return isa<ConstantInt>(N->Idx);
932 }
933
934} // end anonymous namespace
935
936void HexagonCommonGEP::separateChainForNode(GepNode *Node, Use *U,
937 NodeToValueMap &Loc) {
938 User *R = U->getUser();
939 LLVM_DEBUG(dbgs() << "Separating chain for node (" << Node << ") user: " << *R
940 << '\n');
941 BasicBlock *PB = cast<Instruction>(R)->getParent();
942
943 GepNode *N = Node;
944 GepNode *C = nullptr, *NewNode = nullptr;
945 while (is_constant(N) && !(N->Flags & GepNode::Root)) {
946 // XXX if (single-use) dont-replicate;
947 GepNode *NewN = new (*Mem) GepNode(N);
948 Nodes.push_back(NewN);
949 Loc[NewN] = PB;
950
951 if (N == Node)
952 NewNode = NewN;
953 NewN->Flags &= ~GepNode::Used;
954 if (C)
955 C->Parent = NewN;
956 C = NewN;
957 N = N->Parent;
958 }
959 if (!NewNode)
960 return;
961
962 // Move over all uses that share the same user as U from Node to NewNode.
963 NodeToUsesMap::iterator UF = Uses.find(Node);
964 assert(UF != Uses.end());
965 UseSet &Us = UF->second;
966 UseSet NewUs;
967 for (Use *U : Us) {
968 if (U->getUser() == R)
969 NewUs.insert(U);
970 }
971 for (Use *U : NewUs)
972 Us.remove(U); // erase takes an iterator.
973
974 if (Us.empty()) {
975 Node->Flags &= ~GepNode::Used;
976 Uses.erase(UF);
977 }
978
979 // Should at least have U in NewUs.
980 NewNode->Flags |= GepNode::Used;
981 LLVM_DEBUG(dbgs() << "new node: " << NewNode << " " << *NewNode << '\n');
982 assert(!NewUs.empty());
983 Uses[NewNode] = NewUs;
984}
985
986void HexagonCommonGEP::separateConstantChains(GepNode *Node,
987 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
988 // First approximation: extract all chains.
989 NodeSet Ns;
990 nodes_for_root(Node, NCM, Ns);
991
992 LLVM_DEBUG(dbgs() << "Separating constant chains for node: " << Node << '\n');
993 // Collect all used nodes together with the uses from loads and stores,
994 // where the GEP node could be folded into the load/store instruction.
995 NodeToUsesMap FNs; // Foldable nodes.
996 for (GepNode *N : Ns) {
997 if (!(N->Flags & GepNode::Used))
998 continue;
999 NodeToUsesMap::iterator UF = Uses.find(N);
1000 assert(UF != Uses.end());
1001 UseSet &Us = UF->second;
1002 // Loads/stores that use the node N.
1003 UseSet LSs;
1004 for (Use *U : Us) {
1005 User *R = U->getUser();
1006 // We're interested in uses that provide the address. It can happen
1007 // that the value may also be provided via GEP, but we won't handle
1008 // those cases here for now.
1009 if (LoadInst *Ld = dyn_cast<LoadInst>(R)) {
1010 unsigned PtrX = LoadInst::getPointerOperandIndex();
1011 if (&Ld->getOperandUse(PtrX) == U)
1012 LSs.insert(U);
1013 } else if (StoreInst *St = dyn_cast<StoreInst>(R)) {
1014 unsigned PtrX = StoreInst::getPointerOperandIndex();
1015 if (&St->getOperandUse(PtrX) == U)
1016 LSs.insert(U);
1017 }
1018 }
1019 // Even if the total use count is 1, separating the chain may still be
1020 // beneficial, since the constant chain may be longer than the GEP alone
1021 // would be (e.g. if the parent node has a constant index and also has
1022 // other children).
1023 if (!LSs.empty())
1024 FNs.insert(std::make_pair(N, LSs));
1025 }
1026
1027 LLVM_DEBUG(dbgs() << "Nodes with foldable users:\n" << FNs);
1028
1029 for (auto &FN : FNs) {
1030 GepNode *N = FN.first;
1031 UseSet &Us = FN.second;
1032 for (Use *U : Us)
1033 separateChainForNode(N, U, Loc);
1034 }
1035}
1036
1037void HexagonCommonGEP::computeNodePlacement(NodeToValueMap &Loc) {
1038 // Compute the inverse of the Node.Parent links. Also, collect the set
1039 // of root nodes.
1040 NodeChildrenMap NCM;
1041 NodeVect Roots;
1042 invert_find_roots(Nodes, NCM, Roots);
1043
1044 // Compute the initial placement determined by the users' locations, and
1045 // the locations of the child nodes.
1046 for (GepNode *Root : Roots)
1047 recalculatePlacementRec(Root, NCM, Loc);
1048
1049 LLVM_DEBUG(dbgs() << "Initial node placement:\n" << LocationAsBlock(Loc));
1050
1051 if (OptEnableInv) {
1052 for (GepNode *Root : Roots)
1053 adjustForInvariance(Root, NCM, Loc);
1054
1055 LLVM_DEBUG(dbgs() << "Node placement after adjustment for invariance:\n"
1056 << LocationAsBlock(Loc));
1057 }
1058 if (OptEnableConst) {
1059 for (GepNode *Root : Roots)
1060 separateConstantChains(Root, NCM, Loc);
1061 }
1062 LLVM_DEBUG(dbgs() << "Node use information:\n" << Uses);
1063
1064 // At the moment, there is no further refinement of the initial placement.
1065 // Such a refinement could include splitting the nodes if they are placed
1066 // too far from some of its users.
1067
1068 LLVM_DEBUG(dbgs() << "Final node placement:\n" << LocationAsBlock(Loc));
1069}
1070
1071Value *HexagonCommonGEP::fabricateGEP(NodeVect &NA, BasicBlock::iterator At,
1072 BasicBlock *LocB) {
1073 LLVM_DEBUG(dbgs() << "Fabricating GEP in " << LocB->getName()
1074 << " for nodes:\n"
1075 << NA);
1076 unsigned Num = NA.size();
1077 GepNode *RN = NA[0];
1078 assert((RN->Flags & GepNode::Root) && "Creating GEP for non-root");
1079
1080 GetElementPtrInst *NewInst = nullptr;
1081 Value *Input = RN->BaseVal;
1082 Type *InpTy = RN->PTy;
1083
1084 unsigned Idx = 0;
1085 do {
1086 SmallVector<Value*, 4> IdxList;
1087 // If the type of the input of the first node is not a pointer,
1088 // we need to add an artificial i32 0 to the indices (because the
1089 // actual input in the IR will be a pointer).
1090 if (!(NA[Idx]->Flags & GepNode::Pointer)) {
1091 Type *Int32Ty = Type::getInt32Ty(*Ctx);
1092 IdxList.push_back(ConstantInt::get(Int32Ty, 0));
1093 }
1094
1095 // Keep adding indices from NA until we have to stop and generate
1096 // an "intermediate" GEP.
1097 while (++Idx <= Num) {
1098 GepNode *N = NA[Idx-1];
1099 IdxList.push_back(N->Idx);
1100 if (Idx < Num) {
1101 // We have to stop if we reach a pointer.
1102 if (NA[Idx]->Flags & GepNode::Pointer)
1103 break;
1104 }
1105 }
1106 NewInst = GetElementPtrInst::Create(InpTy, Input, IdxList, "cgep", At);
1107 NewInst->setIsInBounds(RN->Flags & GepNode::InBounds);
1108 LLVM_DEBUG(dbgs() << "new GEP: " << *NewInst << '\n');
1109 if (Idx < Num) {
1110 Input = NewInst;
1111 InpTy = NA[Idx]->PTy;
1112 }
1113 } while (Idx <= Num);
1114
1115 return NewInst;
1116}
1117
1118void HexagonCommonGEP::getAllUsersForNode(GepNode *Node, ValueVect &Values,
1119 NodeChildrenMap &NCM) {
1120 NodeVect Work;
1121 Work.push_back(Node);
1122
1123 while (!Work.empty()) {
1124 NodeVect::iterator First = Work.begin();
1125 GepNode *N = *First;
1126 Work.erase(First);
1127 if (N->Flags & GepNode::Used) {
1128 NodeToUsesMap::iterator UF = Uses.find(N);
1129 assert(UF != Uses.end() && "No use information for used node");
1130 UseSet &Us = UF->second;
1131 for (const auto &U : Us)
1132 Values.push_back(U->getUser());
1133 }
1134 NodeChildrenMap::iterator CF = NCM.find(N);
1135 if (CF != NCM.end()) {
1136 NodeVect &Cs = CF->second;
1137 llvm::append_range(Work, Cs);
1138 }
1139 }
1140}
1141
1142void HexagonCommonGEP::materialize(NodeToValueMap &Loc) {
1143 LLVM_DEBUG(dbgs() << "Nodes before materialization:\n" << Nodes << '\n');
1144 NodeChildrenMap NCM;
1145 NodeVect Roots;
1146 // Compute the inversion again, since computing placement could alter
1147 // "parent" relation between nodes.
1148 invert_find_roots(Nodes, NCM, Roots);
1149
1150 while (!Roots.empty()) {
1151 NodeVect::iterator First = Roots.begin();
1152 GepNode *Root = *First, *Last = *First;
1153 Roots.erase(First);
1154
1155 NodeVect NA; // Nodes to assemble.
1156 // Append to NA all child nodes up to (and including) the first child
1157 // that:
1158 // (1) has more than 1 child, or
1159 // (2) is used, or
1160 // (3) has a child located in a different block.
1161 bool LastUsed = false;
1162 unsigned LastCN = 0;
1163 // The location may be null if the computation failed (it can legitimately
1164 // happen for nodes created from dead GEPs).
1165 Value *LocV = Loc[Last];
1166 if (!LocV)
1167 continue;
1168 BasicBlock *LastB = cast<BasicBlock>(LocV);
1169 do {
1170 NA.push_back(Last);
1171 LastUsed = (Last->Flags & GepNode::Used);
1172 if (LastUsed)
1173 break;
1174 NodeChildrenMap::iterator CF = NCM.find(Last);
1175 LastCN = (CF != NCM.end()) ? CF->second.size() : 0;
1176 if (LastCN != 1)
1177 break;
1178 GepNode *Child = CF->second.front();
1179 BasicBlock *ChildB = cast_or_null<BasicBlock>(Loc[Child]);
1180 if (ChildB != nullptr && LastB != ChildB)
1181 break;
1182 Last = Child;
1183 } while (true);
1184
1185 BasicBlock::iterator InsertAt = LastB->getTerminator()->getIterator();
1186 if (LastUsed || LastCN > 0) {
1187 ValueVect Urs;
1188 getAllUsersForNode(Root, Urs, NCM);
1189 BasicBlock::iterator FirstUse = first_use_of_in_block(Urs, LastB);
1190 if (FirstUse != LastB->end())
1191 InsertAt = FirstUse;
1192 }
1193
1194 // Generate a new instruction for NA.
1195 Value *NewInst = fabricateGEP(NA, InsertAt, LastB);
1196
1197 // Convert all the children of Last node into roots, and append them
1198 // to the Roots list.
1199 if (LastCN > 0) {
1200 NodeVect &Cs = NCM[Last];
1201 for (GepNode *CN : Cs) {
1202 CN->Flags &= ~GepNode::Internal;
1203 CN->Flags |= GepNode::Root;
1204 CN->BaseVal = NewInst;
1205 Roots.push_back(CN);
1206 }
1207 }
1208
1209 // Lastly, if the Last node was used, replace all uses with the new GEP.
1210 // The uses reference the original GEP values.
1211 if (LastUsed) {
1212 NodeToUsesMap::iterator UF = Uses.find(Last);
1213 assert(UF != Uses.end() && "No use information found");
1214 UseSet &Us = UF->second;
1215 for (Use *U : Us)
1216 U->set(NewInst);
1217 }
1218 }
1219}
1220
1221void HexagonCommonGEP::removeDeadCode() {
1222 ValueVect BO;
1223 BO.push_back(&Fn->front());
1224
1225 for (unsigned i = 0; i < BO.size(); ++i) {
1226 BasicBlock *B = cast<BasicBlock>(BO[i]);
1227 for (auto *DTN : children<DomTreeNode *>(DT->getNode(B)))
1228 BO.push_back(DTN->getBlock());
1229 }
1230
1231 for (Value *V : llvm::reverse(BO)) {
1233 ValueVect Ins;
1234 for (Instruction &I : llvm::reverse(*B))
1235 Ins.push_back(&I);
1236 for (Value *I : Ins) {
1239 In->eraseFromParent();
1240 }
1241 }
1242}
1243
1244bool HexagonCommonGEP::runOnFunction(Function &F) {
1245 if (skipFunction(F))
1246 return false;
1247
1248 // For now bail out on C++ exception handling.
1249 for (const BasicBlock &BB : F)
1250 for (const Instruction &I : BB)
1252 return false;
1253
1254 Fn = &F;
1255 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1256 PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
1257 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1258 Ctx = &F.getContext();
1259
1260 Nodes.clear();
1261 Uses.clear();
1262 NodeOrder.clear();
1263
1264 SpecificBumpPtrAllocator<GepNode> Allocator;
1265 Mem = &Allocator;
1266
1267 collect();
1268 common();
1269
1270 NodeToValueMap Loc;
1271 computeNodePlacement(Loc);
1272 materialize(Loc);
1273 removeDeadCode();
1274
1275#ifdef EXPENSIVE_CHECKS
1276 // Run this only when expensive checks are enabled.
1277 if (verifyFunction(F, &dbgs()))
1278 report_fatal_error("Broken function");
1279#endif
1280 return true;
1281}
1282
1283namespace llvm {
1284
1286 return new HexagonCommonGEP();
1287 }
1288
1289} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
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")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
static unsigned node_hash(GepNode *N)
static cl::opt< bool > OptEnableInv("commgep-inv", cl::init(true), cl::Hidden)
static NodePair node_pair(GepNode *N1, GepNode *N2)
static bool node_eq(GepNode *N1, GepNode *N2, NodePairSet &Eq, NodePairSet &Ne)
static const NodeSet * node_class(GepNode *N, NodeSymRel &Rel)
static cl::opt< bool > OptEnableConst("commgep-const", cl::init(true), cl::Hidden)
static BasicBlock * nearest_common_dominatee(DominatorTree *DT, T &Blocks)
static cl::opt< bool > OptSpeculate("commgep-speculate", cl::init(true), cl::Hidden)
static void invert_find_roots(const NodeVect &Nodes, NodeChildrenMap &NCM, NodeVect &Roots)
static BasicBlock * nearest_common_dominator(DominatorTree *DT, T &Blocks)
static bool is_empty(const BasicBlock *B)
static void nodes_for_root(GepNode *Root, NodeChildrenMap &NCM, NodeSet &Nodes)
static BasicBlock * preheader(DominatorTree *DT, Loop *L)
static BasicBlock::iterator first_use_of_in_block(T &Values, BasicBlock *B)
This defines the Use class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
#define T
#define P(N)
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
#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
Basic Register Allocator
Remove Loads Into Fake Uses
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt)
Erases a range of instructions from FromIt to (not including) ToIt.
iterator end()
Definition BasicBlock.h:459
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
This is the shared class of boolean and integer constants.
Definition Constants.h:87
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
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.
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
LLVM_ABI 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...
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.
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
Definition FoldingSet.h:181
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
const BasicBlock & front() const
Definition Function.h:845
LLVM_ABI bool isInBounds() const
Determine whether the GEP has the inbounds flag.
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
iterator_range< op_iterator > indices()
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
Type * getSourceElementType() const
user_iterator_impl< Instruction > user_iterator
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
user_iterator user_begin()
user_iterator user_end()
static unsigned getPointerOperandIndex()
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:619
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A NodeSet contains a set of SUnit DAG nodes with additional information that assigns a priority to th...
SetVector< SUnit * >::const_iterator iterator
bool insert(SUnit *SU)
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A vector that has set insertion semantics.
Definition SetVector.h:57
void push_back(const T &Elt)
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
Definition Allocator.h:397
static unsigned getPointerOperandIndex()
Class to represent struct types.
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
LLVM_ABI StringRef getStructName() const
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:271
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasName() const
Definition Value.h:263
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void dump_node_container(raw_ostream &OS, const NodeContainer &S)
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
initializer< Ty > init(const Ty &Val)
@ User
could "use" a pointer
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
std::set< NodeId > NodeSet
Definition RDFGraph.h:551
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:315
auto min_element(R &&Range)
Provide wrappers to std::min_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2078
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
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 bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
auto cast_or_null(const Y &Val)
Definition Casting.h:714
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
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
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
FunctionPass * createHexagonCommonGEP()
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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:2192
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
#define N
GepNode(const GepNode *N)
in_set(const NodeSet &S)