LLVM  10.0.0
MVEVPTBlockPass.cpp
Go to the documentation of this file.
1 //===-- MVEVPTBlockPass.cpp - Insert MVE VPT blocks -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ARM.h"
10 #include "ARMMachineFunctionInfo.h"
11 #include "ARMSubtarget.h"
13 #include "Thumb2InstrInfo.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringRef.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/Support/Debug.h"
29 #include <cassert>
30 #include <new>
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "arm-mve-vpt"
35 
36 namespace {
37  class MVEVPTBlock : public MachineFunctionPass {
38  public:
39  static char ID;
40 
41  MVEVPTBlock() : MachineFunctionPass(ID) {}
42 
43  bool runOnMachineFunction(MachineFunction &Fn) override;
44 
45  void getAnalysisUsage(AnalysisUsage &AU) const override {
46  AU.setPreservesCFG();
49  }
50 
51  MachineFunctionProperties getRequiredProperties() const override {
55  }
56 
57  StringRef getPassName() const override {
58  return "MVE VPT block insertion pass";
59  }
60 
61  private:
62  bool InsertVPTBlocks(MachineBasicBlock &MBB);
63 
64  const Thumb2InstrInfo *TII = nullptr;
65  ReachingDefAnalysis *RDA = nullptr;
66  };
67 
68  char MVEVPTBlock::ID = 0;
69 
70 } // end anonymous namespace
71 
72 INITIALIZE_PASS(MVEVPTBlock, DEBUG_TYPE, "ARM MVE VPT block pass", false, false)
73 
76  unsigned &NewOpcode) {
77  // First, search backwards to the instruction that defines VPR
78  auto *Def = RDA->getReachingMIDef(MI, ARM::VPR);
79  if (!Def)
80  return nullptr;
81 
82  // Now check that Def is a VCMP
83  if (!(NewOpcode = VCMPOpcodeToVPT(Def->getOpcode())))
84  return nullptr;
85 
86  // Check that Def's operands are not defined between the VCMP and MI, i.e.
87  // check that they have the same reaching def.
88  if (!RDA->hasSameReachingDef(Def, MI, Def->getOperand(1).getReg()) ||
89  !RDA->hasSameReachingDef(Def, MI, Def->getOperand(2).getReg()))
90  return nullptr;
91 
92  return Def;
93 }
94 
95 bool MVEVPTBlock::InsertVPTBlocks(MachineBasicBlock &Block) {
96  bool Modified = false;
99  SmallSet<MachineInstr *, 4> RemovedVCMPs;
100 
101  while (MBIter != EndIter) {
102  MachineInstr *MI = &*MBIter;
103  unsigned PredReg = 0;
104  DebugLoc dl = MI->getDebugLoc();
105 
106  ARMVCC::VPTCodes Pred = getVPTInstrPredicate(*MI, PredReg);
107 
108  // The idea of the predicate is that None, Then and Else are for use when
109  // handling assembly language: they correspond to the three possible
110  // suffixes "", "t" and "e" on the mnemonic. So when instructions are read
111  // from assembly source or disassembled from object code, you expect to see
112  // a mixture whenever there's a long VPT block. But in code generation, we
113  // hope we'll never generate an Else as input to this pass.
114  assert(Pred != ARMVCC::Else && "VPT block pass does not expect Else preds");
115 
116  if (Pred == ARMVCC::None) {
117  ++MBIter;
118  continue;
119  }
120 
121  LLVM_DEBUG(dbgs() << "VPT block created for: "; MI->dump());
122  int VPTInstCnt = 1;
123  ARMVCC::VPTCodes NextPred;
124 
125  // Look at subsequent instructions, checking if they can be in the same VPT
126  // block.
127  ++MBIter;
128  while (MBIter != EndIter && VPTInstCnt < 4) {
129  NextPred = getVPTInstrPredicate(*MBIter, PredReg);
130  assert(NextPred != ARMVCC::Else &&
131  "VPT block pass does not expect Else preds");
132  if (NextPred != Pred)
133  break;
134  LLVM_DEBUG(dbgs() << " adding : "; MBIter->dump());
135  ++VPTInstCnt;
136  ++MBIter;
137  };
138 
139  unsigned BlockMask = getARMVPTBlockMask(VPTInstCnt);
140 
141  // Search back for a VCMP that can be folded to create a VPT, or else create
142  // a VPST directly
143  MachineInstrBuilder MIBuilder;
144  unsigned NewOpcode;
145  MachineInstr *VCMP = findVCMPToFoldIntoVPST(MI, RDA, NewOpcode);
146  if (VCMP) {
147  LLVM_DEBUG(dbgs() << " folding VCMP into VPST: "; VCMP->dump());
148  MIBuilder = BuildMI(Block, MI, dl, TII->get(NewOpcode));
149  MIBuilder.addImm(BlockMask);
150  MIBuilder.add(VCMP->getOperand(1));
151  MIBuilder.add(VCMP->getOperand(2));
152  MIBuilder.add(VCMP->getOperand(3));
153  // We delay removing the actual VCMP instruction by saving it to a list
154  // and deleting all instructions in this list in one go after we have
155  // created the VPT blocks. We do this in order not to invalidate the
156  // ReachingDefAnalysis that is queried by 'findVCMPToFoldIntoVPST'.
157  RemovedVCMPs.insert(VCMP);
158  } else {
159  MIBuilder = BuildMI(Block, MI, dl, TII->get(ARM::MVE_VPST));
160  MIBuilder.addImm(BlockMask);
161  }
162 
164  Block, MachineBasicBlock::instr_iterator(MIBuilder.getInstr()), MBIter);
165 
166  Modified = true;
167  }
168 
169  for (auto *I : RemovedVCMPs)
170  I->eraseFromParent();
171 
172  return Modified;
173 }
174 
175 bool MVEVPTBlock::runOnMachineFunction(MachineFunction &Fn) {
176  if (skipFunction(Fn.getFunction()))
177  return false;
178 
179  const ARMSubtarget &STI =
180  static_cast<const ARMSubtarget &>(Fn.getSubtarget());
181 
182  if (!STI.isThumb2() || !STI.hasMVEIntegerOps())
183  return false;
184 
185  TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
186  RDA = &getAnalysis<ReachingDefAnalysis>();
187 
188  LLVM_DEBUG(dbgs() << "********** ARM MVE VPT BLOCKS **********\n"
189  << "********** Function: " << Fn.getName() << '\n');
190 
191  bool Modified = false;
192  for (MachineBasicBlock &MBB : Fn)
193  Modified |= InsertVPTBlocks(MBB);
194 
195  LLVM_DEBUG(dbgs() << "**************************************\n");
196  return Modified;
197 }
198 
199 /// createMVEVPTBlock - Returns an instance of the MVE VPT block
200 /// insertion pass.
201 FunctionPass *llvm::createMVEVPTBlockPass() { return new MVEVPTBlock(); }
const MachineInstrBuilder & add(const MachineOperand &MO) const
instr_iterator instr_begin()
instr_iterator instr_end()
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:400
A debug info location.
Definition: DebugLoc.h:33
This class provides the reaching def analysis.
const ARMBaseInstrInfo * getInstrInfo() const override
Definition: ARMSubtarget.h:526
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static unsigned VCMPOpcodeToVPT(unsigned Opcode)
static unsigned getARMVPTBlockMask(unsigned NumInsts)
Definition: ARMBaseInfo.h:114
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
FunctionPass * createMVEVPTBlockPass()
createMVEVPTBlock - Returns an instance of the MVE VPT block insertion pass.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
Represent the analysis usage information of a pass.
#define DEBUG_TYPE
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn&#39;t already there.
Definition: SmallSet.h:180
static MachineInstr * findVCMPToFoldIntoVPST(MachineInstr *MI, ReachingDefAnalysis *RDA, unsigned &NewOpcode)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
Iterator for intrusive lists based on ilist_node.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
MachineFunctionProperties & set(Property P)
bool isThumb2() const
Definition: ARMSubtarget.h:768
Representation of each machine instruction.
Definition: MachineInstr.h:63
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
ARMVCC::VPTCodes getVPTInstrPredicate(const MachineInstr &MI, unsigned &PredReg)
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasMVEIntegerOps() const
Definition: ARMSubtarget.h:585
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
Properties which a MachineFunction may have at a given point in time.