2005-01-07 07:47:53 +00:00
|
|
|
//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-07 07:47:53 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2005-01-07 07:47:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the SelectionDAGISel class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "isel"
|
2009-02-06 17:22:58 +00:00
|
|
|
#include "ScheduleDAGSDNodes.h"
|
2008-09-03 16:12:24 +00:00
|
|
|
#include "SelectionDAGBuild.h"
|
2009-02-06 17:22:58 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAGISel.h"
|
2006-10-16 20:52:31 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2009-09-16 21:09:07 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2007-04-04 21:14:49 +00:00
|
|
|
#include "llvm/Constants.h"
|
2005-05-13 18:50:42 +00:00
|
|
|
#include "llvm/CallingConv.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
2005-11-29 06:21:05 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2006-01-26 22:24:51 +00:00
|
|
|
#include "llvm/InlineAsm.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2006-03-23 18:06:46 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2008-08-19 22:33:34 +00:00
|
|
|
#include "llvm/CodeGen/FastISel.h"
|
2008-08-17 12:56:54 +00:00
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
2008-08-17 18:44:35 +00:00
|
|
|
#include "llvm/CodeGen/GCMetadata.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-07-31 18:16:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-01-15 22:18:12 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
|
2006-08-02 12:30:23 +00:00
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
2009-01-09 19:11:50 +00:00
|
|
|
#include "llvm/CodeGen/DwarfWriter.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2005-01-07 07:47:53 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2006-05-23 13:43:15 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-06-30 20:45:06 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-06-30 20:45:06 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-02-24 02:52:40 +00:00
|
|
|
#include <algorithm>
|
2005-01-07 07:47:53 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-07-10 23:37:50 +00:00
|
|
|
static cl::opt<bool>
|
2008-10-27 08:42:46 +00:00
|
|
|
DisableLegalizeTypes("disable-legalize-types", cl::Hidden);
|
2008-08-19 22:33:34 +00:00
|
|
|
static cl::opt<bool>
|
2008-09-09 22:06:46 +00:00
|
|
|
EnableFastISelVerbose("fast-isel-verbose", cl::Hidden,
|
2008-10-20 21:30:12 +00:00
|
|
|
cl::desc("Enable verbose messages in the \"fast\" "
|
2008-09-09 22:06:46 +00:00
|
|
|
"instruction selector"));
|
|
|
|
static cl::opt<bool>
|
2008-09-09 23:05:00 +00:00
|
|
|
EnableFastISelAbort("fast-isel-abort", cl::Hidden,
|
|
|
|
cl::desc("Enable abort calls when \"fast\" instruction fails"));
|
2008-09-05 22:59:21 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
SchedLiveInCopies("schedule-livein-copies",
|
|
|
|
cl::desc("Schedule copies of livein registers"),
|
|
|
|
cl::init(false));
|
2008-06-17 06:09:18 +00:00
|
|
|
|
2005-09-01 18:44:10 +00:00
|
|
|
#ifndef NDEBUG
|
2005-01-12 03:41:21 +00:00
|
|
|
static cl::opt<bool>
|
2008-07-21 20:00:07 +00:00
|
|
|
ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show dags before the first "
|
|
|
|
"dag combine pass"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show dags before legalize types"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show dags before legalize"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show dags before the second "
|
|
|
|
"dag combine pass"));
|
|
|
|
static cl::opt<bool>
|
2008-11-24 14:53:14 +00:00
|
|
|
ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show dags before the post legalize types"
|
|
|
|
" dag combine pass"));
|
|
|
|
static cl::opt<bool>
|
2006-01-21 02:32:06 +00:00
|
|
|
ViewISelDAGs("view-isel-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show isel dags as they are selected"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
ViewSchedDAGs("view-sched-dags", cl::Hidden,
|
|
|
|
cl::desc("Pop up a window to show sched dags as they are processed"));
|
2007-08-28 20:32:58 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
ViewSUnitDAGs("view-sunit-dags", cl::Hidden,
|
2008-01-25 17:24:52 +00:00
|
|
|
cl::desc("Pop up a window to show SUnit dags after they are processed"));
|
2005-01-12 03:41:21 +00:00
|
|
|
#else
|
2008-07-21 20:00:07 +00:00
|
|
|
static const bool ViewDAGCombine1 = false,
|
|
|
|
ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false,
|
|
|
|
ViewDAGCombine2 = false,
|
2008-11-24 14:53:14 +00:00
|
|
|
ViewDAGCombineLT = false,
|
2008-07-21 20:00:07 +00:00
|
|
|
ViewISelDAGs = false, ViewSchedDAGs = false,
|
|
|
|
ViewSUnitDAGs = false;
|
2005-01-12 03:41:21 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-02 12:30:23 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// RegisterScheduler class - Track the registration of instruction schedulers.
|
|
|
|
///
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
MachinePassRegistry RegisterScheduler::Registry;
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// ISHeuristic command line option for instruction schedulers.
|
|
|
|
///
|
|
|
|
//===---------------------------------------------------------------------===//
|
2008-05-13 00:00:25 +00:00
|
|
|
static cl::opt<RegisterScheduler::FunctionPassCtor, false,
|
|
|
|
RegisterPassParser<RegisterScheduler> >
|
|
|
|
ISHeuristic("pre-RA-sched",
|
|
|
|
cl::init(&createDefaultScheduler),
|
|
|
|
cl::desc("Instruction schedulers available (before register"
|
|
|
|
" allocation):"));
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterScheduler
|
2008-10-14 20:25:08 +00:00
|
|
|
defaultListDAGScheduler("default", "Best scheduler for the target",
|
2008-05-13 00:00:25 +00:00
|
|
|
createDefaultScheduler);
|
2006-01-23 07:01:07 +00:00
|
|
|
|
2008-09-03 16:12:24 +00:00
|
|
|
namespace llvm {
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// createDefaultScheduler - This creates an instruction scheduler appropriate
|
|
|
|
/// for the target.
|
2009-02-11 04:27:20 +00:00
|
|
|
ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS,
|
2009-04-29 23:29:43 +00:00
|
|
|
CodeGenOpt::Level OptLevel) {
|
2009-01-15 16:58:17 +00:00
|
|
|
const TargetLowering &TLI = IS->getTargetLowering();
|
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
if (OptLevel == CodeGenOpt::None)
|
2009-04-29 00:15:41 +00:00
|
|
|
return createFastDAGScheduler(IS, OptLevel);
|
2008-11-20 03:11:19 +00:00
|
|
|
if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
|
2009-04-29 00:15:41 +00:00
|
|
|
return createTDListDAGScheduler(IS, OptLevel);
|
2008-11-20 03:11:19 +00:00
|
|
|
assert(TLI.getSchedulingPreference() ==
|
|
|
|
TargetLowering::SchedulingForRegPressure && "Unknown sched type!");
|
2009-04-29 00:15:41 +00:00
|
|
|
return createBURRListDAGScheduler(IS, OptLevel);
|
2007-04-12 06:00:20 +00:00
|
|
|
}
|
2005-01-07 07:47:53 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 18:18:23 +00:00
|
|
|
// EmitInstrWithCustomInserter - This method should be implemented by targets
|
|
|
|
// that mark instructions with the 'usesCustomDAGSchedInserter' flag. These
|
2005-08-26 20:54:47 +00:00
|
|
|
// instructions are special in various ways, which require special support to
|
|
|
|
// insert. The specified MachineInstr is created but not inserted into any
|
|
|
|
// basic blocks, and the scheduler passes ownership of it to this method.
|
2008-01-30 18:18:23 +00:00
|
|
|
MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
|
2009-09-18 21:02:19 +00:00
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
|
2009-07-12 20:07:01 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "If a target marks an instruction with "
|
2009-07-12 20:07:01 +00:00
|
|
|
"'usesCustomDAGSchedInserter', it must implement "
|
|
|
|
"TargetLowering::EmitInstrWithCustomInserter!";
|
|
|
|
#endif
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable(0);
|
2009-09-20 02:20:51 +00:00
|
|
|
return 0;
|
2005-08-26 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2008-09-05 22:59:21 +00:00
|
|
|
/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
|
|
|
|
/// physical register has only a single copy use, then coalesced the copy
|
|
|
|
/// if possible.
|
|
|
|
static void EmitLiveInCopy(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator &InsertPos,
|
|
|
|
unsigned VirtReg, unsigned PhysReg,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
DenseMap<MachineInstr*, unsigned> &CopyRegMap,
|
|
|
|
const MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const TargetInstrInfo &TII) {
|
|
|
|
unsigned NumUses = 0;
|
|
|
|
MachineInstr *UseMI = NULL;
|
|
|
|
for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
|
|
|
|
UE = MRI.use_end(); UI != UE; ++UI) {
|
|
|
|
UseMI = &*UI;
|
|
|
|
if (++NumUses > 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the number of uses is not one, or the use is not a move instruction,
|
|
|
|
// don't coalesce. Also, only coalesce away a virtual register to virtual
|
|
|
|
// register copy.
|
|
|
|
bool Coalesced = false;
|
2009-01-20 19:12:24 +00:00
|
|
|
unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
|
2008-09-05 22:59:21 +00:00
|
|
|
if (NumUses == 1 &&
|
2009-01-20 19:12:24 +00:00
|
|
|
TII.isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
|
2008-09-05 22:59:21 +00:00
|
|
|
TargetRegisterInfo::isVirtualRegister(DstReg)) {
|
|
|
|
VirtReg = DstReg;
|
|
|
|
Coalesced = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now find an ideal location to insert the copy.
|
|
|
|
MachineBasicBlock::iterator Pos = InsertPos;
|
|
|
|
while (Pos != MBB->begin()) {
|
|
|
|
MachineInstr *PrevMI = prior(Pos);
|
|
|
|
DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
|
|
|
|
// copyRegToReg might emit multiple instructions to do a copy.
|
|
|
|
unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
|
|
|
|
if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg))
|
|
|
|
// This is what the BB looks like right now:
|
|
|
|
// r1024 = mov r0
|
|
|
|
// ...
|
|
|
|
// r1 = mov r1024
|
|
|
|
//
|
|
|
|
// We want to insert "r1025 = mov r1". Inserting this copy below the
|
|
|
|
// move to r1024 makes it impossible for that move to be coalesced.
|
|
|
|
//
|
|
|
|
// r1025 = mov r1
|
|
|
|
// r1024 = mov r0
|
|
|
|
// ...
|
|
|
|
// r1 = mov 1024
|
|
|
|
// r2 = mov 1025
|
|
|
|
break; // Woot! Found a good location.
|
|
|
|
--Pos;
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:10:31 +00:00
|
|
|
bool Emitted = TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
|
|
|
|
assert(Emitted && "Unable to issue a live-in copy instruction!\n");
|
|
|
|
(void) Emitted;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-10-16 05:42:28 +00:00
|
|
|
CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
|
2008-09-05 22:59:21 +00:00
|
|
|
if (Coalesced) {
|
|
|
|
if (&*InsertPos == UseMI) ++InsertPos;
|
|
|
|
MBB->erase(UseMI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitLiveInCopies - If this is the first basic block in the function,
|
|
|
|
/// and if it has live ins that need to be copied into vregs, emit the
|
|
|
|
/// copies into the block.
|
|
|
|
static void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
|
|
|
|
const MachineRegisterInfo &MRI,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
const TargetInstrInfo &TII) {
|
|
|
|
if (SchedLiveInCopies) {
|
|
|
|
// Emit the copies at a heuristically-determined location in the block.
|
|
|
|
DenseMap<MachineInstr*, unsigned> CopyRegMap;
|
|
|
|
MachineBasicBlock::iterator InsertPos = EntryMBB->begin();
|
|
|
|
for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
|
|
|
|
E = MRI.livein_end(); LI != E; ++LI)
|
|
|
|
if (LI->second) {
|
|
|
|
const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
|
|
|
|
EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first,
|
|
|
|
RC, CopyRegMap, MRI, TRI, TII);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Emit the copies into the top of the block.
|
|
|
|
for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
|
|
|
|
E = MRI.livein_end(); LI != E; ++LI)
|
|
|
|
if (LI->second) {
|
|
|
|
const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
|
2009-07-08 23:10:31 +00:00
|
|
|
bool Emitted = TII.copyRegToReg(*EntryMBB, EntryMBB->begin(),
|
|
|
|
LI->second, LI->first, RC, RC);
|
|
|
|
assert(Emitted && "Unable to issue a live-in copy instruction!\n");
|
|
|
|
(void) Emitted;
|
2008-09-05 22:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-11 05:56:49 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SelectionDAGISel code
|
|
|
|
//===----------------------------------------------------------------------===//
|
2005-01-07 07:47:53 +00:00
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) :
|
2009-07-31 18:16:33 +00:00
|
|
|
MachineFunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()),
|
2008-08-27 23:52:12 +00:00
|
|
|
FuncInfo(new FunctionLoweringInfo(TLI)),
|
|
|
|
CurDAG(new SelectionDAG(TLI, *FuncInfo)),
|
2009-04-29 00:15:41 +00:00
|
|
|
SDL(new SelectionDAGLowering(*CurDAG, TLI, *FuncInfo, OL)),
|
2008-08-27 23:52:12 +00:00
|
|
|
GFI(),
|
2009-04-29 00:15:41 +00:00
|
|
|
OptLevel(OL),
|
2008-08-27 23:52:12 +00:00
|
|
|
DAGSize(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
SelectionDAGISel::~SelectionDAGISel() {
|
|
|
|
delete SDL;
|
|
|
|
delete CurDAG;
|
|
|
|
delete FuncInfo;
|
|
|
|
}
|
|
|
|
|
2009-08-10 22:56:29 +00:00
|
|
|
unsigned SelectionDAGISel::MakeReg(EVT VT) {
|
2007-12-31 04:13:23 +00:00
|
|
|
return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
|
2005-01-07 07:47:53 +00:00
|
|
|
}
|
|
|
|
|
2005-08-17 06:37:43 +00:00
|
|
|
void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
|
2006-10-16 20:52:31 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2009-07-31 23:36:22 +00:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2008-08-17 18:44:35 +00:00
|
|
|
AU.addRequired<GCModuleInfo>();
|
2009-07-31 23:36:22 +00:00
|
|
|
AU.addPreserved<GCModuleInfo>();
|
2009-01-09 19:11:50 +00:00
|
|
|
AU.addRequired<DwarfWriter>();
|
2009-07-31 23:36:22 +00:00
|
|
|
AU.addPreserved<DwarfWriter>();
|
2009-07-31 18:16:33 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2005-08-17 06:37:43 +00:00
|
|
|
}
|
2005-01-07 07:47:53 +00:00
|
|
|
|
2009-07-31 18:16:33 +00:00
|
|
|
bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
Function &Fn = *mf.getFunction();
|
|
|
|
|
2008-09-09 23:05:00 +00:00
|
|
|
// Do some sanity-checking on the command-line options.
|
|
|
|
assert((!EnableFastISelVerbose || EnableFastISel) &&
|
|
|
|
"-fast-isel-verbose requires -fast-isel");
|
|
|
|
assert((!EnableFastISelAbort || EnableFastISel) &&
|
|
|
|
"-fast-isel-abort requires -fast-isel");
|
|
|
|
|
2007-08-27 16:26:13 +00:00
|
|
|
// Get alias analysis for load/store combining.
|
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
|
2009-07-31 18:16:33 +00:00
|
|
|
MF = &mf;
|
2008-09-05 22:59:21 +00:00
|
|
|
const TargetInstrInfo &TII = *TM.getInstrInfo();
|
|
|
|
const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
|
|
|
|
|
2009-08-01 03:51:09 +00:00
|
|
|
if (Fn.hasGC())
|
|
|
|
GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(Fn);
|
2008-01-07 01:30:38 +00:00
|
|
|
else
|
2008-08-17 18:44:35 +00:00
|
|
|
GFI = 0;
|
2009-01-15 19:20:50 +00:00
|
|
|
RegInfo = &MF->getRegInfo();
|
2009-07-25 00:23:56 +00:00
|
|
|
DEBUG(errs() << "\n\n\n=== " << Fn.getName() << "\n");
|
2005-01-07 07:47:53 +00:00
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
|
|
|
|
DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
|
2009-07-09 18:44:09 +00:00
|
|
|
CurDAG->init(*MF, MMI, DW);
|
2009-02-03 18:46:32 +00:00
|
|
|
FuncInfo->set(Fn, *MF, *CurDAG, EnableFastISel);
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->init(GFI, *AA);
|
2005-01-07 07:47:53 +00:00
|
|
|
|
2008-04-02 00:25:04 +00:00
|
|
|
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
|
|
|
|
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
|
|
|
|
// Mark landing pad.
|
2008-08-27 23:52:12 +00:00
|
|
|
FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
|
2007-06-06 10:05:18 +00:00
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
SelectAllBasicBlocks(Fn, *MF, MMI, DW, TII);
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2008-09-05 22:59:21 +00:00
|
|
|
// If the first basic block in the function has live ins that need to be
|
|
|
|
// copied into vregs, emit the copies into the top of the block before
|
|
|
|
// emitting the code for the block.
|
2009-01-15 19:20:50 +00:00
|
|
|
EmitLiveInCopies(MF->begin(), *RegInfo, TRI, TII);
|
2008-09-05 22:59:21 +00:00
|
|
|
|
2007-02-10 02:43:39 +00:00
|
|
|
// Add function live-ins to entry block live-in set.
|
2008-09-05 22:59:21 +00:00
|
|
|
for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(),
|
|
|
|
E = RegInfo->livein_end(); I != E; ++I)
|
2009-01-15 19:20:50 +00:00
|
|
|
MF->begin()->addLiveIn(I->first);
|
2007-02-10 02:43:39 +00:00
|
|
|
|
2007-06-15 19:04:19 +00:00
|
|
|
#ifndef NDEBUG
|
2008-08-27 23:52:12 +00:00
|
|
|
assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() &&
|
2007-06-15 19:04:19 +00:00
|
|
|
"Not all catch info was assigned to a landing pad!");
|
|
|
|
#endif
|
|
|
|
|
2008-08-27 23:52:12 +00:00
|
|
|
FuncInfo->clear();
|
|
|
|
|
2005-01-07 07:47:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-15 19:04:19 +00:00
|
|
|
static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB,
|
|
|
|
MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
|
|
|
|
for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I)
|
2008-09-03 16:12:24 +00:00
|
|
|
if (EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) {
|
2007-06-15 19:04:19 +00:00
|
|
|
// Apply the catch info to DestBB.
|
2008-09-03 16:12:24 +00:00
|
|
|
AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]);
|
2007-06-15 19:04:19 +00:00
|
|
|
#ifndef NDEBUG
|
2007-11-15 09:54:37 +00:00
|
|
|
if (!FLI.MBBMap[SrcBB]->isLandingPad())
|
2008-09-03 16:12:24 +00:00
|
|
|
FLI.CatchInfoFound.insert(EHSel);
|
2007-06-15 19:04:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
|
|
|
|
BasicBlock::iterator Begin,
|
2008-08-28 20:28:56 +00:00
|
|
|
BasicBlock::iterator End) {
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2009-09-28 21:41:20 +00:00
|
|
|
MetadataContext &TheMetadata = LLVMBB->getParent()->getContext().getMetadata();
|
2009-09-28 21:14:55 +00:00
|
|
|
unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
|
2008-08-23 02:25:05 +00:00
|
|
|
|
Major calling convention code refactoring.
Instead of awkwardly encoding calling-convention information with ISD::CALL,
ISD::FORMAL_ARGUMENTS, ISD::RET, and ISD::ARG_FLAGS nodes, TargetLowering
provides three virtual functions for targets to override:
LowerFormalArguments, LowerCall, and LowerRet, which replace the custom
lowering done on the special nodes. They provide the same information, but
in a more immediately usable format.
This also reworks much of the target-independent tail call logic. The
decision of whether or not to perform a tail call is now cleanly split
between target-independent portions, and the target dependent portion
in IsEligibleForTailCallOptimization.
This also synchronizes all in-tree targets, to help enable future
refactoring and feature work.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78142 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-05 01:29:28 +00:00
|
|
|
// Lower all of the non-terminator instructions. If a call is emitted
|
|
|
|
// as a tail call, cease emitting nodes for this block.
|
2009-09-16 20:39:11 +00:00
|
|
|
for (BasicBlock::iterator I = Begin; I != End && !SDL->HasTailCall; ++I) {
|
|
|
|
if (MDDbgKind) {
|
2009-09-20 02:20:51 +00:00
|
|
|
// Update DebugLoc if debug information is attached with this
|
2009-09-16 20:39:11 +00:00
|
|
|
// instruction.
|
2009-09-29 19:56:13 +00:00
|
|
|
if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, I)) {
|
2009-09-20 02:20:51 +00:00
|
|
|
DILocation DILoc(Dbg);
|
|
|
|
DebugLoc Loc = ExtractDebugLocation(DILoc, MF->getDebugLocInfo());
|
|
|
|
SDL->setCurDebugLoc(Loc);
|
2009-10-12 23:10:55 +00:00
|
|
|
if (MF->getDefaultDebugLoc().isUnknown())
|
|
|
|
MF->setDefaultDebugLoc(Loc);
|
2009-09-16 20:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-23 02:25:05 +00:00
|
|
|
if (!isa<TerminatorInst>(I))
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visit(*I);
|
2009-09-16 20:39:11 +00:00
|
|
|
}
|
2008-08-23 02:25:05 +00:00
|
|
|
|
Major calling convention code refactoring.
Instead of awkwardly encoding calling-convention information with ISD::CALL,
ISD::FORMAL_ARGUMENTS, ISD::RET, and ISD::ARG_FLAGS nodes, TargetLowering
provides three virtual functions for targets to override:
LowerFormalArguments, LowerCall, and LowerRet, which replace the custom
lowering done on the special nodes. They provide the same information, but
in a more immediately usable format.
This also reworks much of the target-independent tail call logic. The
decision of whether or not to perform a tail call is now cleanly split
between target-independent portions, and the target dependent portion
in IsEligibleForTailCallOptimization.
This also synchronizes all in-tree targets, to help enable future
refactoring and feature work.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78142 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-05 01:29:28 +00:00
|
|
|
if (!SDL->HasTailCall) {
|
|
|
|
// Ensure that all instructions which are used outside of their defining
|
|
|
|
// blocks are available as virtual registers. Invoke is handled elsewhere.
|
|
|
|
for (BasicBlock::iterator I = Begin; I != End; ++I)
|
|
|
|
if (!isa<PHINode>(I) && !isa<InvokeInst>(I))
|
|
|
|
SDL->CopyToExportRegsIfNeeded(I);
|
2008-08-23 02:25:05 +00:00
|
|
|
|
Major calling convention code refactoring.
Instead of awkwardly encoding calling-convention information with ISD::CALL,
ISD::FORMAL_ARGUMENTS, ISD::RET, and ISD::ARG_FLAGS nodes, TargetLowering
provides three virtual functions for targets to override:
LowerFormalArguments, LowerCall, and LowerRet, which replace the custom
lowering done on the special nodes. They provide the same information, but
in a more immediately usable format.
This also reworks much of the target-independent tail call logic. The
decision of whether or not to perform a tail call is now cleanly split
between target-independent portions, and the target dependent portion
in IsEligibleForTailCallOptimization.
This also synchronizes all in-tree targets, to help enable future
refactoring and feature work.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78142 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-05 01:29:28 +00:00
|
|
|
// Handle PHI nodes in successor blocks.
|
|
|
|
if (End == LLVMBB->end()) {
|
|
|
|
HandlePHINodesInSuccessorBlocks(LLVMBB);
|
2008-09-03 23:12:08 +00:00
|
|
|
|
Major calling convention code refactoring.
Instead of awkwardly encoding calling-convention information with ISD::CALL,
ISD::FORMAL_ARGUMENTS, ISD::RET, and ISD::ARG_FLAGS nodes, TargetLowering
provides three virtual functions for targets to override:
LowerFormalArguments, LowerCall, and LowerRet, which replace the custom
lowering done on the special nodes. They provide the same information, but
in a more immediately usable format.
This also reworks much of the target-independent tail call logic. The
decision of whether or not to perform a tail call is now cleanly split
between target-independent portions, and the target dependent portion
in IsEligibleForTailCallOptimization.
This also synchronizes all in-tree targets, to help enable future
refactoring and feature work.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78142 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-05 01:29:28 +00:00
|
|
|
// Lower the terminator after the copies are emitted.
|
|
|
|
SDL->visit(*LLVMBB->getTerminator());
|
|
|
|
}
|
2008-09-03 23:12:08 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2005-01-17 19:43:36 +00:00
|
|
|
// Make sure the root of the DAG is up-to-date.
|
2008-08-27 23:52:12 +00:00
|
|
|
CurDAG->setRoot(SDL->getControlRoot());
|
2007-10-11 19:40:01 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
// Final step, emit the lowered DAG as machine code.
|
|
|
|
CodeGenAndEmitDAG();
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->clear();
|
2005-01-07 07:47:53 +00:00
|
|
|
}
|
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
void SelectionDAGISel::ComputeLiveOutVRegInfo() {
|
2008-06-17 06:09:18 +00:00
|
|
|
SmallPtrSet<SDNode*, 128> VisitedNodes;
|
|
|
|
SmallVector<SDNode*, 128> Worklist;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-08-28 21:40:38 +00:00
|
|
|
Worklist.push_back(CurDAG->getRoot().getNode());
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
APInt Mask;
|
|
|
|
APInt KnownZero;
|
|
|
|
APInt KnownOne;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
SDNode *N = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
// If we've already seen this node, ignore it.
|
|
|
|
if (!VisitedNodes.insert(N))
|
|
|
|
continue;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
// Otherwise, add all chain operands to the worklist.
|
|
|
|
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
|
2009-08-11 20:47:22 +00:00
|
|
|
if (N->getOperand(i).getValueType() == MVT::Other)
|
2008-08-28 21:40:38 +00:00
|
|
|
Worklist.push_back(N->getOperand(i).getNode());
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
// If this is a CopyToReg with a vreg dest, process it.
|
|
|
|
if (N->getOpcode() != ISD::CopyToReg)
|
|
|
|
continue;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(DestReg))
|
|
|
|
continue;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
// Ignore non-scalar or non-integer values.
|
2008-07-27 21:46:04 +00:00
|
|
|
SDValue Src = N->getOperand(2);
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT SrcVT = Src.getValueType();
|
2008-06-17 06:09:18 +00:00
|
|
|
if (!SrcVT.isInteger() || SrcVT.isVector())
|
|
|
|
continue;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src);
|
2008-06-17 06:09:18 +00:00
|
|
|
Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits());
|
2008-08-23 02:25:05 +00:00
|
|
|
CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-17 06:09:18 +00:00
|
|
|
// Only install this information if it tells us something.
|
|
|
|
if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) {
|
|
|
|
DestReg -= TargetRegisterInfo::FirstVirtualRegister;
|
2009-08-01 03:51:09 +00:00
|
|
|
if (DestReg >= FuncInfo->LiveOutRegInfo.size())
|
|
|
|
FuncInfo->LiveOutRegInfo.resize(DestReg+1);
|
|
|
|
FunctionLoweringInfo::LiveOutInfo &LOI =
|
|
|
|
FuncInfo->LiveOutRegInfo[DestReg];
|
2008-06-17 06:09:18 +00:00
|
|
|
LOI.NumSignBits = NumSignBits;
|
2009-03-27 23:55:04 +00:00
|
|
|
LOI.KnownOne = KnownOne;
|
|
|
|
LOI.KnownZero = KnownZero;
|
2008-06-17 06:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
void SelectionDAGISel::CodeGenAndEmitDAG() {
|
2008-07-21 20:00:07 +00:00
|
|
|
std::string GroupName;
|
|
|
|
if (TimePassesIsEnabled)
|
|
|
|
GroupName = "Instruction Selection and Scheduling";
|
|
|
|
std::string BlockName;
|
|
|
|
if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs ||
|
2008-11-24 14:53:14 +00:00
|
|
|
ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs ||
|
|
|
|
ViewSUnitDAGs)
|
2009-08-01 03:51:09 +00:00
|
|
|
BlockName = MF->getFunction()->getNameStr() + ":" +
|
2009-07-24 08:24:36 +00:00
|
|
|
BB->getBasicBlock()->getNameStr();
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Initial selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName);
|
2007-10-08 15:12:17 +00:00
|
|
|
|
2005-10-10 16:47:10 +00:00
|
|
|
// Run the DAG combiner in pre-legalize mode.
|
2008-07-01 17:59:20 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
2008-07-14 18:19:29 +00:00
|
|
|
NamedRegionTimer T("DAG Combining 1", GroupName);
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(Unrestricted, *AA, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
} else {
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(Unrestricted, *AA, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Optimized lowered selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2005-01-07 07:47:53 +00:00
|
|
|
// Second step, hack on the DAG until it only uses operations and types that
|
|
|
|
// the target supports.
|
2008-10-27 08:42:46 +00:00
|
|
|
if (!DisableLegalizeTypes) {
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " +
|
|
|
|
BlockName);
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-11-24 14:53:14 +00:00
|
|
|
bool Changed;
|
2008-07-21 20:00:07 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("Type Legalization", GroupName);
|
2008-11-24 14:53:14 +00:00
|
|
|
Changed = CurDAG->LegalizeTypes();
|
2008-07-21 20:00:07 +00:00
|
|
|
} else {
|
2008-11-24 14:53:14 +00:00
|
|
|
Changed = CurDAG->LegalizeTypes();
|
2008-07-21 20:00:07 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Type-legalized selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-11-24 14:53:14 +00:00
|
|
|
if (Changed) {
|
|
|
|
if (ViewDAGCombineLT)
|
|
|
|
CurDAG->viewGraph("dag-combine-lt input for " + BlockName);
|
|
|
|
|
|
|
|
// Run the DAG combiner in post-type-legalize mode.
|
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("DAG Combining after legalize types", GroupName);
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
|
2008-11-24 14:53:14 +00:00
|
|
|
} else {
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(NoIllegalTypes, *AA, OptLevel);
|
2008-11-24 14:53:14 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Optimized type-legalized selection DAG:\n");
|
2008-11-24 14:53:14 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
|
|
|
}
|
2009-05-23 12:35:30 +00:00
|
|
|
|
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("Vector Legalization", GroupName);
|
|
|
|
Changed = CurDAG->LegalizeVectors();
|
|
|
|
} else {
|
|
|
|
Changed = CurDAG->LegalizeVectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Changed) {
|
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("Type Legalization 2", GroupName);
|
|
|
|
Changed = CurDAG->LegalizeTypes();
|
|
|
|
} else {
|
|
|
|
Changed = CurDAG->LegalizeTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ViewDAGCombineLT)
|
|
|
|
CurDAG->viewGraph("dag-combine-lv input for " + BlockName);
|
|
|
|
|
|
|
|
// Run the DAG combiner in post-type-legalize mode.
|
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("DAG Combining after legalize vectors", GroupName);
|
|
|
|
CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
|
|
|
|
} else {
|
|
|
|
CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Optimized vector-legalized selection DAG:\n");
|
2009-05-23 12:35:30 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
|
|
|
}
|
2008-07-10 23:37:50 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName);
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-07-01 17:59:20 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
2008-07-14 18:19:29 +00:00
|
|
|
NamedRegionTimer T("DAG Legalization", GroupName);
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
} else {
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Legalize(DisableLegalizeTypes, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Legalized selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName);
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2005-10-10 16:47:10 +00:00
|
|
|
// Run the DAG combiner in post-legalize mode.
|
2008-07-01 17:59:20 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
2008-07-14 18:19:29 +00:00
|
|
|
NamedRegionTimer T("DAG Combining 2", GroupName);
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
} else {
|
2009-04-29 00:15:41 +00:00
|
|
|
CurDAG->Combine(NoIllegalOperations, *AA, OptLevel);
|
2008-07-01 17:59:20 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Optimized legalized selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2007-10-08 15:12:17 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-04-29 23:29:43 +00:00
|
|
|
if (OptLevel != CodeGenOpt::None)
|
2008-08-23 02:25:05 +00:00
|
|
|
ComputeLiveOutVRegInfo();
|
2006-04-28 02:09:19 +00:00
|
|
|
|
2005-03-30 01:10:47 +00:00
|
|
|
// Third, instruction select all of the operations to machine code, adding the
|
|
|
|
// code to the MachineBasicBlock.
|
2008-07-01 17:59:20 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
2008-07-14 18:19:29 +00:00
|
|
|
NamedRegionTimer T("Instruction Selection", GroupName);
|
2008-08-23 02:25:05 +00:00
|
|
|
InstructionSelect();
|
2008-07-01 17:59:20 +00:00
|
|
|
} else {
|
2008-08-23 02:25:05 +00:00
|
|
|
InstructionSelect();
|
2008-07-01 17:59:20 +00:00
|
|
|
}
|
2008-06-30 20:45:06 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Selected selection DAG:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(CurDAG->dump());
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName);
|
2008-07-21 20:00:07 +00:00
|
|
|
|
2008-07-14 18:19:29 +00:00
|
|
|
// Schedule machine code.
|
2009-02-11 04:27:20 +00:00
|
|
|
ScheduleDAGSDNodes *Scheduler = CreateScheduler();
|
2008-07-14 18:19:29 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("Instruction Scheduling", GroupName);
|
2009-02-11 04:27:20 +00:00
|
|
|
Scheduler->Run(CurDAG, BB, BB->end());
|
2008-07-14 18:19:29 +00:00
|
|
|
} else {
|
2009-02-11 04:27:20 +00:00
|
|
|
Scheduler->Run(CurDAG, BB, BB->end());
|
2008-07-14 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
2008-07-21 20:00:07 +00:00
|
|
|
if (ViewSUnitDAGs) Scheduler->viewGraph();
|
|
|
|
|
2009-09-20 02:20:51 +00:00
|
|
|
// Emit machine code to BB. This can change 'BB' to the last block being
|
2008-06-30 20:45:06 +00:00
|
|
|
// inserted into.
|
2008-07-01 17:59:20 +00:00
|
|
|
if (TimePassesIsEnabled) {
|
2008-07-14 18:19:29 +00:00
|
|
|
NamedRegionTimer T("Instruction Creation", GroupName);
|
2009-09-18 21:02:19 +00:00
|
|
|
BB = Scheduler->EmitSchedule(&SDL->EdgeMapping);
|
2008-07-01 17:59:20 +00:00
|
|
|
} else {
|
2009-09-18 21:02:19 +00:00
|
|
|
BB = Scheduler->EmitSchedule(&SDL->EdgeMapping);
|
2008-07-14 18:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free the scheduler state.
|
|
|
|
if (TimePassesIsEnabled) {
|
|
|
|
NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName);
|
|
|
|
delete Scheduler;
|
|
|
|
} else {
|
|
|
|
delete Scheduler;
|
2008-07-01 17:59:20 +00:00
|
|
|
}
|
2008-06-30 20:45:06 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Selected machine code:\n");
|
2005-01-07 07:47:53 +00:00
|
|
|
DEBUG(BB->dump());
|
2009-09-20 02:20:51 +00:00
|
|
|
}
|
2006-03-27 01:32:24 +00:00
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
|
|
|
|
MachineFunction &MF,
|
2008-10-14 23:54:11 +00:00
|
|
|
MachineModuleInfo *MMI,
|
2009-01-13 00:35:13 +00:00
|
|
|
DwarfWriter *DW,
|
2008-10-14 23:54:11 +00:00
|
|
|
const TargetInstrInfo &TII) {
|
2008-09-29 21:55:50 +00:00
|
|
|
// Initialize the Fast-ISel state, if needed.
|
|
|
|
FastISel *FastIS = 0;
|
|
|
|
if (EnableFastISel)
|
2009-01-15 19:20:50 +00:00
|
|
|
FastIS = TLI.createFastISel(MF, MMI, DW,
|
2008-09-29 21:55:50 +00:00
|
|
|
FuncInfo->ValueMap,
|
|
|
|
FuncInfo->MBBMap,
|
2008-10-14 23:54:11 +00:00
|
|
|
FuncInfo->StaticAllocaMap
|
|
|
|
#ifndef NDEBUG
|
|
|
|
, FuncInfo->CatchInfoLost
|
|
|
|
#endif
|
|
|
|
);
|
2008-09-29 21:55:50 +00:00
|
|
|
|
2009-09-28 21:41:20 +00:00
|
|
|
MetadataContext &TheMetadata = Fn.getContext().getMetadata();
|
2009-09-28 21:14:55 +00:00
|
|
|
unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
|
2009-09-16 20:39:11 +00:00
|
|
|
|
2008-09-29 21:55:50 +00:00
|
|
|
// Iterate over all basic blocks in the function.
|
2008-08-07 00:43:25 +00:00
|
|
|
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
|
|
|
|
BasicBlock *LLVMBB = &*I;
|
2008-08-27 23:52:12 +00:00
|
|
|
BB = FuncInfo->MBBMap[LLVMBB];
|
2008-08-23 02:25:05 +00:00
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
BasicBlock::iterator const Begin = LLVMBB->begin();
|
|
|
|
BasicBlock::iterator const End = LLVMBB->end();
|
2008-09-08 16:01:27 +00:00
|
|
|
BasicBlock::iterator BI = Begin;
|
2008-08-28 20:28:56 +00:00
|
|
|
|
|
|
|
// Lower any arguments needed in this block if this is the entry block.
|
2008-09-25 17:05:24 +00:00
|
|
|
bool SuppressFastISel = false;
|
|
|
|
if (LLVMBB == &Fn.getEntryBlock()) {
|
2008-08-28 20:28:56 +00:00
|
|
|
LowerArguments(LLVMBB);
|
2008-08-23 02:25:05 +00:00
|
|
|
|
2008-09-25 17:05:24 +00:00
|
|
|
// If any of the arguments has the byval attribute, forgo
|
|
|
|
// fast-isel in the entry block.
|
2008-09-29 21:55:50 +00:00
|
|
|
if (FastIS) {
|
2008-09-25 17:05:24 +00:00
|
|
|
unsigned j = 1;
|
|
|
|
for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
|
|
|
|
I != E; ++I, ++j)
|
2008-09-25 21:00:45 +00:00
|
|
|
if (Fn.paramHasAttr(j, Attribute::ByVal)) {
|
2008-09-25 17:21:42 +00:00
|
|
|
if (EnableFastISelVerbose || EnableFastISelAbort)
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "FastISel skips entry block due to byval argument\n";
|
2008-09-25 17:05:24 +00:00
|
|
|
SuppressFastISel = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 23:54:11 +00:00
|
|
|
if (MMI && BB->isLandingPad()) {
|
|
|
|
// Add a label to mark the beginning of the landing pad. Deletion of the
|
|
|
|
// landing pad can thus be detected via the MachineModuleInfo.
|
|
|
|
unsigned LabelID = MMI->addLandingPad(BB);
|
|
|
|
|
|
|
|
const TargetInstrDesc &II = TII.get(TargetInstrInfo::EH_LABEL);
|
2009-02-03 01:55:42 +00:00
|
|
|
BuildMI(BB, SDL->getCurDebugLoc(), II).addImm(LabelID);
|
2008-10-14 23:54:11 +00:00
|
|
|
|
|
|
|
// Mark exception register as live in.
|
|
|
|
unsigned Reg = TLI.getExceptionAddressRegister();
|
|
|
|
if (Reg) BB->addLiveIn(Reg);
|
|
|
|
|
|
|
|
// Mark exception selector register as live in.
|
|
|
|
Reg = TLI.getExceptionSelectorRegister();
|
|
|
|
if (Reg) BB->addLiveIn(Reg);
|
|
|
|
|
|
|
|
// FIXME: Hack around an exception handling flaw (PR1508): the personality
|
|
|
|
// function and list of typeids logically belong to the invoke (or, if you
|
|
|
|
// like, the basic block containing the invoke), and need to be associated
|
|
|
|
// with it in the dwarf exception handling tables. Currently however the
|
|
|
|
// information is provided by an intrinsic (eh.selector) that can be moved
|
|
|
|
// to unexpected places by the optimizers: if the unwind edge is critical,
|
|
|
|
// then breaking it can result in the intrinsics being in the successor of
|
|
|
|
// the landing pad, not the landing pad itself. This results in exceptions
|
|
|
|
// not being caught because no typeids are associated with the invoke.
|
|
|
|
// This may not be the only way things can go wrong, but it is the only way
|
|
|
|
// we try to work around for the moment.
|
|
|
|
BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator());
|
|
|
|
|
|
|
|
if (Br && Br->isUnconditional()) { // Critical edge?
|
|
|
|
BasicBlock::iterator I, E;
|
|
|
|
for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I)
|
|
|
|
if (isa<EHSelectorInst>(I))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
// No catch info found - try to extract some from the successor.
|
|
|
|
copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 02:25:05 +00:00
|
|
|
// Before doing SelectionDAG ISel, see if FastISel has been requested.
|
2008-10-14 23:54:11 +00:00
|
|
|
if (FastIS && !SuppressFastISel) {
|
2008-09-29 21:55:50 +00:00
|
|
|
// Emit code for any incoming arguments. This must happen before
|
|
|
|
// beginning FastISel on the entry block.
|
|
|
|
if (LLVMBB == &Fn.getEntryBlock()) {
|
|
|
|
CurDAG->setRoot(SDL->getControlRoot());
|
|
|
|
CodeGenAndEmitDAG();
|
|
|
|
SDL->clear();
|
|
|
|
}
|
2008-10-04 00:56:36 +00:00
|
|
|
FastIS->startNewBlock(BB);
|
2008-09-29 21:55:50 +00:00
|
|
|
// Do FastISel on as many instructions as possible.
|
|
|
|
for (; BI != End; ++BI) {
|
2009-09-20 02:20:51 +00:00
|
|
|
if (MDDbgKind) {
|
|
|
|
// Update DebugLoc if debug information is attached with this
|
|
|
|
// instruction.
|
2009-09-29 19:56:13 +00:00
|
|
|
if (MDNode *Dbg = TheMetadata.getMD(MDDbgKind, BI)) {
|
2009-09-20 02:20:51 +00:00
|
|
|
DILocation DILoc(Dbg);
|
|
|
|
DebugLoc Loc = ExtractDebugLocation(DILoc,
|
|
|
|
MF.getDebugLocInfo());
|
|
|
|
FastIS->setCurDebugLoc(Loc);
|
2009-10-06 00:09:08 +00:00
|
|
|
if (MF.getDefaultDebugLoc().isUnknown())
|
|
|
|
MF.setDefaultDebugLoc(Loc);
|
2009-09-20 02:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-16 20:39:11 +00:00
|
|
|
|
2008-09-29 21:55:50 +00:00
|
|
|
// Just before the terminator instruction, insert instructions to
|
|
|
|
// feed PHI nodes in successor blocks.
|
|
|
|
if (isa<TerminatorInst>(BI))
|
|
|
|
if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) {
|
2008-09-25 17:21:42 +00:00
|
|
|
if (EnableFastISelVerbose || EnableFastISelAbort) {
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "FastISel miss: ";
|
2008-09-25 17:21:42 +00:00
|
|
|
BI->dump();
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
assert(!EnableFastISelAbort &&
|
2009-07-12 20:07:01 +00:00
|
|
|
"FastISel didn't handle a PHI in a successor");
|
2008-09-29 21:55:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-09-25 17:05:24 +00:00
|
|
|
|
2008-09-29 21:55:50 +00:00
|
|
|
// First try normal tablegen-generated "fast" selection.
|
|
|
|
if (FastIS->SelectInstruction(BI))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Next, try calling the target to attempt to handle the instruction.
|
|
|
|
if (FastIS->TargetSelectInstruction(BI))
|
|
|
|
continue;
|
2008-08-23 02:25:05 +00:00
|
|
|
|
2008-09-29 21:55:50 +00:00
|
|
|
// Then handle certain instructions as single-LLVM-Instruction blocks.
|
|
|
|
if (isa<CallInst>(BI)) {
|
|
|
|
if (EnableFastISelVerbose || EnableFastISelAbort) {
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "FastISel missed call: ";
|
2008-09-29 21:55:50 +00:00
|
|
|
BI->dump();
|
2008-08-23 02:25:05 +00:00
|
|
|
}
|
|
|
|
|
2009-08-13 21:58:54 +00:00
|
|
|
if (BI->getType() != Type::getVoidTy(*CurDAG->getContext())) {
|
2008-09-29 21:55:50 +00:00
|
|
|
unsigned &R = FuncInfo->ValueMap[BI];
|
|
|
|
if (!R)
|
|
|
|
R = FuncInfo->CreateRegForValue(BI);
|
2008-08-23 02:25:05 +00:00
|
|
|
}
|
2008-09-29 21:55:50 +00:00
|
|
|
|
2009-04-16 01:33:10 +00:00
|
|
|
SDL->setCurDebugLoc(FastIS->getCurDebugLoc());
|
2008-09-29 21:55:50 +00:00
|
|
|
SelectBasicBlock(LLVMBB, BI, next(BI));
|
2008-10-04 00:56:36 +00:00
|
|
|
// If the instruction was codegen'd with multiple blocks,
|
|
|
|
// inform the FastISel object where to resume inserting.
|
|
|
|
FastIS->setCurrentBlock(BB);
|
2008-09-29 21:55:50 +00:00
|
|
|
continue;
|
2008-08-23 02:25:05 +00:00
|
|
|
}
|
2008-09-29 21:55:50 +00:00
|
|
|
|
|
|
|
// Otherwise, give up on FastISel for the rest of the block.
|
|
|
|
// For now, be a little lenient about non-branch terminators.
|
|
|
|
if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) {
|
|
|
|
if (EnableFastISelVerbose || EnableFastISelAbort) {
|
2009-08-23 07:05:07 +00:00
|
|
|
errs() << "FastISel miss: ";
|
2008-09-29 21:55:50 +00:00
|
|
|
BI->dump();
|
|
|
|
}
|
|
|
|
if (EnableFastISelAbort)
|
|
|
|
// The "fast" selector couldn't handle something and bailed.
|
|
|
|
// For the purpose of debugging, just abort.
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("FastISel didn't select the entire block");
|
2008-09-29 21:55:50 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-08-23 02:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-02 20:17:56 +00:00
|
|
|
// Run SelectionDAG instruction selection on the remainder of the block
|
|
|
|
// not handled by FastISel. If FastISel is not run, this is the entire
|
2008-09-03 23:12:08 +00:00
|
|
|
// block.
|
2009-04-16 01:33:10 +00:00
|
|
|
if (BI != End) {
|
|
|
|
// If FastISel is run and it has known DebugLoc then use it.
|
|
|
|
if (FastIS && !FastIS->getCurDebugLoc().isUnknown())
|
|
|
|
SDL->setCurDebugLoc(FastIS->getCurDebugLoc());
|
2008-09-08 16:01:27 +00:00
|
|
|
SelectBasicBlock(LLVMBB, BI, End);
|
2009-04-16 01:33:10 +00:00
|
|
|
}
|
2008-08-08 07:27:28 +00:00
|
|
|
|
2008-08-27 23:52:12 +00:00
|
|
|
FinishBasicBlock();
|
2008-08-07 00:43:25 +00:00
|
|
|
}
|
2008-09-29 21:55:50 +00:00
|
|
|
|
|
|
|
delete FastIS;
|
2008-07-07 23:02:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-28 21:51:04 +00:00
|
|
|
void
|
2008-08-27 23:52:12 +00:00
|
|
|
SelectionDAGISel::FinishBasicBlock() {
|
2005-01-07 07:47:53 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Target-post-processed machine code:\n");
|
2008-08-23 02:25:05 +00:00
|
|
|
DEBUG(BB->dump());
|
2007-04-09 12:31:58 +00:00
|
|
|
|
2009-08-23 06:35:02 +00:00
|
|
|
DEBUG(errs() << "Total amount of phi nodes to update: "
|
|
|
|
<< SDL->PHINodesToUpdate.size() << "\n");
|
2008-08-27 23:52:12 +00:00
|
|
|
DEBUG(for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i)
|
2009-08-23 06:35:02 +00:00
|
|
|
errs() << "Node " << i << " : ("
|
|
|
|
<< SDL->PHINodesToUpdate[i].first
|
|
|
|
<< ", " << SDL->PHINodesToUpdate[i].second << ")\n");
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2005-03-30 01:10:47 +00:00
|
|
|
// Next, now that we know what the last MBB the LLVM BB expanded is, update
|
2005-01-07 07:47:53 +00:00
|
|
|
// PHI nodes in successors.
|
2008-08-27 23:52:12 +00:00
|
|
|
if (SDL->SwitchCases.empty() &&
|
|
|
|
SDL->JTCases.empty() &&
|
|
|
|
SDL->BitTestCases.empty()) {
|
|
|
|
for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) {
|
|
|
|
MachineInstr *PHI = SDL->PHINodesToUpdate[i].first;
|
2006-03-27 01:32:24 +00:00
|
|
|
assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
|
|
|
|
"This is not a machine PHI node that we are updating!");
|
2008-08-27 23:52:12 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second,
|
2007-12-30 00:57:42 +00:00
|
|
|
false));
|
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(BB));
|
2006-03-27 01:32:24 +00:00
|
|
|
}
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->PHINodesToUpdate.clear();
|
2006-03-27 01:32:24 +00:00
|
|
|
return;
|
2005-01-07 07:47:53 +00:00
|
|
|
}
|
2007-04-09 12:31:58 +00:00
|
|
|
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned i = 0, e = SDL->BitTestCases.size(); i != e; ++i) {
|
2007-04-09 12:31:58 +00:00
|
|
|
// Lower header first, if it wasn't already lowered
|
2008-08-27 23:52:12 +00:00
|
|
|
if (!SDL->BitTestCases[i].Emitted) {
|
2007-04-09 12:31:58 +00:00
|
|
|
// Set the current basic block to the mbb we wish to insert the code into
|
2008-08-27 23:52:12 +00:00
|
|
|
BB = SDL->BitTestCases[i].Parent;
|
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2007-04-09 12:31:58 +00:00
|
|
|
// Emit the code
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitBitTestHeader(SDL->BitTestCases[i]);
|
|
|
|
CurDAG->setRoot(SDL->getRoot());
|
2008-08-23 02:25:05 +00:00
|
|
|
CodeGenAndEmitDAG();
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->clear();
|
2009-09-20 02:20:51 +00:00
|
|
|
}
|
2007-04-09 12:31:58 +00:00
|
|
|
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size(); j != ej; ++j) {
|
2007-04-09 12:31:58 +00:00
|
|
|
// Set the current basic block to the mbb we wish to insert the code into
|
2008-08-27 23:52:12 +00:00
|
|
|
BB = SDL->BitTestCases[i].Cases[j].ThisBB;
|
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2007-04-09 12:31:58 +00:00
|
|
|
// Emit the code
|
|
|
|
if (j+1 != ej)
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitBitTestCase(SDL->BitTestCases[i].Cases[j+1].ThisBB,
|
|
|
|
SDL->BitTestCases[i].Reg,
|
|
|
|
SDL->BitTestCases[i].Cases[j]);
|
2007-04-09 12:31:58 +00:00
|
|
|
else
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitBitTestCase(SDL->BitTestCases[i].Default,
|
|
|
|
SDL->BitTestCases[i].Reg,
|
|
|
|
SDL->BitTestCases[i].Cases[j]);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
|
|
|
|
2008-08-27 23:52:12 +00:00
|
|
|
CurDAG->setRoot(SDL->getRoot());
|
2008-08-23 02:25:05 +00:00
|
|
|
CodeGenAndEmitDAG();
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->clear();
|
2007-04-09 12:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update PHI Nodes
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) {
|
|
|
|
MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first;
|
2007-04-09 12:31:58 +00:00
|
|
|
MachineBasicBlock *PHIBB = PHI->getParent();
|
|
|
|
assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
|
|
|
|
"This is not a machine PHI node that we are updating!");
|
|
|
|
// This is "default" BB. We have two jumps to it. From "header" BB and
|
|
|
|
// from last "case" BB.
|
2008-08-27 23:52:12 +00:00
|
|
|
if (PHIBB == SDL->BitTestCases[i].Default) {
|
|
|
|
PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
|
2007-12-30 00:57:42 +00:00
|
|
|
false));
|
2008-08-27 23:52:12 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Parent));
|
|
|
|
PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
|
2007-12-30 00:57:42 +00:00
|
|
|
false));
|
2008-08-27 23:52:12 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Cases.
|
2007-12-30 00:57:42 +00:00
|
|
|
back().ThisBB));
|
2007-04-09 12:31:58 +00:00
|
|
|
}
|
|
|
|
// One of "cases" BB.
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size();
|
|
|
|
j != ej; ++j) {
|
|
|
|
MachineBasicBlock* cBB = SDL->BitTestCases[i].Cases[j].ThisBB;
|
2007-04-09 12:31:58 +00:00
|
|
|
if (cBB->succ_end() !=
|
|
|
|
std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) {
|
2008-08-27 23:52:12 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second,
|
2007-12-30 00:57:42 +00:00
|
|
|
false));
|
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(cBB));
|
2007-04-09 12:31:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->BitTestCases.clear();
|
2007-04-09 12:31:58 +00:00
|
|
|
|
2006-04-23 06:26:20 +00:00
|
|
|
// If the JumpTable record is filled in, then we need to emit a jump table.
|
|
|
|
// Updating the PHI nodes is tricky in this case, since we need to determine
|
|
|
|
// whether the PHI is a successor of the range check MBB or the jump table MBB
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned i = 0, e = SDL->JTCases.size(); i != e; ++i) {
|
2007-03-25 15:07:15 +00:00
|
|
|
// Lower header first, if it wasn't already lowered
|
2008-08-27 23:52:12 +00:00
|
|
|
if (!SDL->JTCases[i].first.Emitted) {
|
2007-03-25 15:07:15 +00:00
|
|
|
// Set the current basic block to the mbb we wish to insert the code into
|
2008-08-27 23:52:12 +00:00
|
|
|
BB = SDL->JTCases[i].first.HeaderBB;
|
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2007-03-25 15:07:15 +00:00
|
|
|
// Emit the code
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitJumpTableHeader(SDL->JTCases[i].second, SDL->JTCases[i].first);
|
|
|
|
CurDAG->setRoot(SDL->getRoot());
|
2008-08-23 02:25:05 +00:00
|
|
|
CodeGenAndEmitDAG();
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->clear();
|
2007-04-09 12:31:58 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
// Set the current basic block to the mbb we wish to insert the code into
|
2008-08-27 23:52:12 +00:00
|
|
|
BB = SDL->JTCases[i].second.MBB;
|
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2006-04-22 18:53:45 +00:00
|
|
|
// Emit the code
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitJumpTable(SDL->JTCases[i].second);
|
|
|
|
CurDAG->setRoot(SDL->getRoot());
|
2008-08-23 02:25:05 +00:00
|
|
|
CodeGenAndEmitDAG();
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->clear();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
// Update PHI Nodes
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) {
|
|
|
|
MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first;
|
2006-04-22 18:53:45 +00:00
|
|
|
MachineBasicBlock *PHIBB = PHI->getParent();
|
|
|
|
assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
|
|
|
|
"This is not a machine PHI node that we are updating!");
|
2007-04-09 12:31:58 +00:00
|
|
|
// "default" BB. We can go there only from header BB.
|
2008-08-27 23:52:12 +00:00
|
|
|
if (PHIBB == SDL->JTCases[i].second.Default) {
|
2009-09-19 09:51:03 +00:00
|
|
|
PHI->addOperand
|
|
|
|
(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, false));
|
|
|
|
PHI->addOperand
|
|
|
|
(MachineOperand::CreateMBB(SDL->JTCases[i].first.HeaderBB));
|
2006-05-03 03:48:02 +00:00
|
|
|
}
|
2007-04-09 12:31:58 +00:00
|
|
|
// JT BB. Just iterate over successors here
|
2006-05-03 03:48:02 +00:00
|
|
|
if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) {
|
2009-09-19 09:51:03 +00:00
|
|
|
PHI->addOperand
|
|
|
|
(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, false));
|
2007-12-30 00:57:42 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(BB));
|
2006-04-22 18:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->JTCases.clear();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-22 23:00:53 +00:00
|
|
|
// If the switch block involved a branch to one of the actual successors, we
|
|
|
|
// need to update PHI nodes in that block.
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) {
|
|
|
|
MachineInstr *PHI = SDL->PHINodesToUpdate[i].first;
|
2006-10-22 23:00:53 +00:00
|
|
|
assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
|
|
|
|
"This is not a machine PHI node that we are updating!");
|
|
|
|
if (BB->isSuccessor(PHI->getParent())) {
|
2008-08-27 23:52:12 +00:00
|
|
|
PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second,
|
2007-12-30 00:57:42 +00:00
|
|
|
false));
|
|
|
|
PHI->addOperand(MachineOperand::CreateMBB(BB));
|
2006-10-22 23:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-03-27 01:32:24 +00:00
|
|
|
// If we generated any switch lowering information, build and codegen any
|
|
|
|
// additional DAGs necessary.
|
2008-08-27 23:52:12 +00:00
|
|
|
for (unsigned i = 0, e = SDL->SwitchCases.size(); i != e; ++i) {
|
2006-03-27 01:32:24 +00:00
|
|
|
// Set the current basic block to the mbb we wish to insert the code into
|
2009-09-18 21:02:19 +00:00
|
|
|
MachineBasicBlock *ThisBB = BB = SDL->SwitchCases[i].ThisBB;
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->setCurrentBasicBlock(BB);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-03-27 01:32:24 +00:00
|
|
|
// Emit the code
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->visitSwitchCase(SDL->SwitchCases[i]);
|
|
|
|
CurDAG->setRoot(SDL->getRoot());
|
2008-08-23 02:25:05 +00:00
|
|
|
CodeGenAndEmitDAG();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-09-07 01:59:34 +00:00
|
|
|
// Handle any PHI nodes in successors of this chunk, as if we were coming
|
|
|
|
// from the original BB before switch expansion. Note that PHI nodes can
|
|
|
|
// occur multiple times in PHINodesToUpdate. We have to be very careful to
|
|
|
|
// handle them the right number of times.
|
2008-08-27 23:52:12 +00:00
|
|
|
while ((BB = SDL->SwitchCases[i].TrueBB)) { // Handle LHS and RHS.
|
2009-09-18 21:02:19 +00:00
|
|
|
// If new BB's are created during scheduling, the edges may have been
|
2009-09-19 09:51:03 +00:00
|
|
|
// updated. That is, the edge from ThisBB to BB may have been split and
|
|
|
|
// BB's predecessor is now another block.
|
2009-09-18 21:02:19 +00:00
|
|
|
DenseMap<MachineBasicBlock*, MachineBasicBlock*>::iterator EI =
|
|
|
|
SDL->EdgeMapping.find(BB);
|
|
|
|
if (EI != SDL->EdgeMapping.end())
|
|
|
|
ThisBB = EI->second;
|
2006-09-07 01:59:34 +00:00
|
|
|
for (MachineBasicBlock::iterator Phi = BB->begin();
|
|
|
|
Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){
|
|
|
|
// This value for this PHI node is recorded in PHINodesToUpdate, get it.
|
|
|
|
for (unsigned pn = 0; ; ++pn) {
|
2008-08-27 23:52:12 +00:00
|
|
|
assert(pn != SDL->PHINodesToUpdate.size() &&
|
|
|
|
"Didn't find PHI entry!");
|
2009-09-18 08:26:06 +00:00
|
|
|
if (SDL->PHINodesToUpdate[pn].first == Phi) {
|
|
|
|
Phi->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pn].
|
|
|
|
second, false));
|
2009-09-18 21:02:19 +00:00
|
|
|
Phi->addOperand(MachineOperand::CreateMBB(ThisBB));
|
2006-09-07 01:59:34 +00:00
|
|
|
break;
|
2009-09-18 08:26:06 +00:00
|
|
|
}
|
2006-09-07 01:59:34 +00:00
|
|
|
}
|
2006-03-27 01:32:24 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-09-07 01:59:34 +00:00
|
|
|
// Don't process RHS if same block as LHS.
|
2008-08-27 23:52:12 +00:00
|
|
|
if (BB == SDL->SwitchCases[i].FalseBB)
|
|
|
|
SDL->SwitchCases[i].FalseBB = 0;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-09-07 01:59:34 +00:00
|
|
|
// If we haven't handled the RHS, do so now. Otherwise, we're done.
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->SwitchCases[i].TrueBB = SDL->SwitchCases[i].FalseBB;
|
|
|
|
SDL->SwitchCases[i].FalseBB = 0;
|
2006-03-27 01:32:24 +00:00
|
|
|
}
|
2008-08-27 23:52:12 +00:00
|
|
|
assert(SDL->SwitchCases[i].TrueBB == 0 && SDL->SwitchCases[i].FalseBB == 0);
|
2009-09-18 21:02:19 +00:00
|
|
|
SDL->clear();
|
2005-03-30 01:10:47 +00:00
|
|
|
}
|
2008-08-27 23:52:12 +00:00
|
|
|
SDL->SwitchCases.clear();
|
|
|
|
|
|
|
|
SDL->PHINodesToUpdate.clear();
|
2005-01-07 07:47:53 +00:00
|
|
|
}
|
2006-01-21 02:32:06 +00:00
|
|
|
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2009-02-06 18:26:51 +00:00
|
|
|
/// Create the scheduler. If a specific scheduler was specified
|
|
|
|
/// via the SchedulerRegistry, use it, otherwise select the
|
|
|
|
/// one preferred by the target.
|
2008-07-14 18:19:29 +00:00
|
|
|
///
|
2009-02-11 04:27:20 +00:00
|
|
|
ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() {
|
2006-08-02 12:30:23 +00:00
|
|
|
RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-08-01 14:21:23 +00:00
|
|
|
if (!Ctor) {
|
2006-08-02 12:30:23 +00:00
|
|
|
Ctor = ISHeuristic;
|
2006-08-01 19:14:14 +00:00
|
|
|
RegisterScheduler::setDefault(Ctor);
|
2006-01-23 07:01:07 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-04-29 00:15:41 +00:00
|
|
|
return Ctor(this, OptLevel);
|
2006-01-21 02:32:06 +00:00
|
|
|
}
|
2006-02-24 02:13:54 +00:00
|
|
|
|
2009-01-15 22:18:12 +00:00
|
|
|
ScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
|
|
|
|
return new ScheduleHazardRecognizer();
|
2006-08-01 18:29:48 +00:00
|
|
|
}
|
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions used by the generated instruction selector.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Calls to these methods are generated by tblgen.
|
|
|
|
|
|
|
|
/// CheckAndMask - The isel is trying to match something like (and X, 255). If
|
|
|
|
/// the dag combiner simplified the 255, we still want to match. RHS is the
|
|
|
|
/// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value
|
|
|
|
/// specified in the .td file (e.g. 255).
|
2009-09-20 02:20:51 +00:00
|
|
|
bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS,
|
2007-07-24 23:00:27 +00:00
|
|
|
int64_t DesiredMaskS) const {
|
2008-02-25 21:11:39 +00:00
|
|
|
const APInt &ActualMask = RHS->getAPIntValue();
|
|
|
|
const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// If the actual mask exactly matches, success!
|
|
|
|
if (ActualMask == DesiredMask)
|
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// If the actual AND mask is allowing unallowed bits, this doesn't match.
|
2008-02-25 21:11:39 +00:00
|
|
|
if (ActualMask.intersects(~DesiredMask))
|
2006-10-11 03:58:02 +00:00
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// Otherwise, the DAG Combiner may have proven that the value coming in is
|
|
|
|
// either already zero or is not demanded. Check for known zero input bits.
|
2008-02-25 21:11:39 +00:00
|
|
|
APInt NeededMask = DesiredMask & ~ActualMask;
|
2007-06-22 14:59:07 +00:00
|
|
|
if (CurDAG->MaskedValueIsZero(LHS, NeededMask))
|
2006-10-11 03:58:02 +00:00
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// TODO: check to see if missing bits are just not demanded.
|
|
|
|
|
|
|
|
// Otherwise, this pattern doesn't match.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckOrMask - The isel is trying to match something like (or X, 255). If
|
|
|
|
/// the dag combiner simplified the 255, we still want to match. RHS is the
|
|
|
|
/// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value
|
|
|
|
/// specified in the .td file (e.g. 255).
|
2009-09-20 02:20:51 +00:00
|
|
|
bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS,
|
2008-02-25 21:11:39 +00:00
|
|
|
int64_t DesiredMaskS) const {
|
|
|
|
const APInt &ActualMask = RHS->getAPIntValue();
|
|
|
|
const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// If the actual mask exactly matches, success!
|
|
|
|
if (ActualMask == DesiredMask)
|
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// If the actual AND mask is allowing unallowed bits, this doesn't match.
|
2008-02-25 21:11:39 +00:00
|
|
|
if (ActualMask.intersects(~DesiredMask))
|
2006-10-11 03:58:02 +00:00
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// Otherwise, the DAG Combiner may have proven that the value coming in is
|
|
|
|
// either already zero or is not demanded. Check for known zero input bits.
|
2008-02-25 21:11:39 +00:00
|
|
|
APInt NeededMask = DesiredMask & ~ActualMask;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-02-25 21:11:39 +00:00
|
|
|
APInt KnownZero, KnownOne;
|
2007-06-22 14:59:07 +00:00
|
|
|
CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// If all the missing bits in the or are already known to be set, match!
|
|
|
|
if ((NeededMask & KnownOne) == NeededMask)
|
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// TODO: check to see if missing bits are just not demanded.
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-10-11 03:58:02 +00:00
|
|
|
// Otherwise, this pattern doesn't match.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-08-01 18:29:48 +00:00
|
|
|
|
2006-02-24 02:13:54 +00:00
|
|
|
/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
|
|
|
|
/// by tblgen. Others should not call it.
|
|
|
|
void SelectionDAGISel::
|
2008-08-23 02:25:05 +00:00
|
|
|
SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) {
|
2008-07-27 21:46:04 +00:00
|
|
|
std::vector<SDValue> InOps;
|
2006-02-24 02:13:54 +00:00
|
|
|
std::swap(InOps, Ops);
|
|
|
|
|
|
|
|
Ops.push_back(InOps[0]); // input chain.
|
|
|
|
Ops.push_back(InOps[1]); // input asm string.
|
|
|
|
|
|
|
|
unsigned i = 2, e = InOps.size();
|
2009-08-11 20:47:22 +00:00
|
|
|
if (InOps[e-1].getValueType() == MVT::Flag)
|
2006-02-24 02:13:54 +00:00
|
|
|
--e; // Don't process a flag operand if it is here.
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-02-24 02:13:54 +00:00
|
|
|
while (i != e) {
|
2008-09-12 16:56:44 +00:00
|
|
|
unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue();
|
2008-09-24 01:07:17 +00:00
|
|
|
if ((Flags & 7) != 4 /*MEM*/) {
|
2006-02-24 02:13:54 +00:00
|
|
|
// Just skip over this operand, copying the operands verbatim.
|
2009-03-20 18:03:34 +00:00
|
|
|
Ops.insert(Ops.end(), InOps.begin()+i,
|
|
|
|
InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1);
|
|
|
|
i += InlineAsm::getNumOperandRegisters(Flags) + 1;
|
2006-02-24 02:13:54 +00:00
|
|
|
} else {
|
2009-03-20 18:03:34 +00:00
|
|
|
assert(InlineAsm::getNumOperandRegisters(Flags) == 1 &&
|
|
|
|
"Memory operand with multiple values?");
|
2006-02-24 02:13:54 +00:00
|
|
|
// Otherwise, this is a memory operand. Ask the target to select it.
|
2008-07-27 21:46:04 +00:00
|
|
|
std::vector<SDValue> SelOps;
|
2008-08-23 02:25:05 +00:00
|
|
|
if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps)) {
|
2009-07-11 13:10:19 +00:00
|
|
|
llvm_report_error("Could not match memory address. Inline asm"
|
|
|
|
" failure!");
|
2006-02-24 02:13:54 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-02-24 02:13:54 +00:00
|
|
|
// Add this to the output node.
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT IntPtrTy = TLI.getPointerTy();
|
2008-09-24 01:07:17 +00:00
|
|
|
Ops.push_back(CurDAG->getTargetConstant(4/*MEM*/ | (SelOps.size()<< 3),
|
2008-08-23 02:25:05 +00:00
|
|
|
IntPtrTy));
|
2006-02-24 02:13:54 +00:00
|
|
|
Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2006-02-24 02:13:54 +00:00
|
|
|
// Add the flag input back if present.
|
|
|
|
if (e != InOps.size())
|
|
|
|
Ops.push_back(InOps.back());
|
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2009-08-10 22:56:29 +00:00
|
|
|
/// findFlagUse - Return use of EVT::Flag value produced by the specified
|
2009-05-08 18:51:58 +00:00
|
|
|
/// SDNode.
|
|
|
|
///
|
|
|
|
static SDNode *findFlagUse(SDNode *N) {
|
|
|
|
unsigned FlagResNo = N->getNumValues()-1;
|
|
|
|
for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
|
|
|
|
SDUse &Use = I.getUse();
|
|
|
|
if (Use.getResNo() == FlagResNo)
|
|
|
|
return Use.getUser();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// findNonImmUse - Return true if "Use" is a non-immediate use of "Def".
|
|
|
|
/// This function recursively traverses up the operand chain, ignoring
|
|
|
|
/// certain nodes.
|
|
|
|
static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
|
|
|
|
SDNode *Root,
|
|
|
|
SmallPtrSet<SDNode*, 16> &Visited) {
|
|
|
|
if (Use->getNodeId() < Def->getNodeId() ||
|
|
|
|
!Visited.insert(Use))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
|
|
|
|
SDNode *N = Use->getOperand(i).getNode();
|
|
|
|
if (N == Def) {
|
|
|
|
if (Use == ImmedUse || Use == Root)
|
|
|
|
continue; // We are not looking for immediate use.
|
|
|
|
assert(N != Root);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse up the operand chain.
|
|
|
|
if (findNonImmUse(N, Def, ImmedUse, Root, Visited))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isNonImmUse - Start searching from Root up the DAG to check is Def can
|
|
|
|
/// be reached. Return true if that's the case. However, ignore direct uses
|
|
|
|
/// by ImmedUse (which would be U in the example illustrated in
|
|
|
|
/// IsLegalAndProfitableToFold) and by Root (which can happen in the store
|
|
|
|
/// case).
|
|
|
|
/// FIXME: to be really generic, we should allow direct use by any node
|
|
|
|
/// that is being folded. But realisticly since we only fold loads which
|
|
|
|
/// have one non-chain use, we only need to watch out for load/op/store
|
|
|
|
/// and load/op/cmp case where the root (store / cmp) may reach the load via
|
|
|
|
/// its chain operand.
|
|
|
|
static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) {
|
|
|
|
SmallPtrSet<SDNode*, 16> Visited;
|
|
|
|
return findNonImmUse(Root, Def, ImmedUse, Root, Visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsLegalAndProfitableToFold - Returns true if the specific operand node N of
|
|
|
|
/// U can be folded during instruction selection that starts at Root and
|
|
|
|
/// folding N is profitable.
|
|
|
|
bool SelectionDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
|
|
|
|
SDNode *Root) const {
|
|
|
|
if (OptLevel == CodeGenOpt::None) return false;
|
|
|
|
|
|
|
|
// If Root use can somehow reach N through a path that that doesn't contain
|
|
|
|
// U then folding N would create a cycle. e.g. In the following
|
|
|
|
// diagram, Root can reach N through X. If N is folded into into Root, then
|
|
|
|
// X is both a predecessor and a successor of U.
|
|
|
|
//
|
|
|
|
// [N*] //
|
|
|
|
// ^ ^ //
|
|
|
|
// / \ //
|
|
|
|
// [U*] [X]? //
|
|
|
|
// ^ ^ //
|
|
|
|
// \ / //
|
|
|
|
// \ / //
|
|
|
|
// [Root*] //
|
|
|
|
//
|
|
|
|
// * indicates nodes to be folded together.
|
|
|
|
//
|
|
|
|
// If Root produces a flag, then it gets (even more) interesting. Since it
|
|
|
|
// will be "glued" together with its flag use in the scheduler, we need to
|
|
|
|
// check if it might reach N.
|
|
|
|
//
|
|
|
|
// [N*] //
|
|
|
|
// ^ ^ //
|
|
|
|
// / \ //
|
|
|
|
// [U*] [X]? //
|
|
|
|
// ^ ^ //
|
|
|
|
// \ \ //
|
|
|
|
// \ | //
|
|
|
|
// [Root*] | //
|
|
|
|
// ^ | //
|
|
|
|
// f | //
|
|
|
|
// | / //
|
|
|
|
// [Y] / //
|
|
|
|
// ^ / //
|
|
|
|
// f / //
|
|
|
|
// | / //
|
|
|
|
// [FU] //
|
|
|
|
//
|
|
|
|
// If FU (flag use) indirectly reaches N (the load), and Root folds N
|
|
|
|
// (call it Fold), then X is a predecessor of FU and a successor of
|
|
|
|
// Fold. But since Fold and FU are flagged together, this will create
|
|
|
|
// a cycle in the scheduling graph.
|
|
|
|
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT VT = Root->getValueType(Root->getNumValues()-1);
|
2009-08-11 20:47:22 +00:00
|
|
|
while (VT == MVT::Flag) {
|
2009-05-08 18:51:58 +00:00
|
|
|
SDNode *FU = findFlagUse(Root);
|
|
|
|
if (FU == NULL)
|
|
|
|
break;
|
|
|
|
Root = FU;
|
|
|
|
VT = Root->getValueType(Root->getNumValues()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !isNonImmUse(Root, N, U);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char SelectionDAGISel::ID = 0;
|