1 //===- HexagonCommonGEP.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/FoldingSet.h"
11 #include "llvm/ADT/GraphTraits.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/PostDominators.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/Constant.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Dominators.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/IR/Use.h"
27 #include "llvm/IR/User.h"
28 #include "llvm/IR/Value.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Allocator.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Transforms/Utils/Local.h"
49 #define DEBUG_TYPE "commgep"
53 static cl::opt<bool> OptSpeculate("commgep-speculate", cl::init(true),
54 cl::Hidden, cl::ZeroOrMore);
56 static cl::opt<bool> OptEnableInv("commgep-inv", cl::init(true), cl::Hidden,
59 static cl::opt<bool> OptEnableConst("commgep-const", cl::init(true),
60 cl::Hidden, cl::ZeroOrMore);
64 void initializeHexagonCommonGEPPass(PassRegistry&);
66 } // end namespace llvm
71 using NodeSet = std::set<GepNode *>;
72 using NodeToValueMap = std::map<GepNode *, Value *>;
73 using NodeVect = std::vector<GepNode *>;
74 using NodeChildrenMap = std::map<GepNode *, NodeVect>;
75 using UseSet = SetVector<Use *>;
76 using NodeToUsesMap = std::map<GepNode *, UseSet>;
78 // Numbering map for gep nodes. Used to keep track of ordering for
81 NodeOrdering() = default;
83 void insert(const GepNode *N) { Map.insert(std::make_pair(N, ++LastNum)); }
84 void clear() { Map.clear(); }
86 bool operator()(const GepNode *N1, const GepNode *N2) const {
87 auto F1 = Map.find(N1), F2 = Map.find(N2);
88 assert(F1 != Map.end() && F2 != Map.end());
89 return F1->second < F2->second;
93 std::map<const GepNode *, unsigned> Map;
97 class HexagonCommonGEP : public FunctionPass {
101 HexagonCommonGEP() : FunctionPass(ID) {
102 initializeHexagonCommonGEPPass(*PassRegistry::getPassRegistry());
105 bool runOnFunction(Function &F) override;
106 StringRef getPassName() const override { return "Hexagon Common GEP"; }
108 void getAnalysisUsage(AnalysisUsage &AU) const override {
109 AU.addRequired<DominatorTreeWrapperPass>();
110 AU.addPreserved<DominatorTreeWrapperPass>();
111 AU.addRequired<PostDominatorTreeWrapperPass>();
112 AU.addPreserved<PostDominatorTreeWrapperPass>();
113 AU.addRequired<LoopInfoWrapperPass>();
114 AU.addPreserved<LoopInfoWrapperPass>();
115 FunctionPass::getAnalysisUsage(AU);
119 using ValueToNodeMap = std::map<Value *, GepNode *>;
120 using ValueVect = std::vector<Value *>;
121 using NodeToValuesMap = std::map<GepNode *, ValueVect>;
123 void getBlockTraversalOrder(BasicBlock *Root, ValueVect &Order);
124 bool isHandledGepForm(GetElementPtrInst *GepI);
125 void processGepInst(GetElementPtrInst *GepI, ValueToNodeMap &NM);
129 BasicBlock *recalculatePlacement(GepNode *Node, NodeChildrenMap &NCM,
130 NodeToValueMap &Loc);
131 BasicBlock *recalculatePlacementRec(GepNode *Node, NodeChildrenMap &NCM,
132 NodeToValueMap &Loc);
133 bool isInvariantIn(Value *Val, Loop *L);
134 bool isInvariantIn(GepNode *Node, Loop *L);
135 bool isInMainPath(BasicBlock *B, Loop *L);
136 BasicBlock *adjustForInvariance(GepNode *Node, NodeChildrenMap &NCM,
137 NodeToValueMap &Loc);
138 void separateChainForNode(GepNode *Node, Use *U, NodeToValueMap &Loc);
139 void separateConstantChains(GepNode *Node, NodeChildrenMap &NCM,
140 NodeToValueMap &Loc);
141 void computeNodePlacement(NodeToValueMap &Loc);
143 Value *fabricateGEP(NodeVect &NA, BasicBlock::iterator At,
145 void getAllUsersForNode(GepNode *Node, ValueVect &Values,
146 NodeChildrenMap &NCM);
147 void materialize(NodeToValueMap &Loc);
149 void removeDeadCode();
153 NodeOrdering NodeOrder; // Node ordering, for deterministic behavior.
154 SpecificBumpPtrAllocator<GepNode> *Mem;
158 PostDominatorTree *PDT;
162 } // end anonymous namespace
164 char HexagonCommonGEP::ID = 0;
166 INITIALIZE_PASS_BEGIN(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
168 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
169 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
170 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
171 INITIALIZE_PASS_END(HexagonCommonGEP, "hcommgep", "Hexagon Common GEP",
190 Value *Idx = nullptr;
191 Type *PTy = nullptr; // Type of the pointer operand.
193 GepNode() : Parent(nullptr) {}
194 GepNode(const GepNode *N) : Flags(N->Flags), Idx(N->Idx), PTy(N->PTy) {
196 BaseVal = N->BaseVal;
201 friend raw_ostream &operator<< (raw_ostream &OS, const GepNode &GN);
204 Type *next_type(Type *Ty, Value *Idx) {
205 if (auto *PTy = dyn_cast<PointerType>(Ty))
206 return PTy->getElementType();
207 return GetElementPtrInst::getTypeAtIndex(Ty, Idx);
210 raw_ostream &operator<< (raw_ostream &OS, const GepNode &GN) {
213 if (GN.Flags & GepNode::Root) {
217 if (GN.Flags & GepNode::Internal) {
223 if (GN.Flags & GepNode::Used) {
228 if (GN.Flags & GepNode::InBounds) {
234 if (GN.Flags & GepNode::Root)
235 OS << "BaseVal:" << GN.BaseVal->getName() << '(' << GN.BaseVal << ')';
237 OS << "Parent:" << GN.Parent;
240 if (ConstantInt *CI = dyn_cast<ConstantInt>(GN.Idx))
241 OS << CI->getValue().getSExtValue();
242 else if (GN.Idx->hasName())
243 OS << GN.Idx->getName();
245 OS << "<anon> =" << *GN.Idx;
248 if (GN.PTy->isStructTy()) {
249 StructType *STy = cast<StructType>(GN.PTy);
250 if (!STy->isLiteral())
251 OS << GN.PTy->getStructName();
253 OS << "<anon-struct>:" << *STy;
261 template <typename NodeContainer>
262 void dump_node_container(raw_ostream &OS, const NodeContainer &S) {
263 using const_iterator = typename NodeContainer::const_iterator;
265 for (const_iterator I = S.begin(), E = S.end(); I != E; ++I)
266 OS << *I << ' ' << **I << '\n';
269 raw_ostream &operator<< (raw_ostream &OS,
270 const NodeVect &S) LLVM_ATTRIBUTE_UNUSED;
271 raw_ostream &operator<< (raw_ostream &OS, const NodeVect &S) {
272 dump_node_container(OS, S);
276 raw_ostream &operator<< (raw_ostream &OS,
277 const NodeToUsesMap &M) LLVM_ATTRIBUTE_UNUSED;
278 raw_ostream &operator<< (raw_ostream &OS, const NodeToUsesMap &M){
279 using const_iterator = NodeToUsesMap::const_iterator;
281 for (const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
282 const UseSet &Us = I->second;
283 OS << I->first << " -> #" << Us.size() << '{';
284 for (UseSet::const_iterator J = Us.begin(), F = Us.end(); J != F; ++J) {
285 User *R = (*J)->getUser();
287 OS << ' ' << R->getName();
289 OS << " <?>(" << *R << ')';
297 in_set(const NodeSet &S) : NS(S) {}
299 bool operator() (GepNode *N) const {
300 return NS.find(N) != NS.end();
307 } // end anonymous namespace
309 inline void *operator new(size_t, SpecificBumpPtrAllocator<GepNode> &A) {
313 void HexagonCommonGEP::getBlockTraversalOrder(BasicBlock *Root,
315 // Compute block ordering for a typical DT-based traversal of the flow
316 // graph: "before visiting a block, all of its dominators must have been
319 Order.push_back(Root);
320 for (auto *DTN : children<DomTreeNode*>(DT->getNode(Root)))
321 getBlockTraversalOrder(DTN->getBlock(), Order);
324 bool HexagonCommonGEP::isHandledGepForm(GetElementPtrInst *GepI) {
326 if (!GepI->getType()->isPointerTy())
328 // No GEPs without any indices. (Is this possible?)
329 if (GepI->idx_begin() == GepI->idx_end())
334 void HexagonCommonGEP::processGepInst(GetElementPtrInst *GepI,
335 ValueToNodeMap &NM) {
336 LLVM_DEBUG(dbgs() << "Visiting GEP: " << *GepI << '\n');
337 GepNode *N = new (*Mem) GepNode;
338 Value *PtrOp = GepI->getPointerOperand();
339 uint32_t InBounds = GepI->isInBounds() ? GepNode::InBounds : 0;
340 ValueToNodeMap::iterator F = NM.find(PtrOp);
343 N->Flags |= GepNode::Root | InBounds;
345 // If PtrOp was a GEP instruction, it must have already been processed.
346 // The ValueToNodeMap entry for it is the last gep node in the generated
347 // chain. Link to it here.
348 N->Parent = F->second;
350 N->PTy = PtrOp->getType();
351 N->Idx = *GepI->idx_begin();
353 // Collect the list of users of this GEP instruction. Will add it to the
354 // last node created for it.
356 for (Value::user_iterator UI = GepI->user_begin(), UE = GepI->user_end();
358 // Check if this gep is used by anything other than other geps that
360 if (isa<GetElementPtrInst>(*UI)) {
361 GetElementPtrInst *UserG = cast<GetElementPtrInst>(*UI);
362 if (isHandledGepForm(UserG))
365 Us.insert(&UI.getUse());
370 // Skip the first index operand, since we only handle 0. This dereferences
371 // the pointer operand.
373 Type *PtrTy = cast<PointerType>(PtrOp->getType())->getElementType();
374 for (User::op_iterator OI = GepI->idx_begin()+1, OE = GepI->idx_end();
377 GepNode *Nx = new (*Mem) GepNode;
378 Nx->Parent = PN; // Link Nx to the previous node.
379 Nx->Flags |= GepNode::Internal | InBounds;
383 NodeOrder.insert(Nx);
386 PtrTy = next_type(PtrTy, Op);
389 // After last node has been created, update the use information.
391 PN->Flags |= GepNode::Used;
392 Uses[PN].insert(Us.begin(), Us.end());
395 // Link the last node with the originating GEP instruction. This is to
396 // help with linking chained GEP instructions.
397 NM.insert(std::make_pair(GepI, PN));
400 void HexagonCommonGEP::collect() {
401 // Establish depth-first traversal order of the dominator tree.
403 getBlockTraversalOrder(&Fn->front(), BO);
405 // The creation of gep nodes requires DT-traversal. When processing a GEP
406 // instruction that uses another GEP instruction as the base pointer, the
407 // gep node for the base pointer should already exist.
409 for (ValueVect::iterator I = BO.begin(), E = BO.end(); I != E; ++I) {
410 BasicBlock *B = cast<BasicBlock>(*I);
411 for (BasicBlock::iterator J = B->begin(), F = B->end(); J != F; ++J) {
412 if (!isa<GetElementPtrInst>(J))
414 GetElementPtrInst *GepI = cast<GetElementPtrInst>(J);
415 if (isHandledGepForm(GepI))
416 processGepInst(GepI, NM);
420 LLVM_DEBUG(dbgs() << "Gep nodes after initial collection:\n" << Nodes);
423 static void invert_find_roots(const NodeVect &Nodes, NodeChildrenMap &NCM,
425 using const_iterator = NodeVect::const_iterator;
427 for (const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) {
429 if (N->Flags & GepNode::Root) {
433 GepNode *PN = N->Parent;
434 NCM[PN].push_back(N);
438 static void nodes_for_root(GepNode *Root, NodeChildrenMap &NCM,
441 Work.push_back(Root);
444 while (!Work.empty()) {
445 NodeVect::iterator First = Work.begin();
448 NodeChildrenMap::iterator CF = NCM.find(N);
449 if (CF != NCM.end()) {
450 Work.insert(Work.end(), CF->second.begin(), CF->second.end());
451 Nodes.insert(CF->second.begin(), CF->second.end());
458 using NodeSymRel = std::set<NodeSet>;
459 using NodePair = std::pair<GepNode *, GepNode *>;
460 using NodePairSet = std::set<NodePair>;
462 } // end anonymous namespace
464 static const NodeSet *node_class(GepNode *N, NodeSymRel &Rel) {
465 for (NodeSymRel::iterator I = Rel.begin(), E = Rel.end(); I != E; ++I)
471 // Create an ordered pair of GepNode pointers. The pair will be used in
472 // determining equality. The only purpose of the ordering is to eliminate
473 // duplication due to the commutativity of equality/non-equality.
474 static NodePair node_pair(GepNode *N1, GepNode *N2) {
475 uintptr_t P1 = uintptr_t(N1), P2 = uintptr_t(N2);
477 return std::make_pair(N1, N2);
478 return std::make_pair(N2, N1);
481 static unsigned node_hash(GepNode *N) {
482 // Include everything except flags and parent.
484 ID.AddPointer(N->Idx);
485 ID.AddPointer(N->PTy);
486 return ID.ComputeHash();
489 static bool node_eq(GepNode *N1, GepNode *N2, NodePairSet &Eq,
491 // Don't cache the result for nodes with different hashes. The hash
492 // comparison is fast enough.
493 if (node_hash(N1) != node_hash(N2))
496 NodePair NP = node_pair(N1, N2);
497 NodePairSet::iterator FEq = Eq.find(NP);
500 NodePairSet::iterator FNe = Ne.find(NP);
503 // Not previously compared.
504 bool Root1 = N1->Flags & GepNode::Root;
505 bool Root2 = N2->Flags & GepNode::Root;
506 NodePair P = node_pair(N1, N2);
507 // If the Root flag has different values, the nodes are different.
508 // If both nodes are root nodes, but their base pointers differ,
509 // they are different.
510 if (Root1 != Root2 || (Root1 && N1->BaseVal != N2->BaseVal)) {
514 // Here the root flags are identical, and for root nodes the
515 // base pointers are equal, so the root nodes are equal.
516 // For non-root nodes, compare their parent nodes.
517 if (Root1 || node_eq(N1->Parent, N2->Parent, Eq, Ne)) {
524 void HexagonCommonGEP::common() {
525 // The essence of this commoning is finding gep nodes that are equal.
526 // To do this we need to compare all pairs of nodes. To save time,
527 // first, partition the set of all nodes into sets of potentially equal
528 // nodes, and then compare pairs from within each partition.
529 using NodeSetMap = std::map<unsigned, NodeSet>;
532 for (NodeVect::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) {
534 unsigned H = node_hash(N);
535 MaybeEq[H].insert(N);
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 (NodeSetMap::iterator I = MaybeEq.begin(), E = MaybeEq.end();
544 NodeSet &S = I->second;
545 for (NodeSet::iterator NI = S.begin(), NE = S.end(); NI != NE; ++NI) {
547 // If node already has a class, then the class must have been created
548 // in a prior iteration of this loop. Since equality is transitive,
549 // nothing more will be added to that class, so skip it.
550 if (node_class(N, EqRel))
553 // Create a new class candidate now.
555 for (NodeSet::iterator NJ = std::next(NI); NJ != NE; ++NJ)
556 if (node_eq(N, *NJ, Eq, Ne))
558 // If Tmp is empty, N would be the only element in it. Don't bother
559 // creating a class for it then.
561 C.insert(N); // Finalize the set before adding it to the relation.
562 std::pair<NodeSymRel::iterator, bool> Ins = EqRel.insert(C);
564 assert(Ins.second && "Cannot add a class");
570 dbgs() << "Gep node equality:\n";
571 for (NodePairSet::iterator I = Eq.begin(), E = Eq.end(); I != E; ++I)
572 dbgs() << "{ " << I->first << ", " << I->second << " }\n";
574 dbgs() << "Gep equivalence classes:\n";
575 for (NodeSymRel::iterator I = EqRel.begin(), E = EqRel.end(); I != E; ++I) {
577 const NodeSet &S = *I;
578 for (NodeSet::const_iterator J = S.begin(), F = S.end(); J != F; ++J) {
587 // Create a projection from a NodeSet to the minimal element in it.
588 using ProjMap = std::map<const NodeSet *, GepNode *>;
590 for (NodeSymRel::iterator I = EqRel.begin(), E = EqRel.end(); I != E; ++I) {
591 const NodeSet &S = *I;
592 GepNode *Min = *std::min_element(S.begin(), S.end(), NodeOrder);
593 std::pair<ProjMap::iterator,bool> Ins = PM.insert(std::make_pair(&S, Min));
595 assert(Ins.second && "Cannot add minimal element");
597 // Update the min element's flags, and user list.
599 UseSet &MinUs = Uses[Min];
600 for (NodeSet::iterator J = S.begin(), F = S.end(); J != F; ++J) {
602 uint32_t NF = N->Flags;
603 // If N is used, append all original values of N to the list of
604 // original values of Min.
605 if (NF & GepNode::Used)
606 MinUs.insert(Uses[N].begin(), Uses[N].end());
612 // The collected flags should include all the flags from the min element.
613 assert((Min->Flags & Flags) == Min->Flags);
617 // Commoning: for each non-root gep node, replace "Parent" with the
618 // selected (minimum) node from the corresponding equivalence class.
619 // If a given parent does not have an equivalence class, leave it
620 // unchanged (it means that it's the only element in its class).
621 for (NodeVect::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) {
623 if (N->Flags & GepNode::Root)
625 const NodeSet *PC = node_class(N->Parent, EqRel);
628 ProjMap::iterator F = PM.find(PC);
631 // Found a replacement, use it.
632 GepNode *Rep = F->second;
636 LLVM_DEBUG(dbgs() << "Gep nodes after commoning:\n" << Nodes);
638 // Finally, erase the nodes that are no longer used.
640 for (NodeVect::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) {
642 const NodeSet *PC = node_class(N, EqRel);
645 ProjMap::iterator F = PM.find(PC);
653 NodeVect::iterator NewE = remove_if(Nodes, in_set(Erase));
654 Nodes.resize(std::distance(Nodes.begin(), NewE));
656 LLVM_DEBUG(dbgs() << "Gep nodes after post-commoning cleanup:\n" << Nodes);
659 template <typename T>
660 static BasicBlock *nearest_common_dominator(DominatorTree *DT, T &Blocks) {
662 dbgs() << "NCD of {";
663 for (typename T::iterator I = Blocks.begin(), E = Blocks.end(); I != E;
667 BasicBlock *B = cast<BasicBlock>(*I);
668 dbgs() << ' ' << B->getName();
673 // Allow null basic blocks in Blocks. In such cases, return nullptr.
674 typename T::iterator I = Blocks.begin(), E = Blocks.end();
677 BasicBlock *Dom = cast<BasicBlock>(*I);
679 BasicBlock *B = cast_or_null<BasicBlock>(*I);
680 Dom = B ? DT->findNearestCommonDominator(Dom, B) : nullptr;
684 LLVM_DEBUG(dbgs() << "computed:" << Dom->getName() << '\n');
688 template <typename T>
689 static BasicBlock *nearest_common_dominatee(DominatorTree *DT, T &Blocks) {
690 // If two blocks, A and B, dominate a block C, then A dominates B,
692 typename T::iterator I = Blocks.begin(), E = Blocks.end();
693 // Find the first non-null block.
694 while (I != E && !*I)
697 return DT->getRoot();
698 BasicBlock *DomB = cast<BasicBlock>(*I);
702 BasicBlock *B = cast<BasicBlock>(*I);
703 if (DT->dominates(B, DomB))
705 if (!DT->dominates(DomB, B))
712 // Find the first use in B of any value from Values. If no such use,
714 template <typename T>
715 static BasicBlock::iterator first_use_of_in_block(T &Values, BasicBlock *B) {
716 BasicBlock::iterator FirstUse = B->end(), BEnd = B->end();
718 using iterator = typename T::iterator;
720 for (iterator I = Values.begin(), E = Values.end(); I != E; ++I) {
722 // If V is used in a PHI node, the use belongs to the incoming block,
723 // not the block with the PHI node. In the incoming block, the use
724 // would be considered as being at the end of it, so it cannot
725 // influence the position of the first use (which is assumed to be
726 // at the end to start with).
729 if (!isa<Instruction>(V))
731 Instruction *In = cast<Instruction>(V);
732 if (In->getParent() != B)
734 BasicBlock::iterator It = In->getIterator();
735 if (std::distance(FirstUse, BEnd) < std::distance(It, BEnd))
741 static bool is_empty(const BasicBlock *B) {
742 return B->empty() || (&*B->begin() == B->getTerminator());
745 BasicBlock *HexagonCommonGEP::recalculatePlacement(GepNode *Node,
746 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
747 LLVM_DEBUG(dbgs() << "Loc for node:" << Node << '\n');
748 // Recalculate the placement for Node, assuming that the locations of
749 // its children in Loc are valid.
750 // Return nullptr if there is no valid placement for Node (for example, it
751 // uses an index value that is not available at the location required
752 // to dominate all children, etc.).
754 // Find the nearest common dominator for:
755 // - all users, if the node is used, and
758 if (Node->Flags & GepNode::Used) {
759 // Append all blocks with uses of the original values to the
761 NodeToUsesMap::iterator UF = Uses.find(Node);
762 assert(UF != Uses.end() && "Used node with no use information");
763 UseSet &Us = UF->second;
764 for (UseSet::iterator I = Us.begin(), E = Us.end(); I != E; ++I) {
766 User *R = U->getUser();
767 if (!isa<Instruction>(R))
769 BasicBlock *PB = isa<PHINode>(R)
770 ? cast<PHINode>(R)->getIncomingBlock(*U)
771 : cast<Instruction>(R)->getParent();
775 // Append the location of each child.
776 NodeChildrenMap::iterator CF = NCM.find(Node);
777 if (CF != NCM.end()) {
778 NodeVect &Cs = CF->second;
779 for (NodeVect::iterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
781 NodeToValueMap::iterator LF = Loc.find(CN);
782 // If the child is only used in GEP instructions (i.e. is not used in
783 // non-GEP instructions), the nearest dominator computed for it may
784 // have been null. In such case it won't have a location available.
787 Bs.push_back(LF->second);
791 BasicBlock *DomB = nearest_common_dominator(DT, Bs);
794 // Check if the index used by Node dominates the computed dominator.
795 Instruction *IdxI = dyn_cast<Instruction>(Node->Idx);
796 if (IdxI && !DT->dominates(IdxI->getParent(), DomB))
799 // Avoid putting nodes into empty blocks.
800 while (is_empty(DomB)) {
801 DomTreeNode *N = (*DT)[DomB]->getIDom();
804 DomB = N->getBlock();
807 // Otherwise, DomB is fine. Update the location map.
812 BasicBlock *HexagonCommonGEP::recalculatePlacementRec(GepNode *Node,
813 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
814 LLVM_DEBUG(dbgs() << "LocRec begin for node:" << Node << '\n');
815 // Recalculate the placement of Node, after recursively recalculating the
816 // placements of all its children.
817 NodeChildrenMap::iterator CF = NCM.find(Node);
818 if (CF != NCM.end()) {
819 NodeVect &Cs = CF->second;
820 for (NodeVect::iterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
821 recalculatePlacementRec(*I, NCM, Loc);
823 BasicBlock *LB = recalculatePlacement(Node, NCM, Loc);
824 LLVM_DEBUG(dbgs() << "LocRec end for node:" << Node << '\n');
828 bool HexagonCommonGEP::isInvariantIn(Value *Val, Loop *L) {
829 if (isa<Constant>(Val) || isa<Argument>(Val))
831 Instruction *In = dyn_cast<Instruction>(Val);
834 BasicBlock *HdrB = L->getHeader(), *DefB = In->getParent();
835 return DT->properlyDominates(DefB, HdrB);
838 bool HexagonCommonGEP::isInvariantIn(GepNode *Node, Loop *L) {
839 if (Node->Flags & GepNode::Root)
840 if (!isInvariantIn(Node->BaseVal, L))
842 return isInvariantIn(Node->Idx, L);
845 bool HexagonCommonGEP::isInMainPath(BasicBlock *B, Loop *L) {
846 BasicBlock *HB = L->getHeader();
847 BasicBlock *LB = L->getLoopLatch();
848 // B must post-dominate the loop header or dominate the loop latch.
849 if (PDT->dominates(B, HB))
851 if (LB && DT->dominates(B, LB))
856 static BasicBlock *preheader(DominatorTree *DT, Loop *L) {
857 if (BasicBlock *PH = L->getLoopPreheader())
861 DomTreeNode *DN = DT->getNode(L->getHeader());
864 return DN->getIDom()->getBlock();
867 BasicBlock *HexagonCommonGEP::adjustForInvariance(GepNode *Node,
868 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
869 // Find the "topmost" location for Node: it must be dominated by both,
870 // its parent (or the BaseVal, if it's a root node), and by the index
873 if (Node->Flags & GepNode::Root) {
874 if (Instruction *PIn = dyn_cast<Instruction>(Node->BaseVal))
875 Bs.push_back(PIn->getParent());
877 Bs.push_back(Loc[Node->Parent]);
879 if (Instruction *IIn = dyn_cast<Instruction>(Node->Idx))
880 Bs.push_back(IIn->getParent());
881 BasicBlock *TopB = nearest_common_dominatee(DT, Bs);
883 // Traverse the loop nest upwards until we find a loop in which Node
884 // is no longer invariant, or until we get to the upper limit of Node's
885 // placement. The traversal will also stop when a suitable "preheader"
886 // cannot be found for a given loop. The "preheader" may actually be
887 // a regular block outside of the loop (i.e. not guarded), in which case
888 // the Node will be speculated.
889 // For nodes that are not in the main path of the containing loop (i.e.
890 // are not executed in each iteration), do not move them out of the loop.
891 BasicBlock *LocB = cast_or_null<BasicBlock>(Loc[Node]);
893 Loop *Lp = LI->getLoopFor(LocB);
895 if (!isInvariantIn(Node, Lp) || !isInMainPath(LocB, Lp))
897 BasicBlock *NewLoc = preheader(DT, Lp);
898 if (!NewLoc || !DT->dominates(TopB, NewLoc))
900 Lp = Lp->getParentLoop();
906 // Recursively compute the locations of all children nodes.
907 NodeChildrenMap::iterator CF = NCM.find(Node);
908 if (CF != NCM.end()) {
909 NodeVect &Cs = CF->second;
910 for (NodeVect::iterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
911 adjustForInvariance(*I, NCM, Loc);
918 struct LocationAsBlock {
919 LocationAsBlock(const NodeToValueMap &L) : Map(L) {}
921 const NodeToValueMap ⤅
924 raw_ostream &operator<< (raw_ostream &OS,
925 const LocationAsBlock &Loc) LLVM_ATTRIBUTE_UNUSED ;
926 raw_ostream &operator<< (raw_ostream &OS, const LocationAsBlock &Loc) {
927 for (NodeToValueMap::const_iterator I = Loc.Map.begin(), E = Loc.Map.end();
929 OS << I->first << " -> ";
930 BasicBlock *B = cast<BasicBlock>(I->second);
931 OS << B->getName() << '(' << B << ')';
937 inline bool is_constant(GepNode *N) {
938 return isa<ConstantInt>(N->Idx);
941 } // end anonymous namespace
943 void HexagonCommonGEP::separateChainForNode(GepNode *Node, Use *U,
944 NodeToValueMap &Loc) {
945 User *R = U->getUser();
946 LLVM_DEBUG(dbgs() << "Separating chain for node (" << Node << ") user: " << *R
948 BasicBlock *PB = cast<Instruction>(R)->getParent();
951 GepNode *C = nullptr, *NewNode = nullptr;
952 while (is_constant(N) && !(N->Flags & GepNode::Root)) {
953 // XXX if (single-use) dont-replicate;
954 GepNode *NewN = new (*Mem) GepNode(N);
955 Nodes.push_back(NewN);
960 NewN->Flags &= ~GepNode::Used;
969 // Move over all uses that share the same user as U from Node to NewNode.
970 NodeToUsesMap::iterator UF = Uses.find(Node);
971 assert(UF != Uses.end());
972 UseSet &Us = UF->second;
975 if (U->getUser() == R)
979 Us.remove(U); // erase takes an iterator.
982 Node->Flags &= ~GepNode::Used;
986 // Should at least have U in NewUs.
987 NewNode->Flags |= GepNode::Used;
988 LLVM_DEBUG(dbgs() << "new node: " << NewNode << " " << *NewNode << '\n');
989 assert(!NewUs.empty());
990 Uses[NewNode] = NewUs;
993 void HexagonCommonGEP::separateConstantChains(GepNode *Node,
994 NodeChildrenMap &NCM, NodeToValueMap &Loc) {
995 // First approximation: extract all chains.
997 nodes_for_root(Node, NCM, Ns);
999 LLVM_DEBUG(dbgs() << "Separating constant chains for node: " << Node << '\n');
1000 // Collect all used nodes together with the uses from loads and stores,
1001 // where the GEP node could be folded into the load/store instruction.
1002 NodeToUsesMap FNs; // Foldable nodes.
1003 for (NodeSet::iterator I = Ns.begin(), E = Ns.end(); I != E; ++I) {
1005 if (!(N->Flags & GepNode::Used))
1007 NodeToUsesMap::iterator UF = Uses.find(N);
1008 assert(UF != Uses.end());
1009 UseSet &Us = UF->second;
1010 // Loads/stores that use the node N.
1012 for (UseSet::iterator J = Us.begin(), F = Us.end(); J != F; ++J) {
1014 User *R = U->getUser();
1015 // We're interested in uses that provide the address. It can happen
1016 // that the value may also be provided via GEP, but we won't handle
1017 // those cases here for now.
1018 if (LoadInst *Ld = dyn_cast<LoadInst>(R)) {
1019 unsigned PtrX = LoadInst::getPointerOperandIndex();
1020 if (&Ld->getOperandUse(PtrX) == U)
1022 } else if (StoreInst *St = dyn_cast<StoreInst>(R)) {
1023 unsigned PtrX = StoreInst::getPointerOperandIndex();
1024 if (&St->getOperandUse(PtrX) == U)
1028 // Even if the total use count is 1, separating the chain may still be
1029 // beneficial, since the constant chain may be longer than the GEP alone
1030 // would be (e.g. if the parent node has a constant index and also has
1033 FNs.insert(std::make_pair(N, LSs));
1036 LLVM_DEBUG(dbgs() << "Nodes with foldable users:\n" << FNs);
1038 for (NodeToUsesMap::iterator I = FNs.begin(), E = FNs.end(); I != E; ++I) {
1039 GepNode *N = I->first;
1040 UseSet &Us = I->second;
1041 for (UseSet::iterator J = Us.begin(), F = Us.end(); J != F; ++J)
1042 separateChainForNode(N, *J, Loc);
1046 void HexagonCommonGEP::computeNodePlacement(NodeToValueMap &Loc) {
1047 // Compute the inverse of the Node.Parent links. Also, collect the set
1049 NodeChildrenMap NCM;
1051 invert_find_roots(Nodes, NCM, Roots);
1053 // Compute the initial placement determined by the users' locations, and
1054 // the locations of the child nodes.
1055 for (NodeVect::iterator I = Roots.begin(), E = Roots.end(); I != E; ++I)
1056 recalculatePlacementRec(*I, NCM, Loc);
1058 LLVM_DEBUG(dbgs() << "Initial node placement:\n" << LocationAsBlock(Loc));
1061 for (NodeVect::iterator I = Roots.begin(), E = Roots.end(); I != E; ++I)
1062 adjustForInvariance(*I, NCM, Loc);
1064 LLVM_DEBUG(dbgs() << "Node placement after adjustment for invariance:\n"
1065 << LocationAsBlock(Loc));
1067 if (OptEnableConst) {
1068 for (NodeVect::iterator I = Roots.begin(), E = Roots.end(); I != E; ++I)
1069 separateConstantChains(*I, NCM, Loc);
1071 LLVM_DEBUG(dbgs() << "Node use information:\n" << Uses);
1073 // At the moment, there is no further refinement of the initial placement.
1074 // Such a refinement could include splitting the nodes if they are placed
1075 // too far from some of its users.
1077 LLVM_DEBUG(dbgs() << "Final node placement:\n" << LocationAsBlock(Loc));
1080 Value *HexagonCommonGEP::fabricateGEP(NodeVect &NA, BasicBlock::iterator At,
1082 LLVM_DEBUG(dbgs() << "Fabricating GEP in " << LocB->getName()
1085 unsigned Num = NA.size();
1086 GepNode *RN = NA[0];
1087 assert((RN->Flags & GepNode::Root) && "Creating GEP for non-root");
1089 GetElementPtrInst *NewInst = nullptr;
1090 Value *Input = RN->BaseVal;
1091 Value **IdxList = new Value*[Num+1];
1095 // If the type of the input of the first node is not a pointer,
1096 // we need to add an artificial i32 0 to the indices (because the
1097 // actual input in the IR will be a pointer).
1098 if (!NA[nax]->PTy->isPointerTy()) {
1099 Type *Int32Ty = Type::getInt32Ty(*Ctx);
1100 IdxList[IdxC++] = ConstantInt::get(Int32Ty, 0);
1103 // Keep adding indices from NA until we have to stop and generate
1104 // an "intermediate" GEP.
1105 while (++nax <= Num) {
1106 GepNode *N = NA[nax-1];
1107 IdxList[IdxC++] = N->Idx;
1109 // We have to stop, if the expected type of the output of this node
1110 // is not the same as the input type of the next node.
1111 Type *NextTy = next_type(N->PTy, N->Idx);
1112 if (NextTy != NA[nax]->PTy)
1116 ArrayRef<Value*> A(IdxList, IdxC);
1117 Type *InpTy = Input->getType();
1118 Type *ElTy = cast<PointerType>(InpTy->getScalarType())->getElementType();
1119 NewInst = GetElementPtrInst::Create(ElTy, Input, A, "cgep", &*At);
1120 NewInst->setIsInBounds(RN->Flags & GepNode::InBounds);
1121 LLVM_DEBUG(dbgs() << "new GEP: " << *NewInst << '\n');
1123 } while (nax <= Num);
1129 void HexagonCommonGEP::getAllUsersForNode(GepNode *Node, ValueVect &Values,
1130 NodeChildrenMap &NCM) {
1132 Work.push_back(Node);
1134 while (!Work.empty()) {
1135 NodeVect::iterator First = Work.begin();
1136 GepNode *N = *First;
1138 if (N->Flags & GepNode::Used) {
1139 NodeToUsesMap::iterator UF = Uses.find(N);
1140 assert(UF != Uses.end() && "No use information for used node");
1141 UseSet &Us = UF->second;
1142 for (UseSet::iterator I = Us.begin(), E = Us.end(); I != E; ++I)
1143 Values.push_back((*I)->getUser());
1145 NodeChildrenMap::iterator CF = NCM.find(N);
1146 if (CF != NCM.end()) {
1147 NodeVect &Cs = CF->second;
1148 Work.insert(Work.end(), Cs.begin(), Cs.end());
1153 void HexagonCommonGEP::materialize(NodeToValueMap &Loc) {
1154 LLVM_DEBUG(dbgs() << "Nodes before materialization:\n" << Nodes << '\n');
1155 NodeChildrenMap NCM;
1157 // Compute the inversion again, since computing placement could alter
1158 // "parent" relation between nodes.
1159 invert_find_roots(Nodes, NCM, Roots);
1161 while (!Roots.empty()) {
1162 NodeVect::iterator First = Roots.begin();
1163 GepNode *Root = *First, *Last = *First;
1166 NodeVect NA; // Nodes to assemble.
1167 // Append to NA all child nodes up to (and including) the first child
1169 // (1) has more than 1 child, or
1171 // (3) has a child located in a different block.
1172 bool LastUsed = false;
1173 unsigned LastCN = 0;
1174 // The location may be null if the computation failed (it can legitimately
1175 // happen for nodes created from dead GEPs).
1176 Value *LocV = Loc[Last];
1179 BasicBlock *LastB = cast<BasicBlock>(LocV);
1182 LastUsed = (Last->Flags & GepNode::Used);
1185 NodeChildrenMap::iterator CF = NCM.find(Last);
1186 LastCN = (CF != NCM.end()) ? CF->second.size() : 0;
1189 GepNode *Child = CF->second.front();
1190 BasicBlock *ChildB = cast_or_null<BasicBlock>(Loc[Child]);
1191 if (ChildB != nullptr && LastB != ChildB)
1196 BasicBlock::iterator InsertAt = LastB->getTerminator()->getIterator();
1197 if (LastUsed || LastCN > 0) {
1199 getAllUsersForNode(Root, Urs, NCM);
1200 BasicBlock::iterator FirstUse = first_use_of_in_block(Urs, LastB);
1201 if (FirstUse != LastB->end())
1202 InsertAt = FirstUse;
1205 // Generate a new instruction for NA.
1206 Value *NewInst = fabricateGEP(NA, InsertAt, LastB);
1208 // Convert all the children of Last node into roots, and append them
1209 // to the Roots list.
1211 NodeVect &Cs = NCM[Last];
1212 for (NodeVect::iterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1214 CN->Flags &= ~GepNode::Internal;
1215 CN->Flags |= GepNode::Root;
1216 CN->BaseVal = NewInst;
1217 Roots.push_back(CN);
1221 // Lastly, if the Last node was used, replace all uses with the new GEP.
1222 // The uses reference the original GEP values.
1224 NodeToUsesMap::iterator UF = Uses.find(Last);
1225 assert(UF != Uses.end() && "No use information found");
1226 UseSet &Us = UF->second;
1227 for (UseSet::iterator I = Us.begin(), E = Us.end(); I != E; ++I) {
1235 void HexagonCommonGEP::removeDeadCode() {
1237 BO.push_back(&Fn->front());
1239 for (unsigned i = 0; i < BO.size(); ++i) {
1240 BasicBlock *B = cast<BasicBlock>(BO[i]);
1241 for (auto DTN : children<DomTreeNode*>(DT->getNode(B)))
1242 BO.push_back(DTN->getBlock());
1245 for (unsigned i = BO.size(); i > 0; --i) {
1246 BasicBlock *B = cast<BasicBlock>(BO[i-1]);
1247 BasicBlock::InstListType &IL = B->getInstList();
1249 using reverse_iterator = BasicBlock::InstListType::reverse_iterator;
1252 for (reverse_iterator I = IL.rbegin(), E = IL.rend(); I != E; ++I)
1254 for (ValueVect::iterator I = Ins.begin(), E = Ins.end(); I != E; ++I) {
1255 Instruction *In = cast<Instruction>(*I);
1256 if (isInstructionTriviallyDead(In))
1257 In->eraseFromParent();
1262 bool HexagonCommonGEP::runOnFunction(Function &F) {
1263 if (skipFunction(F))
1266 // For now bail out on C++ exception handling.
1267 for (Function::iterator A = F.begin(), Z = F.end(); A != Z; ++A)
1268 for (BasicBlock::iterator I = A->begin(), E = A->end(); I != E; ++I)
1269 if (isa<InvokeInst>(I) || isa<LandingPadInst>(I))
1273 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1274 PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
1275 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1276 Ctx = &F.getContext();
1282 SpecificBumpPtrAllocator<GepNode> Allocator;
1289 computeNodePlacement(Loc);
1293 #ifdef EXPENSIVE_CHECKS
1294 // Run this only when expensive checks are enabled.
1295 if (verifyFunction(F, &dbgs()))
1296 report_fatal_error("Broken function");
1303 FunctionPass *createHexagonCommonGEP() {
1304 return new HexagonCommonGEP();
1307 } // end namespace llvm