llir-opt
0.0.1
Low-Level Post-Link Optimiser for OCaml and C
|
7 #include <llvm/IR/CallingConv.h>
8 #include <llvm/CodeGen/MachineRegisterInfo.h>
9 #include <llvm/CodeGen/SelectionDAG.h>
10 #include <llvm/CodeGen/SelectionDAG/ScheduleDAGSDNodes.h>
11 #include <llvm/CodeGen/SelectionDAGNodes.h>
13 #include "core/analysis/live_variables.h"
14 #include "core/insts.h"
15 #include "core/type.h"
16 #include "emitter/isel_mapping.h"
17 #include "emitter/call_lowering.h"
28 using MVT = llvm::MVT;
29 using EVT = llvm::EVT;
30 using SDNode = llvm::SDNode;
31 using SDValue = llvm::SDValue;
32 using SDVTList = llvm::SDVTList;
33 using SelectionDAG = llvm::SelectionDAG;
34 using GlobalValue = llvm::GlobalValue;
42 llvm::TargetLibraryInfo &libInfo,
43 llvm::CodeGenOpt::Level ol
48 llvm::StringRef getPassName()
const override;
50 void getAnalysisUsage(llvm::AnalysisUsage &AU)
const override;
52 bool doInitialization(llvm::Module &M)
override;
54 bool runOnModule(llvm::Module &M)
override;
62 virtual void Lower(llvm::MachineFunction &mf) = 0;
70 virtual void LowerClone(
const CloneInst *inst) = 0;
72 virtual void LowerReturn(
const ReturnInst *inst) = 0;
74 virtual void LowerRaise(
const RaiseInst *inst) = 0;
76 virtual void LowerSpawn(
const SpawnInst *inst) = 0;
80 virtual void LowerSet(
const SetInst *inst) = 0;
88 virtual llvm::SelectionDAG &
GetDAG()
const = 0;
98 virtual void Select(SDNode *node) = 0;
101 virtual bool Finalize(llvm::MachineFunction &MF) {
return true; }
113 std::pair<llvm::SDValue, llvm::SDValue>
117 const ReturnInst *ret,
118 llvm::SmallVectorImpl<SDValue> &ops
121 std::pair<llvm::SDValue, llvm::SDValue>
125 const RaiseInst *ret,
126 llvm::SmallVectorImpl<llvm::Register> ®s,
132 const LandingPadInst *inst
135 using RegParts = llvm::SmallVector<std::pair<llvm::Register, llvm::MVT>, 2>;
136 using ExportList = std::vector<std::pair<RegParts, SDValue>>;
161 llvm::ArrayRef<llvm::Register> inputs,
162 llvm::ArrayRef<llvm::Register> clobbers,
163 llvm::ArrayRef<llvm::Register> outputs,
164 SDValue glue = SDValue()
168 SDValue
LowerImm(
const APInt &val, Type type);
170 SDValue
LowerImm(
const APFloat &val, Type type);
182 llvm::ISD::CondCode
GetCond(Cond cc);
185 using FrameExports = std::vector<std::pair<ConstRef<Inst>, llvm::SDValue>>;
213 [[noreturn]]
void Error(
const Inst *i,
const std::string_view &message);
215 [[noreturn]]
void Error(
const Func *f,
const std::string_view &message);
235 void LowerUnary(
const UnaryInst *inst,
unsigned opcode);
243 void LowerLD(
const LoadInst *inst);
245 void LowerST(
const StoreInst *inst);
273 void LowerALUO(
const BinaryInst *inst,
unsigned op);
281 virtual void LowerCallSite(llvm::SDValue chain,
const CallSite *call) = 0;
290 const CallSite *call,
292 llvm::SmallVectorImpl<std::pair<unsigned, SDValue>> ®s
299 const CallSite *call,
300 llvm::SmallVectorImpl<CallLowering::RetLoc> &returns,
301 llvm::SmallVectorImpl<SDValue> ®s,
327 llvm::CodeGenOpt::Level
ol_;
338 std::unique_ptr<LiveVariables>
lva_;
340 std::unordered_map<ConstRef<Inst>, llvm::SDValue>
values_;
342 std::unordered_map<ConstRef<Inst>, RegParts>
regs_;
std::unordered_map< ConstRef< Inst >, RegParts > pendingValueInsts_
Pending value-producing instructions to be exported.
Definition: isel.h:353
virtual void LowerArch(const Inst *inst)=0
Lowers a target-specific instruction.
const Target & target_
Reference to the target.
Definition: isel.h:307
void LowerUnary(const UnaryInst *inst, unsigned opcode)
Lowers a unary instruction.
Definition: isel.cpp:2143
void LowerBitCast(const BitCastInst *inst)
Lowers a bit cast instruction.
bool IsExported(ConstRef< Inst > inst)
Check if the value is exported from its defining block.
Definition: isel.cpp:1588
void LowerALUO(const BinaryInst *inst, unsigned op)
Lowers an overflow check instruction.
Definition: isel.cpp:2615
virtual void LowerClone(const CloneInst *inst)=0
Lowers a process clone instruction.
std::unordered_map< ConstRef< Inst >, RegParts > pendingPrimInsts_
Pending primitive instructions to be exported.
Definition: isel.h:351
void LowerUndef(const UndefInst *inst)
Lowers an undefined instruction.
Definition: isel.cpp:2609
llvm::SDValue LowerGlobal(const Global &val, Type type)
Lowers an offset reference to a global.
Definition: isel.cpp:771
virtual void LowerLandingPad(const LandingPadInst *inst)=0
Lowers a landing pad.
void LowerFrameCall(const FrameCallInst *inst)
Lowers a frame call instruction.
Definition: isel.cpp:2055
void LowerShift(const Inst *inst, unsigned op)
Lowers a shift instruction.
Definition: isel.cpp:2097
llvm::SDValue LowerGCFrame(llvm::SDValue chain, llvm::SDValue glue, const CallSite *inst)
Lower a GC frame.
Definition: isel.cpp:1521
virtual SDValue GetRegArch(Register reg)=0
Reads the value from an architecture-specific register.
SDValue LowerExpr(const Expr &expr, Type type)
Lowers an expression value.
Definition: isel.cpp:1444
MVT GetPointerType() const
Return the pointer type.
Definition: isel.cpp:1634
void DoInstructionSelection()
Creates a MachineBasicBlock with MachineInstrs.
Definition: isel.cpp:1880
void LowerFExt(const FExtInst *inst)
Lowers a float extend instruction.
Definition: isel.cpp:2488
llvm::ISD::CondCode GetCond(Cond cc)
Converts a condition code.
Definition: isel.cpp:1456
std::unique_ptr< LiveVariables > lva_
Per-function live variable info.
Definition: isel.h:338
llvm::Type * voidTy_
Void type.
Definition: isel.h:321
llvm::MachineBasicBlock * MBB_
Current basic block.
Definition: isel.h:334
void LowerDebugTrap(const DebugTrapInst *inst)
Lowers a debug trap instruction.
Definition: isel.cpp:2378
void Lower(const Inst *inst)
Lowers an instruction.
Definition: isel.cpp:554
Definition: isel_mapping.h:22
Definition: call_lowering.h:21
void LowerST(const StoreInst *inst)
Lowers a store.
Definition: isel.cpp:2329
virtual void LowerReturn(const ReturnInst *inst)=0
Lowers a return.
void LowerFrame(const FrameInst *inst)
Lowers a frame instruction.
Definition: isel.cpp:2346
llvm::SDLoc SDL_
Dummy SelectionDAG debug location.
Definition: isel.h:316
virtual void LowerSyscall(const SyscallInst *inst)=0
Lowers a system call instruction.
void LowerTrunc(const TruncInst *inst)
Lowers a truncate instruction.
Definition: isel.cpp:2508
const Prog & prog_
Program to lower.
Definition: isel.h:309
void LowerTailCall(const TailCallInst *inst)
Lowers a tail call instruction.
Definition: isel.cpp:2002
std::unordered_map< ConstRef< Inst >, llvm::SDValue > values_
Mapping from nodes to values.
Definition: isel.h:340
ISel(char &ID, const Target &target, const Prog &prog, llvm::TargetLibraryInfo &libInfo, llvm::CodeGenOpt::Level ol)
Initialises the instruction selector.
Definition: isel.cpp:94
virtual llvm::Register GetStackRegister() const =0
Returns the stack pointer.
std::pair< llvm::SDValue, llvm::SDValue > LowerRets(llvm::SDValue chain, const CallLowering &lowering, const ReturnInst *ret, llvm::SmallVectorImpl< SDValue > &ops)
Lower all return values.
Definition: isel.cpp:909
std::unordered_map< ConstRef< Inst >, RegParts > regs_
Mapping from nodes to registers.
Definition: isel.h:342
virtual void LowerSpawn(const SpawnInst *inst)=0
Lowers a spawn instruction.
llvm::Module * M_
Current module.
Definition: isel.h:319
virtual void PrepareFunction(const Func &func, llvm::MachineFunction &MF)
Prepare a function.
Definition: isel.h:203
virtual bool Finalize(llvm::MachineFunction &MF)
Finalize the lowering.
Definition: isel.h:101
void LowerMov(const MovInst *inst)
Lowers a mov instruction.
Definition: isel.cpp:2385
void LowerTrap(const TrapInst *inst)
Lowers a trap instruction.
Definition: isel.cpp:2371
llvm::MachineBasicBlock::iterator insert_
Current insertion point.
Definition: isel.h:336
void LowerInvoke(const InvokeInst *inst)
Lowers an invoke instruction.
Definition: isel.cpp:2008
std::pair< llvm::SDValue, llvm::SDValue > LowerRaises(llvm::SDValue chain, const CallLowering &lowering, const RaiseInst *ret, llvm::SmallVectorImpl< llvm::Register > ®s, llvm::SDValue glue)
Lower all return values.
Definition: isel.cpp:954
void LowerSwitch(const SwitchInst *inst)
Lowers a switch.
Definition: isel.cpp:2243
virtual void LowerRaise(const RaiseInst *inst)=0
Lowers an indirect jump.
void LowerSelect(const SelectInst *inst)
Lowers a select instruction.
Definition: isel.cpp:2576
std::pair< SDValue, SDValue > LowerReturns(SDValue chain, SDValue inFlag, const CallSite *call, llvm::SmallVectorImpl< CallLowering::RetLoc > &returns, llvm::SmallVectorImpl< SDValue > ®s, llvm::SmallVectorImpl< std::pair< ConstRef< Inst >, SDValue >> &values)
Lower values returned from a call.
Definition: isel.cpp:2820
void LowerCmp(const CmpInst *inst)
Lowers a comparison instruction.
Definition: isel.cpp:2352
void Export(ConstRef< Inst > inst, llvm::SDValue val)
Exports a value.
Definition: isel.cpp:757
llvm::FunctionType * funcTy_
Dummy function type.
Definition: isel.h:325
void LowerCall(const CallInst *inst)
Lowers a call instructions.
Definition: isel.cpp:1972
virtual llvm::SelectionDAG & GetDAG() const =0
Returns a reference to the current DAG.
void LowerXExt(const XExtInst *inst)
Lowers a any extend instruction.
Definition: isel.cpp:2469
void LowerGet(const GetInst *inst)
Lowers a fixed register get instruction.
Definition: isel.cpp:2632
virtual void Select(SDNode *node)=0
Target-specific instruction selection.
std::pair< bool, llvm::CallingConv::ID > GetCallingConv(const Func *caller, const CallSite *call)
Find the calling convention of a call.
Definition: isel.cpp:1643
SDValue LoadReg(Register reg)
Lovers a register value.
llvm::Type * i8PtrTy_
Void pointer type.
Definition: isel.h:323
void Error(const Inst *i, const std::string_view &message)
Report an error at an instruction.
Definition: isel.cpp:1935
SDValue LowerImm(const APInt &val, Type type)
Lowers an immediate to a SDValue.
Definition: isel.cpp:1314
virtual void PreprocessISelDAG()=0
Target-specific preprocessing step.
llvm::DebugLoc DL_
Dummy debug location.
Definition: isel.h:314
void LowerAlloca(const AllocaInst *inst)
Lowers an alloca instruction.
Definition: isel.cpp:2547
RegParts ExportValue(SDValue value)
Copies a value to a vreg to be exported later.
Definition: isel.cpp:1207
void LowerZExt(const ZExtInst *inst)
Lowers a zero extend instruction.
Definition: isel.cpp:2442
void LowerJUMP_COND(const JumpCondInst *inst)
Lowers a conditional jump true instruction.
Definition: isel.cpp:2154
virtual void LowerArguments(bool hasVAStart)=0
Lowers variable argument list frame setup.
virtual void LowerSet(const SetInst *inst)=0
Lowers a fixed register set instruction.
SDValue LowerConstant(ConstRef< Inst > inst)
Returns a constant if the instruction introduces one.
Definition: isel.cpp:1380
void LowerJUMP(const JumpInst *inst)
Lowers a jump instruction.
Definition: isel.cpp:2223
RegParts AssignVReg(ConstRef< Inst > inst)
Creates a register for an instruction's result.
Definition: isel.cpp:1182
void LowerSExt(const SExtInst *inst)
Lowers a sign extend instruction.
Definition: isel.cpp:2414
virtual void PostprocessISelDAG()=0
Target-specific post-processing step.
llvm::CodeGenOpt::Level ol_
Chosen optimisation level.
Definition: isel.h:327
ConstRef< Value > GetMoveArg(ConstRef< MovInst > inst)
Follow move arguments to a non-move instruction.
Definition: isel.cpp:1579
SDValue GetExportRoot()
Flushes all pending exports.
Definition: isel.cpp:1086
llvm::DenseMap< unsigned, unsigned > stackIndices_
Mapping from stack_object indices to llvm stack objects.
Definition: isel.h:344
void LowerArgs(const CallLowering &lowering)
Lowers all arguments.
Definition: isel.cpp:823
SDValue LowerCallArguments(SDValue chain, const CallSite *call, CallLowering &ci, llvm::SmallVectorImpl< std::pair< unsigned, SDValue >> ®s)
Lower the arguments to a call.
Definition: isel.cpp:2700
void LowerPad(const CallLowering &lowering, const LandingPadInst *inst)
Lowers a landing pad.
Definition: isel.cpp:998
std::vector< std::pair< RegParts, llvm::SDValue > > pendingPrimValues_
Pending primitives to be exported.
Definition: isel.h:349
bool HasPendingExports()
Checks if there are any pending exports.
Definition: isel.cpp:1167
FrameExports GetFrameExport(const Inst *frame)
Get the relevant vars for a GC frame.
Definition: isel.cpp:1484
virtual void LowerCallSite(llvm::SDValue chain, const CallSite *call)=0
Lowers a call instruction.
SDValue GetValueExportRoot()
Flushes pending exports which are OCaml values.
Definition: isel.cpp:1073
SDValue GetPrimitiveExportRoot()
Flushes pending exports that are not OCaml values.
Definition: isel.cpp:1056
void LowerVAStart(const VaStartInst *inst)
Lowers a vararg frame setup instruction.
Definition: isel.cpp:1954
llvm::TargetLibraryInfo & libInfo_
Target library info.
Definition: isel.h:311
void LowerBinary(const Inst *inst, unsigned op)
Lowers a binary instruction.
Definition: isel.cpp:2085
SDValue LowerInlineAsm(unsigned opcode, SDValue chain, const char *code, unsigned flags, llvm::ArrayRef< llvm::Register > inputs, llvm::ArrayRef< llvm::Register > clobbers, llvm::ArrayRef< llvm::Register > outputs, SDValue glue=SDValue())
Lower an inline asm sequence.
Definition: isel.cpp:1229
int frameIndex_
Frame start index, if necessary.
Definition: isel.h:346
std::vector< std::pair< ConstRef< Inst >, llvm::SDValue > > FrameExports
Vector of exported values from the frame.
Definition: isel.h:185
void HandleSuccessorPHI(const Block *block)
Handle PHI nodes in successor blocks.
Definition: isel.cpp:1686
const Func * func_
Current function.
Definition: isel.h:330
llvm::Function * F_
Current LLVM function.
Definition: isel.h:332
SDValue GetValue(ConstRef< Inst > inst)
Looks up an existing value.
Definition: isel.cpp:709
void LowerLD(const LoadInst *inst)
Lowers a load.
Definition: isel.cpp:2306
void CodeGenAndEmitDAG()
Prepares the dag for instruction selection.
Definition: isel.cpp:1802