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