1 //===-- InstSelectPattern.cpp - A pattern matching inst selector for X86 --===//
3 // This file defines a pattern matching instruction selector for X86.
5 // FIXME: we could allocate one big array of unsigneds to use as the backing
6 // store for all of the nodes costs arrays.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/Pass.h"
12 #include "llvm/Function.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/SSARegMap.h"
19 #include "X86RegisterInfo.h"
21 // Include the generated instruction selector...
22 #include "X86GenInstrSelector.inc"
25 struct ISel : public FunctionPass, SelectionDAGTargetBuilder {
27 ISel(TargetMachine &tm) : TM(tm) {}
28 int VarArgsFrameIndex; // FrameIndex for start of varargs area
30 bool runOnFunction(Function &Fn) {
31 MachineFunction &MF = MachineFunction::construct(&Fn, TM);
32 SelectionDAG DAG(MF, TM, *this);
34 std::cerr << "\n\n\n=== "
35 << DAG.getMachineFunction().getFunction()->getName() << "\n";
38 X86ISel(DAG).generateCode();
39 std::cerr << "\n\n\n";
43 public: // Implementation of the SelectionDAGTargetBuilder class...
44 /// expandArguments - Add nodes to the DAG to indicate how to load arguments
45 /// off of the X86 stack.
46 void expandArguments(SelectionDAG &SD);
47 void expandCall(SelectionDAG &SD, CallInst &CI);
52 void ISel::expandArguments(SelectionDAG &SD) {
54 // Add DAG nodes to load the arguments... On entry to a function on the X86,
55 // the stack frame looks like this:
57 // [ESP] -- return address
58 // [ESP + 4] -- first argument (leftmost lexically)
59 // [ESP + 8] -- second argument, if first argument is four bytes in size
62 MachineFunction &F = SD.getMachineFunction();
63 MachineFrameInfo *MFI = F.getFrameInfo();
64 const Function &Fn = *F.getFunction();
66 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
67 for (Function::const_aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
68 MVT::ValueType ObjectVT = SD.getValueType(I->getType());
69 unsigned ArgIncrement = 4;
72 default: assert(0 && "Unhandled argument type!");
73 case MVT::i8: ObjSize = 1; break;
74 case MVT::i16: ObjSize = 2; break;
75 case MVT::i32: ObjSize = 4; break;
76 case MVT::i64: ObjSize = ArgIncrement = 8; break;
77 case MVT::f32: ObjSize = 4; break;
78 case MVT::f64: ObjSize = ArgIncrement = 8; break;
80 // Create the frame index object for this incoming parameter...
81 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
83 // Create the SelectionDAG nodes corresponding to a load from this parameter
84 SelectionDAGNode *FIN = new SelectionDAGNode(ISD::FrameIndex, MVT::i32);
85 FIN->addValue(new ReducedValue_FrameIndex_i32(FI));
88 = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN);
90 // Add the SelectionDAGNodes to the SelectionDAG... note that there is no
91 // reason to add chain nodes here. We know that no loads ore stores will
92 // ever alias these loads, so we are free to perform the load at any time in
95 SD.addNodeForValue(Arg, I);
97 ArgOffset += ArgIncrement; // Move on to the next argument...
100 // If the function takes variable number of arguments, make a frame index for
101 // the start of the first vararg value... for expansion of llvm.va_start.
102 if (Fn.getFunctionType()->isVarArg())
103 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
106 void ISel::expandCall(SelectionDAG &SD, CallInst &CI) {
107 assert(0 && "ISel::expandCall not implemented!");
111 /// createX86PatternInstructionSelector - This pass converts an LLVM function
112 /// into a machine code representation using pattern matching and a machine
113 /// description file.
115 FunctionPass *createX86PatternInstructionSelector(TargetMachine &TM) {