blob: 434ceee91c66463d85f6434780a9610df3462520 [file] [log] [blame]
Chris Lattnerac0c8682003-08-11 14:59:22 +00001//===-- InstSelectPattern.cpp - A pattern matching inst selector for X86 --===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerac0c8682003-08-11 14:59:22 +00009//
10// This file defines a pattern matching instruction selector for X86.
11//
12// FIXME: we could allocate one big array of unsigneds to use as the backing
13// store for all of the nodes costs arrays.
14//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
18#include "llvm/Pass.h"
19#include "llvm/Function.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/SSARegMap.h"
25
26#include "X86RegisterInfo.h"
27
28// Include the generated instruction selector...
29#include "X86GenInstrSelector.inc"
30
Chris Lattnerac0c8682003-08-11 14:59:22 +000031namespace {
32 struct ISel : public FunctionPass, SelectionDAGTargetBuilder {
33 TargetMachine &TM;
34 ISel(TargetMachine &tm) : TM(tm) {}
35 int VarArgsFrameIndex; // FrameIndex for start of varargs area
36
37 bool runOnFunction(Function &Fn) {
38 MachineFunction &MF = MachineFunction::construct(&Fn, TM);
39 SelectionDAG DAG(MF, TM, *this);
40
41 std::cerr << "\n\n\n=== "
42 << DAG.getMachineFunction().getFunction()->getName() << "\n";
43
44 DAG.dump();
45 X86ISel(DAG).generateCode();
46 std::cerr << "\n\n\n";
47 return true;
48 }
49
50 public: // Implementation of the SelectionDAGTargetBuilder class...
51 /// expandArguments - Add nodes to the DAG to indicate how to load arguments
52 /// off of the X86 stack.
Chris Lattnerf3c274d2003-08-15 04:51:59 +000053 void expandArguments(SelectionDAG &SD);
54 void expandCall(SelectionDAG &SD, CallInst &CI);
Chris Lattnerac0c8682003-08-11 14:59:22 +000055 };
56}
57
58
Chris Lattnerf3c274d2003-08-15 04:51:59 +000059void ISel::expandArguments(SelectionDAG &SD) {
60
Chris Lattnerac0c8682003-08-11 14:59:22 +000061 // Add DAG nodes to load the arguments... On entry to a function on the X86,
62 // the stack frame looks like this:
63 //
64 // [ESP] -- return address
65 // [ESP + 4] -- first argument (leftmost lexically)
66 // [ESP + 8] -- second argument, if first argument is four bytes in size
67 // ...
68 //
Chris Lattnerf3c274d2003-08-15 04:51:59 +000069 MachineFunction &F = SD.getMachineFunction();
Chris Lattnerac0c8682003-08-11 14:59:22 +000070 MachineFrameInfo *MFI = F.getFrameInfo();
71 const Function &Fn = *F.getFunction();
72
Chris Lattnerf3c274d2003-08-15 04:51:59 +000073 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnerac0c8682003-08-11 14:59:22 +000074 for (Function::const_aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
75 MVT::ValueType ObjectVT = SD.getValueType(I->getType());
76 unsigned ArgIncrement = 4;
77 unsigned ObjSize;
78 switch (ObjectVT) {
79 default: assert(0 && "Unhandled argument type!");
80 case MVT::i8: ObjSize = 1; break;
81 case MVT::i16: ObjSize = 2; break;
82 case MVT::i32: ObjSize = 4; break;
83 case MVT::i64: ObjSize = ArgIncrement = 8; break;
84 case MVT::f32: ObjSize = 4; break;
85 case MVT::f64: ObjSize = ArgIncrement = 8; break;
86 }
87 // Create the frame index object for this incoming parameter...
88 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
89
90 // Create the SelectionDAG nodes corresponding to a load from this parameter
Chris Lattnerac0c8682003-08-11 14:59:22 +000091 SelectionDAGNode *FIN = new SelectionDAGNode(ISD::FrameIndex, MVT::i32);
92 FIN->addValue(new ReducedValue_FrameIndex_i32(FI));
93
94 SelectionDAGNode *Arg
95 = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN);
96
97 // Add the SelectionDAGNodes to the SelectionDAG... note that there is no
98 // reason to add chain nodes here. We know that no loads ore stores will
99 // ever alias these loads, so we are free to perform the load at any time in
100 // the function
101 SD.addNode(FIN);
102 SD.addNodeForValue(Arg, I);
103
104 ArgOffset += ArgIncrement; // Move on to the next argument...
105 }
106
107 // If the function takes variable number of arguments, make a frame index for
108 // the start of the first vararg value... for expansion of llvm.va_start.
109 if (Fn.getFunctionType()->isVarArg())
110 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
111}
112
Chris Lattnerf3c274d2003-08-15 04:51:59 +0000113void ISel::expandCall(SelectionDAG &SD, CallInst &CI) {
114 assert(0 && "ISel::expandCall not implemented!");
115}
116
Chris Lattnerac0c8682003-08-11 14:59:22 +0000117
118/// createX86PatternInstructionSelector - This pass converts an LLVM function
119/// into a machine code representation using pattern matching and a machine
120/// description file.
121///
Brian Gaeke19df3872003-08-13 18:18:15 +0000122FunctionPass *createX86PatternInstructionSelector(TargetMachine &TM) {
Chris Lattnerac0c8682003-08-11 14:59:22 +0000123 return new ISel(TM);
124}