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