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