1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
//===-- CodeGenCommonISel.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines common utilies that are shared between SelectionDAG and
// GlobalISel frameworks.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/CodeGenCommonISel.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/IR/DebugInfoMetadata.h"
#define DEBUG_TYPE "codegen-common"
using namespace llvm;
/// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
/// is 0.
MachineBasicBlock *
StackProtectorDescriptor::addSuccessorMBB(
const BasicBlock *BB, MachineBasicBlock *ParentMBB, bool IsLikely,
MachineBasicBlock *SuccMBB) {
// If SuccBB has not been created yet, create it.
if (!SuccMBB) {
MachineFunction *MF = ParentMBB->getParent();
MachineFunction::iterator BBI(ParentMBB);
SuccMBB = MF->CreateMachineBasicBlock(BB);
MF->insert(++BBI, SuccMBB);
}
// Add it as a successor of ParentMBB.
ParentMBB->addSuccessor(
SuccMBB, BranchProbabilityInfo::getBranchProbStackProtector(IsLikely));
return SuccMBB;
}
/// Given that the input MI is before a partial terminator sequence TSeq, return
/// true if M + TSeq also a partial terminator sequence.
///
/// A Terminator sequence is a sequence of MachineInstrs which at this point in
/// lowering copy vregs into physical registers, which are then passed into
/// terminator instructors so we can satisfy ABI constraints. A partial
/// terminator sequence is an improper subset of a terminator sequence (i.e. it
/// may be the whole terminator sequence).
static bool MIIsInTerminatorSequence(const MachineInstr &MI) {
// If we do not have a copy or an implicit def, we return true if and only if
// MI is a debug value.
if (!MI.isCopy() && !MI.isImplicitDef()) {
// Sometimes DBG_VALUE MI sneak in between the copies from the vregs to the
// physical registers if there is debug info associated with the terminator
// of our mbb. We want to include said debug info in our terminator
// sequence, so we return true in that case.
if (MI.isDebugInstr())
return true;
// For GlobalISel, we may have extension instructions for arguments within
// copy sequences. Allow these.
switch (MI.getOpcode()) {
case TargetOpcode::G_TRUNC:
case TargetOpcode::G_ZEXT:
case TargetOpcode::G_ANYEXT:
case TargetOpcode::G_SEXT:
case TargetOpcode::G_MERGE_VALUES:
case TargetOpcode::G_UNMERGE_VALUES:
case TargetOpcode::G_CONCAT_VECTORS:
case TargetOpcode::G_BUILD_VECTOR:
case TargetOpcode::G_EXTRACT:
return true;
default:
return false;
}
}
// We have left the terminator sequence if we are not doing one of the
// following:
//
// 1. Copying a vreg into a physical register.
// 2. Copying a vreg into a vreg.
// 3. Defining a register via an implicit def.
// OPI should always be a register definition...
MachineInstr::const_mop_iterator OPI = MI.operands_begin();
if (!OPI->isReg() || !OPI->isDef())
return false;
// Defining any register via an implicit def is always ok.
if (MI.isImplicitDef())
return true;
// Grab the copy source...
MachineInstr::const_mop_iterator OPI2 = OPI;
++OPI2;
assert(OPI2 != MI.operands_end()
&& "Should have a copy implying we should have 2 arguments.");
// Make sure that the copy dest is not a vreg when the copy source is a
// physical register.
if (!OPI2->isReg() ||
(!OPI->getReg().isPhysical() && OPI2->getReg().isPhysical()))
return false;
return true;
}
/// Find the split point at which to splice the end of BB into its success stack
/// protector check machine basic block.
///
/// On many platforms, due to ABI constraints, terminators, even before register
/// allocation, use physical registers. This creates an issue for us since
/// physical registers at this point can not travel across basic
/// blocks. Luckily, selectiondag always moves physical registers into vregs
/// when they enter functions and moves them through a sequence of copies back
/// into the physical registers right before the terminator creating a
/// ``Terminator Sequence''. This function is searching for the beginning of the
/// terminator sequence so that we can ensure that we splice off not just the
/// terminator, but additionally the copies that move the vregs into the
/// physical registers.
MachineBasicBlock::iterator
llvm::findSplitPointForStackProtector(MachineBasicBlock *BB,
const TargetInstrInfo &TII) {
MachineBasicBlock::iterator SplitPoint = BB->getFirstTerminator();
if (SplitPoint == BB->begin())
return SplitPoint;
MachineBasicBlock::iterator Start = BB->begin();
MachineBasicBlock::iterator Previous = SplitPoint;
do {
--Previous;
} while (Previous != Start && Previous->isDebugInstr());
if (TII.isTailCall(*SplitPoint) &&
Previous->getOpcode() == TII.getCallFrameDestroyOpcode()) {
// Call frames cannot be nested, so if this frame is describing the tail
// call itself, then we must insert before the sequence even starts. For
// example:
// <split point>
// ADJCALLSTACKDOWN ...
// <Moves>
// ADJCALLSTACKUP ...
// TAILJMP somewhere
// On the other hand, it could be an unrelated call in which case this tail
// call has no register moves of its own and should be the split point. For
// example:
// ADJCALLSTACKDOWN
// CALL something_else
// ADJCALLSTACKUP
// <split point>
// TAILJMP somewhere
do {
--Previous;
if (Previous->isCall())
return SplitPoint;
} while(Previous->getOpcode() != TII.getCallFrameSetupOpcode());
return Previous;
}
while (MIIsInTerminatorSequence(*Previous)) {
SplitPoint = Previous;
if (Previous == Start)
break;
--Previous;
}
return SplitPoint;
}
FPClassTest llvm::invertFPClassTestIfSimpler(FPClassTest Test) {
FPClassTest InvertedTest = ~Test;
// Pick the direction with fewer tests
// TODO: Handle more combinations of cases that can be handled together
switch (static_cast<unsigned>(InvertedTest)) {
case fcNan:
case fcSNan:
case fcQNan:
case fcInf:
case fcPosInf:
case fcNegInf:
case fcNormal:
case fcPosNormal:
case fcNegNormal:
case fcSubnormal:
case fcPosSubnormal:
case fcNegSubnormal:
case fcZero:
case fcPosZero:
case fcNegZero:
case fcFinite:
case fcPosFinite:
case fcNegFinite:
case fcZero | fcNan:
case fcSubnormal | fcZero:
case fcSubnormal | fcZero | fcNan:
return InvertedTest;
default:
return fcNone;
}
llvm_unreachable("covered FPClassTest");
}
static MachineOperand *getSalvageOpsForCopy(const MachineRegisterInfo &MRI,
MachineInstr &Copy) {
assert(Copy.getOpcode() == TargetOpcode::COPY && "Must be a COPY");
return &Copy.getOperand(1);
}
static MachineOperand *getSalvageOpsForTrunc(const MachineRegisterInfo &MRI,
MachineInstr &Trunc,
SmallVectorImpl<uint64_t> &Ops) {
assert(Trunc.getOpcode() == TargetOpcode::G_TRUNC && "Must be a G_TRUNC");
const auto FromLLT = MRI.getType(Trunc.getOperand(1).getReg());
const auto ToLLT = MRI.getType(Trunc.defs().begin()->getReg());
// TODO: Support non-scalar types.
if (!FromLLT.isScalar()) {
return nullptr;
}
auto ExtOps = DIExpression::getExtOps(FromLLT.getSizeInBits(),
ToLLT.getSizeInBits(), false);
Ops.append(ExtOps.begin(), ExtOps.end());
return &Trunc.getOperand(1);
}
static MachineOperand *salvageDebugInfoImpl(const MachineRegisterInfo &MRI,
MachineInstr &MI,
SmallVectorImpl<uint64_t> &Ops) {
switch (MI.getOpcode()) {
case TargetOpcode::G_TRUNC:
return getSalvageOpsForTrunc(MRI, MI, Ops);
case TargetOpcode::COPY:
return getSalvageOpsForCopy(MRI, MI);
default:
return nullptr;
}
}
void llvm::salvageDebugInfoForDbgValue(const MachineRegisterInfo &MRI,
MachineInstr &MI,
ArrayRef<MachineOperand *> DbgUsers) {
// These are arbitrary chosen limits on the maximum number of values and the
// maximum size of a debug expression we can salvage up to, used for
// performance reasons.
const unsigned MaxExpressionSize = 128;
for (auto *DefMO : DbgUsers) {
MachineInstr *DbgMI = DefMO->getParent();
if (DbgMI->isIndirectDebugValue()) {
continue;
}
int UseMOIdx =
DbgMI->findRegisterUseOperandIdx(DefMO->getReg(), /*TRI=*/nullptr);
assert(UseMOIdx != -1 && DbgMI->hasDebugOperandForReg(DefMO->getReg()) &&
"Must use salvaged instruction as its location");
// TODO: Support DBG_VALUE_LIST.
if (DbgMI->getOpcode() != TargetOpcode::DBG_VALUE) {
assert(DbgMI->getOpcode() == TargetOpcode::DBG_VALUE_LIST &&
"Must be either DBG_VALUE or DBG_VALUE_LIST");
continue;
}
const DIExpression *SalvagedExpr = DbgMI->getDebugExpression();
SmallVector<uint64_t, 16> Ops;
auto Op0 = salvageDebugInfoImpl(MRI, MI, Ops);
if (!Op0)
continue;
SalvagedExpr = DIExpression::appendOpsToArg(SalvagedExpr, Ops, 0, true);
bool IsValidSalvageExpr =
SalvagedExpr->getNumElements() <= MaxExpressionSize;
if (IsValidSalvageExpr) {
auto &UseMO = DbgMI->getOperand(UseMOIdx);
UseMO.setReg(Op0->getReg());
UseMO.setSubReg(Op0->getSubReg());
DbgMI->getDebugExpressionOp().setMetadata(SalvagedExpr);
LLVM_DEBUG(dbgs() << "SALVAGE: " << *DbgMI << '\n');
}
}
}
|