Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 11 | // Magic number generation for integer divide from the PowerPC Compiler Writer's |
| 12 | // Guide, section 3.2.3.5 |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "PowerPC.h" |
| 17 | #include "PowerPCInstrBuilder.h" |
| 18 | #include "PowerPCInstrInfo.h" |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 19 | #include "PPC32TargetMachine.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetLowering.h" |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include <set> |
| 35 | #include <algorithm> |
| 36 | using namespace llvm; |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface |
| 40 | namespace { |
| 41 | class PPC32TargetLowering : public TargetLowering { |
| 42 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
| 43 | int ReturnAddrIndex; // FrameIndex for return slot. |
| 44 | public: |
| 45 | PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
Chris Lattner | 9bce0f9 | 2005-05-12 02:06:00 +0000 | [diff] [blame] | 46 | // Fold away setcc operations if possible. |
| 47 | setSetCCIsExpensive(); |
| 48 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 49 | // Set up the register classes. |
| 50 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 51 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 52 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 53 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 54 | // PowerPC has no intrinsics for these particular operations |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 55 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 56 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 57 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 58 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 59 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 60 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 61 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 62 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 63 | // PowerPC has no SREM/UREM instructions |
| 64 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 65 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Chris Lattner | 43fdea0 | 2005-04-02 05:03:24 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 67 | // We don't support sin/cos/sqrt/fmod |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 68 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 69 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 70 | setOperationAction(ISD::SREM , MVT::f64, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 71 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 72 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 73 | setOperationAction(ISD::SREM , MVT::f32, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 74 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 75 | // If we're enabling GP optimizations, use hardware square root |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 76 | if (!GPOPT) { |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 77 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 78 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 79 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 80 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 81 | //PowerPC does not have CTPOP or CTTZ |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 82 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 83 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 84 | |
Chris Lattner | cbd06fc | 2005-04-07 19:41:49 +0000 | [diff] [blame] | 85 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 86 | addLegalFPImmediate(+0.0); // Necessary for FSEL |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 87 | addLegalFPImmediate(-0.0); // |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 88 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 89 | computeRegisterProperties(); |
| 90 | } |
| 91 | |
| 92 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 93 | /// lower the arguments for the specified function, into the specified DAG. |
| 94 | virtual std::vector<SDOperand> |
| 95 | LowerArguments(Function &F, SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 96 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 97 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 98 | /// actual call. |
| 99 | virtual std::pair<SDOperand, SDOperand> |
Chris Lattner | c57f682 | 2005-05-12 19:56:45 +0000 | [diff] [blame] | 100 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 101 | bool isTailCall, SDOperand Callee, ArgListTy &Args, |
| 102 | SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 103 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 104 | virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 105 | Value *VAListV, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 106 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 107 | virtual std::pair<SDOperand,SDOperand> |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 108 | LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 109 | const Type *ArgTy, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 110 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 111 | virtual std::pair<SDOperand, SDOperand> |
| 112 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 113 | SelectionDAG &DAG); |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | std::vector<SDOperand> |
| 119 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 120 | // |
| 121 | // add beautiful description of PPC stack frame format, or at least some docs |
| 122 | // |
| 123 | MachineFunction &MF = DAG.getMachineFunction(); |
| 124 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 125 | MachineBasicBlock& BB = MF.front(); |
| 126 | std::vector<SDOperand> ArgValues; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 127 | |
| 128 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 129 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 130 | // handle tracking them for us. |
| 131 | std::vector<unsigned> argVR, argPR, argOp; |
| 132 | unsigned ArgOffset = 24; |
| 133 | unsigned GPR_remaining = 8; |
| 134 | unsigned FPR_remaining = 13; |
| 135 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 136 | static const unsigned GPR[] = { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 137 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 138 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 139 | }; |
| 140 | static const unsigned FPR[] = { |
| 141 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 142 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 143 | }; |
| 144 | |
| 145 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 146 | // the arguments start at offset 24, although they are likely to be passed |
| 147 | // in registers. |
| 148 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 149 | SDOperand newroot, argt; |
| 150 | unsigned ObjSize; |
| 151 | bool needsLoad = false; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 152 | bool ArgLive = !I->use_empty(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 153 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 154 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 155 | switch (ObjectVT) { |
| 156 | default: assert(0 && "Unhandled argument type!"); |
| 157 | case MVT::i1: |
| 158 | case MVT::i8: |
| 159 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 160 | case MVT::i32: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 161 | ObjSize = 4; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 162 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 163 | if (GPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 164 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 165 | argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 166 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 167 | if (ObjectVT != MVT::i32) |
| 168 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 169 | } else { |
| 170 | needsLoad = true; |
| 171 | } |
| 172 | break; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 173 | case MVT::i64: ObjSize = 8; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 174 | if (!ArgLive) break; |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 175 | if (GPR_remaining > 0) { |
| 176 | SDOperand argHi, argLo; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 177 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 178 | argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
| 179 | // If we have two or more remaining argument registers, then both halves |
| 180 | // of the i64 can be sourced from there. Otherwise, the lower half will |
| 181 | // have to come off the stack. This can happen when an i64 is preceded |
| 182 | // by 28 bytes of arguments. |
| 183 | if (GPR_remaining > 1) { |
| 184 | MF.addLiveIn(GPR[GPR_idx+1]); |
| 185 | argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi); |
| 186 | } else { |
| 187 | int FI = MFI->CreateFixedObject(4, ArgOffset+4); |
| 188 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 189 | argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 190 | DAG.getSrcValue(NULL)); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 191 | } |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 192 | // Build the outgoing arg thingy |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 193 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 194 | newroot = argLo; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 195 | } else { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 196 | needsLoad = true; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 197 | } |
| 198 | break; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 199 | case MVT::f32: |
| 200 | case MVT::f64: |
| 201 | ObjSize = (ObjectVT == MVT::f64) ? 8 : 4; |
| 202 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 203 | if (FPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 204 | MF.addLiveIn(FPR[FPR_idx]); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 205 | argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT, |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 206 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 207 | --FPR_remaining; |
| 208 | ++FPR_idx; |
| 209 | } else { |
| 210 | needsLoad = true; |
| 211 | } |
| 212 | break; |
| 213 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 214 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 215 | // We need to load the argument to a virtual register if we determined above |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 216 | // that we ran out of physical registers of the appropriate type |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 217 | if (needsLoad) { |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 218 | unsigned SubregOffset = 0; |
Nate Begeman | c3e2db4 | 2005-04-04 09:09:00 +0000 | [diff] [blame] | 219 | if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3; |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 220 | if (ObjectVT == MVT::i16) SubregOffset = 2; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 221 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 222 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 223 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 224 | DAG.getConstant(SubregOffset, MVT::i32)); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 225 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 226 | DAG.getSrcValue(NULL)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 227 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 228 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 229 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 230 | // argument passing. |
| 231 | if (GPR_remaining > 0) { |
| 232 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 233 | GPR_remaining -= delta; |
| 234 | GPR_idx += delta; |
| 235 | } |
| 236 | ArgOffset += ObjSize; |
Chris Lattner | 91277ea | 2005-04-09 21:23:24 +0000 | [diff] [blame] | 237 | if (newroot.Val) |
| 238 | DAG.setRoot(newroot.getValue(1)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 239 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 240 | ArgValues.push_back(argt); |
| 241 | } |
| 242 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 243 | // If the function takes variable number of arguments, make a frame index for |
| 244 | // the start of the first vararg value... for expansion of llvm.va_start. |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 245 | if (F.isVarArg()) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 246 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 247 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 248 | // If this function is vararg, store any remaining integer argument regs |
| 249 | // to their spots on the stack so that they may be loaded by deferencing the |
| 250 | // result of va_next. |
| 251 | std::vector<SDOperand> MemOps; |
| 252 | for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 253 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 254 | SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 255 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 256 | Val, FIN, DAG.getSrcValue(NULL)); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 257 | MemOps.push_back(Store); |
| 258 | // Increment the address by four for the next argument to store |
| 259 | SDOperand PtrOff = DAG.getConstant(4, getPointerTy()); |
| 260 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff); |
| 261 | } |
| 262 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps)); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 263 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 264 | |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 265 | // Finally, inform the code generator which regs we return values in. |
| 266 | switch (getValueType(F.getReturnType())) { |
| 267 | default: assert(0 && "Unknown type!"); |
| 268 | case MVT::isVoid: break; |
| 269 | case MVT::i1: |
| 270 | case MVT::i8: |
| 271 | case MVT::i16: |
| 272 | case MVT::i32: |
| 273 | MF.addLiveOut(PPC::R3); |
| 274 | break; |
| 275 | case MVT::i64: |
| 276 | MF.addLiveOut(PPC::R3); |
| 277 | MF.addLiveOut(PPC::R4); |
| 278 | break; |
| 279 | case MVT::f32: |
| 280 | case MVT::f64: |
| 281 | MF.addLiveOut(PPC::F1); |
| 282 | break; |
| 283 | } |
| 284 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 285 | return ArgValues; |
| 286 | } |
| 287 | |
| 288 | std::pair<SDOperand, SDOperand> |
| 289 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 290 | const Type *RetTy, bool isVarArg, |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 291 | unsigned CallingConv, bool isTailCall, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 292 | SDOperand Callee, ArgListTy &Args, |
| 293 | SelectionDAG &DAG) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 294 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 295 | // SelectExpr to use to put the arguments in the appropriate registers. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 296 | std::vector<SDOperand> args_to_use; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 297 | |
| 298 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 299 | // area, and parameter passing area. |
| 300 | unsigned NumBytes = 24; |
| 301 | |
| 302 | if (Args.empty()) { |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 303 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | a7e11a4 | 2005-04-01 05:57:17 +0000 | [diff] [blame] | 304 | DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 305 | } else { |
| 306 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 307 | switch (getValueType(Args[i].second)) { |
| 308 | default: assert(0 && "Unknown value type!"); |
| 309 | case MVT::i1: |
| 310 | case MVT::i8: |
| 311 | case MVT::i16: |
| 312 | case MVT::i32: |
| 313 | case MVT::f32: |
| 314 | NumBytes += 4; |
| 315 | break; |
| 316 | case MVT::i64: |
| 317 | case MVT::f64: |
| 318 | NumBytes += 8; |
| 319 | break; |
| 320 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 321 | |
| 322 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 323 | // plus 32 bytes of argument space in case any called code gets funky on us. |
| 324 | if (NumBytes < 56) NumBytes = 56; |
| 325 | |
| 326 | // Adjust the stack pointer for the new arguments... |
| 327 | // These operations are automatically eliminated by the prolog/epilog pass |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 328 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 329 | DAG.getConstant(NumBytes, getPointerTy())); |
| 330 | |
| 331 | // Set up a copy of the stack pointer for use loading and storing any |
| 332 | // arguments that may not fit in the registers available for argument |
| 333 | // passing. |
| 334 | SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32, |
| 335 | DAG.getEntryNode()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 336 | |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 337 | // Figure out which arguments are going to go in registers, and which in |
| 338 | // memory. Also, if this is a vararg function, floating point operations |
| 339 | // must be stored to our stack, and loaded into integer regs as well, if |
| 340 | // any integer regs are available for argument passing. |
| 341 | unsigned ArgOffset = 24; |
| 342 | unsigned GPR_remaining = 8; |
| 343 | unsigned FPR_remaining = 13; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 344 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 345 | std::vector<SDOperand> MemOps; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 346 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 347 | // PtrOff will be used to store the current argument to the stack if a |
| 348 | // register cannot be found for it. |
| 349 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 350 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 351 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 352 | |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 353 | switch (ArgVT) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 354 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 355 | case MVT::i1: |
| 356 | case MVT::i8: |
| 357 | case MVT::i16: |
| 358 | // Promote the integer to 32 bits. If the input type is signed use a |
| 359 | // sign extend, otherwise use a zero extend. |
| 360 | if (Args[i].second->isSigned()) |
| 361 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 362 | else |
| 363 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 364 | // FALL THROUGH |
| 365 | case MVT::i32: |
| 366 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 367 | args_to_use.push_back(Args[i].first); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 368 | --GPR_remaining; |
| 369 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 370 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 371 | Args[i].first, PtrOff, |
| 372 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 373 | } |
| 374 | ArgOffset += 4; |
| 375 | break; |
| 376 | case MVT::i64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 377 | // If we have one free GPR left, we can place the upper half of the i64 |
| 378 | // in it, and store the other half to the stack. If we have two or more |
| 379 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 380 | if (GPR_remaining > 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 381 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 382 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 383 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 384 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 385 | args_to_use.push_back(Hi); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 386 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 387 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 388 | args_to_use.push_back(Lo); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 389 | --GPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 390 | } else { |
| 391 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 392 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 393 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 394 | Lo, PtrOff, DAG.getSrcValue(NULL))); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 395 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 396 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 397 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 398 | Args[i].first, PtrOff, |
| 399 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 400 | } |
| 401 | ArgOffset += 8; |
| 402 | break; |
| 403 | case MVT::f32: |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 404 | case MVT::f64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 405 | if (FPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 406 | args_to_use.push_back(Args[i].first); |
| 407 | --FPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 408 | if (isVarArg) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 409 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 410 | Args[i].first, PtrOff, |
| 411 | DAG.getSrcValue(NULL)); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 412 | MemOps.push_back(Store); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 413 | // Float varargs are always shadowed in available integer registers |
| 414 | if (GPR_remaining > 0) { |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 415 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 416 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 417 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 418 | args_to_use.push_back(Load); |
| 419 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 420 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 421 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 422 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 423 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 424 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 425 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 426 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 427 | args_to_use.push_back(Load); |
| 428 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 429 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 430 | } else { |
| 431 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 432 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 433 | // GPRs. |
| 434 | if (GPR_remaining > 0) { |
| 435 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 436 | --GPR_remaining; |
| 437 | } |
| 438 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 439 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 440 | --GPR_remaining; |
| 441 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 442 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 443 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 444 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 445 | Args[i].first, PtrOff, |
| 446 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 447 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 448 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 451 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 452 | if (!MemOps.empty()) |
| 453 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 454 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 455 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 456 | std::vector<MVT::ValueType> RetVals; |
| 457 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 458 | if (RetTyVT != MVT::isVoid) |
| 459 | RetVals.push_back(RetTyVT); |
| 460 | RetVals.push_back(MVT::Other); |
| 461 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 462 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 463 | Chain, Callee, args_to_use), 0); |
| 464 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 465 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 466 | DAG.getConstant(NumBytes, getPointerTy())); |
| 467 | return std::make_pair(TheCall, Chain); |
| 468 | } |
| 469 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 470 | SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 471 | Value *VAListV, SelectionDAG &DAG) { |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 472 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 473 | // memory location argument. |
| 474 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 475 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 476 | DAG.getSrcValue(VAListV)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 479 | std::pair<SDOperand,SDOperand> |
| 480 | PPC32TargetLowering::LowerVAArg(SDOperand Chain, |
| 481 | SDOperand VAListP, Value *VAListV, |
| 482 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 483 | MVT::ValueType ArgVT = getValueType(ArgTy); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 484 | |
| 485 | SDOperand VAList = |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 486 | DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV)); |
| 487 | SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL)); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 488 | unsigned Amt; |
| 489 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 490 | Amt = 4; |
| 491 | else { |
| 492 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 493 | "Other types should have been promoted for varargs!"); |
| 494 | Amt = 8; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 496 | VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 497 | DAG.getConstant(Amt, VAList.getValueType())); |
| 498 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 499 | VAList, VAListP, DAG.getSrcValue(VAListV)); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 500 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 501 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 502 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 503 | |
| 504 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 505 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 506 | SelectionDAG &DAG) { |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 507 | assert(0 && "LowerFrameReturnAddress unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 508 | abort(); |
| 509 | } |
| 510 | |
| 511 | namespace { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 512 | Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted"); |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 513 | Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations"); |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 514 | Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 515 | //===--------------------------------------------------------------------===// |
| 516 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 517 | /// SelectionDAG operations. |
| 518 | //===--------------------------------------------------------------------===// |
| 519 | class ISel : public SelectionDAGISel { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 520 | PPC32TargetLowering PPC32Lowering; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 521 | SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform |
| 522 | // for sdiv and udiv until it is put into the future |
| 523 | // dag combiner. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 524 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 525 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 526 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 527 | /// tree. |
| 528 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 529 | |
| 530 | unsigned GlobalBaseReg; |
| 531 | bool GlobalBaseInitialized; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 532 | bool RecordSuccess; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 533 | public: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 534 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM), |
| 535 | ISelDAG(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 536 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 537 | /// runOnFunction - Override this function in order to reset our per-function |
| 538 | /// variables. |
| 539 | virtual bool runOnFunction(Function &Fn) { |
| 540 | // Make sure we re-emit a set of the global base reg if necessary |
| 541 | GlobalBaseInitialized = false; |
| 542 | return SelectionDAGISel::runOnFunction(Fn); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 545 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 546 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 547 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 548 | DEBUG(BB->dump()); |
| 549 | // Codegen the basic block. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 550 | ISelDAG = &DAG; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 551 | Select(DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 552 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 553 | // Clear state used for selection. |
| 554 | ExprMap.clear(); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 555 | ISelDAG = 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 556 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 557 | |
| 558 | // dag -> dag expanders for integer divide by constant |
| 559 | SDOperand BuildSDIVSequence(SDOperand N); |
| 560 | SDOperand BuildUDIVSequence(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 561 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 562 | unsigned getGlobalBaseReg(); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 563 | unsigned getConstDouble(double floatVal, unsigned Result); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 564 | void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 565 | bool SelectBitfieldInsert(SDOperand OR, unsigned Result); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 566 | unsigned FoldIfWideZeroExtend(SDOperand N); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 567 | unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx); |
| 568 | unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 569 | unsigned SelectExpr(SDOperand N, bool Recording=false); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 570 | void Select(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 571 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 572 | bool SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 573 | void SelectBranchCC(SDOperand N); |
| 574 | }; |
| 575 | |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 576 | /// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It |
| 577 | /// returns zero when the input is not exactly a power of two. |
| 578 | static unsigned ExactLog2(unsigned Val) { |
| 579 | if (Val == 0 || (Val & (Val-1))) return 0; |
| 580 | unsigned Count = 0; |
| 581 | while (Val != 1) { |
| 582 | Val >>= 1; |
| 583 | ++Count; |
| 584 | } |
| 585 | return Count; |
| 586 | } |
| 587 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 588 | // IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with |
| 589 | // any number of 0's on either side. the 1's are allowed to wrap from LSB to |
| 590 | // MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 591 | // not, since all 1's are not contiguous. |
| 592 | static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 593 | bool isRun = true; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 594 | MB = 0; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 595 | ME = 0; |
| 596 | |
| 597 | // look for first set bit |
| 598 | int i = 0; |
| 599 | for (; i < 32; i++) { |
| 600 | if ((Val & (1 << (31 - i))) != 0) { |
| 601 | MB = i; |
| 602 | ME = i; |
| 603 | break; |
| 604 | } |
| 605 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 606 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 607 | // look for last set bit |
| 608 | for (; i < 32; i++) { |
| 609 | if ((Val & (1 << (31 - i))) == 0) |
| 610 | break; |
| 611 | ME = i; |
| 612 | } |
| 613 | |
| 614 | // look for next set bit |
| 615 | for (; i < 32; i++) { |
| 616 | if ((Val & (1 << (31 - i))) != 0) |
| 617 | break; |
| 618 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 619 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 620 | // if we exhausted all the bits, we found a match at this point for 0*1*0* |
| 621 | if (i == 32) |
| 622 | return true; |
| 623 | |
| 624 | // since we just encountered more 1's, if it doesn't wrap around to the |
| 625 | // most significant bit of the word, then we did not find a match to 1*0*1* so |
| 626 | // exit. |
| 627 | if (MB != 0) |
| 628 | return false; |
| 629 | |
| 630 | // look for last set bit |
| 631 | for (MB = i; i < 32; i++) { |
| 632 | if ((Val & (1 << (31 - i))) == 0) |
| 633 | break; |
| 634 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 635 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 636 | // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise, |
| 637 | // the value is not a run of ones. |
| 638 | if (i == 32) |
| 639 | return true; |
| 640 | return false; |
| 641 | } |
| 642 | |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 643 | /// getImmediateForOpcode - This method returns a value indicating whether |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 644 | /// the ConstantSDNode N can be used as an immediate to Opcode. The return |
| 645 | /// values are either 0, 1 or 2. 0 indicates that either N is not a |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 646 | /// ConstantSDNode, or is not suitable for use by that opcode. |
| 647 | /// Return value codes for turning into an enum someday: |
| 648 | /// 1: constant may be used in normal immediate form. |
| 649 | /// 2: constant may be used in shifted immediate form. |
| 650 | /// 3: log base 2 of the constant may be used. |
| 651 | /// 4: constant is suitable for integer division conversion |
| 652 | /// 5: constant is a bitfield mask |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 653 | /// |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 654 | static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode, |
| 655 | unsigned& Imm, bool U = false) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 656 | if (N.getOpcode() != ISD::Constant) return 0; |
| 657 | |
| 658 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 659 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 660 | switch(Opcode) { |
| 661 | default: return 0; |
| 662 | case ISD::ADD: |
| 663 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 664 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 665 | break; |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 666 | case ISD::AND: { |
| 667 | unsigned MB, ME; |
| 668 | if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; } |
| 669 | if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; } |
| 670 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 671 | break; |
| 672 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 673 | case ISD::XOR: |
| 674 | case ISD::OR: |
| 675 | if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; } |
| 676 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 677 | break; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 678 | case ISD::MUL: |
| 679 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 680 | break; |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 681 | case ISD::SUB: |
| 682 | // handle subtract-from separately from subtract, since subi is really addi |
| 683 | if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 684 | if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; } |
| 685 | break; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 686 | case ISD::SETCC: |
| 687 | if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; } |
| 688 | if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; } |
| 689 | break; |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 690 | case ISD::SDIV: |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 691 | if ((Imm = ExactLog2(v))) { return 3; } |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 692 | if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 693 | if (v <= -2 || v >= 2) { return 4; } |
| 694 | break; |
| 695 | case ISD::UDIV: |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 696 | if (v > 1) { return 4; } |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 697 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 698 | } |
| 699 | return 0; |
| 700 | } |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 701 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 702 | /// NodeHasRecordingVariant - If SelectExpr can always produce code for |
| 703 | /// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise, |
| 704 | /// return false. |
| 705 | static bool NodeHasRecordingVariant(unsigned NodeOpcode) { |
| 706 | switch(NodeOpcode) { |
| 707 | default: return false; |
| 708 | case ISD::AND: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 709 | case ISD::OR: |
Chris Lattner | 519f40b | 2005-04-13 02:46:17 +0000 | [diff] [blame] | 710 | return true; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 714 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 715 | /// to Condition. If the Condition is unordered or unsigned, the bool argument |
| 716 | /// U is set to true, otherwise it is set to false. |
| 717 | static unsigned getBCCForSetCC(unsigned Condition, bool& U) { |
| 718 | U = false; |
| 719 | switch (Condition) { |
| 720 | default: assert(0 && "Unknown condition!"); abort(); |
| 721 | case ISD::SETEQ: return PPC::BEQ; |
| 722 | case ISD::SETNE: return PPC::BNE; |
| 723 | case ISD::SETULT: U = true; |
| 724 | case ISD::SETLT: return PPC::BLT; |
| 725 | case ISD::SETULE: U = true; |
| 726 | case ISD::SETLE: return PPC::BLE; |
| 727 | case ISD::SETUGT: U = true; |
| 728 | case ISD::SETGT: return PPC::BGT; |
| 729 | case ISD::SETUGE: U = true; |
| 730 | case ISD::SETGE: return PPC::BGE; |
| 731 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 735 | /// getCROpForOp - Return the condition register opcode (or inverted opcode) |
| 736 | /// associated with the SelectionDAG opcode. |
| 737 | static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) { |
| 738 | switch (Opcode) { |
| 739 | default: assert(0 && "Unknown opcode!"); abort(); |
| 740 | case ISD::AND: |
| 741 | if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law |
| 742 | if (!Inv1 && !Inv2) return PPC::CRAND; |
| 743 | if (Inv1 ^ Inv2) return PPC::CRANDC; |
| 744 | case ISD::OR: |
| 745 | if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law |
| 746 | if (!Inv1 && !Inv2) return PPC::CROR; |
| 747 | if (Inv1 ^ Inv2) return PPC::CRORC; |
| 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 753 | /// associated with the SetCC condition, and whether or not the field is |
| 754 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
| 755 | static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) { |
| 756 | switch (Condition) { |
| 757 | default: assert(0 && "Unknown condition!"); abort(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 758 | case ISD::SETULT: |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 759 | case ISD::SETLT: Inv = false; return 0; |
| 760 | case ISD::SETUGE: |
| 761 | case ISD::SETGE: Inv = true; return 0; |
| 762 | case ISD::SETUGT: |
| 763 | case ISD::SETGT: Inv = false; return 1; |
| 764 | case ISD::SETULE: |
| 765 | case ISD::SETLE: Inv = true; return 1; |
| 766 | case ISD::SETEQ: Inv = false; return 2; |
| 767 | case ISD::SETNE: Inv = true; return 2; |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 772 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 773 | /// and store immediate instructions. |
| 774 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 775 | switch(Opcode) { |
| 776 | default: assert(0 && "Unknown opcode!"); abort(); |
| 777 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 778 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 779 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 780 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 781 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 782 | case PPC::LFD: return PPC::LFDX; |
| 783 | } |
| 784 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 785 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 786 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 787 | // Structure used to return the necessary information to codegen an SDIV as |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 788 | // a multiply. |
| 789 | struct ms { |
| 790 | int m; // magic number |
| 791 | int s; // shift amount |
| 792 | }; |
| 793 | |
| 794 | struct mu { |
| 795 | unsigned int m; // magic number |
| 796 | int a; // add indicator |
| 797 | int s; // shift amount |
| 798 | }; |
| 799 | |
| 800 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 801 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 802 | /// or -1. |
| 803 | static struct ms magic(int d) { |
| 804 | int p; |
| 805 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
| 806 | const unsigned int two31 = 2147483648U; // 2^31 |
| 807 | struct ms mag; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 808 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 809 | ad = abs(d); |
| 810 | t = two31 + ((unsigned int)d >> 31); |
| 811 | anc = t - 1 - t%ad; // absolute value of nc |
| 812 | p = 31; // initialize p |
| 813 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 814 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 815 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 816 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 817 | do { |
| 818 | p = p + 1; |
| 819 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 820 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 821 | if (r1 >= anc) { // must be unsigned comparison |
| 822 | q1 = q1 + 1; |
| 823 | r1 = r1 - anc; |
| 824 | } |
| 825 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 826 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 827 | if (r2 >= ad) { // must be unsigned comparison |
| 828 | q2 = q2 + 1; |
| 829 | r2 = r2 - ad; |
| 830 | } |
| 831 | delta = ad - r2; |
| 832 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 833 | |
| 834 | mag.m = q2 + 1; |
| 835 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 836 | mag.s = p - 32; // resulting shift |
| 837 | return mag; |
| 838 | } |
| 839 | |
| 840 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 841 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 842 | static struct mu magicu(unsigned d) |
| 843 | { |
| 844 | int p; |
| 845 | unsigned int nc, delta, q1, r1, q2, r2; |
| 846 | struct mu magu; |
| 847 | magu.a = 0; // initialize "add" indicator |
| 848 | nc = - 1 - (-d)%d; |
| 849 | p = 31; // initialize p |
| 850 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 851 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 852 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 853 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 854 | do { |
| 855 | p = p + 1; |
| 856 | if (r1 >= nc - r1 ) { |
| 857 | q1 = 2*q1 + 1; // update q1 |
| 858 | r1 = 2*r1 - nc; // update r1 |
| 859 | } |
| 860 | else { |
| 861 | q1 = 2*q1; // update q1 |
| 862 | r1 = 2*r1; // update r1 |
| 863 | } |
| 864 | if (r2 + 1 >= d - r2) { |
| 865 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 866 | q2 = 2*q2 + 1; // update q2 |
| 867 | r2 = 2*r2 + 1 - d; // update r2 |
| 868 | } |
| 869 | else { |
| 870 | if (q2 >= 0x80000000) magu.a = 1; |
| 871 | q2 = 2*q2; // update q2 |
| 872 | r2 = 2*r2 + 1; // update r2 |
| 873 | } |
| 874 | delta = d - 1 - r2; |
| 875 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 876 | magu.m = q2 + 1; // resulting magic number |
| 877 | magu.s = p - 32; // resulting shift |
| 878 | return magu; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 883 | /// return a DAG expression to select that will generate the same value by |
| 884 | /// multiplying by a magic number. See: |
| 885 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 886 | SDOperand ISel::BuildSDIVSequence(SDOperand N) { |
| 887 | int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 888 | ms magics = magic(d); |
| 889 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 890 | SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 891 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 892 | // If d > 0 and m < 0, add the numerator |
| 893 | if (d > 0 && magics.m < 0) |
| 894 | Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0)); |
| 895 | // If d < 0 and m > 0, subtract the numerator. |
| 896 | if (d < 0 && magics.m > 0) |
| 897 | Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0)); |
| 898 | // Shift right algebraic if shift value is nonzero |
| 899 | if (magics.s > 0) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 900 | Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 901 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 902 | // Extract the sign bit and add it to the quotient |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 903 | SDOperand T = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 904 | ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32)); |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 905 | return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 909 | /// return a DAG expression to select that will generate the same value by |
| 910 | /// multiplying by a magic number. See: |
| 911 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 912 | SDOperand ISel::BuildUDIVSequence(SDOperand N) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 913 | unsigned d = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 914 | (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 915 | mu magics = magicu(d); |
| 916 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 917 | SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 918 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 919 | if (magics.a == 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 920 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 921 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 922 | } else { |
| 923 | SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 924 | NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 925 | ISelDAG->getConstant(1, MVT::i32)); |
| 926 | NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 927 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 928 | ISelDAG->getConstant(magics.s-1, MVT::i32)); |
| 929 | } |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 930 | return Q; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 933 | /// getGlobalBaseReg - Output the instructions required to put the |
| 934 | /// base address to use for accessing globals into a register. |
| 935 | /// |
| 936 | unsigned ISel::getGlobalBaseReg() { |
| 937 | if (!GlobalBaseInitialized) { |
| 938 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 939 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 940 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 941 | GlobalBaseReg = MakeReg(MVT::i32); |
| 942 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 943 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 944 | GlobalBaseInitialized = true; |
| 945 | } |
| 946 | return GlobalBaseReg; |
| 947 | } |
| 948 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 949 | /// getConstDouble - Loads a floating point value into a register, via the |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 950 | /// Constant Pool. Optionally takes a register in which to load the value. |
| 951 | unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) { |
| 952 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 953 | if (0 == Result) Result = MakeReg(MVT::f64); |
| 954 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 955 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal); |
| 956 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 957 | if (PICEnabled) |
| 958 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 959 | .addConstantPoolIndex(CPI); |
| 960 | else |
| 961 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 962 | BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
| 963 | return Result; |
| 964 | } |
| 965 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 966 | /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 967 | /// Inv is true, then invert the result. |
| 968 | void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){ |
| 969 | unsigned IntCR = MakeReg(MVT::i32); |
| 970 | BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 971 | BuildMI(BB, GPOPT ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 972 | if (Inv) { |
| 973 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 974 | BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx)) |
| 975 | .addImm(31).addImm(31); |
| 976 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1); |
| 977 | } else { |
| 978 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx)) |
| 979 | .addImm(31).addImm(31); |
| 980 | } |
| 981 | } |
| 982 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 983 | /// SelectBitfieldInsert - turn an or of two masked values into |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 984 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 985 | /// Returns true on success, false if the caller still needs to select OR. |
| 986 | /// |
| 987 | /// Patterns matched: |
| 988 | /// 1. or shl, and 5. or and, and |
| 989 | /// 2. or and, shl 6. or shl, shr |
| 990 | /// 3. or shr, and 7. or shr, shl |
| 991 | /// 4. or and, shr |
| 992 | bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 993 | bool IsRotate = false; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 994 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 995 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 996 | SDOperand Op0 = OR.getOperand(0); |
| 997 | SDOperand Op1 = OR.getOperand(1); |
| 998 | |
| 999 | unsigned Op0Opc = Op0.getOpcode(); |
| 1000 | unsigned Op1Opc = Op1.getOpcode(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1001 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1002 | // Verify that we have the correct opcodes |
| 1003 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 1004 | return false; |
| 1005 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 1006 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1007 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1008 | // Generate Mask value for Target |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1009 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1010 | dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1011 | switch(Op0Opc) { |
| 1012 | case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break; |
| 1013 | case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break; |
| 1014 | case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break; |
| 1015 | } |
| 1016 | } else { |
| 1017 | return false; |
| 1018 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1019 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1020 | // Generate Mask value for Insert |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1021 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1022 | dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1023 | switch(Op1Opc) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1024 | case ISD::SHL: |
| 1025 | Amount = CN->getValue(); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1026 | InsMask <<= Amount; |
| 1027 | if (Op0Opc == ISD::SRL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1028 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1029 | case ISD::SRL: |
| 1030 | Amount = CN->getValue(); |
| 1031 | InsMask >>= Amount; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1032 | Amount = 32-Amount; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1033 | if (Op0Opc == ISD::SHL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1034 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1035 | case ISD::AND: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1036 | InsMask &= (unsigned)CN->getValue(); |
| 1037 | break; |
| 1038 | } |
| 1039 | } else { |
| 1040 | return false; |
| 1041 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1042 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1043 | unsigned Tmp3 = 0; |
| 1044 | |
| 1045 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 1046 | // constant as its input, make that the inserted value so that we can combine |
| 1047 | // the shift into the rotate part of the rlwimi instruction |
| 1048 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1049 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1050 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1051 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1052 | dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1053 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1054 | CN->getValue() : 32 - CN->getValue(); |
| 1055 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1056 | } |
| 1057 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 1058 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1059 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1060 | dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) { |
| 1061 | std::swap(Op0, Op1); |
| 1062 | std::swap(TgtMask, InsMask); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1063 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1064 | CN->getValue() : 32 - CN->getValue(); |
| 1065 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1070 | // Verify that the Target mask and Insert mask together form a full word mask |
| 1071 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 1072 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 1073 | // instruction. |
| 1074 | unsigned MB, ME; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1075 | if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1076 | unsigned Tmp1, Tmp2; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1077 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1078 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 1079 | // where both bitfield halves are sourced from the same value. |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1080 | if (IsRotate && fullMask && |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1081 | OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1082 | Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0)); |
| 1083 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount) |
| 1084 | .addImm(0).addImm(31); |
| 1085 | return true; |
| 1086 | } |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1087 | if (Op0Opc == ISD::AND && fullMask) |
| 1088 | Tmp1 = SelectExpr(Op0.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1089 | else |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1090 | Tmp1 = SelectExpr(Op0); |
| 1091 | Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1092 | BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2) |
| 1093 | .addImm(Amount).addImm(MB).addImm(ME); |
| 1094 | return true; |
| 1095 | } |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1099 | /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the |
| 1100 | /// low six bits. If the shift amount is an ISD::AND node with a mask that is |
| 1101 | /// wider than the implicit mask, then we can get rid of the AND and let the |
| 1102 | /// shift do the mask. |
| 1103 | unsigned ISel::FoldIfWideZeroExtend(SDOperand N) { |
| 1104 | unsigned C; |
| 1105 | if (N.getOpcode() == ISD::AND && |
| 1106 | 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask |
| 1107 | 31 == (C & 0xFFFF) && // ME |
| 1108 | 26 >= (C >> 16)) // MB |
| 1109 | return SelectExpr(N.getOperand(0)); |
| 1110 | else |
| 1111 | return SelectExpr(N); |
| 1112 | } |
| 1113 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1114 | unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1115 | unsigned Result, Tmp1, Tmp2; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1116 | bool AlreadySelected = false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1117 | static const unsigned CompareOpcodes[] = |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1118 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1119 | |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1120 | // Allocate a condition register for this expression |
| 1121 | Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1122 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1123 | // If the first operand to the select is a SETCC node, then we can fold it |
| 1124 | // into the branch that selects which value to return. |
Nate Begeman | 16ac709 | 2005-04-18 02:43:24 +0000 | [diff] [blame] | 1125 | if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1126 | bool U; |
| 1127 | Opc = getBCCForSetCC(SetCC->getCondition(), U); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1128 | Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1129 | |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1130 | // Pass the optional argument U to getImmediateForOpcode for SETCC, |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1131 | // so that it knows whether the SETCC immediate range is signed or not. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1132 | if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC, |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1133 | Tmp2, U)) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1134 | // For comparisons against zero, we can implicity set CR0 if a recording |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1135 | // variant (e.g. 'or.' instead of 'or') of the instruction that defines |
| 1136 | // operand zero of the SetCC node is available. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1137 | if (0 == Tmp2 && |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1138 | NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) && |
| 1139 | SetCC->getOperand(0).Val->hasOneUse()) { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1140 | RecordSuccess = false; |
| 1141 | Tmp1 = SelectExpr(SetCC->getOperand(0), true); |
| 1142 | if (RecordSuccess) { |
| 1143 | ++Recorded; |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 1144 | BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0); |
| 1145 | return Result; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1146 | } |
| 1147 | AlreadySelected = true; |
| 1148 | } |
| 1149 | // If we could not implicitly set CR0, then emit a compare immediate |
| 1150 | // instead. |
| 1151 | if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0)); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1152 | if (U) |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1153 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1154 | else |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1155 | BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1156 | } else { |
| 1157 | bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); |
| 1158 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1159 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1160 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1161 | BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } else { |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1164 | // If this isn't a SetCC, then select the value and compare it against zero, |
| 1165 | // treating it as if it were a boolean. |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1166 | Opc = PPC::BNE; |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1167 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1168 | Tmp1 = SelectExpr(CC); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1169 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1170 | } |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1171 | return Result; |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1174 | unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1175 | unsigned &Idx) { |
| 1176 | bool Inv0, Inv1; |
| 1177 | unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2; |
| 1178 | |
| 1179 | // Allocate a condition register for this expression |
| 1180 | unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
| 1181 | |
| 1182 | // Check for the operations we support: |
| 1183 | switch(N.getOpcode()) { |
| 1184 | default: |
| 1185 | Opc = PPC::BNE; |
| 1186 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
| 1187 | Tmp1 = SelectExpr(N); |
| 1188 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
| 1189 | break; |
| 1190 | case ISD::OR: |
| 1191 | case ISD::AND: |
| 1192 | ++MultiBranch; |
| 1193 | Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0); |
| 1194 | Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1); |
| 1195 | CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1); |
| 1196 | if (Inv0 && !Inv1) { |
| 1197 | std::swap(Tmp1, Tmp2); |
| 1198 | std::swap(Idx0, Idx1); |
| 1199 | Opc = Opc1; |
| 1200 | } |
| 1201 | if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
| 1202 | BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0) |
| 1203 | .addReg(Tmp2).addImm(Idx1); |
| 1204 | Inv = false; |
| 1205 | Idx = Idx0; |
| 1206 | break; |
| 1207 | case ISD::SETCC: |
| 1208 | Tmp1 = SelectCC(N, Opc, Inv, Idx); |
| 1209 | Result = Tmp1; |
| 1210 | break; |
| 1211 | } |
| 1212 | return Result; |
| 1213 | } |
| 1214 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1215 | /// Check to see if the load is a constant offset from a base register |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1216 | bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1217 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 1218 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1219 | if (N.getOpcode() == ISD::ADD) { |
| 1220 | Reg = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1221 | if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 1222 | offset = imm; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1223 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1224 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1225 | offset = SelectExpr(N.getOperand(1)); |
| 1226 | return true; |
| 1227 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1228 | Reg = SelectExpr(N); |
| 1229 | offset = 0; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1230 | return false; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | void ISel::SelectBranchCC(SDOperand N) |
| 1234 | { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1235 | MachineBasicBlock *Dest = |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1236 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 1237 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1238 | bool Inv; |
| 1239 | unsigned Opc, CCReg, Idx; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1240 | Select(N.getOperand(0)); //chain |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1241 | CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1242 | |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1243 | // Iterate to the next basic block |
| 1244 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1245 | ++It; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1246 | |
| 1247 | // If this is a two way branch, then grab the fallthrough basic block argument |
| 1248 | // and build a PowerPC branch pseudo-op, suitable for long branch conversion |
| 1249 | // if necessary by the branch selection pass. Otherwise, emit a standard |
| 1250 | // conditional branch. |
| 1251 | if (N.getOpcode() == ISD::BRCONDTWOWAY) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1252 | MachineBasicBlock *Fallthrough = |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1253 | cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock(); |
| 1254 | if (Dest != It) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1255 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1256 | .addMBB(Dest).addMBB(Fallthrough); |
| 1257 | if (Fallthrough != It) |
| 1258 | BuildMI(BB, PPC::B, 1).addMBB(Fallthrough); |
| 1259 | } else { |
| 1260 | if (Fallthrough != It) { |
| 1261 | Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1262 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1263 | .addMBB(Fallthrough).addMBB(Dest); |
| 1264 | } |
| 1265 | } |
| 1266 | } else { |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1267 | // If the fallthrough path is off the end of the function, which would be |
| 1268 | // undefined behavior, set it to be the same as the current block because |
| 1269 | // we have nothing better to set it to, and leaving it alone will cause the |
| 1270 | // PowerPC Branch Selection pass to crash. |
| 1271 | if (It == BB->getParent()->end()) It = Dest; |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1272 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 1273 | .addMBB(Dest).addMBB(It); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1274 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1275 | return; |
| 1276 | } |
| 1277 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1278 | unsigned ISel::SelectExpr(SDOperand N, bool Recording) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1279 | unsigned Result; |
| 1280 | unsigned Tmp1, Tmp2, Tmp3; |
| 1281 | unsigned Opc = 0; |
| 1282 | unsigned opcode = N.getOpcode(); |
| 1283 | |
| 1284 | SDNode *Node = N.Val; |
| 1285 | MVT::ValueType DestType = N.getValueType(); |
| 1286 | |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 1287 | if (Node->getOpcode() == ISD::CopyFromReg && |
| 1288 | MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg())) |
| 1289 | // Just use the specified register as our input. |
| 1290 | return cast<RegSDNode>(Node)->getReg(); |
| 1291 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1292 | unsigned &Reg = ExprMap[N]; |
| 1293 | if (Reg) return Reg; |
| 1294 | |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1295 | switch (N.getOpcode()) { |
| 1296 | default: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1297 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1298 | MakeReg(N.getValueType()) : 1; |
| 1299 | break; |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1300 | case ISD::TAILCALL: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1301 | case ISD::CALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1302 | // If this is a call instruction, make sure to prepare ALL of the result |
| 1303 | // values as well as the chain. |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1304 | if (Node->getNumValues() == 1) |
| 1305 | Reg = Result = 1; // Void call, just a chain. |
| 1306 | else { |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1307 | Result = MakeReg(Node->getValueType(0)); |
| 1308 | ExprMap[N.getValue(0)] = Result; |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1309 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1310 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1311 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1312 | } |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1313 | break; |
| 1314 | case ISD::ADD_PARTS: |
| 1315 | case ISD::SUB_PARTS: |
| 1316 | case ISD::SHL_PARTS: |
| 1317 | case ISD::SRL_PARTS: |
| 1318 | case ISD::SRA_PARTS: |
| 1319 | Result = MakeReg(Node->getValueType(0)); |
| 1320 | ExprMap[N.getValue(0)] = Result; |
| 1321 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 1322 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 1323 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1326 | switch (opcode) { |
| 1327 | default: |
| 1328 | Node->dump(); |
| 1329 | assert(0 && "Node not handled!\n"); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1330 | case ISD::UNDEF: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1331 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result); |
| 1332 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1333 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1334 | // Generate both result values. FIXME: Need a better commment here? |
| 1335 | if (Result != 1) |
| 1336 | ExprMap[N.getValue(1)] = 1; |
| 1337 | else |
| 1338 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1339 | |
| 1340 | // FIXME: We are currently ignoring the requested alignment for handling |
| 1341 | // greater than the stack alignment. This will need to be revisited at some |
| 1342 | // point. Align = N.getOperand(2); |
| 1343 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 1344 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 1345 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 1346 | << " the stack alignment yet!"; |
| 1347 | abort(); |
| 1348 | } |
| 1349 | Select(N.getOperand(0)); |
| 1350 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1351 | // Subtract size from stack pointer, thereby allocating some space. |
| 1352 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 1353 | // Put a pointer to the space into the result register by copying the SP |
| 1354 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 1355 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1356 | |
| 1357 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1358 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 1359 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1360 | if (PICEnabled) |
| 1361 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 1362 | .addConstantPoolIndex(Tmp1); |
| 1363 | else |
| 1364 | BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1365 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 1366 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1367 | |
| 1368 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1369 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1370 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1371 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1372 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1373 | case ISD::GlobalAddress: { |
| 1374 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1375 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1376 | if (PICEnabled) |
| 1377 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1378 | .addGlobalAddress(GV); |
| 1379 | else |
| 1380 | BuildMI(BB, PPC::LIS, 2, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1381 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 1382 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 1383 | } else { |
| 1384 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 1385 | } |
| 1386 | return Result; |
| 1387 | } |
| 1388 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1389 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1390 | case ISD::EXTLOAD: |
| 1391 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1392 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1393 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1394 | Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1395 | bool sext = (ISD::SEXTLOAD == opcode); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1396 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1397 | // Make sure we generate both values. |
| 1398 | if (Result != 1) |
| 1399 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 1400 | else |
| 1401 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1402 | |
| 1403 | SDOperand Chain = N.getOperand(0); |
| 1404 | SDOperand Address = N.getOperand(1); |
| 1405 | Select(Chain); |
| 1406 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1407 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1408 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1409 | case MVT::i1: Opc = PPC::LBZ; break; |
| 1410 | case MVT::i8: Opc = PPC::LBZ; break; |
| 1411 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 1412 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1413 | case MVT::f32: Opc = PPC::LFS; break; |
| 1414 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1415 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1416 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1417 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
| 1418 | Tmp1 = MakeReg(MVT::i32); |
| 1419 | int CPI = CP->getIndex(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1420 | if (PICEnabled) |
| 1421 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1422 | .addConstantPoolIndex(CPI); |
| 1423 | else |
| 1424 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1425 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1426 | } else if (Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1427 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1428 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1429 | } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){ |
| 1430 | GlobalValue *GV = GN->getGlobal(); |
| 1431 | Tmp1 = MakeReg(MVT::i32); |
| 1432 | if (PICEnabled) |
| 1433 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1434 | .addGlobalAddress(GV); |
| 1435 | else |
| 1436 | BuildMI(BB, PPC::LIS, 2, Tmp1).addGlobalAddress(GV); |
| 1437 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 1438 | Tmp2 = MakeReg(MVT::i32); |
| 1439 | BuildMI(BB, PPC::LWZ, 2, Tmp2).addGlobalAddress(GV).addReg(Tmp1); |
Nate Begeman | 7b4f0a8 | 2005-07-25 21:15:28 +0000 | [diff] [blame] | 1440 | BuildMI(BB, Opc, 2, Result).addSImm(0).addReg(Tmp2); |
| 1441 | } else { |
| 1442 | BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1443 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1444 | } else { |
| 1445 | int offset; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1446 | bool idx = SelectAddr(Address, Tmp1, offset); |
| 1447 | if (idx) { |
| 1448 | Opc = IndexedOpForOp(Opc); |
| 1449 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
| 1450 | } else { |
| 1451 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 1452 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1453 | } |
| 1454 | return Result; |
| 1455 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1457 | case ISD::TAILCALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1458 | case ISD::CALL: { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1459 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1460 | static const unsigned GPR[] = { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1461 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1462 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1463 | }; |
| 1464 | static const unsigned FPR[] = { |
| 1465 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1466 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1467 | }; |
| 1468 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1469 | // Lower the chain for this call. |
| 1470 | Select(N.getOperand(0)); |
| 1471 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1472 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1473 | MachineInstr *CallMI; |
| 1474 | // Emit the correct call instruction based on the type of symbol called. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1475 | if (GlobalAddressSDNode *GASD = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1476 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1477 | CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1478 | true); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1479 | } else if (ExternalSymbolSDNode *ESSDN = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1480 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1481 | CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1482 | true); |
| 1483 | } else { |
| 1484 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1485 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 1486 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 1487 | CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0) |
| 1488 | .addReg(PPC::R12); |
| 1489 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1490 | |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1491 | // Load the register args to virtual regs |
| 1492 | std::vector<unsigned> ArgVR; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1493 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1494 | ArgVR.push_back(SelectExpr(N.getOperand(i))); |
| 1495 | |
| 1496 | // Copy the virtual registers into the appropriate argument register |
| 1497 | for(int i = 0, e = ArgVR.size(); i < e; ++i) { |
| 1498 | switch(N.getOperand(i+2).getValueType()) { |
| 1499 | default: Node->dump(); assert(0 && "Unknown value type for call"); |
| 1500 | case MVT::i1: |
| 1501 | case MVT::i8: |
| 1502 | case MVT::i16: |
| 1503 | case MVT::i32: |
| 1504 | assert(GPR_idx < 8 && "Too many int args"); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1505 | if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1506 | BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1507 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1508 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1509 | ++GPR_idx; |
| 1510 | break; |
| 1511 | case MVT::f64: |
| 1512 | case MVT::f32: |
| 1513 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1514 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1515 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1516 | ++FPR_idx; |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1520 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1521 | // Put the call instruction in the correct place in the MachineBasicBlock |
| 1522 | BB->push_back(CallMI); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1523 | |
| 1524 | switch (Node->getValueType(0)) { |
| 1525 | default: assert(0 && "Unknown value type for call result!"); |
| 1526 | case MVT::Other: return 1; |
| 1527 | case MVT::i1: |
| 1528 | case MVT::i8: |
| 1529 | case MVT::i16: |
| 1530 | case MVT::i32: |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 1531 | if (Node->getValueType(1) == MVT::i32) { |
| 1532 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3); |
| 1533 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4); |
| 1534 | } else { |
| 1535 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
| 1536 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | case MVT::f32: |
| 1539 | case MVT::f64: |
| 1540 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1541 | break; |
| 1542 | } |
| 1543 | return Result+N.ResNo; |
| 1544 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1545 | |
| 1546 | case ISD::SIGN_EXTEND: |
| 1547 | case ISD::SIGN_EXTEND_INREG: |
| 1548 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1549 | switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1550 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1551 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1552 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1553 | break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1554 | case MVT::i8: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1555 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1556 | break; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1557 | case MVT::i1: |
| 1558 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0); |
| 1559 | break; |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1560 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1561 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1562 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1563 | case ISD::CopyFromReg: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1564 | DestType = N.getValue(0).getValueType(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1565 | if (Result == 1) |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1566 | Result = ExprMap[N.getValue(0)] = MakeReg(DestType); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1567 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1568 | if (MVT::isInteger(DestType)) |
| 1569 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1570 | else |
| 1571 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1572 | return Result; |
| 1573 | |
| 1574 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1575 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1576 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1577 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1578 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1579 | .addImm(31-Tmp2); |
| 1580 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1581 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1582 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1583 | } |
| 1584 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1585 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1586 | case ISD::SRL: |
| 1587 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1588 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1589 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1590 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1591 | .addImm(Tmp2).addImm(31); |
| 1592 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1593 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1594 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1595 | } |
| 1596 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1597 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1598 | case ISD::SRA: |
| 1599 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1600 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1601 | Tmp2 = CN->getValue() & 0x1F; |
| 1602 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1603 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1604 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1605 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1606 | } |
| 1607 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1608 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1609 | case ISD::CTLZ: |
| 1610 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1611 | BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1); |
| 1612 | return Result; |
| 1613 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1614 | case ISD::ADD: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1615 | if (!MVT::isInteger(DestType)) { |
| 1616 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1617 | N.getOperand(0).Val->hasOneUse()) { |
| 1618 | ++FusedFP; // Statistic |
| 1619 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1620 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1621 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1622 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1623 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1624 | return Result; |
| 1625 | } |
| 1626 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1627 | N.getOperand(1).Val->hasOneUse()) { |
| 1628 | ++FusedFP; // Statistic |
| 1629 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1630 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1631 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1632 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1633 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1634 | return Result; |
| 1635 | } |
| 1636 | Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; |
| 1637 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1638 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1639 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1640 | return Result; |
| 1641 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1642 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1643 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1644 | default: assert(0 && "unhandled result code"); |
| 1645 | case 0: // No immediate |
| 1646 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1647 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1648 | break; |
| 1649 | case 1: // Low immediate |
| 1650 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1651 | break; |
| 1652 | case 2: // Shifted immediate |
| 1653 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1654 | break; |
| 1655 | } |
| 1656 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1657 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1658 | case ISD::AND: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1659 | // FIXME: should add check in getImmediateForOpcode to return a value |
| 1660 | // indicating the immediate is a run of set bits so we can emit a bitfield |
| 1661 | // clear with RLWINM instead. |
| 1662 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1663 | default: assert(0 && "unhandled result code"); |
| 1664 | case 0: // No immediate |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1665 | // Check for andc: and, (xor a, -1), b |
| 1666 | if (N.getOperand(0).getOpcode() == ISD::XOR && |
| 1667 | N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant && |
| 1668 | cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) { |
| 1669 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1670 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1671 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1672 | return Result; |
| 1673 | } |
| 1674 | // It wasn't and-with-complement, emit a regular and |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1675 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1676 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1677 | Opc = Recording ? PPC::ANDo : PPC::AND; |
| 1678 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | case 1: // Low immediate |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1681 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1682 | BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1683 | break; |
| 1684 | case 2: // Shifted immediate |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1685 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1686 | BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1687 | break; |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1688 | case 5: // Bitfield mask |
| 1689 | Opc = Recording ? PPC::RLWINMo : PPC::RLWINM; |
| 1690 | Tmp3 = Tmp2 >> 16; // MB |
| 1691 | Tmp2 &= 0xFFFF; // ME |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1692 | |
| 1693 | if (N.getOperand(0).getOpcode() == ISD::SRL) |
| 1694 | if (ConstantSDNode *SA = |
| 1695 | dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) { |
| 1696 | |
| 1697 | // We can fold the RLWINM and the SRL together if the mask is |
| 1698 | // clearing the top bits which are rotated around. |
| 1699 | unsigned RotAmt = 32-(SA->getValue() & 31); |
| 1700 | if (Tmp2 <= RotAmt) { |
| 1701 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1702 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt) |
| 1703 | .addImm(Tmp3).addImm(Tmp2); |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1709 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0) |
| 1710 | .addImm(Tmp3).addImm(Tmp2); |
| 1711 | break; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1712 | } |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1713 | RecordSuccess = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1714 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1715 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1716 | case ISD::OR: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1717 | if (SelectBitfieldInsert(N, Result)) |
| 1718 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1719 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1720 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1721 | default: assert(0 && "unhandled result code"); |
| 1722 | case 0: // No immediate |
| 1723 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1724 | Opc = Recording ? PPC::ORo : PPC::OR; |
| 1725 | RecordSuccess = true; |
| 1726 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1727 | break; |
| 1728 | case 1: // Low immediate |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1729 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1730 | break; |
| 1731 | case 2: // Shifted immediate |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1732 | BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1733 | break; |
| 1734 | } |
| 1735 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1736 | |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1737 | case ISD::XOR: { |
| 1738 | // Check for EQV: xor, (xor a, -1), b |
| 1739 | if (N.getOperand(0).getOpcode() == ISD::XOR && |
| 1740 | N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant && |
| 1741 | cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1742 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1743 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1744 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1745 | return Result; |
| 1746 | } |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1747 | // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1 |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1748 | if (N.getOperand(1).getOpcode() == ISD::Constant && |
| 1749 | cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1750 | switch(N.getOperand(0).getOpcode()) { |
| 1751 | case ISD::OR: |
| 1752 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1753 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1754 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1755 | break; |
| 1756 | case ISD::AND: |
| 1757 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1758 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1759 | BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1760 | break; |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1761 | case ISD::XOR: |
| 1762 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1763 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1764 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1765 | break; |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1766 | default: |
| 1767 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1768 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1769 | break; |
| 1770 | } |
| 1771 | return Result; |
| 1772 | } |
| 1773 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1774 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1775 | default: assert(0 && "unhandled result code"); |
| 1776 | case 0: // No immediate |
| 1777 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1778 | BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1779 | break; |
| 1780 | case 1: // Low immediate |
| 1781 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1782 | break; |
| 1783 | case 2: // Shifted immediate |
| 1784 | BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1785 | break; |
| 1786 | } |
| 1787 | return Result; |
| 1788 | } |
| 1789 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1790 | case ISD::SUB: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1791 | if (!MVT::isInteger(DestType)) { |
| 1792 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1793 | N.getOperand(0).Val->hasOneUse()) { |
| 1794 | ++FusedFP; // Statistic |
| 1795 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1796 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1797 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1798 | Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS; |
| 1799 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1800 | return Result; |
| 1801 | } |
| 1802 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1803 | N.getOperand(1).Val->hasOneUse()) { |
| 1804 | ++FusedFP; // Statistic |
| 1805 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1806 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1807 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1808 | Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS; |
| 1809 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1810 | return Result; |
| 1811 | } |
| 1812 | Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; |
| 1813 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1814 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1815 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1816 | return Result; |
| 1817 | } |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1818 | if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) { |
| 1819 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1820 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1821 | } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1822 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1823 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1824 | } else { |
| 1825 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1826 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1827 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1828 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1829 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1830 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1831 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1832 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1833 | if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1834 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1835 | else { |
| 1836 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1837 | switch (DestType) { |
| 1838 | default: assert(0 && "Unknown type to ISD::MUL"); break; |
| 1839 | case MVT::i32: Opc = PPC::MULLW; break; |
| 1840 | case MVT::f32: Opc = PPC::FMULS; break; |
| 1841 | case MVT::f64: Opc = PPC::FMUL; break; |
| 1842 | } |
| 1843 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1844 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1845 | return Result; |
| 1846 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1847 | case ISD::MULHS: |
| 1848 | case ISD::MULHU: |
| 1849 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1850 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1851 | Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW; |
| 1852 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1853 | return Result; |
| 1854 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1855 | case ISD::SDIV: |
| 1856 | case ISD::UDIV: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1857 | switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) { |
| 1858 | default: break; |
| 1859 | // If this is an sdiv by a power of two, we can use an srawi/addze pair. |
| 1860 | case 3: |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 1861 | Tmp1 = MakeReg(MVT::i32); |
| 1862 | Tmp2 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1863 | if ((int)Tmp3 < 0) { |
| 1864 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1865 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3); |
| 1866 | BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1); |
| 1867 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4); |
| 1868 | } else { |
| 1869 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1870 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1); |
| 1871 | } |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 1872 | return Result; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1873 | // If this is a divide by constant, we can emit code using some magic |
| 1874 | // constants to implement it as a multiply instead. |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 1875 | case 4: |
| 1876 | ExprMap.erase(N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1877 | if (opcode == ISD::SDIV) |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 1878 | return SelectExpr(BuildSDIVSequence(N)); |
| 1879 | else |
| 1880 | return SelectExpr(BuildUDIVSequence(N)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame^] | 1881 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1882 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1883 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1884 | switch (DestType) { |
| 1885 | default: assert(0 && "Unknown type to ISD::SDIV"); break; |
| 1886 | case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break; |
| 1887 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 1888 | case MVT::f64: Opc = PPC::FDIV; break; |
| 1889 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1890 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1891 | return Result; |
| 1892 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1893 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1894 | case ISD::SUB_PARTS: { |
| 1895 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1896 | "Not an i64 add/sub!"); |
| 1897 | // Emit all of the operands. |
| 1898 | std::vector<unsigned> InVals; |
| 1899 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1900 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1901 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1902 | BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 1903 | BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1904 | } else { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1905 | BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]); |
| 1906 | BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]); |
| 1907 | } |
| 1908 | return Result+N.ResNo; |
| 1909 | } |
| 1910 | |
| 1911 | case ISD::SHL_PARTS: |
| 1912 | case ISD::SRA_PARTS: |
| 1913 | case ISD::SRL_PARTS: { |
| 1914 | assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 && |
| 1915 | "Not an i64 shift!"); |
| 1916 | unsigned ShiftOpLo = SelectExpr(N.getOperand(0)); |
| 1917 | unsigned ShiftOpHi = SelectExpr(N.getOperand(1)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1918 | unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2)); |
| 1919 | Tmp1 = MakeReg(MVT::i32); |
| 1920 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1921 | Tmp3 = MakeReg(MVT::i32); |
| 1922 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1923 | unsigned Tmp5 = MakeReg(MVT::i32); |
| 1924 | unsigned Tmp6 = MakeReg(MVT::i32); |
| 1925 | BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32); |
| 1926 | if (ISD::SHL_PARTS == opcode) { |
| 1927 | BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg); |
| 1928 | BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1); |
| 1929 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1930 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 1931 | BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1932 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6); |
| 1933 | BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg); |
| 1934 | } else if (ISD::SRL_PARTS == opcode) { |
| 1935 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1936 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1937 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1938 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1939 | BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1940 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6); |
| 1941 | BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1942 | } else { |
| 1943 | MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1944 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1945 | MachineBasicBlock *OldMBB = BB; |
| 1946 | MachineFunction *F = BB->getParent(); |
| 1947 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1948 | F->getBasicBlockList().insert(It, TmpMBB); |
| 1949 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1950 | BB->addSuccessor(TmpMBB); |
| 1951 | BB->addSuccessor(PhiMBB); |
| 1952 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1953 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1954 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1955 | BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1956 | BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1957 | BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1958 | BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 1959 | // Select correct least significant half if the shift amount > 32 |
| 1960 | BB = TmpMBB; |
| 1961 | unsigned Tmp7 = MakeReg(MVT::i32); |
| 1962 | BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6); |
| 1963 | TmpMBB->addSuccessor(PhiMBB); |
| 1964 | BB = PhiMBB; |
| 1965 | BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB) |
| 1966 | .addReg(Tmp7).addMBB(TmpMBB); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1967 | } |
| 1968 | return Result+N.ResNo; |
| 1969 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1970 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1971 | case ISD::FP_TO_UINT: |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 1972 | case ISD::FP_TO_SINT: { |
| 1973 | bool U = (ISD::FP_TO_UINT == opcode); |
| 1974 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1975 | if (!U) { |
| 1976 | Tmp2 = MakeReg(MVT::f64); |
| 1977 | BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1); |
| 1978 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 1979 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx); |
| 1980 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4); |
| 1981 | return Result; |
| 1982 | } else { |
| 1983 | unsigned Zero = getConstDouble(0.0); |
| 1984 | unsigned MaxInt = getConstDouble((1LL << 32) - 1); |
| 1985 | unsigned Border = getConstDouble(1LL << 31); |
| 1986 | unsigned UseZero = MakeReg(MVT::f64); |
| 1987 | unsigned UseMaxInt = MakeReg(MVT::f64); |
| 1988 | unsigned UseChoice = MakeReg(MVT::f64); |
| 1989 | unsigned TmpReg = MakeReg(MVT::f64); |
| 1990 | unsigned TmpReg2 = MakeReg(MVT::f64); |
| 1991 | unsigned ConvReg = MakeReg(MVT::f64); |
| 1992 | unsigned IntTmp = MakeReg(MVT::i32); |
| 1993 | unsigned XorReg = MakeReg(MVT::i32); |
| 1994 | MachineFunction *F = BB->getParent(); |
| 1995 | int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8); |
| 1996 | // Update machine-CFG edges |
| 1997 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1998 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1999 | MachineBasicBlock *OldMBB = BB; |
| 2000 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 2001 | F->getBasicBlockList().insert(It, XorMBB); |
| 2002 | F->getBasicBlockList().insert(It, PhiMBB); |
| 2003 | BB->addSuccessor(XorMBB); |
| 2004 | BB->addSuccessor(PhiMBB); |
| 2005 | // Convert from floating point to unsigned 32-bit value |
| 2006 | // Use 0 if incoming value is < 0.0 |
| 2007 | BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero); |
| 2008 | // Use 2**32 - 1 if incoming value is >= 2**32 |
| 2009 | BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1); |
| 2010 | BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero) |
| 2011 | .addReg(MaxInt); |
| 2012 | // Subtract 2**31 |
| 2013 | BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
| 2014 | // Use difference if >= 2**31 |
| 2015 | BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border); |
| 2016 | BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
| 2017 | .addReg(UseChoice); |
| 2018 | // Convert to integer |
| 2019 | BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 2020 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx); |
| 2021 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4); |
| 2022 | BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 2023 | BuildMI(BB, PPC::B, 1).addMBB(XorMBB); |
| 2024 | |
| 2025 | // XorMBB: |
| 2026 | // add 2**31 if input was >= 2**31 |
| 2027 | BB = XorMBB; |
| 2028 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
| 2029 | XorMBB->addSuccessor(PhiMBB); |
| 2030 | |
| 2031 | // PhiMBB: |
| 2032 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 2033 | BB = PhiMBB; |
| 2034 | BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB) |
| 2035 | .addReg(XorReg).addMBB(XorMBB); |
| 2036 | return Result; |
| 2037 | } |
| 2038 | assert(0 && "Should never get here"); |
| 2039 | return 0; |
| 2040 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2041 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 2042 | case ISD::SETCC: |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 2043 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2044 | if (ConstantSDNode *CN = |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2045 | dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) { |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2046 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 2047 | // Check for those cases here. |
| 2048 | // setcc op, 0 |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2049 | if (CN->getValue() == 0) { |
| 2050 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
| 2051 | switch (SetCC->getCondition()) { |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 2052 | default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort(); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2053 | case ISD::SETEQ: |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2054 | Tmp2 = MakeReg(MVT::i32); |
| 2055 | BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1); |
| 2056 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27) |
| 2057 | .addImm(5).addImm(31); |
| 2058 | break; |
| 2059 | case ISD::SETNE: |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2060 | Tmp2 = MakeReg(MVT::i32); |
| 2061 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 2062 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 2063 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2064 | case ISD::SETLT: |
| 2065 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1) |
| 2066 | .addImm(31).addImm(31); |
| 2067 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2068 | case ISD::SETGT: |
| 2069 | Tmp2 = MakeReg(MVT::i32); |
| 2070 | Tmp3 = MakeReg(MVT::i32); |
| 2071 | BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1); |
| 2072 | BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2073 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2074 | .addImm(31).addImm(31); |
| 2075 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2076 | } |
| 2077 | return Result; |
| 2078 | } |
| 2079 | // setcc op, -1 |
| 2080 | if (CN->isAllOnesValue()) { |
| 2081 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
| 2082 | switch (SetCC->getCondition()) { |
| 2083 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 2084 | case ISD::SETEQ: |
| 2085 | Tmp2 = MakeReg(MVT::i32); |
| 2086 | Tmp3 = MakeReg(MVT::i32); |
| 2087 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2088 | BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0); |
| 2089 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2090 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2091 | case ISD::SETNE: |
| 2092 | Tmp2 = MakeReg(MVT::i32); |
| 2093 | Tmp3 = MakeReg(MVT::i32); |
| 2094 | BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2095 | BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1); |
| 2096 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2); |
| 2097 | break; |
| 2098 | case ISD::SETLT: |
| 2099 | Tmp2 = MakeReg(MVT::i32); |
| 2100 | Tmp3 = MakeReg(MVT::i32); |
| 2101 | BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2102 | BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2103 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2104 | .addImm(31).addImm(31); |
| 2105 | break; |
| 2106 | case ISD::SETGT: |
| 2107 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2108 | BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1) |
| 2109 | .addImm(31).addImm(31); |
| 2110 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1); |
| 2111 | break; |
| 2112 | } |
| 2113 | return Result; |
| 2114 | } |
| 2115 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2116 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2117 | bool Inv; |
| 2118 | unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2); |
| 2119 | MoveCRtoGPR(CCReg, Inv, Tmp2, Result); |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 2120 | return Result; |
| 2121 | } |
| 2122 | assert(0 && "Is this legal?"); |
| 2123 | return 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2124 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2125 | case ISD::SELECT: { |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2126 | SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val); |
| 2127 | if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC && |
| 2128 | !MVT::isInteger(SetCC->getOperand(0).getValueType()) && |
| 2129 | !MVT::isInteger(N.getOperand(1).getValueType()) && |
| 2130 | !MVT::isInteger(N.getOperand(2).getValueType()) && |
| 2131 | SetCC->getCondition() != ISD::SETEQ && |
| 2132 | SetCC->getCondition() != ISD::SETNE) { |
| 2133 | MVT::ValueType VT = SetCC->getOperand(0).getValueType(); |
| 2134 | unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE |
| 2135 | unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE |
| 2136 | |
| 2137 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)); |
| 2138 | if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) { |
| 2139 | switch(SetCC->getCondition()) { |
| 2140 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2141 | case ISD::SETULT: |
| 2142 | case ISD::SETLT: |
| 2143 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2144 | case ISD::SETUGE: |
| 2145 | case ISD::SETGE: |
| 2146 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2147 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV); |
| 2148 | return Result; |
| 2149 | case ISD::SETUGT: |
| 2150 | case ISD::SETGT: |
| 2151 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2152 | case ISD::SETULE: |
| 2153 | case ISD::SETLE: { |
| 2154 | if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) { |
| 2155 | Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0)); |
| 2156 | } else { |
| 2157 | Tmp2 = MakeReg(VT); |
| 2158 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2159 | BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1); |
| 2160 | } |
| 2161 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV); |
| 2162 | return Result; |
| 2163 | } |
| 2164 | } |
| 2165 | } else { |
| 2166 | Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS; |
| 2167 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2168 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
| 2169 | Tmp3 = MakeReg(VT); |
| 2170 | switch(SetCC->getCondition()) { |
| 2171 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2172 | case ISD::SETULT: |
| 2173 | case ISD::SETLT: |
| 2174 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2175 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2176 | return Result; |
| 2177 | case ISD::SETUGE: |
| 2178 | case ISD::SETGE: |
| 2179 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2180 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2181 | return Result; |
| 2182 | case ISD::SETUGT: |
| 2183 | case ISD::SETGT: |
| 2184 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2185 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2186 | return Result; |
| 2187 | case ISD::SETULE: |
| 2188 | case ISD::SETLE: |
| 2189 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2190 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2191 | return Result; |
| 2192 | } |
| 2193 | } |
| 2194 | assert(0 && "Should never get here"); |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2198 | bool Inv; |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2199 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
| 2200 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2201 | unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3); |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2202 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2203 | // Create an iterator with which to insert the MBB for copying the false |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2204 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 2205 | MachineBasicBlock *thisMBB = BB; |
| 2206 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 2207 | ilist<MachineBasicBlock>::iterator It = BB; |
| 2208 | ++It; |
| 2209 | |
| 2210 | // thisMBB: |
| 2211 | // ... |
| 2212 | // TrueVal = ... |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2213 | // cmpTY ccX, r1, r2 |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2214 | // bCC copy1MBB |
| 2215 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2216 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 2217 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2218 | BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2219 | MachineFunction *F = BB->getParent(); |
| 2220 | F->getBasicBlockList().insert(It, copy0MBB); |
| 2221 | F->getBasicBlockList().insert(It, sinkMBB); |
| 2222 | // Update machine-CFG edges |
| 2223 | BB->addSuccessor(copy0MBB); |
| 2224 | BB->addSuccessor(sinkMBB); |
| 2225 | |
| 2226 | // copy0MBB: |
| 2227 | // %FalseValue = ... |
| 2228 | // # fallthrough to sinkMBB |
| 2229 | BB = copy0MBB; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2230 | // Update machine-CFG edges |
| 2231 | BB->addSuccessor(sinkMBB); |
| 2232 | |
| 2233 | // sinkMBB: |
| 2234 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 2235 | // ... |
| 2236 | BB = sinkMBB; |
| 2237 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 2238 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2239 | return Result; |
| 2240 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2241 | |
| 2242 | case ISD::Constant: |
| 2243 | switch (N.getValueType()) { |
| 2244 | default: assert(0 && "Cannot use constants of this type!"); |
| 2245 | case MVT::i1: |
| 2246 | BuildMI(BB, PPC::LI, 1, Result) |
| 2247 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 2248 | break; |
| 2249 | case MVT::i32: |
| 2250 | { |
| 2251 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 2252 | if (v < 32768 && v >= -32768) { |
| 2253 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 2254 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 2255 | Tmp1 = MakeReg(MVT::i32); |
| 2256 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 2257 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | return Result; |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2262 | |
| 2263 | case ISD::ConstantFP: { |
| 2264 | ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N); |
| 2265 | Result = getConstDouble(CN->getValue(), Result); |
| 2266 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2269 | case ISD::FNEG: |
| 2270 | if (!NoExcessFPPrecision && |
| 2271 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2272 | N.getOperand(0).Val->hasOneUse() && |
| 2273 | ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() && |
| 2274 | N.getOperand(0).getOperand(0).Val->hasOneUse()) { |
| 2275 | ++FusedFP; // Statistic |
| 2276 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0)); |
| 2277 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1)); |
| 2278 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 2279 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2280 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2281 | } else if (!NoExcessFPPrecision && |
| 2282 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2283 | N.getOperand(0).Val->hasOneUse() && |
| 2284 | ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() && |
| 2285 | N.getOperand(0).getOperand(1).Val->hasOneUse()) { |
| 2286 | ++FusedFP; // Statistic |
| 2287 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0)); |
| 2288 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1)); |
| 2289 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2290 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2291 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2292 | } else if (ISD::FABS == N.getOperand(0).getOpcode()) { |
| 2293 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2294 | BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1); |
| 2295 | } else { |
| 2296 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2297 | BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1); |
| 2298 | } |
| 2299 | return Result; |
| 2300 | |
| 2301 | case ISD::FABS: |
| 2302 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2303 | BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1); |
| 2304 | return Result; |
| 2305 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 2306 | case ISD::FSQRT: |
| 2307 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2308 | Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS; |
| 2309 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2310 | return Result; |
| 2311 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2312 | case ISD::FP_ROUND: |
| 2313 | assert (DestType == MVT::f32 && |
| 2314 | N.getOperand(0).getValueType() == MVT::f64 && |
| 2315 | "only f64 to f32 conversion supported here"); |
| 2316 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2317 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 2318 | return Result; |
| 2319 | |
| 2320 | case ISD::FP_EXTEND: |
| 2321 | assert (DestType == MVT::f64 && |
| 2322 | N.getOperand(0).getValueType() == MVT::f32 && |
| 2323 | "only f32 to f64 conversion supported here"); |
| 2324 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2325 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 2326 | return Result; |
| 2327 | |
| 2328 | case ISD::UINT_TO_FP: |
| 2329 | case ISD::SINT_TO_FP: { |
| 2330 | assert (N.getOperand(0).getValueType() == MVT::i32 |
| 2331 | && "int to float must operate on i32"); |
| 2332 | bool IsUnsigned = (ISD::UINT_TO_FP == opcode); |
| 2333 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2334 | Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into |
| 2335 | Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant |
| 2336 | |
| 2337 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 2338 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 2339 | |
| 2340 | if (IsUnsigned) { |
| 2341 | unsigned ConstF = getConstDouble(0x1.000000p52); |
| 2342 | // Store the hi & low halves of the fp value, currently in int regs |
| 2343 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2344 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2345 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4); |
| 2346 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2347 | // Generate the return value with a subtract |
| 2348 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2349 | } else { |
| 2350 | unsigned ConstF = getConstDouble(0x1.000008p52); |
| 2351 | unsigned TmpL = MakeReg(MVT::i32); |
| 2352 | // Store the hi & low halves of the fp value, currently in int regs |
| 2353 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2354 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2355 | BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000); |
| 2356 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4); |
| 2357 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2358 | // Generate the return value with a subtract |
| 2359 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2360 | } |
| 2361 | return Result; |
| 2362 | } |
| 2363 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2364 | return 0; |
| 2365 | } |
| 2366 | |
| 2367 | void ISel::Select(SDOperand N) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2368 | unsigned Tmp1, Tmp2, Tmp3, Opc; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2369 | unsigned opcode = N.getOpcode(); |
| 2370 | |
| 2371 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 2372 | return; // Already selected. |
| 2373 | |
| 2374 | SDNode *Node = N.Val; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2375 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2376 | switch (Node->getOpcode()) { |
| 2377 | default: |
| 2378 | Node->dump(); std::cerr << "\n"; |
| 2379 | assert(0 && "Node not handled yet!"); |
| 2380 | case ISD::EntryToken: return; // Noop |
| 2381 | case ISD::TokenFactor: |
| 2382 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 2383 | Select(Node->getOperand(i)); |
| 2384 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2385 | case ISD::CALLSEQ_START: |
| 2386 | case ISD::CALLSEQ_END: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2387 | Select(N.getOperand(0)); |
| 2388 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2389 | Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN : |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2390 | PPC::ADJCALLSTACKUP; |
| 2391 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 2392 | return; |
| 2393 | case ISD::BR: { |
| 2394 | MachineBasicBlock *Dest = |
| 2395 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2396 | Select(N.getOperand(0)); |
| 2397 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 2398 | return; |
| 2399 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2400 | case ISD::BRCOND: |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 2401 | case ISD::BRCONDTWOWAY: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2402 | SelectBranchCC(N); |
| 2403 | return; |
| 2404 | case ISD::CopyToReg: |
| 2405 | Select(N.getOperand(0)); |
| 2406 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2407 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2408 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2409 | if (Tmp1 != Tmp2) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2410 | if (N.getOperand(1).getValueType() == MVT::f64 || |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2411 | N.getOperand(1).getValueType() == MVT::f32) |
| 2412 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 2413 | else |
| 2414 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2415 | } |
| 2416 | return; |
| 2417 | case ISD::ImplicitDef: |
| 2418 | Select(N.getOperand(0)); |
| 2419 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 2420 | return; |
| 2421 | case ISD::RET: |
| 2422 | switch (N.getNumOperands()) { |
| 2423 | default: |
| 2424 | assert(0 && "Unknown return instruction!"); |
| 2425 | case 3: |
| 2426 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 2427 | N.getOperand(2).getValueType() == MVT::i32 && |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 2428 | "Unknown two-register value!"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2429 | Select(N.getOperand(0)); |
| 2430 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2431 | Tmp2 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 2432 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2); |
| 2433 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2434 | break; |
| 2435 | case 2: |
| 2436 | Select(N.getOperand(0)); |
| 2437 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2438 | switch (N.getOperand(1).getValueType()) { |
| 2439 | default: |
| 2440 | assert(0 && "Unknown return type!"); |
| 2441 | case MVT::f64: |
| 2442 | case MVT::f32: |
| 2443 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 2444 | break; |
| 2445 | case MVT::i32: |
| 2446 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 2447 | break; |
| 2448 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 2449 | case 1: |
| 2450 | Select(N.getOperand(0)); |
| 2451 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2452 | } |
| 2453 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 2454 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2455 | case ISD::TRUNCSTORE: |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2456 | case ISD::STORE: { |
| 2457 | SDOperand Chain = N.getOperand(0); |
| 2458 | SDOperand Value = N.getOperand(1); |
| 2459 | SDOperand Address = N.getOperand(2); |
| 2460 | Select(Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2461 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2462 | Tmp1 = SelectExpr(Value); //value |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2463 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2464 | if (opcode == ISD::STORE) { |
| 2465 | switch(Value.getValueType()) { |
| 2466 | default: assert(0 && "unknown Type in store"); |
| 2467 | case MVT::i32: Opc = PPC::STW; break; |
| 2468 | case MVT::f64: Opc = PPC::STFD; break; |
| 2469 | case MVT::f32: Opc = PPC::STFS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2470 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2471 | } else { //ISD::TRUNCSTORE |
| 2472 | switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) { |
| 2473 | default: assert(0 && "unknown Type in store"); |
| 2474 | case MVT::i1: |
| 2475 | case MVT::i8: Opc = PPC::STB; break; |
| 2476 | case MVT::i16: Opc = PPC::STH; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2477 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2478 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2479 | |
| 2480 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 2481 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 2482 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
| 2483 | } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){ |
| 2484 | GlobalValue *GV = GN->getGlobal(); |
| 2485 | Tmp2 = MakeReg(MVT::i32); |
| 2486 | if (PICEnabled) |
| 2487 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 2488 | .addGlobalAddress(GV); |
| 2489 | else |
| 2490 | BuildMI(BB, PPC::LIS, 2, Tmp2).addGlobalAddress(GV); |
| 2491 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 2492 | Tmp3 = MakeReg(MVT::i32); |
| 2493 | BuildMI(BB, PPC::LWZ, 2, Tmp3).addGlobalAddress(GV).addReg(Tmp2); |
Nate Begeman | 7b4f0a8 | 2005-07-25 21:15:28 +0000 | [diff] [blame] | 2494 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(0).addReg(Tmp3); |
| 2495 | } else { |
| 2496 | BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2497 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2498 | } else { |
| 2499 | int offset; |
| 2500 | bool idx = SelectAddr(Address, Tmp2, offset); |
| 2501 | if (idx) { |
| 2502 | Opc = IndexedOpForOp(Opc); |
| 2503 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
| 2504 | } else { |
| 2505 | BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2); |
| 2506 | } |
| 2507 | } |
| 2508 | return; |
| 2509 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2510 | case ISD::EXTLOAD: |
| 2511 | case ISD::SEXTLOAD: |
| 2512 | case ISD::ZEXTLOAD: |
| 2513 | case ISD::LOAD: |
| 2514 | case ISD::CopyFromReg: |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 2515 | case ISD::TAILCALL: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2516 | case ISD::CALL: |
| 2517 | case ISD::DYNAMIC_STACKALLOC: |
| 2518 | ExprMap.erase(N); |
| 2519 | SelectExpr(N); |
| 2520 | return; |
| 2521 | } |
| 2522 | assert(0 && "Should not be reached!"); |
| 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 2527 | /// into a machine code representation using pattern matching and a machine |
| 2528 | /// description file. |
| 2529 | /// |
| 2530 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2531 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |