Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 1 | //===-- InstSelectPattern.cpp - A pattern matching inst selector for X86 --===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 9 | // |
| 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" |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 25 | #include "X86RegisterInfo.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 26 | #include <iostream> |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 27 | |
| 28 | // Include the generated instruction selector... |
| 29 | #include "X86GenInstrSelector.inc" |
Chris Lattner | 4482715 | 2003-12-28 09:47:19 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | struct ISel : public FunctionPass, SelectionDAGTargetBuilder { |
| 34 | TargetMachine &TM; |
| 35 | ISel(TargetMachine &tm) : TM(tm) {} |
| 36 | int VarArgsFrameIndex; // FrameIndex for start of varargs area |
| 37 | |
| 38 | bool runOnFunction(Function &Fn) { |
| 39 | MachineFunction &MF = MachineFunction::construct(&Fn, TM); |
| 40 | SelectionDAG DAG(MF, TM, *this); |
| 41 | |
| 42 | std::cerr << "\n\n\n=== " |
| 43 | << DAG.getMachineFunction().getFunction()->getName() << "\n"; |
| 44 | |
| 45 | DAG.dump(); |
| 46 | X86ISel(DAG).generateCode(); |
| 47 | std::cerr << "\n\n\n"; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | public: // Implementation of the SelectionDAGTargetBuilder class... |
| 52 | /// expandArguments - Add nodes to the DAG to indicate how to load arguments |
| 53 | /// off of the X86 stack. |
Chris Lattner | f3c274d | 2003-08-15 04:51:59 +0000 | [diff] [blame] | 54 | void expandArguments(SelectionDAG &SD); |
| 55 | void expandCall(SelectionDAG &SD, CallInst &CI); |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 56 | }; |
| 57 | } |
| 58 | |
| 59 | |
Chris Lattner | f3c274d | 2003-08-15 04:51:59 +0000 | [diff] [blame] | 60 | void ISel::expandArguments(SelectionDAG &SD) { |
| 61 | |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 62 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 63 | // the stack frame looks like this: |
| 64 | // |
| 65 | // [ESP] -- return address |
| 66 | // [ESP + 4] -- first argument (leftmost lexically) |
| 67 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
| 68 | // ... |
| 69 | // |
Chris Lattner | f3c274d | 2003-08-15 04:51:59 +0000 | [diff] [blame] | 70 | MachineFunction &F = SD.getMachineFunction(); |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 71 | MachineFrameInfo *MFI = F.getFrameInfo(); |
| 72 | const Function &Fn = *F.getFunction(); |
| 73 | |
Chris Lattner | f3c274d | 2003-08-15 04:51:59 +0000 | [diff] [blame] | 74 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 75 | for (Function::const_aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) { |
| 76 | MVT::ValueType ObjectVT = SD.getValueType(I->getType()); |
| 77 | unsigned ArgIncrement = 4; |
| 78 | unsigned ObjSize; |
| 79 | switch (ObjectVT) { |
| 80 | default: assert(0 && "Unhandled argument type!"); |
| 81 | case MVT::i8: ObjSize = 1; break; |
| 82 | case MVT::i16: ObjSize = 2; break; |
| 83 | case MVT::i32: ObjSize = 4; break; |
| 84 | case MVT::i64: ObjSize = ArgIncrement = 8; break; |
| 85 | case MVT::f32: ObjSize = 4; break; |
| 86 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 87 | } |
| 88 | // Create the frame index object for this incoming parameter... |
| 89 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 90 | |
| 91 | // Create the SelectionDAG nodes corresponding to a load from this parameter |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 92 | SelectionDAGNode *FIN = new SelectionDAGNode(ISD::FrameIndex, MVT::i32); |
| 93 | FIN->addValue(new ReducedValue_FrameIndex_i32(FI)); |
| 94 | |
| 95 | SelectionDAGNode *Arg |
| 96 | = new SelectionDAGNode(ISD::Load, ObjectVT, F.begin(), FIN); |
| 97 | |
| 98 | // Add the SelectionDAGNodes to the SelectionDAG... note that there is no |
| 99 | // reason to add chain nodes here. We know that no loads ore stores will |
| 100 | // ever alias these loads, so we are free to perform the load at any time in |
| 101 | // the function |
| 102 | SD.addNode(FIN); |
| 103 | SD.addNodeForValue(Arg, I); |
| 104 | |
| 105 | ArgOffset += ArgIncrement; // Move on to the next argument... |
| 106 | } |
| 107 | |
| 108 | // If the function takes variable number of arguments, make a frame index for |
| 109 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 110 | if (Fn.getFunctionType()->isVarArg()) |
| 111 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
| 112 | } |
| 113 | |
Chris Lattner | f3c274d | 2003-08-15 04:51:59 +0000 | [diff] [blame] | 114 | void ISel::expandCall(SelectionDAG &SD, CallInst &CI) { |
| 115 | assert(0 && "ISel::expandCall not implemented!"); |
| 116 | } |
| 117 | |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 118 | /// createX86PatternInstructionSelector - This pass converts an LLVM function |
| 119 | /// into a machine code representation using pattern matching and a machine |
| 120 | /// description file. |
| 121 | /// |
Chris Lattner | f70e0c2 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 122 | FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) { |
Chris Lattner | ac0c868 | 2003-08-11 14:59:22 +0000 | [diff] [blame] | 123 | return new ISel(TM); |
| 124 | } |