1f4a2713aSLionel Sambuc //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the TwoAddress instruction pass which is used
11f4a2713aSLionel Sambuc // by most register allocators. Two-Address instructions are rewritten
12f4a2713aSLionel Sambuc // from:
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc // A = B op C
15f4a2713aSLionel Sambuc //
16f4a2713aSLionel Sambuc // to:
17f4a2713aSLionel Sambuc //
18f4a2713aSLionel Sambuc // A = B
19f4a2713aSLionel Sambuc // A op= C
20f4a2713aSLionel Sambuc //
21f4a2713aSLionel Sambuc // Note that if a register allocator chooses to use this pass, that it
22f4a2713aSLionel Sambuc // has to be capable of handling the non-SSA nature of these rewritten
23f4a2713aSLionel Sambuc // virtual registers.
24f4a2713aSLionel Sambuc //
25f4a2713aSLionel Sambuc // It is also worth noting that the duplicate operand of the two
26f4a2713aSLionel Sambuc // address instruction is removed.
27f4a2713aSLionel Sambuc //
28f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h"
31f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h"
32f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
33f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
34f4a2713aSLionel Sambuc #include "llvm/ADT/SmallSet.h"
35f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
36f4a2713aSLionel Sambuc #include "llvm/Analysis/AliasAnalysis.h"
37f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveIntervalAnalysis.h"
38f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveVariables.h"
39f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunctionPass.h"
40f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstr.h"
41f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
42f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
43f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
44f4a2713aSLionel Sambuc #include "llvm/MC/MCInstrItineraries.h"
45f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
46f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
47f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
48f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
49f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
50f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
51*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
52f4a2713aSLionel Sambuc using namespace llvm;
53f4a2713aSLionel Sambuc
54*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "twoaddrinstr"
55*0a6a1f1dSLionel Sambuc
56f4a2713aSLionel Sambuc STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
57f4a2713aSLionel Sambuc STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
58f4a2713aSLionel Sambuc STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted");
59f4a2713aSLionel Sambuc STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
60f4a2713aSLionel Sambuc STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
61f4a2713aSLionel Sambuc STATISTIC(NumReSchedUps, "Number of instructions re-scheduled up");
62f4a2713aSLionel Sambuc STATISTIC(NumReSchedDowns, "Number of instructions re-scheduled down");
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc // Temporary flag to disable rescheduling.
65f4a2713aSLionel Sambuc static cl::opt<bool>
66f4a2713aSLionel Sambuc EnableRescheduling("twoaddr-reschedule",
67f4a2713aSLionel Sambuc cl::desc("Coalesce copies by rescheduling (default=true)"),
68f4a2713aSLionel Sambuc cl::init(true), cl::Hidden);
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc namespace {
71f4a2713aSLionel Sambuc class TwoAddressInstructionPass : public MachineFunctionPass {
72f4a2713aSLionel Sambuc MachineFunction *MF;
73f4a2713aSLionel Sambuc const TargetInstrInfo *TII;
74f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI;
75f4a2713aSLionel Sambuc const InstrItineraryData *InstrItins;
76f4a2713aSLionel Sambuc MachineRegisterInfo *MRI;
77f4a2713aSLionel Sambuc LiveVariables *LV;
78f4a2713aSLionel Sambuc LiveIntervals *LIS;
79f4a2713aSLionel Sambuc AliasAnalysis *AA;
80f4a2713aSLionel Sambuc CodeGenOpt::Level OptLevel;
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc // The current basic block being processed.
83f4a2713aSLionel Sambuc MachineBasicBlock *MBB;
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc // DistanceMap - Keep track the distance of a MI from the start of the
86f4a2713aSLionel Sambuc // current basic block.
87f4a2713aSLionel Sambuc DenseMap<MachineInstr*, unsigned> DistanceMap;
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc // Set of already processed instructions in the current block.
90f4a2713aSLionel Sambuc SmallPtrSet<MachineInstr*, 8> Processed;
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc // SrcRegMap - A map from virtual registers to physical registers which are
93f4a2713aSLionel Sambuc // likely targets to be coalesced to due to copies from physical registers to
94f4a2713aSLionel Sambuc // virtual registers. e.g. v1024 = move r0.
95f4a2713aSLionel Sambuc DenseMap<unsigned, unsigned> SrcRegMap;
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc // DstRegMap - A map from virtual registers to physical registers which are
98f4a2713aSLionel Sambuc // likely targets to be coalesced to due to copies to physical registers from
99f4a2713aSLionel Sambuc // virtual registers. e.g. r1 = move v1024.
100f4a2713aSLionel Sambuc DenseMap<unsigned, unsigned> DstRegMap;
101f4a2713aSLionel Sambuc
102f4a2713aSLionel Sambuc bool sink3AddrInstruction(MachineInstr *MI, unsigned Reg,
103f4a2713aSLionel Sambuc MachineBasicBlock::iterator OldPos);
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc bool noUseAfterLastDef(unsigned Reg, unsigned Dist, unsigned &LastDef);
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
108f4a2713aSLionel Sambuc MachineInstr *MI, unsigned Dist);
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc bool commuteInstruction(MachineBasicBlock::iterator &mi,
111f4a2713aSLionel Sambuc unsigned RegB, unsigned RegC, unsigned Dist);
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc bool isProfitableToConv3Addr(unsigned RegA, unsigned RegB);
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc bool convertInstTo3Addr(MachineBasicBlock::iterator &mi,
116f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
117f4a2713aSLionel Sambuc unsigned RegA, unsigned RegB, unsigned Dist);
118f4a2713aSLionel Sambuc
119f4a2713aSLionel Sambuc bool isDefTooClose(unsigned Reg, unsigned Dist, MachineInstr *MI);
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc bool rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
122f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
123f4a2713aSLionel Sambuc unsigned Reg);
124f4a2713aSLionel Sambuc bool rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
125f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
126f4a2713aSLionel Sambuc unsigned Reg);
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc bool tryInstructionTransform(MachineBasicBlock::iterator &mi,
129f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
130f4a2713aSLionel Sambuc unsigned SrcIdx, unsigned DstIdx,
131f4a2713aSLionel Sambuc unsigned Dist, bool shouldOnlyCommute);
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc void scanUses(unsigned DstReg);
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc void processCopy(MachineInstr *MI);
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc typedef SmallVector<std::pair<unsigned, unsigned>, 4> TiedPairList;
138f4a2713aSLionel Sambuc typedef SmallDenseMap<unsigned, TiedPairList> TiedOperandMap;
139f4a2713aSLionel Sambuc bool collectTiedOperands(MachineInstr *MI, TiedOperandMap&);
140f4a2713aSLionel Sambuc void processTiedPairs(MachineInstr *MI, TiedPairList&, unsigned &Dist);
141f4a2713aSLionel Sambuc void eliminateRegSequence(MachineBasicBlock::iterator&);
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc public:
144f4a2713aSLionel Sambuc static char ID; // Pass identification, replacement for typeid
TwoAddressInstructionPass()145f4a2713aSLionel Sambuc TwoAddressInstructionPass() : MachineFunctionPass(ID) {
146f4a2713aSLionel Sambuc initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry());
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
getAnalysisUsage(AnalysisUsage & AU) const149*0a6a1f1dSLionel Sambuc void getAnalysisUsage(AnalysisUsage &AU) const override {
150f4a2713aSLionel Sambuc AU.setPreservesCFG();
151f4a2713aSLionel Sambuc AU.addRequired<AliasAnalysis>();
152f4a2713aSLionel Sambuc AU.addPreserved<LiveVariables>();
153f4a2713aSLionel Sambuc AU.addPreserved<SlotIndexes>();
154f4a2713aSLionel Sambuc AU.addPreserved<LiveIntervals>();
155f4a2713aSLionel Sambuc AU.addPreservedID(MachineLoopInfoID);
156f4a2713aSLionel Sambuc AU.addPreservedID(MachineDominatorsID);
157f4a2713aSLionel Sambuc MachineFunctionPass::getAnalysisUsage(AU);
158f4a2713aSLionel Sambuc }
159f4a2713aSLionel Sambuc
160f4a2713aSLionel Sambuc /// runOnMachineFunction - Pass entry point.
161*0a6a1f1dSLionel Sambuc bool runOnMachineFunction(MachineFunction&) override;
162f4a2713aSLionel Sambuc };
163f4a2713aSLionel Sambuc } // end anonymous namespace
164f4a2713aSLionel Sambuc
165f4a2713aSLionel Sambuc char TwoAddressInstructionPass::ID = 0;
166f4a2713aSLionel Sambuc INITIALIZE_PASS_BEGIN(TwoAddressInstructionPass, "twoaddressinstruction",
167f4a2713aSLionel Sambuc "Two-Address instruction pass", false, false)
168f4a2713aSLionel Sambuc INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
169f4a2713aSLionel Sambuc INITIALIZE_PASS_END(TwoAddressInstructionPass, "twoaddressinstruction",
170f4a2713aSLionel Sambuc "Two-Address instruction pass", false, false)
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID;
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg, LiveIntervals *LIS);
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc /// sink3AddrInstruction - A two-address instruction has been converted to a
177f4a2713aSLionel Sambuc /// three-address instruction to avoid clobbering a register. Try to sink it
178f4a2713aSLionel Sambuc /// past the instruction that would kill the above mentioned register to reduce
179f4a2713aSLionel Sambuc /// register pressure.
180f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
sink3AddrInstruction(MachineInstr * MI,unsigned SavedReg,MachineBasicBlock::iterator OldPos)181f4a2713aSLionel Sambuc sink3AddrInstruction(MachineInstr *MI, unsigned SavedReg,
182f4a2713aSLionel Sambuc MachineBasicBlock::iterator OldPos) {
183f4a2713aSLionel Sambuc // FIXME: Shouldn't we be trying to do this before we three-addressify the
184f4a2713aSLionel Sambuc // instruction? After this transformation is done, we no longer need
185f4a2713aSLionel Sambuc // the instruction to be in three-address form.
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc // Check if it's safe to move this instruction.
188f4a2713aSLionel Sambuc bool SeenStore = true; // Be conservative.
189f4a2713aSLionel Sambuc if (!MI->isSafeToMove(TII, AA, SeenStore))
190f4a2713aSLionel Sambuc return false;
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc unsigned DefReg = 0;
193f4a2713aSLionel Sambuc SmallSet<unsigned, 4> UseRegs;
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
196f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
197f4a2713aSLionel Sambuc if (!MO.isReg())
198f4a2713aSLionel Sambuc continue;
199f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
200f4a2713aSLionel Sambuc if (!MOReg)
201f4a2713aSLionel Sambuc continue;
202f4a2713aSLionel Sambuc if (MO.isUse() && MOReg != SavedReg)
203f4a2713aSLionel Sambuc UseRegs.insert(MO.getReg());
204f4a2713aSLionel Sambuc if (!MO.isDef())
205f4a2713aSLionel Sambuc continue;
206f4a2713aSLionel Sambuc if (MO.isImplicit())
207f4a2713aSLionel Sambuc // Don't try to move it if it implicitly defines a register.
208f4a2713aSLionel Sambuc return false;
209f4a2713aSLionel Sambuc if (DefReg)
210f4a2713aSLionel Sambuc // For now, don't move any instructions that define multiple registers.
211f4a2713aSLionel Sambuc return false;
212f4a2713aSLionel Sambuc DefReg = MO.getReg();
213f4a2713aSLionel Sambuc }
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc // Find the instruction that kills SavedReg.
216*0a6a1f1dSLionel Sambuc MachineInstr *KillMI = nullptr;
217f4a2713aSLionel Sambuc if (LIS) {
218f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(SavedReg);
219f4a2713aSLionel Sambuc assert(LI.end() != LI.begin() &&
220f4a2713aSLionel Sambuc "Reg should not have empty live interval.");
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
223f4a2713aSLionel Sambuc LiveInterval::const_iterator I = LI.find(MBBEndIdx);
224f4a2713aSLionel Sambuc if (I != LI.end() && I->start < MBBEndIdx)
225f4a2713aSLionel Sambuc return false;
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc --I;
228f4a2713aSLionel Sambuc KillMI = LIS->getInstructionFromIndex(I->end);
229f4a2713aSLionel Sambuc }
230f4a2713aSLionel Sambuc if (!KillMI) {
231f4a2713aSLionel Sambuc for (MachineRegisterInfo::use_nodbg_iterator
232f4a2713aSLionel Sambuc UI = MRI->use_nodbg_begin(SavedReg),
233f4a2713aSLionel Sambuc UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
234*0a6a1f1dSLionel Sambuc MachineOperand &UseMO = *UI;
235f4a2713aSLionel Sambuc if (!UseMO.isKill())
236f4a2713aSLionel Sambuc continue;
237f4a2713aSLionel Sambuc KillMI = UseMO.getParent();
238f4a2713aSLionel Sambuc break;
239f4a2713aSLionel Sambuc }
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc // If we find the instruction that kills SavedReg, and it is in an
243f4a2713aSLionel Sambuc // appropriate location, we can try to sink the current instruction
244f4a2713aSLionel Sambuc // past it.
245f4a2713aSLionel Sambuc if (!KillMI || KillMI->getParent() != MBB || KillMI == MI ||
246f4a2713aSLionel Sambuc KillMI == OldPos || KillMI->isTerminator())
247f4a2713aSLionel Sambuc return false;
248f4a2713aSLionel Sambuc
249f4a2713aSLionel Sambuc // If any of the definitions are used by another instruction between the
250f4a2713aSLionel Sambuc // position and the kill use, then it's not safe to sink it.
251f4a2713aSLionel Sambuc //
252f4a2713aSLionel Sambuc // FIXME: This can be sped up if there is an easy way to query whether an
253f4a2713aSLionel Sambuc // instruction is before or after another instruction. Then we can use
254f4a2713aSLionel Sambuc // MachineRegisterInfo def / use instead.
255*0a6a1f1dSLionel Sambuc MachineOperand *KillMO = nullptr;
256f4a2713aSLionel Sambuc MachineBasicBlock::iterator KillPos = KillMI;
257f4a2713aSLionel Sambuc ++KillPos;
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc unsigned NumVisited = 0;
260*0a6a1f1dSLionel Sambuc for (MachineBasicBlock::iterator I = std::next(OldPos); I != KillPos; ++I) {
261f4a2713aSLionel Sambuc MachineInstr *OtherMI = I;
262f4a2713aSLionel Sambuc // DBG_VALUE cannot be counted against the limit.
263f4a2713aSLionel Sambuc if (OtherMI->isDebugValue())
264f4a2713aSLionel Sambuc continue;
265f4a2713aSLionel Sambuc if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
266f4a2713aSLionel Sambuc return false;
267f4a2713aSLionel Sambuc ++NumVisited;
268f4a2713aSLionel Sambuc for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
269f4a2713aSLionel Sambuc MachineOperand &MO = OtherMI->getOperand(i);
270f4a2713aSLionel Sambuc if (!MO.isReg())
271f4a2713aSLionel Sambuc continue;
272f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
273f4a2713aSLionel Sambuc if (!MOReg)
274f4a2713aSLionel Sambuc continue;
275f4a2713aSLionel Sambuc if (DefReg == MOReg)
276f4a2713aSLionel Sambuc return false;
277f4a2713aSLionel Sambuc
278f4a2713aSLionel Sambuc if (MO.isKill() || (LIS && isPlainlyKilled(OtherMI, MOReg, LIS))) {
279f4a2713aSLionel Sambuc if (OtherMI == KillMI && MOReg == SavedReg)
280f4a2713aSLionel Sambuc // Save the operand that kills the register. We want to unset the kill
281f4a2713aSLionel Sambuc // marker if we can sink MI past it.
282f4a2713aSLionel Sambuc KillMO = &MO;
283f4a2713aSLionel Sambuc else if (UseRegs.count(MOReg))
284f4a2713aSLionel Sambuc // One of the uses is killed before the destination.
285f4a2713aSLionel Sambuc return false;
286f4a2713aSLionel Sambuc }
287f4a2713aSLionel Sambuc }
288f4a2713aSLionel Sambuc }
289f4a2713aSLionel Sambuc assert(KillMO && "Didn't find kill");
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc if (!LIS) {
292f4a2713aSLionel Sambuc // Update kill and LV information.
293f4a2713aSLionel Sambuc KillMO->setIsKill(false);
294f4a2713aSLionel Sambuc KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
295f4a2713aSLionel Sambuc KillMO->setIsKill(true);
296f4a2713aSLionel Sambuc
297f4a2713aSLionel Sambuc if (LV)
298f4a2713aSLionel Sambuc LV->replaceKillInstruction(SavedReg, KillMI, MI);
299f4a2713aSLionel Sambuc }
300f4a2713aSLionel Sambuc
301f4a2713aSLionel Sambuc // Move instruction to its destination.
302f4a2713aSLionel Sambuc MBB->remove(MI);
303f4a2713aSLionel Sambuc MBB->insert(KillPos, MI);
304f4a2713aSLionel Sambuc
305f4a2713aSLionel Sambuc if (LIS)
306f4a2713aSLionel Sambuc LIS->handleMove(MI);
307f4a2713aSLionel Sambuc
308f4a2713aSLionel Sambuc ++Num3AddrSunk;
309f4a2713aSLionel Sambuc return true;
310f4a2713aSLionel Sambuc }
311f4a2713aSLionel Sambuc
312f4a2713aSLionel Sambuc /// noUseAfterLastDef - Return true if there are no intervening uses between the
313f4a2713aSLionel Sambuc /// last instruction in the MBB that defines the specified register and the
314f4a2713aSLionel Sambuc /// two-address instruction which is being processed. It also returns the last
315f4a2713aSLionel Sambuc /// def location by reference
noUseAfterLastDef(unsigned Reg,unsigned Dist,unsigned & LastDef)316f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::noUseAfterLastDef(unsigned Reg, unsigned Dist,
317f4a2713aSLionel Sambuc unsigned &LastDef) {
318f4a2713aSLionel Sambuc LastDef = 0;
319f4a2713aSLionel Sambuc unsigned LastUse = Dist;
320*0a6a1f1dSLionel Sambuc for (MachineOperand &MO : MRI->reg_operands(Reg)) {
321f4a2713aSLionel Sambuc MachineInstr *MI = MO.getParent();
322f4a2713aSLionel Sambuc if (MI->getParent() != MBB || MI->isDebugValue())
323f4a2713aSLionel Sambuc continue;
324f4a2713aSLionel Sambuc DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
325f4a2713aSLionel Sambuc if (DI == DistanceMap.end())
326f4a2713aSLionel Sambuc continue;
327f4a2713aSLionel Sambuc if (MO.isUse() && DI->second < LastUse)
328f4a2713aSLionel Sambuc LastUse = DI->second;
329f4a2713aSLionel Sambuc if (MO.isDef() && DI->second > LastDef)
330f4a2713aSLionel Sambuc LastDef = DI->second;
331f4a2713aSLionel Sambuc }
332f4a2713aSLionel Sambuc
333f4a2713aSLionel Sambuc return !(LastUse > LastDef && LastUse < Dist);
334f4a2713aSLionel Sambuc }
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc /// isCopyToReg - Return true if the specified MI is a copy instruction or
337f4a2713aSLionel Sambuc /// a extract_subreg instruction. It also returns the source and destination
338f4a2713aSLionel Sambuc /// registers and whether they are physical registers by reference.
isCopyToReg(MachineInstr & MI,const TargetInstrInfo * TII,unsigned & SrcReg,unsigned & DstReg,bool & IsSrcPhys,bool & IsDstPhys)339f4a2713aSLionel Sambuc static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
340f4a2713aSLionel Sambuc unsigned &SrcReg, unsigned &DstReg,
341f4a2713aSLionel Sambuc bool &IsSrcPhys, bool &IsDstPhys) {
342f4a2713aSLionel Sambuc SrcReg = 0;
343f4a2713aSLionel Sambuc DstReg = 0;
344f4a2713aSLionel Sambuc if (MI.isCopy()) {
345f4a2713aSLionel Sambuc DstReg = MI.getOperand(0).getReg();
346f4a2713aSLionel Sambuc SrcReg = MI.getOperand(1).getReg();
347f4a2713aSLionel Sambuc } else if (MI.isInsertSubreg() || MI.isSubregToReg()) {
348f4a2713aSLionel Sambuc DstReg = MI.getOperand(0).getReg();
349f4a2713aSLionel Sambuc SrcReg = MI.getOperand(2).getReg();
350f4a2713aSLionel Sambuc } else
351f4a2713aSLionel Sambuc return false;
352f4a2713aSLionel Sambuc
353f4a2713aSLionel Sambuc IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
354f4a2713aSLionel Sambuc IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
355f4a2713aSLionel Sambuc return true;
356f4a2713aSLionel Sambuc }
357f4a2713aSLionel Sambuc
358f4a2713aSLionel Sambuc /// isPLainlyKilled - Test if the given register value, which is used by the
359f4a2713aSLionel Sambuc // given instruction, is killed by the given instruction.
isPlainlyKilled(MachineInstr * MI,unsigned Reg,LiveIntervals * LIS)360f4a2713aSLionel Sambuc static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg,
361f4a2713aSLionel Sambuc LiveIntervals *LIS) {
362f4a2713aSLionel Sambuc if (LIS && TargetRegisterInfo::isVirtualRegister(Reg) &&
363f4a2713aSLionel Sambuc !LIS->isNotInMIMap(MI)) {
364f4a2713aSLionel Sambuc // FIXME: Sometimes tryInstructionTransform() will add instructions and
365f4a2713aSLionel Sambuc // test whether they can be folded before keeping them. In this case it
366f4a2713aSLionel Sambuc // sets a kill before recursively calling tryInstructionTransform() again.
367f4a2713aSLionel Sambuc // If there is no interval available, we assume that this instruction is
368f4a2713aSLionel Sambuc // one of those. A kill flag is manually inserted on the operand so the
369f4a2713aSLionel Sambuc // check below will handle it.
370f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(Reg);
371f4a2713aSLionel Sambuc // This is to match the kill flag version where undefs don't have kill
372f4a2713aSLionel Sambuc // flags.
373f4a2713aSLionel Sambuc if (!LI.hasAtLeastOneValue())
374f4a2713aSLionel Sambuc return false;
375f4a2713aSLionel Sambuc
376f4a2713aSLionel Sambuc SlotIndex useIdx = LIS->getInstructionIndex(MI);
377f4a2713aSLionel Sambuc LiveInterval::const_iterator I = LI.find(useIdx);
378f4a2713aSLionel Sambuc assert(I != LI.end() && "Reg must be live-in to use.");
379f4a2713aSLionel Sambuc return !I->end.isBlock() && SlotIndex::isSameInstr(I->end, useIdx);
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc
382f4a2713aSLionel Sambuc return MI->killsRegister(Reg);
383f4a2713aSLionel Sambuc }
384f4a2713aSLionel Sambuc
385f4a2713aSLionel Sambuc /// isKilled - Test if the given register value, which is used by the given
386f4a2713aSLionel Sambuc /// instruction, is killed by the given instruction. This looks through
387f4a2713aSLionel Sambuc /// coalescable copies to see if the original value is potentially not killed.
388f4a2713aSLionel Sambuc ///
389f4a2713aSLionel Sambuc /// For example, in this code:
390f4a2713aSLionel Sambuc ///
391f4a2713aSLionel Sambuc /// %reg1034 = copy %reg1024
392f4a2713aSLionel Sambuc /// %reg1035 = copy %reg1025<kill>
393f4a2713aSLionel Sambuc /// %reg1036 = add %reg1034<kill>, %reg1035<kill>
394f4a2713aSLionel Sambuc ///
395f4a2713aSLionel Sambuc /// %reg1034 is not considered to be killed, since it is copied from a
396f4a2713aSLionel Sambuc /// register which is not killed. Treating it as not killed lets the
397f4a2713aSLionel Sambuc /// normal heuristics commute the (two-address) add, which lets
398f4a2713aSLionel Sambuc /// coalescing eliminate the extra copy.
399f4a2713aSLionel Sambuc ///
400f4a2713aSLionel Sambuc /// If allowFalsePositives is true then likely kills are treated as kills even
401f4a2713aSLionel Sambuc /// if it can't be proven that they are kills.
isKilled(MachineInstr & MI,unsigned Reg,const MachineRegisterInfo * MRI,const TargetInstrInfo * TII,LiveIntervals * LIS,bool allowFalsePositives)402f4a2713aSLionel Sambuc static bool isKilled(MachineInstr &MI, unsigned Reg,
403f4a2713aSLionel Sambuc const MachineRegisterInfo *MRI,
404f4a2713aSLionel Sambuc const TargetInstrInfo *TII,
405f4a2713aSLionel Sambuc LiveIntervals *LIS,
406f4a2713aSLionel Sambuc bool allowFalsePositives) {
407f4a2713aSLionel Sambuc MachineInstr *DefMI = &MI;
408f4a2713aSLionel Sambuc for (;;) {
409f4a2713aSLionel Sambuc // All uses of physical registers are likely to be kills.
410f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
411f4a2713aSLionel Sambuc (allowFalsePositives || MRI->hasOneUse(Reg)))
412f4a2713aSLionel Sambuc return true;
413f4a2713aSLionel Sambuc if (!isPlainlyKilled(DefMI, Reg, LIS))
414f4a2713aSLionel Sambuc return false;
415f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Reg))
416f4a2713aSLionel Sambuc return true;
417f4a2713aSLionel Sambuc MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
418f4a2713aSLionel Sambuc // If there are multiple defs, we can't do a simple analysis, so just
419f4a2713aSLionel Sambuc // go with what the kill flag says.
420*0a6a1f1dSLionel Sambuc if (std::next(Begin) != MRI->def_end())
421f4a2713aSLionel Sambuc return true;
422*0a6a1f1dSLionel Sambuc DefMI = Begin->getParent();
423f4a2713aSLionel Sambuc bool IsSrcPhys, IsDstPhys;
424f4a2713aSLionel Sambuc unsigned SrcReg, DstReg;
425f4a2713aSLionel Sambuc // If the def is something other than a copy, then it isn't going to
426f4a2713aSLionel Sambuc // be coalesced, so follow the kill flag.
427f4a2713aSLionel Sambuc if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
428f4a2713aSLionel Sambuc return true;
429f4a2713aSLionel Sambuc Reg = SrcReg;
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc }
432f4a2713aSLionel Sambuc
433f4a2713aSLionel Sambuc /// isTwoAddrUse - Return true if the specified MI uses the specified register
434f4a2713aSLionel Sambuc /// as a two-address use. If so, return the destination register by reference.
isTwoAddrUse(MachineInstr & MI,unsigned Reg,unsigned & DstReg)435f4a2713aSLionel Sambuc static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
436f4a2713aSLionel Sambuc for (unsigned i = 0, NumOps = MI.getNumOperands(); i != NumOps; ++i) {
437f4a2713aSLionel Sambuc const MachineOperand &MO = MI.getOperand(i);
438f4a2713aSLionel Sambuc if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
439f4a2713aSLionel Sambuc continue;
440f4a2713aSLionel Sambuc unsigned ti;
441f4a2713aSLionel Sambuc if (MI.isRegTiedToDefOperand(i, &ti)) {
442f4a2713aSLionel Sambuc DstReg = MI.getOperand(ti).getReg();
443f4a2713aSLionel Sambuc return true;
444f4a2713aSLionel Sambuc }
445f4a2713aSLionel Sambuc }
446f4a2713aSLionel Sambuc return false;
447f4a2713aSLionel Sambuc }
448f4a2713aSLionel Sambuc
449f4a2713aSLionel Sambuc /// findOnlyInterestingUse - Given a register, if has a single in-basic block
450f4a2713aSLionel Sambuc /// use, return the use instruction if it's a copy or a two-address use.
451f4a2713aSLionel Sambuc static
findOnlyInterestingUse(unsigned Reg,MachineBasicBlock * MBB,MachineRegisterInfo * MRI,const TargetInstrInfo * TII,bool & IsCopy,unsigned & DstReg,bool & IsDstPhys)452f4a2713aSLionel Sambuc MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
453f4a2713aSLionel Sambuc MachineRegisterInfo *MRI,
454f4a2713aSLionel Sambuc const TargetInstrInfo *TII,
455f4a2713aSLionel Sambuc bool &IsCopy,
456f4a2713aSLionel Sambuc unsigned &DstReg, bool &IsDstPhys) {
457f4a2713aSLionel Sambuc if (!MRI->hasOneNonDBGUse(Reg))
458f4a2713aSLionel Sambuc // None or more than one use.
459*0a6a1f1dSLionel Sambuc return nullptr;
460*0a6a1f1dSLionel Sambuc MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(Reg);
461f4a2713aSLionel Sambuc if (UseMI.getParent() != MBB)
462*0a6a1f1dSLionel Sambuc return nullptr;
463f4a2713aSLionel Sambuc unsigned SrcReg;
464f4a2713aSLionel Sambuc bool IsSrcPhys;
465f4a2713aSLionel Sambuc if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
466f4a2713aSLionel Sambuc IsCopy = true;
467f4a2713aSLionel Sambuc return &UseMI;
468f4a2713aSLionel Sambuc }
469f4a2713aSLionel Sambuc IsDstPhys = false;
470f4a2713aSLionel Sambuc if (isTwoAddrUse(UseMI, Reg, DstReg)) {
471f4a2713aSLionel Sambuc IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
472f4a2713aSLionel Sambuc return &UseMI;
473f4a2713aSLionel Sambuc }
474*0a6a1f1dSLionel Sambuc return nullptr;
475f4a2713aSLionel Sambuc }
476f4a2713aSLionel Sambuc
477f4a2713aSLionel Sambuc /// getMappedReg - Return the physical register the specified virtual register
478f4a2713aSLionel Sambuc /// might be mapped to.
479f4a2713aSLionel Sambuc static unsigned
getMappedReg(unsigned Reg,DenseMap<unsigned,unsigned> & RegMap)480f4a2713aSLionel Sambuc getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
481f4a2713aSLionel Sambuc while (TargetRegisterInfo::isVirtualRegister(Reg)) {
482f4a2713aSLionel Sambuc DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
483f4a2713aSLionel Sambuc if (SI == RegMap.end())
484f4a2713aSLionel Sambuc return 0;
485f4a2713aSLionel Sambuc Reg = SI->second;
486f4a2713aSLionel Sambuc }
487f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Reg))
488f4a2713aSLionel Sambuc return Reg;
489f4a2713aSLionel Sambuc return 0;
490f4a2713aSLionel Sambuc }
491f4a2713aSLionel Sambuc
492f4a2713aSLionel Sambuc /// regsAreCompatible - Return true if the two registers are equal or aliased.
493f4a2713aSLionel Sambuc ///
494f4a2713aSLionel Sambuc static bool
regsAreCompatible(unsigned RegA,unsigned RegB,const TargetRegisterInfo * TRI)495f4a2713aSLionel Sambuc regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
496f4a2713aSLionel Sambuc if (RegA == RegB)
497f4a2713aSLionel Sambuc return true;
498f4a2713aSLionel Sambuc if (!RegA || !RegB)
499f4a2713aSLionel Sambuc return false;
500f4a2713aSLionel Sambuc return TRI->regsOverlap(RegA, RegB);
501f4a2713aSLionel Sambuc }
502f4a2713aSLionel Sambuc
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc /// isProfitableToCommute - Return true if it's potentially profitable to commute
505f4a2713aSLionel Sambuc /// the two-address instruction that's being processed.
506f4a2713aSLionel Sambuc bool
507f4a2713aSLionel Sambuc TwoAddressInstructionPass::
isProfitableToCommute(unsigned regA,unsigned regB,unsigned regC,MachineInstr * MI,unsigned Dist)508f4a2713aSLionel Sambuc isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
509f4a2713aSLionel Sambuc MachineInstr *MI, unsigned Dist) {
510f4a2713aSLionel Sambuc if (OptLevel == CodeGenOpt::None)
511f4a2713aSLionel Sambuc return false;
512f4a2713aSLionel Sambuc
513f4a2713aSLionel Sambuc // Determine if it's profitable to commute this two address instruction. In
514f4a2713aSLionel Sambuc // general, we want no uses between this instruction and the definition of
515f4a2713aSLionel Sambuc // the two-address register.
516f4a2713aSLionel Sambuc // e.g.
517f4a2713aSLionel Sambuc // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
518f4a2713aSLionel Sambuc // %reg1029<def> = MOV8rr %reg1028
519f4a2713aSLionel Sambuc // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
520f4a2713aSLionel Sambuc // insert => %reg1030<def> = MOV8rr %reg1028
521f4a2713aSLionel Sambuc // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
522f4a2713aSLionel Sambuc // In this case, it might not be possible to coalesce the second MOV8rr
523f4a2713aSLionel Sambuc // instruction if the first one is coalesced. So it would be profitable to
524f4a2713aSLionel Sambuc // commute it:
525f4a2713aSLionel Sambuc // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
526f4a2713aSLionel Sambuc // %reg1029<def> = MOV8rr %reg1028
527f4a2713aSLionel Sambuc // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
528f4a2713aSLionel Sambuc // insert => %reg1030<def> = MOV8rr %reg1029
529f4a2713aSLionel Sambuc // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
530f4a2713aSLionel Sambuc
531f4a2713aSLionel Sambuc if (!isPlainlyKilled(MI, regC, LIS))
532f4a2713aSLionel Sambuc return false;
533f4a2713aSLionel Sambuc
534f4a2713aSLionel Sambuc // Ok, we have something like:
535f4a2713aSLionel Sambuc // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
536f4a2713aSLionel Sambuc // let's see if it's worth commuting it.
537f4a2713aSLionel Sambuc
538f4a2713aSLionel Sambuc // Look for situations like this:
539f4a2713aSLionel Sambuc // %reg1024<def> = MOV r1
540f4a2713aSLionel Sambuc // %reg1025<def> = MOV r0
541f4a2713aSLionel Sambuc // %reg1026<def> = ADD %reg1024, %reg1025
542f4a2713aSLionel Sambuc // r0 = MOV %reg1026
543f4a2713aSLionel Sambuc // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
544f4a2713aSLionel Sambuc unsigned ToRegA = getMappedReg(regA, DstRegMap);
545f4a2713aSLionel Sambuc if (ToRegA) {
546f4a2713aSLionel Sambuc unsigned FromRegB = getMappedReg(regB, SrcRegMap);
547f4a2713aSLionel Sambuc unsigned FromRegC = getMappedReg(regC, SrcRegMap);
548*0a6a1f1dSLionel Sambuc bool CompB = FromRegB && regsAreCompatible(FromRegB, ToRegA, TRI);
549*0a6a1f1dSLionel Sambuc bool CompC = FromRegC && regsAreCompatible(FromRegC, ToRegA, TRI);
550*0a6a1f1dSLionel Sambuc
551*0a6a1f1dSLionel Sambuc // Compute if any of the following are true:
552*0a6a1f1dSLionel Sambuc // -RegB is not tied to a register and RegC is compatible with RegA.
553*0a6a1f1dSLionel Sambuc // -RegB is tied to the wrong physical register, but RegC is.
554*0a6a1f1dSLionel Sambuc // -RegB is tied to the wrong physical register, and RegC isn't tied.
555*0a6a1f1dSLionel Sambuc if ((!FromRegB && CompC) || (FromRegB && !CompB && (!FromRegC || CompC)))
556*0a6a1f1dSLionel Sambuc return true;
557*0a6a1f1dSLionel Sambuc // Don't compute if any of the following are true:
558*0a6a1f1dSLionel Sambuc // -RegC is not tied to a register and RegB is compatible with RegA.
559*0a6a1f1dSLionel Sambuc // -RegC is tied to the wrong physical register, but RegB is.
560*0a6a1f1dSLionel Sambuc // -RegC is tied to the wrong physical register, and RegB isn't tied.
561*0a6a1f1dSLionel Sambuc if ((!FromRegC && CompB) || (FromRegC && !CompC && (!FromRegB || CompB)))
562*0a6a1f1dSLionel Sambuc return false;
563f4a2713aSLionel Sambuc }
564f4a2713aSLionel Sambuc
565f4a2713aSLionel Sambuc // If there is a use of regC between its last def (could be livein) and this
566f4a2713aSLionel Sambuc // instruction, then bail.
567f4a2713aSLionel Sambuc unsigned LastDefC = 0;
568f4a2713aSLionel Sambuc if (!noUseAfterLastDef(regC, Dist, LastDefC))
569f4a2713aSLionel Sambuc return false;
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc // If there is a use of regB between its last def (could be livein) and this
572f4a2713aSLionel Sambuc // instruction, then go ahead and make this transformation.
573f4a2713aSLionel Sambuc unsigned LastDefB = 0;
574f4a2713aSLionel Sambuc if (!noUseAfterLastDef(regB, Dist, LastDefB))
575f4a2713aSLionel Sambuc return true;
576f4a2713aSLionel Sambuc
577f4a2713aSLionel Sambuc // Since there are no intervening uses for both registers, then commute
578f4a2713aSLionel Sambuc // if the def of regC is closer. Its live interval is shorter.
579f4a2713aSLionel Sambuc return LastDefB && LastDefC && LastDefC > LastDefB;
580f4a2713aSLionel Sambuc }
581f4a2713aSLionel Sambuc
582f4a2713aSLionel Sambuc /// commuteInstruction - Commute a two-address instruction and update the basic
583f4a2713aSLionel Sambuc /// block, distance map, and live variables if needed. Return true if it is
584f4a2713aSLionel Sambuc /// successful.
585f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
commuteInstruction(MachineBasicBlock::iterator & mi,unsigned RegB,unsigned RegC,unsigned Dist)586f4a2713aSLionel Sambuc commuteInstruction(MachineBasicBlock::iterator &mi,
587f4a2713aSLionel Sambuc unsigned RegB, unsigned RegC, unsigned Dist) {
588f4a2713aSLionel Sambuc MachineInstr *MI = mi;
589f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: COMMUTING : " << *MI);
590f4a2713aSLionel Sambuc MachineInstr *NewMI = TII->commuteInstruction(MI);
591f4a2713aSLionel Sambuc
592*0a6a1f1dSLionel Sambuc if (NewMI == nullptr) {
593f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
594f4a2713aSLionel Sambuc return false;
595f4a2713aSLionel Sambuc }
596f4a2713aSLionel Sambuc
597f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
598f4a2713aSLionel Sambuc assert(NewMI == MI &&
599f4a2713aSLionel Sambuc "TargetInstrInfo::commuteInstruction() should not return a new "
600f4a2713aSLionel Sambuc "instruction unless it was requested.");
601f4a2713aSLionel Sambuc
602f4a2713aSLionel Sambuc // Update source register map.
603f4a2713aSLionel Sambuc unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
604f4a2713aSLionel Sambuc if (FromRegC) {
605f4a2713aSLionel Sambuc unsigned RegA = MI->getOperand(0).getReg();
606f4a2713aSLionel Sambuc SrcRegMap[RegA] = FromRegC;
607f4a2713aSLionel Sambuc }
608f4a2713aSLionel Sambuc
609f4a2713aSLionel Sambuc return true;
610f4a2713aSLionel Sambuc }
611f4a2713aSLionel Sambuc
612f4a2713aSLionel Sambuc /// isProfitableToConv3Addr - Return true if it is profitable to convert the
613f4a2713aSLionel Sambuc /// given 2-address instruction to a 3-address one.
614f4a2713aSLionel Sambuc bool
isProfitableToConv3Addr(unsigned RegA,unsigned RegB)615f4a2713aSLionel Sambuc TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA,unsigned RegB){
616f4a2713aSLionel Sambuc // Look for situations like this:
617f4a2713aSLionel Sambuc // %reg1024<def> = MOV r1
618f4a2713aSLionel Sambuc // %reg1025<def> = MOV r0
619f4a2713aSLionel Sambuc // %reg1026<def> = ADD %reg1024, %reg1025
620f4a2713aSLionel Sambuc // r2 = MOV %reg1026
621f4a2713aSLionel Sambuc // Turn ADD into a 3-address instruction to avoid a copy.
622f4a2713aSLionel Sambuc unsigned FromRegB = getMappedReg(RegB, SrcRegMap);
623f4a2713aSLionel Sambuc if (!FromRegB)
624f4a2713aSLionel Sambuc return false;
625f4a2713aSLionel Sambuc unsigned ToRegA = getMappedReg(RegA, DstRegMap);
626f4a2713aSLionel Sambuc return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI));
627f4a2713aSLionel Sambuc }
628f4a2713aSLionel Sambuc
629f4a2713aSLionel Sambuc /// convertInstTo3Addr - Convert the specified two-address instruction into a
630f4a2713aSLionel Sambuc /// three address one. Return true if this transformation was successful.
631f4a2713aSLionel Sambuc bool
convertInstTo3Addr(MachineBasicBlock::iterator & mi,MachineBasicBlock::iterator & nmi,unsigned RegA,unsigned RegB,unsigned Dist)632f4a2713aSLionel Sambuc TwoAddressInstructionPass::convertInstTo3Addr(MachineBasicBlock::iterator &mi,
633f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
634f4a2713aSLionel Sambuc unsigned RegA, unsigned RegB,
635f4a2713aSLionel Sambuc unsigned Dist) {
636f4a2713aSLionel Sambuc // FIXME: Why does convertToThreeAddress() need an iterator reference?
637f4a2713aSLionel Sambuc MachineFunction::iterator MFI = MBB;
638f4a2713aSLionel Sambuc MachineInstr *NewMI = TII->convertToThreeAddress(MFI, mi, LV);
639f4a2713aSLionel Sambuc assert(MBB == MFI && "convertToThreeAddress changed iterator reference");
640f4a2713aSLionel Sambuc if (!NewMI)
641f4a2713aSLionel Sambuc return false;
642f4a2713aSLionel Sambuc
643f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
644f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
645f4a2713aSLionel Sambuc bool Sunk = false;
646f4a2713aSLionel Sambuc
647f4a2713aSLionel Sambuc if (LIS)
648f4a2713aSLionel Sambuc LIS->ReplaceMachineInstrInMaps(mi, NewMI);
649f4a2713aSLionel Sambuc
650f4a2713aSLionel Sambuc if (NewMI->findRegisterUseOperand(RegB, false, TRI))
651f4a2713aSLionel Sambuc // FIXME: Temporary workaround. If the new instruction doesn't
652f4a2713aSLionel Sambuc // uses RegB, convertToThreeAddress must have created more
653f4a2713aSLionel Sambuc // then one instruction.
654f4a2713aSLionel Sambuc Sunk = sink3AddrInstruction(NewMI, RegB, mi);
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc MBB->erase(mi); // Nuke the old inst.
657f4a2713aSLionel Sambuc
658f4a2713aSLionel Sambuc if (!Sunk) {
659f4a2713aSLionel Sambuc DistanceMap.insert(std::make_pair(NewMI, Dist));
660f4a2713aSLionel Sambuc mi = NewMI;
661*0a6a1f1dSLionel Sambuc nmi = std::next(mi);
662f4a2713aSLionel Sambuc }
663f4a2713aSLionel Sambuc
664f4a2713aSLionel Sambuc // Update source and destination register maps.
665f4a2713aSLionel Sambuc SrcRegMap.erase(RegA);
666f4a2713aSLionel Sambuc DstRegMap.erase(RegB);
667f4a2713aSLionel Sambuc return true;
668f4a2713aSLionel Sambuc }
669f4a2713aSLionel Sambuc
670f4a2713aSLionel Sambuc /// scanUses - Scan forward recursively for only uses, update maps if the use
671f4a2713aSLionel Sambuc /// is a copy or a two-address instruction.
672f4a2713aSLionel Sambuc void
scanUses(unsigned DstReg)673f4a2713aSLionel Sambuc TwoAddressInstructionPass::scanUses(unsigned DstReg) {
674f4a2713aSLionel Sambuc SmallVector<unsigned, 4> VirtRegPairs;
675f4a2713aSLionel Sambuc bool IsDstPhys;
676f4a2713aSLionel Sambuc bool IsCopy = false;
677f4a2713aSLionel Sambuc unsigned NewReg = 0;
678f4a2713aSLionel Sambuc unsigned Reg = DstReg;
679f4a2713aSLionel Sambuc while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy,
680f4a2713aSLionel Sambuc NewReg, IsDstPhys)) {
681*0a6a1f1dSLionel Sambuc if (IsCopy && !Processed.insert(UseMI).second)
682f4a2713aSLionel Sambuc break;
683f4a2713aSLionel Sambuc
684f4a2713aSLionel Sambuc DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
685f4a2713aSLionel Sambuc if (DI != DistanceMap.end())
686f4a2713aSLionel Sambuc // Earlier in the same MBB.Reached via a back edge.
687f4a2713aSLionel Sambuc break;
688f4a2713aSLionel Sambuc
689f4a2713aSLionel Sambuc if (IsDstPhys) {
690f4a2713aSLionel Sambuc VirtRegPairs.push_back(NewReg);
691f4a2713aSLionel Sambuc break;
692f4a2713aSLionel Sambuc }
693f4a2713aSLionel Sambuc bool isNew = SrcRegMap.insert(std::make_pair(NewReg, Reg)).second;
694f4a2713aSLionel Sambuc if (!isNew)
695f4a2713aSLionel Sambuc assert(SrcRegMap[NewReg] == Reg && "Can't map to two src registers!");
696f4a2713aSLionel Sambuc VirtRegPairs.push_back(NewReg);
697f4a2713aSLionel Sambuc Reg = NewReg;
698f4a2713aSLionel Sambuc }
699f4a2713aSLionel Sambuc
700f4a2713aSLionel Sambuc if (!VirtRegPairs.empty()) {
701f4a2713aSLionel Sambuc unsigned ToReg = VirtRegPairs.back();
702f4a2713aSLionel Sambuc VirtRegPairs.pop_back();
703f4a2713aSLionel Sambuc while (!VirtRegPairs.empty()) {
704f4a2713aSLionel Sambuc unsigned FromReg = VirtRegPairs.back();
705f4a2713aSLionel Sambuc VirtRegPairs.pop_back();
706f4a2713aSLionel Sambuc bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
707f4a2713aSLionel Sambuc if (!isNew)
708f4a2713aSLionel Sambuc assert(DstRegMap[FromReg] == ToReg &&"Can't map to two dst registers!");
709f4a2713aSLionel Sambuc ToReg = FromReg;
710f4a2713aSLionel Sambuc }
711f4a2713aSLionel Sambuc bool isNew = DstRegMap.insert(std::make_pair(DstReg, ToReg)).second;
712f4a2713aSLionel Sambuc if (!isNew)
713f4a2713aSLionel Sambuc assert(DstRegMap[DstReg] == ToReg && "Can't map to two dst registers!");
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc }
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc /// processCopy - If the specified instruction is not yet processed, process it
718f4a2713aSLionel Sambuc /// if it's a copy. For a copy instruction, we find the physical registers the
719f4a2713aSLionel Sambuc /// source and destination registers might be mapped to. These are kept in
720f4a2713aSLionel Sambuc /// point-to maps used to determine future optimizations. e.g.
721f4a2713aSLionel Sambuc /// v1024 = mov r0
722f4a2713aSLionel Sambuc /// v1025 = mov r1
723f4a2713aSLionel Sambuc /// v1026 = add v1024, v1025
724f4a2713aSLionel Sambuc /// r1 = mov r1026
725f4a2713aSLionel Sambuc /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
726f4a2713aSLionel Sambuc /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
727f4a2713aSLionel Sambuc /// potentially joined with r1 on the output side. It's worthwhile to commute
728f4a2713aSLionel Sambuc /// 'add' to eliminate a copy.
processCopy(MachineInstr * MI)729f4a2713aSLionel Sambuc void TwoAddressInstructionPass::processCopy(MachineInstr *MI) {
730f4a2713aSLionel Sambuc if (Processed.count(MI))
731f4a2713aSLionel Sambuc return;
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc bool IsSrcPhys, IsDstPhys;
734f4a2713aSLionel Sambuc unsigned SrcReg, DstReg;
735f4a2713aSLionel Sambuc if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
736f4a2713aSLionel Sambuc return;
737f4a2713aSLionel Sambuc
738f4a2713aSLionel Sambuc if (IsDstPhys && !IsSrcPhys)
739f4a2713aSLionel Sambuc DstRegMap.insert(std::make_pair(SrcReg, DstReg));
740f4a2713aSLionel Sambuc else if (!IsDstPhys && IsSrcPhys) {
741f4a2713aSLionel Sambuc bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
742f4a2713aSLionel Sambuc if (!isNew)
743f4a2713aSLionel Sambuc assert(SrcRegMap[DstReg] == SrcReg &&
744f4a2713aSLionel Sambuc "Can't map to two src physical registers!");
745f4a2713aSLionel Sambuc
746f4a2713aSLionel Sambuc scanUses(DstReg);
747f4a2713aSLionel Sambuc }
748f4a2713aSLionel Sambuc
749f4a2713aSLionel Sambuc Processed.insert(MI);
750f4a2713aSLionel Sambuc return;
751f4a2713aSLionel Sambuc }
752f4a2713aSLionel Sambuc
753f4a2713aSLionel Sambuc /// rescheduleMIBelowKill - If there is one more local instruction that reads
754f4a2713aSLionel Sambuc /// 'Reg' and it kills 'Reg, consider moving the instruction below the kill
755f4a2713aSLionel Sambuc /// instruction in order to eliminate the need for the copy.
756f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
rescheduleMIBelowKill(MachineBasicBlock::iterator & mi,MachineBasicBlock::iterator & nmi,unsigned Reg)757f4a2713aSLionel Sambuc rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
758f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
759f4a2713aSLionel Sambuc unsigned Reg) {
760f4a2713aSLionel Sambuc // Bail immediately if we don't have LV or LIS available. We use them to find
761f4a2713aSLionel Sambuc // kills efficiently.
762f4a2713aSLionel Sambuc if (!LV && !LIS)
763f4a2713aSLionel Sambuc return false;
764f4a2713aSLionel Sambuc
765f4a2713aSLionel Sambuc MachineInstr *MI = &*mi;
766f4a2713aSLionel Sambuc DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
767f4a2713aSLionel Sambuc if (DI == DistanceMap.end())
768f4a2713aSLionel Sambuc // Must be created from unfolded load. Don't waste time trying this.
769f4a2713aSLionel Sambuc return false;
770f4a2713aSLionel Sambuc
771*0a6a1f1dSLionel Sambuc MachineInstr *KillMI = nullptr;
772f4a2713aSLionel Sambuc if (LIS) {
773f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(Reg);
774f4a2713aSLionel Sambuc assert(LI.end() != LI.begin() &&
775f4a2713aSLionel Sambuc "Reg should not have empty live interval.");
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
778f4a2713aSLionel Sambuc LiveInterval::const_iterator I = LI.find(MBBEndIdx);
779f4a2713aSLionel Sambuc if (I != LI.end() && I->start < MBBEndIdx)
780f4a2713aSLionel Sambuc return false;
781f4a2713aSLionel Sambuc
782f4a2713aSLionel Sambuc --I;
783f4a2713aSLionel Sambuc KillMI = LIS->getInstructionFromIndex(I->end);
784f4a2713aSLionel Sambuc } else {
785f4a2713aSLionel Sambuc KillMI = LV->getVarInfo(Reg).findKill(MBB);
786f4a2713aSLionel Sambuc }
787f4a2713aSLionel Sambuc if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
788f4a2713aSLionel Sambuc // Don't mess with copies, they may be coalesced later.
789f4a2713aSLionel Sambuc return false;
790f4a2713aSLionel Sambuc
791f4a2713aSLionel Sambuc if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() ||
792f4a2713aSLionel Sambuc KillMI->isBranch() || KillMI->isTerminator())
793f4a2713aSLionel Sambuc // Don't move pass calls, etc.
794f4a2713aSLionel Sambuc return false;
795f4a2713aSLionel Sambuc
796f4a2713aSLionel Sambuc unsigned DstReg;
797f4a2713aSLionel Sambuc if (isTwoAddrUse(*KillMI, Reg, DstReg))
798f4a2713aSLionel Sambuc return false;
799f4a2713aSLionel Sambuc
800f4a2713aSLionel Sambuc bool SeenStore = true;
801f4a2713aSLionel Sambuc if (!MI->isSafeToMove(TII, AA, SeenStore))
802f4a2713aSLionel Sambuc return false;
803f4a2713aSLionel Sambuc
804f4a2713aSLionel Sambuc if (TII->getInstrLatency(InstrItins, MI) > 1)
805f4a2713aSLionel Sambuc // FIXME: Needs more sophisticated heuristics.
806f4a2713aSLionel Sambuc return false;
807f4a2713aSLionel Sambuc
808f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Uses;
809f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Kills;
810f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Defs;
811f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
812f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
813f4a2713aSLionel Sambuc if (!MO.isReg())
814f4a2713aSLionel Sambuc continue;
815f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
816f4a2713aSLionel Sambuc if (!MOReg)
817f4a2713aSLionel Sambuc continue;
818f4a2713aSLionel Sambuc if (MO.isDef())
819f4a2713aSLionel Sambuc Defs.insert(MOReg);
820f4a2713aSLionel Sambuc else {
821f4a2713aSLionel Sambuc Uses.insert(MOReg);
822f4a2713aSLionel Sambuc if (MOReg != Reg && (MO.isKill() ||
823f4a2713aSLionel Sambuc (LIS && isPlainlyKilled(MI, MOReg, LIS))))
824f4a2713aSLionel Sambuc Kills.insert(MOReg);
825f4a2713aSLionel Sambuc }
826f4a2713aSLionel Sambuc }
827f4a2713aSLionel Sambuc
828f4a2713aSLionel Sambuc // Move the copies connected to MI down as well.
829f4a2713aSLionel Sambuc MachineBasicBlock::iterator Begin = MI;
830*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator AfterMI = std::next(Begin);
831f4a2713aSLionel Sambuc
832f4a2713aSLionel Sambuc MachineBasicBlock::iterator End = AfterMI;
833f4a2713aSLionel Sambuc while (End->isCopy() && Defs.count(End->getOperand(1).getReg())) {
834f4a2713aSLionel Sambuc Defs.insert(End->getOperand(0).getReg());
835f4a2713aSLionel Sambuc ++End;
836f4a2713aSLionel Sambuc }
837f4a2713aSLionel Sambuc
838f4a2713aSLionel Sambuc // Check if the reschedule will not break depedencies.
839f4a2713aSLionel Sambuc unsigned NumVisited = 0;
840f4a2713aSLionel Sambuc MachineBasicBlock::iterator KillPos = KillMI;
841f4a2713aSLionel Sambuc ++KillPos;
842f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator I = End; I != KillPos; ++I) {
843f4a2713aSLionel Sambuc MachineInstr *OtherMI = I;
844f4a2713aSLionel Sambuc // DBG_VALUE cannot be counted against the limit.
845f4a2713aSLionel Sambuc if (OtherMI->isDebugValue())
846f4a2713aSLionel Sambuc continue;
847f4a2713aSLionel Sambuc if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost.
848f4a2713aSLionel Sambuc return false;
849f4a2713aSLionel Sambuc ++NumVisited;
850f4a2713aSLionel Sambuc if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() ||
851f4a2713aSLionel Sambuc OtherMI->isBranch() || OtherMI->isTerminator())
852f4a2713aSLionel Sambuc // Don't move pass calls, etc.
853f4a2713aSLionel Sambuc return false;
854f4a2713aSLionel Sambuc for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
855f4a2713aSLionel Sambuc const MachineOperand &MO = OtherMI->getOperand(i);
856f4a2713aSLionel Sambuc if (!MO.isReg())
857f4a2713aSLionel Sambuc continue;
858f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
859f4a2713aSLionel Sambuc if (!MOReg)
860f4a2713aSLionel Sambuc continue;
861f4a2713aSLionel Sambuc if (MO.isDef()) {
862f4a2713aSLionel Sambuc if (Uses.count(MOReg))
863f4a2713aSLionel Sambuc // Physical register use would be clobbered.
864f4a2713aSLionel Sambuc return false;
865f4a2713aSLionel Sambuc if (!MO.isDead() && Defs.count(MOReg))
866f4a2713aSLionel Sambuc // May clobber a physical register def.
867f4a2713aSLionel Sambuc // FIXME: This may be too conservative. It's ok if the instruction
868f4a2713aSLionel Sambuc // is sunken completely below the use.
869f4a2713aSLionel Sambuc return false;
870f4a2713aSLionel Sambuc } else {
871f4a2713aSLionel Sambuc if (Defs.count(MOReg))
872f4a2713aSLionel Sambuc return false;
873f4a2713aSLionel Sambuc bool isKill = MO.isKill() ||
874f4a2713aSLionel Sambuc (LIS && isPlainlyKilled(OtherMI, MOReg, LIS));
875f4a2713aSLionel Sambuc if (MOReg != Reg &&
876f4a2713aSLionel Sambuc ((isKill && Uses.count(MOReg)) || Kills.count(MOReg)))
877f4a2713aSLionel Sambuc // Don't want to extend other live ranges and update kills.
878f4a2713aSLionel Sambuc return false;
879f4a2713aSLionel Sambuc if (MOReg == Reg && !isKill)
880f4a2713aSLionel Sambuc // We can't schedule across a use of the register in question.
881f4a2713aSLionel Sambuc return false;
882f4a2713aSLionel Sambuc // Ensure that if this is register in question, its the kill we expect.
883f4a2713aSLionel Sambuc assert((MOReg != Reg || OtherMI == KillMI) &&
884f4a2713aSLionel Sambuc "Found multiple kills of a register in a basic block");
885f4a2713aSLionel Sambuc }
886f4a2713aSLionel Sambuc }
887f4a2713aSLionel Sambuc }
888f4a2713aSLionel Sambuc
889f4a2713aSLionel Sambuc // Move debug info as well.
890*0a6a1f1dSLionel Sambuc while (Begin != MBB->begin() && std::prev(Begin)->isDebugValue())
891f4a2713aSLionel Sambuc --Begin;
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc nmi = End;
894f4a2713aSLionel Sambuc MachineBasicBlock::iterator InsertPos = KillPos;
895f4a2713aSLionel Sambuc if (LIS) {
896f4a2713aSLionel Sambuc // We have to move the copies first so that the MBB is still well-formed
897f4a2713aSLionel Sambuc // when calling handleMove().
898f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator MBBI = AfterMI; MBBI != End;) {
899f4a2713aSLionel Sambuc MachineInstr *CopyMI = MBBI;
900f4a2713aSLionel Sambuc ++MBBI;
901f4a2713aSLionel Sambuc MBB->splice(InsertPos, MBB, CopyMI);
902f4a2713aSLionel Sambuc LIS->handleMove(CopyMI);
903f4a2713aSLionel Sambuc InsertPos = CopyMI;
904f4a2713aSLionel Sambuc }
905*0a6a1f1dSLionel Sambuc End = std::next(MachineBasicBlock::iterator(MI));
906f4a2713aSLionel Sambuc }
907f4a2713aSLionel Sambuc
908f4a2713aSLionel Sambuc // Copies following MI may have been moved as well.
909f4a2713aSLionel Sambuc MBB->splice(InsertPos, MBB, Begin, End);
910f4a2713aSLionel Sambuc DistanceMap.erase(DI);
911f4a2713aSLionel Sambuc
912f4a2713aSLionel Sambuc // Update live variables
913f4a2713aSLionel Sambuc if (LIS) {
914f4a2713aSLionel Sambuc LIS->handleMove(MI);
915f4a2713aSLionel Sambuc } else {
916f4a2713aSLionel Sambuc LV->removeVirtualRegisterKilled(Reg, KillMI);
917f4a2713aSLionel Sambuc LV->addVirtualRegisterKilled(Reg, MI);
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc DEBUG(dbgs() << "\trescheduled below kill: " << *KillMI);
921f4a2713aSLionel Sambuc return true;
922f4a2713aSLionel Sambuc }
923f4a2713aSLionel Sambuc
924f4a2713aSLionel Sambuc /// isDefTooClose - Return true if the re-scheduling will put the given
925f4a2713aSLionel Sambuc /// instruction too close to the defs of its register dependencies.
isDefTooClose(unsigned Reg,unsigned Dist,MachineInstr * MI)926f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::isDefTooClose(unsigned Reg, unsigned Dist,
927f4a2713aSLionel Sambuc MachineInstr *MI) {
928*0a6a1f1dSLionel Sambuc for (MachineInstr &DefMI : MRI->def_instructions(Reg)) {
929*0a6a1f1dSLionel Sambuc if (DefMI.getParent() != MBB || DefMI.isCopy() || DefMI.isCopyLike())
930f4a2713aSLionel Sambuc continue;
931*0a6a1f1dSLionel Sambuc if (&DefMI == MI)
932f4a2713aSLionel Sambuc return true; // MI is defining something KillMI uses
933*0a6a1f1dSLionel Sambuc DenseMap<MachineInstr*, unsigned>::iterator DDI = DistanceMap.find(&DefMI);
934f4a2713aSLionel Sambuc if (DDI == DistanceMap.end())
935f4a2713aSLionel Sambuc return true; // Below MI
936f4a2713aSLionel Sambuc unsigned DefDist = DDI->second;
937f4a2713aSLionel Sambuc assert(Dist > DefDist && "Visited def already?");
938*0a6a1f1dSLionel Sambuc if (TII->getInstrLatency(InstrItins, &DefMI) > (Dist - DefDist))
939f4a2713aSLionel Sambuc return true;
940f4a2713aSLionel Sambuc }
941f4a2713aSLionel Sambuc return false;
942f4a2713aSLionel Sambuc }
943f4a2713aSLionel Sambuc
944f4a2713aSLionel Sambuc /// rescheduleKillAboveMI - If there is one more local instruction that reads
945f4a2713aSLionel Sambuc /// 'Reg' and it kills 'Reg, consider moving the kill instruction above the
946f4a2713aSLionel Sambuc /// current two-address instruction in order to eliminate the need for the
947f4a2713aSLionel Sambuc /// copy.
948f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
rescheduleKillAboveMI(MachineBasicBlock::iterator & mi,MachineBasicBlock::iterator & nmi,unsigned Reg)949f4a2713aSLionel Sambuc rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
950f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
951f4a2713aSLionel Sambuc unsigned Reg) {
952f4a2713aSLionel Sambuc // Bail immediately if we don't have LV or LIS available. We use them to find
953f4a2713aSLionel Sambuc // kills efficiently.
954f4a2713aSLionel Sambuc if (!LV && !LIS)
955f4a2713aSLionel Sambuc return false;
956f4a2713aSLionel Sambuc
957f4a2713aSLionel Sambuc MachineInstr *MI = &*mi;
958f4a2713aSLionel Sambuc DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
959f4a2713aSLionel Sambuc if (DI == DistanceMap.end())
960f4a2713aSLionel Sambuc // Must be created from unfolded load. Don't waste time trying this.
961f4a2713aSLionel Sambuc return false;
962f4a2713aSLionel Sambuc
963*0a6a1f1dSLionel Sambuc MachineInstr *KillMI = nullptr;
964f4a2713aSLionel Sambuc if (LIS) {
965f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(Reg);
966f4a2713aSLionel Sambuc assert(LI.end() != LI.begin() &&
967f4a2713aSLionel Sambuc "Reg should not have empty live interval.");
968f4a2713aSLionel Sambuc
969f4a2713aSLionel Sambuc SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
970f4a2713aSLionel Sambuc LiveInterval::const_iterator I = LI.find(MBBEndIdx);
971f4a2713aSLionel Sambuc if (I != LI.end() && I->start < MBBEndIdx)
972f4a2713aSLionel Sambuc return false;
973f4a2713aSLionel Sambuc
974f4a2713aSLionel Sambuc --I;
975f4a2713aSLionel Sambuc KillMI = LIS->getInstructionFromIndex(I->end);
976f4a2713aSLionel Sambuc } else {
977f4a2713aSLionel Sambuc KillMI = LV->getVarInfo(Reg).findKill(MBB);
978f4a2713aSLionel Sambuc }
979f4a2713aSLionel Sambuc if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
980f4a2713aSLionel Sambuc // Don't mess with copies, they may be coalesced later.
981f4a2713aSLionel Sambuc return false;
982f4a2713aSLionel Sambuc
983f4a2713aSLionel Sambuc unsigned DstReg;
984f4a2713aSLionel Sambuc if (isTwoAddrUse(*KillMI, Reg, DstReg))
985f4a2713aSLionel Sambuc return false;
986f4a2713aSLionel Sambuc
987f4a2713aSLionel Sambuc bool SeenStore = true;
988f4a2713aSLionel Sambuc if (!KillMI->isSafeToMove(TII, AA, SeenStore))
989f4a2713aSLionel Sambuc return false;
990f4a2713aSLionel Sambuc
991f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Uses;
992f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Kills;
993f4a2713aSLionel Sambuc SmallSet<unsigned, 2> Defs;
994f4a2713aSLionel Sambuc SmallSet<unsigned, 2> LiveDefs;
995f4a2713aSLionel Sambuc for (unsigned i = 0, e = KillMI->getNumOperands(); i != e; ++i) {
996f4a2713aSLionel Sambuc const MachineOperand &MO = KillMI->getOperand(i);
997f4a2713aSLionel Sambuc if (!MO.isReg())
998f4a2713aSLionel Sambuc continue;
999f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
1000f4a2713aSLionel Sambuc if (MO.isUse()) {
1001f4a2713aSLionel Sambuc if (!MOReg)
1002f4a2713aSLionel Sambuc continue;
1003f4a2713aSLionel Sambuc if (isDefTooClose(MOReg, DI->second, MI))
1004f4a2713aSLionel Sambuc return false;
1005f4a2713aSLionel Sambuc bool isKill = MO.isKill() || (LIS && isPlainlyKilled(KillMI, MOReg, LIS));
1006f4a2713aSLionel Sambuc if (MOReg == Reg && !isKill)
1007f4a2713aSLionel Sambuc return false;
1008f4a2713aSLionel Sambuc Uses.insert(MOReg);
1009f4a2713aSLionel Sambuc if (isKill && MOReg != Reg)
1010f4a2713aSLionel Sambuc Kills.insert(MOReg);
1011f4a2713aSLionel Sambuc } else if (TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1012f4a2713aSLionel Sambuc Defs.insert(MOReg);
1013f4a2713aSLionel Sambuc if (!MO.isDead())
1014f4a2713aSLionel Sambuc LiveDefs.insert(MOReg);
1015f4a2713aSLionel Sambuc }
1016f4a2713aSLionel Sambuc }
1017f4a2713aSLionel Sambuc
1018f4a2713aSLionel Sambuc // Check if the reschedule will not break depedencies.
1019f4a2713aSLionel Sambuc unsigned NumVisited = 0;
1020f4a2713aSLionel Sambuc MachineBasicBlock::iterator KillPos = KillMI;
1021f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator I = mi; I != KillPos; ++I) {
1022f4a2713aSLionel Sambuc MachineInstr *OtherMI = I;
1023f4a2713aSLionel Sambuc // DBG_VALUE cannot be counted against the limit.
1024f4a2713aSLionel Sambuc if (OtherMI->isDebugValue())
1025f4a2713aSLionel Sambuc continue;
1026f4a2713aSLionel Sambuc if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost.
1027f4a2713aSLionel Sambuc return false;
1028f4a2713aSLionel Sambuc ++NumVisited;
1029f4a2713aSLionel Sambuc if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() ||
1030f4a2713aSLionel Sambuc OtherMI->isBranch() || OtherMI->isTerminator())
1031f4a2713aSLionel Sambuc // Don't move pass calls, etc.
1032f4a2713aSLionel Sambuc return false;
1033f4a2713aSLionel Sambuc SmallVector<unsigned, 2> OtherDefs;
1034f4a2713aSLionel Sambuc for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
1035f4a2713aSLionel Sambuc const MachineOperand &MO = OtherMI->getOperand(i);
1036f4a2713aSLionel Sambuc if (!MO.isReg())
1037f4a2713aSLionel Sambuc continue;
1038f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
1039f4a2713aSLionel Sambuc if (!MOReg)
1040f4a2713aSLionel Sambuc continue;
1041f4a2713aSLionel Sambuc if (MO.isUse()) {
1042f4a2713aSLionel Sambuc if (Defs.count(MOReg))
1043f4a2713aSLionel Sambuc // Moving KillMI can clobber the physical register if the def has
1044f4a2713aSLionel Sambuc // not been seen.
1045f4a2713aSLionel Sambuc return false;
1046f4a2713aSLionel Sambuc if (Kills.count(MOReg))
1047f4a2713aSLionel Sambuc // Don't want to extend other live ranges and update kills.
1048f4a2713aSLionel Sambuc return false;
1049f4a2713aSLionel Sambuc if (OtherMI != MI && MOReg == Reg &&
1050f4a2713aSLionel Sambuc !(MO.isKill() || (LIS && isPlainlyKilled(OtherMI, MOReg, LIS))))
1051f4a2713aSLionel Sambuc // We can't schedule across a use of the register in question.
1052f4a2713aSLionel Sambuc return false;
1053f4a2713aSLionel Sambuc } else {
1054f4a2713aSLionel Sambuc OtherDefs.push_back(MOReg);
1055f4a2713aSLionel Sambuc }
1056f4a2713aSLionel Sambuc }
1057f4a2713aSLionel Sambuc
1058f4a2713aSLionel Sambuc for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) {
1059f4a2713aSLionel Sambuc unsigned MOReg = OtherDefs[i];
1060f4a2713aSLionel Sambuc if (Uses.count(MOReg))
1061f4a2713aSLionel Sambuc return false;
1062f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1063f4a2713aSLionel Sambuc LiveDefs.count(MOReg))
1064f4a2713aSLionel Sambuc return false;
1065f4a2713aSLionel Sambuc // Physical register def is seen.
1066f4a2713aSLionel Sambuc Defs.erase(MOReg);
1067f4a2713aSLionel Sambuc }
1068f4a2713aSLionel Sambuc }
1069f4a2713aSLionel Sambuc
1070f4a2713aSLionel Sambuc // Move the old kill above MI, don't forget to move debug info as well.
1071f4a2713aSLionel Sambuc MachineBasicBlock::iterator InsertPos = mi;
1072*0a6a1f1dSLionel Sambuc while (InsertPos != MBB->begin() && std::prev(InsertPos)->isDebugValue())
1073f4a2713aSLionel Sambuc --InsertPos;
1074f4a2713aSLionel Sambuc MachineBasicBlock::iterator From = KillMI;
1075*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator To = std::next(From);
1076*0a6a1f1dSLionel Sambuc while (std::prev(From)->isDebugValue())
1077f4a2713aSLionel Sambuc --From;
1078f4a2713aSLionel Sambuc MBB->splice(InsertPos, MBB, From, To);
1079f4a2713aSLionel Sambuc
1080*0a6a1f1dSLionel Sambuc nmi = std::prev(InsertPos); // Backtrack so we process the moved instr.
1081f4a2713aSLionel Sambuc DistanceMap.erase(DI);
1082f4a2713aSLionel Sambuc
1083f4a2713aSLionel Sambuc // Update live variables
1084f4a2713aSLionel Sambuc if (LIS) {
1085f4a2713aSLionel Sambuc LIS->handleMove(KillMI);
1086f4a2713aSLionel Sambuc } else {
1087f4a2713aSLionel Sambuc LV->removeVirtualRegisterKilled(Reg, KillMI);
1088f4a2713aSLionel Sambuc LV->addVirtualRegisterKilled(Reg, MI);
1089f4a2713aSLionel Sambuc }
1090f4a2713aSLionel Sambuc
1091f4a2713aSLionel Sambuc DEBUG(dbgs() << "\trescheduled kill: " << *KillMI);
1092f4a2713aSLionel Sambuc return true;
1093f4a2713aSLionel Sambuc }
1094f4a2713aSLionel Sambuc
1095f4a2713aSLionel Sambuc /// tryInstructionTransform - For the case where an instruction has a single
1096f4a2713aSLionel Sambuc /// pair of tied register operands, attempt some transformations that may
1097f4a2713aSLionel Sambuc /// either eliminate the tied operands or improve the opportunities for
1098f4a2713aSLionel Sambuc /// coalescing away the register copy. Returns true if no copy needs to be
1099f4a2713aSLionel Sambuc /// inserted to untie mi's operands (either because they were untied, or
1100f4a2713aSLionel Sambuc /// because mi was rescheduled, and will be visited again later). If the
1101f4a2713aSLionel Sambuc /// shouldOnlyCommute flag is true, only instruction commutation is attempted.
1102f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
tryInstructionTransform(MachineBasicBlock::iterator & mi,MachineBasicBlock::iterator & nmi,unsigned SrcIdx,unsigned DstIdx,unsigned Dist,bool shouldOnlyCommute)1103f4a2713aSLionel Sambuc tryInstructionTransform(MachineBasicBlock::iterator &mi,
1104f4a2713aSLionel Sambuc MachineBasicBlock::iterator &nmi,
1105f4a2713aSLionel Sambuc unsigned SrcIdx, unsigned DstIdx,
1106f4a2713aSLionel Sambuc unsigned Dist, bool shouldOnlyCommute) {
1107f4a2713aSLionel Sambuc if (OptLevel == CodeGenOpt::None)
1108f4a2713aSLionel Sambuc return false;
1109f4a2713aSLionel Sambuc
1110f4a2713aSLionel Sambuc MachineInstr &MI = *mi;
1111f4a2713aSLionel Sambuc unsigned regA = MI.getOperand(DstIdx).getReg();
1112f4a2713aSLionel Sambuc unsigned regB = MI.getOperand(SrcIdx).getReg();
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isVirtualRegister(regB) &&
1115f4a2713aSLionel Sambuc "cannot make instruction into two-address form");
1116f4a2713aSLionel Sambuc bool regBKilled = isKilled(MI, regB, MRI, TII, LIS, true);
1117f4a2713aSLionel Sambuc
1118f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(regA))
1119f4a2713aSLionel Sambuc scanUses(regA);
1120f4a2713aSLionel Sambuc
1121f4a2713aSLionel Sambuc // Check if it is profitable to commute the operands.
1122f4a2713aSLionel Sambuc unsigned SrcOp1, SrcOp2;
1123f4a2713aSLionel Sambuc unsigned regC = 0;
1124f4a2713aSLionel Sambuc unsigned regCIdx = ~0U;
1125f4a2713aSLionel Sambuc bool TryCommute = false;
1126f4a2713aSLionel Sambuc bool AggressiveCommute = false;
1127f4a2713aSLionel Sambuc if (MI.isCommutable() && MI.getNumOperands() >= 3 &&
1128f4a2713aSLionel Sambuc TII->findCommutedOpIndices(&MI, SrcOp1, SrcOp2)) {
1129f4a2713aSLionel Sambuc if (SrcIdx == SrcOp1)
1130f4a2713aSLionel Sambuc regCIdx = SrcOp2;
1131f4a2713aSLionel Sambuc else if (SrcIdx == SrcOp2)
1132f4a2713aSLionel Sambuc regCIdx = SrcOp1;
1133f4a2713aSLionel Sambuc
1134f4a2713aSLionel Sambuc if (regCIdx != ~0U) {
1135f4a2713aSLionel Sambuc regC = MI.getOperand(regCIdx).getReg();
1136f4a2713aSLionel Sambuc if (!regBKilled && isKilled(MI, regC, MRI, TII, LIS, false))
1137f4a2713aSLionel Sambuc // If C dies but B does not, swap the B and C operands.
1138f4a2713aSLionel Sambuc // This makes the live ranges of A and C joinable.
1139f4a2713aSLionel Sambuc TryCommute = true;
1140f4a2713aSLionel Sambuc else if (isProfitableToCommute(regA, regB, regC, &MI, Dist)) {
1141f4a2713aSLionel Sambuc TryCommute = true;
1142f4a2713aSLionel Sambuc AggressiveCommute = true;
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc }
1145f4a2713aSLionel Sambuc }
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc // If it's profitable to commute, try to do so.
1148f4a2713aSLionel Sambuc if (TryCommute && commuteInstruction(mi, regB, regC, Dist)) {
1149f4a2713aSLionel Sambuc ++NumCommuted;
1150f4a2713aSLionel Sambuc if (AggressiveCommute)
1151f4a2713aSLionel Sambuc ++NumAggrCommuted;
1152f4a2713aSLionel Sambuc return false;
1153f4a2713aSLionel Sambuc }
1154f4a2713aSLionel Sambuc
1155f4a2713aSLionel Sambuc if (shouldOnlyCommute)
1156f4a2713aSLionel Sambuc return false;
1157f4a2713aSLionel Sambuc
1158f4a2713aSLionel Sambuc // If there is one more use of regB later in the same MBB, consider
1159f4a2713aSLionel Sambuc // re-schedule this MI below it.
1160f4a2713aSLionel Sambuc if (EnableRescheduling && rescheduleMIBelowKill(mi, nmi, regB)) {
1161f4a2713aSLionel Sambuc ++NumReSchedDowns;
1162f4a2713aSLionel Sambuc return true;
1163f4a2713aSLionel Sambuc }
1164f4a2713aSLionel Sambuc
1165f4a2713aSLionel Sambuc if (MI.isConvertibleTo3Addr()) {
1166f4a2713aSLionel Sambuc // This instruction is potentially convertible to a true
1167f4a2713aSLionel Sambuc // three-address instruction. Check if it is profitable.
1168f4a2713aSLionel Sambuc if (!regBKilled || isProfitableToConv3Addr(regA, regB)) {
1169f4a2713aSLionel Sambuc // Try to convert it.
1170f4a2713aSLionel Sambuc if (convertInstTo3Addr(mi, nmi, regA, regB, Dist)) {
1171f4a2713aSLionel Sambuc ++NumConvertedTo3Addr;
1172f4a2713aSLionel Sambuc return true; // Done with this instruction.
1173f4a2713aSLionel Sambuc }
1174f4a2713aSLionel Sambuc }
1175f4a2713aSLionel Sambuc }
1176f4a2713aSLionel Sambuc
1177f4a2713aSLionel Sambuc // If there is one more use of regB later in the same MBB, consider
1178f4a2713aSLionel Sambuc // re-schedule it before this MI if it's legal.
1179f4a2713aSLionel Sambuc if (EnableRescheduling && rescheduleKillAboveMI(mi, nmi, regB)) {
1180f4a2713aSLionel Sambuc ++NumReSchedUps;
1181f4a2713aSLionel Sambuc return true;
1182f4a2713aSLionel Sambuc }
1183f4a2713aSLionel Sambuc
1184f4a2713aSLionel Sambuc // If this is an instruction with a load folded into it, try unfolding
1185f4a2713aSLionel Sambuc // the load, e.g. avoid this:
1186f4a2713aSLionel Sambuc // movq %rdx, %rcx
1187f4a2713aSLionel Sambuc // addq (%rax), %rcx
1188f4a2713aSLionel Sambuc // in favor of this:
1189f4a2713aSLionel Sambuc // movq (%rax), %rcx
1190f4a2713aSLionel Sambuc // addq %rdx, %rcx
1191f4a2713aSLionel Sambuc // because it's preferable to schedule a load than a register copy.
1192f4a2713aSLionel Sambuc if (MI.mayLoad() && !regBKilled) {
1193f4a2713aSLionel Sambuc // Determine if a load can be unfolded.
1194f4a2713aSLionel Sambuc unsigned LoadRegIndex;
1195f4a2713aSLionel Sambuc unsigned NewOpc =
1196f4a2713aSLionel Sambuc TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
1197f4a2713aSLionel Sambuc /*UnfoldLoad=*/true,
1198f4a2713aSLionel Sambuc /*UnfoldStore=*/false,
1199f4a2713aSLionel Sambuc &LoadRegIndex);
1200f4a2713aSLionel Sambuc if (NewOpc != 0) {
1201f4a2713aSLionel Sambuc const MCInstrDesc &UnfoldMCID = TII->get(NewOpc);
1202f4a2713aSLionel Sambuc if (UnfoldMCID.getNumDefs() == 1) {
1203f4a2713aSLionel Sambuc // Unfold the load.
1204f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: UNFOLDING: " << MI);
1205f4a2713aSLionel Sambuc const TargetRegisterClass *RC =
1206f4a2713aSLionel Sambuc TRI->getAllocatableClass(
1207f4a2713aSLionel Sambuc TII->getRegClass(UnfoldMCID, LoadRegIndex, TRI, *MF));
1208f4a2713aSLionel Sambuc unsigned Reg = MRI->createVirtualRegister(RC);
1209f4a2713aSLionel Sambuc SmallVector<MachineInstr *, 2> NewMIs;
1210f4a2713aSLionel Sambuc if (!TII->unfoldMemoryOperand(*MF, &MI, Reg,
1211f4a2713aSLionel Sambuc /*UnfoldLoad=*/true,/*UnfoldStore=*/false,
1212f4a2713aSLionel Sambuc NewMIs)) {
1213f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
1214f4a2713aSLionel Sambuc return false;
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc assert(NewMIs.size() == 2 &&
1217f4a2713aSLionel Sambuc "Unfolded a load into multiple instructions!");
1218f4a2713aSLionel Sambuc // The load was previously folded, so this is the only use.
1219f4a2713aSLionel Sambuc NewMIs[1]->addRegisterKilled(Reg, TRI);
1220f4a2713aSLionel Sambuc
1221f4a2713aSLionel Sambuc // Tentatively insert the instructions into the block so that they
1222f4a2713aSLionel Sambuc // look "normal" to the transformation logic.
1223f4a2713aSLionel Sambuc MBB->insert(mi, NewMIs[0]);
1224f4a2713aSLionel Sambuc MBB->insert(mi, NewMIs[1]);
1225f4a2713aSLionel Sambuc
1226f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0]
1227f4a2713aSLionel Sambuc << "2addr: NEW INST: " << *NewMIs[1]);
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc // Transform the instruction, now that it no longer has a load.
1230f4a2713aSLionel Sambuc unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
1231f4a2713aSLionel Sambuc unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
1232f4a2713aSLionel Sambuc MachineBasicBlock::iterator NewMI = NewMIs[1];
1233f4a2713aSLionel Sambuc bool TransformResult =
1234f4a2713aSLionel Sambuc tryInstructionTransform(NewMI, mi, NewSrcIdx, NewDstIdx, Dist, true);
1235f4a2713aSLionel Sambuc (void)TransformResult;
1236f4a2713aSLionel Sambuc assert(!TransformResult &&
1237f4a2713aSLionel Sambuc "tryInstructionTransform() should return false.");
1238f4a2713aSLionel Sambuc if (NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
1239f4a2713aSLionel Sambuc // Success, or at least we made an improvement. Keep the unfolded
1240f4a2713aSLionel Sambuc // instructions and discard the original.
1241f4a2713aSLionel Sambuc if (LV) {
1242f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1243f4a2713aSLionel Sambuc MachineOperand &MO = MI.getOperand(i);
1244f4a2713aSLionel Sambuc if (MO.isReg() &&
1245f4a2713aSLionel Sambuc TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
1246f4a2713aSLionel Sambuc if (MO.isUse()) {
1247f4a2713aSLionel Sambuc if (MO.isKill()) {
1248f4a2713aSLionel Sambuc if (NewMIs[0]->killsRegister(MO.getReg()))
1249f4a2713aSLionel Sambuc LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[0]);
1250f4a2713aSLionel Sambuc else {
1251f4a2713aSLionel Sambuc assert(NewMIs[1]->killsRegister(MO.getReg()) &&
1252f4a2713aSLionel Sambuc "Kill missing after load unfold!");
1253f4a2713aSLionel Sambuc LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[1]);
1254f4a2713aSLionel Sambuc }
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc } else if (LV->removeVirtualRegisterDead(MO.getReg(), &MI)) {
1257f4a2713aSLionel Sambuc if (NewMIs[1]->registerDefIsDead(MO.getReg()))
1258f4a2713aSLionel Sambuc LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]);
1259f4a2713aSLionel Sambuc else {
1260f4a2713aSLionel Sambuc assert(NewMIs[0]->registerDefIsDead(MO.getReg()) &&
1261f4a2713aSLionel Sambuc "Dead flag missing after load unfold!");
1262f4a2713aSLionel Sambuc LV->addVirtualRegisterDead(MO.getReg(), NewMIs[0]);
1263f4a2713aSLionel Sambuc }
1264f4a2713aSLionel Sambuc }
1265f4a2713aSLionel Sambuc }
1266f4a2713aSLionel Sambuc }
1267f4a2713aSLionel Sambuc LV->addVirtualRegisterKilled(Reg, NewMIs[1]);
1268f4a2713aSLionel Sambuc }
1269f4a2713aSLionel Sambuc
1270f4a2713aSLionel Sambuc SmallVector<unsigned, 4> OrigRegs;
1271f4a2713aSLionel Sambuc if (LIS) {
1272f4a2713aSLionel Sambuc for (MachineInstr::const_mop_iterator MOI = MI.operands_begin(),
1273f4a2713aSLionel Sambuc MOE = MI.operands_end(); MOI != MOE; ++MOI) {
1274f4a2713aSLionel Sambuc if (MOI->isReg())
1275f4a2713aSLionel Sambuc OrigRegs.push_back(MOI->getReg());
1276f4a2713aSLionel Sambuc }
1277f4a2713aSLionel Sambuc }
1278f4a2713aSLionel Sambuc
1279f4a2713aSLionel Sambuc MI.eraseFromParent();
1280f4a2713aSLionel Sambuc
1281f4a2713aSLionel Sambuc // Update LiveIntervals.
1282f4a2713aSLionel Sambuc if (LIS) {
1283f4a2713aSLionel Sambuc MachineBasicBlock::iterator Begin(NewMIs[0]);
1284f4a2713aSLionel Sambuc MachineBasicBlock::iterator End(NewMIs[1]);
1285f4a2713aSLionel Sambuc LIS->repairIntervalsInRange(MBB, Begin, End, OrigRegs);
1286f4a2713aSLionel Sambuc }
1287f4a2713aSLionel Sambuc
1288f4a2713aSLionel Sambuc mi = NewMIs[1];
1289f4a2713aSLionel Sambuc } else {
1290f4a2713aSLionel Sambuc // Transforming didn't eliminate the tie and didn't lead to an
1291f4a2713aSLionel Sambuc // improvement. Clean up the unfolded instructions and keep the
1292f4a2713aSLionel Sambuc // original.
1293f4a2713aSLionel Sambuc DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
1294f4a2713aSLionel Sambuc NewMIs[0]->eraseFromParent();
1295f4a2713aSLionel Sambuc NewMIs[1]->eraseFromParent();
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc }
1298f4a2713aSLionel Sambuc }
1299f4a2713aSLionel Sambuc }
1300f4a2713aSLionel Sambuc
1301f4a2713aSLionel Sambuc return false;
1302f4a2713aSLionel Sambuc }
1303f4a2713aSLionel Sambuc
1304f4a2713aSLionel Sambuc // Collect tied operands of MI that need to be handled.
1305f4a2713aSLionel Sambuc // Rewrite trivial cases immediately.
1306f4a2713aSLionel Sambuc // Return true if any tied operands where found, including the trivial ones.
1307f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::
collectTiedOperands(MachineInstr * MI,TiedOperandMap & TiedOperands)1308f4a2713aSLionel Sambuc collectTiedOperands(MachineInstr *MI, TiedOperandMap &TiedOperands) {
1309f4a2713aSLionel Sambuc const MCInstrDesc &MCID = MI->getDesc();
1310f4a2713aSLionel Sambuc bool AnyOps = false;
1311f4a2713aSLionel Sambuc unsigned NumOps = MI->getNumOperands();
1312f4a2713aSLionel Sambuc
1313f4a2713aSLionel Sambuc for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
1314f4a2713aSLionel Sambuc unsigned DstIdx = 0;
1315f4a2713aSLionel Sambuc if (!MI->isRegTiedToDefOperand(SrcIdx, &DstIdx))
1316f4a2713aSLionel Sambuc continue;
1317f4a2713aSLionel Sambuc AnyOps = true;
1318f4a2713aSLionel Sambuc MachineOperand &SrcMO = MI->getOperand(SrcIdx);
1319f4a2713aSLionel Sambuc MachineOperand &DstMO = MI->getOperand(DstIdx);
1320f4a2713aSLionel Sambuc unsigned SrcReg = SrcMO.getReg();
1321f4a2713aSLionel Sambuc unsigned DstReg = DstMO.getReg();
1322f4a2713aSLionel Sambuc // Tied constraint already satisfied?
1323f4a2713aSLionel Sambuc if (SrcReg == DstReg)
1324f4a2713aSLionel Sambuc continue;
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc assert(SrcReg && SrcMO.isUse() && "two address instruction invalid");
1327f4a2713aSLionel Sambuc
1328f4a2713aSLionel Sambuc // Deal with <undef> uses immediately - simply rewrite the src operand.
1329*0a6a1f1dSLionel Sambuc if (SrcMO.isUndef() && !DstMO.getSubReg()) {
1330f4a2713aSLionel Sambuc // Constrain the DstReg register class if required.
1331f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(DstReg))
1332f4a2713aSLionel Sambuc if (const TargetRegisterClass *RC = TII->getRegClass(MCID, SrcIdx,
1333f4a2713aSLionel Sambuc TRI, *MF))
1334f4a2713aSLionel Sambuc MRI->constrainRegClass(DstReg, RC);
1335f4a2713aSLionel Sambuc SrcMO.setReg(DstReg);
1336*0a6a1f1dSLionel Sambuc SrcMO.setSubReg(0);
1337f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\trewrite undef:\t" << *MI);
1338f4a2713aSLionel Sambuc continue;
1339f4a2713aSLionel Sambuc }
1340f4a2713aSLionel Sambuc TiedOperands[SrcReg].push_back(std::make_pair(SrcIdx, DstIdx));
1341f4a2713aSLionel Sambuc }
1342f4a2713aSLionel Sambuc return AnyOps;
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc // Process a list of tied MI operands that all use the same source register.
1346f4a2713aSLionel Sambuc // The tied pairs are of the form (SrcIdx, DstIdx).
1347f4a2713aSLionel Sambuc void
processTiedPairs(MachineInstr * MI,TiedPairList & TiedPairs,unsigned & Dist)1348f4a2713aSLionel Sambuc TwoAddressInstructionPass::processTiedPairs(MachineInstr *MI,
1349f4a2713aSLionel Sambuc TiedPairList &TiedPairs,
1350f4a2713aSLionel Sambuc unsigned &Dist) {
1351f4a2713aSLionel Sambuc bool IsEarlyClobber = false;
1352f4a2713aSLionel Sambuc for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
1353f4a2713aSLionel Sambuc const MachineOperand &DstMO = MI->getOperand(TiedPairs[tpi].second);
1354f4a2713aSLionel Sambuc IsEarlyClobber |= DstMO.isEarlyClobber();
1355f4a2713aSLionel Sambuc }
1356f4a2713aSLionel Sambuc
1357f4a2713aSLionel Sambuc bool RemovedKillFlag = false;
1358f4a2713aSLionel Sambuc bool AllUsesCopied = true;
1359f4a2713aSLionel Sambuc unsigned LastCopiedReg = 0;
1360f4a2713aSLionel Sambuc SlotIndex LastCopyIdx;
1361f4a2713aSLionel Sambuc unsigned RegB = 0;
1362*0a6a1f1dSLionel Sambuc unsigned SubRegB = 0;
1363f4a2713aSLionel Sambuc for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
1364f4a2713aSLionel Sambuc unsigned SrcIdx = TiedPairs[tpi].first;
1365f4a2713aSLionel Sambuc unsigned DstIdx = TiedPairs[tpi].second;
1366f4a2713aSLionel Sambuc
1367f4a2713aSLionel Sambuc const MachineOperand &DstMO = MI->getOperand(DstIdx);
1368f4a2713aSLionel Sambuc unsigned RegA = DstMO.getReg();
1369f4a2713aSLionel Sambuc
1370f4a2713aSLionel Sambuc // Grab RegB from the instruction because it may have changed if the
1371f4a2713aSLionel Sambuc // instruction was commuted.
1372f4a2713aSLionel Sambuc RegB = MI->getOperand(SrcIdx).getReg();
1373*0a6a1f1dSLionel Sambuc SubRegB = MI->getOperand(SrcIdx).getSubReg();
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc if (RegA == RegB) {
1376f4a2713aSLionel Sambuc // The register is tied to multiple destinations (or else we would
1377f4a2713aSLionel Sambuc // not have continued this far), but this use of the register
1378f4a2713aSLionel Sambuc // already matches the tied destination. Leave it.
1379f4a2713aSLionel Sambuc AllUsesCopied = false;
1380f4a2713aSLionel Sambuc continue;
1381f4a2713aSLionel Sambuc }
1382f4a2713aSLionel Sambuc LastCopiedReg = RegA;
1383f4a2713aSLionel Sambuc
1384f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isVirtualRegister(RegB) &&
1385f4a2713aSLionel Sambuc "cannot make instruction into two-address form");
1386f4a2713aSLionel Sambuc
1387f4a2713aSLionel Sambuc #ifndef NDEBUG
1388f4a2713aSLionel Sambuc // First, verify that we don't have a use of "a" in the instruction
1389f4a2713aSLionel Sambuc // (a = b + a for example) because our transformation will not
1390f4a2713aSLionel Sambuc // work. This should never occur because we are in SSA form.
1391f4a2713aSLionel Sambuc for (unsigned i = 0; i != MI->getNumOperands(); ++i)
1392f4a2713aSLionel Sambuc assert(i == DstIdx ||
1393f4a2713aSLionel Sambuc !MI->getOperand(i).isReg() ||
1394f4a2713aSLionel Sambuc MI->getOperand(i).getReg() != RegA);
1395f4a2713aSLionel Sambuc #endif
1396f4a2713aSLionel Sambuc
1397f4a2713aSLionel Sambuc // Emit a copy.
1398*0a6a1f1dSLionel Sambuc MachineInstrBuilder MIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1399*0a6a1f1dSLionel Sambuc TII->get(TargetOpcode::COPY), RegA);
1400*0a6a1f1dSLionel Sambuc // If this operand is folding a truncation, the truncation now moves to the
1401*0a6a1f1dSLionel Sambuc // copy so that the register classes remain valid for the operands.
1402*0a6a1f1dSLionel Sambuc MIB.addReg(RegB, 0, SubRegB);
1403*0a6a1f1dSLionel Sambuc const TargetRegisterClass *RC = MRI->getRegClass(RegB);
1404*0a6a1f1dSLionel Sambuc if (SubRegB) {
1405*0a6a1f1dSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(RegA)) {
1406*0a6a1f1dSLionel Sambuc assert(TRI->getMatchingSuperRegClass(RC, MRI->getRegClass(RegA),
1407*0a6a1f1dSLionel Sambuc SubRegB) &&
1408*0a6a1f1dSLionel Sambuc "tied subregister must be a truncation");
1409*0a6a1f1dSLionel Sambuc // The superreg class will not be used to constrain the subreg class.
1410*0a6a1f1dSLionel Sambuc RC = nullptr;
1411*0a6a1f1dSLionel Sambuc }
1412*0a6a1f1dSLionel Sambuc else {
1413*0a6a1f1dSLionel Sambuc assert(TRI->getMatchingSuperReg(RegA, SubRegB, MRI->getRegClass(RegB))
1414*0a6a1f1dSLionel Sambuc && "tied subregister must be a truncation");
1415*0a6a1f1dSLionel Sambuc }
1416*0a6a1f1dSLionel Sambuc }
1417f4a2713aSLionel Sambuc
1418f4a2713aSLionel Sambuc // Update DistanceMap.
1419f4a2713aSLionel Sambuc MachineBasicBlock::iterator PrevMI = MI;
1420f4a2713aSLionel Sambuc --PrevMI;
1421f4a2713aSLionel Sambuc DistanceMap.insert(std::make_pair(PrevMI, Dist));
1422f4a2713aSLionel Sambuc DistanceMap[MI] = ++Dist;
1423f4a2713aSLionel Sambuc
1424f4a2713aSLionel Sambuc if (LIS) {
1425f4a2713aSLionel Sambuc LastCopyIdx = LIS->InsertMachineInstrInMaps(PrevMI).getRegSlot();
1426f4a2713aSLionel Sambuc
1427f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(RegA)) {
1428f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(RegA);
1429f4a2713aSLionel Sambuc VNInfo *VNI = LI.getNextValue(LastCopyIdx, LIS->getVNInfoAllocator());
1430f4a2713aSLionel Sambuc SlotIndex endIdx =
1431f4a2713aSLionel Sambuc LIS->getInstructionIndex(MI).getRegSlot(IsEarlyClobber);
1432f4a2713aSLionel Sambuc LI.addSegment(LiveInterval::Segment(LastCopyIdx, endIdx, VNI));
1433f4a2713aSLionel Sambuc }
1434f4a2713aSLionel Sambuc }
1435f4a2713aSLionel Sambuc
1436*0a6a1f1dSLionel Sambuc DEBUG(dbgs() << "\t\tprepend:\t" << *MIB);
1437f4a2713aSLionel Sambuc
1438f4a2713aSLionel Sambuc MachineOperand &MO = MI->getOperand(SrcIdx);
1439f4a2713aSLionel Sambuc assert(MO.isReg() && MO.getReg() == RegB && MO.isUse() &&
1440f4a2713aSLionel Sambuc "inconsistent operand info for 2-reg pass");
1441f4a2713aSLionel Sambuc if (MO.isKill()) {
1442f4a2713aSLionel Sambuc MO.setIsKill(false);
1443f4a2713aSLionel Sambuc RemovedKillFlag = true;
1444f4a2713aSLionel Sambuc }
1445f4a2713aSLionel Sambuc
1446f4a2713aSLionel Sambuc // Make sure regA is a legal regclass for the SrcIdx operand.
1447f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(RegA) &&
1448f4a2713aSLionel Sambuc TargetRegisterInfo::isVirtualRegister(RegB))
1449*0a6a1f1dSLionel Sambuc MRI->constrainRegClass(RegA, RC);
1450f4a2713aSLionel Sambuc MO.setReg(RegA);
1451*0a6a1f1dSLionel Sambuc // The getMatchingSuper asserts guarantee that the register class projected
1452*0a6a1f1dSLionel Sambuc // by SubRegB is compatible with RegA with no subregister. So regardless of
1453*0a6a1f1dSLionel Sambuc // whether the dest oper writes a subreg, the source oper should not.
1454*0a6a1f1dSLionel Sambuc MO.setSubReg(0);
1455f4a2713aSLionel Sambuc
1456f4a2713aSLionel Sambuc // Propagate SrcRegMap.
1457f4a2713aSLionel Sambuc SrcRegMap[RegA] = RegB;
1458f4a2713aSLionel Sambuc }
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc
1461f4a2713aSLionel Sambuc if (AllUsesCopied) {
1462f4a2713aSLionel Sambuc if (!IsEarlyClobber) {
1463f4a2713aSLionel Sambuc // Replace other (un-tied) uses of regB with LastCopiedReg.
1464f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1465f4a2713aSLionel Sambuc MachineOperand &MO = MI->getOperand(i);
1466*0a6a1f1dSLionel Sambuc if (MO.isReg() && MO.getReg() == RegB && MO.getSubReg() == SubRegB &&
1467*0a6a1f1dSLionel Sambuc MO.isUse()) {
1468f4a2713aSLionel Sambuc if (MO.isKill()) {
1469f4a2713aSLionel Sambuc MO.setIsKill(false);
1470f4a2713aSLionel Sambuc RemovedKillFlag = true;
1471f4a2713aSLionel Sambuc }
1472f4a2713aSLionel Sambuc MO.setReg(LastCopiedReg);
1473*0a6a1f1dSLionel Sambuc MO.setSubReg(0);
1474f4a2713aSLionel Sambuc }
1475f4a2713aSLionel Sambuc }
1476f4a2713aSLionel Sambuc }
1477f4a2713aSLionel Sambuc
1478f4a2713aSLionel Sambuc // Update live variables for regB.
1479f4a2713aSLionel Sambuc if (RemovedKillFlag && LV && LV->getVarInfo(RegB).removeKill(MI)) {
1480f4a2713aSLionel Sambuc MachineBasicBlock::iterator PrevMI = MI;
1481f4a2713aSLionel Sambuc --PrevMI;
1482f4a2713aSLionel Sambuc LV->addVirtualRegisterKilled(RegB, PrevMI);
1483f4a2713aSLionel Sambuc }
1484f4a2713aSLionel Sambuc
1485f4a2713aSLionel Sambuc // Update LiveIntervals.
1486f4a2713aSLionel Sambuc if (LIS) {
1487f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(RegB);
1488f4a2713aSLionel Sambuc SlotIndex MIIdx = LIS->getInstructionIndex(MI);
1489f4a2713aSLionel Sambuc LiveInterval::const_iterator I = LI.find(MIIdx);
1490f4a2713aSLionel Sambuc assert(I != LI.end() && "RegB must be live-in to use.");
1491f4a2713aSLionel Sambuc
1492f4a2713aSLionel Sambuc SlotIndex UseIdx = MIIdx.getRegSlot(IsEarlyClobber);
1493f4a2713aSLionel Sambuc if (I->end == UseIdx)
1494f4a2713aSLionel Sambuc LI.removeSegment(LastCopyIdx, UseIdx);
1495f4a2713aSLionel Sambuc }
1496f4a2713aSLionel Sambuc
1497f4a2713aSLionel Sambuc } else if (RemovedKillFlag) {
1498f4a2713aSLionel Sambuc // Some tied uses of regB matched their destination registers, so
1499f4a2713aSLionel Sambuc // regB is still used in this instruction, but a kill flag was
1500f4a2713aSLionel Sambuc // removed from a different tied use of regB, so now we need to add
1501f4a2713aSLionel Sambuc // a kill flag to one of the remaining uses of regB.
1502f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1503f4a2713aSLionel Sambuc MachineOperand &MO = MI->getOperand(i);
1504f4a2713aSLionel Sambuc if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) {
1505f4a2713aSLionel Sambuc MO.setIsKill(true);
1506f4a2713aSLionel Sambuc break;
1507f4a2713aSLionel Sambuc }
1508f4a2713aSLionel Sambuc }
1509f4a2713aSLionel Sambuc }
1510f4a2713aSLionel Sambuc }
1511f4a2713aSLionel Sambuc
1512f4a2713aSLionel Sambuc /// runOnMachineFunction - Reduce two-address instructions to two operands.
1513f4a2713aSLionel Sambuc ///
runOnMachineFunction(MachineFunction & Func)1514f4a2713aSLionel Sambuc bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) {
1515f4a2713aSLionel Sambuc MF = &Func;
1516f4a2713aSLionel Sambuc const TargetMachine &TM = MF->getTarget();
1517f4a2713aSLionel Sambuc MRI = &MF->getRegInfo();
1518*0a6a1f1dSLionel Sambuc TII = TM.getSubtargetImpl()->getInstrInfo();
1519*0a6a1f1dSLionel Sambuc TRI = TM.getSubtargetImpl()->getRegisterInfo();
1520*0a6a1f1dSLionel Sambuc InstrItins = TM.getSubtargetImpl()->getInstrItineraryData();
1521f4a2713aSLionel Sambuc LV = getAnalysisIfAvailable<LiveVariables>();
1522f4a2713aSLionel Sambuc LIS = getAnalysisIfAvailable<LiveIntervals>();
1523f4a2713aSLionel Sambuc AA = &getAnalysis<AliasAnalysis>();
1524f4a2713aSLionel Sambuc OptLevel = TM.getOptLevel();
1525f4a2713aSLionel Sambuc
1526f4a2713aSLionel Sambuc bool MadeChange = false;
1527f4a2713aSLionel Sambuc
1528f4a2713aSLionel Sambuc DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
1529f4a2713aSLionel Sambuc DEBUG(dbgs() << "********** Function: "
1530f4a2713aSLionel Sambuc << MF->getName() << '\n');
1531f4a2713aSLionel Sambuc
1532f4a2713aSLionel Sambuc // This pass takes the function out of SSA form.
1533f4a2713aSLionel Sambuc MRI->leaveSSA();
1534f4a2713aSLionel Sambuc
1535f4a2713aSLionel Sambuc TiedOperandMap TiedOperands;
1536f4a2713aSLionel Sambuc for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
1537f4a2713aSLionel Sambuc MBBI != MBBE; ++MBBI) {
1538f4a2713aSLionel Sambuc MBB = MBBI;
1539f4a2713aSLionel Sambuc unsigned Dist = 0;
1540f4a2713aSLionel Sambuc DistanceMap.clear();
1541f4a2713aSLionel Sambuc SrcRegMap.clear();
1542f4a2713aSLionel Sambuc DstRegMap.clear();
1543f4a2713aSLionel Sambuc Processed.clear();
1544f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator mi = MBB->begin(), me = MBB->end();
1545f4a2713aSLionel Sambuc mi != me; ) {
1546*0a6a1f1dSLionel Sambuc MachineBasicBlock::iterator nmi = std::next(mi);
1547f4a2713aSLionel Sambuc if (mi->isDebugValue()) {
1548f4a2713aSLionel Sambuc mi = nmi;
1549f4a2713aSLionel Sambuc continue;
1550f4a2713aSLionel Sambuc }
1551f4a2713aSLionel Sambuc
1552f4a2713aSLionel Sambuc // Expand REG_SEQUENCE instructions. This will position mi at the first
1553f4a2713aSLionel Sambuc // expanded instruction.
1554f4a2713aSLionel Sambuc if (mi->isRegSequence())
1555f4a2713aSLionel Sambuc eliminateRegSequence(mi);
1556f4a2713aSLionel Sambuc
1557f4a2713aSLionel Sambuc DistanceMap.insert(std::make_pair(mi, ++Dist));
1558f4a2713aSLionel Sambuc
1559f4a2713aSLionel Sambuc processCopy(&*mi);
1560f4a2713aSLionel Sambuc
1561f4a2713aSLionel Sambuc // First scan through all the tied register uses in this instruction
1562f4a2713aSLionel Sambuc // and record a list of pairs of tied operands for each register.
1563f4a2713aSLionel Sambuc if (!collectTiedOperands(mi, TiedOperands)) {
1564f4a2713aSLionel Sambuc mi = nmi;
1565f4a2713aSLionel Sambuc continue;
1566f4a2713aSLionel Sambuc }
1567f4a2713aSLionel Sambuc
1568f4a2713aSLionel Sambuc ++NumTwoAddressInstrs;
1569f4a2713aSLionel Sambuc MadeChange = true;
1570f4a2713aSLionel Sambuc DEBUG(dbgs() << '\t' << *mi);
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc // If the instruction has a single pair of tied operands, try some
1573f4a2713aSLionel Sambuc // transformations that may either eliminate the tied operands or
1574f4a2713aSLionel Sambuc // improve the opportunities for coalescing away the register copy.
1575f4a2713aSLionel Sambuc if (TiedOperands.size() == 1) {
1576f4a2713aSLionel Sambuc SmallVectorImpl<std::pair<unsigned, unsigned> > &TiedPairs
1577f4a2713aSLionel Sambuc = TiedOperands.begin()->second;
1578f4a2713aSLionel Sambuc if (TiedPairs.size() == 1) {
1579f4a2713aSLionel Sambuc unsigned SrcIdx = TiedPairs[0].first;
1580f4a2713aSLionel Sambuc unsigned DstIdx = TiedPairs[0].second;
1581f4a2713aSLionel Sambuc unsigned SrcReg = mi->getOperand(SrcIdx).getReg();
1582f4a2713aSLionel Sambuc unsigned DstReg = mi->getOperand(DstIdx).getReg();
1583f4a2713aSLionel Sambuc if (SrcReg != DstReg &&
1584f4a2713aSLionel Sambuc tryInstructionTransform(mi, nmi, SrcIdx, DstIdx, Dist, false)) {
1585f4a2713aSLionel Sambuc // The tied operands have been eliminated or shifted further down the
1586f4a2713aSLionel Sambuc // block to ease elimination. Continue processing with 'nmi'.
1587f4a2713aSLionel Sambuc TiedOperands.clear();
1588f4a2713aSLionel Sambuc mi = nmi;
1589f4a2713aSLionel Sambuc continue;
1590f4a2713aSLionel Sambuc }
1591f4a2713aSLionel Sambuc }
1592f4a2713aSLionel Sambuc }
1593f4a2713aSLionel Sambuc
1594f4a2713aSLionel Sambuc // Now iterate over the information collected above.
1595f4a2713aSLionel Sambuc for (TiedOperandMap::iterator OI = TiedOperands.begin(),
1596f4a2713aSLionel Sambuc OE = TiedOperands.end(); OI != OE; ++OI) {
1597f4a2713aSLionel Sambuc processTiedPairs(mi, OI->second, Dist);
1598f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
1599f4a2713aSLionel Sambuc }
1600f4a2713aSLionel Sambuc
1601f4a2713aSLionel Sambuc // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form.
1602f4a2713aSLionel Sambuc if (mi->isInsertSubreg()) {
1603f4a2713aSLionel Sambuc // From %reg = INSERT_SUBREG %reg, %subreg, subidx
1604f4a2713aSLionel Sambuc // To %reg:subidx = COPY %subreg
1605f4a2713aSLionel Sambuc unsigned SubIdx = mi->getOperand(3).getImm();
1606f4a2713aSLionel Sambuc mi->RemoveOperand(3);
1607f4a2713aSLionel Sambuc assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx");
1608f4a2713aSLionel Sambuc mi->getOperand(0).setSubReg(SubIdx);
1609f4a2713aSLionel Sambuc mi->getOperand(0).setIsUndef(mi->getOperand(1).isUndef());
1610f4a2713aSLionel Sambuc mi->RemoveOperand(1);
1611f4a2713aSLionel Sambuc mi->setDesc(TII->get(TargetOpcode::COPY));
1612f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tconvert to:\t" << *mi);
1613f4a2713aSLionel Sambuc }
1614f4a2713aSLionel Sambuc
1615f4a2713aSLionel Sambuc // Clear TiedOperands here instead of at the top of the loop
1616f4a2713aSLionel Sambuc // since most instructions do not have tied operands.
1617f4a2713aSLionel Sambuc TiedOperands.clear();
1618f4a2713aSLionel Sambuc mi = nmi;
1619f4a2713aSLionel Sambuc }
1620f4a2713aSLionel Sambuc }
1621f4a2713aSLionel Sambuc
1622f4a2713aSLionel Sambuc if (LIS)
1623f4a2713aSLionel Sambuc MF->verify(this, "After two-address instruction pass");
1624f4a2713aSLionel Sambuc
1625f4a2713aSLionel Sambuc return MadeChange;
1626f4a2713aSLionel Sambuc }
1627f4a2713aSLionel Sambuc
1628f4a2713aSLionel Sambuc /// Eliminate a REG_SEQUENCE instruction as part of the de-ssa process.
1629f4a2713aSLionel Sambuc ///
1630f4a2713aSLionel Sambuc /// The instruction is turned into a sequence of sub-register copies:
1631f4a2713aSLionel Sambuc ///
1632f4a2713aSLionel Sambuc /// %dst = REG_SEQUENCE %v1, ssub0, %v2, ssub1
1633f4a2713aSLionel Sambuc ///
1634f4a2713aSLionel Sambuc /// Becomes:
1635f4a2713aSLionel Sambuc ///
1636f4a2713aSLionel Sambuc /// %dst:ssub0<def,undef> = COPY %v1
1637f4a2713aSLionel Sambuc /// %dst:ssub1<def> = COPY %v2
1638f4a2713aSLionel Sambuc ///
1639f4a2713aSLionel Sambuc void TwoAddressInstructionPass::
eliminateRegSequence(MachineBasicBlock::iterator & MBBI)1640f4a2713aSLionel Sambuc eliminateRegSequence(MachineBasicBlock::iterator &MBBI) {
1641f4a2713aSLionel Sambuc MachineInstr *MI = MBBI;
1642f4a2713aSLionel Sambuc unsigned DstReg = MI->getOperand(0).getReg();
1643f4a2713aSLionel Sambuc if (MI->getOperand(0).getSubReg() ||
1644f4a2713aSLionel Sambuc TargetRegisterInfo::isPhysicalRegister(DstReg) ||
1645f4a2713aSLionel Sambuc !(MI->getNumOperands() & 1)) {
1646f4a2713aSLionel Sambuc DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
1647*0a6a1f1dSLionel Sambuc llvm_unreachable(nullptr);
1648f4a2713aSLionel Sambuc }
1649f4a2713aSLionel Sambuc
1650f4a2713aSLionel Sambuc SmallVector<unsigned, 4> OrigRegs;
1651f4a2713aSLionel Sambuc if (LIS) {
1652f4a2713aSLionel Sambuc OrigRegs.push_back(MI->getOperand(0).getReg());
1653f4a2713aSLionel Sambuc for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2)
1654f4a2713aSLionel Sambuc OrigRegs.push_back(MI->getOperand(i).getReg());
1655f4a2713aSLionel Sambuc }
1656f4a2713aSLionel Sambuc
1657f4a2713aSLionel Sambuc bool DefEmitted = false;
1658f4a2713aSLionel Sambuc for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
1659f4a2713aSLionel Sambuc MachineOperand &UseMO = MI->getOperand(i);
1660f4a2713aSLionel Sambuc unsigned SrcReg = UseMO.getReg();
1661f4a2713aSLionel Sambuc unsigned SubIdx = MI->getOperand(i+1).getImm();
1662f4a2713aSLionel Sambuc // Nothing needs to be inserted for <undef> operands.
1663f4a2713aSLionel Sambuc if (UseMO.isUndef())
1664f4a2713aSLionel Sambuc continue;
1665f4a2713aSLionel Sambuc
1666f4a2713aSLionel Sambuc // Defer any kill flag to the last operand using SrcReg. Otherwise, we
1667f4a2713aSLionel Sambuc // might insert a COPY that uses SrcReg after is was killed.
1668f4a2713aSLionel Sambuc bool isKill = UseMO.isKill();
1669f4a2713aSLionel Sambuc if (isKill)
1670f4a2713aSLionel Sambuc for (unsigned j = i + 2; j < e; j += 2)
1671f4a2713aSLionel Sambuc if (MI->getOperand(j).getReg() == SrcReg) {
1672f4a2713aSLionel Sambuc MI->getOperand(j).setIsKill();
1673f4a2713aSLionel Sambuc UseMO.setIsKill(false);
1674f4a2713aSLionel Sambuc isKill = false;
1675f4a2713aSLionel Sambuc break;
1676f4a2713aSLionel Sambuc }
1677f4a2713aSLionel Sambuc
1678f4a2713aSLionel Sambuc // Insert the sub-register copy.
1679f4a2713aSLionel Sambuc MachineInstr *CopyMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1680f4a2713aSLionel Sambuc TII->get(TargetOpcode::COPY))
1681f4a2713aSLionel Sambuc .addReg(DstReg, RegState::Define, SubIdx)
1682f4a2713aSLionel Sambuc .addOperand(UseMO);
1683f4a2713aSLionel Sambuc
1684f4a2713aSLionel Sambuc // The first def needs an <undef> flag because there is no live register
1685f4a2713aSLionel Sambuc // before it.
1686f4a2713aSLionel Sambuc if (!DefEmitted) {
1687f4a2713aSLionel Sambuc CopyMI->getOperand(0).setIsUndef(true);
1688f4a2713aSLionel Sambuc // Return an iterator pointing to the first inserted instr.
1689f4a2713aSLionel Sambuc MBBI = CopyMI;
1690f4a2713aSLionel Sambuc }
1691f4a2713aSLionel Sambuc DefEmitted = true;
1692f4a2713aSLionel Sambuc
1693f4a2713aSLionel Sambuc // Update LiveVariables' kill info.
1694f4a2713aSLionel Sambuc if (LV && isKill && !TargetRegisterInfo::isPhysicalRegister(SrcReg))
1695f4a2713aSLionel Sambuc LV->replaceKillInstruction(SrcReg, MI, CopyMI);
1696f4a2713aSLionel Sambuc
1697f4a2713aSLionel Sambuc DEBUG(dbgs() << "Inserted: " << *CopyMI);
1698f4a2713aSLionel Sambuc }
1699f4a2713aSLionel Sambuc
1700f4a2713aSLionel Sambuc MachineBasicBlock::iterator EndMBBI =
1701*0a6a1f1dSLionel Sambuc std::next(MachineBasicBlock::iterator(MI));
1702f4a2713aSLionel Sambuc
1703f4a2713aSLionel Sambuc if (!DefEmitted) {
1704f4a2713aSLionel Sambuc DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF");
1705f4a2713aSLionel Sambuc MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
1706f4a2713aSLionel Sambuc for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
1707f4a2713aSLionel Sambuc MI->RemoveOperand(j);
1708f4a2713aSLionel Sambuc } else {
1709f4a2713aSLionel Sambuc DEBUG(dbgs() << "Eliminated: " << *MI);
1710f4a2713aSLionel Sambuc MI->eraseFromParent();
1711f4a2713aSLionel Sambuc }
1712f4a2713aSLionel Sambuc
1713f4a2713aSLionel Sambuc // Udpate LiveIntervals.
1714f4a2713aSLionel Sambuc if (LIS)
1715f4a2713aSLionel Sambuc LIS->repairIntervalsInRange(MBB, MBBI, EndMBBI, OrigRegs);
1716f4a2713aSLionel Sambuc }
1717