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