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 //===----------------------------------------------------------------------===//
31 struct ISel : public FunctionPass, SelectionDAGTargetBuilder {
33 ISel(TargetMachine &tm) : TM(tm) {}
34 int VarArgsFrameIndex; // FrameIndex for start of varargs area
36 bool runOnFunction(Function &Fn) {
37 MachineFunction &MF = MachineFunction::construct(&Fn, TM);
38 SelectionDAG DAG(MF, TM, *this);
40 std::cerr << "\n\n\n=== "
41 << DAG.getMachineFunction().getFunction()->getName() << "\n";
44 X86ISel(DAG).generateCode();
45 std::cerr << "\n\n\n";
49 public: // Implementation of the SelectionDAGTargetBuilder class...
50 /// expandArguments - Add nodes to the DAG to indicate how to load arguments
51 /// off of the X86 stack.
52 void expandArguments(SelectionDAG &SD, MachineFunction &MF);
57 void ISel::expandArguments(SelectionDAG &SD, MachineFunction &F) {
58 // Add DAG nodes to load the arguments... On entry to a function on the X86,
59 // the stack frame looks like this:
61 // [ESP] -- return address
62 // [ESP + 4] -- first argument (leftmost lexically)
63 // [ESP + 8] -- second argument, if first argument is four bytes in size
66 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
67 MachineFrameInfo *MFI = F.getFrameInfo();
68 const Function &Fn = *F.getFunction();
70 for (Function::const_aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
71 MVT::ValueType ObjectVT = SD.getValueType(I->getType());
72 unsigned ArgIncrement = 4;
75 default: assert(0 && "Unhandled argument type!");
76 case MVT::i8: ObjSize = 1; break;
77 case MVT::i16: ObjSize = 2; break;
78 case MVT::i32: ObjSize = 4; break;
79 case MVT::i64: ObjSize = ArgIncrement = 8; break;
80 case MVT::f32: ObjSize = 4; break;
81 case MVT::f64: ObjSize = ArgIncrement = 8; break;
83 // Create the frame index object for this incoming parameter...
84 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
86 // Create the SelectionDAG nodes corresponding to a load from this parameter
88 SelectionDAGNode *FIN = new SelectionDAGNode(ISD::FrameIndex, MVT::i32);
89 FIN->addValue(new ReducedValue_FrameIndex_i32(FI));
92 = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN);
94 // Add the SelectionDAGNodes to the SelectionDAG... note that there is no
95 // reason to add chain nodes here. We know that no loads ore stores will
96 // ever alias these loads, so we are free to perform the load at any time in
99 SD.addNodeForValue(Arg, I);
101 ArgOffset += ArgIncrement; // Move on to the next argument...
104 // If the function takes variable number of arguments, make a frame index for
105 // the start of the first vararg value... for expansion of llvm.va_start.
106 if (Fn.getFunctionType()->isVarArg())
107 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
111 /// createX86PatternInstructionSelector - This pass converts an LLVM function
112 /// into a machine code representation using pattern matching and a machine
113 /// description file.
115 Pass *createX86PatternInstructionSelector(TargetMachine &TM) {