1 //===--- HexagonStoreWidening.cpp------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Replace sequences of "narrow" stores to adjacent memory locations with
10 // a fewer "wide" stores that have the same effect.
11 // For example, replace:
12 //   S4_storeirb_io  %vreg100, 0, 0   ; store-immediate-byte
13 //   S4_storeirb_io  %vreg100, 1, 0   ; store-immediate-byte
14 // with
15 //   S4_storeirh_io  %vreg100, 0, 0   ; store-immediate-halfword
16 // The above is the general idea.  The actual cases handled by the code
17 // may be a bit more complex.
18 // The purpose of this pass is to reduce the number of outstanding stores,
19 // or as one could say, "reduce store queue pressure".  Also, wide stores
20 // mean fewer stores, and since there are only two memory instructions allowed
21 // per packet, it also means fewer packets, and ultimately fewer cycles.
22 //===---------------------------------------------------------------------===//
23 
24 #define DEBUG_TYPE "hexagon-widen-stores"
25 
26 #include "HexagonTargetMachine.h"
27 
28 #include "llvm/PassSupport.h"
29 #include "llvm/Analysis/AliasAnalysis.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/MC/MCInstrDesc.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetRegisterInfo.h"
40 #include "llvm/Target/TargetInstrInfo.h"
41 
42 #include <algorithm>
43 
44 
45 using namespace llvm;
46 
47 namespace llvm {
48   FunctionPass *createHexagonStoreWidening();
49   void initializeHexagonStoreWideningPass(PassRegistry&);
50 }
51 
52 namespace {
53   struct HexagonStoreWidening : public MachineFunctionPass {
54     const HexagonInstrInfo      *TII;
55     const HexagonRegisterInfo   *TRI;
56     const MachineRegisterInfo   *MRI;
57     AliasAnalysis               *AA;
58     MachineFunction             *MF;
59 
60   public:
61     static char ID;
62     HexagonStoreWidening() : MachineFunctionPass(ID) {
63       initializeHexagonStoreWideningPass(*PassRegistry::getPassRegistry());
64     }
65 
66     bool runOnMachineFunction(MachineFunction &MF) override;
67 
68     StringRef getPassName() const override { return "Hexagon Store Widening"; }
69 
70     void getAnalysisUsage(AnalysisUsage &AU) const override {
71       AU.addRequired<AAResultsWrapperPass>();
72       AU.addPreserved<AAResultsWrapperPass>();
73       MachineFunctionPass::getAnalysisUsage(AU);
74     }
75 
76     static bool handledStoreType(const MachineInstr *MI);
77 
78   private:
79     static const int MaxWideSize = 4;
80 
81     typedef std::vector<MachineInstr*> InstrGroup;
82     typedef std::vector<InstrGroup> InstrGroupList;
83 
84     bool instrAliased(InstrGroup &Stores, const MachineMemOperand &MMO);
85     bool instrAliased(InstrGroup &Stores, const MachineInstr *MI);
86     void createStoreGroup(MachineInstr *BaseStore, InstrGroup::iterator Begin,
87         InstrGroup::iterator End, InstrGroup &Group);
88     void createStoreGroups(MachineBasicBlock &MBB,
89         InstrGroupList &StoreGroups);
90     bool processBasicBlock(MachineBasicBlock &MBB);
91     bool processStoreGroup(InstrGroup &Group);
92     bool selectStores(InstrGroup::iterator Begin, InstrGroup::iterator End,
93         InstrGroup &OG, unsigned &TotalSize, unsigned MaxSize);
94     bool createWideStores(InstrGroup &OG, InstrGroup &NG, unsigned TotalSize);
95     bool replaceStores(InstrGroup &OG, InstrGroup &NG);
96     bool storesAreAdjacent(const MachineInstr *S1, const MachineInstr *S2);
97   };
98 
99 } // namespace
100 
101 
102 namespace {
103 
104 // Some local helper functions...
105 unsigned getBaseAddressRegister(const MachineInstr *MI) {
106   const MachineOperand &MO = MI->getOperand(0);
107   assert(MO.isReg() && "Expecting register operand");
108   return MO.getReg();
109 }
110 
111 int64_t getStoreOffset(const MachineInstr *MI) {
112   unsigned OpC = MI->getOpcode();
113   assert(HexagonStoreWidening::handledStoreType(MI) && "Unhandled opcode");
114 
115   switch (OpC) {
116     case Hexagon::S4_storeirb_io:
117     case Hexagon::S4_storeirh_io:
118     case Hexagon::S4_storeiri_io: {
119       const MachineOperand &MO = MI->getOperand(1);
120       assert(MO.isImm() && "Expecting immediate offset");
121       return MO.getImm();
122     }
123   }
124   dbgs() << *MI;
125   llvm_unreachable("Store offset calculation missing for a handled opcode");
126   return 0;
127 }
128 
129 const MachineMemOperand &getStoreTarget(const MachineInstr *MI) {
130   assert(!MI->memoperands_empty() && "Expecting memory operands");
131   return **MI->memoperands_begin();
132 }
133 
134 } // namespace
135 
136 
137 char HexagonStoreWidening::ID = 0;
138 
139 INITIALIZE_PASS_BEGIN(HexagonStoreWidening, "hexagon-widen-stores",
140                 "Hexason Store Widening", false, false)
141 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
142 INITIALIZE_PASS_END(HexagonStoreWidening, "hexagon-widen-stores",
143                 "Hexagon Store Widening", false, false)
144 
145 
146 // Filtering function: any stores whose opcodes are not "approved" of by
147 // this function will not be subjected to widening.
148 inline bool HexagonStoreWidening::handledStoreType(const MachineInstr *MI) {
149   // For now, only handle stores of immediate values.
150   // Also, reject stores to stack slots.
151   unsigned Opc = MI->getOpcode();
152   switch (Opc) {
153     case Hexagon::S4_storeirb_io:
154     case Hexagon::S4_storeirh_io:
155     case Hexagon::S4_storeiri_io:
156       // Base address must be a register. (Implement FI later.)
157       return MI->getOperand(0).isReg();
158     default:
159       return false;
160   }
161 }
162 
163 
164 // Check if the machine memory operand MMO is aliased with any of the
165 // stores in the store group Stores.
166 bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
167       const MachineMemOperand &MMO) {
168   if (!MMO.getValue())
169     return true;
170 
171   MemoryLocation L(MMO.getValue(), MMO.getSize(), MMO.getAAInfo());
172 
173   for (auto SI : Stores) {
174     const MachineMemOperand &SMO = getStoreTarget(SI);
175     if (!SMO.getValue())
176       return true;
177 
178     MemoryLocation SL(SMO.getValue(), SMO.getSize(), SMO.getAAInfo());
179     if (AA->alias(L, SL))
180       return true;
181   }
182 
183   return false;
184 }
185 
186 
187 // Check if the machine instruction MI accesses any storage aliased with
188 // any store in the group Stores.
189 bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
190       const MachineInstr *MI) {
191   for (auto &I : MI->memoperands())
192     if (instrAliased(Stores, *I))
193       return true;
194   return false;
195 }
196 
197 
198 // Inspect a machine basic block, and generate store groups out of stores
199 // encountered in the block.
200 //
201 // A store group is a group of stores that use the same base register,
202 // and which can be reordered within that group without altering the
203 // semantics of the program.  A single store group could be widened as
204 // a whole, if there existed a single store instruction with the same
205 // semantics as the entire group.  In many cases, a single store group
206 // may need more than one wide store.
207 void HexagonStoreWidening::createStoreGroups(MachineBasicBlock &MBB,
208       InstrGroupList &StoreGroups) {
209   InstrGroup AllInsns;
210 
211   // Copy all instruction pointers from the basic block to a temporary
212   // list.  This will allow operating on the list, and modifying its
213   // elements without affecting the basic block.
214   for (auto &I : MBB)
215     AllInsns.push_back(&I);
216 
217   // Traverse all instructions in the AllInsns list, and if we encounter
218   // a store, then try to create a store group starting at that instruction
219   // i.e. a sequence of independent stores that can be widened.
220   for (auto I = AllInsns.begin(), E = AllInsns.end(); I != E; ++I) {
221     MachineInstr *MI = *I;
222     // Skip null pointers (processed instructions).
223     if (!MI || !handledStoreType(MI))
224       continue;
225 
226     // Found a store.  Try to create a store group.
227     InstrGroup G;
228     createStoreGroup(MI, I+1, E, G);
229     if (G.size() > 1)
230       StoreGroups.push_back(G);
231   }
232 }
233 
234 
235 // Create a single store group.  The stores need to be independent between
236 // themselves, and also there cannot be other instructions between them
237 // that could read or modify storage being stored into.
238 void HexagonStoreWidening::createStoreGroup(MachineInstr *BaseStore,
239       InstrGroup::iterator Begin, InstrGroup::iterator End, InstrGroup &Group) {
240   assert(handledStoreType(BaseStore) && "Unexpected instruction");
241   unsigned BaseReg = getBaseAddressRegister(BaseStore);
242   InstrGroup Other;
243 
244   Group.push_back(BaseStore);
245 
246   for (auto I = Begin; I != End; ++I) {
247     MachineInstr *MI = *I;
248     if (!MI)
249       continue;
250 
251     if (handledStoreType(MI)) {
252       // If this store instruction is aliased with anything already in the
253       // group, terminate the group now.
254       if (instrAliased(Group, getStoreTarget(MI)))
255         return;
256       // If this store is aliased to any of the memory instructions we have
257       // seen so far (that are not a part of this group), terminate the group.
258       if (instrAliased(Other, getStoreTarget(MI)))
259         return;
260 
261       unsigned BR = getBaseAddressRegister(MI);
262       if (BR == BaseReg) {
263         Group.push_back(MI);
264         *I = 0;
265         continue;
266       }
267     }
268 
269     // Assume calls are aliased to everything.
270     if (MI->isCall() || MI->hasUnmodeledSideEffects())
271       return;
272 
273     if (MI->mayLoad() || MI->mayStore()) {
274       if (MI->hasOrderedMemoryRef() || instrAliased(Group, MI))
275         return;
276       Other.push_back(MI);
277     }
278   } // for
279 }
280 
281 
282 // Check if store instructions S1 and S2 are adjacent.  More precisely,
283 // S2 has to access memory immediately following that accessed by S1.
284 bool HexagonStoreWidening::storesAreAdjacent(const MachineInstr *S1,
285       const MachineInstr *S2) {
286   if (!handledStoreType(S1) || !handledStoreType(S2))
287     return false;
288 
289   const MachineMemOperand &S1MO = getStoreTarget(S1);
290 
291   // Currently only handling immediate stores.
292   int Off1 = S1->getOperand(1).getImm();
293   int Off2 = S2->getOperand(1).getImm();
294 
295   return (Off1 >= 0) ? Off1+S1MO.getSize() == unsigned(Off2)
296                      : int(Off1+S1MO.getSize()) == Off2;
297 }
298 
299 
300 /// Given a sequence of adjacent stores, and a maximum size of a single wide
301 /// store, pick a group of stores that  can be replaced by a single store
302 /// of size not exceeding MaxSize.  The selected sequence will be recorded
303 /// in OG ("old group" of instructions).
304 /// OG should be empty on entry, and should be left empty if the function
305 /// fails.
306 bool HexagonStoreWidening::selectStores(InstrGroup::iterator Begin,
307       InstrGroup::iterator End, InstrGroup &OG, unsigned &TotalSize,
308       unsigned MaxSize) {
309   assert(Begin != End && "No instructions to analyze");
310   assert(OG.empty() && "Old group not empty on entry");
311 
312   if (std::distance(Begin, End) <= 1)
313     return false;
314 
315   MachineInstr *FirstMI = *Begin;
316   assert(!FirstMI->memoperands_empty() && "Expecting some memory operands");
317   const MachineMemOperand &FirstMMO = getStoreTarget(FirstMI);
318   unsigned Alignment = FirstMMO.getAlignment();
319   unsigned SizeAccum = FirstMMO.getSize();
320   unsigned FirstOffset = getStoreOffset(FirstMI);
321 
322   // The initial value of SizeAccum should always be a power of 2.
323   assert(isPowerOf2_32(SizeAccum) && "First store size not a power of 2");
324 
325   // If the size of the first store equals to or exceeds the limit, do nothing.
326   if (SizeAccum >= MaxSize)
327     return false;
328 
329   // If the size of the first store is greater than or equal to the address
330   // stored to, then the store cannot be made any wider.
331   if (SizeAccum >= Alignment)
332     return false;
333 
334   // The offset of a store will put restrictions on how wide the store can be.
335   // Offsets in stores of size 2^n bytes need to have the n lowest bits be 0.
336   // If the first store already exhausts the offset limits, quit.  Test this
337   // by checking if the next wider size would exceed the limit.
338   if ((2*SizeAccum-1) & FirstOffset)
339     return false;
340 
341   OG.push_back(FirstMI);
342   MachineInstr *S1 = FirstMI, *S2 = *(Begin+1);
343   InstrGroup::iterator I = Begin+1;
344 
345   // Pow2Num will be the largest number of elements in OG such that the sum
346   // of sizes of stores 0...Pow2Num-1 will be a power of 2.
347   unsigned Pow2Num = 1;
348   unsigned Pow2Size = SizeAccum;
349 
350   // Be greedy: keep accumulating stores as long as they are to adjacent
351   // memory locations, and as long as the total number of bytes stored
352   // does not exceed the limit (MaxSize).
353   // Keep track of when the total size covered is a power of 2, since
354   // this is a size a single store can cover.
355   while (I != End) {
356     S2 = *I;
357     // Stores are sorted, so if S1 and S2 are not adjacent, there won't be
358     // any other store to fill the "hole".
359     if (!storesAreAdjacent(S1, S2))
360       break;
361 
362     unsigned S2Size = getStoreTarget(S2).getSize();
363     if (SizeAccum + S2Size > std::min(MaxSize, Alignment))
364       break;
365 
366     OG.push_back(S2);
367     SizeAccum += S2Size;
368     if (isPowerOf2_32(SizeAccum)) {
369       Pow2Num = OG.size();
370       Pow2Size = SizeAccum;
371     }
372     if ((2*Pow2Size-1) & FirstOffset)
373       break;
374 
375     S1 = S2;
376     ++I;
377   }
378 
379   // The stores don't add up to anything that can be widened.  Clean up.
380   if (Pow2Num <= 1) {
381     OG.clear();
382     return false;
383   }
384 
385   // Only leave the stored being widened.
386   OG.resize(Pow2Num);
387   TotalSize = Pow2Size;
388   return true;
389 }
390 
391 
392 /// Given an "old group" OG of stores, create a "new group" NG of instructions
393 /// to replace them.  Ideally, NG would only have a single instruction in it,
394 /// but that may only be possible for store-immediate.
395 bool HexagonStoreWidening::createWideStores(InstrGroup &OG, InstrGroup &NG,
396       unsigned TotalSize) {
397   // XXX Current limitations:
398   // - only expect stores of immediate values in OG,
399   // - only handle a TotalSize of up to 4.
400 
401   if (TotalSize > 4)
402     return false;
403 
404   unsigned Acc = 0;  // Value accumulator.
405   unsigned Shift = 0;
406 
407   for (InstrGroup::iterator I = OG.begin(), E = OG.end(); I != E; ++I) {
408     MachineInstr *MI = *I;
409     const MachineMemOperand &MMO = getStoreTarget(MI);
410     MachineOperand &SO = MI->getOperand(2);  // Source.
411     assert(SO.isImm() && "Expecting an immediate operand");
412 
413     unsigned NBits = MMO.getSize()*8;
414     unsigned Mask = (0xFFFFFFFFU >> (32-NBits));
415     unsigned Val = (SO.getImm() & Mask) << Shift;
416     Acc |= Val;
417     Shift += NBits;
418   }
419 
420 
421   MachineInstr *FirstSt = OG.front();
422   DebugLoc DL = OG.back()->getDebugLoc();
423   const MachineMemOperand &OldM = getStoreTarget(FirstSt);
424   MachineMemOperand *NewM =
425     MF->getMachineMemOperand(OldM.getPointerInfo(), OldM.getFlags(),
426                              TotalSize, OldM.getAlignment(),
427                              OldM.getAAInfo());
428 
429   if (Acc < 0x10000) {
430     // Create mem[hw] = #Acc
431     unsigned WOpc = (TotalSize == 2) ? Hexagon::S4_storeirh_io :
432                     (TotalSize == 4) ? Hexagon::S4_storeiri_io : 0;
433     assert(WOpc && "Unexpected size");
434 
435     int Val = (TotalSize == 2) ? int16_t(Acc) : int(Acc);
436     const MCInstrDesc &StD = TII->get(WOpc);
437     MachineOperand &MR = FirstSt->getOperand(0);
438     int64_t Off = FirstSt->getOperand(1).getImm();
439     MachineInstr *StI = BuildMI(*MF, DL, StD)
440                           .addReg(MR.getReg(), getKillRegState(MR.isKill()))
441                           .addImm(Off)
442                           .addImm(Val);
443     StI->addMemOperand(*MF, NewM);
444     NG.push_back(StI);
445   } else {
446     // Create vreg = A2_tfrsi #Acc; mem[hw] = vreg
447     const MCInstrDesc &TfrD = TII->get(Hexagon::A2_tfrsi);
448     const TargetRegisterClass *RC = TII->getRegClass(TfrD, 0, TRI, *MF);
449     unsigned VReg = MF->getRegInfo().createVirtualRegister(RC);
450     MachineInstr *TfrI = BuildMI(*MF, DL, TfrD, VReg)
451                            .addImm(int(Acc));
452     NG.push_back(TfrI);
453 
454     unsigned WOpc = (TotalSize == 2) ? Hexagon::S2_storerh_io :
455                     (TotalSize == 4) ? Hexagon::S2_storeri_io : 0;
456     assert(WOpc && "Unexpected size");
457 
458     const MCInstrDesc &StD = TII->get(WOpc);
459     MachineOperand &MR = FirstSt->getOperand(0);
460     int64_t Off = FirstSt->getOperand(1).getImm();
461     MachineInstr *StI = BuildMI(*MF, DL, StD)
462                           .addReg(MR.getReg(), getKillRegState(MR.isKill()))
463                           .addImm(Off)
464                           .addReg(VReg, RegState::Kill);
465     StI->addMemOperand(*MF, NewM);
466     NG.push_back(StI);
467   }
468 
469   return true;
470 }
471 
472 
473 // Replace instructions from the old group OG with instructions from the
474 // new group NG.  Conceptually, remove all instructions in OG, and then
475 // insert all instructions in NG, starting at where the first instruction
476 // from OG was (in the order in which they appeared in the basic block).
477 // (The ordering in OG does not have to match the order in the basic block.)
478 bool HexagonStoreWidening::replaceStores(InstrGroup &OG, InstrGroup &NG) {
479   DEBUG({
480     dbgs() << "Replacing:\n";
481     for (auto I : OG)
482       dbgs() << "  " << *I;
483     dbgs() << "with\n";
484     for (auto I : NG)
485       dbgs() << "  " << *I;
486   });
487 
488   MachineBasicBlock *MBB = OG.back()->getParent();
489   MachineBasicBlock::iterator InsertAt = MBB->end();
490 
491   // Need to establish the insertion point.  The best one is right before
492   // the first store in the OG, but in the order in which the stores occur
493   // in the program list.  Since the ordering in OG does not correspond
494   // to the order in the program list, we need to do some work to find
495   // the insertion point.
496 
497   // Create a set of all instructions in OG (for quick lookup).
498   SmallPtrSet<MachineInstr*, 4> InstrSet;
499   for (auto I : OG)
500     InstrSet.insert(I);
501 
502   // Traverse the block, until we hit an instruction from OG.
503   for (auto &I : *MBB) {
504     if (InstrSet.count(&I)) {
505       InsertAt = I;
506       break;
507     }
508   }
509 
510   assert((InsertAt != MBB->end()) && "Cannot locate any store from the group");
511 
512   bool AtBBStart = false;
513 
514   // InsertAt points at the first instruction that will be removed.  We need
515   // to move it out of the way, so it remains valid after removing all the
516   // old stores, and so we are able to recover it back to the proper insertion
517   // position.
518   if (InsertAt != MBB->begin())
519     --InsertAt;
520   else
521     AtBBStart = true;
522 
523   for (auto I : OG)
524     I->eraseFromParent();
525 
526   if (!AtBBStart)
527     ++InsertAt;
528   else
529     InsertAt = MBB->begin();
530 
531   for (auto I : NG)
532     MBB->insert(InsertAt, I);
533 
534   return true;
535 }
536 
537 
538 // Break up the group into smaller groups, each of which can be replaced by
539 // a single wide store.  Widen each such smaller group and replace the old
540 // instructions with the widened ones.
541 bool HexagonStoreWidening::processStoreGroup(InstrGroup &Group) {
542   bool Changed = false;
543   InstrGroup::iterator I = Group.begin(), E = Group.end();
544   InstrGroup OG, NG;   // Old and new groups.
545   unsigned CollectedSize;
546 
547   while (I != E) {
548     OG.clear();
549     NG.clear();
550 
551     bool Succ = selectStores(I++, E, OG, CollectedSize, MaxWideSize) &&
552                 createWideStores(OG, NG, CollectedSize)              &&
553                 replaceStores(OG, NG);
554     if (!Succ)
555       continue;
556 
557     assert(OG.size() > 1 && "Created invalid group");
558     assert(distance(I, E)+1 >= int(OG.size()) && "Too many elements");
559     I += OG.size()-1;
560 
561     Changed = true;
562   }
563 
564   return Changed;
565 }
566 
567 
568 // Process a single basic block: create the store groups, and replace them
569 // with the widened stores, if possible.  Processing of each basic block
570 // is independent from processing of any other basic block.  This transfor-
571 // mation could be stopped after having processed any basic block without
572 // any ill effects (other than not having performed widening in the unpro-
573 // cessed blocks).  Also, the basic blocks can be processed in any order.
574 bool HexagonStoreWidening::processBasicBlock(MachineBasicBlock &MBB) {
575   InstrGroupList SGs;
576   bool Changed = false;
577 
578   createStoreGroups(MBB, SGs);
579 
580   auto Less = [] (const MachineInstr *A, const MachineInstr *B) -> bool {
581     return getStoreOffset(A) < getStoreOffset(B);
582   };
583   for (auto &G : SGs) {
584     assert(G.size() > 1 && "Store group with fewer than 2 elements");
585     std::sort(G.begin(), G.end(), Less);
586 
587     Changed |= processStoreGroup(G);
588   }
589 
590   return Changed;
591 }
592 
593 
594 bool HexagonStoreWidening::runOnMachineFunction(MachineFunction &MFn) {
595   if (skipFunction(*MFn.getFunction()))
596     return false;
597 
598   MF = &MFn;
599   auto &ST = MFn.getSubtarget<HexagonSubtarget>();
600   TII = ST.getInstrInfo();
601   TRI = ST.getRegisterInfo();
602   MRI = &MFn.getRegInfo();
603   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
604 
605   bool Changed = false;
606 
607   for (auto &B : MFn)
608     Changed |= processBasicBlock(B);
609 
610   return Changed;
611 }
612 
613 
614 FunctionPass *llvm::createHexagonStoreWidening() {
615   return new HexagonStoreWidening();
616 }
617 
618