2019-06-25 18:45:51 +08:00
|
|
|
//===-- ARMLowOverheadLoops.cpp - CodeGen Low-overhead Loops ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// Finalize v8.1-m low-overhead loops by converting the associated pseudo
|
|
|
|
/// instructions into machine operations.
|
|
|
|
/// The expectation is that the loop contains three pseudo instructions:
|
|
|
|
/// - t2*LoopStart - placed in the preheader or pre-preheader. The do-loop
|
|
|
|
/// form should be in the preheader, whereas the while form should be in the
|
2019-08-07 15:39:19 +08:00
|
|
|
/// preheaders only predecessor.
|
2019-06-25 18:45:51 +08:00
|
|
|
/// - t2LoopDec - placed within in the loop body.
|
|
|
|
/// - t2LoopEnd - the loop latch terminator.
|
|
|
|
///
|
2020-01-06 17:56:02 +08:00
|
|
|
/// In addition to this, we also look for the presence of the VCTP instruction,
|
|
|
|
/// which determines whether we can generated the tail-predicated low-overhead
|
|
|
|
/// loop form.
|
|
|
|
///
|
2020-01-09 20:52:50 +08:00
|
|
|
/// Assumptions and Dependencies:
|
|
|
|
/// Low-overhead loops are constructed and executed using a setup instruction:
|
|
|
|
/// DLS, WLS, DLSTP or WLSTP and an instruction that loops back: LE or LETP.
|
|
|
|
/// WLS(TP) and LE(TP) are branching instructions with a (large) limited range
|
|
|
|
/// but fixed polarity: WLS can only branch forwards and LE can only branch
|
|
|
|
/// backwards. These restrictions mean that this pass is dependent upon block
|
|
|
|
/// layout and block sizes, which is why it's the last pass to run. The same is
|
|
|
|
/// true for ConstantIslands, but this pass does not increase the size of the
|
|
|
|
/// basic blocks, nor does it change the CFG. Instructions are mainly removed
|
|
|
|
/// during the transform and pseudo instructions are replaced by real ones. In
|
|
|
|
/// some cases, when we have to revert to a 'normal' loop, we have to introduce
|
|
|
|
/// multiple instructions for a single pseudo (see RevertWhile and
|
[ARM] Improve WLS lowering
Recently we improved the lowering of low overhead loops and tail
predicated loops, but concentrated first on the DLS do style loops. This
extends those improvements over to the WLS while loops, improving the
chance of lowering them successfully. To do this the lowering has to
change a little as the instructions are terminators that produce a value
- something that needs to be treated carefully.
Lowering starts at the Hardware Loop pass, inserting a new
llvm.test.start.loop.iterations that produces both an i1 to control the
loop entry and an i32 similar to the llvm.start.loop.iterations
intrinsic added for do loops. This feeds into the loop phi, properly
gluing the values together:
%wls = call { i32, i1 } @llvm.test.start.loop.iterations.i32(i32 %div)
%wls0 = extractvalue { i32, i1 } %wls, 0
%wls1 = extractvalue { i32, i1 } %wls, 1
br i1 %wls1, label %loop.ph, label %loop.exit
...
loop:
%lsr.iv = phi i32 [ %wls0, %loop.ph ], [ %iv.next, %loop ]
..
%iv.next = call i32 @llvm.loop.decrement.reg.i32(i32 %lsr.iv, i32 1)
%cmp = icmp ne i32 %iv.next, 0
br i1 %cmp, label %loop, label %loop.exit
The llvm.test.start.loop.iterations need to be lowered through ISel
lowering as a pair of WLS and WLSSETUP nodes, which each get converted
to t2WhileLoopSetup and t2WhileLoopStart Pseudos. This helps prevent
t2WhileLoopStart from being a terminator that produces a value,
something difficult to control at that stage in the pipeline. Instead
the t2WhileLoopSetup produces the value of LR (essentially acting as a
lr = subs rn, 0), t2WhileLoopStart consumes that lr value (the Bcc).
These are then converted into a single t2WhileLoopStartLR at the same
point as t2DoLoopStartTP and t2LoopEndDec. Otherwise we revert the loop
to prevent them from progressing further in the pipeline. The
t2WhileLoopStartLR is a single instruction that takes a GPR and produces
LR, similar to the WLS instruction.
%1:gprlr = t2WhileLoopStartLR %0:rgpr, %bb.3
t2B %bb.1
...
bb.2.loop:
%2:gprlr = PHI %1:gprlr, %bb.1, %3:gprlr, %bb.2
...
%3:gprlr = t2LoopEndDec %2:gprlr, %bb.2
t2B %bb.3
The t2WhileLoopStartLR can then be treated similar to the other low
overhead loop pseudos, eventually being lowered to a WLS providing the
branches are within range.
Differential Revision: https://reviews.llvm.org/D97729
2021-03-11 22:06:04 +08:00
|
|
|
/// RevertLoopEnd). To handle this situation, t2WhileLoopStartLR and t2LoopEnd
|
2020-01-09 20:52:50 +08:00
|
|
|
/// are defined to be as large as this maximum sequence of replacement
|
|
|
|
/// instructions.
|
|
|
|
///
|
2020-04-08 21:31:21 +08:00
|
|
|
/// A note on VPR.P0 (the lane mask):
|
|
|
|
/// VPT, VCMP, VPNOT and VCTP won't overwrite VPR.P0 when they update it in a
|
|
|
|
/// "VPT Active" context (which includes low-overhead loops and vpt blocks).
|
|
|
|
/// They will simply "and" the result of their calculation with the current
|
|
|
|
/// value of VPR.P0. You can think of it like this:
|
|
|
|
/// \verbatim
|
|
|
|
/// if VPT active: ; Between a DLSTP/LETP, or for predicated instrs
|
|
|
|
/// VPR.P0 &= Value
|
|
|
|
/// else
|
|
|
|
/// VPR.P0 = Value
|
|
|
|
/// \endverbatim
|
|
|
|
/// When we're inside the low-overhead loop (between DLSTP and LETP), we always
|
|
|
|
/// fall in the "VPT active" case, so we can consider that all VPR writes by
|
|
|
|
/// one of those instruction is actually a "and".
|
2019-06-25 18:45:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMBaseInstrInfo.h"
|
|
|
|
#include "ARMBaseRegisterInfo.h"
|
|
|
|
#include "ARMBasicBlockInfo.h"
|
|
|
|
#include "ARMSubtarget.h"
|
2020-12-07 23:44:40 +08:00
|
|
|
#include "MVETailPredUtils.h"
|
2020-01-10 22:47:29 +08:00
|
|
|
#include "Thumb2InstrInfo.h"
|
2019-12-20 16:42:11 +08:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2019-12-20 17:32:36 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2020-01-17 21:08:24 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2019-06-25 18:45:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
[ARM][LowOverheadLoops] Remove dead loop update instructions.
After creating a low-overhead loop, the loop update instruction was still
lingering around hurting performance. This removes dead loop update
instructions, which in our case are mostly SUBS instructions.
To support this, some helper functions were added to MachineLoopUtils and
ReachingDefAnalysis to analyse live-ins of loop exit blocks and find uses
before a particular loop instruction, respectively.
This is a first version that removes a SUBS instruction when there are no other
uses inside and outside the loop block, but there are some more interesting
cases in test/CodeGen/Thumb2/LowOverheadLoops/mve-tail-data-types.ll which
shows that there is room for improvement. For example, we can't handle this
case yet:
..
dlstp.32 lr, r2
.LBB0_1:
mov r3, r2
subs r2, #4
vldrh.u32 q2, [r1], #8
vmov q1, q0
vmla.u32 q0, q2, r0
letp lr, .LBB0_1
@ %bb.2:
vctp.32 r3
..
which is a lot more tricky because r2 is not only used by the subs, but also by
the mov to r3, which is used outside the low-overhead loop by the vctp
instruction, and that requires a bit of a different approach, and I will follow
up on this.
Differential Revision: https://reviews.llvm.org/D71007
2019-12-11 18:11:48 +08:00
|
|
|
#include "llvm/CodeGen/MachineLoopUtils.h"
|
2019-06-25 18:45:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2019-11-26 18:03:25 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/ReachingDefAnalysis.h"
|
2019-11-19 01:07:56 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
2019-06-25 18:45:51 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "arm-low-overhead-loops"
|
|
|
|
#define ARM_LOW_OVERHEAD_LOOPS_NAME "ARM Low Overhead Loops pass"
|
|
|
|
|
2020-09-24 18:47:30 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableTailPredication("arm-loloops-disable-tailpred", cl::Hidden,
|
|
|
|
cl::desc("Disable tail-predication in the ARM LowOverheadLoop pass"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2020-09-21 18:34:06 +08:00
|
|
|
static bool isVectorPredicated(MachineInstr *MI) {
|
|
|
|
int PIdx = llvm::findFirstVPTPredOperandIdx(*MI);
|
|
|
|
return PIdx != -1 && MI->getOperand(PIdx + 1).getReg() == ARM::VPR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isVectorPredicate(MachineInstr *MI) {
|
|
|
|
return MI->findRegisterDefOperandIdx(ARM::VPR) != -1;
|
|
|
|
}
|
|
|
|
|
2020-11-06 21:35:35 +08:00
|
|
|
static bool hasVPRUse(MachineInstr &MI) {
|
|
|
|
return MI.findRegisterUseOperandIdx(ARM::VPR) != -1;
|
2020-09-21 18:34:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isDomainMVE(MachineInstr *MI) {
|
|
|
|
uint64_t Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
|
|
|
|
return Domain == ARMII::DomainMVE;
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:07:52 +08:00
|
|
|
static int getVecSize(const MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
uint64_t Flags = MCID.TSFlags;
|
|
|
|
return (Flags & ARMII::VecSize) >> ARMII::VecSizeShift;
|
|
|
|
}
|
|
|
|
|
2020-09-21 18:34:06 +08:00
|
|
|
static bool shouldInspect(MachineInstr &MI) {
|
2021-09-22 19:07:52 +08:00
|
|
|
if (MI.isDebugInstr())
|
|
|
|
return false;
|
2020-11-06 21:35:35 +08:00
|
|
|
return isDomainMVE(&MI) || isVectorPredicate(&MI) || hasVPRUse(MI);
|
2020-09-21 18:34:06 +08:00
|
|
|
}
|
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
namespace {
|
|
|
|
|
2020-03-24 16:41:48 +08:00
|
|
|
using InstSet = SmallPtrSetImpl<MachineInstr *>;
|
|
|
|
|
2020-01-16 23:42:41 +08:00
|
|
|
class PostOrderLoopTraversal {
|
|
|
|
MachineLoop &ML;
|
|
|
|
MachineLoopInfo &MLI;
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 4> Visited;
|
|
|
|
SmallVector<MachineBasicBlock*, 4> Order;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PostOrderLoopTraversal(MachineLoop &ML, MachineLoopInfo &MLI)
|
|
|
|
: ML(ML), MLI(MLI) { }
|
|
|
|
|
|
|
|
const SmallVectorImpl<MachineBasicBlock*> &getOrder() const {
|
|
|
|
return Order;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit all the blocks within the loop, as well as exit blocks and any
|
|
|
|
// blocks properly dominating the header.
|
|
|
|
void ProcessLoop() {
|
|
|
|
std::function<void(MachineBasicBlock*)> Search = [this, &Search]
|
|
|
|
(MachineBasicBlock *MBB) -> void {
|
|
|
|
if (Visited.count(MBB))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Visited.insert(MBB);
|
|
|
|
for (auto *Succ : MBB->successors()) {
|
|
|
|
if (!ML.contains(Succ))
|
|
|
|
continue;
|
|
|
|
Search(Succ);
|
|
|
|
}
|
|
|
|
Order.push_back(MBB);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Insert exit blocks.
|
|
|
|
SmallVector<MachineBasicBlock*, 2> ExitBlocks;
|
|
|
|
ML.getExitBlocks(ExitBlocks);
|
2021-01-25 04:18:55 +08:00
|
|
|
append_range(Order, ExitBlocks);
|
2020-01-16 23:42:41 +08:00
|
|
|
|
|
|
|
// Then add the loop body.
|
|
|
|
Search(ML.getHeader());
|
|
|
|
|
|
|
|
// Then try the preheader and its predecessors.
|
|
|
|
std::function<void(MachineBasicBlock*)> GetPredecessor =
|
|
|
|
[this, &GetPredecessor] (MachineBasicBlock *MBB) -> void {
|
|
|
|
Order.push_back(MBB);
|
|
|
|
if (MBB->pred_size() == 1)
|
|
|
|
GetPredecessor(*MBB->pred_begin());
|
|
|
|
};
|
|
|
|
|
|
|
|
if (auto *Preheader = ML.getLoopPreheader())
|
|
|
|
GetPredecessor(Preheader);
|
2021-05-24 19:22:15 +08:00
|
|
|
else if (auto *Preheader = MLI.findLoopPreheader(&ML, true, true))
|
2020-01-16 23:42:41 +08:00
|
|
|
GetPredecessor(Preheader);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-20 16:42:11 +08:00
|
|
|
struct PredicatedMI {
|
|
|
|
MachineInstr *MI = nullptr;
|
|
|
|
SetVector<MachineInstr*> Predicates;
|
|
|
|
|
|
|
|
public:
|
2020-04-08 21:31:21 +08:00
|
|
|
PredicatedMI(MachineInstr *I, SetVector<MachineInstr *> &Preds) : MI(I) {
|
|
|
|
assert(I && "Instruction must not be null!");
|
|
|
|
Predicates.insert(Preds.begin(), Preds.end());
|
|
|
|
}
|
2019-12-20 16:42:11 +08:00
|
|
|
};
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// Represent the current state of the VPR and hold all instances which
|
|
|
|
// represent a VPT block, which is a list of instructions that begins with a
|
|
|
|
// VPT/VPST and has a maximum of four proceeding instructions. All
|
|
|
|
// instructions within the block are predicated upon the vpr and we allow
|
|
|
|
// instructions to define the vpr within in the block too.
|
|
|
|
class VPTState {
|
|
|
|
friend struct LowOverheadLoop;
|
|
|
|
|
|
|
|
SmallVector<MachineInstr *, 4> Insts;
|
|
|
|
|
|
|
|
static SmallVector<VPTState, 4> Blocks;
|
|
|
|
static SetVector<MachineInstr *> CurrentPredicates;
|
|
|
|
static std::map<MachineInstr *,
|
|
|
|
std::unique_ptr<PredicatedMI>> PredicatedInsts;
|
|
|
|
|
|
|
|
static void CreateVPTBlock(MachineInstr *MI) {
|
2020-09-24 21:02:53 +08:00
|
|
|
assert((CurrentPredicates.size() || MI->getParent()->isLiveIn(ARM::VPR))
|
|
|
|
&& "Can't begin VPT without predicate");
|
2020-09-22 16:22:11 +08:00
|
|
|
Blocks.emplace_back(MI);
|
|
|
|
// The execution of MI is predicated upon the current set of instructions
|
|
|
|
// that are AND'ed together to form the VPR predicate value. In the case
|
|
|
|
// that MI is a VPT, CurrentPredicates will also just be MI.
|
|
|
|
PredicatedInsts.emplace(
|
|
|
|
MI, std::make_unique<PredicatedMI>(MI, CurrentPredicates));
|
|
|
|
}
|
2019-12-20 16:42:11 +08:00
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
static void reset() {
|
|
|
|
Blocks.clear();
|
|
|
|
PredicatedInsts.clear();
|
|
|
|
CurrentPredicates.clear();
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
static void addInst(MachineInstr *MI) {
|
|
|
|
Blocks.back().insert(MI);
|
|
|
|
PredicatedInsts.emplace(
|
|
|
|
MI, std::make_unique<PredicatedMI>(MI, CurrentPredicates));
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
static void addPredicate(MachineInstr *MI) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Adding VPT Predicate: " << *MI);
|
|
|
|
CurrentPredicates.insert(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resetPredicate(MachineInstr *MI) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Resetting VPT Predicate: " << *MI);
|
|
|
|
CurrentPredicates.clear();
|
|
|
|
CurrentPredicates.insert(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-12-20 16:42:11 +08:00
|
|
|
// Have we found an instruction within the block which defines the vpr? If
|
|
|
|
// so, not all the instructions in the block will have the same predicate.
|
2020-09-22 16:22:11 +08:00
|
|
|
static bool hasUniformPredicate(VPTState &Block) {
|
|
|
|
return getDivergent(Block) == nullptr;
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// If it exists, return the first internal instruction which modifies the
|
|
|
|
// VPR.
|
|
|
|
static MachineInstr *getDivergent(VPTState &Block) {
|
|
|
|
SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
|
|
|
|
for (unsigned i = 1; i < Insts.size(); ++i) {
|
|
|
|
MachineInstr *Next = Insts[i];
|
|
|
|
if (isVectorPredicate(Next))
|
|
|
|
return Next; // Found an instruction altering the vpr.
|
|
|
|
}
|
|
|
|
return nullptr;
|
2020-04-08 21:31:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// Return whether the given instruction is predicated upon a VCTP.
|
|
|
|
static bool isPredicatedOnVCTP(MachineInstr *MI, bool Exclusive = false) {
|
|
|
|
SetVector<MachineInstr *> &Predicates = PredicatedInsts[MI]->Predicates;
|
|
|
|
if (Exclusive && Predicates.size() != 1)
|
|
|
|
return false;
|
|
|
|
for (auto *PredMI : Predicates)
|
|
|
|
if (isVCTP(PredMI))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-08 21:31:21 +08:00
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// Is the VPST, controlling the block entry, predicated upon a VCTP.
|
|
|
|
static bool isEntryPredicatedOnVCTP(VPTState &Block,
|
|
|
|
bool Exclusive = false) {
|
|
|
|
SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
|
|
|
|
return isPredicatedOnVCTP(Insts.front(), Exclusive);
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 17:43:18 +08:00
|
|
|
// If this block begins with a VPT, we can check whether it's using
|
|
|
|
// at least one predicated input(s), as well as possible loop invariant
|
|
|
|
// which would result in it being implicitly predicated.
|
|
|
|
static bool hasImplicitlyValidVPT(VPTState &Block,
|
|
|
|
ReachingDefAnalysis &RDA) {
|
|
|
|
SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
|
|
|
|
MachineInstr *VPT = Insts.front();
|
|
|
|
assert(isVPTOpcode(VPT->getOpcode()) &&
|
|
|
|
"Expected VPT block to begin with VPT/VPST");
|
|
|
|
|
|
|
|
if (VPT->getOpcode() == ARM::MVE_VPST)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto IsOperandPredicated = [&](MachineInstr *MI, unsigned Idx) {
|
|
|
|
MachineInstr *Op = RDA.getMIOperand(MI, MI->getOperand(Idx));
|
|
|
|
return Op && PredicatedInsts.count(Op) && isPredicatedOnVCTP(Op);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto IsOperandInvariant = [&](MachineInstr *MI, unsigned Idx) {
|
|
|
|
MachineOperand &MO = MI->getOperand(Idx);
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr *, 2> Defs;
|
|
|
|
RDA.getGlobalReachingDefs(MI, MO.getReg(), Defs);
|
|
|
|
if (Defs.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto *Def : Defs)
|
|
|
|
if (Def->getParent() == VPT->getParent())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that at least one of the operands is directly predicated on a
|
|
|
|
// vctp and allow an invariant value too.
|
|
|
|
return (IsOperandPredicated(VPT, 1) || IsOperandPredicated(VPT, 2)) &&
|
|
|
|
(IsOperandPredicated(VPT, 1) || IsOperandInvariant(VPT, 1)) &&
|
|
|
|
(IsOperandPredicated(VPT, 2) || IsOperandInvariant(VPT, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isValid(ReachingDefAnalysis &RDA) {
|
2020-09-22 16:22:11 +08:00
|
|
|
// All predication within the loop should be based on vctp. If the block
|
|
|
|
// isn't predicated on entry, check whether the vctp is within the block
|
|
|
|
// and that all other instructions are then predicated on it.
|
|
|
|
for (auto &Block : Blocks) {
|
2020-09-22 17:43:18 +08:00
|
|
|
if (isEntryPredicatedOnVCTP(Block, false) ||
|
|
|
|
hasImplicitlyValidVPT(Block, RDA))
|
2020-09-22 16:22:11 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
|
2020-12-14 19:17:01 +08:00
|
|
|
// We don't know how to convert a block with just a VPT;VCTP into
|
|
|
|
// anything valid once we remove the VCTP. For now just bail out.
|
|
|
|
assert(isVPTOpcode(Insts.front()->getOpcode()) &&
|
|
|
|
"Expected VPT block to start with a VPST or VPT!");
|
|
|
|
if (Insts.size() == 2 && Insts.front()->getOpcode() != ARM::MVE_VPST &&
|
|
|
|
isVCTP(Insts.back()))
|
|
|
|
return false;
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
for (auto *MI : Insts) {
|
|
|
|
// Check that any internal VCTPs are 'Then' predicated.
|
|
|
|
if (isVCTP(MI) && getVPTInstrPredicate(*MI) != ARMVCC::Then)
|
|
|
|
return false;
|
|
|
|
// Skip other instructions that build up the predicate.
|
|
|
|
if (MI->getOpcode() == ARM::MVE_VPST || isVectorPredicate(MI))
|
|
|
|
continue;
|
|
|
|
// Check that any other instructions are predicated upon a vctp.
|
|
|
|
// TODO: We could infer when VPTs are implicitly predicated on the
|
|
|
|
// vctp (when the operands are predicated).
|
|
|
|
if (!isPredicatedOnVCTP(MI)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Can't convert: " << *MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VPTState(MachineInstr *MI) { Insts.push_back(MI); }
|
|
|
|
|
|
|
|
void insert(MachineInstr *MI) {
|
|
|
|
Insts.push_back(MI);
|
|
|
|
// VPT/VPST + 4 predicated instructions.
|
|
|
|
assert(Insts.size() <= 5 && "Too many instructions in VPT block!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool containsVCTP() const {
|
|
|
|
for (auto *MI : Insts)
|
|
|
|
if (isVCTP(MI))
|
|
|
|
return true;
|
|
|
|
return false;
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned size() const { return Insts.size(); }
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &getInsts() { return Insts; }
|
2019-12-20 16:42:11 +08:00
|
|
|
};
|
|
|
|
|
2019-11-19 01:07:56 +08:00
|
|
|
struct LowOverheadLoop {
|
|
|
|
|
2020-02-14 16:28:26 +08:00
|
|
|
MachineLoop &ML;
|
2020-07-01 15:27:12 +08:00
|
|
|
MachineBasicBlock *Preheader = nullptr;
|
2020-02-14 16:28:26 +08:00
|
|
|
MachineLoopInfo &MLI;
|
|
|
|
ReachingDefAnalysis &RDA;
|
2020-02-18 22:05:39 +08:00
|
|
|
const TargetRegisterInfo &TRI;
|
2020-07-01 15:27:12 +08:00
|
|
|
const ARMBaseInstrInfo &TII;
|
2019-11-19 01:07:56 +08:00
|
|
|
MachineFunction *MF = nullptr;
|
2020-09-30 22:15:42 +08:00
|
|
|
MachineBasicBlock::iterator StartInsertPt;
|
|
|
|
MachineBasicBlock *StartInsertBB = nullptr;
|
2019-11-19 01:07:56 +08:00
|
|
|
MachineInstr *Start = nullptr;
|
|
|
|
MachineInstr *Dec = nullptr;
|
|
|
|
MachineInstr *End = nullptr;
|
2020-08-17 23:03:55 +08:00
|
|
|
MachineOperand TPNumElements;
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallVector<MachineInstr*, 4> VCTPs;
|
2020-01-17 21:08:24 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 4> ToRemove;
|
2020-04-08 18:55:09 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 4> BlockMasksToRecompute;
|
2021-09-22 19:07:52 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 4> DoubleWidthResultInstrs;
|
2019-11-19 01:07:56 +08:00
|
|
|
bool Revert = false;
|
|
|
|
bool CannotTailPredicate = false;
|
|
|
|
|
2020-02-14 16:28:26 +08:00
|
|
|
LowOverheadLoop(MachineLoop &ML, MachineLoopInfo &MLI,
|
2020-07-01 15:27:12 +08:00
|
|
|
ReachingDefAnalysis &RDA, const TargetRegisterInfo &TRI,
|
|
|
|
const ARMBaseInstrInfo &TII)
|
2020-08-17 23:03:55 +08:00
|
|
|
: ML(ML), MLI(MLI), RDA(RDA), TRI(TRI), TII(TII),
|
|
|
|
TPNumElements(MachineOperand::CreateImm(0)) {
|
2020-02-14 16:28:26 +08:00
|
|
|
MF = ML.getHeader()->getParent();
|
2020-07-01 15:27:12 +08:00
|
|
|
if (auto *MBB = ML.getLoopPreheader())
|
|
|
|
Preheader = MBB;
|
2021-05-24 19:22:15 +08:00
|
|
|
else if (auto *MBB = MLI.findLoopPreheader(&ML, true, true))
|
2020-07-01 15:27:12 +08:00
|
|
|
Preheader = MBB;
|
2020-09-22 16:22:11 +08:00
|
|
|
VPTState::reset();
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an MVE instruction, check that we know how to use tail
|
2020-01-14 20:02:32 +08:00
|
|
|
// predication with it. Record VPT blocks and return whether the
|
|
|
|
// instruction is valid for tail predication.
|
|
|
|
bool ValidateMVEInst(MachineInstr *MI);
|
2019-11-19 01:07:56 +08:00
|
|
|
|
2020-01-14 20:02:32 +08:00
|
|
|
void AnalyseMVEInst(MachineInstr *MI) {
|
|
|
|
CannotTailPredicate = !ValidateMVEInst(MI);
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsTailPredicationLegal() const {
|
|
|
|
// For now, let's keep things really simple and only support a single
|
|
|
|
// block for tail predication.
|
2020-09-22 16:22:11 +08:00
|
|
|
return !Revert && FoundAllComponents() && !VCTPs.empty() &&
|
2020-02-14 16:28:26 +08:00
|
|
|
!CannotTailPredicate && ML.getNumBlocks() == 1;
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// Given that MI is a VCTP, check that is equivalent to any other VCTPs
|
|
|
|
// found.
|
|
|
|
bool AddVCTP(MachineInstr *MI);
|
|
|
|
|
2020-02-18 22:05:39 +08:00
|
|
|
// Check that the predication in the loop will be equivalent once we
|
|
|
|
// perform the conversion. Also ensure that we can provide the number
|
|
|
|
// of elements to the loop start instruction.
|
2020-09-30 22:15:42 +08:00
|
|
|
bool ValidateTailPredicate();
|
2020-01-03 16:48:33 +08:00
|
|
|
|
2020-02-18 22:05:39 +08:00
|
|
|
// Check that any values available outside of the loop will be the same
|
|
|
|
// after tail predication conversion.
|
2020-07-01 15:27:12 +08:00
|
|
|
bool ValidateLiveOuts();
|
2020-02-18 22:05:39 +08:00
|
|
|
|
2019-11-19 01:07:56 +08:00
|
|
|
// Is it safe to define LR with DLS/WLS?
|
|
|
|
// LR can be defined if it is the operand to start, because it's the same
|
|
|
|
// value, or if it's going to be equivalent to the operand to Start.
|
2020-01-29 16:26:11 +08:00
|
|
|
MachineInstr *isSafeToDefineLR();
|
2019-11-19 01:07:56 +08:00
|
|
|
|
2019-11-26 18:03:25 +08:00
|
|
|
// Check the branch targets are within range and we satisfy our
|
|
|
|
// restrictions.
|
2020-09-25 16:36:40 +08:00
|
|
|
void Validate(ARMBasicBlockUtils *BBUtils);
|
2019-11-19 01:07:56 +08:00
|
|
|
|
|
|
|
bool FoundAllComponents() const {
|
|
|
|
return Start && Dec && End;
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallVectorImpl<VPTState> &getVPTBlocks() {
|
|
|
|
return VPTState::Blocks;
|
|
|
|
}
|
2019-12-20 16:42:11 +08:00
|
|
|
|
2020-08-17 23:03:55 +08:00
|
|
|
// Return the operand for the loop start instruction. This will be the loop
|
|
|
|
// iteration count, or the number of elements if we're tail predicating.
|
|
|
|
MachineOperand &getLoopStartOperand() {
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
if (IsTailPredicationLegal())
|
|
|
|
return TPNumElements;
|
[ARM] Improve WLS lowering
Recently we improved the lowering of low overhead loops and tail
predicated loops, but concentrated first on the DLS do style loops. This
extends those improvements over to the WLS while loops, improving the
chance of lowering them successfully. To do this the lowering has to
change a little as the instructions are terminators that produce a value
- something that needs to be treated carefully.
Lowering starts at the Hardware Loop pass, inserting a new
llvm.test.start.loop.iterations that produces both an i1 to control the
loop entry and an i32 similar to the llvm.start.loop.iterations
intrinsic added for do loops. This feeds into the loop phi, properly
gluing the values together:
%wls = call { i32, i1 } @llvm.test.start.loop.iterations.i32(i32 %div)
%wls0 = extractvalue { i32, i1 } %wls, 0
%wls1 = extractvalue { i32, i1 } %wls, 1
br i1 %wls1, label %loop.ph, label %loop.exit
...
loop:
%lsr.iv = phi i32 [ %wls0, %loop.ph ], [ %iv.next, %loop ]
..
%iv.next = call i32 @llvm.loop.decrement.reg.i32(i32 %lsr.iv, i32 1)
%cmp = icmp ne i32 %iv.next, 0
br i1 %cmp, label %loop, label %loop.exit
The llvm.test.start.loop.iterations need to be lowered through ISel
lowering as a pair of WLS and WLSSETUP nodes, which each get converted
to t2WhileLoopSetup and t2WhileLoopStart Pseudos. This helps prevent
t2WhileLoopStart from being a terminator that produces a value,
something difficult to control at that stage in the pipeline. Instead
the t2WhileLoopSetup produces the value of LR (essentially acting as a
lr = subs rn, 0), t2WhileLoopStart consumes that lr value (the Bcc).
These are then converted into a single t2WhileLoopStartLR at the same
point as t2DoLoopStartTP and t2LoopEndDec. Otherwise we revert the loop
to prevent them from progressing further in the pipeline. The
t2WhileLoopStartLR is a single instruction that takes a GPR and produces
LR, similar to the WLS instruction.
%1:gprlr = t2WhileLoopStartLR %0:rgpr, %bb.3
t2B %bb.1
...
bb.2.loop:
%2:gprlr = PHI %1:gprlr, %bb.1, %3:gprlr, %bb.2
...
%3:gprlr = t2LoopEndDec %2:gprlr, %bb.2
t2B %bb.3
The t2WhileLoopStartLR can then be treated similar to the other low
overhead loop pseudos, eventually being lowered to a WLS providing the
branches are within range.
Differential Revision: https://reviews.llvm.org/D97729
2021-03-11 22:06:04 +08:00
|
|
|
return Start->getOperand(1);
|
2019-11-26 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getStartOpcode() const {
|
2021-06-13 20:55:34 +08:00
|
|
|
bool IsDo = isDoLoopStart(*Start);
|
2019-11-26 18:25:04 +08:00
|
|
|
if (!IsTailPredicationLegal())
|
|
|
|
return IsDo ? ARM::t2DLS : ARM::t2WLS;
|
2019-12-16 17:11:47 +08:00
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
return VCTPOpcodeToLSTP(VCTPs.back()->getOpcode(), IsDo);
|
2019-11-26 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:56 +08:00
|
|
|
void dump() const {
|
|
|
|
if (Start) dbgs() << "ARM Loops: Found Loop Start: " << *Start;
|
|
|
|
if (Dec) dbgs() << "ARM Loops: Found Loop Dec: " << *Dec;
|
|
|
|
if (End) dbgs() << "ARM Loops: Found Loop End: " << *End;
|
2020-09-22 16:22:11 +08:00
|
|
|
if (!VCTPs.empty()) {
|
|
|
|
dbgs() << "ARM Loops: Found VCTP(s):\n";
|
|
|
|
for (auto *MI : VCTPs)
|
|
|
|
dbgs() << " - " << *MI;
|
|
|
|
}
|
2019-11-19 01:07:56 +08:00
|
|
|
if (!FoundAllComponents())
|
|
|
|
dbgs() << "ARM Loops: Not a low-overhead loop.\n";
|
|
|
|
else if (!(Start && Dec && End))
|
|
|
|
dbgs() << "ARM Loops: Failed to find all loop components.\n";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
class ARMLowOverheadLoops : public MachineFunctionPass {
|
2019-09-17 20:19:32 +08:00
|
|
|
MachineFunction *MF = nullptr;
|
2019-11-26 18:25:04 +08:00
|
|
|
MachineLoopInfo *MLI = nullptr;
|
2019-11-26 18:03:25 +08:00
|
|
|
ReachingDefAnalysis *RDA = nullptr;
|
2019-06-25 18:45:51 +08:00
|
|
|
const ARMBaseInstrInfo *TII = nullptr;
|
|
|
|
MachineRegisterInfo *MRI = nullptr;
|
[ARM][LowOverheadLoops] Remove dead loop update instructions.
After creating a low-overhead loop, the loop update instruction was still
lingering around hurting performance. This removes dead loop update
instructions, which in our case are mostly SUBS instructions.
To support this, some helper functions were added to MachineLoopUtils and
ReachingDefAnalysis to analyse live-ins of loop exit blocks and find uses
before a particular loop instruction, respectively.
This is a first version that removes a SUBS instruction when there are no other
uses inside and outside the loop block, but there are some more interesting
cases in test/CodeGen/Thumb2/LowOverheadLoops/mve-tail-data-types.ll which
shows that there is room for improvement. For example, we can't handle this
case yet:
..
dlstp.32 lr, r2
.LBB0_1:
mov r3, r2
subs r2, #4
vldrh.u32 q2, [r1], #8
vmov q1, q0
vmla.u32 q0, q2, r0
letp lr, .LBB0_1
@ %bb.2:
vctp.32 r3
..
which is a lot more tricky because r2 is not only used by the subs, but also by
the mov to r3, which is used outside the low-overhead loop by the vctp
instruction, and that requires a bit of a different approach, and I will follow
up on this.
Differential Revision: https://reviews.llvm.org/D71007
2019-12-11 18:11:48 +08:00
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
2019-06-25 18:45:51 +08:00
|
|
|
std::unique_ptr<ARMBasicBlockUtils> BBUtils = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
ARMLowOverheadLoops() : MachineFunctionPass(ID) { }
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2019-11-26 18:03:25 +08:00
|
|
|
AU.addRequired<ReachingDefAnalysis>();
|
2019-06-25 18:45:51 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
2019-09-17 20:19:32 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2019-11-26 18:03:25 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs).set(
|
|
|
|
MachineFunctionProperties::Property::TracksLiveness);
|
2019-09-17 20:19:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return ARM_LOW_OVERHEAD_LOOPS_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-06-25 18:45:51 +08:00
|
|
|
bool ProcessLoop(MachineLoop *ML);
|
|
|
|
|
2019-09-17 20:19:32 +08:00
|
|
|
bool RevertNonLoops();
|
2019-07-22 22:16:40 +08:00
|
|
|
|
2019-07-10 20:29:43 +08:00
|
|
|
void RevertWhile(MachineInstr *MI) const;
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
void RevertDo(MachineInstr *MI) const;
|
2019-07-10 20:29:43 +08:00
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
bool RevertLoopDec(MachineInstr *MI) const;
|
2019-07-10 20:29:43 +08:00
|
|
|
|
2019-09-23 16:57:50 +08:00
|
|
|
void RevertLoopEnd(MachineInstr *MI, bool SkipCmp = false) const;
|
2019-07-10 20:29:43 +08:00
|
|
|
|
2020-12-10 20:14:23 +08:00
|
|
|
void RevertLoopEndDec(MachineInstr *MI) const;
|
|
|
|
|
2019-12-20 16:42:11 +08:00
|
|
|
void ConvertVPTBlocks(LowOverheadLoop &LoLoop);
|
2019-11-19 01:07:56 +08:00
|
|
|
|
|
|
|
MachineInstr *ExpandLoopStart(LowOverheadLoop &LoLoop);
|
|
|
|
|
|
|
|
void Expand(LowOverheadLoop &LoLoop);
|
2019-06-25 18:45:51 +08:00
|
|
|
|
2020-02-05 23:15:46 +08:00
|
|
|
void IterationCountDCE(LowOverheadLoop &LoLoop);
|
2019-06-25 18:45:51 +08:00
|
|
|
};
|
|
|
|
}
|
2019-07-24 21:30:36 +08:00
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
char ARMLowOverheadLoops::ID = 0;
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallVector<VPTState, 4> VPTState::Blocks;
|
|
|
|
SetVector<MachineInstr *> VPTState::CurrentPredicates;
|
|
|
|
std::map<MachineInstr *,
|
|
|
|
std::unique_ptr<PredicatedMI>> VPTState::PredicatedInsts;
|
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
INITIALIZE_PASS(ARMLowOverheadLoops, DEBUG_TYPE, ARM_LOW_OVERHEAD_LOOPS_NAME,
|
|
|
|
false, false)
|
|
|
|
|
2020-09-30 16:36:57 +08:00
|
|
|
static bool TryRemove(MachineInstr *MI, ReachingDefAnalysis &RDA,
|
|
|
|
InstSet &ToRemove, InstSet &Ignore) {
|
|
|
|
|
|
|
|
// Check that we can remove all of Killed without having to modify any IT
|
|
|
|
// blocks.
|
|
|
|
auto WontCorruptITs = [](InstSet &Killed, ReachingDefAnalysis &RDA) {
|
|
|
|
// Collect the dead code and the MBBs in which they reside.
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 2> BasicBlocks;
|
|
|
|
for (auto *Dead : Killed)
|
|
|
|
BasicBlocks.insert(Dead->getParent());
|
|
|
|
|
|
|
|
// Collect IT blocks in all affected basic blocks.
|
|
|
|
std::map<MachineInstr *, SmallPtrSet<MachineInstr *, 2>> ITBlocks;
|
|
|
|
for (auto *MBB : BasicBlocks) {
|
|
|
|
for (auto &IT : *MBB) {
|
|
|
|
if (IT.getOpcode() != ARM::t2IT)
|
|
|
|
continue;
|
2020-10-22 04:59:45 +08:00
|
|
|
RDA.getReachingLocalUses(&IT, MCRegister::from(ARM::ITSTATE),
|
|
|
|
ITBlocks[&IT]);
|
2020-09-30 16:36:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're removing all of the instructions within an IT block, then
|
|
|
|
// also remove the IT instruction.
|
|
|
|
SmallPtrSet<MachineInstr *, 2> ModifiedITs;
|
|
|
|
SmallPtrSet<MachineInstr *, 2> RemoveITs;
|
|
|
|
for (auto *Dead : Killed) {
|
|
|
|
if (MachineOperand *MO = Dead->findRegisterUseOperand(ARM::ITSTATE)) {
|
|
|
|
MachineInstr *IT = RDA.getMIOperand(Dead, *MO);
|
|
|
|
RemoveITs.insert(IT);
|
|
|
|
auto &CurrentBlock = ITBlocks[IT];
|
|
|
|
CurrentBlock.erase(Dead);
|
|
|
|
if (CurrentBlock.empty())
|
|
|
|
ModifiedITs.erase(IT);
|
|
|
|
else
|
|
|
|
ModifiedITs.insert(IT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ModifiedITs.empty())
|
|
|
|
return false;
|
|
|
|
Killed.insert(RemoveITs.begin(), RemoveITs.end());
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr *, 2> Uses;
|
|
|
|
if (!RDA.isSafeToRemove(MI, Uses, Ignore))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (WontCorruptITs(Uses, RDA)) {
|
|
|
|
ToRemove.insert(Uses.begin(), Uses.end());
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Able to remove: " << *MI
|
|
|
|
<< " - can also remove:\n";
|
|
|
|
for (auto *Use : Uses)
|
|
|
|
dbgs() << " - " << *Use);
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr*, 4> Killed;
|
|
|
|
RDA.collectKilledOperands(MI, Killed);
|
|
|
|
if (WontCorruptITs(Killed, RDA)) {
|
|
|
|
ToRemove.insert(Killed.begin(), Killed.end());
|
|
|
|
LLVM_DEBUG(for (auto *Dead : Killed)
|
|
|
|
dbgs() << " - " << *Dead);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:15:42 +08:00
|
|
|
bool LowOverheadLoop::ValidateTailPredicate() {
|
2020-09-25 16:36:40 +08:00
|
|
|
if (!IsTailPredicationLegal()) {
|
|
|
|
LLVM_DEBUG(if (VCTPs.empty())
|
|
|
|
dbgs() << "ARM Loops: Didn't find a VCTP instruction.\n";
|
|
|
|
dbgs() << "ARM Loops: Tail-predication is not valid.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
assert(!VCTPs.empty() && "VCTP instruction expected but is not set");
|
2020-09-25 16:36:40 +08:00
|
|
|
assert(ML.getBlocks().size() == 1 &&
|
|
|
|
"Shouldn't be processing a loop with more than one block");
|
2020-09-22 16:22:11 +08:00
|
|
|
|
2020-09-24 18:47:30 +08:00
|
|
|
if (DisableTailPredication) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: tail-predication is disabled\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:37:47 +08:00
|
|
|
if (!VPTState::isValid(RDA)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Invalid VPT state.\n");
|
2020-09-22 16:22:11 +08:00
|
|
|
return false;
|
2020-10-01 20:37:47 +08:00
|
|
|
}
|
2019-12-20 16:42:11 +08:00
|
|
|
|
2020-08-28 20:56:16 +08:00
|
|
|
if (!ValidateLiveOuts()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Invalid live outs.\n");
|
2020-02-18 22:05:39 +08:00
|
|
|
return false;
|
2020-08-28 20:56:16 +08:00
|
|
|
}
|
2020-02-18 22:05:39 +08:00
|
|
|
|
2019-11-26 18:25:04 +08:00
|
|
|
// For tail predication, we need to provide the number of elements, instead
|
|
|
|
// of the iteration count, to the loop start instruction. The number of
|
|
|
|
// elements is provided to the vctp instruction, so we need to check that
|
|
|
|
// we can use this register at InsertPt.
|
2020-09-22 16:22:11 +08:00
|
|
|
MachineInstr *VCTP = VCTPs.back();
|
2021-06-13 20:55:34 +08:00
|
|
|
if (Start->getOpcode() == ARM::t2DoLoopStartTP ||
|
|
|
|
Start->getOpcode() == ARM::t2WhileLoopStartTP) {
|
2020-11-11 02:08:12 +08:00
|
|
|
TPNumElements = Start->getOperand(2);
|
|
|
|
StartInsertPt = Start;
|
|
|
|
StartInsertBB = Start->getParent();
|
|
|
|
} else {
|
|
|
|
TPNumElements = VCTP->getOperand(1);
|
|
|
|
MCRegister NumElements = TPNumElements.getReg().asMCReg();
|
|
|
|
|
|
|
|
// If the register is defined within loop, then we can't perform TP.
|
|
|
|
// TODO: Check whether this is just a mov of a register that would be
|
|
|
|
// available.
|
|
|
|
if (RDA.hasLocalDefBefore(VCTP, NumElements)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: VCTP operand is defined in the loop.\n");
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-26 18:25:04 +08:00
|
|
|
|
2020-11-11 02:08:12 +08:00
|
|
|
// The element count register maybe defined after InsertPt, in which case we
|
|
|
|
// need to try to move either InsertPt or the def so that the [w|d]lstp can
|
|
|
|
// use the value.
|
|
|
|
|
|
|
|
if (StartInsertPt != StartInsertBB->end() &&
|
|
|
|
!RDA.isReachingDefLiveOut(&*StartInsertPt, NumElements)) {
|
|
|
|
if (auto *ElemDef =
|
|
|
|
RDA.getLocalLiveOutMIDef(StartInsertBB, NumElements)) {
|
|
|
|
if (RDA.isSafeToMoveForwards(ElemDef, &*StartInsertPt)) {
|
|
|
|
ElemDef->removeFromParent();
|
|
|
|
StartInsertBB->insert(StartInsertPt, ElemDef);
|
2020-10-20 15:55:21 +08:00
|
|
|
LLVM_DEBUG(dbgs()
|
2020-11-11 02:08:12 +08:00
|
|
|
<< "ARM Loops: Moved element count def: " << *ElemDef);
|
|
|
|
} else if (RDA.isSafeToMoveBackwards(&*StartInsertPt, ElemDef)) {
|
|
|
|
StartInsertPt->removeFromParent();
|
|
|
|
StartInsertBB->insertAfter(MachineBasicBlock::iterator(ElemDef),
|
|
|
|
&*StartInsertPt);
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Moved start past: " << *ElemDef);
|
|
|
|
} else {
|
|
|
|
// If we fail to move an instruction and the element count is provided
|
|
|
|
// by a mov, use the mov operand if it will have the same value at the
|
|
|
|
// insertion point
|
|
|
|
MachineOperand Operand = ElemDef->getOperand(1);
|
|
|
|
if (isMovRegOpcode(ElemDef->getOpcode()) &&
|
|
|
|
RDA.getUniqueReachingMIDef(ElemDef, Operand.getReg().asMCReg()) ==
|
|
|
|
RDA.getUniqueReachingMIDef(&*StartInsertPt,
|
|
|
|
Operand.getReg().asMCReg())) {
|
|
|
|
TPNumElements = Operand;
|
|
|
|
NumElements = TPNumElements.getReg();
|
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs()
|
|
|
|
<< "ARM Loops: Unable to move element count to loop "
|
|
|
|
<< "start instruction.\n");
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-20 15:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 02:08:12 +08:00
|
|
|
|
|
|
|
// Especially in the case of while loops, InsertBB may not be the
|
|
|
|
// preheader, so we need to check that the register isn't redefined
|
|
|
|
// before entering the loop.
|
|
|
|
auto CannotProvideElements = [this](MachineBasicBlock *MBB,
|
|
|
|
MCRegister NumElements) {
|
|
|
|
if (MBB->empty())
|
|
|
|
return false;
|
|
|
|
// NumElements is redefined in this block.
|
|
|
|
if (RDA.hasLocalDefBefore(&MBB->back(), NumElements))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Don't continue searching up through multiple predecessors.
|
|
|
|
if (MBB->pred_size() > 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Search backwards for a def, until we get to InsertBB.
|
|
|
|
MachineBasicBlock *MBB = Preheader;
|
|
|
|
while (MBB && MBB != StartInsertBB) {
|
|
|
|
if (CannotProvideElements(MBB, NumElements)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Unable to provide element count.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MBB = *MBB->pred_begin();
|
|
|
|
}
|
2020-10-20 15:55:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:55:17 +08:00
|
|
|
// Could inserting the [W|D]LSTP cause some unintended affects? In a perfect
|
|
|
|
// world the [w|d]lstp instruction would be last instruction in the preheader
|
|
|
|
// and so it would only affect instructions within the loop body. But due to
|
2020-10-20 15:55:21 +08:00
|
|
|
// scheduling, and/or the logic in this pass (above), the insertion point can
|
2020-09-24 19:55:17 +08:00
|
|
|
// be moved earlier. So if the Loop Start isn't the last instruction in the
|
|
|
|
// preheader, and if the initial element count is smaller than the vector
|
|
|
|
// width, the Loop Start instruction will immediately generate one or more
|
|
|
|
// false lane mask which can, incorrectly, affect the proceeding MVE
|
|
|
|
// instructions in the preheader.
|
2020-11-11 18:02:20 +08:00
|
|
|
if (std::any_of(StartInsertPt, StartInsertBB->end(), shouldInspect)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Instruction blocks [W|D]LSTP\n");
|
2020-09-24 19:55:17 +08:00
|
|
|
return false;
|
2020-11-11 18:02:20 +08:00
|
|
|
}
|
2020-09-24 19:55:17 +08:00
|
|
|
|
2021-09-22 19:07:52 +08:00
|
|
|
// For any DoubleWidthResultInstrs we found whilst scanning instructions, they
|
|
|
|
// need to compute an output size that is smaller than the VCTP mask operates
|
|
|
|
// on. The VecSize of the DoubleWidthResult is the larger vector size - the
|
|
|
|
// size it extends into, so any VCTP VecSize <= is valid.
|
|
|
|
unsigned VCTPVecSize = getVecSize(*VCTP);
|
|
|
|
for (MachineInstr *MI : DoubleWidthResultInstrs) {
|
|
|
|
unsigned InstrVecSize = getVecSize(*MI);
|
|
|
|
if (InstrVecSize > VCTPVecSize) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Double width result larger than VCTP "
|
|
|
|
<< "VecSize:\n" << *MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:08:24 +08:00
|
|
|
// Check that the value change of the element count is what we expect and
|
|
|
|
// that the predication will be equivalent. For this we need:
|
|
|
|
// NumElements = NumElements - VectorWidth. The sub will be a sub immediate
|
|
|
|
// and we can also allow register copies within the chain too.
|
2020-04-22 20:30:22 +08:00
|
|
|
auto IsValidSub = [](MachineInstr *MI, int ExpectedVecWidth) {
|
|
|
|
return -getAddSubImmediate(*MI) == ExpectedVecWidth;
|
2020-01-17 21:08:24 +08:00
|
|
|
};
|
|
|
|
|
2020-11-11 02:08:12 +08:00
|
|
|
MachineBasicBlock *MBB = VCTP->getParent();
|
2020-09-07 17:39:14 +08:00
|
|
|
// Remove modifications to the element count since they have no purpose in a
|
|
|
|
// tail predicated loop. Explicitly refer to the vctp operand no matter which
|
|
|
|
// register NumElements has been assigned to, since that is what the
|
|
|
|
// modifications will be using
|
2020-10-22 04:59:45 +08:00
|
|
|
if (auto *Def = RDA.getUniqueReachingMIDef(
|
|
|
|
&MBB->back(), VCTP->getOperand(1).getReg().asMCReg())) {
|
2020-01-17 21:08:24 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 2> ElementChain;
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 2> Ignore;
|
2020-01-17 21:08:24 +08:00
|
|
|
unsigned ExpectedVectorWidth = getTailPredVectorWidth(VCTP->getOpcode());
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
Ignore.insert(VCTPs.begin(), VCTPs.end());
|
2020-04-08 21:31:21 +08:00
|
|
|
|
2020-09-30 16:36:57 +08:00
|
|
|
if (TryRemove(Def, RDA, ElementChain, Ignore)) {
|
2020-01-17 21:08:24 +08:00
|
|
|
bool FoundSub = false;
|
|
|
|
|
|
|
|
for (auto *MI : ElementChain) {
|
|
|
|
if (isMovRegOpcode(MI->getOpcode()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isSubImmOpcode(MI->getOpcode())) {
|
2020-10-01 20:37:47 +08:00
|
|
|
if (FoundSub || !IsValidSub(MI, ExpectedVectorWidth)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Unexpected instruction in element"
|
|
|
|
" count: " << *MI);
|
2020-01-17 21:08:24 +08:00
|
|
|
return false;
|
2020-10-01 20:37:47 +08:00
|
|
|
}
|
2020-01-17 21:08:24 +08:00
|
|
|
FoundSub = true;
|
2020-10-01 20:37:47 +08:00
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Unexpected instruction in element"
|
|
|
|
" count: " << *MI);
|
2020-01-17 21:08:24 +08:00
|
|
|
return false;
|
2020-10-01 20:37:47 +08:00
|
|
|
}
|
2020-01-17 21:08:24 +08:00
|
|
|
}
|
|
|
|
ToRemove.insert(ElementChain.begin(), ElementChain.end());
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 18:48:20 +08:00
|
|
|
|
2021-06-13 20:55:34 +08:00
|
|
|
// If we converted the LoopStart to a t2DoLoopStartTP/t2WhileLoopStartTP, we
|
|
|
|
// can also remove any extra instructions in the preheader, which often
|
|
|
|
// includes a now unused MOV.
|
|
|
|
if ((Start->getOpcode() == ARM::t2DoLoopStartTP ||
|
|
|
|
Start->getOpcode() == ARM::t2WhileLoopStartTP) &&
|
|
|
|
Preheader && !Preheader->empty() &&
|
2021-02-11 18:48:20 +08:00
|
|
|
!RDA.hasLocalDefBefore(VCTP, VCTP->getOperand(1).getReg())) {
|
|
|
|
if (auto *Def = RDA.getUniqueReachingMIDef(
|
|
|
|
&Preheader->back(), VCTP->getOperand(1).getReg().asMCReg())) {
|
|
|
|
SmallPtrSet<MachineInstr*, 2> Ignore;
|
|
|
|
Ignore.insert(VCTPs.begin(), VCTPs.end());
|
|
|
|
TryRemove(Def, RDA, ToRemove, Ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:48:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
static bool isRegInClass(const MachineOperand &MO,
|
|
|
|
const TargetRegisterClass *Class) {
|
|
|
|
return MO.isReg() && MO.getReg() && Class->contains(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2020-03-27 21:58:50 +08:00
|
|
|
// MVE 'narrowing' operate on half a lane, reading from half and writing
|
|
|
|
// to half, which are referred to has the top and bottom half. The other
|
|
|
|
// half retains its previous value.
|
|
|
|
static bool retainsPreviousHalfElement(const MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
uint64_t Flags = MCID.TSFlags;
|
|
|
|
return (Flags & ARMII::RetainsPreviousHalfElement) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some MVE instructions read from the top/bottom halves of their operand(s)
|
|
|
|
// and generate a vector result with result elements that are double the
|
|
|
|
// width of the input.
|
|
|
|
static bool producesDoubleWidthResult(const MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
uint64_t Flags = MCID.TSFlags;
|
|
|
|
return (Flags & ARMII::DoubleWidthResult) != 0;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:54:25 +08:00
|
|
|
static bool isHorizontalReduction(const MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
uint64_t Flags = MCID.TSFlags;
|
|
|
|
return (Flags & ARMII::HorizontalReduction) != 0;
|
|
|
|
}
|
|
|
|
|
2020-03-24 16:41:48 +08:00
|
|
|
// Can this instruction generate a non-zero result when given only zeroed
|
|
|
|
// operands? This allows us to know that, given operands with false bytes
|
|
|
|
// zeroed by masked loads, that the result will also contain zeros in those
|
|
|
|
// bytes.
|
|
|
|
static bool canGenerateNonZeros(const MachineInstr &MI) {
|
2020-03-27 21:58:50 +08:00
|
|
|
|
|
|
|
// Check for instructions which can write into a larger element size,
|
|
|
|
// possibly writing into a previous zero'd lane.
|
|
|
|
if (producesDoubleWidthResult(MI))
|
|
|
|
return true;
|
|
|
|
|
2020-03-24 16:41:48 +08:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
2020-03-27 21:58:50 +08:00
|
|
|
// FIXME: VNEG FP and -0? I think we'll need to handle this once we allow
|
|
|
|
// fp16 -> fp32 vector conversions.
|
|
|
|
// Instructions that perform a NOT will generate 1s from 0s.
|
2020-03-24 16:41:48 +08:00
|
|
|
case ARM::MVE_VMVN:
|
|
|
|
case ARM::MVE_VORN:
|
2020-03-27 21:58:50 +08:00
|
|
|
// Count leading zeros will do just that!
|
2020-03-24 16:41:48 +08:00
|
|
|
case ARM::MVE_VCLZs8:
|
|
|
|
case ARM::MVE_VCLZs16:
|
|
|
|
case ARM::MVE_VCLZs32:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at its register uses to see if it only can only receive zeros
|
|
|
|
// into its false lanes which would then produce zeros. Also check that
|
2020-03-30 16:54:25 +08:00
|
|
|
// the output register is also defined by an FalseLanesZero instruction
|
2020-03-24 16:41:48 +08:00
|
|
|
// so that if tail-predication happens, the lanes that aren't updated will
|
|
|
|
// still be zeros.
|
2020-03-30 16:54:25 +08:00
|
|
|
static bool producesFalseLanesZero(MachineInstr &MI,
|
2020-03-24 16:41:48 +08:00
|
|
|
const TargetRegisterClass *QPRs,
|
|
|
|
const ReachingDefAnalysis &RDA,
|
2020-03-30 16:54:25 +08:00
|
|
|
InstSet &FalseLanesZero) {
|
2020-03-24 16:41:48 +08:00
|
|
|
if (canGenerateNonZeros(MI))
|
|
|
|
return false;
|
2020-03-30 16:54:25 +08:00
|
|
|
|
2020-08-28 20:56:16 +08:00
|
|
|
bool isPredicated = isVectorPredicated(&MI);
|
|
|
|
// Predicated loads will write zeros to the falsely predicated bytes of the
|
|
|
|
// destination register.
|
|
|
|
if (MI.mayLoad())
|
|
|
|
return isPredicated;
|
|
|
|
|
|
|
|
auto IsZeroInit = [](MachineInstr *Def) {
|
|
|
|
return !isVectorPredicated(Def) &&
|
|
|
|
Def->getOpcode() == ARM::MVE_VMOVimmi32 &&
|
|
|
|
Def->getOperand(1).getImm() == 0;
|
|
|
|
};
|
|
|
|
|
2020-03-30 16:54:25 +08:00
|
|
|
bool AllowScalars = isHorizontalReduction(MI);
|
2020-03-24 16:41:48 +08:00
|
|
|
for (auto &MO : MI.operands()) {
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
2020-03-30 16:54:25 +08:00
|
|
|
if (!isRegInClass(MO, QPRs) && AllowScalars)
|
|
|
|
continue;
|
[ARM] Add a tail-predication loop predicate register
The semantics of tail predication loops means that the value of LR as an
instruction is executed determines the predicate. In other words:
mov r3, #3
DLSTP lr, r3 // Start tail predication, lr==3
VADD.s32 q0, q1, q2 // Lanes 0,1 and 2 are updated in q0.
mov lr, #1
VADD.s32 q0, q1, q2 // Only first lane is updated.
This means that the value of lr cannot be spilled and re-used in tail
predication regions without potentially altering the behaviour of the
program. More lanes than required could be stored, for example, and in
the case of a gather those lanes might not have been setup, leading to
alignment exceptions.
This patch adds a new lr predicate operand to MVE instructions in order
to keep a reference to the lr that they use as a tail predicate. It will
usually hold the zeroreg meaning not predicated, being set to the LR phi
value in the MVETPAndVPTOptimisationsPass. This will prevent it from
being spilled anywhere that it needs to be used.
A lot of tests needed updating.
Differential Revision: https://reviews.llvm.org/D107638
2021-09-02 20:42:58 +08:00
|
|
|
// Skip the lr predicate reg
|
|
|
|
int PIdx = llvm::findFirstVPTPredOperandIdx(MI);
|
|
|
|
if (PIdx != -1 && (int)MI.getOperandNo(&MO) == PIdx + 2)
|
|
|
|
continue;
|
2020-07-01 15:27:12 +08:00
|
|
|
|
2020-08-28 20:56:16 +08:00
|
|
|
// Check that this instruction will produce zeros in its false lanes:
|
|
|
|
// - If it only consumes false lanes zero or constant 0 (vmov #0)
|
|
|
|
// - If it's predicated, it only matters that it's def register already has
|
|
|
|
// false lane zeros, so we can ignore the uses.
|
|
|
|
SmallPtrSet<MachineInstr *, 2> Defs;
|
|
|
|
RDA.getGlobalReachingDefs(&MI, MO.getReg(), Defs);
|
|
|
|
for (auto *Def : Defs) {
|
|
|
|
if (Def == &MI || FalseLanesZero.count(Def) || IsZeroInit(Def))
|
|
|
|
continue;
|
|
|
|
if (MO.isUse() && isPredicated)
|
|
|
|
continue;
|
2020-07-01 15:27:12 +08:00
|
|
|
return false;
|
2020-08-28 20:56:16 +08:00
|
|
|
}
|
2020-07-01 15:27:12 +08:00
|
|
|
}
|
2020-08-28 20:56:16 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Always False Zeros: " << MI);
|
2020-07-01 15:27:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowOverheadLoop::ValidateLiveOuts() {
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
// We want to find out if the tail-predicated version of this loop will
|
|
|
|
// produce the same values as the loop in its original form. For this to
|
|
|
|
// be true, the newly inserted implicit predication must not change the
|
|
|
|
// the (observable) results.
|
|
|
|
// We're doing this because many instructions in the loop will not be
|
|
|
|
// predicated and so the conversion from VPT predication to tail-predication
|
|
|
|
// can result in different values being produced; due to the tail-predication
|
|
|
|
// preventing many instructions from updating their falsely predicated
|
|
|
|
// lanes. This analysis assumes that all the instructions perform lane-wise
|
|
|
|
// operations and don't perform any exchanges.
|
|
|
|
// A masked load, whether through VPT or tail predication, will write zeros
|
|
|
|
// to any of the falsely predicated bytes. So, from the loads, we know that
|
|
|
|
// the false lanes are zeroed and here we're trying to track that those false
|
|
|
|
// lanes remain zero, or where they change, the differences are masked away
|
|
|
|
// by their user(s).
|
2020-08-19 00:15:45 +08:00
|
|
|
// All MVE stores have to be predicated, so we know that any predicate load
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
// operands, or stored results are equivalent already. Other explicitly
|
|
|
|
// predicated instructions will perform the same operation in the original
|
|
|
|
// loop and the tail-predicated form too. Because of this, we can insert
|
2020-03-24 16:41:48 +08:00
|
|
|
// loads, stores and other predicated instructions into our Predicated
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
// set and build from there.
|
2020-03-11 19:39:14 +08:00
|
|
|
const TargetRegisterClass *QPRs = TRI.getRegClass(ARM::MQPRRegClassID);
|
2020-03-30 16:54:25 +08:00
|
|
|
SetVector<MachineInstr *> FalseLanesUnknown;
|
|
|
|
SmallPtrSet<MachineInstr *, 4> FalseLanesZero;
|
2020-03-24 16:41:48 +08:00
|
|
|
SmallPtrSet<MachineInstr *, 4> Predicated;
|
2020-07-01 15:27:12 +08:00
|
|
|
MachineBasicBlock *Header = ML.getHeader();
|
2020-03-24 16:41:48 +08:00
|
|
|
|
2021-08-11 17:35:53 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Validating Live outs\n");
|
|
|
|
|
2020-07-01 15:27:12 +08:00
|
|
|
for (auto &MI : *Header) {
|
2020-09-21 18:34:06 +08:00
|
|
|
if (!shouldInspect(MI))
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
continue;
|
|
|
|
|
2020-04-08 21:31:21 +08:00
|
|
|
if (isVCTP(&MI) || isVPTOpcode(MI.getOpcode()))
|
2020-03-30 16:54:25 +08:00
|
|
|
continue;
|
|
|
|
|
2020-08-28 20:56:16 +08:00
|
|
|
bool isPredicated = isVectorPredicated(&MI);
|
|
|
|
bool retainsOrReduces =
|
|
|
|
retainsPreviousHalfElement(MI) || isHorizontalReduction(MI);
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
|
2020-08-28 20:56:16 +08:00
|
|
|
if (isPredicated)
|
|
|
|
Predicated.insert(&MI);
|
|
|
|
if (producesFalseLanesZero(MI, QPRs, RDA, FalseLanesZero))
|
|
|
|
FalseLanesZero.insert(&MI);
|
|
|
|
else if (MI.getNumDefs() == 0)
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
continue;
|
2021-08-11 17:35:53 +08:00
|
|
|
else if (!isPredicated && retainsOrReduces) {
|
|
|
|
LLVM_DEBUG(dbgs() << " Unpredicated instruction that retainsOrReduces: " << MI);
|
2020-08-28 20:56:16 +08:00
|
|
|
return false;
|
2021-08-11 17:35:53 +08:00
|
|
|
}
|
2020-09-09 21:01:02 +08:00
|
|
|
else if (!isPredicated)
|
2020-03-30 16:54:25 +08:00
|
|
|
FalseLanesUnknown.insert(&MI);
|
2020-02-18 22:05:39 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:35:53 +08:00
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << " Predicated:\n";
|
|
|
|
for (auto *I : Predicated)
|
|
|
|
dbgs() << " " << *I;
|
|
|
|
dbgs() << " FalseLanesZero:\n";
|
|
|
|
for (auto *I : FalseLanesZero)
|
|
|
|
dbgs() << " " << *I;
|
|
|
|
dbgs() << " FalseLanesUnknown:\n";
|
|
|
|
for (auto *I : FalseLanesUnknown)
|
|
|
|
dbgs() << " " << *I;
|
|
|
|
});
|
|
|
|
|
2020-03-24 16:41:48 +08:00
|
|
|
auto HasPredicatedUsers = [this](MachineInstr *MI, const MachineOperand &MO,
|
|
|
|
SmallPtrSetImpl<MachineInstr *> &Predicated) {
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
SmallPtrSet<MachineInstr *, 2> Uses;
|
2020-10-22 04:59:45 +08:00
|
|
|
RDA.getGlobalUses(MI, MO.getReg().asMCReg(), Uses);
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
for (auto *Use : Uses) {
|
2020-03-24 16:41:48 +08:00
|
|
|
if (Use != MI && !Predicated.count(Use))
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-03-24 16:41:48 +08:00
|
|
|
// Visit the unknowns in reverse so that we can start at the values being
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
// stored and then we can work towards the leaves, hopefully adding more
|
2020-03-30 16:54:25 +08:00
|
|
|
// instructions to Predicated. Successfully terminating the loop means that
|
|
|
|
// all the unknown values have to found to be masked by predicated user(s).
|
2020-07-01 15:27:12 +08:00
|
|
|
// For any unpredicated values, we store them in NonPredicated so that we
|
|
|
|
// can later check whether these form a reduction.
|
|
|
|
SmallPtrSet<MachineInstr*, 2> NonPredicated;
|
2020-03-30 16:54:25 +08:00
|
|
|
for (auto *MI : reverse(FalseLanesUnknown)) {
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
for (auto &MO : MI->operands()) {
|
|
|
|
if (!isRegInClass(MO, QPRs) || !MO.isDef())
|
|
|
|
continue;
|
2020-03-24 16:41:48 +08:00
|
|
|
if (!HasPredicatedUsers(MI, MO, Predicated)) {
|
2021-08-11 17:35:53 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Found an unknown def of : "
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
<< TRI.getRegAsmName(MO.getReg()) << " at " << *MI);
|
2020-07-01 15:27:12 +08:00
|
|
|
NonPredicated.insert(MI);
|
2020-08-28 20:56:16 +08:00
|
|
|
break;
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Any unknown false lanes have been masked away by the user(s).
|
2020-08-28 20:56:16 +08:00
|
|
|
if (!NonPredicated.contains(MI))
|
|
|
|
Predicated.insert(MI);
|
[ARM][MVE] Validate tail predication values
Iterate through the loop and check that the observable values
produced are the same whether tail predication happens or not.
We want to find out if the tail-predicated version of this loop will
produce the same values as the loop in its original form. For this to
be true, the newly inserted implicit predication must not change the
the (observable) results.
We're doing this because many instructions in the loop will not be
predicated and so the conversion from VPT predication to tail
predication can result in different values being produced, because of
falsely predicated lanes not being updated in the converted form.
A masked load, whether through VPT or tail predication, will write
zeros to any of the falsely predicated bytes. So, from the loads, we
know that the false lanes are zeroed and here we're trying to track
that those false lanes remain zero, or where they change, the
differences are masked away by their user(s).
All MVE loads and stores have to be predicated, so we know that any
load operands, or stored results are equivalent already. Other
explicitly predicated instructions will perform the same operation in
the original loop and the tail-predicated form too. Because of this,
we can insert loads, stores and other predicated instructions into
our KnownFalseZeros set and build from there.
Differential Revision: https://reviews.llvm.org/D75452
2020-03-10 17:58:29 +08:00
|
|
|
}
|
2020-03-11 19:39:14 +08:00
|
|
|
|
2020-07-01 15:27:12 +08:00
|
|
|
SmallPtrSet<MachineInstr *, 2> LiveOutMIs;
|
2020-03-11 19:39:14 +08:00
|
|
|
SmallVector<MachineBasicBlock *, 2> ExitBlocks;
|
|
|
|
ML.getExitBlocks(ExitBlocks);
|
|
|
|
assert(ML.getNumBlocks() == 1 && "Expected single block loop!");
|
2020-07-01 15:27:12 +08:00
|
|
|
assert(ExitBlocks.size() == 1 && "Expected a single exit block");
|
|
|
|
MachineBasicBlock *ExitBB = ExitBlocks.front();
|
|
|
|
for (const MachineBasicBlock::RegisterMaskPair &RegMask : ExitBB->liveins()) {
|
2020-08-28 20:56:16 +08:00
|
|
|
// TODO: Instead of blocking predication, we could move the vctp to the exit
|
|
|
|
// block and calculate it's operand there in or the preheader.
|
2021-08-11 17:35:53 +08:00
|
|
|
if (RegMask.PhysReg == ARM::VPR) {
|
|
|
|
LLVM_DEBUG(dbgs() << " VPR is live in to the exit block.");
|
2020-08-28 20:56:16 +08:00
|
|
|
return false;
|
2021-08-11 17:35:53 +08:00
|
|
|
}
|
2020-07-01 15:27:12 +08:00
|
|
|
// Check Q-regs that are live in the exit blocks. We don't collect scalars
|
|
|
|
// because they won't be affected by lane predication.
|
2020-08-28 20:56:16 +08:00
|
|
|
if (QPRs->contains(RegMask.PhysReg))
|
2020-07-01 15:27:12 +08:00
|
|
|
if (auto *MI = RDA.getLocalLiveOutMIDef(Header, RegMask.PhysReg))
|
|
|
|
LiveOutMIs.insert(MI);
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:39:14 +08:00
|
|
|
// We've already validated that any VPT predication within the loop will be
|
|
|
|
// equivalent when we perform the predication transformation; so we know that
|
|
|
|
// any VPT predicated instruction is predicated upon VCTP. Any live-out
|
2020-07-01 15:27:12 +08:00
|
|
|
// instruction needs to be predicated, so check this here. The instructions
|
|
|
|
// in NonPredicated have been found to be a reduction that we can ensure its
|
|
|
|
// legality.
|
2020-08-28 20:56:16 +08:00
|
|
|
for (auto *MI : LiveOutMIs) {
|
|
|
|
if (NonPredicated.count(MI) && FalseLanesUnknown.contains(MI)) {
|
2021-08-11 17:35:53 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Unable to handle live out: " << *MI);
|
2020-03-11 19:39:14 +08:00
|
|
|
return false;
|
2020-08-28 20:56:16 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-11 19:39:14 +08:00
|
|
|
|
2020-02-18 22:05:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:36:40 +08:00
|
|
|
void LowOverheadLoop::Validate(ARMBasicBlockUtils *BBUtils) {
|
2020-01-03 16:48:33 +08:00
|
|
|
if (Revert)
|
|
|
|
return;
|
|
|
|
|
2020-09-29 15:41:53 +08:00
|
|
|
// Check branch target ranges: WLS[TP] can only branch forwards and LE[TP]
|
|
|
|
// can only jump back.
|
|
|
|
auto ValidateRanges = [](MachineInstr *Start, MachineInstr *End,
|
|
|
|
ARMBasicBlockUtils *BBUtils, MachineLoop &ML) {
|
2020-12-10 20:14:23 +08:00
|
|
|
MachineBasicBlock *TgtBB = End->getOpcode() == ARM::t2LoopEnd
|
|
|
|
? End->getOperand(1).getMBB()
|
|
|
|
: End->getOperand(2).getMBB();
|
2020-09-25 16:36:40 +08:00
|
|
|
// TODO Maybe there's cases where the target doesn't have to be the header,
|
|
|
|
// but for now be safe and revert.
|
2020-12-10 20:14:23 +08:00
|
|
|
if (TgtBB != ML.getHeader()) {
|
2020-09-29 15:41:53 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: LoopEnd is not targeting header.\n");
|
2020-09-25 16:36:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-03 16:48:33 +08:00
|
|
|
|
2020-09-25 16:36:40 +08:00
|
|
|
// The WLS and LE instructions have 12-bits for the label offset. WLS
|
|
|
|
// requires a positive offset, while LE uses negative.
|
|
|
|
if (BBUtils->getOffsetOf(End) < BBUtils->getOffsetOf(ML.getHeader()) ||
|
|
|
|
!BBUtils->isBBInRange(End, ML.getHeader(), 4094)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: LE offset is out-of-range\n");
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-03 16:48:33 +08:00
|
|
|
|
2021-06-13 20:55:34 +08:00
|
|
|
if (isWhileLoopStart(*Start)) {
|
|
|
|
MachineBasicBlock *TargetBB = getWhileLoopStartTargetBB(*Start);
|
|
|
|
if (BBUtils->getOffsetOf(Start) > BBUtils->getOffsetOf(TargetBB) ||
|
|
|
|
!BBUtils->isBBInRange(Start, TargetBB, 4094)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: WLS offset is out-of-range!\n");
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-25 16:36:40 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2020-01-03 16:48:33 +08:00
|
|
|
|
[ARM] Improve WLS lowering
Recently we improved the lowering of low overhead loops and tail
predicated loops, but concentrated first on the DLS do style loops. This
extends those improvements over to the WLS while loops, improving the
chance of lowering them successfully. To do this the lowering has to
change a little as the instructions are terminators that produce a value
- something that needs to be treated carefully.
Lowering starts at the Hardware Loop pass, inserting a new
llvm.test.start.loop.iterations that produces both an i1 to control the
loop entry and an i32 similar to the llvm.start.loop.iterations
intrinsic added for do loops. This feeds into the loop phi, properly
gluing the values together:
%wls = call { i32, i1 } @llvm.test.start.loop.iterations.i32(i32 %div)
%wls0 = extractvalue { i32, i1 } %wls, 0
%wls1 = extractvalue { i32, i1 } %wls, 1
br i1 %wls1, label %loop.ph, label %loop.exit
...
loop:
%lsr.iv = phi i32 [ %wls0, %loop.ph ], [ %iv.next, %loop ]
..
%iv.next = call i32 @llvm.loop.decrement.reg.i32(i32 %lsr.iv, i32 1)
%cmp = icmp ne i32 %iv.next, 0
br i1 %cmp, label %loop, label %loop.exit
The llvm.test.start.loop.iterations need to be lowered through ISel
lowering as a pair of WLS and WLSSETUP nodes, which each get converted
to t2WhileLoopSetup and t2WhileLoopStart Pseudos. This helps prevent
t2WhileLoopStart from being a terminator that produces a value,
something difficult to control at that stage in the pipeline. Instead
the t2WhileLoopSetup produces the value of LR (essentially acting as a
lr = subs rn, 0), t2WhileLoopStart consumes that lr value (the Bcc).
These are then converted into a single t2WhileLoopStartLR at the same
point as t2DoLoopStartTP and t2LoopEndDec. Otherwise we revert the loop
to prevent them from progressing further in the pipeline. The
t2WhileLoopStartLR is a single instruction that takes a GPR and produces
LR, similar to the WLS instruction.
%1:gprlr = t2WhileLoopStartLR %0:rgpr, %bb.3
t2B %bb.1
...
bb.2.loop:
%2:gprlr = PHI %1:gprlr, %bb.1, %3:gprlr, %bb.2
...
%3:gprlr = t2LoopEndDec %2:gprlr, %bb.2
t2B %bb.3
The t2WhileLoopStartLR can then be treated similar to the other low
overhead loop pseudos, eventually being lowered to a WLS providing the
branches are within range.
Differential Revision: https://reviews.llvm.org/D97729
2021-03-11 22:06:04 +08:00
|
|
|
StartInsertPt = MachineBasicBlock::iterator(Start);
|
|
|
|
StartInsertBB = Start->getParent();
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Will insert LoopStart at "
|
|
|
|
<< *StartInsertPt);
|
2020-10-01 20:37:47 +08:00
|
|
|
|
2020-09-30 22:15:42 +08:00
|
|
|
Revert = !ValidateRanges(Start, End, BBUtils, ML);
|
|
|
|
CannotTailPredicate = !ValidateTailPredicate();
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
bool LowOverheadLoop::AddVCTP(MachineInstr *MI) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Adding VCTP: " << *MI);
|
|
|
|
if (VCTPs.empty()) {
|
|
|
|
VCTPs.push_back(MI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we find another VCTP, check whether it uses the same value as the main VCTP.
|
|
|
|
// If it does, store it in the VCTPs set, else refuse it.
|
|
|
|
MachineInstr *Prev = VCTPs.back();
|
|
|
|
if (!Prev->getOperand(1).isIdenticalTo(MI->getOperand(1)) ||
|
2020-10-22 04:59:45 +08:00
|
|
|
!RDA.hasSameReachingDef(Prev, MI, MI->getOperand(1).getReg().asMCReg())) {
|
2020-09-22 16:22:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Found VCTP with a different reaching "
|
|
|
|
"definition from the main VCTP");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
VCTPs.push_back(MI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-05 23:08:58 +08:00
|
|
|
static bool ValidateMVEStore(MachineInstr *MI, MachineLoop *ML) {
|
|
|
|
|
|
|
|
auto GetFrameIndex = [](MachineMemOperand *Operand) {
|
|
|
|
const PseudoSourceValue *PseudoValue = Operand->getPseudoValue();
|
|
|
|
if (PseudoValue && PseudoValue->kind() == PseudoSourceValue::FixedStack) {
|
|
|
|
if (const auto *FS = dyn_cast<FixedStackPseudoSourceValue>(PseudoValue)) {
|
|
|
|
return FS->getFrameIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto IsStackOp = [GetFrameIndex](MachineInstr *I) {
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case ARM::MVE_VSTRWU32:
|
|
|
|
case ARM::MVE_VLDRWU32: {
|
|
|
|
return I->getOperand(1).getReg() == ARM::SP &&
|
|
|
|
I->memoperands().size() == 1 &&
|
|
|
|
GetFrameIndex(I->memoperands().front()) >= 0;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// An unpredicated vector register spill is allowed if all of the uses of the
|
|
|
|
// stack slot are within the loop
|
|
|
|
if (MI->getOpcode() != ARM::MVE_VSTRWU32 || !IsStackOp(MI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Search all blocks after the loop for accesses to the same stack slot.
|
|
|
|
// ReachingDefAnalysis doesn't work for sp as it relies on registers being
|
|
|
|
// live-out (which sp never is) to know what blocks to look in
|
|
|
|
if (MI->memoperands().size() == 0)
|
|
|
|
return false;
|
|
|
|
int FI = GetFrameIndex(MI->memoperands().front());
|
|
|
|
|
2021-08-06 14:21:08 +08:00
|
|
|
auto &FrameInfo = MI->getParent()->getParent()->getFrameInfo();
|
2021-07-05 23:08:58 +08:00
|
|
|
if (FI == -1 || !FrameInfo.isSpillSlotObjectIndex(FI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<MachineBasicBlock *> Frontier;
|
|
|
|
ML->getExitBlocks(Frontier);
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 4> Visited{MI->getParent()};
|
|
|
|
unsigned Idx = 0;
|
|
|
|
while (Idx < Frontier.size()) {
|
|
|
|
MachineBasicBlock *BB = Frontier[Idx];
|
|
|
|
bool LookAtSuccessors = true;
|
|
|
|
for (auto &I : *BB) {
|
|
|
|
if (!IsStackOp(&I) || I.memoperands().size() == 0)
|
|
|
|
continue;
|
|
|
|
if (GetFrameIndex(I.memoperands().front()) != FI)
|
|
|
|
continue;
|
|
|
|
// If this block has a store to the stack slot before any loads then we
|
|
|
|
// can ignore the block
|
|
|
|
if (I.getOpcode() == ARM::MVE_VSTRWU32) {
|
|
|
|
LookAtSuccessors = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If the store and the load are using the same stack slot then the
|
|
|
|
// store isn't valid for tail predication
|
|
|
|
if (I.getOpcode() == ARM::MVE_VLDRWU32)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LookAtSuccessors) {
|
|
|
|
for (auto Succ : BB->successors()) {
|
|
|
|
if (!Visited.contains(Succ) && !is_contained(Frontier, Succ))
|
|
|
|
Frontier.push_back(Succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Visited.insert(BB);
|
|
|
|
Idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowOverheadLoop::ValidateMVEInst(MachineInstr *MI) {
|
2020-01-14 19:02:32 +08:00
|
|
|
if (CannotTailPredicate)
|
|
|
|
return false;
|
|
|
|
|
2020-09-21 18:34:06 +08:00
|
|
|
if (!shouldInspect(*MI))
|
2020-09-16 20:38:36 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (MI->getOpcode() == ARM::MVE_VPSEL ||
|
|
|
|
MI->getOpcode() == ARM::MVE_VPNOT) {
|
|
|
|
// TODO: Allow VPSEL and VPNOT, we currently cannot because:
|
|
|
|
// 1) It will use the VPR as a predicate operand, but doesn't have to be
|
|
|
|
// instead a VPT block, which means we can assert while building up
|
|
|
|
// the VPT block because we don't find another VPT or VPST to being a new
|
|
|
|
// one.
|
|
|
|
// 2) VPSEL still requires a VPR operand even after tail predicating,
|
|
|
|
// which means we can't remove it unless there is another
|
|
|
|
// instruction, such as vcmp, that can provide the VPR def.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-22 16:22:11 +08:00
|
|
|
// Record all VCTPs and check that they're equivalent to one another.
|
|
|
|
if (isVCTP(MI) && !AddVCTP(MI))
|
|
|
|
return false;
|
2019-12-20 16:42:11 +08:00
|
|
|
|
2020-09-21 18:34:06 +08:00
|
|
|
// Inspect uses first so that any instructions that alter the VPR don't
|
|
|
|
// alter the predicate upon themselves.
|
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
2019-12-20 16:42:11 +08:00
|
|
|
bool IsUse = false;
|
2020-09-22 20:33:09 +08:00
|
|
|
unsigned LastOpIdx = MI->getNumOperands() - 1;
|
|
|
|
for (auto &Op : enumerate(reverse(MCID.operands()))) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(LastOpIdx - Op.index());
|
2020-09-22 16:22:11 +08:00
|
|
|
if (!MO.isReg() || !MO.isUse() || MO.getReg() != ARM::VPR)
|
2019-12-20 16:42:11 +08:00
|
|
|
continue;
|
|
|
|
|
2020-09-22 20:33:09 +08:00
|
|
|
if (ARM::isVpred(Op.value().OperandType)) {
|
2020-09-22 16:22:11 +08:00
|
|
|
VPTState::addInst(MI);
|
2020-01-10 22:47:29 +08:00
|
|
|
IsUse = true;
|
2020-09-22 16:22:11 +08:00
|
|
|
} else if (MI->getOpcode() != ARM::MVE_VPST) {
|
2019-12-20 16:42:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Found instruction using vpr: " << *MI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:02:32 +08:00
|
|
|
// If we find an instruction that has been marked as not valid for tail
|
|
|
|
// predication, only allow the instruction if it's contained within a valid
|
|
|
|
// VPT block.
|
2020-09-21 18:34:06 +08:00
|
|
|
bool RequiresExplicitPredication =
|
|
|
|
(MCID.TSFlags & ARMII::ValidForTailPredication) == 0;
|
|
|
|
if (isDomainMVE(MI) && RequiresExplicitPredication) {
|
2021-09-22 19:07:52 +08:00
|
|
|
if (!IsUse && producesDoubleWidthResult(*MI)) {
|
|
|
|
DoubleWidthResultInstrs.insert(MI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(if (!IsUse) dbgs()
|
|
|
|
<< "ARM Loops: Can't tail predicate: " << *MI);
|
2020-09-16 18:47:26 +08:00
|
|
|
return IsUse;
|
2020-01-14 20:02:32 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 22:05:39 +08:00
|
|
|
// If the instruction is already explicitly predicated, then the conversion
|
2020-08-19 00:15:45 +08:00
|
|
|
// will be fine, but ensure that all store operations are predicated.
|
2021-07-05 23:08:58 +08:00
|
|
|
if (MI->mayStore() && !ValidateMVEStore(MI, &ML))
|
2020-09-22 16:22:11 +08:00
|
|
|
return IsUse;
|
|
|
|
|
|
|
|
// If this instruction defines the VPR, update the predicate for the
|
|
|
|
// proceeding instructions.
|
|
|
|
if (isVectorPredicate(MI)) {
|
|
|
|
// Clear the existing predicate when we're not in VPT Active state,
|
|
|
|
// otherwise we add to it.
|
|
|
|
if (!isVectorPredicated(MI))
|
|
|
|
VPTState::resetPredicate(MI);
|
|
|
|
else
|
|
|
|
VPTState::addPredicate(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally once the predicate has been modified, we can start a new VPT
|
|
|
|
// block if necessary.
|
|
|
|
if (isVPTOpcode(MI->getOpcode()))
|
|
|
|
VPTState::CreateVPTBlock(MI);
|
|
|
|
|
|
|
|
return true;
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMLowOverheadLoops::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
const ARMSubtarget &ST = static_cast<const ARMSubtarget&>(mf.getSubtarget());
|
|
|
|
if (!ST.hasLOB())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MF = &mf;
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops on " << MF->getName() << " ------------- \n");
|
|
|
|
|
2019-11-26 18:25:04 +08:00
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
2019-11-26 18:03:25 +08:00
|
|
|
RDA = &getAnalysis<ReachingDefAnalysis>();
|
2019-11-19 01:07:56 +08:00
|
|
|
MF->getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
|
|
|
|
MRI = &MF->getRegInfo();
|
|
|
|
TII = static_cast<const ARMBaseInstrInfo*>(ST.getInstrInfo());
|
[ARM][LowOverheadLoops] Remove dead loop update instructions.
After creating a low-overhead loop, the loop update instruction was still
lingering around hurting performance. This removes dead loop update
instructions, which in our case are mostly SUBS instructions.
To support this, some helper functions were added to MachineLoopUtils and
ReachingDefAnalysis to analyse live-ins of loop exit blocks and find uses
before a particular loop instruction, respectively.
This is a first version that removes a SUBS instruction when there are no other
uses inside and outside the loop block, but there are some more interesting
cases in test/CodeGen/Thumb2/LowOverheadLoops/mve-tail-data-types.ll which
shows that there is room for improvement. For example, we can't handle this
case yet:
..
dlstp.32 lr, r2
.LBB0_1:
mov r3, r2
subs r2, #4
vldrh.u32 q2, [r1], #8
vmov q1, q0
vmla.u32 q0, q2, r0
letp lr, .LBB0_1
@ %bb.2:
vctp.32 r3
..
which is a lot more tricky because r2 is not only used by the subs, but also by
the mov to r3, which is used outside the low-overhead loop by the vctp
instruction, and that requires a bit of a different approach, and I will follow
up on this.
Differential Revision: https://reviews.llvm.org/D71007
2019-12-11 18:11:48 +08:00
|
|
|
TRI = ST.getRegisterInfo();
|
2019-11-19 01:07:56 +08:00
|
|
|
BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(*MF));
|
|
|
|
BBUtils->computeAllBlockSizes();
|
|
|
|
BBUtils->adjustBBOffsetsAfter(&MF->front());
|
|
|
|
|
|
|
|
bool Changed = false;
|
2019-11-26 18:25:04 +08:00
|
|
|
for (auto ML : *MLI) {
|
2020-09-23 04:28:00 +08:00
|
|
|
if (ML->isOutermost())
|
2019-11-19 01:07:56 +08:00
|
|
|
Changed |= ProcessLoop(ML);
|
|
|
|
}
|
|
|
|
Changed |= RevertNonLoops();
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
bool ARMLowOverheadLoops::ProcessLoop(MachineLoop *ML) {
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Process inner loops first.
|
|
|
|
for (auto I = ML->begin(), E = ML->end(); I != E; ++I)
|
|
|
|
Changed |= ProcessLoop(*I);
|
|
|
|
|
2021-05-24 19:22:15 +08:00
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << "ARM Loops: Processing loop containing:\n";
|
|
|
|
if (auto *Preheader = ML->getLoopPreheader())
|
|
|
|
dbgs() << " - Preheader: " << printMBBReference(*Preheader) << "\n";
|
|
|
|
else if (auto *Preheader = MLI->findLoopPreheader(ML, true, true))
|
|
|
|
dbgs() << " - Preheader: " << printMBBReference(*Preheader) << "\n";
|
|
|
|
for (auto *MBB : ML->getBlocks())
|
|
|
|
dbgs() << " - Block: " << printMBBReference(*MBB) << "\n";
|
|
|
|
});
|
2019-06-25 18:45:51 +08:00
|
|
|
|
2019-07-01 16:21:28 +08:00
|
|
|
// Search the given block for a loop start instruction. If one isn't found,
|
|
|
|
// and there's only one predecessor block, search that one too.
|
|
|
|
std::function<MachineInstr*(MachineBasicBlock*)> SearchForStart =
|
2019-07-22 22:16:40 +08:00
|
|
|
[&SearchForStart](MachineBasicBlock *MBB) -> MachineInstr* {
|
2019-06-25 18:45:51 +08:00
|
|
|
for (auto &MI : *MBB) {
|
2019-12-16 17:11:47 +08:00
|
|
|
if (isLoopStart(MI))
|
2019-06-25 18:45:51 +08:00
|
|
|
return &MI;
|
|
|
|
}
|
2019-07-01 16:21:28 +08:00
|
|
|
if (MBB->pred_size() == 1)
|
|
|
|
return SearchForStart(*MBB->pred_begin());
|
2019-06-25 18:45:51 +08:00
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2020-07-01 15:27:12 +08:00
|
|
|
LowOverheadLoop LoLoop(*ML, *MLI, *RDA, *TRI, *TII);
|
2019-11-26 18:25:04 +08:00
|
|
|
// Search the preheader for the start intrinsic.
|
2019-07-01 16:21:28 +08:00
|
|
|
// FIXME: I don't see why we shouldn't be supporting multiple predecessors
|
|
|
|
// with potentially multiple set.loop.iterations, so we need to enable this.
|
2020-07-01 15:27:12 +08:00
|
|
|
if (LoLoop.Preheader)
|
|
|
|
LoLoop.Start = SearchForStart(LoLoop.Preheader);
|
2019-11-26 18:25:04 +08:00
|
|
|
else
|
2021-06-02 05:48:42 +08:00
|
|
|
return Changed;
|
2019-06-25 18:45:51 +08:00
|
|
|
|
|
|
|
// Find the low-overhead loop components and decide whether or not to fall
|
2019-11-19 01:07:56 +08:00
|
|
|
// back to a normal loop. Also look for a vctp instructions and decide
|
|
|
|
// whether we can convert that predicate using tail predication.
|
2019-06-25 18:45:51 +08:00
|
|
|
for (auto *MBB : reverse(ML->getBlocks())) {
|
|
|
|
for (auto &MI : *MBB) {
|
2020-01-30 18:47:55 +08:00
|
|
|
if (MI.isDebugValue())
|
|
|
|
continue;
|
|
|
|
else if (MI.getOpcode() == ARM::t2LoopDec)
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.Dec = &MI;
|
2019-06-25 18:45:51 +08:00
|
|
|
else if (MI.getOpcode() == ARM::t2LoopEnd)
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.End = &MI;
|
2020-12-10 20:14:23 +08:00
|
|
|
else if (MI.getOpcode() == ARM::t2LoopEndDec)
|
|
|
|
LoLoop.End = LoLoop.Dec = &MI;
|
2019-12-16 17:11:47 +08:00
|
|
|
else if (isLoopStart(MI))
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.Start = &MI;
|
2019-09-17 20:19:32 +08:00
|
|
|
else if (MI.getDesc().isCall()) {
|
2019-06-25 23:11:17 +08:00
|
|
|
// TODO: Though the call will require LE to execute again, does this
|
|
|
|
// mean we should revert? Always executing LE hopefully should be
|
|
|
|
// faster than performing a sub,cmp,br or even subs,br.
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.Revert = true;
|
2019-09-17 20:19:32 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Found call.\n");
|
2019-11-19 01:07:56 +08:00
|
|
|
} else {
|
2019-12-20 16:42:11 +08:00
|
|
|
// Record VPR defs and build up their corresponding vpt blocks.
|
2019-11-19 01:07:56 +08:00
|
|
|
// Check we know how to tail predicate any mve instructions.
|
2020-01-03 16:48:33 +08:00
|
|
|
LoLoop.AnalyseMVEInst(&MI);
|
2019-09-17 20:19:32 +08:00
|
|
|
}
|
2019-06-25 18:45:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:07:56 +08:00
|
|
|
LLVM_DEBUG(LoLoop.dump());
|
2020-01-10 22:11:52 +08:00
|
|
|
if (!LoLoop.FoundAllComponents()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Didn't find loop start, update, end\n");
|
2021-06-02 05:48:42 +08:00
|
|
|
return Changed;
|
2020-01-10 22:11:52 +08:00
|
|
|
}
|
2019-09-17 20:19:32 +08:00
|
|
|
|
[ARM] Improve WLS lowering
Recently we improved the lowering of low overhead loops and tail
predicated loops, but concentrated first on the DLS do style loops. This
extends those improvements over to the WLS while loops, improving the
chance of lowering them successfully. To do this the lowering has to
change a little as the instructions are terminators that produce a value
- something that needs to be treated carefully.
Lowering starts at the Hardware Loop pass, inserting a new
llvm.test.start.loop.iterations that produces both an i1 to control the
loop entry and an i32 similar to the llvm.start.loop.iterations
intrinsic added for do loops. This feeds into the loop phi, properly
gluing the values together:
%wls = call { i32, i1 } @llvm.test.start.loop.iterations.i32(i32 %div)
%wls0 = extractvalue { i32, i1 } %wls, 0
%wls1 = extractvalue { i32, i1 } %wls, 1
br i1 %wls1, label %loop.ph, label %loop.exit
...
loop:
%lsr.iv = phi i32 [ %wls0, %loop.ph ], [ %iv.next, %loop ]
..
%iv.next = call i32 @llvm.loop.decrement.reg.i32(i32 %lsr.iv, i32 1)
%cmp = icmp ne i32 %iv.next, 0
br i1 %cmp, label %loop, label %loop.exit
The llvm.test.start.loop.iterations need to be lowered through ISel
lowering as a pair of WLS and WLSSETUP nodes, which each get converted
to t2WhileLoopSetup and t2WhileLoopStart Pseudos. This helps prevent
t2WhileLoopStart from being a terminator that produces a value,
something difficult to control at that stage in the pipeline. Instead
the t2WhileLoopSetup produces the value of LR (essentially acting as a
lr = subs rn, 0), t2WhileLoopStart consumes that lr value (the Bcc).
These are then converted into a single t2WhileLoopStartLR at the same
point as t2DoLoopStartTP and t2LoopEndDec. Otherwise we revert the loop
to prevent them from progressing further in the pipeline. The
t2WhileLoopStartLR is a single instruction that takes a GPR and produces
LR, similar to the WLS instruction.
%1:gprlr = t2WhileLoopStartLR %0:rgpr, %bb.3
t2B %bb.1
...
bb.2.loop:
%2:gprlr = PHI %1:gprlr, %bb.1, %3:gprlr, %bb.2
...
%3:gprlr = t2LoopEndDec %2:gprlr, %bb.2
t2B %bb.3
The t2WhileLoopStartLR can then be treated similar to the other low
overhead loop pseudos, eventually being lowered to a WLS providing the
branches are within range.
Differential Revision: https://reviews.llvm.org/D97729
2021-03-11 22:06:04 +08:00
|
|
|
assert(LoLoop.Start->getOpcode() != ARM::t2WhileLoopStart &&
|
|
|
|
"Expected t2WhileLoopStart to be removed before regalloc!");
|
|
|
|
|
2020-12-10 20:14:23 +08:00
|
|
|
// Check that the only instruction using LoopDec is LoopEnd. This can only
|
|
|
|
// happen when the Dec and End are separate, not a single t2LoopEndDec.
|
2020-02-05 21:20:50 +08:00
|
|
|
// TODO: Check for copy chains that really have no effect.
|
2020-12-10 20:14:23 +08:00
|
|
|
if (LoLoop.Dec != LoLoop.End) {
|
|
|
|
SmallPtrSet<MachineInstr *, 2> Uses;
|
|
|
|
RDA->getReachingLocalUses(LoLoop.Dec, MCRegister::from(ARM::LR), Uses);
|
|
|
|
if (Uses.size() > 1 || !Uses.count(LoLoop.End)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Unable to remove LoopDec.\n");
|
|
|
|
LoLoop.Revert = true;
|
|
|
|
}
|
2020-01-17 21:08:24 +08:00
|
|
|
}
|
2020-09-25 16:36:40 +08:00
|
|
|
LoLoop.Validate(BBUtils.get());
|
2019-11-19 01:07:56 +08:00
|
|
|
Expand(LoLoop);
|
2019-06-25 18:45:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:29:43 +08:00
|
|
|
// WhileLoopStart holds the exit block, so produce a cmp lr, 0 and then a
|
|
|
|
// beq that branches to the exit branch.
|
2019-09-23 16:35:31 +08:00
|
|
|
// TODO: We could also try to generate a cbz if the value in LR is also in
|
2019-07-10 20:29:43 +08:00
|
|
|
// another low register.
|
|
|
|
void ARMLowOverheadLoops::RevertWhile(MachineInstr *MI) const {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp: " << *MI);
|
2021-06-13 20:55:34 +08:00
|
|
|
MachineBasicBlock *DestBB = getWhileLoopStartTargetBB(*MI);
|
2019-09-23 16:35:31 +08:00
|
|
|
unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
|
|
|
|
ARM::tBcc : ARM::t2Bcc;
|
2019-07-10 20:29:43 +08:00
|
|
|
|
[ARM] Improve WLS lowering
Recently we improved the lowering of low overhead loops and tail
predicated loops, but concentrated first on the DLS do style loops. This
extends those improvements over to the WLS while loops, improving the
chance of lowering them successfully. To do this the lowering has to
change a little as the instructions are terminators that produce a value
- something that needs to be treated carefully.
Lowering starts at the Hardware Loop pass, inserting a new
llvm.test.start.loop.iterations that produces both an i1 to control the
loop entry and an i32 similar to the llvm.start.loop.iterations
intrinsic added for do loops. This feeds into the loop phi, properly
gluing the values together:
%wls = call { i32, i1 } @llvm.test.start.loop.iterations.i32(i32 %div)
%wls0 = extractvalue { i32, i1 } %wls, 0
%wls1 = extractvalue { i32, i1 } %wls, 1
br i1 %wls1, label %loop.ph, label %loop.exit
...
loop:
%lsr.iv = phi i32 [ %wls0, %loop.ph ], [ %iv.next, %loop ]
..
%iv.next = call i32 @llvm.loop.decrement.reg.i32(i32 %lsr.iv, i32 1)
%cmp = icmp ne i32 %iv.next, 0
br i1 %cmp, label %loop, label %loop.exit
The llvm.test.start.loop.iterations need to be lowered through ISel
lowering as a pair of WLS and WLSSETUP nodes, which each get converted
to t2WhileLoopSetup and t2WhileLoopStart Pseudos. This helps prevent
t2WhileLoopStart from being a terminator that produces a value,
something difficult to control at that stage in the pipeline. Instead
the t2WhileLoopSetup produces the value of LR (essentially acting as a
lr = subs rn, 0), t2WhileLoopStart consumes that lr value (the Bcc).
These are then converted into a single t2WhileLoopStartLR at the same
point as t2DoLoopStartTP and t2LoopEndDec. Otherwise we revert the loop
to prevent them from progressing further in the pipeline. The
t2WhileLoopStartLR is a single instruction that takes a GPR and produces
LR, similar to the WLS instruction.
%1:gprlr = t2WhileLoopStartLR %0:rgpr, %bb.3
t2B %bb.1
...
bb.2.loop:
%2:gprlr = PHI %1:gprlr, %bb.1, %3:gprlr, %bb.2
...
%3:gprlr = t2LoopEndDec %2:gprlr, %bb.2
t2B %bb.3
The t2WhileLoopStartLR can then be treated similar to the other low
overhead loop pseudos, eventually being lowered to a WLS providing the
branches are within range.
Differential Revision: https://reviews.llvm.org/D97729
2021-03-11 22:06:04 +08:00
|
|
|
RevertWhileLoopStartLR(MI, TII, BrOpc);
|
2019-07-10 20:29:43 +08:00
|
|
|
}
|
|
|
|
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
void ARMLowOverheadLoops::RevertDo(MachineInstr *MI) const {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to mov: " << *MI);
|
2020-12-07 23:44:40 +08:00
|
|
|
RevertDoLoopStart(MI, TII);
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
}
|
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
bool ARMLowOverheadLoops::RevertLoopDec(MachineInstr *MI) const {
|
2019-07-10 20:29:43 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to sub: " << *MI);
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2020-01-29 16:26:11 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 1> Ignore;
|
2020-02-28 19:14:42 +08:00
|
|
|
for (auto I = MachineBasicBlock::iterator(MI), E = MBB->end(); I != E; ++I) {
|
|
|
|
if (I->getOpcode() == ARM::t2LoopEnd) {
|
|
|
|
Ignore.insert(&*I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 16:57:50 +08:00
|
|
|
|
2019-11-26 18:03:25 +08:00
|
|
|
// If nothing defines CPSR between LoopDec and LoopEnd, use a t2SUBS.
|
2020-10-22 04:59:45 +08:00
|
|
|
bool SetFlags =
|
|
|
|
RDA->isSafeToDefRegAt(MI, MCRegister::from(ARM::CPSR), Ignore);
|
2019-09-23 16:57:50 +08:00
|
|
|
|
2020-12-07 23:44:40 +08:00
|
|
|
llvm::RevertLoopDec(MI, TII, SetFlags);
|
2019-09-23 16:57:50 +08:00
|
|
|
return SetFlags;
|
2019-07-10 20:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a subs, or sub and cmp, and a branch instead of an LE.
|
2019-09-23 16:57:50 +08:00
|
|
|
void ARMLowOverheadLoops::RevertLoopEnd(MachineInstr *MI, bool SkipCmp) const {
|
2019-07-10 20:29:43 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to cmp, br: " << *MI);
|
|
|
|
|
2019-09-23 16:35:31 +08:00
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(1).getMBB();
|
|
|
|
unsigned BrOpc = BBUtils->isBBInRange(MI, DestBB, 254) ?
|
|
|
|
ARM::tBcc : ARM::t2Bcc;
|
|
|
|
|
2020-12-07 23:44:40 +08:00
|
|
|
llvm::RevertLoopEnd(MI, TII, BrOpc, SkipCmp);
|
2019-07-10 20:29:43 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 20:14:23 +08:00
|
|
|
// Generate a subs, or sub and cmp, and a branch instead of an LE.
|
|
|
|
void ARMLowOverheadLoops::RevertLoopEndDec(MachineInstr *MI) const {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting to subs, br: " << *MI);
|
|
|
|
assert(MI->getOpcode() == ARM::t2LoopEndDec && "Expected a t2LoopEndDec!");
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(ARM::t2SUBri));
|
|
|
|
MIB.addDef(ARM::LR);
|
|
|
|
MIB.add(MI->getOperand(1));
|
|
|
|
MIB.addImm(1);
|
|
|
|
MIB.addImm(ARMCC::AL);
|
|
|
|
MIB.addReg(ARM::NoRegister);
|
|
|
|
MIB.addReg(ARM::CPSR);
|
|
|
|
MIB->getOperand(5).setIsDef(true);
|
|
|
|
|
|
|
|
MachineBasicBlock *DestBB = MI->getOperand(2).getMBB();
|
|
|
|
unsigned BrOpc =
|
|
|
|
BBUtils->isBBInRange(MI, DestBB, 254) ? ARM::tBcc : ARM::t2Bcc;
|
|
|
|
|
|
|
|
// Create bne
|
|
|
|
MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(BrOpc));
|
|
|
|
MIB.add(MI->getOperand(2)); // branch target
|
|
|
|
MIB.addImm(ARMCC::NE); // condition code
|
|
|
|
MIB.addReg(ARM::CPSR);
|
|
|
|
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2020-02-05 23:15:46 +08:00
|
|
|
// Perform dead code elimation on the loop iteration count setup expression.
|
|
|
|
// If we are tail-predicating, the number of elements to be processed is the
|
|
|
|
// operand of the VCTP instruction in the vector body, see getCount(), which is
|
|
|
|
// register $r3 in this example:
|
|
|
|
//
|
|
|
|
// $lr = big-itercount-expression
|
|
|
|
// ..
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
// $lr = t2DoLoopStart renamable $lr
|
2020-02-05 23:15:46 +08:00
|
|
|
// vector.body:
|
|
|
|
// ..
|
|
|
|
// $vpr = MVE_VCTP32 renamable $r3
|
|
|
|
// renamable $lr = t2LoopDec killed renamable $lr, 1
|
|
|
|
// t2LoopEnd renamable $lr, %vector.body
|
|
|
|
// tB %end
|
|
|
|
//
|
|
|
|
// What we would like achieve here is to replace the do-loop start pseudo
|
|
|
|
// instruction t2DoLoopStart with:
|
|
|
|
//
|
|
|
|
// $lr = MVE_DLSTP_32 killed renamable $r3
|
|
|
|
//
|
|
|
|
// Thus, $r3 which defines the number of elements, is written to $lr,
|
|
|
|
// and then we want to delete the whole chain that used to define $lr,
|
|
|
|
// see the comment below how this chain could look like.
|
|
|
|
//
|
|
|
|
void ARMLowOverheadLoops::IterationCountDCE(LowOverheadLoop &LoLoop) {
|
|
|
|
if (!LoLoop.IsTailPredicationLegal())
|
|
|
|
return;
|
2020-01-30 16:26:28 +08:00
|
|
|
|
2020-03-03 23:19:57 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Trying DCE on loop iteration count.\n");
|
|
|
|
|
2021-05-21 16:29:30 +08:00
|
|
|
MachineInstr *Def = RDA->getMIOperand(LoLoop.Start, 1);
|
2020-03-03 23:19:57 +08:00
|
|
|
if (!Def) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Couldn't find iteration count.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect and remove the users of iteration count.
|
|
|
|
SmallPtrSet<MachineInstr*, 4> Killed = { LoLoop.Start, LoLoop.Dec,
|
2020-09-30 22:15:42 +08:00
|
|
|
LoLoop.End };
|
2020-09-30 16:36:57 +08:00
|
|
|
if (!TryRemove(Def, *RDA, LoLoop.ToRemove, Killed))
|
2020-09-28 21:44:51 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Unsafe to remove loop iteration count.\n");
|
2020-02-05 23:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr* ARMLowOverheadLoops::ExpandLoopStart(LowOverheadLoop &LoLoop) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Expanding LoopStart.\n");
|
|
|
|
// When using tail-predication, try to delete the dead code that was used to
|
|
|
|
// calculate the number of loop iterations.
|
|
|
|
IterationCountDCE(LoLoop);
|
2020-01-17 21:08:24 +08:00
|
|
|
|
2020-09-30 22:15:42 +08:00
|
|
|
MachineBasicBlock::iterator InsertPt = LoLoop.StartInsertPt;
|
2019-11-19 01:07:56 +08:00
|
|
|
MachineInstr *Start = LoLoop.Start;
|
2020-09-30 22:15:42 +08:00
|
|
|
MachineBasicBlock *MBB = LoLoop.StartInsertBB;
|
2019-11-26 18:25:04 +08:00
|
|
|
unsigned Opc = LoLoop.getStartOpcode();
|
2020-08-19 00:20:05 +08:00
|
|
|
MachineOperand &Count = LoLoop.getLoopStartOperand();
|
2019-06-25 18:45:51 +08:00
|
|
|
|
2021-02-02 19:09:31 +08:00
|
|
|
// A DLS lr, lr we needn't emit
|
|
|
|
MachineInstr* NewStart;
|
|
|
|
if (Opc == ARM::t2DLS && Count.isReg() && Count.getReg() == ARM::LR) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Didn't insert start: DLS lr, lr");
|
|
|
|
NewStart = nullptr;
|
|
|
|
} else {
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*MBB, InsertPt, Start->getDebugLoc(), TII->get(Opc));
|
2019-06-25 18:45:51 +08:00
|
|
|
|
2021-02-02 19:09:31 +08:00
|
|
|
MIB.addDef(ARM::LR);
|
|
|
|
MIB.add(Count);
|
2021-06-13 20:55:34 +08:00
|
|
|
if (isWhileLoopStart(*Start))
|
|
|
|
MIB.addMBB(getWhileLoopStartTargetBB(*Start));
|
2021-02-02 19:09:31 +08:00
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Inserted start: " << *MIB);
|
|
|
|
NewStart = &*MIB;
|
|
|
|
}
|
2019-11-19 01:07:56 +08:00
|
|
|
|
2020-01-17 21:08:24 +08:00
|
|
|
LoLoop.ToRemove.insert(Start);
|
2021-02-02 19:09:31 +08:00
|
|
|
return NewStart;
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
2019-12-20 16:42:11 +08:00
|
|
|
void ARMLowOverheadLoops::ConvertVPTBlocks(LowOverheadLoop &LoLoop) {
|
|
|
|
auto RemovePredicate = [](MachineInstr *MI) {
|
2021-03-19 19:19:32 +08:00
|
|
|
if (MI->isDebugInstr())
|
|
|
|
return;
|
2019-12-20 16:42:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Removing predicate from: " << *MI);
|
2021-03-19 19:19:32 +08:00
|
|
|
int PIdx = llvm::findFirstVPTPredOperandIdx(*MI);
|
|
|
|
assert(PIdx >= 1 && "Trying to unpredicate a non-predicated instruction");
|
|
|
|
assert(MI->getOperand(PIdx).getImm() == ARMVCC::Then &&
|
|
|
|
"Expected Then predicate!");
|
|
|
|
MI->getOperand(PIdx).setImm(ARMVCC::None);
|
|
|
|
MI->getOperand(PIdx + 1).setReg(0);
|
2019-12-20 16:42:11 +08:00
|
|
|
};
|
2019-11-19 01:07:56 +08:00
|
|
|
|
2019-12-20 16:42:11 +08:00
|
|
|
for (auto &Block : LoLoop.getVPTBlocks()) {
|
2020-09-22 16:22:11 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &Insts = Block.getInsts();
|
|
|
|
|
2020-11-06 21:35:35 +08:00
|
|
|
auto ReplaceVCMPWithVPT = [&](MachineInstr *&TheVCMP, MachineInstr *At) {
|
|
|
|
assert(TheVCMP && "Replacing a removed or non-existent VCMP");
|
|
|
|
// Replace the VCMP with a VPT
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*At->getParent(), At, At->getDebugLoc(),
|
|
|
|
TII->get(VCMPOpcodeToVPT(TheVCMP->getOpcode())));
|
|
|
|
MIB.addImm(ARMVCC::Then);
|
|
|
|
// Register one
|
|
|
|
MIB.add(TheVCMP->getOperand(1));
|
|
|
|
// Register two
|
|
|
|
MIB.add(TheVCMP->getOperand(2));
|
|
|
|
// The comparison code, e.g. ge, eq, lt
|
|
|
|
MIB.add(TheVCMP->getOperand(3));
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Combining with VCMP to VPT: " << *MIB);
|
|
|
|
LoLoop.BlockMasksToRecompute.insert(MIB.getInstr());
|
|
|
|
LoLoop.ToRemove.insert(TheVCMP);
|
|
|
|
TheVCMP = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (VPTState::isEntryPredicatedOnVCTP(Block, /*exclusive*/ true)) {
|
|
|
|
MachineInstr *VPST = Insts.front();
|
2020-09-22 16:22:11 +08:00
|
|
|
if (VPTState::hasUniformPredicate(Block)) {
|
|
|
|
// A vpt block starting with VPST, is only predicated upon vctp and has no
|
|
|
|
// internal vpr defs:
|
|
|
|
// - Remove vpst.
|
|
|
|
// - Unpredicate the remaining instructions.
|
2020-11-06 21:35:35 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
|
2020-09-22 16:22:11 +08:00
|
|
|
for (unsigned i = 1; i < Insts.size(); ++i)
|
|
|
|
RemovePredicate(Insts[i]);
|
|
|
|
} else {
|
2020-04-08 21:31:21 +08:00
|
|
|
// The VPT block has a non-uniform predicate but it uses a vpst and its
|
|
|
|
// entry is guarded only by a vctp, which means we:
|
2019-12-20 16:42:11 +08:00
|
|
|
// - Need to remove the original vpst.
|
|
|
|
// - Then need to unpredicate any following instructions, until
|
|
|
|
// we come across the divergent vpr def.
|
|
|
|
// - Insert a new vpst to predicate the instruction(s) that following
|
|
|
|
// the divergent vpr def.
|
2020-09-22 16:22:11 +08:00
|
|
|
MachineInstr *Divergent = VPTState::getDivergent(Block);
|
2021-03-19 19:19:32 +08:00
|
|
|
MachineBasicBlock *MBB = Divergent->getParent();
|
2020-10-30 21:30:58 +08:00
|
|
|
auto DivergentNext = ++MachineBasicBlock::iterator(Divergent);
|
2021-03-19 19:19:32 +08:00
|
|
|
while (DivergentNext != MBB->end() && DivergentNext->isDebugInstr())
|
|
|
|
++DivergentNext;
|
|
|
|
|
2020-10-30 21:30:58 +08:00
|
|
|
bool DivergentNextIsPredicated =
|
2021-03-19 19:19:32 +08:00
|
|
|
DivergentNext != MBB->end() &&
|
2020-10-30 21:30:58 +08:00
|
|
|
getVPTInstrPredicate(*DivergentNext) != ARMVCC::None;
|
|
|
|
|
|
|
|
for (auto I = ++MachineBasicBlock::iterator(VPST), E = DivergentNext;
|
|
|
|
I != E; ++I)
|
2019-12-20 16:42:11 +08:00
|
|
|
RemovePredicate(&*I);
|
|
|
|
|
2020-09-14 22:44:54 +08:00
|
|
|
// Check if the instruction defining vpr is a vcmp so it can be combined
|
|
|
|
// with the VPST This should be the divergent instruction
|
2020-10-30 21:30:58 +08:00
|
|
|
MachineInstr *VCMP =
|
|
|
|
VCMPOpcodeToVPT(Divergent->getOpcode()) != 0 ? Divergent : nullptr;
|
|
|
|
|
|
|
|
if (DivergentNextIsPredicated) {
|
|
|
|
// Insert a VPST at the divergent only if the next instruction
|
|
|
|
// would actually use it. A VCMP following a VPST can be
|
|
|
|
// merged into a VPT so do that instead if the VCMP exists.
|
|
|
|
if (!VCMP) {
|
|
|
|
// Create a VPST (with a null mask for now, we'll recompute it
|
|
|
|
// later)
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*Divergent->getParent(), Divergent,
|
2020-09-24 22:29:05 +08:00
|
|
|
Divergent->getDebugLoc(), TII->get(ARM::MVE_VPST));
|
2020-10-30 21:30:58 +08:00
|
|
|
MIB.addImm(0);
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Created VPST: " << *MIB);
|
|
|
|
LoLoop.BlockMasksToRecompute.insert(MIB.getInstr());
|
|
|
|
} else {
|
|
|
|
// No RDA checks are necessary here since the VPST would have been
|
2020-11-06 21:35:35 +08:00
|
|
|
// directly after the VCMP
|
|
|
|
ReplaceVCMPWithVPT(VCMP, VCMP);
|
2020-10-30 21:30:58 +08:00
|
|
|
}
|
2020-09-14 22:44:54 +08:00
|
|
|
}
|
2019-12-20 16:42:11 +08:00
|
|
|
}
|
2020-11-06 21:35:35 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
|
|
|
|
LoLoop.ToRemove.insert(VPST);
|
2020-09-22 16:22:11 +08:00
|
|
|
} else if (Block.containsVCTP()) {
|
2020-12-14 19:17:01 +08:00
|
|
|
// The vctp will be removed, so either the entire block will be dead or
|
|
|
|
// the block mask of the vp(s)t will need to be recomputed.
|
|
|
|
MachineInstr *VPST = Insts.front();
|
|
|
|
if (Block.size() == 2) {
|
|
|
|
assert(VPST->getOpcode() == ARM::MVE_VPST &&
|
|
|
|
"Found a VPST in an otherwise empty vpt block");
|
|
|
|
LoLoop.ToRemove.insert(VPST);
|
|
|
|
} else
|
|
|
|
LoLoop.BlockMasksToRecompute.insert(VPST);
|
2020-11-06 21:35:35 +08:00
|
|
|
} else if (Insts.front()->getOpcode() == ARM::MVE_VPST) {
|
|
|
|
// If this block starts with a VPST then attempt to merge it with the
|
|
|
|
// preceeding un-merged VCMP into a VPT. This VCMP comes from a VPT
|
|
|
|
// block that no longer exists
|
|
|
|
MachineInstr *VPST = Insts.front();
|
|
|
|
auto Next = ++MachineBasicBlock::iterator(VPST);
|
|
|
|
assert(getVPTInstrPredicate(*Next) != ARMVCC::None &&
|
|
|
|
"The instruction after a VPST must be predicated");
|
2020-11-18 22:10:57 +08:00
|
|
|
(void)Next;
|
2020-11-06 21:35:35 +08:00
|
|
|
MachineInstr *VprDef = RDA->getUniqueReachingMIDef(VPST, ARM::VPR);
|
|
|
|
if (VprDef && VCMPOpcodeToVPT(VprDef->getOpcode()) &&
|
|
|
|
!LoLoop.ToRemove.contains(VprDef)) {
|
|
|
|
MachineInstr *VCMP = VprDef;
|
|
|
|
// The VCMP and VPST can only be merged if the VCMP's operands will have
|
2020-11-19 21:32:23 +08:00
|
|
|
// the same values at the VPST.
|
|
|
|
// If any of the instructions between the VCMP and VPST are predicated
|
|
|
|
// then a different code path is expected to have merged the VCMP and
|
|
|
|
// VPST already.
|
|
|
|
if (!std::any_of(++MachineBasicBlock::iterator(VCMP),
|
|
|
|
MachineBasicBlock::iterator(VPST), hasVPRUse) &&
|
|
|
|
RDA->hasSameReachingDef(VCMP, VPST, VCMP->getOperand(1).getReg()) &&
|
2020-11-06 21:35:35 +08:00
|
|
|
RDA->hasSameReachingDef(VCMP, VPST, VCMP->getOperand(2).getReg())) {
|
|
|
|
ReplaceVCMPWithVPT(VCMP, VPST);
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Removing VPST: " << *VPST);
|
|
|
|
LoLoop.ToRemove.insert(VPST);
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 21:31:21 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 16:22:11 +08:00
|
|
|
|
|
|
|
LoLoop.ToRemove.insert(LoLoop.VCTPs.begin(), LoLoop.VCTPs.end());
|
2019-11-19 01:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
|
2019-06-25 18:45:51 +08:00
|
|
|
|
|
|
|
// Combine the LoopDec and LoopEnd instructions into LE(TP).
|
2019-11-19 01:07:56 +08:00
|
|
|
auto ExpandLoopEnd = [this](LowOverheadLoop &LoLoop) {
|
|
|
|
MachineInstr *End = LoLoop.End;
|
2019-06-25 18:45:51 +08:00
|
|
|
MachineBasicBlock *MBB = End->getParent();
|
2019-11-19 01:07:56 +08:00
|
|
|
unsigned Opc = LoLoop.IsTailPredicationLegal() ?
|
|
|
|
ARM::MVE_LETP : ARM::t2LEUpdate;
|
2019-06-25 18:45:51 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MBB, End, End->getDebugLoc(),
|
2019-11-19 01:07:56 +08:00
|
|
|
TII->get(Opc));
|
2019-06-25 18:45:51 +08:00
|
|
|
MIB.addDef(ARM::LR);
|
2020-12-10 20:14:23 +08:00
|
|
|
unsigned Off = LoLoop.Dec == LoLoop.End ? 1 : 0;
|
|
|
|
MIB.add(End->getOperand(Off + 0));
|
|
|
|
MIB.add(End->getOperand(Off + 1));
|
2019-06-25 18:45:51 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Inserted LE: " << *MIB);
|
2020-03-03 23:19:57 +08:00
|
|
|
LoLoop.ToRemove.insert(LoLoop.Dec);
|
|
|
|
LoLoop.ToRemove.insert(End);
|
2019-07-01 16:21:28 +08:00
|
|
|
return &*MIB;
|
2019-06-25 18:45:51 +08:00
|
|
|
};
|
|
|
|
|
2019-07-01 16:21:28 +08:00
|
|
|
// TODO: We should be able to automatically remove these branches before we
|
|
|
|
// get here - probably by teaching analyzeBranch about the pseudo
|
|
|
|
// instructions.
|
|
|
|
// If there is an unconditional branch, after I, that just branches to the
|
|
|
|
// next block, remove it.
|
|
|
|
auto RemoveDeadBranch = [](MachineInstr *I) {
|
|
|
|
MachineBasicBlock *BB = I->getParent();
|
|
|
|
MachineInstr *Terminator = &BB->instr_back();
|
|
|
|
if (Terminator->isUnconditionalBranch() && I != Terminator) {
|
|
|
|
MachineBasicBlock *Succ = Terminator->getOperand(0).getMBB();
|
|
|
|
if (BB->isLayoutSuccessor(Succ)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Removing branch: " << *Terminator);
|
|
|
|
Terminator->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-19 01:07:56 +08:00
|
|
|
if (LoLoop.Revert) {
|
2021-06-13 20:55:34 +08:00
|
|
|
if (isWhileLoopStart(*LoLoop.Start))
|
2019-11-19 01:07:56 +08:00
|
|
|
RevertWhile(LoLoop.Start);
|
2019-07-10 20:29:43 +08:00
|
|
|
else
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
RevertDo(LoLoop.Start);
|
2020-12-10 20:14:23 +08:00
|
|
|
if (LoLoop.Dec == LoLoop.End)
|
|
|
|
RevertLoopEndDec(LoLoop.End);
|
|
|
|
else
|
|
|
|
RevertLoopEnd(LoLoop.End, RevertLoopDec(LoLoop.Dec));
|
2019-06-25 18:45:51 +08:00
|
|
|
} else {
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.Start = ExpandLoopStart(LoLoop);
|
2021-02-02 19:09:31 +08:00
|
|
|
if (LoLoop.Start)
|
|
|
|
RemoveDeadBranch(LoLoop.Start);
|
2019-11-19 01:07:56 +08:00
|
|
|
LoLoop.End = ExpandLoopEnd(LoLoop);
|
|
|
|
RemoveDeadBranch(LoLoop.End);
|
2020-08-28 20:56:16 +08:00
|
|
|
if (LoLoop.IsTailPredicationLegal())
|
2019-12-20 16:42:11 +08:00
|
|
|
ConvertVPTBlocks(LoLoop);
|
2020-01-17 21:08:24 +08:00
|
|
|
for (auto *I : LoLoop.ToRemove) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Erasing " << *I);
|
|
|
|
I->eraseFromParent();
|
[ARM][LowOverheadLoops] Remove dead loop update instructions.
After creating a low-overhead loop, the loop update instruction was still
lingering around hurting performance. This removes dead loop update
instructions, which in our case are mostly SUBS instructions.
To support this, some helper functions were added to MachineLoopUtils and
ReachingDefAnalysis to analyse live-ins of loop exit blocks and find uses
before a particular loop instruction, respectively.
This is a first version that removes a SUBS instruction when there are no other
uses inside and outside the loop block, but there are some more interesting
cases in test/CodeGen/Thumb2/LowOverheadLoops/mve-tail-data-types.ll which
shows that there is room for improvement. For example, we can't handle this
case yet:
..
dlstp.32 lr, r2
.LBB0_1:
mov r3, r2
subs r2, #4
vldrh.u32 q2, [r1], #8
vmov q1, q0
vmla.u32 q0, q2, r0
letp lr, .LBB0_1
@ %bb.2:
vctp.32 r3
..
which is a lot more tricky because r2 is not only used by the subs, but also by
the mov to r3, which is used outside the low-overhead loop by the vctp
instruction, and that requires a bit of a different approach, and I will follow
up on this.
Differential Revision: https://reviews.llvm.org/D71007
2019-12-11 18:11:48 +08:00
|
|
|
}
|
2020-04-08 18:55:09 +08:00
|
|
|
for (auto *I : LoLoop.BlockMasksToRecompute) {
|
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Recomputing VPT/VPST Block Mask: " << *I);
|
|
|
|
recomputeVPTBlockMask(*I);
|
|
|
|
LLVM_DEBUG(dbgs() << " ... done: " << *I);
|
|
|
|
}
|
2019-06-25 18:45:51 +08:00
|
|
|
}
|
2020-01-16 23:42:41 +08:00
|
|
|
|
2020-02-14 16:28:26 +08:00
|
|
|
PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
|
2020-01-16 23:42:41 +08:00
|
|
|
DFS.ProcessLoop();
|
|
|
|
const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
|
|
|
|
for (auto *MBB : PostOrder) {
|
|
|
|
recomputeLiveIns(*MBB);
|
|
|
|
// FIXME: For some reason, the live-in print order is non-deterministic for
|
|
|
|
// our tests and I can't out why... So just sort them.
|
|
|
|
MBB->sortUniqueLiveIns();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *MBB : reverse(PostOrder))
|
|
|
|
recomputeLivenessFlags(*MBB);
|
2020-02-26 19:14:54 +08:00
|
|
|
|
|
|
|
// We've moved, removed and inserted new instructions, so update RDA.
|
|
|
|
RDA->reset();
|
2019-06-25 18:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 20:19:32 +08:00
|
|
|
bool ARMLowOverheadLoops::RevertNonLoops() {
|
2019-07-22 22:16:40 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ARM Loops: Reverting any remaining pseudos...\n");
|
|
|
|
bool Changed = false;
|
|
|
|
|
2019-09-17 20:19:32 +08:00
|
|
|
for (auto &MBB : *MF) {
|
2019-07-22 22:16:40 +08:00
|
|
|
SmallVector<MachineInstr*, 4> Starts;
|
|
|
|
SmallVector<MachineInstr*, 4> Decs;
|
|
|
|
SmallVector<MachineInstr*, 4> Ends;
|
2020-12-10 20:14:23 +08:00
|
|
|
SmallVector<MachineInstr *, 4> EndDecs;
|
2019-07-22 22:16:40 +08:00
|
|
|
|
|
|
|
for (auto &I : MBB) {
|
2019-12-16 17:11:47 +08:00
|
|
|
if (isLoopStart(I))
|
2019-07-22 22:16:40 +08:00
|
|
|
Starts.push_back(&I);
|
|
|
|
else if (I.getOpcode() == ARM::t2LoopDec)
|
|
|
|
Decs.push_back(&I);
|
|
|
|
else if (I.getOpcode() == ARM::t2LoopEnd)
|
|
|
|
Ends.push_back(&I);
|
2020-12-10 20:14:23 +08:00
|
|
|
else if (I.getOpcode() == ARM::t2LoopEndDec)
|
|
|
|
EndDecs.push_back(&I);
|
2019-07-22 22:16:40 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 20:14:23 +08:00
|
|
|
if (Starts.empty() && Decs.empty() && Ends.empty() && EndDecs.empty())
|
2019-07-22 22:16:40 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
for (auto *Start : Starts) {
|
2021-06-13 20:55:34 +08:00
|
|
|
if (isWhileLoopStart(*Start))
|
2019-07-22 22:16:40 +08:00
|
|
|
RevertWhile(Start);
|
|
|
|
else
|
[ARM] Alter t2DoLoopStart to define lr
This changes the definition of t2DoLoopStart from
t2DoLoopStart rGPR
to
GPRlr = t2DoLoopStart rGPR
This will hopefully mean that low overhead loops are more tied together,
and we can more reliably generate loops without reverting or being at
the whims of the register allocator.
This is a fairly simple change in itself, but leads to a number of other
required alterations.
- The hardware loop pass, if UsePhi is set, now generates loops of the
form:
%start = llvm.start.loop.iterations(%N)
loop:
%p = phi [%start], [%dec]
%dec = llvm.loop.decrement.reg(%p, 1)
%c = icmp ne %dec, 0
br %c, loop, exit
- For this a new llvm.start.loop.iterations intrinsic was added, identical
to llvm.set.loop.iterations but produces a value as seen above, gluing
the loop together more through def-use chains.
- This new instrinsic conceptually produces the same output as input,
which is taught to SCEV so that the checks in MVETailPredication are not
affected.
- Some minor changes are needed to the ARMLowOverheadLoop pass, but it has
been left mostly as before. We should now more reliably be able to tell
that the t2DoLoopStart is correct without having to prove it, but
t2WhileLoopStart and tail-predicated loops will remain the same.
- And all the tests have been updated. There are a lot of them!
This patch on it's own might cause more trouble that it helps, with more
tail-predicated loops being reverted, but some additional patches can
hopefully improve upon that to get to something that is better overall.
Differential Revision: https://reviews.llvm.org/D89881
2020-11-10 23:57:58 +08:00
|
|
|
RevertDo(Start);
|
2019-07-22 22:16:40 +08:00
|
|
|
}
|
|
|
|
for (auto *Dec : Decs)
|
|
|
|
RevertLoopDec(Dec);
|
|
|
|
|
|
|
|
for (auto *End : Ends)
|
|
|
|
RevertLoopEnd(End);
|
2020-12-10 20:14:23 +08:00
|
|
|
for (auto *End : EndDecs)
|
|
|
|
RevertLoopEndDec(End);
|
2019-07-22 22:16:40 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:45:51 +08:00
|
|
|
FunctionPass *llvm::createARMLowOverheadLoopsPass() {
|
|
|
|
return new ARMLowOverheadLoops();
|
|
|
|
}
|