74f540a8da1fd06efc2126c84c2301b8983ce186
[oota-llvm.git] / lib / Target / X86 / InstSelectPattern.cpp
1 //===-- InstSelectPattern.cpp - A pattern matching inst selector for X86 --===//
2 //
3 // This file defines a pattern matching instruction selector for X86.
4 //
5 //  FIXME: we could allocate one big array of unsigneds to use as the backing
6 //         store for all of the nodes costs arrays.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "X86.h"
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"
18
19 #include "X86RegisterInfo.h"
20
21 // Include the generated instruction selector...
22 #include "X86GenInstrSelector.inc"
23
24 namespace {
25   struct ISel : public FunctionPass, SelectionDAGTargetBuilder {
26     TargetMachine &TM;
27     ISel(TargetMachine &tm) : TM(tm) {}
28     int VarArgsFrameIndex;              // FrameIndex for start of varargs area
29
30     bool runOnFunction(Function &Fn) {
31       MachineFunction &MF = MachineFunction::construct(&Fn, TM);
32       SelectionDAG DAG(MF, TM, *this);
33
34       std::cerr << "\n\n\n=== "
35                 << DAG.getMachineFunction().getFunction()->getName() << "\n";
36
37       DAG.dump();
38       X86ISel(DAG).generateCode();
39       std::cerr << "\n\n\n";
40       return true;
41     }
42
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);
48   };
49 }
50
51
52 void ISel::expandArguments(SelectionDAG &SD) {
53
54   // Add DAG nodes to load the arguments...  On entry to a function on the X86,
55   // the stack frame looks like this:
56   //
57   // [ESP] -- return address
58   // [ESP + 4] -- first argument (leftmost lexically)
59   // [ESP + 8] -- second argument, if first argument is four bytes in size
60   //    ... 
61   //
62   MachineFunction &F = SD.getMachineFunction();
63   MachineFrameInfo *MFI = F.getFrameInfo();
64   const Function &Fn = *F.getFunction();
65   
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;
70     unsigned ObjSize;
71     switch (ObjectVT) {
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;
79     }
80     // Create the frame index object for this incoming parameter...
81     int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
82     
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));
86
87     SelectionDAGNode *Arg
88       = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN);
89
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
93     // the function
94     SD.addNode(FIN);
95     SD.addNodeForValue(Arg, I);
96
97     ArgOffset += ArgIncrement;   // Move on to the next argument...
98   }
99
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);
104 }
105
106 void ISel::expandCall(SelectionDAG &SD, CallInst &CI) {
107   assert(0 && "ISel::expandCall not implemented!");
108 }
109
110
111 /// createX86PatternInstructionSelector - This pass converts an LLVM function
112 /// into a machine code representation using pattern matching and a machine
113 /// description file.
114 ///
115 FunctionPass *createX86PatternInstructionSelector(TargetMachine &TM) {
116   return new ISel(TM);  
117 }