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