blob: 8c936d996cbeb8bb95c0d7a2008dd7f61df6451b [file] [log] [blame]
Krzysztof Parzyszek8b26fbf2015-07-09 15:40:25 +00001//===--- HexagonExpandCondsets.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
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000010// Replace mux instructions with the corresponding legal instructions.
11// It is meant to work post-SSA, but still on virtual registers. It was
12// originally placed between register coalescing and machine instruction
13// scheduler.
14// In this place in the optimization sequence, live interval analysis had
15// been performed, and the live intervals should be preserved. A large part
16// of the code deals with preserving the liveness information.
17//
18// Liveness tracking aside, the main functionality of this pass is divided
19// into two steps. The first step is to replace an instruction
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +000020// vreg0 = C2_mux vreg1, vreg2, vreg3
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000021// with a pair of conditional transfers
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +000022// vreg0 = A2_tfrt vreg1, vreg2
23// vreg0 = A2_tfrf vreg1, vreg3
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000024// It is the intention that the execution of this pass could be terminated
25// after this step, and the code generated would be functionally correct.
26//
27// If the uses of the source values vreg1 and vreg2 are kills, and their
28// definitions are predicable, then in the second step, the conditional
29// transfers will then be rewritten as predicated instructions. E.g.
30// vreg0 = A2_or vreg1, vreg2
31// vreg3 = A2_tfrt vreg99, vreg0<kill>
32// will be rewritten as
33// vreg3 = A2_port vreg99, vreg1, vreg2
34//
35// This replacement has two variants: "up" and "down". Consider this case:
36// vreg0 = A2_or vreg1, vreg2
37// ... [intervening instructions] ...
38// vreg3 = A2_tfrt vreg99, vreg0<kill>
39// variant "up":
40// vreg3 = A2_port vreg99, vreg1, vreg2
41// ... [intervening instructions, vreg0->vreg3] ...
42// [deleted]
43// variant "down":
44// [deleted]
45// ... [intervening instructions] ...
46// vreg3 = A2_port vreg99, vreg1, vreg2
47//
48// Both, one or none of these variants may be valid, and checks are made
49// to rule out inapplicable variants.
50//
51// As an additional optimization, before either of the two steps above is
52// executed, the pass attempts to coalesce the target register with one of
53// the source registers, e.g. given an instruction
54// vreg3 = C2_mux vreg0, vreg1, vreg2
55// vreg3 will be coalesced with either vreg1 or vreg2. If this succeeds,
56// the instruction would then be (for example)
57// vreg3 = C2_mux vreg0, vreg3, vreg2
58// and, under certain circumstances, this could result in only one predicated
59// instruction:
60// vreg3 = A2_tfrf vreg0, vreg2
61//
62
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +000063// Splitting a definition of a register into two predicated transfers
64// creates a complication in liveness tracking. Live interval computation
65// will see both instructions as actual definitions, and will mark the
66// first one as dead. The definition is not actually dead, and this
67// situation will need to be fixed. For example:
68// vreg1<def,dead> = A2_tfrt ... ; marked as dead
69// vreg1<def> = A2_tfrf ...
70//
71// Since any of the individual predicated transfers may end up getting
72// removed (in case it is an identity copy), some pre-existing def may
73// be marked as dead after live interval recomputation:
74// vreg1<def,dead> = ... ; marked as dead
75// ...
76// vreg1<def> = A2_tfrf ... ; if A2_tfrt is removed
77// This case happens if vreg1 was used as a source in A2_tfrt, which means
78// that is it actually live at the A2_tfrf, and so the now dead definition
79// of vreg1 will need to be updated to non-dead at some point.
80//
81// This issue could be remedied by adding implicit uses to the predicated
82// transfers, but this will create a problem with subsequent predication,
83// since the transfers will no longer be possible to reorder. To avoid
84// that, the initial splitting will not add any implicit uses. These
85// implicit uses will be added later, after predication. The extra price,
86// however, is that finding the locations where the implicit uses need
87// to be added, and updating the live ranges will be more involved.
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +000088
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +000089#define DEBUG_TYPE "expand-condsets"
90
91#include "HexagonTargetMachine.h"
92#include "llvm/ADT/SetVector.h"
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000093#include "llvm/CodeGen/Passes.h"
94#include "llvm/CodeGen/LiveInterval.h"
95#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +000096#include "llvm/CodeGen/MachineDominators.h"
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000097#include "llvm/CodeGen/MachineFunction.h"
98#include "llvm/CodeGen/MachineInstrBuilder.h"
99#include "llvm/CodeGen/MachineRegisterInfo.h"
100#include "llvm/Target/TargetInstrInfo.h"
101#include "llvm/Target/TargetMachine.h"
102#include "llvm/Target/TargetRegisterInfo.h"
103#include "llvm/Support/CommandLine.h"
104#include "llvm/Support/Debug.h"
105#include "llvm/Support/raw_ostream.h"
106
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000107#include <algorithm>
108#include <iterator>
109#include <set>
110#include <utility>
111
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000112using namespace llvm;
113
114static cl::opt<unsigned> OptTfrLimit("expand-condsets-tfr-limit",
115 cl::init(~0U), cl::Hidden, cl::desc("Max number of mux expansions"));
116static cl::opt<unsigned> OptCoaLimit("expand-condsets-coa-limit",
117 cl::init(~0U), cl::Hidden, cl::desc("Max number of segment coalescings"));
118
119namespace llvm {
120 void initializeHexagonExpandCondsetsPass(PassRegistry&);
121 FunctionPass *createHexagonExpandCondsets();
122}
123
124namespace {
125 class HexagonExpandCondsets : public MachineFunctionPass {
126 public:
127 static char ID;
128 HexagonExpandCondsets() :
129 MachineFunctionPass(ID), HII(0), TRI(0), MRI(0),
130 LIS(0), CoaLimitActive(false),
131 TfrLimitActive(false), CoaCounter(0), TfrCounter(0) {
132 if (OptCoaLimit.getPosition())
133 CoaLimitActive = true, CoaLimit = OptCoaLimit;
134 if (OptTfrLimit.getPosition())
135 TfrLimitActive = true, TfrLimit = OptTfrLimit;
136 initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
137 }
138
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000139 const char *getPassName() const override {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000140 return "Hexagon Expand Condsets";
141 }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000142 void getAnalysisUsage(AnalysisUsage &AU) const override {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000143 AU.addRequired<LiveIntervals>();
144 AU.addPreserved<LiveIntervals>();
145 AU.addPreserved<SlotIndexes>();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000146 AU.addRequired<MachineDominatorTree>();
147 AU.addPreserved<MachineDominatorTree>();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000148 MachineFunctionPass::getAnalysisUsage(AU);
149 }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000150 bool runOnMachineFunction(MachineFunction &MF) override;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000151
152 private:
153 const HexagonInstrInfo *HII;
154 const TargetRegisterInfo *TRI;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000155 MachineDominatorTree *MDT;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000156 MachineRegisterInfo *MRI;
157 LiveIntervals *LIS;
158
159 bool CoaLimitActive, TfrLimitActive;
160 unsigned CoaLimit, TfrLimit, CoaCounter, TfrCounter;
161
162 struct RegisterRef {
163 RegisterRef(const MachineOperand &Op) : Reg(Op.getReg()),
164 Sub(Op.getSubReg()) {}
165 RegisterRef(unsigned R = 0, unsigned S = 0) : Reg(R), Sub(S) {}
166 bool operator== (RegisterRef RR) const {
167 return Reg == RR.Reg && Sub == RR.Sub;
168 }
169 bool operator!= (RegisterRef RR) const { return !operator==(RR); }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000170 bool operator< (RegisterRef RR) const {
171 return Reg < RR.Reg || (Reg == RR.Reg && Sub < RR.Sub);
172 }
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000173 unsigned Reg, Sub;
174 };
175
176 typedef DenseMap<unsigned,unsigned> ReferenceMap;
177 enum { Sub_Low = 0x1, Sub_High = 0x2, Sub_None = (Sub_Low | Sub_High) };
178 enum { Exec_Then = 0x10, Exec_Else = 0x20 };
179 unsigned getMaskForSub(unsigned Sub);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000180 bool isCondset(const MachineInstr &MI);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000181 LaneBitmask getLaneMask(unsigned Reg, unsigned Sub);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000182
183 void addRefToMap(RegisterRef RR, ReferenceMap &Map, unsigned Exec);
184 bool isRefInMap(RegisterRef, ReferenceMap &Map, unsigned Exec);
185
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000186 void updateDeadsInRange(unsigned Reg, LaneBitmask LM, LiveRange &Range);
187 void updateKillFlags(unsigned Reg);
188 void updateDeadFlags(unsigned Reg);
189 void recalculateLiveInterval(unsigned Reg);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000190 void removeInstr(MachineInstr &MI);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000191 void updateLiveness(std::set<unsigned> &RegSet, bool Recalc,
192 bool UpdateKills, bool UpdateDeads);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000193
194 unsigned getCondTfrOpcode(const MachineOperand &SO, bool Cond);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000195 MachineInstr *genCondTfrFor(MachineOperand &SrcOp,
196 MachineBasicBlock::iterator At, unsigned DstR,
197 unsigned DstSR, const MachineOperand &PredOp, bool PredSense,
198 bool ReadUndef, bool ImpUse);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000199 bool split(MachineInstr &MI, std::set<unsigned> &UpdRegs);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000200 bool splitInBlock(MachineBasicBlock &B, std::set<unsigned> &UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000201
202 bool isPredicable(MachineInstr *MI);
203 MachineInstr *getReachingDefForPred(RegisterRef RD,
204 MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000205 bool canMoveOver(MachineInstr &MI, ReferenceMap &Defs, ReferenceMap &Uses);
206 bool canMoveMemTo(MachineInstr &MI, MachineInstr &ToI, bool IsDown);
207 void predicateAt(const MachineOperand &DefOp, MachineInstr &MI,
208 MachineBasicBlock::iterator Where,
209 const MachineOperand &PredOp, bool Cond,
210 std::set<unsigned> &UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000211 void renameInRange(RegisterRef RO, RegisterRef RN, unsigned PredR,
212 bool Cond, MachineBasicBlock::iterator First,
213 MachineBasicBlock::iterator Last);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000214 bool predicate(MachineInstr &TfrI, bool Cond, std::set<unsigned> &UpdRegs);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000215 bool predicateInBlock(MachineBasicBlock &B,
216 std::set<unsigned> &UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000217
218 bool isIntReg(RegisterRef RR, unsigned &BW);
219 bool isIntraBlocks(LiveInterval &LI);
220 bool coalesceRegisters(RegisterRef R1, RegisterRef R2);
221 bool coalesceSegments(MachineFunction &MF);
222 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000223}
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000224
225char HexagonExpandCondsets::ID = 0;
226
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000227namespace llvm {
228 char &HexagonExpandCondsetsID = HexagonExpandCondsets::ID;
229}
230
Krzysztof Parzyszek764fed92016-05-27 21:15:34 +0000231INITIALIZE_PASS_BEGIN(HexagonExpandCondsets, "expand-condsets",
232 "Hexagon Expand Condsets", false, false)
233INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
234INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
235INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
236INITIALIZE_PASS_END(HexagonExpandCondsets, "expand-condsets",
237 "Hexagon Expand Condsets", false, false)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000238
239unsigned HexagonExpandCondsets::getMaskForSub(unsigned Sub) {
240 switch (Sub) {
241 case Hexagon::subreg_loreg:
242 return Sub_Low;
243 case Hexagon::subreg_hireg:
244 return Sub_High;
245 case Hexagon::NoSubRegister:
246 return Sub_None;
247 }
248 llvm_unreachable("Invalid subregister");
249}
250
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000251bool HexagonExpandCondsets::isCondset(const MachineInstr &MI) {
252 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000253 switch (Opc) {
254 case Hexagon::C2_mux:
255 case Hexagon::C2_muxii:
256 case Hexagon::C2_muxir:
257 case Hexagon::C2_muxri:
Krzysztof Parzyszek258af192016-08-11 19:12:18 +0000258 case Hexagon::PS_pselect:
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000259 return true;
260 break;
261 }
262 return false;
263}
264
265
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000266LaneBitmask HexagonExpandCondsets::getLaneMask(unsigned Reg, unsigned Sub) {
267 assert(TargetRegisterInfo::isVirtualRegister(Reg));
268 return Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
269 : MRI->getMaxLaneMaskForVReg(Reg);
270}
271
272
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000273void HexagonExpandCondsets::addRefToMap(RegisterRef RR, ReferenceMap &Map,
274 unsigned Exec) {
275 unsigned Mask = getMaskForSub(RR.Sub) | Exec;
276 ReferenceMap::iterator F = Map.find(RR.Reg);
277 if (F == Map.end())
278 Map.insert(std::make_pair(RR.Reg, Mask));
279 else
280 F->second |= Mask;
281}
282
283
284bool HexagonExpandCondsets::isRefInMap(RegisterRef RR, ReferenceMap &Map,
285 unsigned Exec) {
286 ReferenceMap::iterator F = Map.find(RR.Reg);
287 if (F == Map.end())
288 return false;
289 unsigned Mask = getMaskForSub(RR.Sub) | Exec;
290 if (Mask & F->second)
291 return true;
292 return false;
293}
294
295
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000296void HexagonExpandCondsets::updateKillFlags(unsigned Reg) {
297 auto KillAt = [this,Reg] (SlotIndex K, LaneBitmask LM) -> void {
298 // Set the <kill> flag on a use of Reg whose lane mask is contained in LM.
299 MachineInstr *MI = LIS->getInstructionFromIndex(K);
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000300 for (auto &Op : MI->operands()) {
Krzysztof Parzyszek1bba8962016-07-01 20:45:19 +0000301 if (!Op.isReg() || !Op.isUse() || Op.getReg() != Reg)
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000302 continue;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000303 LaneBitmask SLM = getLaneMask(Reg, Op.getSubReg());
304 if ((SLM & LM) == SLM) {
305 // Only set the kill flag on the first encountered use of Reg in this
306 // instruction.
307 Op.setIsKill(true);
308 break;
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000309 }
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000310 }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000311 };
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000312
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000313 LiveInterval &LI = LIS->getInterval(Reg);
314 for (auto I = LI.begin(), E = LI.end(); I != E; ++I) {
315 if (!I->end.isRegister())
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000316 continue;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000317 // Do not mark the end of the segment as <kill>, if the next segment
318 // starts with a predicated instruction.
319 auto NextI = std::next(I);
320 if (NextI != E && NextI->start.isRegister()) {
321 MachineInstr *DefI = LIS->getInstructionFromIndex(NextI->start);
322 if (HII->isPredicated(*DefI))
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000323 continue;
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000324 }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000325 bool WholeReg = true;
326 if (LI.hasSubRanges()) {
327 auto EndsAtI = [I] (LiveInterval::SubRange &S) -> bool {
328 LiveRange::iterator F = S.find(I->end);
329 return F != S.end() && I->end == F->end;
330 };
331 // Check if all subranges end at I->end. If so, make sure to kill
332 // the whole register.
333 for (LiveInterval::SubRange &S : LI.subranges()) {
334 if (EndsAtI(S))
335 KillAt(I->end, S.LaneMask);
336 else
337 WholeReg = false;
338 }
339 }
340 if (WholeReg)
341 KillAt(I->end, MRI->getMaxLaneMaskForVReg(Reg));
342 }
343}
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000344
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000345
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000346void HexagonExpandCondsets::updateDeadsInRange(unsigned Reg, LaneBitmask LM,
347 LiveRange &Range) {
348 assert(TargetRegisterInfo::isVirtualRegister(Reg));
349 if (Range.empty())
350 return;
351
352 auto IsRegDef = [this,Reg,LM] (MachineOperand &Op) -> bool {
353 if (!Op.isReg() || !Op.isDef())
354 return false;
355 unsigned DR = Op.getReg(), DSR = Op.getSubReg();
356 if (!TargetRegisterInfo::isVirtualRegister(DR) || DR != Reg)
357 return false;
358 LaneBitmask SLM = getLaneMask(DR, DSR);
359 return (SLM & LM) != 0;
360 };
361
362 // The splitting step will create pairs of predicated definitions without
363 // any implicit uses (since implicit uses would interfere with predication).
364 // This can cause the reaching defs to become dead after live range
365 // recomputation, even though they are not really dead.
366 // We need to identify predicated defs that need implicit uses, and
367 // dead defs that are not really dead, and correct both problems.
368
369 SetVector<MachineBasicBlock*> Defs;
370 auto Dominate = [this] (SetVector<MachineBasicBlock*> &Defs,
371 MachineBasicBlock *Dest) -> bool {
372 for (MachineBasicBlock *D : Defs)
373 if (D != Dest && MDT->dominates(D, Dest))
374 return true;
375
376 MachineBasicBlock *Entry = &Dest->getParent()->front();
377 SetVector<MachineBasicBlock*> Work(Dest->pred_begin(), Dest->pred_end());
378 for (unsigned i = 0; i < Work.size(); ++i) {
379 MachineBasicBlock *B = Work[i];
380 if (Defs.count(B))
381 continue;
382 if (B == Entry)
383 return false;
384 for (auto *P : B->predecessors())
385 Work.insert(P);
386 }
387 return true;
388 };
389
390 // First, try to extend live range within individual basic blocks. This
391 // will leave us only with dead defs that do not reach any predicated
392 // defs in the same block.
393 SmallVector<SlotIndex,4> PredDefs;
394 for (auto &Seg : Range) {
395 if (!Seg.start.isRegister())
396 continue;
397 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000398 Defs.insert(DefI->getParent());
399 if (HII->isPredicated(*DefI))
400 PredDefs.push_back(Seg.start);
401 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000402
403 SmallVector<SlotIndex,8> Undefs;
404 LiveInterval &LI = LIS->getInterval(Reg);
405 LI.computeSubRangeUndefs(Undefs, LM, *MRI, *LIS->getSlotIndexes());
406
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000407 for (auto &SI : PredDefs) {
408 MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000409 auto P = Range.extendInBlock(Undefs, LIS->getMBBStartIdx(BB), SI);
410 if (P.first != nullptr || P.second)
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000411 SI = SlotIndex();
412 }
413
414 // Calculate reachability for those predicated defs that were not handled
415 // by the in-block extension.
416 SmallVector<SlotIndex,4> ExtTo;
417 for (auto &SI : PredDefs) {
418 if (!SI.isValid())
419 continue;
420 MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
421 if (BB->pred_empty())
422 continue;
423 // If the defs from this range reach SI via all predecessors, it is live.
424 if (Dominate(Defs, BB))
425 ExtTo.push_back(SI);
426 }
Krzysztof Parzyszek07d9f532016-09-01 13:59:35 +0000427 LIS->extendToIndices(Range, ExtTo, Undefs);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000428
429 // Remove <dead> flags from all defs that are not dead after live range
430 // extension, and collect all def operands. They will be used to generate
431 // the necessary implicit uses.
432 std::set<RegisterRef> DefRegs;
433 for (auto &Seg : Range) {
434 if (!Seg.start.isRegister())
435 continue;
436 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000437 for (auto &Op : DefI->operands()) {
438 if (Seg.start.isDead() || !IsRegDef(Op))
439 continue;
440 DefRegs.insert(Op);
441 Op.setIsDead(false);
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000442 }
443 }
444
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000445 // Finally, add implicit uses to each predicated def that is reached
Krzysztof Parzyszekcbd559f2016-08-24 16:36:37 +0000446 // by other defs.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000447 for (auto &Seg : Range) {
448 if (!Seg.start.isRegister() || !Range.liveAt(Seg.start.getPrevSlot()))
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000449 continue;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000450 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
451 if (!HII->isPredicated(*DefI))
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000452 continue;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000453 MachineFunction &MF = *DefI->getParent()->getParent();
454 // Construct the set of all necessary implicit uses, based on the def
455 // operands in the instruction.
456 std::set<RegisterRef> ImpUses;
457 for (auto &Op : DefI->operands())
458 if (Op.isReg() && Op.isDef() && DefRegs.count(Op))
459 ImpUses.insert(Op);
460 for (RegisterRef R : ImpUses)
461 MachineInstrBuilder(MF, DefI).addReg(R.Reg, RegState::Implicit, R.Sub);
Krzysztof Parzyszek8b617592016-06-07 19:25:28 +0000462 }
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000463}
464
465
466void HexagonExpandCondsets::updateDeadFlags(unsigned Reg) {
467 LiveInterval &LI = LIS->getInterval(Reg);
468 if (LI.hasSubRanges()) {
469 for (LiveInterval::SubRange &S : LI.subranges()) {
470 updateDeadsInRange(Reg, S.LaneMask, S);
471 LIS->shrinkToUses(S, Reg);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000472 }
473 LI.clear();
474 LIS->constructMainRangeFromSubranges(LI);
475 } else {
476 updateDeadsInRange(Reg, MRI->getMaxLaneMaskForVReg(Reg), LI);
477 }
478}
479
480
481void HexagonExpandCondsets::recalculateLiveInterval(unsigned Reg) {
482 LIS->removeInterval(Reg);
483 LIS->createAndComputeVirtRegInterval(Reg);
484}
485
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000486void HexagonExpandCondsets::removeInstr(MachineInstr &MI) {
487 LIS->RemoveMachineInstrFromMaps(MI);
488 MI.eraseFromParent();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000489}
490
491
492void HexagonExpandCondsets::updateLiveness(std::set<unsigned> &RegSet,
493 bool Recalc, bool UpdateKills, bool UpdateDeads) {
494 UpdateKills |= UpdateDeads;
495 for (auto R : RegSet) {
496 if (Recalc)
497 recalculateLiveInterval(R);
498 if (UpdateKills)
499 MRI->clearKillFlags(R);
500 if (UpdateDeads)
501 updateDeadFlags(R);
502 // Fixing <dead> flags may extend live ranges, so reset <kill> flags
503 // after that.
504 if (UpdateKills)
505 updateKillFlags(R);
506 LIS->getInterval(R).verify();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000507 }
508}
509
510
511/// Get the opcode for a conditional transfer of the value in SO (source
512/// operand). The condition (true/false) is given in Cond.
513unsigned HexagonExpandCondsets::getCondTfrOpcode(const MachineOperand &SO,
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000514 bool IfTrue) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000515 using namespace Hexagon;
516 if (SO.isReg()) {
517 unsigned PhysR;
518 RegisterRef RS = SO;
519 if (TargetRegisterInfo::isVirtualRegister(RS.Reg)) {
520 const TargetRegisterClass *VC = MRI->getRegClass(RS.Reg);
521 assert(VC->begin() != VC->end() && "Empty register class");
522 PhysR = *VC->begin();
523 } else {
524 assert(TargetRegisterInfo::isPhysicalRegister(RS.Reg));
525 PhysR = RS.Reg;
526 }
527 unsigned PhysS = (RS.Sub == 0) ? PhysR : TRI->getSubReg(PhysR, RS.Sub);
528 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysS);
529 switch (RC->getSize()) {
530 case 4:
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000531 return IfTrue ? A2_tfrt : A2_tfrf;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000532 case 8:
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000533 return IfTrue ? A2_tfrpt : A2_tfrpf;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000534 }
535 llvm_unreachable("Invalid register operand");
536 }
537 if (SO.isImm() || SO.isFPImm())
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000538 return IfTrue ? C2_cmoveit : C2_cmoveif;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000539 llvm_unreachable("Unexpected source operand");
540}
541
542
543/// Generate a conditional transfer, copying the value SrcOp to the
544/// destination register DstR:DstSR, and using the predicate register from
545/// PredOp. The Cond argument specifies whether the predicate is to be
546/// if(PredOp), or if(!PredOp).
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000547MachineInstr *HexagonExpandCondsets::genCondTfrFor(MachineOperand &SrcOp,
548 MachineBasicBlock::iterator At,
549 unsigned DstR, unsigned DstSR, const MachineOperand &PredOp,
550 bool PredSense, bool ReadUndef, bool ImpUse) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000551 MachineInstr *MI = SrcOp.getParent();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000552 MachineBasicBlock &B = *At->getParent();
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +0000553 const DebugLoc &DL = MI->getDebugLoc();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000554
555 // Don't avoid identity copies here (i.e. if the source and the destination
556 // are the same registers). It is actually better to generate them here,
557 // since this would cause the copy to potentially be predicated in the next
558 // step. The predication will remove such a copy if it is unable to
559 /// predicate.
560
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000561 unsigned Opc = getCondTfrOpcode(SrcOp, PredSense);
562 unsigned State = RegState::Define | (ReadUndef ? RegState::Undef : 0);
563 MachineInstrBuilder MIB = BuildMI(B, At, DL, HII->get(Opc))
564 .addReg(DstR, State, DstSR)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000565 .addOperand(PredOp)
566 .addOperand(SrcOp);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000567
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000568 // We don't want any kills yet.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000569 MIB->clearKillInfo();
570 DEBUG(dbgs() << "created an initial copy: " << *MIB);
571 return &*MIB;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000572}
573
574
575/// Replace a MUX instruction MI with a pair A2_tfrt/A2_tfrf. This function
576/// performs all necessary changes to complete the replacement.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000577bool HexagonExpandCondsets::split(MachineInstr &MI,
578 std::set<unsigned> &UpdRegs) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000579 if (TfrLimitActive) {
580 if (TfrCounter >= TfrLimit)
581 return false;
582 TfrCounter++;
583 }
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000584 DEBUG(dbgs() << "\nsplitting BB#" << MI.getParent()->getNumber() << ": "
585 << MI);
586 MachineOperand &MD = MI.getOperand(0); // Definition
587 MachineOperand &MP = MI.getOperand(1); // Predicate register
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000588 assert(MD.isDef());
589 unsigned DR = MD.getReg(), DSR = MD.getSubReg();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000590 bool ReadUndef = MD.isUndef();
591 MachineBasicBlock::iterator At = MI;
592
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000593 // First, create the two invididual conditional transfers, and add each
594 // of them to the live intervals information. Do that first and then remove
595 // the old instruction from live intervals.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000596 MachineInstr *TfrT =
597 genCondTfrFor(MI.getOperand(2), At, DR, DSR, MP, true, ReadUndef, false);
598 MachineInstr *TfrF =
599 genCondTfrFor(MI.getOperand(3), At, DR, DSR, MP, false, ReadUndef, true);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000600 LIS->InsertMachineInstrInMaps(*TfrT);
601 LIS->InsertMachineInstrInMaps(*TfrF);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000602
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000603 // Will need to recalculate live intervals for all registers in MI.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000604 for (auto &Op : MI.operands())
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000605 if (Op.isReg())
606 UpdRegs.insert(Op.getReg());
607
608 removeInstr(MI);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000609 return true;
610}
611
612
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000613/// Split all MUX instructions in the given block into pairs of conditional
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000614/// transfers.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000615bool HexagonExpandCondsets::splitInBlock(MachineBasicBlock &B,
616 std::set<unsigned> &UpdRegs) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000617 bool Changed = false;
618 MachineBasicBlock::iterator I, E, NextI;
619 for (I = B.begin(), E = B.end(); I != E; I = NextI) {
620 NextI = std::next(I);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000621 if (isCondset(*I))
622 Changed |= split(*I, UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000623 }
624 return Changed;
625}
626
627
628bool HexagonExpandCondsets::isPredicable(MachineInstr *MI) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000629 if (HII->isPredicated(*MI) || !HII->isPredicable(*MI))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000630 return false;
631 if (MI->hasUnmodeledSideEffects() || MI->mayStore())
632 return false;
633 // Reject instructions with multiple defs (e.g. post-increment loads).
634 bool HasDef = false;
635 for (auto &Op : MI->operands()) {
636 if (!Op.isReg() || !Op.isDef())
637 continue;
638 if (HasDef)
639 return false;
640 HasDef = true;
641 }
642 for (auto &Mo : MI->memoperands())
643 if (Mo->isVolatile())
644 return false;
645 return true;
646}
647
648
649/// Find the reaching definition for a predicated use of RD. The RD is used
650/// under the conditions given by PredR and Cond, and this function will ignore
651/// definitions that set RD under the opposite conditions.
652MachineInstr *HexagonExpandCondsets::getReachingDefForPred(RegisterRef RD,
653 MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond) {
654 MachineBasicBlock &B = *UseIt->getParent();
655 MachineBasicBlock::iterator I = UseIt, S = B.begin();
656 if (I == S)
657 return 0;
658
659 bool PredValid = true;
660 do {
661 --I;
662 MachineInstr *MI = &*I;
663 // Check if this instruction can be ignored, i.e. if it is predicated
664 // on the complementary condition.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000665 if (PredValid && HII->isPredicated(*MI)) {
666 if (MI->readsRegister(PredR) && (Cond != HII->isPredicatedTrue(*MI)))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000667 continue;
668 }
669
670 // Check the defs. If the PredR is defined, invalidate it. If RD is
671 // defined, return the instruction or 0, depending on the circumstances.
672 for (auto &Op : MI->operands()) {
673 if (!Op.isReg() || !Op.isDef())
674 continue;
675 RegisterRef RR = Op;
676 if (RR.Reg == PredR) {
677 PredValid = false;
678 continue;
679 }
680 if (RR.Reg != RD.Reg)
681 continue;
682 // If the "Reg" part agrees, there is still the subregister to check.
683 // If we are looking for vreg1:loreg, we can skip vreg1:hireg, but
684 // not vreg1 (w/o subregisters).
685 if (RR.Sub == RD.Sub)
686 return MI;
687 if (RR.Sub == 0 || RD.Sub == 0)
688 return 0;
689 // We have different subregisters, so we can continue looking.
690 }
691 } while (I != S);
692
693 return 0;
694}
695
696
697/// Check if the instruction MI can be safely moved over a set of instructions
698/// whose side-effects (in terms of register defs and uses) are expressed in
699/// the maps Defs and Uses. These maps reflect the conditional defs and uses
700/// that depend on the same predicate register to allow moving instructions
701/// over instructions predicated on the opposite condition.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000702bool HexagonExpandCondsets::canMoveOver(MachineInstr &MI, ReferenceMap &Defs,
703 ReferenceMap &Uses) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000704 // In order to be able to safely move MI over instructions that define
705 // "Defs" and use "Uses", no def operand from MI can be defined or used
706 // and no use operand can be defined.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000707 for (auto &Op : MI.operands()) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000708 if (!Op.isReg())
709 continue;
710 RegisterRef RR = Op;
711 // For physical register we would need to check register aliases, etc.
712 // and we don't want to bother with that. It would be of little value
713 // before the actual register rewriting (from virtual to physical).
714 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
715 return false;
716 // No redefs for any operand.
717 if (isRefInMap(RR, Defs, Exec_Then))
718 return false;
719 // For defs, there cannot be uses.
720 if (Op.isDef() && isRefInMap(RR, Uses, Exec_Then))
721 return false;
722 }
723 return true;
724}
725
726
727/// Check if the instruction accessing memory (TheI) can be moved to the
728/// location ToI.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000729bool HexagonExpandCondsets::canMoveMemTo(MachineInstr &TheI, MachineInstr &ToI,
730 bool IsDown) {
731 bool IsLoad = TheI.mayLoad(), IsStore = TheI.mayStore();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000732 if (!IsLoad && !IsStore)
733 return true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000734 if (HII->areMemAccessesTriviallyDisjoint(TheI, ToI))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000735 return true;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000736 if (TheI.hasUnmodeledSideEffects())
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000737 return false;
738
739 MachineBasicBlock::iterator StartI = IsDown ? TheI : ToI;
740 MachineBasicBlock::iterator EndI = IsDown ? ToI : TheI;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000741 bool Ordered = TheI.hasOrderedMemoryRef();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000742
743 // Search for aliased memory reference in (StartI, EndI).
744 for (MachineBasicBlock::iterator I = std::next(StartI); I != EndI; ++I) {
745 MachineInstr *MI = &*I;
746 if (MI->hasUnmodeledSideEffects())
747 return false;
748 bool L = MI->mayLoad(), S = MI->mayStore();
749 if (!L && !S)
750 continue;
751 if (Ordered && MI->hasOrderedMemoryRef())
752 return false;
753
754 bool Conflict = (L && IsStore) || S;
755 if (Conflict)
756 return false;
757 }
758 return true;
759}
760
761
762/// Generate a predicated version of MI (where the condition is given via
763/// PredR and Cond) at the point indicated by Where.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000764void HexagonExpandCondsets::predicateAt(const MachineOperand &DefOp,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000765 MachineInstr &MI,
766 MachineBasicBlock::iterator Where,
767 const MachineOperand &PredOp, bool Cond,
768 std::set<unsigned> &UpdRegs) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000769 // The problem with updating live intervals is that we can move one def
770 // past another def. In particular, this can happen when moving an A2_tfrt
771 // over an A2_tfrf defining the same register. From the point of view of
772 // live intervals, these two instructions are two separate definitions,
773 // and each one starts another live segment. LiveIntervals's "handleMove"
774 // does not allow such moves, so we need to handle it ourselves. To avoid
775 // invalidating liveness data while we are using it, the move will be
776 // implemented in 4 steps: (1) add a clone of the instruction MI at the
777 // target location, (2) update liveness, (3) delete the old instruction,
778 // and (4) update liveness again.
779
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000780 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000781 DebugLoc DL = Where->getDebugLoc(); // "Where" points to an instruction.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000782 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000783 unsigned PredOpc = HII->getCondOpcode(Opc, !Cond);
784 MachineInstrBuilder MB = BuildMI(B, Where, DL, HII->get(PredOpc));
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000785 unsigned Ox = 0, NP = MI.getNumOperands();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000786 // Skip all defs from MI first.
787 while (Ox < NP) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000788 MachineOperand &MO = MI.getOperand(Ox);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000789 if (!MO.isReg() || !MO.isDef())
790 break;
791 Ox++;
792 }
793 // Add the new def, then the predicate register, then the rest of the
794 // operands.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000795 MB.addReg(DefOp.getReg(), getRegState(DefOp), DefOp.getSubReg());
796 MB.addReg(PredOp.getReg(), PredOp.isUndef() ? RegState::Undef : 0,
797 PredOp.getSubReg());
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000798 while (Ox < NP) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000799 MachineOperand &MO = MI.getOperand(Ox);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000800 if (!MO.isReg() || !MO.isImplicit())
801 MB.addOperand(MO);
802 Ox++;
803 }
804
805 MachineFunction &MF = *B.getParent();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000806 MachineInstr::mmo_iterator I = MI.memoperands_begin();
807 unsigned NR = std::distance(I, MI.memoperands_end());
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000808 MachineInstr::mmo_iterator MemRefs = MF.allocateMemRefsArray(NR);
809 for (unsigned i = 0; i < NR; ++i)
810 MemRefs[i] = *I++;
811 MB.setMemRefs(MemRefs, MemRefs+NR);
812
813 MachineInstr *NewI = MB;
814 NewI->clearKillInfo();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000815 LIS->InsertMachineInstrInMaps(*NewI);
816
817 for (auto &Op : NewI->operands())
818 if (Op.isReg())
819 UpdRegs.insert(Op.getReg());
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000820}
821
822
823/// In the range [First, Last], rename all references to the "old" register RO
824/// to the "new" register RN, but only in instructions predicated on the given
825/// condition.
826void HexagonExpandCondsets::renameInRange(RegisterRef RO, RegisterRef RN,
827 unsigned PredR, bool Cond, MachineBasicBlock::iterator First,
828 MachineBasicBlock::iterator Last) {
829 MachineBasicBlock::iterator End = std::next(Last);
830 for (MachineBasicBlock::iterator I = First; I != End; ++I) {
831 MachineInstr *MI = &*I;
832 // Do not touch instructions that are not predicated, or are predicated
833 // on the opposite condition.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000834 if (!HII->isPredicated(*MI))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000835 continue;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000836 if (!MI->readsRegister(PredR) || (Cond != HII->isPredicatedTrue(*MI)))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000837 continue;
838
839 for (auto &Op : MI->operands()) {
840 if (!Op.isReg() || RO != RegisterRef(Op))
841 continue;
842 Op.setReg(RN.Reg);
843 Op.setSubReg(RN.Sub);
844 // In practice, this isn't supposed to see any defs.
845 assert(!Op.isDef() && "Not expecting a def");
846 }
847 }
848}
849
850
851/// For a given conditional copy, predicate the definition of the source of
852/// the copy under the given condition (using the same predicate register as
853/// the copy).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000854bool HexagonExpandCondsets::predicate(MachineInstr &TfrI, bool Cond,
855 std::set<unsigned> &UpdRegs) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000856 // TfrI - A2_tfr[tf] Instruction (not A2_tfrsi).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000857 unsigned Opc = TfrI.getOpcode();
Simon Atanasyan772944a2015-03-31 19:43:47 +0000858 (void)Opc;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000859 assert(Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf);
860 DEBUG(dbgs() << "\nattempt to predicate if-" << (Cond ? "true" : "false")
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000861 << ": " << TfrI);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000862
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000863 MachineOperand &MD = TfrI.getOperand(0);
864 MachineOperand &MP = TfrI.getOperand(1);
865 MachineOperand &MS = TfrI.getOperand(2);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000866 // The source operand should be a <kill>. This is not strictly necessary,
867 // but it makes things a lot simpler. Otherwise, we would need to rename
868 // some registers, which would complicate the transformation considerably.
869 if (!MS.isKill())
870 return false;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000871 // Avoid predicating instructions that define a subregister if subregister
872 // liveness tracking is not enabled.
873 if (MD.getSubReg() && !MRI->shouldTrackSubRegLiveness(MD.getReg()))
Krzysztof Parzyszeka5802732016-05-31 14:27:10 +0000874 return false;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000875
876 RegisterRef RT(MS);
877 unsigned PredR = MP.getReg();
878 MachineInstr *DefI = getReachingDefForPred(RT, TfrI, PredR, Cond);
879 if (!DefI || !isPredicable(DefI))
880 return false;
881
882 DEBUG(dbgs() << "Source def: " << *DefI);
883
884 // Collect the information about registers defined and used between the
885 // DefI and the TfrI.
886 // Map: reg -> bitmask of subregs
887 ReferenceMap Uses, Defs;
888 MachineBasicBlock::iterator DefIt = DefI, TfrIt = TfrI;
889
890 // Check if the predicate register is valid between DefI and TfrI.
891 // If it is, we can then ignore instructions predicated on the negated
892 // conditions when collecting def and use information.
893 bool PredValid = true;
894 for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) {
895 if (!I->modifiesRegister(PredR, 0))
896 continue;
897 PredValid = false;
898 break;
899 }
900
901 for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) {
902 MachineInstr *MI = &*I;
903 // If this instruction is predicated on the same register, it could
904 // potentially be ignored.
905 // By default assume that the instruction executes on the same condition
906 // as TfrI (Exec_Then), and also on the opposite one (Exec_Else).
907 unsigned Exec = Exec_Then | Exec_Else;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000908 if (PredValid && HII->isPredicated(*MI) && MI->readsRegister(PredR))
909 Exec = (Cond == HII->isPredicatedTrue(*MI)) ? Exec_Then : Exec_Else;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000910
911 for (auto &Op : MI->operands()) {
912 if (!Op.isReg())
913 continue;
914 // We don't want to deal with physical registers. The reason is that
915 // they can be aliased with other physical registers. Aliased virtual
916 // registers must share the same register number, and can only differ
917 // in the subregisters, which we are keeping track of. Physical
918 // registers ters no longer have subregisters---their super- and
919 // subregisters are other physical registers, and we are not checking
920 // that.
921 RegisterRef RR = Op;
922 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
923 return false;
924
925 ReferenceMap &Map = Op.isDef() ? Defs : Uses;
926 addRefToMap(RR, Map, Exec);
927 }
928 }
929
930 // The situation:
931 // RT = DefI
932 // ...
933 // RD = TfrI ..., RT
934
935 // If the register-in-the-middle (RT) is used or redefined between
936 // DefI and TfrI, we may not be able proceed with this transformation.
937 // We can ignore a def that will not execute together with TfrI, and a
938 // use that will. If there is such a use (that does execute together with
939 // TfrI), we will not be able to move DefI down. If there is a use that
940 // executed if TfrI's condition is false, then RT must be available
941 // unconditionally (cannot be predicated).
942 // Essentially, we need to be able to rename RT to RD in this segment.
943 if (isRefInMap(RT, Defs, Exec_Then) || isRefInMap(RT, Uses, Exec_Else))
944 return false;
945 RegisterRef RD = MD;
946 // If the predicate register is defined between DefI and TfrI, the only
947 // potential thing to do would be to move the DefI down to TfrI, and then
948 // predicate. The reaching def (DefI) must be movable down to the location
949 // of the TfrI.
950 // If the target register of the TfrI (RD) is not used or defined between
951 // DefI and TfrI, consider moving TfrI up to DefI.
952 bool CanUp = canMoveOver(TfrI, Defs, Uses);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000953 bool CanDown = canMoveOver(*DefI, Defs, Uses);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000954 // The TfrI does not access memory, but DefI could. Check if it's safe
955 // to move DefI down to TfrI.
956 if (DefI->mayLoad() || DefI->mayStore())
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000957 if (!canMoveMemTo(*DefI, TfrI, true))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000958 CanDown = false;
959
960 DEBUG(dbgs() << "Can move up: " << (CanUp ? "yes" : "no")
961 << ", can move down: " << (CanDown ? "yes\n" : "no\n"));
962 MachineBasicBlock::iterator PastDefIt = std::next(DefIt);
963 if (CanUp)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000964 predicateAt(MD, *DefI, PastDefIt, MP, Cond, UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000965 else if (CanDown)
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000966 predicateAt(MD, *DefI, TfrIt, MP, Cond, UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000967 else
968 return false;
969
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000970 if (RT != RD) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000971 renameInRange(RT, RD, PredR, Cond, PastDefIt, TfrIt);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000972 UpdRegs.insert(RT.Reg);
973 }
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000974
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000975 removeInstr(TfrI);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000976 removeInstr(*DefI);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000977 return true;
978}
979
980
981/// Predicate all cases of conditional copies in the specified block.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000982bool HexagonExpandCondsets::predicateInBlock(MachineBasicBlock &B,
983 std::set<unsigned> &UpdRegs) {
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000984 bool Changed = false;
985 MachineBasicBlock::iterator I, E, NextI;
986 for (I = B.begin(), E = B.end(); I != E; I = NextI) {
987 NextI = std::next(I);
988 unsigned Opc = I->getOpcode();
989 if (Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000990 bool Done = predicate(*I, (Opc == Hexagon::A2_tfrt), UpdRegs);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000991 if (!Done) {
992 // If we didn't predicate I, we may need to remove it in case it is
993 // an "identity" copy, e.g. vreg1 = A2_tfrt vreg2, vreg1.
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000994 if (RegisterRef(I->getOperand(0)) == RegisterRef(I->getOperand(2))) {
995 for (auto &Op : I->operands())
996 if (Op.isReg())
997 UpdRegs.insert(Op.getReg());
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000998 removeInstr(*I);
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +0000999 }
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001000 }
1001 Changed |= Done;
1002 }
1003 }
1004 return Changed;
1005}
1006
1007
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001008bool HexagonExpandCondsets::isIntReg(RegisterRef RR, unsigned &BW) {
1009 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
1010 return false;
1011 const TargetRegisterClass *RC = MRI->getRegClass(RR.Reg);
1012 if (RC == &Hexagon::IntRegsRegClass) {
1013 BW = 32;
1014 return true;
1015 }
1016 if (RC == &Hexagon::DoubleRegsRegClass) {
1017 BW = (RR.Sub != 0) ? 32 : 64;
1018 return true;
1019 }
1020 return false;
1021}
1022
1023
1024bool HexagonExpandCondsets::isIntraBlocks(LiveInterval &LI) {
1025 for (LiveInterval::iterator I = LI.begin(), E = LI.end(); I != E; ++I) {
1026 LiveRange::Segment &LR = *I;
1027 // Range must start at a register...
1028 if (!LR.start.isRegister())
1029 return false;
1030 // ...and end in a register or in a dead slot.
1031 if (!LR.end.isRegister() && !LR.end.isDead())
1032 return false;
1033 }
1034 return true;
1035}
1036
1037
1038bool HexagonExpandCondsets::coalesceRegisters(RegisterRef R1, RegisterRef R2) {
1039 if (CoaLimitActive) {
1040 if (CoaCounter >= CoaLimit)
1041 return false;
1042 CoaCounter++;
1043 }
1044 unsigned BW1, BW2;
1045 if (!isIntReg(R1, BW1) || !isIntReg(R2, BW2) || BW1 != BW2)
1046 return false;
1047 if (MRI->isLiveIn(R1.Reg))
1048 return false;
1049 if (MRI->isLiveIn(R2.Reg))
1050 return false;
1051
1052 LiveInterval &L1 = LIS->getInterval(R1.Reg);
1053 LiveInterval &L2 = LIS->getInterval(R2.Reg);
Krzysztof Parzyszek66dd6792016-08-19 14:29:43 +00001054 if (L2.empty())
1055 return false;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001056 bool Overlap = L1.overlaps(L2);
1057
1058 DEBUG(dbgs() << "compatible registers: ("
1059 << (Overlap ? "overlap" : "disjoint") << ")\n "
1060 << PrintReg(R1.Reg, TRI, R1.Sub) << " " << L1 << "\n "
1061 << PrintReg(R2.Reg, TRI, R2.Sub) << " " << L2 << "\n");
1062 if (R1.Sub || R2.Sub)
1063 return false;
1064 if (Overlap)
1065 return false;
1066
1067 // Coalescing could have a negative impact on scheduling, so try to limit
1068 // to some reasonable extent. Only consider coalescing segments, when one
1069 // of them does not cross basic block boundaries.
1070 if (!isIntraBlocks(L1) && !isIntraBlocks(L2))
1071 return false;
1072
1073 MRI->replaceRegWith(R2.Reg, R1.Reg);
1074
1075 // Move all live segments from L2 to L1.
1076 typedef DenseMap<VNInfo*,VNInfo*> ValueInfoMap;
1077 ValueInfoMap VM;
1078 for (LiveInterval::iterator I = L2.begin(), E = L2.end(); I != E; ++I) {
1079 VNInfo *NewVN, *OldVN = I->valno;
1080 ValueInfoMap::iterator F = VM.find(OldVN);
1081 if (F == VM.end()) {
1082 NewVN = L1.getNextValue(I->valno->def, LIS->getVNInfoAllocator());
1083 VM.insert(std::make_pair(OldVN, NewVN));
1084 } else {
1085 NewVN = F->second;
1086 }
1087 L1.addSegment(LiveRange::Segment(I->start, I->end, NewVN));
1088 }
1089 while (L2.begin() != L2.end())
1090 L2.removeSegment(*L2.begin());
1091
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001092 updateKillFlags(R1.Reg);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001093 DEBUG(dbgs() << "coalesced: " << L1 << "\n");
1094 L1.verify();
1095
1096 return true;
1097}
1098
1099
1100/// Attempt to coalesce one of the source registers to a MUX intruction with
1101/// the destination register. This could lead to having only one predicated
1102/// instruction in the end instead of two.
1103bool HexagonExpandCondsets::coalesceSegments(MachineFunction &MF) {
1104 SmallVector<MachineInstr*,16> Condsets;
1105 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
1106 MachineBasicBlock &B = *I;
1107 for (MachineBasicBlock::iterator J = B.begin(), F = B.end(); J != F; ++J) {
1108 MachineInstr *MI = &*J;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001109 if (!isCondset(*MI))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001110 continue;
1111 MachineOperand &S1 = MI->getOperand(2), &S2 = MI->getOperand(3);
1112 if (!S1.isReg() && !S2.isReg())
1113 continue;
1114 Condsets.push_back(MI);
1115 }
1116 }
1117
1118 bool Changed = false;
1119 for (unsigned i = 0, n = Condsets.size(); i < n; ++i) {
1120 MachineInstr *CI = Condsets[i];
1121 RegisterRef RD = CI->getOperand(0);
1122 RegisterRef RP = CI->getOperand(1);
1123 MachineOperand &S1 = CI->getOperand(2), &S2 = CI->getOperand(3);
1124 bool Done = false;
1125 // Consider this case:
1126 // vreg1 = instr1 ...
1127 // vreg2 = instr2 ...
1128 // vreg0 = C2_mux ..., vreg1, vreg2
1129 // If vreg0 was coalesced with vreg1, we could end up with the following
1130 // code:
1131 // vreg0 = instr1 ...
1132 // vreg2 = instr2 ...
1133 // vreg0 = A2_tfrf ..., vreg2
1134 // which will later become:
1135 // vreg0 = instr1 ...
1136 // vreg0 = instr2_cNotPt ...
1137 // i.e. there will be an unconditional definition (instr1) of vreg0
1138 // followed by a conditional one. The output dependency was there before
1139 // and it unavoidable, but if instr1 is predicable, we will no longer be
1140 // able to predicate it here.
1141 // To avoid this scenario, don't coalesce the destination register with
1142 // a source register that is defined by a predicable instruction.
1143 if (S1.isReg()) {
1144 RegisterRef RS = S1;
1145 MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, true);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001146 if (!RDef || !HII->isPredicable(*RDef))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001147 Done = coalesceRegisters(RD, RegisterRef(S1));
1148 }
1149 if (!Done && S2.isReg()) {
1150 RegisterRef RS = S2;
1151 MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, false);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001152 if (!RDef || !HII->isPredicable(*RDef))
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001153 Done = coalesceRegisters(RD, RegisterRef(S2));
1154 }
1155 Changed |= Done;
1156 }
1157 return Changed;
1158}
1159
1160
1161bool HexagonExpandCondsets::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +00001162 if (skipFunction(*MF.getFunction()))
1163 return false;
1164
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001165 HII = static_cast<const HexagonInstrInfo*>(MF.getSubtarget().getInstrInfo());
1166 TRI = MF.getSubtarget().getRegisterInfo();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001167 MDT = &getAnalysis<MachineDominatorTree>();
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001168 LIS = &getAnalysis<LiveIntervals>();
1169 MRI = &MF.getRegInfo();
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001170
1171 DEBUG(LIS->print(dbgs() << "Before expand-condsets\n",
1172 MF.getFunction()->getParent()));
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001173
1174 bool Changed = false;
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001175 std::set<unsigned> SplitUpd, PredUpd;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001176
1177 // Try to coalesce the target of a mux with one of its sources.
1178 // This could eliminate a register copy in some circumstances.
1179 Changed |= coalesceSegments(MF);
1180
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001181 // First, simply split all muxes into a pair of conditional transfers
1182 // and update the live intervals to reflect the new arrangement. The
1183 // goal is to update the kill flags, since predication will rely on
1184 // them.
1185 for (auto &B : MF)
1186 Changed |= splitInBlock(B, SplitUpd);
1187 updateLiveness(SplitUpd, true, true, false);
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001188
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001189 // Traverse all blocks and collapse predicable instructions feeding
1190 // conditional transfers into predicated instructions.
1191 // Walk over all the instructions again, so we may catch pre-existing
1192 // cases that were not created in the previous step.
1193 for (auto &B : MF)
1194 Changed |= predicateInBlock(B, PredUpd);
Krzysztof Parzyszek9062b752016-04-22 16:47:01 +00001195
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001196 updateLiveness(PredUpd, true, true, true);
1197 // Remove from SplitUpd all registers contained in PredUpd to avoid
1198 // unnecessary liveness recalculation.
1199 std::set<unsigned> Diff;
1200 std::set_difference(SplitUpd.begin(), SplitUpd.end(),
1201 PredUpd.begin(), PredUpd.end(),
1202 std::inserter(Diff, Diff.begin()));
1203 updateLiveness(Diff, false, false, true);
1204
Krzysztof Parzyszekb16882d2016-06-08 12:31:16 +00001205 DEBUG({
1206 if (Changed)
1207 LIS->print(dbgs() << "After expand-condsets\n",
1208 MF.getFunction()->getParent());
1209 });
Krzysztof Parzyszek9062b752016-04-22 16:47:01 +00001210
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001211 return Changed;
1212}
1213
1214
1215//===----------------------------------------------------------------------===//
1216// Public Constructor Functions
1217//===----------------------------------------------------------------------===//
1218
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001219FunctionPass *llvm::createHexagonExpandCondsets() {
1220 return new HexagonExpandCondsets();
1221}