1349cc55cSDimitry Andric //===-- CodeGenCommonISel.cpp ---------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric //
9349cc55cSDimitry Andric // This file defines common utilies that are shared between SelectionDAG and
10349cc55cSDimitry Andric // GlobalISel frameworks.
11349cc55cSDimitry Andric //
12349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
13349cc55cSDimitry Andric
14349cc55cSDimitry Andric #include "llvm/CodeGen/CodeGenCommonISel.h"
15349cc55cSDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
16349cc55cSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
17349cc55cSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
18349cc55cSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
19349cc55cSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
20bdd1243dSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
21bdd1243dSDimitry Andric
22bdd1243dSDimitry Andric #define DEBUG_TYPE "codegen-common"
23349cc55cSDimitry Andric
24349cc55cSDimitry Andric using namespace llvm;
25349cc55cSDimitry Andric
26349cc55cSDimitry Andric /// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
27349cc55cSDimitry Andric /// is 0.
28349cc55cSDimitry Andric MachineBasicBlock *
addSuccessorMBB(const BasicBlock * BB,MachineBasicBlock * ParentMBB,bool IsLikely,MachineBasicBlock * SuccMBB)29349cc55cSDimitry Andric StackProtectorDescriptor::addSuccessorMBB(
30349cc55cSDimitry Andric const BasicBlock *BB, MachineBasicBlock *ParentMBB, bool IsLikely,
31349cc55cSDimitry Andric MachineBasicBlock *SuccMBB) {
32349cc55cSDimitry Andric // If SuccBB has not been created yet, create it.
33349cc55cSDimitry Andric if (!SuccMBB) {
34349cc55cSDimitry Andric MachineFunction *MF = ParentMBB->getParent();
35349cc55cSDimitry Andric MachineFunction::iterator BBI(ParentMBB);
36349cc55cSDimitry Andric SuccMBB = MF->CreateMachineBasicBlock(BB);
37349cc55cSDimitry Andric MF->insert(++BBI, SuccMBB);
38349cc55cSDimitry Andric }
39349cc55cSDimitry Andric // Add it as a successor of ParentMBB.
40349cc55cSDimitry Andric ParentMBB->addSuccessor(
41349cc55cSDimitry Andric SuccMBB, BranchProbabilityInfo::getBranchProbStackProtector(IsLikely));
42349cc55cSDimitry Andric return SuccMBB;
43349cc55cSDimitry Andric }
44349cc55cSDimitry Andric
45349cc55cSDimitry Andric /// Given that the input MI is before a partial terminator sequence TSeq, return
46349cc55cSDimitry Andric /// true if M + TSeq also a partial terminator sequence.
47349cc55cSDimitry Andric ///
48349cc55cSDimitry Andric /// A Terminator sequence is a sequence of MachineInstrs which at this point in
49349cc55cSDimitry Andric /// lowering copy vregs into physical registers, which are then passed into
50349cc55cSDimitry Andric /// terminator instructors so we can satisfy ABI constraints. A partial
51349cc55cSDimitry Andric /// terminator sequence is an improper subset of a terminator sequence (i.e. it
52349cc55cSDimitry Andric /// may be the whole terminator sequence).
MIIsInTerminatorSequence(const MachineInstr & MI)53349cc55cSDimitry Andric static bool MIIsInTerminatorSequence(const MachineInstr &MI) {
54349cc55cSDimitry Andric // If we do not have a copy or an implicit def, we return true if and only if
55349cc55cSDimitry Andric // MI is a debug value.
56349cc55cSDimitry Andric if (!MI.isCopy() && !MI.isImplicitDef()) {
57349cc55cSDimitry Andric // Sometimes DBG_VALUE MI sneak in between the copies from the vregs to the
58349cc55cSDimitry Andric // physical registers if there is debug info associated with the terminator
59349cc55cSDimitry Andric // of our mbb. We want to include said debug info in our terminator
60349cc55cSDimitry Andric // sequence, so we return true in that case.
61349cc55cSDimitry Andric if (MI.isDebugInstr())
62349cc55cSDimitry Andric return true;
63349cc55cSDimitry Andric
64349cc55cSDimitry Andric // For GlobalISel, we may have extension instructions for arguments within
65349cc55cSDimitry Andric // copy sequences. Allow these.
66349cc55cSDimitry Andric switch (MI.getOpcode()) {
67349cc55cSDimitry Andric case TargetOpcode::G_TRUNC:
68349cc55cSDimitry Andric case TargetOpcode::G_ZEXT:
69349cc55cSDimitry Andric case TargetOpcode::G_ANYEXT:
70349cc55cSDimitry Andric case TargetOpcode::G_SEXT:
71349cc55cSDimitry Andric case TargetOpcode::G_MERGE_VALUES:
72349cc55cSDimitry Andric case TargetOpcode::G_UNMERGE_VALUES:
73349cc55cSDimitry Andric case TargetOpcode::G_CONCAT_VECTORS:
74349cc55cSDimitry Andric case TargetOpcode::G_BUILD_VECTOR:
75349cc55cSDimitry Andric case TargetOpcode::G_EXTRACT:
76349cc55cSDimitry Andric return true;
77349cc55cSDimitry Andric default:
78349cc55cSDimitry Andric return false;
79349cc55cSDimitry Andric }
80349cc55cSDimitry Andric }
81349cc55cSDimitry Andric
82349cc55cSDimitry Andric // We have left the terminator sequence if we are not doing one of the
83349cc55cSDimitry Andric // following:
84349cc55cSDimitry Andric //
85349cc55cSDimitry Andric // 1. Copying a vreg into a physical register.
86349cc55cSDimitry Andric // 2. Copying a vreg into a vreg.
87349cc55cSDimitry Andric // 3. Defining a register via an implicit def.
88349cc55cSDimitry Andric
89349cc55cSDimitry Andric // OPI should always be a register definition...
90349cc55cSDimitry Andric MachineInstr::const_mop_iterator OPI = MI.operands_begin();
91349cc55cSDimitry Andric if (!OPI->isReg() || !OPI->isDef())
92349cc55cSDimitry Andric return false;
93349cc55cSDimitry Andric
94349cc55cSDimitry Andric // Defining any register via an implicit def is always ok.
95349cc55cSDimitry Andric if (MI.isImplicitDef())
96349cc55cSDimitry Andric return true;
97349cc55cSDimitry Andric
98349cc55cSDimitry Andric // Grab the copy source...
99349cc55cSDimitry Andric MachineInstr::const_mop_iterator OPI2 = OPI;
100349cc55cSDimitry Andric ++OPI2;
101349cc55cSDimitry Andric assert(OPI2 != MI.operands_end()
102349cc55cSDimitry Andric && "Should have a copy implying we should have 2 arguments.");
103349cc55cSDimitry Andric
104349cc55cSDimitry Andric // Make sure that the copy dest is not a vreg when the copy source is a
105349cc55cSDimitry Andric // physical register.
106bdd1243dSDimitry Andric if (!OPI2->isReg() ||
107bdd1243dSDimitry Andric (!OPI->getReg().isPhysical() && OPI2->getReg().isPhysical()))
108349cc55cSDimitry Andric return false;
109349cc55cSDimitry Andric
110349cc55cSDimitry Andric return true;
111349cc55cSDimitry Andric }
112349cc55cSDimitry Andric
113349cc55cSDimitry Andric /// Find the split point at which to splice the end of BB into its success stack
114349cc55cSDimitry Andric /// protector check machine basic block.
115349cc55cSDimitry Andric ///
116349cc55cSDimitry Andric /// On many platforms, due to ABI constraints, terminators, even before register
117349cc55cSDimitry Andric /// allocation, use physical registers. This creates an issue for us since
118349cc55cSDimitry Andric /// physical registers at this point can not travel across basic
119349cc55cSDimitry Andric /// blocks. Luckily, selectiondag always moves physical registers into vregs
120349cc55cSDimitry Andric /// when they enter functions and moves them through a sequence of copies back
121349cc55cSDimitry Andric /// into the physical registers right before the terminator creating a
122349cc55cSDimitry Andric /// ``Terminator Sequence''. This function is searching for the beginning of the
123349cc55cSDimitry Andric /// terminator sequence so that we can ensure that we splice off not just the
124349cc55cSDimitry Andric /// terminator, but additionally the copies that move the vregs into the
125349cc55cSDimitry Andric /// physical registers.
126349cc55cSDimitry Andric MachineBasicBlock::iterator
findSplitPointForStackProtector(MachineBasicBlock * BB,const TargetInstrInfo & TII)127349cc55cSDimitry Andric llvm::findSplitPointForStackProtector(MachineBasicBlock *BB,
128349cc55cSDimitry Andric const TargetInstrInfo &TII) {
129349cc55cSDimitry Andric MachineBasicBlock::iterator SplitPoint = BB->getFirstTerminator();
130349cc55cSDimitry Andric if (SplitPoint == BB->begin())
131349cc55cSDimitry Andric return SplitPoint;
132349cc55cSDimitry Andric
133349cc55cSDimitry Andric MachineBasicBlock::iterator Start = BB->begin();
134349cc55cSDimitry Andric MachineBasicBlock::iterator Previous = SplitPoint;
13581ad6265SDimitry Andric do {
136349cc55cSDimitry Andric --Previous;
13781ad6265SDimitry Andric } while (Previous != Start && Previous->isDebugInstr());
138349cc55cSDimitry Andric
139349cc55cSDimitry Andric if (TII.isTailCall(*SplitPoint) &&
140349cc55cSDimitry Andric Previous->getOpcode() == TII.getCallFrameDestroyOpcode()) {
141349cc55cSDimitry Andric // Call frames cannot be nested, so if this frame is describing the tail
142349cc55cSDimitry Andric // call itself, then we must insert before the sequence even starts. For
143349cc55cSDimitry Andric // example:
144349cc55cSDimitry Andric // <split point>
145349cc55cSDimitry Andric // ADJCALLSTACKDOWN ...
146349cc55cSDimitry Andric // <Moves>
147349cc55cSDimitry Andric // ADJCALLSTACKUP ...
148349cc55cSDimitry Andric // TAILJMP somewhere
149349cc55cSDimitry Andric // On the other hand, it could be an unrelated call in which case this tail
15081ad6265SDimitry Andric // call has no register moves of its own and should be the split point. For
151349cc55cSDimitry Andric // example:
152349cc55cSDimitry Andric // ADJCALLSTACKDOWN
153349cc55cSDimitry Andric // CALL something_else
154349cc55cSDimitry Andric // ADJCALLSTACKUP
155349cc55cSDimitry Andric // <split point>
156349cc55cSDimitry Andric // TAILJMP somewhere
157349cc55cSDimitry Andric do {
158349cc55cSDimitry Andric --Previous;
159349cc55cSDimitry Andric if (Previous->isCall())
160349cc55cSDimitry Andric return SplitPoint;
161349cc55cSDimitry Andric } while(Previous->getOpcode() != TII.getCallFrameSetupOpcode());
162349cc55cSDimitry Andric
163349cc55cSDimitry Andric return Previous;
164349cc55cSDimitry Andric }
165349cc55cSDimitry Andric
166349cc55cSDimitry Andric while (MIIsInTerminatorSequence(*Previous)) {
167349cc55cSDimitry Andric SplitPoint = Previous;
168349cc55cSDimitry Andric if (Previous == Start)
169349cc55cSDimitry Andric break;
170349cc55cSDimitry Andric --Previous;
171349cc55cSDimitry Andric }
172349cc55cSDimitry Andric
173349cc55cSDimitry Andric return SplitPoint;
174349cc55cSDimitry Andric }
17581ad6265SDimitry Andric
invertFPClassTestIfSimpler(FPClassTest Test)176*fe013be4SDimitry Andric FPClassTest llvm::invertFPClassTestIfSimpler(FPClassTest Test) {
177*fe013be4SDimitry Andric FPClassTest InvertedTest = ~Test;
178*fe013be4SDimitry Andric // Pick the direction with fewer tests
179*fe013be4SDimitry Andric // TODO: Handle more combinations of cases that can be handled together
180*fe013be4SDimitry Andric switch (static_cast<unsigned>(InvertedTest)) {
18181ad6265SDimitry Andric case fcNan:
18281ad6265SDimitry Andric case fcSNan:
18381ad6265SDimitry Andric case fcQNan:
18481ad6265SDimitry Andric case fcInf:
18581ad6265SDimitry Andric case fcPosInf:
18681ad6265SDimitry Andric case fcNegInf:
18781ad6265SDimitry Andric case fcNormal:
18881ad6265SDimitry Andric case fcPosNormal:
18981ad6265SDimitry Andric case fcNegNormal:
19081ad6265SDimitry Andric case fcSubnormal:
19181ad6265SDimitry Andric case fcPosSubnormal:
19281ad6265SDimitry Andric case fcNegSubnormal:
19381ad6265SDimitry Andric case fcZero:
19481ad6265SDimitry Andric case fcPosZero:
19581ad6265SDimitry Andric case fcNegZero:
19681ad6265SDimitry Andric case fcFinite:
19781ad6265SDimitry Andric case fcPosFinite:
19881ad6265SDimitry Andric case fcNegFinite:
199*fe013be4SDimitry Andric case fcZero | fcNan:
200*fe013be4SDimitry Andric case fcSubnormal | fcZero:
201*fe013be4SDimitry Andric case fcSubnormal | fcZero | fcNan:
20281ad6265SDimitry Andric return InvertedTest;
203*fe013be4SDimitry Andric default:
204*fe013be4SDimitry Andric return fcNone;
20581ad6265SDimitry Andric }
206*fe013be4SDimitry Andric
207*fe013be4SDimitry Andric llvm_unreachable("covered FPClassTest");
20881ad6265SDimitry Andric }
209bdd1243dSDimitry Andric
getSalvageOpsForCopy(const MachineRegisterInfo & MRI,MachineInstr & Copy)210bdd1243dSDimitry Andric static MachineOperand *getSalvageOpsForCopy(const MachineRegisterInfo &MRI,
211bdd1243dSDimitry Andric MachineInstr &Copy) {
212bdd1243dSDimitry Andric assert(Copy.getOpcode() == TargetOpcode::COPY && "Must be a COPY");
213bdd1243dSDimitry Andric
214bdd1243dSDimitry Andric return &Copy.getOperand(1);
215bdd1243dSDimitry Andric }
216bdd1243dSDimitry Andric
getSalvageOpsForTrunc(const MachineRegisterInfo & MRI,MachineInstr & Trunc,SmallVectorImpl<uint64_t> & Ops)217bdd1243dSDimitry Andric static MachineOperand *getSalvageOpsForTrunc(const MachineRegisterInfo &MRI,
218bdd1243dSDimitry Andric MachineInstr &Trunc,
219bdd1243dSDimitry Andric SmallVectorImpl<uint64_t> &Ops) {
220bdd1243dSDimitry Andric assert(Trunc.getOpcode() == TargetOpcode::G_TRUNC && "Must be a G_TRUNC");
221bdd1243dSDimitry Andric
222bdd1243dSDimitry Andric const auto FromLLT = MRI.getType(Trunc.getOperand(1).getReg());
223bdd1243dSDimitry Andric const auto ToLLT = MRI.getType(Trunc.defs().begin()->getReg());
224bdd1243dSDimitry Andric
225bdd1243dSDimitry Andric // TODO: Support non-scalar types.
226bdd1243dSDimitry Andric if (!FromLLT.isScalar()) {
227bdd1243dSDimitry Andric return nullptr;
228bdd1243dSDimitry Andric }
229bdd1243dSDimitry Andric
230bdd1243dSDimitry Andric auto ExtOps = DIExpression::getExtOps(FromLLT.getSizeInBits(),
231bdd1243dSDimitry Andric ToLLT.getSizeInBits(), false);
232bdd1243dSDimitry Andric Ops.append(ExtOps.begin(), ExtOps.end());
233bdd1243dSDimitry Andric return &Trunc.getOperand(1);
234bdd1243dSDimitry Andric }
235bdd1243dSDimitry Andric
salvageDebugInfoImpl(const MachineRegisterInfo & MRI,MachineInstr & MI,SmallVectorImpl<uint64_t> & Ops)236bdd1243dSDimitry Andric static MachineOperand *salvageDebugInfoImpl(const MachineRegisterInfo &MRI,
237bdd1243dSDimitry Andric MachineInstr &MI,
238bdd1243dSDimitry Andric SmallVectorImpl<uint64_t> &Ops) {
239bdd1243dSDimitry Andric switch (MI.getOpcode()) {
240bdd1243dSDimitry Andric case TargetOpcode::G_TRUNC:
241bdd1243dSDimitry Andric return getSalvageOpsForTrunc(MRI, MI, Ops);
242bdd1243dSDimitry Andric case TargetOpcode::COPY:
243bdd1243dSDimitry Andric return getSalvageOpsForCopy(MRI, MI);
244bdd1243dSDimitry Andric default:
245bdd1243dSDimitry Andric return nullptr;
246bdd1243dSDimitry Andric }
247bdd1243dSDimitry Andric }
248bdd1243dSDimitry Andric
salvageDebugInfoForDbgValue(const MachineRegisterInfo & MRI,MachineInstr & MI,ArrayRef<MachineOperand * > DbgUsers)249bdd1243dSDimitry Andric void llvm::salvageDebugInfoForDbgValue(const MachineRegisterInfo &MRI,
250bdd1243dSDimitry Andric MachineInstr &MI,
251bdd1243dSDimitry Andric ArrayRef<MachineOperand *> DbgUsers) {
252bdd1243dSDimitry Andric // These are arbitrary chosen limits on the maximum number of values and the
253bdd1243dSDimitry Andric // maximum size of a debug expression we can salvage up to, used for
254bdd1243dSDimitry Andric // performance reasons.
255bdd1243dSDimitry Andric const unsigned MaxExpressionSize = 128;
256bdd1243dSDimitry Andric
257bdd1243dSDimitry Andric for (auto *DefMO : DbgUsers) {
258bdd1243dSDimitry Andric MachineInstr *DbgMI = DefMO->getParent();
259bdd1243dSDimitry Andric if (DbgMI->isIndirectDebugValue()) {
260bdd1243dSDimitry Andric continue;
261bdd1243dSDimitry Andric }
262bdd1243dSDimitry Andric
263bdd1243dSDimitry Andric int UseMOIdx = DbgMI->findRegisterUseOperandIdx(DefMO->getReg());
264bdd1243dSDimitry Andric assert(UseMOIdx != -1 && DbgMI->hasDebugOperandForReg(DefMO->getReg()) &&
265bdd1243dSDimitry Andric "Must use salvaged instruction as its location");
266bdd1243dSDimitry Andric
267bdd1243dSDimitry Andric // TODO: Support DBG_VALUE_LIST.
268bdd1243dSDimitry Andric if (DbgMI->getOpcode() != TargetOpcode::DBG_VALUE) {
269bdd1243dSDimitry Andric assert(DbgMI->getOpcode() == TargetOpcode::DBG_VALUE_LIST &&
270bdd1243dSDimitry Andric "Must be either DBG_VALUE or DBG_VALUE_LIST");
271bdd1243dSDimitry Andric continue;
272bdd1243dSDimitry Andric }
273bdd1243dSDimitry Andric
274bdd1243dSDimitry Andric const DIExpression *SalvagedExpr = DbgMI->getDebugExpression();
275bdd1243dSDimitry Andric
276bdd1243dSDimitry Andric SmallVector<uint64_t, 16> Ops;
277bdd1243dSDimitry Andric auto Op0 = salvageDebugInfoImpl(MRI, MI, Ops);
278bdd1243dSDimitry Andric if (!Op0)
279bdd1243dSDimitry Andric continue;
280bdd1243dSDimitry Andric SalvagedExpr = DIExpression::appendOpsToArg(SalvagedExpr, Ops, 0, true);
281bdd1243dSDimitry Andric
282bdd1243dSDimitry Andric bool IsValidSalvageExpr =
283bdd1243dSDimitry Andric SalvagedExpr->getNumElements() <= MaxExpressionSize;
284bdd1243dSDimitry Andric if (IsValidSalvageExpr) {
285bdd1243dSDimitry Andric auto &UseMO = DbgMI->getOperand(UseMOIdx);
286bdd1243dSDimitry Andric UseMO.setReg(Op0->getReg());
287bdd1243dSDimitry Andric UseMO.setSubReg(Op0->getSubReg());
288bdd1243dSDimitry Andric DbgMI->getDebugExpressionOp().setMetadata(SalvagedExpr);
289bdd1243dSDimitry Andric
290bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "SALVAGE: " << *DbgMI << '\n');
291bdd1243dSDimitry Andric }
292bdd1243dSDimitry Andric }
293bdd1243dSDimitry Andric }
294