blob: 4b9e381ec173fadf20d8074727390fb69689b801 [file] [log] [blame]
Chris Lattnerac0c8682003-08-11 14:59:22 +00001//===-- 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
25//===----------------------------------------------------------------------===//
26// User code
27//
28
29
30namespace {
31 struct ISel : public FunctionPass, SelectionDAGTargetBuilder {
32 TargetMachine &TM;
33 ISel(TargetMachine &tm) : TM(tm) {}
34 int VarArgsFrameIndex; // FrameIndex for start of varargs area
35
36 bool runOnFunction(Function &Fn) {
37 MachineFunction &MF = MachineFunction::construct(&Fn, TM);
38 SelectionDAG DAG(MF, TM, *this);
39
40 std::cerr << "\n\n\n=== "
41 << DAG.getMachineFunction().getFunction()->getName() << "\n";
42
43 DAG.dump();
44 X86ISel(DAG).generateCode();
45 std::cerr << "\n\n\n";
46 return true;
47 }
48
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);
53 };
54}
55
56
57void 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:
60 //
61 // [ESP] -- return address
62 // [ESP + 4] -- first argument (leftmost lexically)
63 // [ESP + 8] -- second argument, if first argument is four bytes in size
64 // ...
65 //
66 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
67 MachineFrameInfo *MFI = F.getFrameInfo();
68 const Function &Fn = *F.getFunction();
69
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;
73 unsigned ObjSize;
74 switch (ObjectVT) {
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;
82 }
83 // Create the frame index object for this incoming parameter...
84 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
85
86 // Create the SelectionDAG nodes corresponding to a load from this parameter
87 // FIXME:
88 SelectionDAGNode *FIN = new SelectionDAGNode(ISD::FrameIndex, MVT::i32);
89 FIN->addValue(new ReducedValue_FrameIndex_i32(FI));
90
91 SelectionDAGNode *Arg
92 = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN);
93
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
97 // the function
98 SD.addNode(FIN);
99 SD.addNodeForValue(Arg, I);
100
101 ArgOffset += ArgIncrement; // Move on to the next argument...
102 }
103
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);
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///
115Pass *createX86PatternInstructionSelector(TargetMachine &TM) {
116 return new ISel(TM);
117}