xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/CodeGenCommonISel.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
20*bdd1243dSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
21*bdd1243dSDimitry Andric 
22*bdd1243dSDimitry 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 *
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).
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.
106*bdd1243dSDimitry Andric   if (!OPI2->isReg() ||
107*bdd1243dSDimitry 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
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 
17681ad6265SDimitry Andric unsigned llvm::getInvertedFPClassTest(unsigned Test) {
17781ad6265SDimitry Andric   unsigned InvertedTest = ~Test & fcAllFlags;
17881ad6265SDimitry Andric   switch (InvertedTest) {
17981ad6265SDimitry Andric   default:
18081ad6265SDimitry Andric     break;
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:
19981ad6265SDimitry Andric     return InvertedTest;
20081ad6265SDimitry Andric   }
20181ad6265SDimitry Andric   return 0;
20281ad6265SDimitry Andric }
203*bdd1243dSDimitry Andric 
204*bdd1243dSDimitry Andric static MachineOperand *getSalvageOpsForCopy(const MachineRegisterInfo &MRI,
205*bdd1243dSDimitry Andric                                             MachineInstr &Copy) {
206*bdd1243dSDimitry Andric   assert(Copy.getOpcode() == TargetOpcode::COPY && "Must be a COPY");
207*bdd1243dSDimitry Andric 
208*bdd1243dSDimitry Andric   return &Copy.getOperand(1);
209*bdd1243dSDimitry Andric }
210*bdd1243dSDimitry Andric 
211*bdd1243dSDimitry Andric static MachineOperand *getSalvageOpsForTrunc(const MachineRegisterInfo &MRI,
212*bdd1243dSDimitry Andric                                             MachineInstr &Trunc,
213*bdd1243dSDimitry Andric                                             SmallVectorImpl<uint64_t> &Ops) {
214*bdd1243dSDimitry Andric   assert(Trunc.getOpcode() == TargetOpcode::G_TRUNC && "Must be a G_TRUNC");
215*bdd1243dSDimitry Andric 
216*bdd1243dSDimitry Andric   const auto FromLLT = MRI.getType(Trunc.getOperand(1).getReg());
217*bdd1243dSDimitry Andric   const auto ToLLT = MRI.getType(Trunc.defs().begin()->getReg());
218*bdd1243dSDimitry Andric 
219*bdd1243dSDimitry Andric   // TODO: Support non-scalar types.
220*bdd1243dSDimitry Andric   if (!FromLLT.isScalar()) {
221*bdd1243dSDimitry Andric     return nullptr;
222*bdd1243dSDimitry Andric   }
223*bdd1243dSDimitry Andric 
224*bdd1243dSDimitry Andric   auto ExtOps = DIExpression::getExtOps(FromLLT.getSizeInBits(),
225*bdd1243dSDimitry Andric                                         ToLLT.getSizeInBits(), false);
226*bdd1243dSDimitry Andric   Ops.append(ExtOps.begin(), ExtOps.end());
227*bdd1243dSDimitry Andric   return &Trunc.getOperand(1);
228*bdd1243dSDimitry Andric }
229*bdd1243dSDimitry Andric 
230*bdd1243dSDimitry Andric static MachineOperand *salvageDebugInfoImpl(const MachineRegisterInfo &MRI,
231*bdd1243dSDimitry Andric                                             MachineInstr &MI,
232*bdd1243dSDimitry Andric                                             SmallVectorImpl<uint64_t> &Ops) {
233*bdd1243dSDimitry Andric   switch (MI.getOpcode()) {
234*bdd1243dSDimitry Andric   case TargetOpcode::G_TRUNC:
235*bdd1243dSDimitry Andric     return getSalvageOpsForTrunc(MRI, MI, Ops);
236*bdd1243dSDimitry Andric   case TargetOpcode::COPY:
237*bdd1243dSDimitry Andric     return getSalvageOpsForCopy(MRI, MI);
238*bdd1243dSDimitry Andric   default:
239*bdd1243dSDimitry Andric     return nullptr;
240*bdd1243dSDimitry Andric   }
241*bdd1243dSDimitry Andric }
242*bdd1243dSDimitry Andric 
243*bdd1243dSDimitry Andric void llvm::salvageDebugInfoForDbgValue(const MachineRegisterInfo &MRI,
244*bdd1243dSDimitry Andric                                        MachineInstr &MI,
245*bdd1243dSDimitry Andric                                        ArrayRef<MachineOperand *> DbgUsers) {
246*bdd1243dSDimitry Andric   // These are arbitrary chosen limits on the maximum number of values and the
247*bdd1243dSDimitry Andric   // maximum size of a debug expression we can salvage up to, used for
248*bdd1243dSDimitry Andric   // performance reasons.
249*bdd1243dSDimitry Andric   const unsigned MaxExpressionSize = 128;
250*bdd1243dSDimitry Andric 
251*bdd1243dSDimitry Andric   for (auto *DefMO : DbgUsers) {
252*bdd1243dSDimitry Andric     MachineInstr *DbgMI = DefMO->getParent();
253*bdd1243dSDimitry Andric     if (DbgMI->isIndirectDebugValue()) {
254*bdd1243dSDimitry Andric       continue;
255*bdd1243dSDimitry Andric     }
256*bdd1243dSDimitry Andric 
257*bdd1243dSDimitry Andric     int UseMOIdx = DbgMI->findRegisterUseOperandIdx(DefMO->getReg());
258*bdd1243dSDimitry Andric     assert(UseMOIdx != -1 && DbgMI->hasDebugOperandForReg(DefMO->getReg()) &&
259*bdd1243dSDimitry Andric            "Must use salvaged instruction as its location");
260*bdd1243dSDimitry Andric 
261*bdd1243dSDimitry Andric     // TODO: Support DBG_VALUE_LIST.
262*bdd1243dSDimitry Andric     if (DbgMI->getOpcode() != TargetOpcode::DBG_VALUE) {
263*bdd1243dSDimitry Andric       assert(DbgMI->getOpcode() == TargetOpcode::DBG_VALUE_LIST &&
264*bdd1243dSDimitry Andric              "Must be either DBG_VALUE or DBG_VALUE_LIST");
265*bdd1243dSDimitry Andric       continue;
266*bdd1243dSDimitry Andric     }
267*bdd1243dSDimitry Andric 
268*bdd1243dSDimitry Andric     const DIExpression *SalvagedExpr = DbgMI->getDebugExpression();
269*bdd1243dSDimitry Andric 
270*bdd1243dSDimitry Andric     SmallVector<uint64_t, 16> Ops;
271*bdd1243dSDimitry Andric     auto Op0 = salvageDebugInfoImpl(MRI, MI, Ops);
272*bdd1243dSDimitry Andric     if (!Op0)
273*bdd1243dSDimitry Andric       continue;
274*bdd1243dSDimitry Andric     SalvagedExpr = DIExpression::appendOpsToArg(SalvagedExpr, Ops, 0, true);
275*bdd1243dSDimitry Andric 
276*bdd1243dSDimitry Andric     bool IsValidSalvageExpr =
277*bdd1243dSDimitry Andric         SalvagedExpr->getNumElements() <= MaxExpressionSize;
278*bdd1243dSDimitry Andric     if (IsValidSalvageExpr) {
279*bdd1243dSDimitry Andric       auto &UseMO = DbgMI->getOperand(UseMOIdx);
280*bdd1243dSDimitry Andric       UseMO.setReg(Op0->getReg());
281*bdd1243dSDimitry Andric       UseMO.setSubReg(Op0->getSubReg());
282*bdd1243dSDimitry Andric       DbgMI->getDebugExpressionOp().setMetadata(SalvagedExpr);
283*bdd1243dSDimitry Andric 
284*bdd1243dSDimitry Andric       LLVM_DEBUG(dbgs() << "SALVAGE: " << *DbgMI << '\n');
285*bdd1243dSDimitry Andric     }
286*bdd1243dSDimitry Andric   }
287*bdd1243dSDimitry Andric }
288