2010-08-20 02:52:17 +08:00
|
|
|
//===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
|
2009-05-16 08:33:53 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Pass to verify generated machine code. The following is checked:
|
|
|
|
//
|
|
|
|
// Operand counts: All explicit operands must be present.
|
|
|
|
//
|
|
|
|
// Register classes: All physical and virtual register operands must be
|
|
|
|
// compatible with the register class required by the instruction descriptor.
|
|
|
|
//
|
|
|
|
// Register live intervals: Registers must be defined only once, and must be
|
|
|
|
// defined before use.
|
|
|
|
//
|
|
|
|
// The machine code verifier is enabled from LLVMTargetMachine.cpp with the
|
|
|
|
// command-line option -verify-machineinstrs, or by defining the environment
|
|
|
|
// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
|
|
|
|
// the verifier errors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2013-07-16 05:26:31 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2015-12-03 07:06:39 +08:00
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
2010-08-06 06:32:21 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2010-11-02 03:49:52 +08:00
|
|
|
#include "llvm/CodeGen/LiveStackAnalysis.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2009-08-14 00:19:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-10-08 01:36:00 +08:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
2009-05-16 08:33:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/InlineAsm.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2011-05-05 06:54:05 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-05-16 08:33:53 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2009-05-16 08:33:53 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-19 04:36:57 +08:00
|
|
|
struct MachineVerifier {
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2010-12-18 08:06:56 +08:00
|
|
|
MachineVerifier(Pass *pass, const char *b) :
|
2009-11-19 04:36:57 +08:00
|
|
|
PASS(pass),
|
2015-02-04 08:02:59 +08:00
|
|
|
Banner(b)
|
2009-11-19 04:36:57 +08:00
|
|
|
{}
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2016-02-16 03:25:31 +08:00
|
|
|
unsigned verify(MachineFunction &MF);
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
Pass *const PASS;
|
2010-12-18 08:06:56 +08:00
|
|
|
const char *Banner;
|
2009-05-16 08:33:53 +08:00
|
|
|
const MachineFunction *MF;
|
|
|
|
const TargetMachine *TM;
|
2011-06-28 05:26:13 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2009-05-16 08:33:53 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
|
|
|
|
unsigned foundErrors;
|
|
|
|
|
2016-08-03 00:17:15 +08:00
|
|
|
// Avoid querying the MachineFunctionProperties for each operand.
|
|
|
|
bool isFunctionRegBankSelected;
|
2016-08-03 00:49:22 +08:00
|
|
|
bool isFunctionSelected;
|
2016-08-03 00:17:15 +08:00
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
typedef SmallVector<unsigned, 16> RegVector;
|
2012-02-28 09:42:41 +08:00
|
|
|
typedef SmallVector<const uint32_t*, 4> RegMaskVector;
|
2009-05-16 08:33:53 +08:00
|
|
|
typedef DenseSet<unsigned> RegSet;
|
|
|
|
typedef DenseMap<unsigned, const MachineInstr*> RegMap;
|
2012-08-21 04:52:06 +08:00
|
|
|
typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2011-09-24 06:45:39 +08:00
|
|
|
const MachineInstr *FirstTerminator;
|
2012-08-21 04:52:06 +08:00
|
|
|
BlockSet FunctionBlocks;
|
2011-09-24 06:45:39 +08:00
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
BitVector regsReserved;
|
|
|
|
RegSet regsLive;
|
2009-08-08 21:19:25 +08:00
|
|
|
RegVector regsDefined, regsDead, regsKilled;
|
2012-02-28 09:42:41 +08:00
|
|
|
RegMaskVector regMasks;
|
2009-08-08 21:19:25 +08:00
|
|
|
RegSet regsLiveInButUnused;
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2011-01-13 05:27:48 +08:00
|
|
|
SlotIndex lastIndex;
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
// Add Reg and any sub-registers to RV
|
|
|
|
void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
|
|
|
|
RV.push_back(Reg);
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg))
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
|
|
|
|
RV.push_back(*SubRegs);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct BBInfo {
|
|
|
|
// Is this MBB reachable from the MF entry point?
|
|
|
|
bool reachable;
|
|
|
|
|
|
|
|
// Vregs that must be live in because they are used without being
|
|
|
|
// defined. Map value is the user.
|
|
|
|
RegMap vregsLiveIn;
|
|
|
|
|
|
|
|
// Regs killed in MBB. They may be defined again, and will then be in both
|
|
|
|
// regsKilled and regsLiveOut.
|
|
|
|
RegSet regsKilled;
|
|
|
|
|
|
|
|
// Regs defined in MBB and live out. Note that vregs passing through may
|
|
|
|
// be live out without being mentioned here.
|
|
|
|
RegSet regsLiveOut;
|
|
|
|
|
|
|
|
// Vregs that pass through MBB untouched. This set is disjoint from
|
|
|
|
// regsKilled and regsLiveOut.
|
|
|
|
RegSet vregsPassed;
|
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
// Vregs that must pass through MBB because they are needed by a successor
|
|
|
|
// block. This set is disjoint from regsLiveOut.
|
|
|
|
RegSet vregsRequired;
|
|
|
|
|
2012-08-21 04:52:06 +08:00
|
|
|
// Set versions of block's predecessor and successor lists.
|
|
|
|
BlockSet Preds, Succs;
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
BBInfo() : reachable(false) {}
|
|
|
|
|
|
|
|
// Add register to vregsPassed if it belongs there. Return true if
|
|
|
|
// anything changed.
|
|
|
|
bool addPassed(unsigned Reg) {
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
return false;
|
|
|
|
if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
|
|
|
|
return false;
|
|
|
|
return vregsPassed.insert(Reg).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for a full set.
|
|
|
|
bool addPassed(const RegSet &RS) {
|
|
|
|
bool changed = false;
|
|
|
|
for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
|
|
|
|
if (addPassed(*I))
|
|
|
|
changed = true;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
// Add register to vregsRequired if it belongs there. Return true if
|
|
|
|
// anything changed.
|
|
|
|
bool addRequired(unsigned Reg) {
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
return false;
|
|
|
|
if (regsLiveOut.count(Reg))
|
|
|
|
return false;
|
|
|
|
return vregsRequired.insert(Reg).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for a full set.
|
|
|
|
bool addRequired(const RegSet &RS) {
|
|
|
|
bool changed = false;
|
|
|
|
for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
|
|
|
|
if (addRequired(*I))
|
|
|
|
changed = true;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for a full map.
|
|
|
|
bool addRequired(const RegMap &RM) {
|
|
|
|
bool changed = false;
|
|
|
|
for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
|
|
|
|
if (addRequired(I->first))
|
|
|
|
changed = true;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
// Live-out registers are either in regsLiveOut or vregsPassed.
|
|
|
|
bool isLiveOut(unsigned Reg) const {
|
|
|
|
return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extra register info per MBB.
|
|
|
|
DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
|
|
|
|
|
|
|
|
bool isReserved(unsigned Reg) {
|
2009-08-05 03:18:01 +08:00
|
|
|
return Reg < regsReserved.size() && regsReserved.test(Reg);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 03:17:48 +08:00
|
|
|
bool isAllocatable(unsigned Reg) {
|
2012-10-16 08:05:06 +08:00
|
|
|
return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
|
2012-02-15 03:17:48 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
// Analysis information if available
|
|
|
|
LiveVariables *LiveVars;
|
2010-10-27 06:36:07 +08:00
|
|
|
LiveIntervals *LiveInts;
|
2010-11-02 03:49:52 +08:00
|
|
|
LiveStacks *LiveStks;
|
2010-10-27 04:21:46 +08:00
|
|
|
SlotIndexes *Indexes;
|
2009-11-19 04:36:57 +08:00
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
void visitMachineFunctionBefore();
|
|
|
|
void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
|
2012-06-07 06:34:30 +08:00
|
|
|
void visitMachineBundleBefore(const MachineInstr *MI);
|
2009-05-16 08:33:53 +08:00
|
|
|
void visitMachineInstrBefore(const MachineInstr *MI);
|
|
|
|
void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
|
|
|
|
void visitMachineInstrAfter(const MachineInstr *MI);
|
2012-06-07 06:34:30 +08:00
|
|
|
void visitMachineBundleAfter(const MachineInstr *MI);
|
2009-05-16 08:33:53 +08:00
|
|
|
void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
|
|
|
|
void visitMachineFunctionAfter();
|
|
|
|
|
|
|
|
void report(const char *msg, const MachineFunction *MF);
|
|
|
|
void report(const char *msg, const MachineBasicBlock *MBB);
|
|
|
|
void report(const char *msg, const MachineInstr *MI);
|
|
|
|
void report(const char *msg, const MachineOperand *MO, unsigned MONum);
|
2015-11-10 07:59:33 +08:00
|
|
|
|
|
|
|
void report_context(const LiveInterval &LI) const;
|
2016-07-26 03:39:01 +08:00
|
|
|
void report_context(const LiveRange &LR, unsigned VRegUnit,
|
2015-11-10 07:59:33 +08:00
|
|
|
LaneBitmask LaneMask) const;
|
|
|
|
void report_context(const LiveRange::Segment &S) const;
|
|
|
|
void report_context(const VNInfo &VNI) const;
|
2016-02-02 10:44:25 +08:00
|
|
|
void report_context(SlotIndex Pos) const;
|
|
|
|
void report_context_liverange(const LiveRange &LR) const;
|
2016-02-03 04:04:51 +08:00
|
|
|
void report_context_lanemask(LaneBitmask LaneMask) const;
|
2016-05-12 05:31:39 +08:00
|
|
|
void report_context_vreg(unsigned VReg) const;
|
2016-02-03 04:04:51 +08:00
|
|
|
void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2012-08-30 02:11:05 +08:00
|
|
|
void verifyInlineAsm(const MachineInstr *MI);
|
|
|
|
|
2012-03-29 04:47:35 +08:00
|
|
|
void checkLiveness(const MachineOperand *MO, unsigned MONum);
|
2016-02-03 04:04:51 +08:00
|
|
|
void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
|
|
|
|
SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask LaneMask = LaneBitmask::getNone());
|
2016-02-03 04:04:51 +08:00
|
|
|
void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
|
|
|
|
SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask LaneMask = LaneBitmask::getNone());
|
2016-02-03 04:04:51 +08:00
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
void markReachable(const MachineBasicBlock *MBB);
|
2010-01-06 04:59:36 +08:00
|
|
|
void calcRegsPassed();
|
2009-05-16 08:33:53 +08:00
|
|
|
void checkPHIOps(const MachineBasicBlock *MBB);
|
2009-11-19 04:36:57 +08:00
|
|
|
|
|
|
|
void calcRegsRequired();
|
|
|
|
void verifyLiveVariables();
|
2010-08-07 02:04:19 +08:00
|
|
|
void verifyLiveIntervals();
|
2012-08-02 08:20:20 +08:00
|
|
|
void verifyLiveInterval(const LiveInterval&);
|
2014-12-10 09:12:10 +08:00
|
|
|
void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask);
|
2013-10-11 05:28:54 +08:00
|
|
|
void verifyLiveRangeSegment(const LiveRange&,
|
2014-12-10 09:12:10 +08:00
|
|
|
const LiveRange::const_iterator I, unsigned,
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask);
|
|
|
|
void verifyLiveRange(const LiveRange&, unsigned,
|
|
|
|
LaneBitmask LaneMask = LaneBitmask::getNone());
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
void verifyStackFrame();
|
2015-09-10 01:49:46 +08:00
|
|
|
|
|
|
|
void verifySlotIndexes() const;
|
2016-03-30 01:40:22 +08:00
|
|
|
void verifyProperties(const MachineFunction &MF);
|
2009-11-19 04:36:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MachineVerifierPass : public MachineFunctionPass {
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2014-12-12 03:41:51 +08:00
|
|
|
const std::string Banner;
|
2009-11-19 04:36:57 +08:00
|
|
|
|
2014-12-12 03:41:51 +08:00
|
|
|
MachineVerifierPass(const std::string &banner = nullptr)
|
|
|
|
: MachineFunctionPass(ID), Banner(banner) {
|
2010-10-20 01:21:58 +08:00
|
|
|
initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-11-19 04:36:57 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2009-11-19 04:36:57 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override {
|
2016-02-16 03:25:31 +08:00
|
|
|
unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
|
|
|
|
if (FoundErrors)
|
|
|
|
report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
|
2009-11-19 04:36:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-05-16 08:33:53 +08:00
|
|
|
};
|
2009-11-19 04:36:57 +08:00
|
|
|
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
char MachineVerifierPass::ID = 0;
|
2010-08-24 01:52:01 +08:00
|
|
|
INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Verify generated machine code", false, false)
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2014-12-12 03:41:51 +08:00
|
|
|
FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
|
2010-12-18 08:06:56 +08:00
|
|
|
return new MachineVerifierPass(Banner);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 03:25:31 +08:00
|
|
|
bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
|
|
|
|
const {
|
|
|
|
MachineFunction &MF = const_cast<MachineFunction&>(*this);
|
|
|
|
unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
|
|
|
|
if (AbortOnErrors && FoundErrors)
|
|
|
|
report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
|
|
|
|
return FoundErrors == 0;
|
2009-11-14 05:56:09 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:49:46 +08:00
|
|
|
void MachineVerifier::verifySlotIndexes() const {
|
|
|
|
if (Indexes == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure the IdxMBB list is sorted by slot indexes.
|
|
|
|
SlotIndex Last;
|
|
|
|
for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
|
|
|
|
E = Indexes->MBBIndexEnd(); I != E; ++I) {
|
|
|
|
assert(!Last.isValid() || I->first > Last);
|
|
|
|
Last = I->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 01:40:22 +08:00
|
|
|
void MachineVerifier::verifyProperties(const MachineFunction &MF) {
|
|
|
|
// If a pass has introduced virtual registers without clearing the
|
2016-08-25 09:27:13 +08:00
|
|
|
// NoVRegs property (or set it without allocating the vregs)
|
2016-03-30 01:40:22 +08:00
|
|
|
// then report an error.
|
|
|
|
if (MF.getProperties().hasProperty(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs) &&
|
|
|
|
MRI->getNumVirtRegs())
|
|
|
|
report("Function has NoVRegs property but there are VReg operands", &MF);
|
2016-03-30 01:40:22 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 03:25:31 +08:00
|
|
|
unsigned MachineVerifier::verify(MachineFunction &MF) {
|
2009-05-16 08:33:53 +08:00
|
|
|
foundErrors = 0;
|
|
|
|
|
|
|
|
this->MF = &MF;
|
|
|
|
TM = &MF.getTarget();
|
2014-10-14 15:00:33 +08:00
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
2009-05-16 08:33:53 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
|
2016-08-03 00:17:15 +08:00
|
|
|
isFunctionRegBankSelected = MF.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::RegBankSelected);
|
2016-08-03 00:49:22 +08:00
|
|
|
isFunctionSelected = MF.getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::Selected);
|
2016-08-03 00:17:15 +08:00
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
LiveVars = nullptr;
|
|
|
|
LiveInts = nullptr;
|
|
|
|
LiveStks = nullptr;
|
|
|
|
Indexes = nullptr;
|
2009-11-19 04:36:57 +08:00
|
|
|
if (PASS) {
|
2010-08-06 06:32:21 +08:00
|
|
|
LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
|
2010-08-06 07:51:26 +08:00
|
|
|
// We don't want to verify LiveVariables if LiveIntervals is available.
|
|
|
|
if (!LiveInts)
|
|
|
|
LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
|
2010-11-02 03:49:52 +08:00
|
|
|
LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
|
2010-10-27 04:21:46 +08:00
|
|
|
Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
|
2009-11-19 04:36:57 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:49:46 +08:00
|
|
|
verifySlotIndexes();
|
|
|
|
|
2016-03-30 01:40:22 +08:00
|
|
|
verifyProperties(MF);
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
visitMachineFunctionBefore();
|
|
|
|
for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
|
|
|
|
MFI!=MFE; ++MFI) {
|
2015-10-10 03:40:45 +08:00
|
|
|
visitMachineBasicBlockBefore(&*MFI);
|
2012-06-07 06:34:30 +08:00
|
|
|
// Keep track of the current bundle header.
|
2014-04-14 08:51:57 +08:00
|
|
|
const MachineInstr *CurBundle = nullptr;
|
2012-12-19 06:55:07 +08:00
|
|
|
// Do we expect the next instruction to be part of the same bundle?
|
|
|
|
bool InBundle = false;
|
|
|
|
|
2011-12-14 10:11:42 +08:00
|
|
|
for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
|
|
|
|
MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
|
2015-10-10 03:40:45 +08:00
|
|
|
if (MBBI->getParent() != &*MFI) {
|
2016-09-03 09:22:56 +08:00
|
|
|
report("Bad instruction parent pointer", &*MFI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Instruction: " << *MBBI;
|
2011-01-13 05:27:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-19 06:55:07 +08:00
|
|
|
|
|
|
|
// Check for consistent bundle flags.
|
|
|
|
if (InBundle && !MBBI->isBundledWithPred())
|
|
|
|
report("Missing BundledPred flag, "
|
2015-10-10 03:40:45 +08:00
|
|
|
"BundledSucc was set on predecessor",
|
|
|
|
&*MBBI);
|
2012-12-19 06:55:07 +08:00
|
|
|
if (!InBundle && MBBI->isBundledWithPred())
|
|
|
|
report("BundledPred flag is set, "
|
2015-10-10 03:40:45 +08:00
|
|
|
"but BundledSucc not set on predecessor",
|
|
|
|
&*MBBI);
|
2012-12-19 06:55:07 +08:00
|
|
|
|
2012-06-07 06:34:30 +08:00
|
|
|
// Is this a bundle header?
|
|
|
|
if (!MBBI->isInsideBundle()) {
|
|
|
|
if (CurBundle)
|
|
|
|
visitMachineBundleAfter(CurBundle);
|
2015-10-10 03:40:45 +08:00
|
|
|
CurBundle = &*MBBI;
|
2012-06-07 06:34:30 +08:00
|
|
|
visitMachineBundleBefore(CurBundle);
|
|
|
|
} else if (!CurBundle)
|
2016-09-03 09:22:56 +08:00
|
|
|
report("No bundle header", &*MBBI);
|
2015-10-10 03:40:45 +08:00
|
|
|
visitMachineInstrBefore(&*MBBI);
|
2015-05-01 03:35:41 +08:00
|
|
|
for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
|
|
|
|
const MachineInstr &MI = *MBBI;
|
|
|
|
const MachineOperand &Op = MI.getOperand(I);
|
|
|
|
if (Op.getParent() != &MI) {
|
2015-05-01 07:20:56 +08:00
|
|
|
// Make sure to use correct addOperand / RemoveOperand / ChangeTo
|
2015-05-01 03:35:41 +08:00
|
|
|
// functions when replacing operands of a MachineInstr.
|
|
|
|
report("Instruction has operand with wrong parent set", &MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
visitMachineOperand(&Op, I);
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:40:45 +08:00
|
|
|
visitMachineInstrAfter(&*MBBI);
|
2012-12-19 06:55:07 +08:00
|
|
|
|
|
|
|
// Was this the last bundled instruction?
|
|
|
|
InBundle = MBBI->isBundledWithSucc();
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2012-06-07 06:34:30 +08:00
|
|
|
if (CurBundle)
|
|
|
|
visitMachineBundleAfter(CurBundle);
|
2012-12-19 06:55:07 +08:00
|
|
|
if (InBundle)
|
|
|
|
report("BundledSucc flag set on last instruction in block", &MFI->back());
|
2015-10-10 03:40:45 +08:00
|
|
|
visitMachineBasicBlockAfter(&*MFI);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
visitMachineFunctionAfter();
|
|
|
|
|
2009-08-08 23:34:50 +08:00
|
|
|
// Clean up.
|
|
|
|
regsLive.clear();
|
|
|
|
regsDefined.clear();
|
|
|
|
regsDead.clear();
|
|
|
|
regsKilled.clear();
|
2012-02-28 09:42:41 +08:00
|
|
|
regMasks.clear();
|
2009-08-08 23:34:50 +08:00
|
|
|
regsLiveInButUnused.clear();
|
|
|
|
MBBInfoMap.clear();
|
|
|
|
|
2016-02-16 03:25:31 +08:00
|
|
|
return foundErrors;
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-08-23 09:03:30 +08:00
|
|
|
void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
|
2009-05-16 08:33:53 +08:00
|
|
|
assert(MF);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << '\n';
|
2010-12-18 08:06:56 +08:00
|
|
|
if (!foundErrors++) {
|
|
|
|
if (Banner)
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "# " << Banner << '\n';
|
2015-11-10 07:59:23 +08:00
|
|
|
if (LiveInts != nullptr)
|
|
|
|
LiveInts->print(errs());
|
|
|
|
else
|
|
|
|
MF->print(errs(), Indexes);
|
2010-12-18 08:06:56 +08:00
|
|
|
}
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "*** Bad machine code: " << msg << " ***\n"
|
2012-08-22 14:07:19 +08:00
|
|
|
<< "- function: " << MF->getName() << "\n";
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
|
2009-05-16 08:33:53 +08:00
|
|
|
assert(MBB);
|
|
|
|
report(msg, MBB->getParent());
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "- basic block: BB#" << MBB->getNumber()
|
2012-08-02 22:31:49 +08:00
|
|
|
<< ' ' << MBB->getName()
|
2012-09-06 06:26:57 +08:00
|
|
|
<< " (" << (const void*)MBB << ')';
|
2010-10-27 04:21:46 +08:00
|
|
|
if (Indexes)
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << " [" << Indexes->getMBBStartIdx(MBB)
|
2010-10-27 04:21:46 +08:00
|
|
|
<< ';' << Indexes->getMBBEndIdx(MBB) << ')';
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << '\n';
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
|
2009-05-16 08:33:53 +08:00
|
|
|
assert(MI);
|
|
|
|
report(msg, MI->getParent());
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "- instruction: ";
|
2016-02-27 14:40:41 +08:00
|
|
|
if (Indexes && Indexes->hasIndex(*MI))
|
|
|
|
errs() << Indexes->getInstructionIndex(*MI) << '\t';
|
2015-11-10 07:59:25 +08:00
|
|
|
MI->print(errs(), /*SkipOpers=*/true);
|
2015-11-10 07:59:29 +08:00
|
|
|
errs() << '\n';
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::report(const char *msg,
|
|
|
|
const MachineOperand *MO, unsigned MONum) {
|
2009-05-16 08:33:53 +08:00
|
|
|
assert(MO);
|
|
|
|
report(msg, MO->getParent());
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "- operand " << MONum << ": ";
|
2015-02-27 08:11:34 +08:00
|
|
|
MO->print(errs(), TRI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "\n";
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 10:44:25 +08:00
|
|
|
void MachineVerifier::report_context(SlotIndex Pos) const {
|
|
|
|
errs() << "- at: " << Pos << '\n';
|
|
|
|
}
|
|
|
|
|
2015-11-10 07:59:33 +08:00
|
|
|
void MachineVerifier::report_context(const LiveInterval &LI) const {
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "- interval: " << LI << '\n';
|
2012-08-02 22:31:49 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 03:39:01 +08:00
|
|
|
void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit,
|
2015-11-10 07:59:33 +08:00
|
|
|
LaneBitmask LaneMask) const {
|
2016-02-02 10:44:25 +08:00
|
|
|
report_context_liverange(LR);
|
2016-07-26 03:39:01 +08:00
|
|
|
report_context_vreg_regunit(VRegUnit);
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any())
|
2016-02-03 04:04:51 +08:00
|
|
|
report_context_lanemask(LaneMask);
|
2013-10-11 05:28:54 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 07:59:33 +08:00
|
|
|
void MachineVerifier::report_context(const LiveRange::Segment &S) const {
|
|
|
|
errs() << "- segment: " << S << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineVerifier::report_context(const VNInfo &VNI) const {
|
|
|
|
errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
|
2013-10-11 05:28:54 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 10:44:25 +08:00
|
|
|
void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
|
|
|
|
errs() << "- liverange: " << LR << '\n';
|
|
|
|
}
|
|
|
|
|
2016-05-12 05:31:39 +08:00
|
|
|
void MachineVerifier::report_context_vreg(unsigned VReg) const {
|
|
|
|
errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:04:51 +08:00
|
|
|
void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
|
2016-05-12 05:31:39 +08:00
|
|
|
report_context_vreg(VRegOrUnit);
|
2016-02-03 04:04:51 +08:00
|
|
|
} else {
|
|
|
|
errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
|
|
|
|
errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
|
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
|
2009-05-16 08:33:53 +08:00
|
|
|
BBInfo &MInfo = MBBInfoMap[MBB];
|
|
|
|
if (!MInfo.reachable) {
|
|
|
|
MInfo.reachable = true;
|
|
|
|
for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
|
|
|
|
SuE = MBB->succ_end(); SuI != SuE; ++SuI)
|
|
|
|
markReachable(*SuI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::visitMachineFunctionBefore() {
|
2011-01-13 05:27:48 +08:00
|
|
|
lastIndex = SlotIndex();
|
2012-10-16 05:57:41 +08:00
|
|
|
regsReserved = MRI->getReservedRegs();
|
2009-08-05 03:18:01 +08:00
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
markReachable(&MF->front());
|
2012-08-21 04:52:06 +08:00
|
|
|
|
|
|
|
// Build a set of the basic blocks in the function.
|
|
|
|
FunctionBlocks.clear();
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
FunctionBlocks.insert(&MBB);
|
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
|
|
|
|
|
|
|
MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
|
|
|
|
if (MInfo.Preds.size() != MBB.pred_size())
|
|
|
|
report("MBB has duplicate entries in its predecessor list.", &MBB);
|
|
|
|
|
|
|
|
MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
|
|
|
|
if (MInfo.Succs.size() != MBB.succ_size())
|
|
|
|
report("MBB has duplicate entries in its successor list.", &MBB);
|
2012-08-21 04:52:06 +08:00
|
|
|
}
|
2013-04-20 05:40:57 +08:00
|
|
|
|
|
|
|
// Check that the register use lists are sane.
|
|
|
|
MRI->verifyUseLists();
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
verifyStackFrame();
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 05:55:54 +08:00
|
|
|
// Does iterator point to a and b as the first two elements?
|
2010-04-16 01:08:50 +08:00
|
|
|
static bool matchPair(MachineBasicBlock::const_succ_iterator i,
|
|
|
|
const MachineBasicBlock *a, const MachineBasicBlock *b) {
|
2009-11-14 05:55:54 +08:00
|
|
|
if (*i == a)
|
|
|
|
return *++i == b;
|
|
|
|
if (*i == b)
|
|
|
|
return *++i == a;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
|
2014-04-14 08:51:57 +08:00
|
|
|
FirstTerminator = nullptr;
|
2011-09-24 06:45:39 +08:00
|
|
|
|
2016-08-24 09:32:41 +08:00
|
|
|
if (!MF->getProperties().hasProperty(
|
2017-01-06 04:01:19 +08:00
|
|
|
MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
|
2012-02-15 03:17:48 +08:00
|
|
|
// If this block has allocatable physical registers live-in, check that
|
|
|
|
// it is an entry block or landing pad.
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : MBB->liveins()) {
|
|
|
|
if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
|
2016-02-22 04:39:50 +08:00
|
|
|
MBB->getIterator() != MBB->getParent()->begin()) {
|
2012-02-15 03:17:48 +08:00
|
|
|
report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 02:47:06 +08:00
|
|
|
// Count the number of landing pad successors.
|
2010-12-20 12:19:48 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
|
2010-10-22 02:47:06 +08:00
|
|
|
for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
|
2010-12-20 12:19:48 +08:00
|
|
|
E = MBB->succ_end(); I != E; ++I) {
|
2015-08-28 07:27:47 +08:00
|
|
|
if ((*I)->isEHPad())
|
2010-12-20 12:19:48 +08:00
|
|
|
LandingPadSuccs.insert(*I);
|
2012-08-21 04:52:06 +08:00
|
|
|
if (!FunctionBlocks.count(*I))
|
|
|
|
report("MBB has successor that isn't part of the function.", MBB);
|
|
|
|
if (!MBBInfoMap[*I].Preds.count(MBB)) {
|
|
|
|
report("Inconsistent CFG", MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "MBB is not in the predecessor list of the successor BB#"
|
2012-08-21 04:52:06 +08:00
|
|
|
<< (*I)->getNumber() << ".\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the predecessor list.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
|
|
|
|
E = MBB->pred_end(); I != E; ++I) {
|
|
|
|
if (!FunctionBlocks.count(*I))
|
|
|
|
report("MBB has predecessor that isn't part of the function.", MBB);
|
|
|
|
if (!MBBInfoMap[*I].Succs.count(MBB)) {
|
|
|
|
report("Inconsistent CFG", MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "MBB is not in the successor list of the predecessor BB#"
|
2012-08-21 04:52:06 +08:00
|
|
|
<< (*I)->getNumber() << ".\n";
|
|
|
|
}
|
2010-12-20 12:19:48 +08:00
|
|
|
}
|
2011-05-05 06:54:05 +08:00
|
|
|
|
|
|
|
const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
|
|
|
|
const BasicBlock *BB = MBB->getBasicBlock();
|
2015-11-10 05:04:00 +08:00
|
|
|
const Function *Fn = MF->getFunction();
|
2011-05-05 06:54:05 +08:00
|
|
|
if (LandingPadSuccs.size() > 1 &&
|
|
|
|
!(AsmInfo &&
|
|
|
|
AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
|
2015-11-10 05:04:00 +08:00
|
|
|
BB && isa<SwitchInst>(BB->getTerminator())) &&
|
|
|
|
!isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
|
2010-10-22 02:47:06 +08:00
|
|
|
report("MBB has more than one landing pad successor", MBB);
|
|
|
|
|
2009-08-27 10:43:49 +08:00
|
|
|
// Call AnalyzeBranch. If it succeeds, there several more conditions to check.
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2009-08-27 10:43:49 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 22:41:04 +08:00
|
|
|
if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
|
|
|
|
Cond)) {
|
2009-08-27 10:43:49 +08:00
|
|
|
// Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
|
|
|
|
// check whether its answers match up with reality.
|
|
|
|
if (!TBB && !FBB) {
|
|
|
|
// Block falls through to its successor.
|
2015-10-10 03:40:45 +08:00
|
|
|
MachineFunction::const_iterator MBBI = MBB->getIterator();
|
2009-08-27 10:43:49 +08:00
|
|
|
++MBBI;
|
|
|
|
if (MBBI == MF->end()) {
|
2009-08-28 02:14:26 +08:00
|
|
|
// It's possible that the block legitimately ends with a noreturn
|
|
|
|
// call or an unreachable, in which case it won't actually fall
|
|
|
|
// out the bottom of the function.
|
2010-12-20 12:19:48 +08:00
|
|
|
} else if (MBB->succ_size() == LandingPadSuccs.size()) {
|
2009-08-28 02:14:26 +08:00
|
|
|
// It's possible that the block legitimately ends with a noreturn
|
|
|
|
// call or an unreachable, in which case it won't actuall fall
|
|
|
|
// out of the block.
|
2010-12-20 12:19:48 +08:00
|
|
|
} else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional fall-through but doesn't have "
|
|
|
|
"exactly one CFG successor!", MBB);
|
2015-10-10 03:40:45 +08:00
|
|
|
} else if (!MBB->isSuccessor(&*MBBI)) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional fall-through but its successor "
|
|
|
|
"differs from its CFG successor!", MBB);
|
|
|
|
}
|
2014-05-24 21:31:10 +08:00
|
|
|
if (!MBB->empty() && MBB->back().isBarrier() &&
|
2016-02-23 10:46:52 +08:00
|
|
|
!TII->isPredicated(MBB->back())) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional fall-through but ends with a "
|
|
|
|
"barrier instruction!", MBB);
|
|
|
|
}
|
|
|
|
if (!Cond.empty()) {
|
|
|
|
report("MBB exits via unconditional fall-through but has a condition!",
|
|
|
|
MBB);
|
|
|
|
}
|
|
|
|
} else if (TBB && !FBB && Cond.empty()) {
|
|
|
|
// Block unconditionally branches somewhere.
|
2014-12-02 02:43:53 +08:00
|
|
|
// If the block has exactly one successor, that happens to be a
|
|
|
|
// landingpad, accept it as valid control flow.
|
|
|
|
if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
|
|
|
|
(MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
|
|
|
|
*MBB->succ_begin() != *LandingPadSuccs.begin())) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional branch but doesn't have "
|
|
|
|
"exactly one CFG successor!", MBB);
|
2010-10-22 02:47:06 +08:00
|
|
|
} else if (!MBB->isSuccessor(TBB)) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional branch but the CFG "
|
|
|
|
"successor doesn't match the actual successor!", MBB);
|
|
|
|
}
|
|
|
|
if (MBB->empty()) {
|
|
|
|
report("MBB exits via unconditional branch but doesn't contain "
|
|
|
|
"any instructions!", MBB);
|
2014-05-24 21:31:10 +08:00
|
|
|
} else if (!MBB->back().isBarrier()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional branch but doesn't end with a "
|
|
|
|
"barrier instruction!", MBB);
|
2014-05-24 21:31:10 +08:00
|
|
|
} else if (!MBB->back().isTerminator()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via unconditional branch but the branch isn't a "
|
|
|
|
"terminator instruction!", MBB);
|
|
|
|
}
|
|
|
|
} else if (TBB && !FBB && !Cond.empty()) {
|
|
|
|
// Block conditionally branches somewhere, otherwise falls through.
|
2015-10-10 03:40:45 +08:00
|
|
|
MachineFunction::const_iterator MBBI = MBB->getIterator();
|
2009-08-27 10:43:49 +08:00
|
|
|
++MBBI;
|
|
|
|
if (MBBI == MF->end()) {
|
|
|
|
report("MBB conditionally falls through out of function!", MBB);
|
2012-12-20 06:13:01 +08:00
|
|
|
} else if (MBB->succ_size() == 1) {
|
2012-08-21 05:39:52 +08:00
|
|
|
// A conditional branch with only one successor is weird, but allowed.
|
|
|
|
if (&*MBBI != TBB)
|
|
|
|
report("MBB exits via conditional branch/fall-through but only has "
|
|
|
|
"one CFG successor!", MBB);
|
|
|
|
else if (TBB != *MBB->succ_begin())
|
|
|
|
report("MBB exits via conditional branch/fall-through but the CFG "
|
|
|
|
"successor don't match the actual successor!", MBB);
|
|
|
|
} else if (MBB->succ_size() != 2) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/fall-through but doesn't have "
|
|
|
|
"exactly two CFG successors!", MBB);
|
2015-10-10 03:40:45 +08:00
|
|
|
} else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/fall-through but the CFG "
|
|
|
|
"successors don't match the actual successors!", MBB);
|
|
|
|
}
|
|
|
|
if (MBB->empty()) {
|
|
|
|
report("MBB exits via conditional branch/fall-through but doesn't "
|
|
|
|
"contain any instructions!", MBB);
|
2014-05-24 21:31:10 +08:00
|
|
|
} else if (MBB->back().isBarrier()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/fall-through but ends with a "
|
|
|
|
"barrier instruction!", MBB);
|
2014-05-24 21:31:10 +08:00
|
|
|
} else if (!MBB->back().isTerminator()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/fall-through but the branch "
|
|
|
|
"isn't a terminator instruction!", MBB);
|
|
|
|
}
|
|
|
|
} else if (TBB && FBB) {
|
|
|
|
// Block conditionally branches somewhere, otherwise branches
|
|
|
|
// somewhere else.
|
2012-08-21 05:39:52 +08:00
|
|
|
if (MBB->succ_size() == 1) {
|
|
|
|
// A conditional branch with only one successor is weird, but allowed.
|
|
|
|
if (FBB != TBB)
|
|
|
|
report("MBB exits via conditional branch/branch through but only has "
|
|
|
|
"one CFG successor!", MBB);
|
|
|
|
else if (TBB != *MBB->succ_begin())
|
|
|
|
report("MBB exits via conditional branch/branch through but the CFG "
|
|
|
|
"successor don't match the actual successor!", MBB);
|
|
|
|
} else if (MBB->succ_size() != 2) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/branch but doesn't have "
|
|
|
|
"exactly two CFG successors!", MBB);
|
2009-11-14 05:55:54 +08:00
|
|
|
} else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/branch but the CFG "
|
|
|
|
"successors don't match the actual successors!", MBB);
|
|
|
|
}
|
|
|
|
if (MBB->empty()) {
|
|
|
|
report("MBB exits via conditional branch/branch but doesn't "
|
|
|
|
"contain any instructions!", MBB);
|
2014-05-24 21:13:17 +08:00
|
|
|
} else if (!MBB->back().isBarrier()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/branch but doesn't end with a "
|
|
|
|
"barrier instruction!", MBB);
|
2014-05-24 21:13:17 +08:00
|
|
|
} else if (!MBB->back().isTerminator()) {
|
2009-08-27 10:43:49 +08:00
|
|
|
report("MBB exits via conditional branch/branch but the branch "
|
|
|
|
"isn't a terminator instruction!", MBB);
|
|
|
|
}
|
|
|
|
if (Cond.empty()) {
|
|
|
|
report("MBB exits via conditinal branch/branch but there's no "
|
|
|
|
"condition!", MBB);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
report("AnalyzeBranch returned invalid data!", MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
regsLive.clear();
|
2017-01-06 04:01:19 +08:00
|
|
|
if (MRI->tracksLiveness()) {
|
|
|
|
for (const auto &LI : MBB->liveins()) {
|
|
|
|
if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
|
|
|
|
report("MBB live-in list contains non-physical register", MBB);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
|
|
|
|
SubRegs.isValid(); ++SubRegs)
|
|
|
|
regsLive.insert(*SubRegs);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-08 21:19:25 +08:00
|
|
|
regsLiveInButUnused = regsLive;
|
2009-08-14 00:19:51 +08:00
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
const MachineFrameInfo &MFI = MF->getFrameInfo();
|
|
|
|
BitVector PR = MFI.getPristineRegs(*MF);
|
2009-08-14 00:19:51 +08:00
|
|
|
for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
|
2013-05-23 07:17:36 +08:00
|
|
|
for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
|
|
|
|
SubRegs.isValid(); ++SubRegs)
|
2012-06-02 07:28:30 +08:00
|
|
|
regsLive.insert(*SubRegs);
|
2009-08-14 00:19:51 +08:00
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
regsKilled.clear();
|
|
|
|
regsDefined.clear();
|
2011-01-13 05:27:48 +08:00
|
|
|
|
|
|
|
if (Indexes)
|
|
|
|
lastIndex = Indexes->getMBBStartIdx(MBB);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 06:34:30 +08:00
|
|
|
// This function gets called for all bundle headers, including normal
|
|
|
|
// stand-alone unbundled instructions.
|
|
|
|
void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
|
2016-02-27 14:40:41 +08:00
|
|
|
if (Indexes && Indexes->hasIndex(*MI)) {
|
|
|
|
SlotIndex idx = Indexes->getInstructionIndex(*MI);
|
2012-06-07 06:34:30 +08:00
|
|
|
if (!(idx > lastIndex)) {
|
|
|
|
report("Instruction index out of order", MI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Last instruction was at " << lastIndex << '\n';
|
2012-06-07 06:34:30 +08:00
|
|
|
}
|
|
|
|
lastIndex = idx;
|
|
|
|
}
|
2012-06-08 01:41:39 +08:00
|
|
|
|
|
|
|
// Ensure non-terminators don't follow terminators.
|
|
|
|
// Ignore predicated terminators formed by if conversion.
|
|
|
|
// FIXME: If conversion shouldn't need to violate this rule.
|
2016-02-23 10:46:52 +08:00
|
|
|
if (MI->isTerminator() && !TII->isPredicated(*MI)) {
|
2012-06-08 01:41:39 +08:00
|
|
|
if (!FirstTerminator)
|
|
|
|
FirstTerminator = MI;
|
|
|
|
} else if (FirstTerminator) {
|
|
|
|
report("Non-terminator instruction after the first terminator", MI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "First terminator was:\t" << *FirstTerminator;
|
2012-06-08 01:41:39 +08:00
|
|
|
}
|
2012-06-07 06:34:30 +08:00
|
|
|
}
|
|
|
|
|
2012-08-30 02:11:05 +08:00
|
|
|
// The operands on an INLINEASM instruction must follow a template.
|
|
|
|
// Verify that the flag operands make sense.
|
|
|
|
void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
|
|
|
|
// The first two operands on INLINEASM are the asm string and global flags.
|
|
|
|
if (MI->getNumOperands() < 2) {
|
|
|
|
report("Too few operands on inline asm", MI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!MI->getOperand(0).isSymbol())
|
|
|
|
report("Asm string must be an external symbol", MI);
|
|
|
|
if (!MI->getOperand(1).isImm())
|
|
|
|
report("Asm flags must be an immediate", MI);
|
2012-10-31 03:11:54 +08:00
|
|
|
// Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
|
2016-06-23 02:51:08 +08:00
|
|
|
// Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
|
|
|
|
// and Extra_IsConvergent = 32.
|
|
|
|
if (!isUInt<6>(MI->getOperand(1).getImm()))
|
2012-08-30 02:11:05 +08:00
|
|
|
report("Unknown asm flags", &MI->getOperand(1), 1);
|
|
|
|
|
2015-03-16 17:53:42 +08:00
|
|
|
static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
|
2012-08-30 02:11:05 +08:00
|
|
|
|
|
|
|
unsigned OpNo = InlineAsm::MIOp_FirstOperand;
|
|
|
|
unsigned NumOps;
|
|
|
|
for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
// There may be implicit ops after the fixed operands.
|
|
|
|
if (!MO.isImm())
|
|
|
|
break;
|
|
|
|
NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2009-10-08 01:36:00 +08:00
|
|
|
|
2012-08-30 02:11:05 +08:00
|
|
|
if (OpNo > MI->getNumOperands())
|
|
|
|
report("Missing operands in last group", MI);
|
|
|
|
|
|
|
|
// An optional MDNode follows the groups.
|
|
|
|
if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
|
|
|
|
++OpNo;
|
|
|
|
|
|
|
|
// All trailing operands must be implicit registers.
|
|
|
|
for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
if (!MO.isReg() || !MO.isImplicit())
|
|
|
|
report("Expected implicit register after groups", &MO, OpNo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
|
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
if (MI->getNumOperands() < MCID.getNumOperands()) {
|
|
|
|
report("Too few operands", MI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << MCID.getNumOperands() << " operands expected, but "
|
2013-11-16 06:18:19 +08:00
|
|
|
<< MI->getNumOperands() << " given.\n";
|
2012-08-30 02:11:05 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 05:19:49 +08:00
|
|
|
if (MI->isPHI() && MF->getProperties().hasProperty(
|
|
|
|
MachineFunctionProperties::Property::NoPHIs))
|
|
|
|
report("Found PHI instruction with NoPHIs property set", MI);
|
|
|
|
|
2012-08-30 02:11:05 +08:00
|
|
|
// Check the tied operands.
|
|
|
|
if (MI->isInlineAsm())
|
|
|
|
verifyInlineAsm(MI);
|
2012-08-29 08:38:03 +08:00
|
|
|
|
2009-10-08 01:36:00 +08:00
|
|
|
// Check the MachineMemOperands for basic consistency.
|
|
|
|
for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
|
|
|
|
E = MI->memoperands_end(); I != E; ++I) {
|
2011-12-07 15:15:52 +08:00
|
|
|
if ((*I)->isLoad() && !MI->mayLoad())
|
2009-10-08 01:36:00 +08:00
|
|
|
report("Missing mayLoad flag", MI);
|
2011-12-07 15:15:52 +08:00
|
|
|
if ((*I)->isStore() && !MI->mayStore())
|
2009-10-08 01:36:00 +08:00
|
|
|
report("Missing mayStore flag", MI);
|
|
|
|
}
|
2010-08-06 06:32:21 +08:00
|
|
|
|
|
|
|
// Debug values must not have a slot index.
|
2012-02-28 02:24:30 +08:00
|
|
|
// Other instructions must have one, unless they are inside a bundle.
|
2010-08-06 06:32:21 +08:00
|
|
|
if (LiveInts) {
|
2016-02-27 14:40:41 +08:00
|
|
|
bool mapped = !LiveInts->isNotInMIMap(*MI);
|
2010-08-06 06:32:21 +08:00
|
|
|
if (MI->isDebugValue()) {
|
|
|
|
if (mapped)
|
|
|
|
report("Debug instruction has a slot index", MI);
|
2012-02-28 02:24:30 +08:00
|
|
|
} else if (MI->isInsideBundle()) {
|
|
|
|
if (mapped)
|
|
|
|
report("Instruction inside bundle has a slot index", MI);
|
2010-08-06 06:32:21 +08:00
|
|
|
} else {
|
|
|
|
if (!mapped)
|
|
|
|
report("Missing slot index", MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 00:58:27 +08:00
|
|
|
// Check types.
|
|
|
|
if (isPreISelGenericOpcode(MCID.getOpcode())) {
|
2016-08-03 00:49:22 +08:00
|
|
|
if (isFunctionSelected)
|
|
|
|
report("Unexpected generic instruction in a Selected function", MI);
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
// Generic instructions specify equality constraints between some
|
|
|
|
// of their operands. Make sure these are consistent.
|
|
|
|
SmallVector<LLT, 4> Types;
|
|
|
|
for (unsigned i = 0; i < MCID.getNumOperands(); ++i) {
|
|
|
|
if (!MCID.OpInfo[i].isGenericType())
|
|
|
|
continue;
|
|
|
|
size_t TypeIdx = MCID.OpInfo[i].getGenericTypeIndex();
|
|
|
|
Types.resize(std::max(TypeIdx + 1, Types.size()));
|
|
|
|
|
|
|
|
LLT OpTy = MRI->getType(MI->getOperand(i).getReg());
|
|
|
|
if (Types[TypeIdx].isValid() && Types[TypeIdx] != OpTy)
|
|
|
|
report("type mismatch in generic instruction", MI);
|
|
|
|
Types[TypeIdx] = OpTy;
|
|
|
|
}
|
2016-07-29 00:58:27 +08:00
|
|
|
}
|
|
|
|
|
2016-08-31 02:52:46 +08:00
|
|
|
// Generic opcodes must not have physical register operands.
|
2016-09-09 19:47:31 +08:00
|
|
|
if (isPreISelGenericOpcode(MCID.getOpcode())) {
|
2016-08-31 02:52:46 +08:00
|
|
|
for (auto &Op : MI->operands()) {
|
|
|
|
if (Op.isReg() && TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
|
|
|
|
report("Generic instruction cannot have physical register", MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 10:20:46 +08:00
|
|
|
StringRef ErrorInfo;
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!TII->verifyInstruction(*MI, ErrorInfo))
|
2011-09-21 10:20:46 +08:00
|
|
|
report(ErrorInfo.data(), MI);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-05 02:18:39 +08:00
|
|
|
MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
|
2009-05-16 08:33:53 +08:00
|
|
|
const MachineInstr *MI = MO->getParent();
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
2015-08-11 05:47:36 +08:00
|
|
|
unsigned NumDefs = MCID.getNumDefs();
|
|
|
|
if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
|
|
|
|
NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
|
2009-05-16 15:25:20 +08:00
|
|
|
|
2011-06-29 03:10:37 +08:00
|
|
|
// The first MCID.NumDefs operands must be explicit register defines
|
2015-08-11 05:47:36 +08:00
|
|
|
if (MONum < NumDefs) {
|
2012-08-15 09:39:31 +08:00
|
|
|
const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
|
2009-05-16 15:25:20 +08:00
|
|
|
if (!MO->isReg())
|
|
|
|
report("Explicit definition must be a register", MO, MONum);
|
2012-05-30 03:40:44 +08:00
|
|
|
else if (!MO->isDef() && !MCOI.isOptionalDef())
|
2009-05-16 15:25:20 +08:00
|
|
|
report("Explicit definition marked as use", MO, MONum);
|
|
|
|
else if (MO->isImplicit())
|
|
|
|
report("Explicit definition marked as implicit", MO, MONum);
|
2011-06-29 03:10:37 +08:00
|
|
|
} else if (MONum < MCID.getNumOperands()) {
|
2012-08-15 09:39:31 +08:00
|
|
|
const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
|
2010-11-17 08:55:36 +08:00
|
|
|
// Don't check if it's the last operand in a variadic instruction. See,
|
|
|
|
// e.g., LDM_RET in the arm back end.
|
2011-06-29 03:10:37 +08:00
|
|
|
if (MO->isReg() &&
|
2011-12-07 15:15:52 +08:00
|
|
|
!(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
|
2011-06-29 03:10:37 +08:00
|
|
|
if (MO->isDef() && !MCOI.isOptionalDef())
|
2013-10-05 00:53:00 +08:00
|
|
|
report("Explicit operand marked as def", MO, MONum);
|
2009-09-24 04:57:55 +08:00
|
|
|
if (MO->isImplicit())
|
|
|
|
report("Explicit operand marked as implicit", MO, MONum);
|
|
|
|
}
|
2012-08-29 08:38:03 +08:00
|
|
|
|
2012-09-05 02:38:28 +08:00
|
|
|
int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
|
|
|
|
if (TiedTo != -1) {
|
2012-08-29 08:38:03 +08:00
|
|
|
if (!MO->isReg())
|
|
|
|
report("Tied use must be a register", MO, MONum);
|
|
|
|
else if (!MO->isTied())
|
|
|
|
report("Operand should be tied", MO, MONum);
|
2012-09-05 02:38:28 +08:00
|
|
|
else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
|
|
|
|
report("Tied def doesn't match MCInstrDesc", MO, MONum);
|
2012-08-29 08:38:03 +08:00
|
|
|
} else if (MO->isReg() && MO->isTied())
|
|
|
|
report("Explicit operand should not be tied", MO, MONum);
|
2009-09-24 04:57:55 +08:00
|
|
|
} else {
|
2009-12-23 05:48:20 +08:00
|
|
|
// ARM adds %reg0 operands to indicate predicates. We'll allow that.
|
2011-12-07 15:15:52 +08:00
|
|
|
if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
|
2009-09-24 04:57:55 +08:00
|
|
|
report("Extra explicit operand on non-variadic instruction", MO, MONum);
|
2009-05-16 15:25:20 +08:00
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
switch (MO->getType()) {
|
|
|
|
case MachineOperand::MO_Register: {
|
|
|
|
const unsigned Reg = MO->getReg();
|
|
|
|
if (!Reg)
|
|
|
|
return;
|
2012-03-29 04:47:35 +08:00
|
|
|
if (MRI->tracksLiveness() && !MI->isDebugValue())
|
|
|
|
checkLiveness(MO, MONum);
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2012-09-05 02:38:28 +08:00
|
|
|
// Verify the consistency of tied operands.
|
|
|
|
if (MO->isTied()) {
|
|
|
|
unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
|
|
|
|
const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
|
|
|
|
if (!OtherMO.isReg())
|
|
|
|
report("Must be tied to a register", MO, MONum);
|
|
|
|
if (!OtherMO.isTied())
|
|
|
|
report("Missing tie flags on tied operand", MO, MONum);
|
|
|
|
if (MI->findTiedOperandIdx(OtherIdx) != MONum)
|
|
|
|
report("Inconsistent tie links", MO, MONum);
|
|
|
|
if (MONum < MCID.getNumDefs()) {
|
|
|
|
if (OtherIdx < MCID.getNumOperands()) {
|
|
|
|
if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
|
|
|
|
report("Explicit def tied to explicit use without tie constraint",
|
|
|
|
MO, MONum);
|
|
|
|
} else {
|
|
|
|
if (!OtherMO.isImplicit())
|
|
|
|
report("Explicit def should be tied to implicit use", MO, MONum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 00:49:11 +08:00
|
|
|
// Verify two-address constraints after leaving SSA form.
|
|
|
|
unsigned DefIdx;
|
|
|
|
if (!MRI->isSSA() && MO->isUse() &&
|
|
|
|
MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
|
|
|
|
Reg != MI->getOperand(DefIdx).getReg())
|
|
|
|
report("Two-address instruction operands must be identical", MO, MONum);
|
2009-05-16 08:33:53 +08:00
|
|
|
|
|
|
|
// Check register classes.
|
2011-06-29 03:10:37 +08:00
|
|
|
if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
|
2009-05-16 08:33:53 +08:00
|
|
|
unsigned SubIdx = MO->getSubReg();
|
|
|
|
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
if (SubIdx) {
|
2011-10-06 06:12:57 +08:00
|
|
|
report("Illegal subregister index for physical register", MO, MONum);
|
|
|
|
return;
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2012-05-08 06:10:26 +08:00
|
|
|
if (const TargetRegisterClass *DRC =
|
|
|
|
TII->getRegClass(MCID, MONum, TRI, *MF)) {
|
2011-10-06 06:12:57 +08:00
|
|
|
if (!DRC->contains(Reg)) {
|
2009-05-16 08:33:53 +08:00
|
|
|
report("Illegal physical register for instruction", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << TRI->getName(Reg) << " is not a "
|
2014-11-17 13:50:14 +08:00
|
|
|
<< TRI->getRegClassName(DRC) << " register.\n";
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Virtual register.
|
2016-04-09 00:35:22 +08:00
|
|
|
const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
|
|
|
|
if (!RC) {
|
|
|
|
// This is a generic virtual register.
|
2016-08-03 00:49:22 +08:00
|
|
|
|
|
|
|
// If we're post-Select, we can't have gvregs anymore.
|
|
|
|
if (isFunctionSelected) {
|
|
|
|
report("Generic virtual register invalid in a Selected function",
|
|
|
|
MO, MONum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-23 06:50:34 +08:00
|
|
|
// The gvreg must have a type and it must not have a SubIdx.
|
2016-09-09 19:46:34 +08:00
|
|
|
LLT Ty = MRI->getType(Reg);
|
|
|
|
if (!Ty.isValid()) {
|
|
|
|
report("Generic virtual register must have a valid type", MO,
|
|
|
|
MONum);
|
2016-04-09 00:35:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-08-03 00:17:15 +08:00
|
|
|
|
2016-04-09 00:35:22 +08:00
|
|
|
const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
|
2016-08-03 00:17:15 +08:00
|
|
|
|
|
|
|
// If we're post-RegBankSelect, the gvreg must have a bank.
|
|
|
|
if (!RegBank && isFunctionRegBankSelected) {
|
|
|
|
report("Generic virtual register must have a bank in a "
|
|
|
|
"RegBankSelected function",
|
|
|
|
MO, MONum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the register fits into its register bank if any.
|
2016-09-15 18:09:59 +08:00
|
|
|
if (RegBank && Ty.isValid() &&
|
2016-09-09 19:46:34 +08:00
|
|
|
RegBank->getSize() < Ty.getSizeInBits()) {
|
2016-04-09 00:35:22 +08:00
|
|
|
report("Register bank is too small for virtual register", MO,
|
|
|
|
MONum);
|
|
|
|
errs() << "Register bank " << RegBank->getName() << " too small("
|
2016-09-09 19:46:34 +08:00
|
|
|
<< RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
|
|
|
|
<< "-bits\n";
|
2016-04-09 00:35:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SubIdx) {
|
2016-09-09 19:46:34 +08:00
|
|
|
report("Generic virtual register does not subregister index", MO,
|
|
|
|
MONum);
|
2016-04-09 00:35:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-12-23 05:56:39 +08:00
|
|
|
|
|
|
|
// If this is a target specific instruction and this operand
|
|
|
|
// has register class constraint, the virtual register must
|
|
|
|
// comply to it.
|
|
|
|
if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
|
|
|
|
TII->getRegClass(MCID, MONum, TRI, *MF)) {
|
|
|
|
report("Virtual register does not match instruction constraint", MO,
|
|
|
|
MONum);
|
|
|
|
errs() << "Expect register class "
|
|
|
|
<< TRI->getRegClassName(
|
|
|
|
TII->getRegClass(MCID, MONum, TRI, *MF))
|
|
|
|
<< " but got nothing\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 00:35:22 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-05-16 08:33:53 +08:00
|
|
|
if (SubIdx) {
|
2011-10-06 06:12:57 +08:00
|
|
|
const TargetRegisterClass *SRC =
|
|
|
|
TRI->getSubClassWithSubReg(RC, SubIdx);
|
2010-05-19 01:31:12 +08:00
|
|
|
if (!SRC) {
|
2009-05-16 08:33:53 +08:00
|
|
|
report("Invalid subregister index for virtual register", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Register class " << TRI->getRegClassName(RC)
|
2010-05-19 01:31:12 +08:00
|
|
|
<< " does not support subreg index " << SubIdx << "\n";
|
2009-05-16 08:33:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-10-06 06:12:57 +08:00
|
|
|
if (RC != SRC) {
|
|
|
|
report("Invalid register class for subregister index", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Register class " << TRI->getRegClassName(RC)
|
2011-10-06 06:12:57 +08:00
|
|
|
<< " does not fully support subreg index " << SubIdx << "\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2012-05-08 06:10:26 +08:00
|
|
|
if (const TargetRegisterClass *DRC =
|
|
|
|
TII->getRegClass(MCID, MONum, TRI, *MF)) {
|
2011-10-06 06:12:57 +08:00
|
|
|
if (SubIdx) {
|
|
|
|
const TargetRegisterClass *SuperRC =
|
2015-03-11 07:46:01 +08:00
|
|
|
TRI->getLargestLegalSuperClass(RC, *MF);
|
2011-10-06 06:12:57 +08:00
|
|
|
if (!SuperRC) {
|
|
|
|
report("No largest legal super class exists.", MO, MONum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
|
|
|
|
if (!DRC) {
|
|
|
|
report("No matching super-reg register class.", MO, MONum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-06-02 13:43:46 +08:00
|
|
|
if (!RC->hasSuperClassEq(DRC)) {
|
2009-05-16 08:33:53 +08:00
|
|
|
report("Illegal virtual register for instruction", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Expected a " << TRI->getRegClassName(DRC)
|
2014-11-17 13:50:14 +08:00
|
|
|
<< " register, but got a " << TRI->getRegClassName(RC)
|
|
|
|
<< " register\n";
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-09-21 15:19:08 +08:00
|
|
|
|
2012-02-28 09:42:41 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
regMasks.push_back(MO->getRegMask());
|
|
|
|
break;
|
|
|
|
|
2009-09-21 15:19:08 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2010-02-10 03:54:29 +08:00
|
|
|
if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
|
|
|
|
report("PHI operand is not in the CFG", MO, MONum);
|
2009-09-21 15:19:08 +08:00
|
|
|
break;
|
|
|
|
|
2010-11-02 03:49:52 +08:00
|
|
|
case MachineOperand::MO_FrameIndex:
|
|
|
|
if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
|
2016-02-27 14:40:41 +08:00
|
|
|
LiveInts && !LiveInts->isNotInMIMap(*MI)) {
|
2015-10-29 16:28:35 +08:00
|
|
|
int FI = MO->getIndex();
|
|
|
|
LiveInterval &LI = LiveStks->getInterval(FI);
|
2016-02-27 14:40:41 +08:00
|
|
|
SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
|
2015-10-21 15:39:47 +08:00
|
|
|
|
|
|
|
bool stores = MI->mayStore();
|
2015-10-29 16:28:35 +08:00
|
|
|
bool loads = MI->mayLoad();
|
|
|
|
// For a memory-to-memory move, we need to check if the frame
|
|
|
|
// index is used for storing or loading, by inspecting the
|
|
|
|
// memory operands.
|
|
|
|
if (stores && loads) {
|
|
|
|
for (auto *MMO : MI->memoperands()) {
|
|
|
|
const PseudoSourceValue *PSV = MMO->getPseudoValue();
|
|
|
|
if (PSV == nullptr) continue;
|
|
|
|
const FixedStackPseudoSourceValue *Value =
|
|
|
|
dyn_cast<FixedStackPseudoSourceValue>(PSV);
|
|
|
|
if (Value == nullptr) continue;
|
|
|
|
if (Value->getFrameIndex() != FI) continue;
|
|
|
|
|
|
|
|
if (MMO->isStore())
|
|
|
|
loads = false;
|
|
|
|
else
|
|
|
|
stores = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (loads == stores)
|
|
|
|
report("Missing fixed stack memoperand.", MI);
|
|
|
|
}
|
|
|
|
if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
|
2010-11-02 03:49:52 +08:00
|
|
|
report("Instruction loads from dead spill slot", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Live stack: " << LI << '\n';
|
2010-11-02 03:49:52 +08:00
|
|
|
}
|
2015-10-21 15:39:47 +08:00
|
|
|
if (stores && !LI.liveAt(Idx.getRegSlot())) {
|
2010-11-02 03:49:52 +08:00
|
|
|
report("Instruction stores to dead spill slot", MO, MONum);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Live stack: " << LI << '\n';
|
2010-11-02 03:49:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:04:51 +08:00
|
|
|
void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
|
|
|
|
unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
|
|
|
|
LaneBitmask LaneMask) {
|
|
|
|
LiveQueryResult LRQ = LR.Query(UseIdx);
|
|
|
|
// Check if we have a segment at the use, note however that we only need one
|
|
|
|
// live subregister range, the others may be dead.
|
2016-12-15 22:36:06 +08:00
|
|
|
if (!LRQ.valueIn() && LaneMask.none()) {
|
2016-02-03 04:04:51 +08:00
|
|
|
report("No live segment at use", MO, MONum);
|
|
|
|
report_context_liverange(LR);
|
|
|
|
report_context_vreg_regunit(VRegOrUnit);
|
|
|
|
report_context(UseIdx);
|
|
|
|
}
|
|
|
|
if (MO->isKill() && !LRQ.isKill()) {
|
|
|
|
report("Live range continues after kill flag", MO, MONum);
|
|
|
|
report_context_liverange(LR);
|
|
|
|
report_context_vreg_regunit(VRegOrUnit);
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any())
|
2016-02-03 04:04:51 +08:00
|
|
|
report_context_lanemask(LaneMask);
|
|
|
|
report_context(UseIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
|
|
|
|
unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
|
|
|
|
LaneBitmask LaneMask) {
|
|
|
|
if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
|
|
|
|
assert(VNI && "NULL valno is not allowed");
|
|
|
|
if (VNI->def != DefIdx) {
|
|
|
|
report("Inconsistent valno->def", MO, MONum);
|
|
|
|
report_context_liverange(LR);
|
|
|
|
report_context_vreg_regunit(VRegOrUnit);
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any())
|
2016-02-03 04:04:51 +08:00
|
|
|
report_context_lanemask(LaneMask);
|
|
|
|
report_context(*VNI);
|
|
|
|
report_context(DefIdx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
report("No live segment at def", MO, MONum);
|
|
|
|
report_context_liverange(LR);
|
|
|
|
report_context_vreg_regunit(VRegOrUnit);
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any())
|
2016-02-03 04:04:51 +08:00
|
|
|
report_context_lanemask(LaneMask);
|
|
|
|
report_context(DefIdx);
|
|
|
|
}
|
|
|
|
// Check that, if the dead def flag is present, LiveInts agree.
|
|
|
|
if (MO->isDead()) {
|
|
|
|
LiveQueryResult LRQ = LR.Query(DefIdx);
|
|
|
|
if (!LRQ.isDeadDef()) {
|
|
|
|
// In case of physregs we can have a non-dead definition on another
|
|
|
|
// operand.
|
|
|
|
bool otherDef = false;
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
|
|
|
|
const MachineInstr &MI = *MO->getParent();
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isDef() || MO.isDead())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
|
|
|
|
if (*Units == VRegOrUnit) {
|
|
|
|
otherDef = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!otherDef) {
|
|
|
|
report("Live range continues after dead def flag", MO, MONum);
|
|
|
|
report_context_liverange(LR);
|
|
|
|
report_context_vreg_regunit(VRegOrUnit);
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any())
|
2016-02-03 04:04:51 +08:00
|
|
|
report_context_lanemask(LaneMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 04:47:35 +08:00
|
|
|
void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
|
|
|
|
const MachineInstr *MI = MO->getParent();
|
|
|
|
const unsigned Reg = MO->getReg();
|
|
|
|
|
|
|
|
// Both use and def operands can read a register.
|
|
|
|
if (MO->readsReg()) {
|
|
|
|
regsLiveInButUnused.erase(Reg);
|
|
|
|
|
2012-07-26 00:49:11 +08:00
|
|
|
if (MO->isKill())
|
2012-03-29 04:47:35 +08:00
|
|
|
addRegWithSubRegs(regsKilled, Reg);
|
|
|
|
|
|
|
|
// Check that LiveVars knows this kill.
|
|
|
|
if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
|
|
|
|
MO->isKill()) {
|
|
|
|
LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
|
2016-08-12 06:21:41 +08:00
|
|
|
if (!is_contained(VI.Kills, MI))
|
2012-03-29 04:47:35 +08:00
|
|
|
report("Kill missing from LiveVariables", MO, MONum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check LiveInts liveness and kill.
|
2016-02-27 14:40:41 +08:00
|
|
|
if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
|
|
|
|
SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
|
2012-08-02 07:52:40 +08:00
|
|
|
// Check the cached regunit intervals.
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
|
|
|
|
for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
|
2016-02-03 04:04:51 +08:00
|
|
|
if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
|
|
|
|
checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
|
2012-03-29 04:47:35 +08:00
|
|
|
}
|
2012-08-02 07:52:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
|
|
|
if (LiveInts->hasInterval(Reg)) {
|
|
|
|
// This is a virtual register interval.
|
|
|
|
const LiveInterval &LI = LiveInts->getInterval(Reg);
|
2016-02-03 04:04:51 +08:00
|
|
|
checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
|
|
|
|
|
|
|
|
if (LI.hasSubRanges() && !MO->isDef()) {
|
|
|
|
unsigned SubRegIdx = MO->getSubReg();
|
|
|
|
LaneBitmask MOMask = SubRegIdx != 0
|
|
|
|
? TRI->getSubRegIndexLaneMask(SubRegIdx)
|
|
|
|
: MRI->getMaxLaneMaskForVReg(Reg);
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask LiveInMask;
|
2016-02-03 04:04:51 +08:00
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
2016-12-15 22:36:06 +08:00
|
|
|
if ((MOMask & SR.LaneMask).none())
|
2016-02-03 04:04:51 +08:00
|
|
|
continue;
|
|
|
|
checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
|
|
|
|
LiveQueryResult LRQ = SR.Query(UseIdx);
|
|
|
|
if (LRQ.valueIn())
|
|
|
|
LiveInMask |= SR.LaneMask;
|
|
|
|
}
|
|
|
|
// At least parts of the register has to be live at the use.
|
2016-12-15 22:36:06 +08:00
|
|
|
if ((LiveInMask & MOMask).none()) {
|
2016-02-03 04:04:51 +08:00
|
|
|
report("No live subrange at use", MO, MONum);
|
|
|
|
report_context(LI);
|
|
|
|
report_context(UseIdx);
|
|
|
|
}
|
2012-08-02 07:52:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
report("Virtual register has no live interval", MO, MONum);
|
2012-03-29 04:47:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use of a dead register.
|
|
|
|
if (!regsLive.count(Reg)) {
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
// Reserved registers may be used even when 'dead'.
|
2014-12-10 09:13:13 +08:00
|
|
|
bool Bad = !isReserved(Reg);
|
|
|
|
// We are fine if just any subregister has a defined value.
|
|
|
|
if (Bad) {
|
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
|
|
|
|
++SubRegs) {
|
|
|
|
if (regsLive.count(*SubRegs)) {
|
|
|
|
Bad = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 06:25:14 +08:00
|
|
|
// If there is an additional implicit-use of a super register we stop
|
|
|
|
// here. By definition we are fine if the super register is not
|
|
|
|
// (completely) dead, if the complete super register is dead we will
|
|
|
|
// get a report for its operand.
|
|
|
|
if (Bad) {
|
|
|
|
for (const MachineOperand &MOP : MI->uses()) {
|
|
|
|
if (!MOP.isReg())
|
|
|
|
continue;
|
|
|
|
if (!MOP.isImplicit())
|
|
|
|
continue;
|
|
|
|
for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
|
|
|
|
++SubRegs) {
|
|
|
|
if (*SubRegs == Reg) {
|
|
|
|
Bad = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 09:13:13 +08:00
|
|
|
if (Bad)
|
2012-03-29 04:47:35 +08:00
|
|
|
report("Using an undefined physical register", MO, MONum);
|
2012-07-20 07:40:38 +08:00
|
|
|
} else if (MRI->def_empty(Reg)) {
|
|
|
|
report("Reading virtual register without a def", MO, MONum);
|
2012-03-29 04:47:35 +08:00
|
|
|
} else {
|
|
|
|
BBInfo &MInfo = MBBInfoMap[MI->getParent()];
|
|
|
|
// We don't know which virtual registers are live in, so only complain
|
|
|
|
// if vreg was killed in this MBB. Otherwise keep track of vregs that
|
|
|
|
// must be live in. PHI instructions are handled separately.
|
|
|
|
if (MInfo.regsKilled.count(Reg))
|
|
|
|
report("Using a killed virtual register", MO, MONum);
|
|
|
|
else if (!MI->isPHI())
|
|
|
|
MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MO->isDef()) {
|
|
|
|
// Register defined.
|
|
|
|
// TODO: verify that earlyclobber ops are not used.
|
|
|
|
if (MO->isDead())
|
|
|
|
addRegWithSubRegs(regsDead, Reg);
|
|
|
|
else
|
|
|
|
addRegWithSubRegs(regsDefined, Reg);
|
|
|
|
|
|
|
|
// Verify SSA form.
|
|
|
|
if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
|
2014-03-02 20:27:27 +08:00
|
|
|
std::next(MRI->def_begin(Reg)) != MRI->def_end())
|
2012-03-29 04:47:35 +08:00
|
|
|
report("Multiple virtual register defs in SSA form", MO, MONum);
|
|
|
|
|
2013-10-11 05:28:43 +08:00
|
|
|
// Check LiveInts for a live segment, but only for virtual registers.
|
2016-02-27 14:40:41 +08:00
|
|
|
if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
|
|
|
|
SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
|
2012-06-23 06:23:58 +08:00
|
|
|
DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
|
2016-02-03 04:04:51 +08:00
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
|
|
|
if (LiveInts->hasInterval(Reg)) {
|
|
|
|
const LiveInterval &LI = LiveInts->getInterval(Reg);
|
|
|
|
checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
|
|
|
|
|
|
|
|
if (LI.hasSubRanges()) {
|
|
|
|
unsigned SubRegIdx = MO->getSubReg();
|
|
|
|
LaneBitmask MOMask = SubRegIdx != 0
|
|
|
|
? TRI->getSubRegIndexLaneMask(SubRegIdx)
|
|
|
|
: MRI->getMaxLaneMaskForVReg(Reg);
|
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
2016-12-15 22:36:06 +08:00
|
|
|
if ((SR.LaneMask & MOMask).none())
|
2016-02-03 04:04:51 +08:00
|
|
|
continue;
|
|
|
|
checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
|
|
|
|
}
|
2012-03-29 04:47:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-03 04:04:51 +08:00
|
|
|
report("Virtual register has no Live interval", MO, MONum);
|
2012-03-29 04:47:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
|
2012-06-07 06:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function gets called after visiting all instructions in a bundle. The
|
|
|
|
// argument points to the bundle header.
|
|
|
|
// Normal stand-alone instructions are also considered 'bundles', and this
|
|
|
|
// function is called for all of them.
|
|
|
|
void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
|
2009-05-16 08:33:53 +08:00
|
|
|
BBInfo &MInfo = MBBInfoMap[MI->getParent()];
|
|
|
|
set_union(MInfo.regsKilled, regsKilled);
|
2010-08-06 02:59:59 +08:00
|
|
|
set_subtract(regsLive, regsKilled); regsKilled.clear();
|
2012-02-28 09:42:41 +08:00
|
|
|
// Kill any masked registers.
|
|
|
|
while (!regMasks.empty()) {
|
|
|
|
const uint32_t *Mask = regMasks.pop_back_val();
|
|
|
|
for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(*I) &&
|
|
|
|
MachineOperand::clobbersPhysReg(Mask, *I))
|
|
|
|
regsDead.push_back(*I);
|
|
|
|
}
|
2010-08-06 02:59:59 +08:00
|
|
|
set_subtract(regsLive, regsDead); regsDead.clear();
|
|
|
|
set_union(regsLive, regsDefined); regsDefined.clear();
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-05 02:18:39 +08:00
|
|
|
MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
|
2009-05-16 08:33:53 +08:00
|
|
|
MBBInfoMap[MBB].regsLiveOut = regsLive;
|
|
|
|
regsLive.clear();
|
2011-01-13 05:27:48 +08:00
|
|
|
|
|
|
|
if (Indexes) {
|
|
|
|
SlotIndex stop = Indexes->getMBBEndIdx(MBB);
|
|
|
|
if (!(stop > lastIndex)) {
|
|
|
|
report("Block ends before last instruction index", MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Block ends at " << stop
|
2011-01-13 05:27:48 +08:00
|
|
|
<< " last instruction was at " << lastIndex << '\n';
|
|
|
|
}
|
|
|
|
lastIndex = stop;
|
|
|
|
}
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the largest possible vregsPassed sets. These are the registers that
|
|
|
|
// can pass through an MBB live, but may not be live every time. It is assumed
|
|
|
|
// that all vregsPassed sets are empty before the call.
|
2010-01-06 04:59:36 +08:00
|
|
|
void MachineVerifier::calcRegsPassed() {
|
2009-05-16 08:33:53 +08:00
|
|
|
// First push live-out regs to successors' vregsPassed. Remember the MBBs that
|
|
|
|
// have any vregsPassed.
|
2012-03-10 08:36:04 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 8> todo;
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
2009-05-16 08:33:53 +08:00
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
|
|
|
if (!MInfo.reachable)
|
|
|
|
continue;
|
|
|
|
for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
|
|
|
|
SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
|
|
|
|
BBInfo &SInfo = MBBInfoMap[*SuI];
|
|
|
|
if (SInfo.addPassed(MInfo.regsLiveOut))
|
|
|
|
todo.insert(*SuI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteratively push vregsPassed to successors. This will converge to the same
|
|
|
|
// final state regardless of DenseSet iteration order.
|
|
|
|
while (!todo.empty()) {
|
|
|
|
const MachineBasicBlock *MBB = *todo.begin();
|
|
|
|
todo.erase(MBB);
|
|
|
|
BBInfo &MInfo = MBBInfoMap[MBB];
|
|
|
|
for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
|
|
|
|
SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
|
|
|
|
if (*SuI == MBB)
|
|
|
|
continue;
|
|
|
|
BBInfo &SInfo = MBBInfoMap[*SuI];
|
|
|
|
if (SInfo.addPassed(MInfo.vregsPassed))
|
|
|
|
todo.insert(*SuI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 04:36:57 +08:00
|
|
|
// Calculate the set of virtual registers that must be passed through each basic
|
|
|
|
// block in order to satisfy the requirements of successor blocks. This is very
|
2010-01-06 04:59:36 +08:00
|
|
|
// similar to calcRegsPassed, only backwards.
|
2009-11-19 04:36:57 +08:00
|
|
|
void MachineVerifier::calcRegsRequired() {
|
|
|
|
// First push live-in regs to predecessors' vregsRequired.
|
2012-03-10 08:36:04 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 8> todo;
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
2009-11-19 04:36:57 +08:00
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
|
|
|
|
PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
|
|
|
|
BBInfo &PInfo = MBBInfoMap[*PrI];
|
|
|
|
if (PInfo.addRequired(MInfo.vregsLiveIn))
|
|
|
|
todo.insert(*PrI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteratively push vregsRequired to predecessors. This will converge to the
|
|
|
|
// same final state regardless of DenseSet iteration order.
|
|
|
|
while (!todo.empty()) {
|
|
|
|
const MachineBasicBlock *MBB = *todo.begin();
|
|
|
|
todo.erase(MBB);
|
|
|
|
BBInfo &MInfo = MBBInfoMap[MBB];
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
|
|
|
|
PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
|
|
|
|
if (*PrI == MBB)
|
|
|
|
continue;
|
|
|
|
BBInfo &SInfo = MBBInfoMap[*PrI];
|
|
|
|
if (SInfo.addRequired(MInfo.vregsRequired))
|
|
|
|
todo.insert(*PrI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
// Check PHI instructions at the beginning of MBB. It is assumed that
|
2010-01-06 04:59:36 +08:00
|
|
|
// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
|
2012-03-10 08:36:04 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 8> seen;
|
2014-05-01 06:17:38 +08:00
|
|
|
for (const auto &BBI : *MBB) {
|
|
|
|
if (!BBI.isPHI())
|
|
|
|
break;
|
2012-03-10 08:36:04 +08:00
|
|
|
seen.clear();
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2014-05-01 06:17:38 +08:00
|
|
|
for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
|
|
|
|
unsigned Reg = BBI.getOperand(i).getReg();
|
|
|
|
const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
|
2009-05-16 08:33:53 +08:00
|
|
|
if (!Pre->isSuccessor(MBB))
|
|
|
|
continue;
|
|
|
|
seen.insert(Pre);
|
|
|
|
BBInfo &PrInfo = MBBInfoMap[Pre];
|
|
|
|
if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
|
|
|
|
report("PHI operand is not live-out from predecessor",
|
2014-05-01 06:17:38 +08:00
|
|
|
&BBI.getOperand(i), i);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Did we see all predecessors?
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
|
|
|
|
PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
|
|
|
|
if (!seen.count(*PrI)) {
|
2014-05-01 06:17:38 +08:00
|
|
|
report("Missing PHI operand", &BBI);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "BB#" << (*PrI)->getNumber()
|
2009-05-16 08:33:53 +08:00
|
|
|
<< " is a predecessor according to the CFG.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-05 02:18:39 +08:00
|
|
|
void MachineVerifier::visitMachineFunctionAfter() {
|
2010-01-06 04:59:36 +08:00
|
|
|
calcRegsPassed();
|
2009-05-16 08:33:53 +08:00
|
|
|
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
2009-05-16 08:33:53 +08:00
|
|
|
|
|
|
|
// Skip unreachable MBBs.
|
|
|
|
if (!MInfo.reachable)
|
|
|
|
continue;
|
|
|
|
|
2014-05-01 02:29:51 +08:00
|
|
|
checkPHIOps(&MBB);
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:04:19 +08:00
|
|
|
// Now check liveness info if available
|
2012-03-10 08:36:06 +08:00
|
|
|
calcRegsRequired();
|
|
|
|
|
2012-06-30 05:00:00 +08:00
|
|
|
// Check for killed virtual registers that should be live out.
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
2012-06-30 05:00:00 +08:00
|
|
|
for (RegSet::iterator
|
|
|
|
I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
|
|
|
|
++I)
|
|
|
|
if (MInfo.regsKilled.count(*I)) {
|
2014-05-01 02:29:51 +08:00
|
|
|
report("Virtual register killed in block, but needed live out.", &MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Virtual register " << PrintReg(*I)
|
2012-06-30 05:00:00 +08:00
|
|
|
<< " is used after the block.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 02:18:27 +08:00
|
|
|
if (!MF->empty()) {
|
2012-03-10 08:36:06 +08:00
|
|
|
BBInfo &MInfo = MBBInfoMap[&MF->front()];
|
|
|
|
for (RegSet::iterator
|
|
|
|
I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
|
2016-05-12 05:31:39 +08:00
|
|
|
++I) {
|
|
|
|
report("Virtual register defs don't dominate all uses.", MF);
|
|
|
|
report_context_vreg(*I);
|
|
|
|
}
|
2012-03-10 08:36:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:04:19 +08:00
|
|
|
if (LiveVars)
|
2009-11-19 04:36:57 +08:00
|
|
|
verifyLiveVariables();
|
2010-08-07 02:04:19 +08:00
|
|
|
if (LiveInts)
|
|
|
|
verifyLiveIntervals();
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2009-11-19 04:36:57 +08:00
|
|
|
|
|
|
|
void MachineVerifier::verifyLiveVariables() {
|
|
|
|
assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
|
2011-01-09 07:11:02 +08:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
2009-11-19 04:36:57 +08:00
|
|
|
LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
BBInfo &MInfo = MBBInfoMap[&MBB];
|
2009-11-19 04:36:57 +08:00
|
|
|
|
|
|
|
// Our vregsRequired should be identical to LiveVariables' AliveBlocks
|
|
|
|
if (MInfo.vregsRequired.count(Reg)) {
|
2014-05-01 02:29:51 +08:00
|
|
|
if (!VI.AliveBlocks.test(MBB.getNumber())) {
|
|
|
|
report("LiveVariables: Block missing from AliveBlocks", &MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Virtual register " << PrintReg(Reg)
|
2009-11-19 04:36:57 +08:00
|
|
|
<< " must be live through the block.\n";
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-01 02:29:51 +08:00
|
|
|
if (VI.AliveBlocks.test(MBB.getNumber())) {
|
|
|
|
report("LiveVariables: Block should not be in AliveBlocks", &MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Virtual register " << PrintReg(Reg)
|
2009-11-19 04:36:57 +08:00
|
|
|
<< " is not needed live through the block.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-07 02:04:19 +08:00
|
|
|
void MachineVerifier::verifyLiveIntervals() {
|
|
|
|
assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
|
2012-06-21 07:23:59 +08:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
2010-10-07 07:54:35 +08:00
|
|
|
|
|
|
|
// Spilling and splitting may leave unused registers around. Skip them.
|
2012-06-21 07:23:59 +08:00
|
|
|
if (MRI->reg_nodbg_empty(Reg))
|
2010-10-07 07:54:35 +08:00
|
|
|
continue;
|
|
|
|
|
2012-06-21 07:23:59 +08:00
|
|
|
if (!LiveInts->hasInterval(Reg)) {
|
|
|
|
report("Missing live interval for virtual register", MF);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
|
2010-10-29 04:44:22 +08:00
|
|
|
continue;
|
2012-06-21 07:23:59 +08:00
|
|
|
}
|
2010-10-29 04:44:22 +08:00
|
|
|
|
2012-06-21 07:23:59 +08:00
|
|
|
const LiveInterval &LI = LiveInts->getInterval(Reg);
|
|
|
|
assert(Reg == LI.reg && "Invalid reg to interval mapping");
|
2012-08-02 08:20:20 +08:00
|
|
|
verifyLiveInterval(LI);
|
|
|
|
}
|
2012-08-03 00:36:50 +08:00
|
|
|
|
|
|
|
// Verify all the cached regunit intervals.
|
|
|
|
for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
|
2013-10-11 05:29:02 +08:00
|
|
|
if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
|
|
|
|
verifyLiveRange(*LR, i);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
|
2014-12-10 09:12:10 +08:00
|
|
|
const VNInfo *VNI, unsigned Reg,
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask) {
|
2012-08-02 08:20:20 +08:00
|
|
|
if (VNI->isUnused())
|
|
|
|
return;
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
if (!DefVNI) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Value not live at VNInfo def and not marked unused", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
if (DefVNI != VNI) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment at def has different VNInfo", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
|
|
|
|
if (!MBB) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Invalid VNInfo definition index", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-10-23 06:48:58 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
if (VNI->isPHIDef()) {
|
|
|
|
if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("PHIDef VNInfo is not defined at MBB start", MBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-12-20 11:15:20 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// Non-PHI def.
|
|
|
|
const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
|
|
|
|
if (!MI) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("No instruction at VNInfo def index", MBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-28 02:24:30 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
if (Reg != 0) {
|
|
|
|
bool hasDef = false;
|
|
|
|
bool isEarlyClobber = false;
|
2016-02-28 01:05:33 +08:00
|
|
|
for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!MOI->isReg() || !MOI->isDef())
|
2012-08-02 08:20:20 +08:00
|
|
|
continue;
|
2013-10-11 05:28:54 +08:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
|
|
|
if (MOI->getReg() != Reg)
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
|
|
|
|
!TRI->hasRegUnit(MOI->getReg(), Reg))
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any() &&
|
2016-12-15 22:36:06 +08:00
|
|
|
(TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
|
2014-12-10 09:12:10 +08:00
|
|
|
continue;
|
2013-10-11 05:28:54 +08:00
|
|
|
hasDef = true;
|
|
|
|
if (MOI->isEarlyClobber())
|
|
|
|
isEarlyClobber = true;
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2010-12-20 11:15:20 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!hasDef) {
|
|
|
|
report("Defining instruction does not modify register", MI);
|
2015-11-10 07:59:33 +08:00
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2013-10-11 05:28:54 +08:00
|
|
|
}
|
2012-08-02 08:20:20 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
// Early clobber defs begin at USE slots, but other defs must begin at
|
|
|
|
// DEF slots.
|
|
|
|
if (isEarlyClobber) {
|
|
|
|
if (!VNI->def.isEarlyClobber()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Early clobber def must be at an early-clobber slot", MBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2013-10-11 05:28:54 +08:00
|
|
|
}
|
|
|
|
} else if (!VNI->def.isRegister()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Non-PHI, non-early clobber def must be at a register slot", MBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
2010-08-07 02:04:19 +08:00
|
|
|
}
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
|
|
|
|
const LiveRange::const_iterator I,
|
2015-09-26 05:51:14 +08:00
|
|
|
unsigned Reg, LaneBitmask LaneMask)
|
|
|
|
{
|
2013-10-11 05:28:54 +08:00
|
|
|
const LiveRange::Segment &S = *I;
|
|
|
|
const VNInfo *VNI = S.valno;
|
2013-10-11 05:28:43 +08:00
|
|
|
assert(VNI && "Live segment has no valno");
|
2012-08-02 08:20:20 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Foreign valno in live segment", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
|
|
|
report_context(*VNI);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
if (VNI->isUnused()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment valno is marked unused", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
|
2012-08-02 08:20:20 +08:00
|
|
|
if (!MBB) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Bad start of live segment, no basic block", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.start != MBBStartIdx && S.start != VNI->def) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment must begin at MBB entry or valno def", MBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
const MachineBasicBlock *EndMBB =
|
2013-10-11 05:28:54 +08:00
|
|
|
LiveInts->getMBBFromIndex(S.end.getPrevSlot());
|
2012-08-02 08:20:20 +08:00
|
|
|
if (!EndMBB) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Bad end of live segment, no basic block", MF);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-10-23 08:49:09 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// No more checks for live-out segments.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.end == LiveInts->getMBBEndIdx(EndMBB))
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-03 00:36:50 +08:00
|
|
|
// RegUnit intervals are allowed dead phis.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
|
|
|
|
S.start == VNI->def && S.end == VNI->def.getDeadSlot())
|
2012-08-03 00:36:50 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// The live segment is ending inside EndMBB
|
|
|
|
const MachineInstr *MI =
|
2013-10-11 05:28:54 +08:00
|
|
|
LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
|
2012-08-02 08:20:20 +08:00
|
|
|
if (!MI) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment doesn't end at a valid instruction", EndMBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-28 02:24:30 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// The block slot must refer to a basic block boundary.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.end.isBlock()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment ends at B slot of an instruction", EndMBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
2012-02-28 02:24:30 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.end.isDead()) {
|
2012-08-02 08:20:20 +08:00
|
|
|
// Segment ends on the dead slot.
|
|
|
|
// That means there must be a dead def.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!SlotIndex::isSameInstr(S.start, S.end)) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Live segment ending at dead slot spans instructions", EndMBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A live segment can only end at an early-clobber slot if it is being
|
|
|
|
// redefined by an early-clobber def.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.end.isEarlyClobber()) {
|
|
|
|
if (I+1 == LR.end() || (I+1)->start != S.end) {
|
2012-08-02 08:20:20 +08:00
|
|
|
report("Live segment ending at early clobber slot must be "
|
2015-11-10 07:59:33 +08:00
|
|
|
"redefined by an EC def in the same instruction", EndMBB);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following checks only apply to virtual registers. Physreg liveness
|
|
|
|
// is too weird to check.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-10-11 05:28:43 +08:00
|
|
|
// A live segment can end with either a redefinition, a kill flag on a
|
2012-08-02 08:20:20 +08:00
|
|
|
// use, or a dead flag on a def.
|
|
|
|
bool hasRead = false;
|
2014-12-10 09:13:11 +08:00
|
|
|
bool hasSubRegDef = false;
|
2016-03-30 03:07:43 +08:00
|
|
|
bool hasDeadDef = false;
|
2016-02-28 01:05:33 +08:00
|
|
|
for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!MOI->isReg() || MOI->getReg() != Reg)
|
2012-02-28 02:24:30 +08:00
|
|
|
continue;
|
2016-08-24 21:37:55 +08:00
|
|
|
unsigned Sub = MOI->getSubReg();
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
|
|
|
|
: LaneBitmask::getAll();
|
2016-03-30 03:07:43 +08:00
|
|
|
if (MOI->isDef()) {
|
2016-08-24 21:37:55 +08:00
|
|
|
if (Sub != 0) {
|
2016-03-30 03:07:43 +08:00
|
|
|
hasSubRegDef = true;
|
2016-08-24 21:37:55 +08:00
|
|
|
// An operand vreg0:sub0<def> reads vreg0:sub1..n. Invert the lane
|
|
|
|
// mask for subregister defs. Read-undef defs will be handled by
|
|
|
|
// readsReg below.
|
2016-08-29 21:15:35 +08:00
|
|
|
SLM = ~SLM;
|
2016-08-24 21:37:55 +08:00
|
|
|
}
|
2016-03-30 03:07:43 +08:00
|
|
|
if (MOI->isDead())
|
|
|
|
hasDeadDef = true;
|
|
|
|
}
|
2016-12-17 03:11:56 +08:00
|
|
|
if (LaneMask.any() && (LaneMask & SLM).none())
|
2016-08-24 21:37:55 +08:00
|
|
|
continue;
|
2012-08-02 08:20:20 +08:00
|
|
|
if (MOI->readsReg())
|
|
|
|
hasRead = true;
|
|
|
|
}
|
2016-03-30 03:07:43 +08:00
|
|
|
if (S.end.isDead()) {
|
|
|
|
// Make sure that the corresponding machine operand for a "dead" live
|
|
|
|
// range has the dead flag. We cannot perform this check for subregister
|
|
|
|
// liveranges as partially dead values are allowed.
|
2016-12-15 22:36:06 +08:00
|
|
|
if (LaneMask.none() && !hasDeadDef) {
|
2016-03-30 03:07:43 +08:00
|
|
|
report("Instruction ending live segment on dead slot has no dead flag",
|
|
|
|
MI);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-02 08:20:20 +08:00
|
|
|
if (!hasRead) {
|
2014-12-10 09:13:11 +08:00
|
|
|
// When tracking subregister liveness, the main range must start new
|
|
|
|
// values on partial register writes, even if there is no read.
|
2016-12-17 03:11:56 +08:00
|
|
|
if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
|
2015-03-19 08:21:58 +08:00
|
|
|
!hasSubRegDef) {
|
2014-12-10 09:13:11 +08:00
|
|
|
report("Instruction ending live segment doesn't read the register",
|
|
|
|
MI);
|
2015-11-10 07:59:33 +08:00
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(S);
|
2014-12-10 09:13:11 +08:00
|
|
|
}
|
2012-02-28 02:24:30 +08:00
|
|
|
}
|
2012-08-02 08:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-28 02:24:30 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// Now check all the basic blocks in this live segment.
|
2015-10-10 03:40:45 +08:00
|
|
|
MachineFunction::const_iterator MFI = MBB->getIterator();
|
2013-10-11 05:28:43 +08:00
|
|
|
// Is this live segment the beginning of a non-PHIDef VN?
|
2013-10-11 05:28:54 +08:00
|
|
|
if (S.start == VNI->def && !VNI->isPHIDef()) {
|
2012-08-02 08:20:20 +08:00
|
|
|
// Not live-in to any blocks.
|
|
|
|
if (MBB == EndMBB)
|
|
|
|
return;
|
|
|
|
// Skip this block.
|
|
|
|
++MFI;
|
|
|
|
}
|
|
|
|
for (;;) {
|
2015-10-10 03:40:45 +08:00
|
|
|
assert(LiveInts->isLiveInToMBB(LR, &*MFI));
|
2012-08-02 08:20:20 +08:00
|
|
|
// We don't know how to track physregs into a landing pad.
|
2013-10-11 05:28:54 +08:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
|
2015-08-28 07:27:47 +08:00
|
|
|
MFI->isEHPad()) {
|
2012-08-02 08:20:20 +08:00
|
|
|
if (&*MFI == EndMBB)
|
|
|
|
break;
|
|
|
|
++MFI;
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-23 08:49:09 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// Is VNI a PHI-def in the current block?
|
|
|
|
bool IsPHI = VNI->isPHIDef() &&
|
2015-10-10 03:40:45 +08:00
|
|
|
VNI->def == LiveInts->getMBBStartIdx(&*MFI);
|
2012-08-02 08:20:20 +08:00
|
|
|
|
|
|
|
// Check that VNI is live-out of all predecessors.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
|
|
|
|
PE = MFI->pred_end(); PI != PE; ++PI) {
|
|
|
|
SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
|
2013-10-11 05:28:54 +08:00
|
|
|
const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
|
2012-08-02 08:20:20 +08:00
|
|
|
|
2016-05-21 07:02:13 +08:00
|
|
|
// All predecessors must have a live-out value if this is not a
|
|
|
|
// subregister liverange.
|
2016-12-15 22:36:06 +08:00
|
|
|
if (!PVNI && LaneMask.none()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Register not marked live out of predecessor", *PI);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
|
|
|
report_context(*VNI);
|
|
|
|
errs() << " live into BB#" << MFI->getNumber()
|
2015-10-10 03:40:45 +08:00
|
|
|
<< '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
|
|
|
|
<< PEnd << '\n';
|
2012-08-02 08:20:20 +08:00
|
|
|
continue;
|
2010-10-23 08:49:09 +08:00
|
|
|
}
|
2010-12-29 07:45:38 +08:00
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// Only PHI-defs can take different predecessor values.
|
|
|
|
if (!IsPHI && PVNI != VNI) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Different value live out of predecessor", *PI);
|
|
|
|
report_context(LR, Reg, LaneMask);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Valno #" << PVNI->id << " live out of BB#"
|
2015-10-10 03:40:45 +08:00
|
|
|
<< (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
|
|
|
|
<< " live into BB#" << MFI->getNumber() << '@'
|
|
|
|
<< LiveInts->getMBBStartIdx(&*MFI) << '\n';
|
2010-10-23 08:49:09 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
}
|
2012-08-02 08:20:20 +08:00
|
|
|
if (&*MFI == EndMBB)
|
|
|
|
break;
|
|
|
|
++MFI;
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 06:36:07 +08:00
|
|
|
|
2014-12-10 09:12:10 +08:00
|
|
|
void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask) {
|
2014-12-11 07:07:54 +08:00
|
|
|
for (const VNInfo *VNI : LR.valnos)
|
|
|
|
verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
|
2012-08-02 08:20:20 +08:00
|
|
|
|
2013-10-11 05:28:54 +08:00
|
|
|
for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
|
2014-12-10 09:12:10 +08:00
|
|
|
verifyLiveRangeSegment(LR, I, Reg, LaneMask);
|
2013-10-11 05:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
|
2014-12-10 09:12:10 +08:00
|
|
|
unsigned Reg = LI.reg;
|
2015-03-26 05:18:22 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg));
|
|
|
|
verifyLiveRange(LI, Reg);
|
|
|
|
|
2016-12-15 22:36:06 +08:00
|
|
|
LaneBitmask Mask;
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
|
2015-03-26 05:18:22 +08:00
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
2016-12-17 03:11:56 +08:00
|
|
|
if ((Mask & SR.LaneMask).any()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Lane masks of sub ranges overlap in live interval", MF);
|
|
|
|
report_context(LI);
|
|
|
|
}
|
2016-12-17 03:11:56 +08:00
|
|
|
if ((SR.LaneMask & ~MaxMask).any()) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Subrange lanemask is invalid", MF);
|
|
|
|
report_context(LI);
|
|
|
|
}
|
|
|
|
if (SR.empty()) {
|
|
|
|
report("Subrange must not be empty", MF);
|
|
|
|
report_context(SR, LI.reg, SR.LaneMask);
|
|
|
|
}
|
2015-03-26 05:18:22 +08:00
|
|
|
Mask |= SR.LaneMask;
|
|
|
|
verifyLiveRange(SR, LI.reg, SR.LaneMask);
|
2015-11-10 07:59:33 +08:00
|
|
|
if (!LI.covers(SR)) {
|
|
|
|
report("A Subrange is not covered by the main range", MF);
|
|
|
|
report_context(LI);
|
|
|
|
}
|
2014-12-10 09:12:10 +08:00
|
|
|
}
|
|
|
|
|
2012-08-02 08:20:20 +08:00
|
|
|
// Check the LI only has one connected component.
|
2015-03-26 05:18:22 +08:00
|
|
|
ConnectedVNInfoEqClasses ConEQ(*LiveInts);
|
2016-01-08 09:16:35 +08:00
|
|
|
unsigned NumComp = ConEQ.Classify(LI);
|
2015-03-26 05:18:22 +08:00
|
|
|
if (NumComp > 1) {
|
2015-11-10 07:59:33 +08:00
|
|
|
report("Multiple connected components in live interval", MF);
|
|
|
|
report_context(LI);
|
2015-03-26 05:18:22 +08:00
|
|
|
for (unsigned comp = 0; comp != NumComp; ++comp) {
|
|
|
|
errs() << comp << ": valnos";
|
|
|
|
for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
|
|
|
|
E = LI.vni_end(); I!=E; ++I)
|
|
|
|
if (comp == ConEQ.getEqClass(*I))
|
|
|
|
errs() << ' ' << (*I)->id;
|
|
|
|
errs() << '\n';
|
2010-10-27 06:36:07 +08:00
|
|
|
}
|
2010-08-07 02:04:19 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// FrameSetup and FrameDestroy can have zero adjustment, so using a single
|
|
|
|
// integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
|
|
|
|
// value is zero.
|
|
|
|
// We use a bool plus an integer to capture the stack state.
|
|
|
|
struct StackStateOfBB {
|
|
|
|
StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
|
|
|
|
ExitIsSetup(false) { }
|
|
|
|
StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
|
|
|
|
EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
|
|
|
|
ExitIsSetup(ExitSetup) { }
|
|
|
|
// Can be negative, which means we are setting up a frame.
|
|
|
|
int EntryValue;
|
|
|
|
int ExitValue;
|
|
|
|
bool EntryIsSetup;
|
|
|
|
bool ExitIsSetup;
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
|
|
|
|
/// by a FrameDestroy <n>, stack adjustments are identical on all
|
|
|
|
/// CFG edges to a merge point, and frame is destroyed at end of a return block.
|
|
|
|
void MachineVerifier::verifyStackFrame() {
|
2015-05-19 04:27:55 +08:00
|
|
|
unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
|
|
|
|
unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
SmallVector<StackStateOfBB, 8> SPState;
|
|
|
|
SPState.resize(MF->getNumBlockIDs());
|
2016-10-06 05:36:16 +08:00
|
|
|
df_iterator_default_set<const MachineBasicBlock*> Reachable;
|
2013-07-16 05:26:31 +08:00
|
|
|
|
|
|
|
// Visit the MBBs in DFS order.
|
|
|
|
for (df_ext_iterator<const MachineFunction*,
|
2016-10-06 05:36:16 +08:00
|
|
|
df_iterator_default_set<const MachineBasicBlock*> >
|
2013-07-16 05:26:31 +08:00
|
|
|
DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
|
|
|
|
DFI != DFE; ++DFI) {
|
|
|
|
const MachineBasicBlock *MBB = *DFI;
|
|
|
|
|
|
|
|
StackStateOfBB BBState;
|
|
|
|
// Check the exit state of the DFS stack predecessor.
|
|
|
|
if (DFI.getPathLength() >= 2) {
|
|
|
|
const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
|
|
|
|
assert(Reachable.count(StackPred) &&
|
|
|
|
"DFS stack predecessor is already visited.\n");
|
|
|
|
BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
|
|
|
|
BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
|
|
|
|
BBState.ExitValue = BBState.EntryValue;
|
|
|
|
BBState.ExitIsSetup = BBState.EntryIsSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update stack state by checking contents of MBB.
|
2014-05-01 06:17:38 +08:00
|
|
|
for (const auto &I : *MBB) {
|
|
|
|
if (I.getOpcode() == FrameSetupOpcode) {
|
2013-07-16 05:26:31 +08:00
|
|
|
// The first operand of a FrameOpcode should be i32.
|
2014-05-01 06:17:38 +08:00
|
|
|
int Size = I.getOperand(0).getImm();
|
2013-07-16 05:26:31 +08:00
|
|
|
assert(Size >= 0 &&
|
|
|
|
"Value should be non-negative in FrameSetup and FrameDestroy.\n");
|
|
|
|
|
|
|
|
if (BBState.ExitIsSetup)
|
2014-05-01 06:17:38 +08:00
|
|
|
report("FrameSetup is after another FrameSetup", &I);
|
2013-07-16 05:26:31 +08:00
|
|
|
BBState.ExitValue -= Size;
|
|
|
|
BBState.ExitIsSetup = true;
|
|
|
|
}
|
|
|
|
|
2014-05-01 06:17:38 +08:00
|
|
|
if (I.getOpcode() == FrameDestroyOpcode) {
|
2013-07-16 05:26:31 +08:00
|
|
|
// The first operand of a FrameOpcode should be i32.
|
2014-05-01 06:17:38 +08:00
|
|
|
int Size = I.getOperand(0).getImm();
|
2013-07-16 05:26:31 +08:00
|
|
|
assert(Size >= 0 &&
|
|
|
|
"Value should be non-negative in FrameSetup and FrameDestroy.\n");
|
|
|
|
|
|
|
|
if (!BBState.ExitIsSetup)
|
2014-05-01 06:17:38 +08:00
|
|
|
report("FrameDestroy is not after a FrameSetup", &I);
|
2013-07-16 05:26:31 +08:00
|
|
|
int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
|
|
|
|
BBState.ExitValue;
|
|
|
|
if (BBState.ExitIsSetup && AbsSPAdj != Size) {
|
2014-05-01 06:17:38 +08:00
|
|
|
report("FrameDestroy <n> is after FrameSetup <m>", &I);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
|
2013-07-16 05:26:31 +08:00
|
|
|
<< AbsSPAdj << ">.\n";
|
|
|
|
}
|
|
|
|
BBState.ExitValue += Size;
|
|
|
|
BBState.ExitIsSetup = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SPState[MBB->getNumber()] = BBState;
|
|
|
|
|
|
|
|
// Make sure the exit state of any predecessor is consistent with the entry
|
|
|
|
// state.
|
|
|
|
for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
|
|
|
|
E = MBB->pred_end(); I != E; ++I) {
|
|
|
|
if (Reachable.count(*I) &&
|
|
|
|
(SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
|
|
|
|
SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
|
|
|
|
report("The exit stack state of a predecessor is inconsistent.", MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
|
2013-07-16 05:26:31 +08:00
|
|
|
<< SPState[(*I)->getNumber()].ExitValue << ", "
|
|
|
|
<< SPState[(*I)->getNumber()].ExitIsSetup
|
|
|
|
<< "), while BB#" << MBB->getNumber() << " has entry state ("
|
|
|
|
<< BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the entry state of any successor is consistent with the exit
|
|
|
|
// state.
|
|
|
|
for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
|
|
|
|
E = MBB->succ_end(); I != E; ++I) {
|
|
|
|
if (Reachable.count(*I) &&
|
|
|
|
(SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
|
|
|
|
SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
|
|
|
|
report("The entry stack state of a successor is inconsistent.", MBB);
|
2015-02-04 08:02:59 +08:00
|
|
|
errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
|
2013-07-16 05:26:31 +08:00
|
|
|
<< SPState[(*I)->getNumber()].EntryValue << ", "
|
|
|
|
<< SPState[(*I)->getNumber()].EntryIsSetup
|
|
|
|
<< "), while BB#" << MBB->getNumber() << " has exit state ("
|
|
|
|
<< BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure a basic block with return ends with zero stack adjustment.
|
|
|
|
if (!MBB->empty() && MBB->back().isReturn()) {
|
|
|
|
if (BBState.ExitIsSetup)
|
|
|
|
report("A return block ends with a FrameSetup.", MBB);
|
|
|
|
if (BBState.ExitValue)
|
|
|
|
report("A return block ends with a nonzero stack adjustment.", MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|