Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAGISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/BitVector.h" |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 20 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 24 | #include "llvm/InlineAsm.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Intrinsics.h" |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 27 | #include "llvm/IntrinsicInst.h" |
Reid Spencer | 5694b6e | 2007-04-09 06:17:21 +0000 | [diff] [blame] | 28 | #include "llvm/ParameterAttributes.h" |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/Collector.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 34 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetData.h" |
| 40 | #include "llvm/Target/TargetFrameInfo.h" |
| 41 | #include "llvm/Target/TargetInstrInfo.h" |
| 42 | #include "llvm/Target/TargetLowering.h" |
| 43 | #include "llvm/Target/TargetMachine.h" |
Vladimir Prus | 1247291 | 2006-05-23 13:43:15 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Compiler.h" |
Jeff Cohen | 7e88103 | 2006-02-24 02:52:40 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 51 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 53 | ViewISelDAGs("view-isel-dags", cl::Hidden, |
| 54 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 55 | static cl::opt<bool> |
| 56 | ViewSchedDAGs("view-sched-dags", cl::Hidden, |
| 57 | cl::desc("Pop up a window to show sched dags as they are processed")); |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
| 59 | ViewSUnitDAGs("view-sunit-dags", cl::Hidden, |
Chris Lattner | 5bab785 | 2008-01-25 17:24:52 +0000 | [diff] [blame] | 60 | cl::desc("Pop up a window to show SUnit dags after they are processed")); |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 61 | #else |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 62 | static const bool ViewISelDAGs = 0, ViewSchedDAGs = 0, ViewSUnitDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 65 | //===---------------------------------------------------------------------===// |
| 66 | /// |
| 67 | /// RegisterScheduler class - Track the registration of instruction schedulers. |
| 68 | /// |
| 69 | //===---------------------------------------------------------------------===// |
| 70 | MachinePassRegistry RegisterScheduler::Registry; |
| 71 | |
| 72 | //===---------------------------------------------------------------------===// |
| 73 | /// |
| 74 | /// ISHeuristic command line option for instruction schedulers. |
| 75 | /// |
| 76 | //===---------------------------------------------------------------------===// |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 77 | namespace { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 78 | cl::opt<RegisterScheduler::FunctionPassCtor, false, |
| 79 | RegisterPassParser<RegisterScheduler> > |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 80 | ISHeuristic("pre-RA-sched", |
Chris Lattner | 3700f90 | 2006-08-03 00:18:59 +0000 | [diff] [blame] | 81 | cl::init(&createDefaultScheduler), |
Chris Lattner | 5bab785 | 2008-01-25 17:24:52 +0000 | [diff] [blame] | 82 | cl::desc("Instruction schedulers available (before register" |
| 83 | " allocation):")); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 84 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 85 | static RegisterScheduler |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 86 | defaultListDAGScheduler("default", " Best scheduler for the target", |
| 87 | createDefaultScheduler); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 90 | namespace { struct SDISelAsmOperandInfo; } |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 91 | |
Chris Lattner | f899fce | 2008-04-27 23:48:12 +0000 | [diff] [blame] | 92 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 93 | /// MVT::ValueTypes that represent all the individual underlying |
| 94 | /// non-aggregate types that comprise it. |
| 95 | static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, |
| 96 | SmallVectorImpl<MVT::ValueType> &ValueVTs) { |
| 97 | // Given a struct type, recursively traverse the elements. |
| 98 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 99 | for (StructType::element_iterator EI = STy->element_begin(), |
| 100 | EB = STy->element_end(); |
| 101 | EI != EB; ++EI) |
| 102 | ComputeValueVTs(TLI, *EI, ValueVTs); |
| 103 | return; |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | f899fce | 2008-04-27 23:48:12 +0000 | [diff] [blame] | 105 | // Given an array type, recursively traverse the elements. |
| 106 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 107 | const Type *EltTy = ATy->getElementType(); |
| 108 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 109 | ComputeValueVTs(TLI, EltTy, ValueVTs); |
| 110 | return; |
| 111 | } |
| 112 | // Base case: we can get an MVT::ValueType for this LLVM IR type. |
| 113 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 114 | } |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 115 | |
Chris Lattner | f899fce | 2008-04-27 23:48:12 +0000 | [diff] [blame] | 116 | namespace { |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 117 | /// RegsForValue - This struct represents the registers (physical or virtual) |
| 118 | /// that a particular set of values is assigned, and the type information about |
| 119 | /// the value. The most common situation is to represent one value at a time, |
| 120 | /// but struct or array values are handled element-wise as multiple values. |
| 121 | /// The splitting of aggregates is performed recursively, so that we never |
| 122 | /// have aggregate-typed registers. The values at this point do not necessarily |
| 123 | /// have legal types, so each value may require one or more registers of some |
| 124 | /// legal type. |
| 125 | /// |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 126 | struct VISIBILITY_HIDDEN RegsForValue { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 127 | /// TLI - The TargetLowering object. |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 128 | /// |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 129 | const TargetLowering *TLI; |
| 130 | |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 131 | /// ValueVTs - The value types of the values, which may not be legal, and |
| 132 | /// may need be promoted or synthesized from one or more registers. |
| 133 | /// |
| 134 | SmallVector<MVT::ValueType, 4> ValueVTs; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 136 | /// RegVTs - The value types of the registers. This is the same size as |
| 137 | /// ValueVTs and it records, for each value, what the type of the assigned |
| 138 | /// register or registers are. (Individual values are never synthesized |
| 139 | /// from more than one type of register.) |
| 140 | /// |
| 141 | /// With virtual registers, the contents of RegVTs is redundant with TLI's |
| 142 | /// getRegisterType member function, however when with physical registers |
| 143 | /// it is necessary to have a separate record of the types. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 144 | /// |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 145 | SmallVector<MVT::ValueType, 4> RegVTs; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 146 | |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 147 | /// Regs - This list holds the registers assigned to the values. |
| 148 | /// Each legal or promoted value requires one register, and each |
| 149 | /// expanded value requires multiple registers. |
| 150 | /// |
| 151 | SmallVector<unsigned, 4> Regs; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 152 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 153 | RegsForValue() : TLI(0) {} |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 154 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 155 | RegsForValue(const TargetLowering &tli, |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 156 | const SmallVector<unsigned, 4> ®s, |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 157 | MVT::ValueType regvt, MVT::ValueType valuevt) |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 158 | : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {} |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 159 | RegsForValue(const TargetLowering &tli, |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 160 | const SmallVector<unsigned, 4> ®s, |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 161 | const SmallVector<MVT::ValueType, 4> ®vts, |
| 162 | const SmallVector<MVT::ValueType, 4> &valuevts) |
Dan Gohman | 0fe0090 | 2008-04-28 18:10:39 +0000 | [diff] [blame] | 163 | : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {} |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 164 | RegsForValue(const TargetLowering &tli, |
| 165 | unsigned Reg, const Type *Ty) : TLI(&tli) { |
| 166 | ComputeValueVTs(tli, Ty, ValueVTs); |
| 167 | |
Dan Gohman | b20d4f8 | 2008-04-28 17:42:03 +0000 | [diff] [blame] | 168 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 169 | MVT::ValueType ValueVT = ValueVTs[Value]; |
| 170 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 171 | MVT::ValueType RegisterVT = TLI->getRegisterType(ValueVT); |
| 172 | for (unsigned i = 0; i != NumRegs; ++i) |
| 173 | Regs.push_back(Reg + i); |
| 174 | RegVTs.push_back(RegisterVT); |
| 175 | Reg += NumRegs; |
| 176 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 41f6259 | 2008-04-29 04:29:54 +0000 | [diff] [blame] | 179 | /// append - Add the specified values to this one. |
| 180 | void append(const RegsForValue &RHS) { |
| 181 | TLI = RHS.TLI; |
| 182 | ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end()); |
| 183 | RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end()); |
| 184 | Regs.append(RHS.Regs.begin(), RHS.Regs.end()); |
| 185 | } |
| 186 | |
| 187 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 188 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 189 | /// this value and returns the result as a ValueVTs value. This uses |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 190 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 191 | /// If the Flag pointer is NULL, no flag is used. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 192 | SDOperand getCopyFromRegs(SelectionDAG &DAG, |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 193 | SDOperand &Chain, SDOperand *Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 194 | |
| 195 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 196 | /// specified value into the registers specified by this object. This uses |
| 197 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 198 | /// If the Flag pointer is NULL, no flag is used. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 199 | void getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 200 | SDOperand &Chain, SDOperand *Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 201 | |
| 202 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 203 | /// operand list. This adds the code marker and includes the number of |
| 204 | /// values added into it. |
| 205 | void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 206 | std::vector<SDOperand> &Ops) const; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 207 | }; |
| 208 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 210 | namespace llvm { |
| 211 | //===--------------------------------------------------------------------===// |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 212 | /// createDefaultScheduler - This creates an instruction scheduler appropriate |
| 213 | /// for the target. |
| 214 | ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS, |
| 215 | SelectionDAG *DAG, |
| 216 | MachineBasicBlock *BB) { |
| 217 | TargetLowering &TLI = IS->getTargetLowering(); |
| 218 | |
| 219 | if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) { |
| 220 | return createTDListDAGScheduler(IS, DAG, BB); |
| 221 | } else { |
| 222 | assert(TLI.getSchedulingPreference() == |
| 223 | TargetLowering::SchedulingForRegPressure && "Unknown sched type!"); |
| 224 | return createBURRListDAGScheduler(IS, DAG, BB); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | //===--------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 230 | /// FunctionLoweringInfo - This contains information that is global to a |
| 231 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 232 | class FunctionLoweringInfo { |
| 233 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 234 | TargetLowering &TLI; |
| 235 | Function &Fn; |
| 236 | MachineFunction &MF; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 237 | MachineRegisterInfo &RegInfo; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 238 | |
| 239 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 240 | |
| 241 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 242 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 243 | |
| 244 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 245 | /// we must remember which virtual registers hold the values for |
| 246 | /// cross-basic-block values. |
Chris Lattner | 9f24ad7 | 2007-02-04 01:35:11 +0000 | [diff] [blame] | 247 | DenseMap<const Value*, unsigned> ValueMap; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 248 | |
| 249 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 250 | /// the entry block. This allows the allocas to be efficiently referenced |
| 251 | /// anywhere in the function. |
| 252 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 253 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 254 | #ifndef NDEBUG |
| 255 | SmallSet<Instruction*, 8> CatchInfoLost; |
| 256 | SmallSet<Instruction*, 8> CatchInfoFound; |
| 257 | #endif |
| 258 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 259 | unsigned MakeReg(MVT::ValueType VT) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 260 | return RegInfo.createVirtualRegister(TLI.getRegClassFor(VT)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 262 | |
| 263 | /// isExportedInst - Return true if the specified value is an instruction |
| 264 | /// exported from its block. |
| 265 | bool isExportedInst(const Value *V) { |
| 266 | return ValueMap.count(V); |
| 267 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 269 | unsigned CreateRegForValue(const Value *V); |
| 270 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 271 | unsigned InitializeRegForValue(const Value *V) { |
| 272 | unsigned &R = ValueMap[V]; |
| 273 | assert(R == 0 && "Already initialized this value register!"); |
| 274 | return R = CreateRegForValue(V); |
| 275 | } |
| 276 | }; |
| 277 | } |
| 278 | |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 279 | /// isSelector - Return true if this instruction is a call to the |
| 280 | /// eh.selector intrinsic. |
| 281 | static bool isSelector(Instruction *I) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 282 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 283 | return (II->getIntrinsicID() == Intrinsic::eh_selector_i32 || |
| 284 | II->getIntrinsicID() == Intrinsic::eh_selector_i64); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 288 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 289 | /// PHI nodes or outside of the basic block that defines it, or used by a |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 290 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 291 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 292 | if (isa<PHINode>(I)) return true; |
| 293 | BasicBlock *BB = I->getParent(); |
| 294 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 295 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 296 | // FIXME: Remove switchinst special case. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 297 | isa<SwitchInst>(*UI)) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 298 | return true; |
| 299 | return false; |
| 300 | } |
| 301 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 302 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 303 | /// entry block, return true. This includes arguments used by switches, since |
| 304 | /// the switch may expand into multiple basic blocks. |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 305 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 306 | BasicBlock *Entry = A->getParent()->begin(); |
| 307 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 308 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 309 | return false; // Use not in entry block. |
| 310 | return true; |
| 311 | } |
| 312 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 313 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 314 | Function &fn, MachineFunction &mf) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 315 | : TLI(tli), Fn(fn), MF(mf), RegInfo(MF.getRegInfo()) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 316 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 317 | // Create a vreg for each argument register that is not dead and is used |
| 318 | // outside of the entry block for the function. |
| 319 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 320 | AI != E; ++AI) |
| 321 | if (!isOnlyUsedInEntryBlock(AI)) |
| 322 | InitializeRegForValue(AI); |
| 323 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 324 | // Initialize the mapping of values to registers. This is only set up for |
| 325 | // instruction values that are used outside of the block that defines |
| 326 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 327 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 328 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 329 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 330 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 331 | const Type *Ty = AI->getAllocatedType(); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 332 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 333 | unsigned Align = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 334 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 335 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 336 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 337 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 338 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 339 | StaticAllocaMap[AI] = |
Chris Lattner | 6266c18 | 2007-04-25 04:08:28 +0000 | [diff] [blame] | 340 | MF.getFrameInfo()->CreateStackObject(TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 343 | for (; BB != EB; ++BB) |
| 344 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 345 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 346 | if (!isa<AllocaInst>(I) || |
| 347 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 348 | InitializeRegForValue(I); |
| 349 | |
| 350 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 351 | // also creates the initial PHI MachineInstrs, though none of the input |
| 352 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 353 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 354 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 355 | MBBMap[BB] = MBB; |
| 356 | MF.getBasicBlockList().push_back(MBB); |
| 357 | |
| 358 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 359 | // appropriate. |
| 360 | PHINode *PN; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 361 | for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){ |
| 362 | if (PN->use_empty()) continue; |
| 363 | |
| 364 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 365 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 366 | unsigned PHIReg = ValueMap[PN]; |
| 367 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 368 | const TargetInstrInfo *TII = TLI.getTargetMachine().getInstrInfo(); |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 369 | for (unsigned i = 0; i != NumRegisters; ++i) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 370 | BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 375 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 376 | /// the correctly promoted or expanded types. Assign these registers |
| 377 | /// consecutive vreg numbers and return the first assigned number. |
Dan Gohman | 10a6b7a | 2008-04-28 18:19:43 +0000 | [diff] [blame] | 378 | /// |
| 379 | /// In the case that the given value has struct or array type, this function |
| 380 | /// will assign registers for each member or element. |
| 381 | /// |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 382 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 383 | SmallVector<MVT::ValueType, 4> ValueVTs; |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 384 | ComputeValueVTs(TLI, V->getType(), ValueVTs); |
Bill Wendling | 95b3955 | 2007-04-24 21:13:23 +0000 | [diff] [blame] | 385 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 386 | unsigned FirstReg = 0; |
Dan Gohman | b20d4f8 | 2008-04-28 17:42:03 +0000 | [diff] [blame] | 387 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 388 | MVT::ValueType ValueVT = ValueVTs[Value]; |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 389 | MVT::ValueType RegisterVT = TLI.getRegisterType(ValueVT); |
Dan Gohman | 8c8c5fc | 2007-06-27 14:34:07 +0000 | [diff] [blame] | 390 | |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 391 | unsigned NumRegs = TLI.getNumRegisters(ValueVT); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 392 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 393 | unsigned R = MakeReg(RegisterVT); |
| 394 | if (!FirstReg) FirstReg = R; |
| 395 | } |
| 396 | } |
| 397 | return FirstReg; |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 399 | |
| 400 | //===----------------------------------------------------------------------===// |
| 401 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 402 | /// implementation that is parameterized by a TargetLowering object. |
| 403 | /// Also, targets can overload any lowering method. |
| 404 | /// |
| 405 | namespace llvm { |
| 406 | class SelectionDAGLowering { |
| 407 | MachineBasicBlock *CurMBB; |
| 408 | |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 409 | DenseMap<const Value*, SDOperand> NodeMap; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 410 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 411 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 412 | /// them up and then emit token factor nodes when possible. This allows us to |
| 413 | /// get simple disambiguation between loads without worrying about alias |
| 414 | /// analysis. |
| 415 | std::vector<SDOperand> PendingLoads; |
| 416 | |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 417 | /// PendingExports - CopyToReg nodes that copy values to virtual registers |
| 418 | /// for export to other blocks need to be emitted before any terminator |
| 419 | /// instruction, but they have no other ordering requirements. We bunch them |
| 420 | /// up and the emit a single tokenfactor for them just before terminator |
| 421 | /// instructions. |
| 422 | std::vector<SDOperand> PendingExports; |
| 423 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 424 | /// Case - A struct to record the Value for a switch case, and the |
| 425 | /// case's target basic block. |
| 426 | struct Case { |
| 427 | Constant* Low; |
| 428 | Constant* High; |
| 429 | MachineBasicBlock* BB; |
| 430 | |
| 431 | Case() : Low(0), High(0), BB(0) { } |
| 432 | Case(Constant* low, Constant* high, MachineBasicBlock* bb) : |
| 433 | Low(low), High(high), BB(bb) { } |
| 434 | uint64_t size() const { |
| 435 | uint64_t rHigh = cast<ConstantInt>(High)->getSExtValue(); |
| 436 | uint64_t rLow = cast<ConstantInt>(Low)->getSExtValue(); |
| 437 | return (rHigh - rLow + 1ULL); |
| 438 | } |
| 439 | }; |
| 440 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 441 | struct CaseBits { |
| 442 | uint64_t Mask; |
| 443 | MachineBasicBlock* BB; |
| 444 | unsigned Bits; |
| 445 | |
| 446 | CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits): |
| 447 | Mask(mask), BB(bb), Bits(bits) { } |
| 448 | }; |
| 449 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 450 | typedef std::vector<Case> CaseVector; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 451 | typedef std::vector<CaseBits> CaseBitsVector; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 452 | typedef CaseVector::iterator CaseItr; |
| 453 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 454 | |
| 455 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 456 | /// of conditional branches. |
| 457 | struct CaseRec { |
| 458 | CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) : |
| 459 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 460 | |
| 461 | /// CaseBB - The MBB in which to emit the compare and branch |
| 462 | MachineBasicBlock *CaseBB; |
| 463 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 464 | /// greater-than-or-equal-to these Constants. |
| 465 | Constant *LT; |
| 466 | Constant *GE; |
| 467 | /// Range - A pair of iterators representing the range of case values to be |
| 468 | /// processed at this point in the binary search tree. |
| 469 | CaseRange Range; |
| 470 | }; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 471 | |
| 472 | typedef std::vector<CaseRec> CaseRecVector; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 473 | |
| 474 | /// The comparison function for sorting the switch case values in the vector. |
| 475 | /// WARNING: Case ranges should be disjoint! |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 476 | struct CaseCmp { |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 477 | bool operator () (const Case& C1, const Case& C2) { |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 478 | assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); |
| 479 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 480 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 481 | return CI1->getValue().slt(CI2->getValue()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 482 | } |
| 483 | }; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 484 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 485 | struct CaseBitsCmp { |
| 486 | bool operator () (const CaseBits& C1, const CaseBits& C2) { |
| 487 | return C1.Bits > C2.Bits; |
| 488 | } |
| 489 | }; |
| 490 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 491 | unsigned Clusterify(CaseVector& Cases, const SwitchInst &SI); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 493 | public: |
| 494 | // TLI - This is information that describes the available target features we |
| 495 | // need for lowering. This indicates when operations are unavailable, |
| 496 | // implemented with a libcall, etc. |
| 497 | TargetLowering &TLI; |
| 498 | SelectionDAG &DAG; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 499 | const TargetData *TD; |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 500 | AliasAnalysis &AA; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 501 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 502 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 503 | /// SwitchInst code generation information. |
| 504 | std::vector<SelectionDAGISel::CaseBlock> SwitchCases; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 505 | /// JTCases - Vector of JumpTable structures used to communicate |
| 506 | /// SwitchInst code generation information. |
| 507 | std::vector<SelectionDAGISel::JumpTableBlock> JTCases; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 508 | std::vector<SelectionDAGISel::BitTestBlock> BitTestCases; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 510 | /// FuncInfo - Information about the function as a whole. |
| 511 | /// |
| 512 | FunctionLoweringInfo &FuncInfo; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 513 | |
| 514 | /// GCI - Garbage collection metadata for the function. |
| 515 | CollectorMetadata *GCI; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 516 | |
| 517 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 518 | AliasAnalysis &aa, |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 519 | FunctionLoweringInfo &funcinfo, |
| 520 | CollectorMetadata *gci) |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 521 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), AA(aa), |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 522 | FuncInfo(funcinfo), GCI(gci) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 525 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 526 | /// flushing any PendingLoad items. This must be done before emitting |
| 527 | /// a store or any other node that may need to be ordered after any |
| 528 | /// prior load instructions. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 529 | /// |
| 530 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 531 | if (PendingLoads.empty()) |
| 532 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 533 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 534 | if (PendingLoads.size() == 1) { |
| 535 | SDOperand Root = PendingLoads[0]; |
| 536 | DAG.setRoot(Root); |
| 537 | PendingLoads.clear(); |
| 538 | return Root; |
| 539 | } |
| 540 | |
| 541 | // Otherwise, we have to make a token factor node. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 542 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 543 | &PendingLoads[0], PendingLoads.size()); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 544 | PendingLoads.clear(); |
| 545 | DAG.setRoot(Root); |
| 546 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 549 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 550 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 551 | /// to do this before emitting a terminator instruction. |
| 552 | /// |
| 553 | SDOperand getControlRoot() { |
| 554 | SDOperand Root = DAG.getRoot(); |
| 555 | |
| 556 | if (PendingExports.empty()) |
| 557 | return Root; |
| 558 | |
| 559 | // Turn all of the CopyToReg chains into one factored node. |
| 560 | if (Root.getOpcode() != ISD::EntryToken) { |
| 561 | unsigned i = 0, e = PendingExports.size(); |
| 562 | for (; i != e; ++i) { |
| 563 | assert(PendingExports[i].Val->getNumOperands() > 1); |
| 564 | if (PendingExports[i].Val->getOperand(0) == Root) |
| 565 | break; // Don't add the root if we already indirectly depend on it. |
| 566 | } |
| 567 | |
| 568 | if (i == e) |
| 569 | PendingExports.push_back(Root); |
| 570 | } |
| 571 | |
| 572 | Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 573 | &PendingExports[0], |
| 574 | PendingExports.size()); |
| 575 | PendingExports.clear(); |
| 576 | DAG.setRoot(Root); |
| 577 | return Root; |
| 578 | } |
| 579 | |
| 580 | void CopyValueToVirtualRegister(Value *V, unsigned Reg); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 582 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 583 | |
| 584 | void visit(unsigned Opcode, User &I) { |
Chris Lattner | 1e7aa5c | 2006-11-10 04:41:34 +0000 | [diff] [blame] | 585 | // Note: this doesn't use InstVisitor, because it has to work with |
| 586 | // ConstantExpr's in addition to instructions. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 587 | switch (Opcode) { |
| 588 | default: assert(0 && "Unknown instruction type encountered!"); |
| 589 | abort(); |
| 590 | // Build the switch statement using the Instruction.def file. |
| 591 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 592 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 593 | #include "llvm/Instruction.def" |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 598 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 599 | SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 600 | const Value *SV, SDOperand Root, |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 601 | bool isVolatile, unsigned Alignment); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 603 | SDOperand getValue(const Value *V); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 605 | void setValue(const Value *V, SDOperand NewN) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 606 | SDOperand &N = NodeMap[V]; |
| 607 | assert(N.Val == 0 && "Already set a value for this node!"); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 608 | N = NewN; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 610 | |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 611 | void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, bool HasEarlyClobber, |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 612 | std::set<unsigned> &OutputRegs, |
| 613 | std::set<unsigned> &InputRegs); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 615 | void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB, |
| 616 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
| 617 | unsigned Opc); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 618 | bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 619 | void ExportFromCurrentBlock(Value *V); |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 620 | void LowerCallTo(CallSite CS, SDOperand Callee, bool IsTailCall, |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 621 | MachineBasicBlock *LandingPad = NULL); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 623 | // Terminator instructions. |
| 624 | void visitRet(ReturnInst &I); |
| 625 | void visitBr(BranchInst &I); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 626 | void visitSwitch(SwitchInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 627 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 628 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 629 | // Helpers for visitSwitch |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 630 | bool handleSmallSwitchRange(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 631 | CaseRecVector& WorkList, |
| 632 | Value* SV, |
| 633 | MachineBasicBlock* Default); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 634 | bool handleJTSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 635 | CaseRecVector& WorkList, |
| 636 | Value* SV, |
| 637 | MachineBasicBlock* Default); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 638 | bool handleBTSplitSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 639 | CaseRecVector& WorkList, |
| 640 | Value* SV, |
| 641 | MachineBasicBlock* Default); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 642 | bool handleBitTestsSwitchCase(CaseRec& CR, |
| 643 | CaseRecVector& WorkList, |
| 644 | Value* SV, |
| 645 | MachineBasicBlock* Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 646 | void visitSwitchCase(SelectionDAGISel::CaseBlock &CB); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 647 | void visitBitTestHeader(SelectionDAGISel::BitTestBlock &B); |
| 648 | void visitBitTestCase(MachineBasicBlock* NextMBB, |
| 649 | unsigned Reg, |
| 650 | SelectionDAGISel::BitTestCase &B); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 651 | void visitJumpTable(SelectionDAGISel::JumpTable &JT); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 652 | void visitJumpTableHeader(SelectionDAGISel::JumpTable &JT, |
| 653 | SelectionDAGISel::JumpTableHeader &JTH); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 655 | // These all get lowered before this pass. |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 656 | void visitInvoke(InvokeInst &I); |
| 657 | void visitUnwind(UnwindInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 658 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 659 | void visitBinary(User &I, unsigned OpCode); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 660 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 661 | void visitAdd(User &I) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 662 | if (I.getType()->isFPOrFPVector()) |
| 663 | visitBinary(I, ISD::FADD); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 664 | else |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 665 | visitBinary(I, ISD::ADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 666 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 667 | void visitSub(User &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 668 | void visitMul(User &I) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 669 | if (I.getType()->isFPOrFPVector()) |
| 670 | visitBinary(I, ISD::FMUL); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 671 | else |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 672 | visitBinary(I, ISD::MUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 673 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 674 | void visitURem(User &I) { visitBinary(I, ISD::UREM); } |
| 675 | void visitSRem(User &I) { visitBinary(I, ISD::SREM); } |
| 676 | void visitFRem(User &I) { visitBinary(I, ISD::FREM); } |
| 677 | void visitUDiv(User &I) { visitBinary(I, ISD::UDIV); } |
| 678 | void visitSDiv(User &I) { visitBinary(I, ISD::SDIV); } |
| 679 | void visitFDiv(User &I) { visitBinary(I, ISD::FDIV); } |
| 680 | void visitAnd (User &I) { visitBinary(I, ISD::AND); } |
| 681 | void visitOr (User &I) { visitBinary(I, ISD::OR); } |
| 682 | void visitXor (User &I) { visitBinary(I, ISD::XOR); } |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 683 | void visitShl (User &I) { visitShift(I, ISD::SHL); } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 684 | void visitLShr(User &I) { visitShift(I, ISD::SRL); } |
| 685 | void visitAShr(User &I) { visitShift(I, ISD::SRA); } |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 686 | void visitICmp(User &I); |
| 687 | void visitFCmp(User &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 688 | // Visit the conversion instructions |
| 689 | void visitTrunc(User &I); |
| 690 | void visitZExt(User &I); |
| 691 | void visitSExt(User &I); |
| 692 | void visitFPTrunc(User &I); |
| 693 | void visitFPExt(User &I); |
| 694 | void visitFPToUI(User &I); |
| 695 | void visitFPToSI(User &I); |
| 696 | void visitUIToFP(User &I); |
| 697 | void visitSIToFP(User &I); |
| 698 | void visitPtrToInt(User &I); |
| 699 | void visitIntToPtr(User &I); |
| 700 | void visitBitCast(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 702 | void visitExtractElement(User &I); |
| 703 | void visitInsertElement(User &I); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 704 | void visitShuffleVector(User &I); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 706 | void visitGetElementPtr(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 707 | void visitSelect(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 708 | |
| 709 | void visitMalloc(MallocInst &I); |
| 710 | void visitFree(FreeInst &I); |
| 711 | void visitAlloca(AllocaInst &I); |
| 712 | void visitLoad(LoadInst &I); |
| 713 | void visitStore(StoreInst &I); |
| 714 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 715 | void visitCall(CallInst &I); |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 716 | void visitInlineAsm(CallSite CS); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 717 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 718 | void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 720 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 721 | void visitVAArg(VAArgInst &I); |
| 722 | void visitVAEnd(CallInst &I); |
| 723 | void visitVACopy(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 724 | |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 725 | void visitGetResult(GetResultInst &I); |
Devang Patel | 40a0421 | 2008-02-19 22:15:16 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 727 | void visitUserOp1(Instruction &I) { |
| 728 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 729 | abort(); |
| 730 | } |
| 731 | void visitUserOp2(Instruction &I) { |
| 732 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 733 | abort(); |
| 734 | } |
| 735 | }; |
| 736 | } // end namespace llvm |
| 737 | |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 738 | |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 739 | /// getCopyFromParts - Create a value that contains the specified legal parts |
| 740 | /// combined into the value they represent. If the parts combine to a type |
| 741 | /// larger then ValueVT then AssertOp can be used to specify whether the extra |
| 742 | /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT |
Chris Lattner | 4468c1f | 2008-03-09 09:38:46 +0000 | [diff] [blame] | 743 | /// (ISD::AssertSext). |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 744 | static SDOperand getCopyFromParts(SelectionDAG &DAG, |
| 745 | const SDOperand *Parts, |
| 746 | unsigned NumParts, |
| 747 | MVT::ValueType PartVT, |
| 748 | MVT::ValueType ValueVT, |
Chris Lattner | 4468c1f | 2008-03-09 09:38:46 +0000 | [diff] [blame] | 749 | ISD::NodeType AssertOp = ISD::DELETED_NODE) { |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 750 | assert(NumParts > 0 && "No parts to assemble!"); |
| 751 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 752 | SDOperand Val = Parts[0]; |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 753 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 754 | if (NumParts > 1) { |
| 755 | // Assemble the value from multiple parts. |
| 756 | if (!MVT::isVector(ValueVT)) { |
| 757 | unsigned PartBits = MVT::getSizeInBits(PartVT); |
| 758 | unsigned ValueBits = MVT::getSizeInBits(ValueVT); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 759 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 760 | // Assemble the power of 2 part. |
| 761 | unsigned RoundParts = NumParts & (NumParts - 1) ? |
| 762 | 1 << Log2_32(NumParts) : NumParts; |
| 763 | unsigned RoundBits = PartBits * RoundParts; |
| 764 | MVT::ValueType RoundVT = RoundBits == ValueBits ? |
| 765 | ValueVT : MVT::getIntegerType(RoundBits); |
| 766 | SDOperand Lo, Hi; |
| 767 | |
| 768 | if (RoundParts > 2) { |
| 769 | MVT::ValueType HalfVT = MVT::getIntegerType(RoundBits/2); |
| 770 | Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT); |
| 771 | Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2, |
| 772 | PartVT, HalfVT); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 773 | } else { |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 774 | Lo = Parts[0]; |
| 775 | Hi = Parts[1]; |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 776 | } |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 777 | if (TLI.isBigEndian()) |
| 778 | std::swap(Lo, Hi); |
| 779 | Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi); |
| 780 | |
| 781 | if (RoundParts < NumParts) { |
| 782 | // Assemble the trailing non-power-of-2 part. |
| 783 | unsigned OddParts = NumParts - RoundParts; |
| 784 | MVT::ValueType OddVT = MVT::getIntegerType(OddParts * PartBits); |
| 785 | Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT); |
| 786 | |
| 787 | // Combine the round and odd parts. |
| 788 | Lo = Val; |
| 789 | if (TLI.isBigEndian()) |
| 790 | std::swap(Lo, Hi); |
| 791 | MVT::ValueType TotalVT = MVT::getIntegerType(NumParts * PartBits); |
| 792 | Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi); |
| 793 | Hi = DAG.getNode(ISD::SHL, TotalVT, Hi, |
| 794 | DAG.getConstant(MVT::getSizeInBits(Lo.getValueType()), |
| 795 | TLI.getShiftAmountTy())); |
| 796 | Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo); |
| 797 | Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi); |
| 798 | } |
| 799 | } else { |
| 800 | // Handle a multi-element vector. |
| 801 | MVT::ValueType IntermediateVT, RegisterVT; |
| 802 | unsigned NumIntermediates; |
| 803 | unsigned NumRegs = |
| 804 | TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 805 | RegisterVT); |
| 806 | |
| 807 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 808 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 809 | assert(RegisterVT == Parts[0].getValueType() && |
| 810 | "Part type doesn't match part!"); |
| 811 | |
| 812 | // Assemble the parts into intermediate operands. |
| 813 | SmallVector<SDOperand, 8> Ops(NumIntermediates); |
| 814 | if (NumIntermediates == NumParts) { |
| 815 | // If the register was not expanded, truncate or copy the value, |
| 816 | // as appropriate. |
| 817 | for (unsigned i = 0; i != NumParts; ++i) |
| 818 | Ops[i] = getCopyFromParts(DAG, &Parts[i], 1, |
| 819 | PartVT, IntermediateVT); |
| 820 | } else if (NumParts > 0) { |
| 821 | // If the intermediate type was expanded, build the intermediate operands |
| 822 | // from the parts. |
| 823 | assert(NumParts % NumIntermediates == 0 && |
| 824 | "Must expand into a divisible number of parts!"); |
| 825 | unsigned Factor = NumParts / NumIntermediates; |
| 826 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 827 | Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor, |
| 828 | PartVT, IntermediateVT); |
| 829 | } |
| 830 | |
| 831 | // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate |
| 832 | // operands. |
| 833 | Val = DAG.getNode(MVT::isVector(IntermediateVT) ? |
| 834 | ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, |
| 835 | ValueVT, &Ops[0], NumIntermediates); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 836 | } |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 839 | // There is now one part, held in Val. Correct it to match ValueVT. |
| 840 | PartVT = Val.getValueType(); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 841 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 842 | if (PartVT == ValueVT) |
| 843 | return Val; |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 844 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 845 | if (MVT::isVector(PartVT)) { |
| 846 | assert(MVT::isVector(ValueVT) && "Unknown vector conversion!"); |
| 847 | return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 848 | } |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 849 | |
| 850 | if (MVT::isVector(ValueVT)) { |
| 851 | assert(MVT::getVectorElementType(ValueVT) == PartVT && |
| 852 | MVT::getVectorNumElements(ValueVT) == 1 && |
| 853 | "Only trivial scalar-to-vector conversions should get here!"); |
| 854 | return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val); |
| 855 | } |
| 856 | |
| 857 | if (MVT::isInteger(PartVT) && |
| 858 | MVT::isInteger(ValueVT)) { |
| 859 | if (MVT::getSizeInBits(ValueVT) < MVT::getSizeInBits(PartVT)) { |
| 860 | // For a truncate, see if we have any information to |
| 861 | // indicate whether the truncated bits will always be |
| 862 | // zero or sign-extension. |
| 863 | if (AssertOp != ISD::DELETED_NODE) |
| 864 | Val = DAG.getNode(AssertOp, PartVT, Val, |
| 865 | DAG.getValueType(ValueVT)); |
| 866 | return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 867 | } else { |
| 868 | return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val); |
| 869 | } |
| 870 | } |
| 871 | |
Chris Lattner | d43d85c | 2008-03-09 07:47:22 +0000 | [diff] [blame] | 872 | if (MVT::isFloatingPoint(PartVT) && MVT::isFloatingPoint(ValueVT)) { |
| 873 | if (ValueVT < Val.getValueType()) |
Chris Lattner | 4468c1f | 2008-03-09 09:38:46 +0000 | [diff] [blame] | 874 | // FP_ROUND's are always exact here. |
Chris Lattner | d43d85c | 2008-03-09 07:47:22 +0000 | [diff] [blame] | 875 | return DAG.getNode(ISD::FP_ROUND, ValueVT, Val, |
Chris Lattner | 4468c1f | 2008-03-09 09:38:46 +0000 | [diff] [blame] | 876 | DAG.getIntPtrConstant(1)); |
Chris Lattner | d43d85c | 2008-03-09 07:47:22 +0000 | [diff] [blame] | 877 | return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val); |
| 878 | } |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 879 | |
| 880 | if (MVT::getSizeInBits(PartVT) == MVT::getSizeInBits(ValueVT)) |
| 881 | return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val); |
| 882 | |
| 883 | assert(0 && "Unknown mismatch!"); |
Chris Lattner | d27c991 | 2008-03-30 18:22:13 +0000 | [diff] [blame] | 884 | return SDOperand(); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 887 | /// getCopyToParts - Create a series of nodes that contain the specified value |
| 888 | /// split into legal parts. If the parts contain more bits than Val, then, for |
| 889 | /// integers, ExtendKind can be used to specify how to generate the extra bits. |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 890 | static void getCopyToParts(SelectionDAG &DAG, |
| 891 | SDOperand Val, |
| 892 | SDOperand *Parts, |
| 893 | unsigned NumParts, |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 894 | MVT::ValueType PartVT, |
| 895 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { |
Dan Gohman | 25ac7e8 | 2007-08-10 14:59:38 +0000 | [diff] [blame] | 896 | TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 897 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 898 | MVT::ValueType ValueVT = Val.getValueType(); |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 899 | unsigned PartBits = MVT::getSizeInBits(PartVT); |
| 900 | assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!"); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 901 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 902 | if (!NumParts) |
| 903 | return; |
| 904 | |
| 905 | if (!MVT::isVector(ValueVT)) { |
| 906 | if (PartVT == ValueVT) { |
| 907 | assert(NumParts == 1 && "No-op copy with multiple parts!"); |
| 908 | Parts[0] = Val; |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 909 | return; |
| 910 | } |
| 911 | |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 912 | if (NumParts * PartBits > MVT::getSizeInBits(ValueVT)) { |
| 913 | // If the parts cover more bits than the value has, promote the value. |
| 914 | if (MVT::isFloatingPoint(PartVT) && MVT::isFloatingPoint(ValueVT)) { |
| 915 | assert(NumParts == 1 && "Do not know what to promote to!"); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 916 | Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val); |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 917 | } else if (MVT::isInteger(PartVT) && MVT::isInteger(ValueVT)) { |
| 918 | ValueVT = MVT::getIntegerType(NumParts * PartBits); |
| 919 | Val = DAG.getNode(ExtendKind, ValueVT, Val); |
| 920 | } else { |
| 921 | assert(0 && "Unknown mismatch!"); |
| 922 | } |
| 923 | } else if (PartBits == MVT::getSizeInBits(ValueVT)) { |
| 924 | // Different types of the same size. |
| 925 | assert(NumParts == 1 && PartVT != ValueVT); |
| 926 | Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); |
| 927 | } else if (NumParts * PartBits < MVT::getSizeInBits(ValueVT)) { |
| 928 | // If the parts cover less bits than value has, truncate the value. |
| 929 | if (MVT::isInteger(PartVT) && MVT::isInteger(ValueVT)) { |
| 930 | ValueVT = MVT::getIntegerType(NumParts * PartBits); |
| 931 | Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 932 | } else { |
| 933 | assert(0 && "Unknown mismatch!"); |
| 934 | } |
| 935 | } |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 936 | |
| 937 | // The value may have changed - recompute ValueVT. |
| 938 | ValueVT = Val.getValueType(); |
| 939 | assert(NumParts * PartBits == MVT::getSizeInBits(ValueVT) && |
| 940 | "Failed to tile the value with PartVT!"); |
| 941 | |
| 942 | if (NumParts == 1) { |
| 943 | assert(PartVT == ValueVT && "Type conversion failed!"); |
| 944 | Parts[0] = Val; |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | // Expand the value into multiple parts. |
| 949 | if (NumParts & (NumParts - 1)) { |
| 950 | // The number of parts is not a power of 2. Split off and copy the tail. |
| 951 | assert(MVT::isInteger(PartVT) && MVT::isInteger(ValueVT) && |
| 952 | "Do not know what to expand to!"); |
| 953 | unsigned RoundParts = 1 << Log2_32(NumParts); |
| 954 | unsigned RoundBits = RoundParts * PartBits; |
| 955 | unsigned OddParts = NumParts - RoundParts; |
| 956 | SDOperand OddVal = DAG.getNode(ISD::SRL, ValueVT, Val, |
| 957 | DAG.getConstant(RoundBits, |
| 958 | TLI.getShiftAmountTy())); |
| 959 | getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT); |
| 960 | if (TLI.isBigEndian()) |
| 961 | // The odd parts were reversed by getCopyToParts - unreverse them. |
| 962 | std::reverse(Parts + RoundParts, Parts + NumParts); |
| 963 | NumParts = RoundParts; |
| 964 | ValueVT = MVT::getIntegerType(NumParts * PartBits); |
| 965 | Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 966 | } |
| 967 | |
| 968 | // The number of parts is a power of 2. Repeatedly bisect the value using |
| 969 | // EXTRACT_ELEMENT. |
Duncan Sands | 25eb043 | 2008-03-12 20:30:08 +0000 | [diff] [blame] | 970 | Parts[0] = DAG.getNode(ISD::BIT_CONVERT, |
| 971 | MVT::getIntegerType(MVT::getSizeInBits(ValueVT)), |
| 972 | Val); |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 973 | for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { |
| 974 | for (unsigned i = 0; i < NumParts; i += StepSize) { |
| 975 | unsigned ThisBits = StepSize * PartBits / 2; |
Duncan Sands | 25eb043 | 2008-03-12 20:30:08 +0000 | [diff] [blame] | 976 | MVT::ValueType ThisVT = MVT::getIntegerType (ThisBits); |
| 977 | SDOperand &Part0 = Parts[i]; |
| 978 | SDOperand &Part1 = Parts[i+StepSize/2]; |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 979 | |
Duncan Sands | 25eb043 | 2008-03-12 20:30:08 +0000 | [diff] [blame] | 980 | Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, |
| 981 | DAG.getConstant(1, PtrVT)); |
| 982 | Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0, |
| 983 | DAG.getConstant(0, PtrVT)); |
| 984 | |
| 985 | if (ThisBits == PartBits && ThisVT != PartVT) { |
| 986 | Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0); |
| 987 | Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1); |
| 988 | } |
Duncan Sands | 014e04a | 2008-02-12 20:46:31 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | if (TLI.isBigEndian()) |
| 993 | std::reverse(Parts, Parts + NumParts); |
| 994 | |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | // Vector ValueVT. |
| 999 | if (NumParts == 1) { |
| 1000 | if (PartVT != ValueVT) { |
| 1001 | if (MVT::isVector(PartVT)) { |
| 1002 | Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val); |
| 1003 | } else { |
| 1004 | assert(MVT::getVectorElementType(ValueVT) == PartVT && |
| 1005 | MVT::getVectorNumElements(ValueVT) == 1 && |
| 1006 | "Only trivial vector-to-scalar conversions should get here!"); |
| 1007 | Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val, |
| 1008 | DAG.getConstant(0, PtrVT)); |
| 1009 | } |
| 1010 | } |
| 1011 | |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 1012 | Parts[0] = Val; |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | // Handle a multi-element vector. |
| 1017 | MVT::ValueType IntermediateVT, RegisterVT; |
| 1018 | unsigned NumIntermediates; |
| 1019 | unsigned NumRegs = |
| 1020 | DAG.getTargetLoweringInfo() |
| 1021 | .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates, |
| 1022 | RegisterVT); |
| 1023 | unsigned NumElements = MVT::getVectorNumElements(ValueVT); |
| 1024 | |
| 1025 | assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); |
| 1026 | assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); |
| 1027 | |
| 1028 | // Split the vector into intermediate operands. |
| 1029 | SmallVector<SDOperand, 8> Ops(NumIntermediates); |
| 1030 | for (unsigned i = 0; i != NumIntermediates; ++i) |
| 1031 | if (MVT::isVector(IntermediateVT)) |
| 1032 | Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, |
| 1033 | IntermediateVT, Val, |
| 1034 | DAG.getConstant(i * (NumElements / NumIntermediates), |
Dan Gohman | 25ac7e8 | 2007-08-10 14:59:38 +0000 | [diff] [blame] | 1035 | PtrVT)); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 1036 | else |
| 1037 | Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 1038 | IntermediateVT, Val, |
Dan Gohman | 25ac7e8 | 2007-08-10 14:59:38 +0000 | [diff] [blame] | 1039 | DAG.getConstant(i, PtrVT)); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 1040 | |
| 1041 | // Split the intermediate operands into legal parts. |
| 1042 | if (NumParts == NumIntermediates) { |
| 1043 | // If the register was not expanded, promote or copy the value, |
| 1044 | // as appropriate. |
| 1045 | for (unsigned i = 0; i != NumParts; ++i) |
Dan Gohman | 532dc2e | 2007-07-09 20:59:04 +0000 | [diff] [blame] | 1046 | getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 1047 | } else if (NumParts > 0) { |
| 1048 | // If the intermediate type was expanded, split each the value into |
| 1049 | // legal parts. |
| 1050 | assert(NumParts % NumIntermediates == 0 && |
| 1051 | "Must expand into a divisible number of parts!"); |
| 1052 | unsigned Factor = NumParts / NumIntermediates; |
| 1053 | for (unsigned i = 0; i != NumIntermediates; ++i) |
Dan Gohman | 532dc2e | 2007-07-09 20:59:04 +0000 | [diff] [blame] | 1054 | getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT); |
Dan Gohman | 6183f78 | 2007-07-05 20:12:34 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1059 | SDOperand SelectionDAGLowering::getValue(const Value *V) { |
| 1060 | SDOperand &N = NodeMap[V]; |
| 1061 | if (N.Val) return N; |
| 1062 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1063 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1064 | MVT::ValueType VT = TLI.getValueType(V->getType(), true); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 1067 | return N = DAG.getConstant(CI->getValue(), VT); |
| 1068 | |
| 1069 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1070 | return N = DAG.getGlobalAddress(GV, VT); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (isa<ConstantPointerNull>(C)) |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1073 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 1076 | return N = DAG.getConstantFP(CFP->getValueAPF(), VT); |
| 1077 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1078 | if (isa<UndefValue>(C) && !isa<VectorType>(V->getType())) |
| 1079 | return N = DAG.getNode(ISD::UNDEF, VT); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 1082 | visit(CE->getOpcode(), *CE); |
| 1083 | SDOperand N1 = NodeMap[V]; |
| 1084 | assert(N1.Val && "visit didn't populate the ValueMap!"); |
| 1085 | return N1; |
| 1086 | } |
| 1087 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1088 | const VectorType *VecTy = cast<VectorType>(V->getType()); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1089 | unsigned NumElements = VecTy->getNumElements(); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1091 | // Now that we know the number and type of the elements, get that number of |
| 1092 | // elements into the Ops array based on what kind of constant it is. |
| 1093 | SmallVector<SDOperand, 16> Ops; |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1094 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 1095 | for (unsigned i = 0; i != NumElements; ++i) |
| 1096 | Ops.push_back(getValue(CP->getOperand(i))); |
| 1097 | } else { |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1098 | assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && |
| 1099 | "Unknown vector constant!"); |
| 1100 | MVT::ValueType EltVT = TLI.getValueType(VecTy->getElementType()); |
| 1101 | |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1102 | SDOperand Op; |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1103 | if (isa<UndefValue>(C)) |
| 1104 | Op = DAG.getNode(ISD::UNDEF, EltVT); |
| 1105 | else if (MVT::isFloatingPoint(EltVT)) |
| 1106 | Op = DAG.getConstantFP(0, EltVT); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1107 | else |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1108 | Op = DAG.getConstant(0, EltVT); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1109 | Ops.assign(NumElements, Op); |
| 1110 | } |
| 1111 | |
| 1112 | // Create a BUILD_VECTOR node. |
| 1113 | return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1116 | // If this is a static alloca, generate it as the frameindex instead of |
| 1117 | // computation. |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1118 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 1119 | std::map<const AllocaInst*, int>::iterator SI = |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1120 | FuncInfo.StaticAllocaMap.find(AI); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1121 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 1122 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 1123 | } |
| 1124 | |
Chris Lattner | 251db18 | 2007-02-25 18:40:32 +0000 | [diff] [blame] | 1125 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 1126 | assert(InReg && "Value not in map!"); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 1128 | RegsForValue RFV(TLI, InReg, V->getType()); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 1129 | SDOperand Chain = DAG.getEntryNode(); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 1130 | return RFV.getCopyFromRegs(DAG, Chain, NULL); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1134 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 1135 | if (I.getNumOperands() == 0) { |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1136 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getControlRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1137 | return; |
| 1138 | } |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1140 | SmallVector<SDOperand, 8> NewValues; |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1141 | NewValues.push_back(getControlRoot()); |
| 1142 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 1143 | SDOperand RetOp = getValue(I.getOperand(i)); |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 1144 | MVT::ValueType VT = RetOp.getValueType(); |
| 1145 | |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1146 | // FIXME: C calling convention requires the return type to be promoted to |
| 1147 | // at least 32-bit. But this is not necessary for non-C calling conventions. |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 1148 | if (MVT::isInteger(VT)) { |
| 1149 | MVT::ValueType MinVT = TLI.getRegisterType(MVT::i32); |
| 1150 | if (MVT::getSizeInBits(VT) < MVT::getSizeInBits(MinVT)) |
| 1151 | VT = MinVT; |
| 1152 | } |
| 1153 | |
| 1154 | unsigned NumParts = TLI.getNumRegisters(VT); |
| 1155 | MVT::ValueType PartVT = TLI.getRegisterType(VT); |
| 1156 | SmallVector<SDOperand, 4> Parts(NumParts); |
| 1157 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 1158 | |
| 1159 | const Function *F = I.getParent()->getParent(); |
| 1160 | if (F->paramHasAttr(0, ParamAttr::SExt)) |
| 1161 | ExtendKind = ISD::SIGN_EXTEND; |
| 1162 | else if (F->paramHasAttr(0, ParamAttr::ZExt)) |
| 1163 | ExtendKind = ISD::ZERO_EXTEND; |
| 1164 | |
| 1165 | getCopyToParts(DAG, RetOp, &Parts[0], NumParts, PartVT, ExtendKind); |
| 1166 | |
| 1167 | for (unsigned i = 0; i < NumParts; ++i) { |
| 1168 | NewValues.push_back(Parts[i]); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1169 | NewValues.push_back(DAG.getArgFlags(ISD::ArgFlagsTy())); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 1170 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1171 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1172 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, |
| 1173 | &NewValues[0], NewValues.size())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1176 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 1177 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 1178 | /// CopyTo/FromReg. |
| 1179 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 1180 | // No need to export constants. |
| 1181 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
| 1182 | |
| 1183 | // Already exported? |
| 1184 | if (FuncInfo.isExportedInst(V)) return; |
| 1185 | |
| 1186 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1187 | CopyValueToVirtualRegister(V, Reg); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 1190 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 1191 | const BasicBlock *FromBB) { |
| 1192 | // The operands of the setcc have to be in this block. We don't know |
| 1193 | // how to export them from some other block. |
| 1194 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 1195 | // Can export from current BB. |
| 1196 | if (VI->getParent() == FromBB) |
| 1197 | return true; |
| 1198 | |
| 1199 | // Is already exported, noop. |
| 1200 | return FuncInfo.isExportedInst(V); |
| 1201 | } |
| 1202 | |
| 1203 | // If this is an argument, we can export it if the BB is the entry block or |
| 1204 | // if it is already exported. |
| 1205 | if (isa<Argument>(V)) { |
| 1206 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 1207 | return true; |
| 1208 | |
| 1209 | // Otherwise, can only export this if it is already exported. |
| 1210 | return FuncInfo.isExportedInst(V); |
| 1211 | } |
| 1212 | |
| 1213 | // Otherwise, constants can always be exported. |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 1217 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 1218 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 1219 | return I->getParent() == BB; |
| 1220 | return true; |
| 1221 | } |
| 1222 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1223 | /// FindMergedConditions - If Cond is an expression like |
| 1224 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 1225 | MachineBasicBlock *TBB, |
| 1226 | MachineBasicBlock *FBB, |
| 1227 | MachineBasicBlock *CurBB, |
| 1228 | unsigned Opc) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1229 | // If this node is not part of the or/and tree, emit it as a branch. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1230 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1231 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1232 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
| 1233 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 1234 | BOp->getParent() != CurBB->getBasicBlock() || |
| 1235 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 1236 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1237 | const BasicBlock *BB = CurBB->getBasicBlock(); |
| 1238 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1239 | // If the leaf of the tree is a comparison, merge the condition into |
| 1240 | // the caseblock. |
| 1241 | if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) && |
| 1242 | // The operands of the cmp have to be in this block. We don't know |
Chris Lattner | 5a145f0 | 2006-10-29 18:23:37 +0000 | [diff] [blame] | 1243 | // how to export them from some other block. If this is the first block |
| 1244 | // of the sequence, no exporting is needed. |
| 1245 | (CurBB == CurMBB || |
| 1246 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 1247 | isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1248 | BOp = cast<Instruction>(Cond); |
| 1249 | ISD::CondCode Condition; |
| 1250 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
| 1251 | switch (IC->getPredicate()) { |
| 1252 | default: assert(0 && "Unknown icmp predicate opcode!"); |
| 1253 | case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break; |
| 1254 | case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break; |
| 1255 | case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break; |
| 1256 | case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break; |
| 1257 | case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break; |
| 1258 | case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break; |
| 1259 | case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break; |
| 1260 | case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break; |
| 1261 | case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break; |
| 1262 | case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break; |
| 1263 | } |
| 1264 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
| 1265 | ISD::CondCode FPC, FOC; |
| 1266 | switch (FC->getPredicate()) { |
| 1267 | default: assert(0 && "Unknown fcmp predicate opcode!"); |
| 1268 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1269 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1270 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1271 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1272 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1273 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1274 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
Chris Lattner | 6bf30ab | 2008-05-01 07:26:11 +0000 | [diff] [blame] | 1275 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 1276 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1277 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1278 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1279 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1280 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1281 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1282 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1283 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1284 | } |
| 1285 | if (FiniteOnlyFPMath()) |
| 1286 | Condition = FOC; |
| 1287 | else |
| 1288 | Condition = FPC; |
| 1289 | } else { |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 1290 | Condition = ISD::SETEQ; // silence warning. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1291 | assert(0 && "Unknown compare instruction"); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1294 | SelectionDAGISel::CaseBlock CB(Condition, BOp->getOperand(0), |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1295 | BOp->getOperand(1), NULL, TBB, FBB, CurBB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1296 | SwitchCases.push_back(CB); |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | // Create a CaseBlock record representing this branch. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1301 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(), |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1302 | NULL, TBB, FBB, CurBB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1303 | SwitchCases.push_back(CB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1304 | return; |
| 1305 | } |
| 1306 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Create TmpBB after CurBB. |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1309 | MachineFunction::iterator BBI = CurBB; |
| 1310 | MachineBasicBlock *TmpBB = new MachineBasicBlock(CurBB->getBasicBlock()); |
| 1311 | CurBB->getParent()->getBasicBlockList().insert(++BBI, TmpBB); |
| 1312 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1313 | if (Opc == Instruction::Or) { |
| 1314 | // Codegen X | Y as: |
| 1315 | // jmp_if_X TBB |
| 1316 | // jmp TmpBB |
| 1317 | // TmpBB: |
| 1318 | // jmp_if_Y TBB |
| 1319 | // jmp FBB |
| 1320 | // |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1321 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1322 | // Emit the LHS condition. |
| 1323 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
| 1324 | |
| 1325 | // Emit the RHS condition into TmpBB. |
| 1326 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1327 | } else { |
| 1328 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 1329 | // Codegen X & Y as: |
| 1330 | // jmp_if_X TmpBB |
| 1331 | // jmp FBB |
| 1332 | // TmpBB: |
| 1333 | // jmp_if_Y TBB |
| 1334 | // jmp FBB |
| 1335 | // |
| 1336 | // This requires creation of TmpBB after CurBB. |
| 1337 | |
| 1338 | // Emit the LHS condition. |
| 1339 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
| 1340 | |
| 1341 | // Emit the RHS condition into TmpBB. |
| 1342 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 1343 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1346 | /// If the set of cases should be emitted as a series of branches, return true. |
| 1347 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 1348 | /// false. |
| 1349 | static bool |
| 1350 | ShouldEmitAsBranches(const std::vector<SelectionDAGISel::CaseBlock> &Cases) { |
| 1351 | if (Cases.size() != 2) return true; |
| 1352 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1353 | // If this is two comparisons of the same values or'd or and'd together, they |
| 1354 | // will get folded into a single comparison, so don't emit two blocks. |
| 1355 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 1356 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 1357 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 1358 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 1359 | return false; |
| 1360 | } |
| 1361 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1362 | return true; |
| 1363 | } |
| 1364 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1365 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 1366 | // Update machine-CFG edges. |
| 1367 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Figure out which block is immediately after the current one. |
| 1370 | MachineBasicBlock *NextBlock = 0; |
| 1371 | MachineFunction::iterator BBI = CurMBB; |
| 1372 | if (++BBI != CurMBB->getParent()->end()) |
| 1373 | NextBlock = BBI; |
| 1374 | |
| 1375 | if (I.isUnconditional()) { |
| 1376 | // If this is not a fall-through branch, emit the branch. |
| 1377 | if (Succ0MBB != NextBlock) |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1378 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 1379 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1381 | // Update machine-CFG edges. |
| 1382 | CurMBB->addSuccessor(Succ0MBB); |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | // If this condition is one of the special cases we handle, do special stuff |
| 1387 | // now. |
| 1388 | Value *CondVal = I.getCondition(); |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1389 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1390 | |
| 1391 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1392 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1393 | // For example, instead of something like: |
| 1394 | // cmp A, B |
| 1395 | // C = seteq |
| 1396 | // cmp D, E |
| 1397 | // F = setle |
| 1398 | // or C, F |
| 1399 | // jnz foo |
| 1400 | // Emit: |
| 1401 | // cmp A, B |
| 1402 | // je foo |
| 1403 | // cmp D, E |
| 1404 | // jle foo |
| 1405 | // |
| 1406 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
| 1407 | if (BOp->hasOneUse() && |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1408 | (BOp->getOpcode() == Instruction::And || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1409 | BOp->getOpcode() == Instruction::Or)) { |
| 1410 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1411 | // If the compares in later blocks need to use values not currently |
| 1412 | // exported from this block, export them now. This block should always |
| 1413 | // be the first entry. |
| 1414 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
| 1415 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1416 | // Allow some cases to be rejected. |
| 1417 | if (ShouldEmitAsBranches(SwitchCases)) { |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1418 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1419 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1420 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1421 | } |
| 1422 | |
| 1423 | // Emit the branch for this block. |
| 1424 | visitSwitchCase(SwitchCases[0]); |
| 1425 | SwitchCases.erase(SwitchCases.begin()); |
| 1426 | return; |
Chris Lattner | 5a145f0 | 2006-10-29 18:23:37 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1429 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1430 | // SwitchCases. |
| 1431 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1432 | CurMBB->getParent()->getBasicBlockList().erase(SwitchCases[i].ThisBB); |
| 1433 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1434 | SwitchCases.clear(); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1437 | |
| 1438 | // Create a CaseBlock record representing this branch. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1439 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(), |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1440 | NULL, Succ0MBB, Succ1MBB, CurMBB); |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1441 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1442 | // cond branch. |
| 1443 | visitSwitchCase(CB); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1446 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1447 | /// the binary search tree resulting from lowering a switch instruction. |
| 1448 | void SelectionDAGLowering::visitSwitchCase(SelectionDAGISel::CaseBlock &CB) { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1449 | SDOperand Cond; |
| 1450 | SDOperand CondLHS = getValue(CB.CmpLHS); |
| 1451 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1452 | // Build the setcc now. |
| 1453 | if (CB.CmpMHS == NULL) { |
| 1454 | // Fold "(X == true)" to X and "(X == false)" to !X to |
| 1455 | // handle common cases produced by branch lowering. |
| 1456 | if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ) |
| 1457 | Cond = CondLHS; |
| 1458 | else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) { |
| 1459 | SDOperand True = DAG.getConstant(1, CondLHS.getValueType()); |
| 1460 | Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True); |
| 1461 | } else |
| 1462 | Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
| 1463 | } else { |
| 1464 | assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1465 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1466 | uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue(); |
| 1467 | uint64_t High = cast<ConstantInt>(CB.CmpRHS)->getSExtValue(); |
| 1468 | |
| 1469 | SDOperand CmpOp = getValue(CB.CmpMHS); |
| 1470 | MVT::ValueType VT = CmpOp.getValueType(); |
| 1471 | |
| 1472 | if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { |
| 1473 | Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE); |
| 1474 | } else { |
| 1475 | SDOperand SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT)); |
| 1476 | Cond = DAG.getSetCC(MVT::i1, SUB, |
| 1477 | DAG.getConstant(High-Low, VT), ISD::SETULE); |
| 1478 | } |
| 1479 | |
| 1480 | } |
| 1481 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1482 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1483 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1484 | MachineBasicBlock *NextBlock = 0; |
| 1485 | MachineFunction::iterator BBI = CurMBB; |
| 1486 | if (++BBI != CurMBB->getParent()->end()) |
| 1487 | NextBlock = BBI; |
| 1488 | |
| 1489 | // If the lhs block is the next block, invert the condition so that we can |
| 1490 | // fall through to the lhs instead of the rhs block. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1491 | if (CB.TrueBB == NextBlock) { |
| 1492 | std::swap(CB.TrueBB, CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1493 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 1494 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
| 1495 | } |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1496 | SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1497 | DAG.getBasicBlock(CB.TrueBB)); |
| 1498 | if (CB.FalseBB == NextBlock) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1499 | DAG.setRoot(BrCond); |
| 1500 | else |
| 1501 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1502 | DAG.getBasicBlock(CB.FalseBB))); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1503 | // Update successor info |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1504 | CurMBB->addSuccessor(CB.TrueBB); |
| 1505 | CurMBB->addSuccessor(CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1508 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1509 | void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1510 | // Emit the code for the jump table |
Scott Michel | f147a8d | 2007-04-24 01:24:20 +0000 | [diff] [blame] | 1511 | assert(JT.Reg != -1U && "Should lower JT Header first!"); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1512 | MVT::ValueType PTy = TLI.getPointerTy(); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1513 | SDOperand Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1514 | SDOperand Table = DAG.getJumpTable(JT.JTI, PTy); |
| 1515 | DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1), |
| 1516 | Table, Index)); |
| 1517 | return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1520 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1521 | /// in the JumpTable from switch case. |
| 1522 | void SelectionDAGLowering::visitJumpTableHeader(SelectionDAGISel::JumpTable &JT, |
| 1523 | SelectionDAGISel::JumpTableHeader &JTH) { |
| 1524 | // Subtract the lowest switch case value from the value being switched on |
| 1525 | // and conditional branch to default mbb if the result is greater than the |
| 1526 | // difference between smallest and largest cases. |
| 1527 | SDOperand SwitchOp = getValue(JTH.SValue); |
| 1528 | MVT::ValueType VT = SwitchOp.getValueType(); |
| 1529 | SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1530 | DAG.getConstant(JTH.First, VT)); |
| 1531 | |
| 1532 | // The SDNode we just created, which holds the value being switched on |
| 1533 | // minus the the smallest case value, needs to be copied to a virtual |
| 1534 | // register so it can be used as an index into the jump table in a |
| 1535 | // subsequent basic block. This value may be smaller or larger than the |
| 1536 | // target's pointer type, and therefore require extension or truncating. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1537 | if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getPointerTy())) |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1538 | SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB); |
| 1539 | else |
| 1540 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB); |
| 1541 | |
| 1542 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1543 | SDOperand CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1544 | JT.Reg = JumpTableReg; |
| 1545 | |
| 1546 | // Emit the range check for the jump table, and branch to the default |
| 1547 | // block for the switch statement if the value being switched on exceeds |
| 1548 | // the largest case in the switch. |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 1549 | SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB, |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1550 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1551 | ISD::SETUGT); |
| 1552 | |
| 1553 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1554 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1555 | MachineBasicBlock *NextBlock = 0; |
| 1556 | MachineFunction::iterator BBI = CurMBB; |
| 1557 | if (++BBI != CurMBB->getParent()->end()) |
| 1558 | NextBlock = BBI; |
| 1559 | |
| 1560 | SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP, |
| 1561 | DAG.getBasicBlock(JT.Default)); |
| 1562 | |
| 1563 | if (JT.MBB == NextBlock) |
| 1564 | DAG.setRoot(BrCond); |
| 1565 | else |
| 1566 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1567 | DAG.getBasicBlock(JT.MBB))); |
| 1568 | |
| 1569 | return; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1572 | /// visitBitTestHeader - This function emits necessary code to produce value |
| 1573 | /// suitable for "bit tests" |
| 1574 | void SelectionDAGLowering::visitBitTestHeader(SelectionDAGISel::BitTestBlock &B) { |
| 1575 | // Subtract the minimum value |
| 1576 | SDOperand SwitchOp = getValue(B.SValue); |
| 1577 | MVT::ValueType VT = SwitchOp.getValueType(); |
| 1578 | SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1579 | DAG.getConstant(B.First, VT)); |
| 1580 | |
| 1581 | // Check range |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 1582 | SDOperand RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB, |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1583 | DAG.getConstant(B.Range, VT), |
| 1584 | ISD::SETUGT); |
| 1585 | |
| 1586 | SDOperand ShiftOp; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1587 | if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getShiftAmountTy())) |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1588 | ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB); |
| 1589 | else |
| 1590 | ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB); |
| 1591 | |
| 1592 | // Make desired shift |
| 1593 | SDOperand SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(), |
| 1594 | DAG.getConstant(1, TLI.getPointerTy()), |
| 1595 | ShiftOp); |
| 1596 | |
| 1597 | unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1598 | SDOperand CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1599 | B.Reg = SwitchReg; |
| 1600 | |
| 1601 | SDOperand BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp, |
| 1602 | DAG.getBasicBlock(B.Default)); |
| 1603 | |
| 1604 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1605 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1606 | MachineBasicBlock *NextBlock = 0; |
| 1607 | MachineFunction::iterator BBI = CurMBB; |
| 1608 | if (++BBI != CurMBB->getParent()->end()) |
| 1609 | NextBlock = BBI; |
| 1610 | |
| 1611 | MachineBasicBlock* MBB = B.Cases[0].ThisBB; |
| 1612 | if (MBB == NextBlock) |
| 1613 | DAG.setRoot(BrRange); |
| 1614 | else |
| 1615 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo, |
| 1616 | DAG.getBasicBlock(MBB))); |
| 1617 | |
| 1618 | CurMBB->addSuccessor(B.Default); |
| 1619 | CurMBB->addSuccessor(MBB); |
| 1620 | |
| 1621 | return; |
| 1622 | } |
| 1623 | |
| 1624 | /// visitBitTestCase - this function produces one "bit test" |
| 1625 | void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, |
| 1626 | unsigned Reg, |
| 1627 | SelectionDAGISel::BitTestCase &B) { |
| 1628 | // Emit bit tests and jumps |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1629 | SDOperand SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg, TLI.getPointerTy()); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1630 | |
| 1631 | SDOperand AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(), |
| 1632 | SwitchVal, |
| 1633 | DAG.getConstant(B.Mask, |
| 1634 | TLI.getPointerTy())); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 1635 | SDOperand AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp, |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1636 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1637 | ISD::SETNE); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1638 | SDOperand BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1639 | AndCmp, DAG.getBasicBlock(B.TargetBB)); |
| 1640 | |
| 1641 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1642 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1643 | MachineBasicBlock *NextBlock = 0; |
| 1644 | MachineFunction::iterator BBI = CurMBB; |
| 1645 | if (++BBI != CurMBB->getParent()->end()) |
| 1646 | NextBlock = BBI; |
| 1647 | |
| 1648 | if (NextMBB == NextBlock) |
| 1649 | DAG.setRoot(BrAnd); |
| 1650 | else |
| 1651 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd, |
| 1652 | DAG.getBasicBlock(NextMBB))); |
| 1653 | |
| 1654 | CurMBB->addSuccessor(B.TargetBB); |
| 1655 | CurMBB->addSuccessor(NextMBB); |
| 1656 | |
| 1657 | return; |
| 1658 | } |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1659 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1660 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
| 1661 | // Retrieve successors. |
| 1662 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 1663 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 1664 | |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 1665 | if (isa<InlineAsm>(I.getCalledValue())) |
| 1666 | visitInlineAsm(&I); |
| 1667 | else |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 1668 | LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad); |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 1669 | |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 1670 | // If the value of the invoke is used outside of its defining block, make it |
| 1671 | // available as a virtual register. |
| 1672 | if (!I.use_empty()) { |
| 1673 | DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I); |
| 1674 | if (VMI != FuncInfo.ValueMap.end()) |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1675 | CopyValueToVirtualRegister(&I, VMI->second); |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 1676 | } |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Drop into normal successor. |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 1679 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 1680 | DAG.getBasicBlock(Return))); |
| 1681 | |
| 1682 | // Update successor info |
| 1683 | CurMBB->addSuccessor(Return); |
| 1684 | CurMBB->addSuccessor(LandingPad); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1688 | } |
| 1689 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1690 | /// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1691 | /// small case ranges). |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1692 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1693 | CaseRecVector& WorkList, |
| 1694 | Value* SV, |
| 1695 | MachineBasicBlock* Default) { |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1696 | Case& BackCase = *(CR.Range.second-1); |
| 1697 | |
| 1698 | // Size is the number of Cases represented by this range. |
| 1699 | unsigned Size = CR.Range.second - CR.Range.first; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1700 | if (Size > 3) |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1701 | return false; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1702 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1703 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1704 | // inserting any additional MBBs necessary to represent the switch. |
| 1705 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1706 | |
| 1707 | // Figure out which block is immediately after the current one. |
| 1708 | MachineBasicBlock *NextBlock = 0; |
| 1709 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1710 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1711 | if (++BBI != CurMBB->getParent()->end()) |
| 1712 | NextBlock = BBI; |
| 1713 | |
| 1714 | // TODO: If any two of the cases has the same destination, and if one value |
| 1715 | // is the same as the other, but has one bit unset that the other has set, |
| 1716 | // use bit manipulation to do two compares at once. For example: |
| 1717 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
| 1718 | |
| 1719 | // Rearrange the case blocks so that the last one falls through if possible. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1720 | if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) { |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1721 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1722 | // branches in this order. See if rearranging a case value would help. |
| 1723 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1724 | if (I->BB == NextBlock) { |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1725 | std::swap(*I, BackCase); |
| 1726 | break; |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | // Create a CaseBlock record representing a conditional branch to |
| 1732 | // the Case's target mbb if the value being switched on SV is equal |
| 1733 | // to C. |
| 1734 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1735 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1736 | MachineBasicBlock *FallThrough; |
| 1737 | if (I != E-1) { |
| 1738 | FallThrough = new MachineBasicBlock(CurBlock->getBasicBlock()); |
| 1739 | CurMF->getBasicBlockList().insert(BBI, FallThrough); |
| 1740 | } else { |
| 1741 | // If the last case doesn't match, go to the default block. |
| 1742 | FallThrough = Default; |
| 1743 | } |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1744 | |
| 1745 | Value *RHS, *LHS, *MHS; |
| 1746 | ISD::CondCode CC; |
| 1747 | if (I->High == I->Low) { |
| 1748 | // This is just small small case range :) containing exactly 1 case |
| 1749 | CC = ISD::SETEQ; |
| 1750 | LHS = SV; RHS = I->High; MHS = NULL; |
| 1751 | } else { |
| 1752 | CC = ISD::SETLE; |
| 1753 | LHS = I->Low; MHS = SV; RHS = I->High; |
| 1754 | } |
| 1755 | SelectionDAGISel::CaseBlock CB(CC, LHS, RHS, MHS, |
| 1756 | I->BB, FallThrough, CurBlock); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1757 | |
| 1758 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1759 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1760 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1761 | // before the next MBB. |
| 1762 | if (CurBlock == CurMBB) |
| 1763 | visitSwitchCase(CB); |
| 1764 | else |
| 1765 | SwitchCases.push_back(CB); |
| 1766 | |
| 1767 | CurBlock = FallThrough; |
| 1768 | } |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1769 | |
| 1770 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Anton Korobeynikov | 7294b58 | 2007-05-09 20:07:08 +0000 | [diff] [blame] | 1773 | static inline bool areJTsAllowed(const TargetLowering &TLI) { |
| 1774 | return (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) || |
| 1775 | TLI.isOperationLegal(ISD::BRIND, MVT::Other)); |
| 1776 | } |
| 1777 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1778 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1779 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1780 | CaseRecVector& WorkList, |
| 1781 | Value* SV, |
| 1782 | MachineBasicBlock* Default) { |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1783 | Case& FrontCase = *CR.Range.first; |
| 1784 | Case& BackCase = *(CR.Range.second-1); |
| 1785 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1786 | int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue(); |
| 1787 | int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue(); |
| 1788 | |
| 1789 | uint64_t TSize = 0; |
| 1790 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1791 | I!=E; ++I) |
| 1792 | TSize += I->size(); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1793 | |
Anton Korobeynikov | 7294b58 | 2007-05-09 20:07:08 +0000 | [diff] [blame] | 1794 | if (!areJTsAllowed(TLI) || TSize <= 3) |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1795 | return false; |
| 1796 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1797 | double Density = (double)TSize / (double)((Last - First) + 1ULL); |
| 1798 | if (Density < 0.4) |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1799 | return false; |
| 1800 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1801 | DOUT << "Lowering jump table\n" |
| 1802 | << "First entry: " << First << ". Last entry: " << Last << "\n" |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1803 | << "Size: " << TSize << ". Density: " << Density << "\n\n"; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1804 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1805 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1806 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1807 | MachineFunction *CurMF = CurMBB->getParent(); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1808 | |
| 1809 | // Figure out which block is immediately after the current one. |
| 1810 | MachineBasicBlock *NextBlock = 0; |
| 1811 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1812 | |
| 1813 | if (++BBI != CurMBB->getParent()->end()) |
| 1814 | NextBlock = BBI; |
| 1815 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1816 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1817 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1818 | // Create a new basic block to hold the code for loading the address |
| 1819 | // of the jump table, and jumping to it. Update successor information; |
| 1820 | // we will either branch to the default case for the switch, or the jump |
| 1821 | // table. |
| 1822 | MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB); |
| 1823 | CurMF->getBasicBlockList().insert(BBI, JumpTableBB); |
| 1824 | CR.CaseBB->addSuccessor(Default); |
| 1825 | CR.CaseBB->addSuccessor(JumpTableBB); |
| 1826 | |
| 1827 | // Build a vector of destination BBs, corresponding to each target |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1828 | // of the jump table. If the value of the jump table slot corresponds to |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1829 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1830 | // the default BB. |
| 1831 | std::vector<MachineBasicBlock*> DestBBs; |
| 1832 | int64_t TEI = First; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1833 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) { |
| 1834 | int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue(); |
| 1835 | int64_t High = cast<ConstantInt>(I->High)->getSExtValue(); |
| 1836 | |
| 1837 | if ((Low <= TEI) && (TEI <= High)) { |
| 1838 | DestBBs.push_back(I->BB); |
| 1839 | if (TEI==High) |
| 1840 | ++I; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1841 | } else { |
| 1842 | DestBBs.push_back(Default); |
| 1843 | } |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1844 | } |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1845 | |
| 1846 | // Update successor info. Add one edge to each unique successor. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1847 | BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs()); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1848 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
| 1849 | E = DestBBs.end(); I != E; ++I) { |
| 1850 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1851 | SuccsHandled[(*I)->getNumber()] = true; |
| 1852 | JumpTableBB->addSuccessor(*I); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | // Create a jump table index for this jump table, or return an existing |
| 1857 | // one. |
| 1858 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
| 1859 | |
| 1860 | // Set the jump table information so that we can codegen it as a second |
| 1861 | // MachineBasicBlock |
Scott Michel | f147a8d | 2007-04-24 01:24:20 +0000 | [diff] [blame] | 1862 | SelectionDAGISel::JumpTable JT(-1U, JTI, JumpTableBB, Default); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1863 | SelectionDAGISel::JumpTableHeader JTH(First, Last, SV, CR.CaseBB, |
| 1864 | (CR.CaseBB == CurMBB)); |
| 1865 | if (CR.CaseBB == CurMBB) |
| 1866 | visitJumpTableHeader(JT, JTH); |
| 1867 | |
| 1868 | JTCases.push_back(SelectionDAGISel::JumpTableBlock(JTH, JT)); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1869 | |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1870 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1874 | /// 2 subtrees. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1875 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1876 | CaseRecVector& WorkList, |
| 1877 | Value* SV, |
| 1878 | MachineBasicBlock* Default) { |
| 1879 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1880 | // inserting any additional MBBs necessary to represent the switch. |
| 1881 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1882 | |
| 1883 | // Figure out which block is immediately after the current one. |
| 1884 | MachineBasicBlock *NextBlock = 0; |
| 1885 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1886 | |
| 1887 | if (++BBI != CurMBB->getParent()->end()) |
| 1888 | NextBlock = BBI; |
| 1889 | |
| 1890 | Case& FrontCase = *CR.Range.first; |
| 1891 | Case& BackCase = *(CR.Range.second-1); |
| 1892 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1893 | |
| 1894 | // Size is the number of Cases represented by this range. |
| 1895 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1896 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1897 | int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue(); |
| 1898 | int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1899 | double FMetric = 0; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1900 | CaseItr Pivot = CR.Range.first + Size/2; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1901 | |
| 1902 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1903 | // (heuristically) allow us to emit JumpTable's later. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1904 | uint64_t TSize = 0; |
| 1905 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 1906 | I!=E; ++I) |
| 1907 | TSize += I->size(); |
| 1908 | |
| 1909 | uint64_t LSize = FrontCase.size(); |
| 1910 | uint64_t RSize = TSize-LSize; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1911 | DOUT << "Selecting best pivot: \n" |
| 1912 | << "First: " << First << ", Last: " << Last <<"\n" |
| 1913 | << "LSize: " << LSize << ", RSize: " << RSize << "\n"; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1914 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1915 | J!=E; ++I, ++J) { |
| 1916 | int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue(); |
| 1917 | int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1918 | assert((RBegin-LEnd>=1) && "Invalid case distance"); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1919 | double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL); |
| 1920 | double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL); |
Anton Korobeynikov | 54e2b14 | 2007-04-09 21:57:03 +0000 | [diff] [blame] | 1921 | double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1922 | // Should always split in some non-trivial place |
| 1923 | DOUT <<"=>Step\n" |
| 1924 | << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n" |
| 1925 | << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n" |
| 1926 | << "Metric: " << Metric << "\n"; |
| 1927 | if (FMetric < Metric) { |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1928 | Pivot = J; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1929 | FMetric = Metric; |
| 1930 | DOUT << "Current metric set to: " << FMetric << "\n"; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1931 | } |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1932 | |
| 1933 | LSize += J->size(); |
| 1934 | RSize -= J->size(); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1935 | } |
Anton Korobeynikov | 7294b58 | 2007-05-09 20:07:08 +0000 | [diff] [blame] | 1936 | if (areJTsAllowed(TLI)) { |
| 1937 | // If our case is dense we *really* should handle it earlier! |
| 1938 | assert((FMetric > 0) && "Should handle dense range earlier!"); |
| 1939 | } else { |
| 1940 | Pivot = CR.Range.first + Size/2; |
| 1941 | } |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1942 | |
| 1943 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1944 | CaseRange RHSR(Pivot, CR.Range.second); |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1945 | Constant *C = Pivot->Low; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1946 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
| 1947 | |
| 1948 | // We know that we branch to the LHS if the Value being switched on is |
| 1949 | // less than the Pivot value, C. We use this to optimize our binary |
| 1950 | // tree a bit, by recognizing that if SV is greater than or equal to the |
| 1951 | // LHS's Case Value, and that Case Value is exactly one less than the |
| 1952 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1953 | // rather than creating a leaf node for it. |
| 1954 | if ((LHSR.second - LHSR.first) == 1 && |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1955 | LHSR.first->High == CR.GE && |
| 1956 | cast<ConstantInt>(C)->getSExtValue() == |
| 1957 | (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) { |
| 1958 | TrueBB = LHSR.first->BB; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1959 | } else { |
| 1960 | TrueBB = new MachineBasicBlock(LLVMBB); |
| 1961 | CurMF->getBasicBlockList().insert(BBI, TrueBB); |
| 1962 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
| 1963 | } |
| 1964 | |
| 1965 | // Similar to the optimization above, if the Value being switched on is |
| 1966 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1967 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1968 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1969 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1970 | cast<ConstantInt>(RHSR.first->Low)->getSExtValue() == |
| 1971 | (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) { |
| 1972 | FalseBB = RHSR.first->BB; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1973 | } else { |
| 1974 | FalseBB = new MachineBasicBlock(LLVMBB); |
| 1975 | CurMF->getBasicBlockList().insert(BBI, FalseBB); |
| 1976 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
| 1977 | } |
| 1978 | |
| 1979 | // Create a CaseBlock record representing a conditional branch to |
| 1980 | // the LHS node if the value being switched on SV is less than C. |
| 1981 | // Otherwise, branch to LHS. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 1982 | SelectionDAGISel::CaseBlock CB(ISD::SETLT, SV, C, NULL, |
| 1983 | TrueBB, FalseBB, CR.CaseBB); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1984 | |
| 1985 | if (CR.CaseBB == CurMBB) |
| 1986 | visitSwitchCase(CB); |
| 1987 | else |
| 1988 | SwitchCases.push_back(CB); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 1989 | |
| 1990 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1993 | /// handleBitTestsSwitchCase - if current case range has few destination and |
| 1994 | /// range span less, than machine word bitwidth, encode case range into series |
| 1995 | /// of masks and emit bit tests with these masks. |
| 1996 | bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR, |
| 1997 | CaseRecVector& WorkList, |
| 1998 | Value* SV, |
Chris Lattner | 3ff9817 | 2007-04-14 02:26:56 +0000 | [diff] [blame] | 1999 | MachineBasicBlock* Default){ |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2000 | unsigned IntPtrBits = MVT::getSizeInBits(TLI.getPointerTy()); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2001 | |
| 2002 | Case& FrontCase = *CR.Range.first; |
| 2003 | Case& BackCase = *(CR.Range.second-1); |
| 2004 | |
| 2005 | // Get the MachineFunction which holds the current MBB. This is used when |
| 2006 | // inserting any additional MBBs necessary to represent the switch. |
| 2007 | MachineFunction *CurMF = CurMBB->getParent(); |
| 2008 | |
| 2009 | unsigned numCmps = 0; |
| 2010 | for (CaseItr I = CR.Range.first, E = CR.Range.second; |
| 2011 | I!=E; ++I) { |
| 2012 | // Single case counts one, case range - two. |
| 2013 | if (I->Low == I->High) |
| 2014 | numCmps +=1; |
| 2015 | else |
| 2016 | numCmps +=2; |
| 2017 | } |
| 2018 | |
| 2019 | // Count unique destinations |
| 2020 | SmallSet<MachineBasicBlock*, 4> Dests; |
| 2021 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2022 | Dests.insert(I->BB); |
| 2023 | if (Dests.size() > 3) |
| 2024 | // Don't bother the code below, if there are too much unique destinations |
| 2025 | return false; |
| 2026 | } |
| 2027 | DOUT << "Total number of unique destinations: " << Dests.size() << "\n" |
| 2028 | << "Total number of comparisons: " << numCmps << "\n"; |
| 2029 | |
| 2030 | // Compute span of values. |
| 2031 | Constant* minValue = FrontCase.Low; |
| 2032 | Constant* maxValue = BackCase.High; |
| 2033 | uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() - |
| 2034 | cast<ConstantInt>(minValue)->getSExtValue(); |
| 2035 | DOUT << "Compare range: " << range << "\n" |
| 2036 | << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n" |
| 2037 | << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n"; |
| 2038 | |
Anton Korobeynikov | ab8fd40 | 2007-04-26 20:44:04 +0000 | [diff] [blame] | 2039 | if (range>=IntPtrBits || |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2040 | (!(Dests.size() == 1 && numCmps >= 3) && |
| 2041 | !(Dests.size() == 2 && numCmps >= 5) && |
| 2042 | !(Dests.size() >= 3 && numCmps >= 6))) |
| 2043 | return false; |
| 2044 | |
| 2045 | DOUT << "Emitting bit tests\n"; |
| 2046 | int64_t lowBound = 0; |
| 2047 | |
| 2048 | // Optimize the case where all the case values fit in a |
| 2049 | // word without having to subtract minValue. In this case, |
| 2050 | // we can optimize away the subtraction. |
| 2051 | if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 && |
Anton Korobeynikov | e01017b | 2007-04-14 13:25:55 +0000 | [diff] [blame] | 2052 | cast<ConstantInt>(maxValue)->getSExtValue() < IntPtrBits) { |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2053 | range = cast<ConstantInt>(maxValue)->getSExtValue(); |
| 2054 | } else { |
| 2055 | lowBound = cast<ConstantInt>(minValue)->getSExtValue(); |
| 2056 | } |
| 2057 | |
| 2058 | CaseBitsVector CasesBits; |
| 2059 | unsigned i, count = 0; |
| 2060 | |
| 2061 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) { |
| 2062 | MachineBasicBlock* Dest = I->BB; |
| 2063 | for (i = 0; i < count; ++i) |
| 2064 | if (Dest == CasesBits[i].BB) |
| 2065 | break; |
| 2066 | |
| 2067 | if (i == count) { |
| 2068 | assert((count < 3) && "Too much destinations to test!"); |
| 2069 | CasesBits.push_back(CaseBits(0, Dest, 0)); |
| 2070 | count++; |
| 2071 | } |
| 2072 | |
| 2073 | uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound; |
| 2074 | uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound; |
| 2075 | |
| 2076 | for (uint64_t j = lo; j <= hi; j++) { |
Anton Korobeynikov | e01017b | 2007-04-14 13:25:55 +0000 | [diff] [blame] | 2077 | CasesBits[i].Mask |= 1ULL << j; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2078 | CasesBits[i].Bits++; |
| 2079 | } |
| 2080 | |
| 2081 | } |
| 2082 | std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp()); |
| 2083 | |
| 2084 | SelectionDAGISel::BitTestInfo BTC; |
| 2085 | |
| 2086 | // Figure out which block is immediately after the current one. |
| 2087 | MachineFunction::iterator BBI = CR.CaseBB; |
| 2088 | ++BBI; |
| 2089 | |
| 2090 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 2091 | |
| 2092 | DOUT << "Cases:\n"; |
| 2093 | for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) { |
| 2094 | DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits |
| 2095 | << ", BB: " << CasesBits[i].BB << "\n"; |
| 2096 | |
| 2097 | MachineBasicBlock *CaseBB = new MachineBasicBlock(LLVMBB); |
| 2098 | CurMF->getBasicBlockList().insert(BBI, CaseBB); |
| 2099 | BTC.push_back(SelectionDAGISel::BitTestCase(CasesBits[i].Mask, |
| 2100 | CaseBB, |
| 2101 | CasesBits[i].BB)); |
| 2102 | } |
| 2103 | |
| 2104 | SelectionDAGISel::BitTestBlock BTB(lowBound, range, SV, |
Jeff Cohen | efc3662 | 2007-04-09 14:32:59 +0000 | [diff] [blame] | 2105 | -1U, (CR.CaseBB == CurMBB), |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2106 | CR.CaseBB, Default, BTC); |
| 2107 | |
| 2108 | if (CR.CaseBB == CurMBB) |
| 2109 | visitBitTestHeader(BTB); |
| 2110 | |
| 2111 | BitTestCases.push_back(BTB); |
| 2112 | |
| 2113 | return true; |
| 2114 | } |
| 2115 | |
| 2116 | |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 2117 | /// Clusterify - Transform simple list of Cases into list of CaseRange's |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2118 | unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases, |
| 2119 | const SwitchInst& SI) { |
| 2120 | unsigned numCmps = 0; |
| 2121 | |
| 2122 | // Start with "simple" cases |
| 2123 | for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) { |
| 2124 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)]; |
| 2125 | Cases.push_back(Case(SI.getSuccessorValue(i), |
| 2126 | SI.getSuccessorValue(i), |
| 2127 | SMBB)); |
| 2128 | } |
Chris Lattner | b3d9cdb | 2007-11-27 06:14:32 +0000 | [diff] [blame] | 2129 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Merge case into clusters |
| 2132 | if (Cases.size()>=2) |
David Greene | a2a4885 | 2007-06-29 03:42:23 +0000 | [diff] [blame] | 2133 | // Must recompute end() each iteration because it may be |
| 2134 | // invalidated by erase if we hold on to it |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 2135 | for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) { |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2136 | int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue(); |
| 2137 | int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue(); |
| 2138 | MachineBasicBlock* nextBB = J->BB; |
| 2139 | MachineBasicBlock* currentBB = I->BB; |
| 2140 | |
| 2141 | // If the two neighboring cases go to the same destination, merge them |
| 2142 | // into a single case. |
| 2143 | if ((nextValue-currentValue==1) && (currentBB == nextBB)) { |
| 2144 | I->High = J->High; |
| 2145 | J = Cases.erase(J); |
| 2146 | } else { |
| 2147 | I = J++; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) { |
| 2152 | if (I->Low != I->High) |
| 2153 | // A range counts double, since it requires two compares. |
| 2154 | ++numCmps; |
| 2155 | } |
| 2156 | |
| 2157 | return numCmps; |
| 2158 | } |
| 2159 | |
| 2160 | void SelectionDAGLowering::visitSwitch(SwitchInst &SI) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2161 | // Figure out which block is immediately after the current one. |
| 2162 | MachineBasicBlock *NextBlock = 0; |
| 2163 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 2164 | |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2165 | MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()]; |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 2166 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2167 | // If there is only the default destination, branch to it if it is not the |
| 2168 | // next basic block. Otherwise, just fall through. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2169 | if (SI.getNumOperands() == 2) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2170 | // Update machine-CFG edges. |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 2171 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2172 | // If this is not a fall-through branch, emit the branch. |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 2173 | if (Default != NextBlock) |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 2174 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(), |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 2175 | DAG.getBasicBlock(Default))); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 2177 | CurMBB->addSuccessor(Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
| 2181 | // If there are any non-default case statements, create a vector of Cases |
| 2182 | // representing each one, and sort the vector so that we can efficiently |
| 2183 | // create a binary search tree from them. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2184 | CaseVector Cases; |
| 2185 | unsigned numCmps = Clusterify(Cases, SI); |
| 2186 | DOUT << "Clusterify finished. Total clusters: " << Cases.size() |
| 2187 | << ". Total compares: " << numCmps << "\n"; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 2188 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2189 | // Get the Value to be switched on and default basic blocks, which will be |
| 2190 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 2191 | // search tree. |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 2192 | Value *SV = SI.getOperand(0); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 2193 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2194 | // Push the initial CaseRec onto the worklist |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 2195 | CaseRecVector WorkList; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2196 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 2197 | |
| 2198 | while (!WorkList.empty()) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2199 | // Grab a record representing a case range to process off the worklist |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2200 | CaseRec CR = WorkList.back(); |
| 2201 | WorkList.pop_back(); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2202 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2203 | if (handleBitTestsSwitchCase(CR, WorkList, SV, Default)) |
| 2204 | continue; |
| 2205 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2206 | // If the range has few cases (two or less) emit a series of specific |
| 2207 | // tests. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 2208 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 2209 | continue; |
| 2210 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 2211 | // If the switch has more than 5 blocks, and at least 40% dense, and the |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2212 | // target supports indirect branches, then emit a jump table rather than |
| 2213 | // lowering the switch to a binary tree of conditional branches. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame] | 2214 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 2215 | continue; |
| 2216 | |
| 2217 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 2218 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 2219 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 2224 | void SelectionDAGLowering::visitSub(User &I) { |
| 2225 | // -0.0 - X --> fneg |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2226 | const Type *Ty = I.getType(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2227 | if (isa<VectorType>(Ty)) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2228 | if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) { |
| 2229 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
| 2230 | const Type *ElTy = DestTy->getElementType(); |
Evan Cheng | c45453f | 2007-06-29 21:44:35 +0000 | [diff] [blame] | 2231 | if (ElTy->isFloatingPoint()) { |
| 2232 | unsigned VL = DestTy->getNumElements(); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2233 | std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy)); |
Evan Cheng | c45453f | 2007-06-29 21:44:35 +0000 | [diff] [blame] | 2234 | Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size()); |
| 2235 | if (CV == CNZ) { |
| 2236 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 2237 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 2238 | return; |
| 2239 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | if (Ty->isFloatingPoint()) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2244 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2245 | if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2246 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 2247 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 2248 | return; |
| 2249 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2255 | void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2256 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 2257 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2258 | |
| 2259 | setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 2262 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 2263 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 2264 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 2265 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2266 | if (MVT::getSizeInBits(TLI.getShiftAmountTy()) < |
| 2267 | MVT::getSizeInBits(Op2.getValueType())) |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2268 | Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2); |
| 2269 | else if (TLI.getShiftAmountTy() > Op2.getValueType()) |
| 2270 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 2271 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2272 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 2273 | } |
| 2274 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2275 | void SelectionDAGLowering::visitICmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2276 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 2277 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 2278 | predicate = IC->getPredicate(); |
| 2279 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 2280 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 2281 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 2282 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2283 | ISD::CondCode Opcode; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2284 | switch (predicate) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2285 | case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break; |
| 2286 | case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break; |
| 2287 | case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break; |
| 2288 | case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break; |
| 2289 | case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break; |
| 2290 | case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break; |
| 2291 | case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break; |
| 2292 | case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break; |
| 2293 | case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break; |
| 2294 | case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break; |
| 2295 | default: |
| 2296 | assert(!"Invalid ICmp predicate value"); |
| 2297 | Opcode = ISD::SETEQ; |
| 2298 | break; |
| 2299 | } |
| 2300 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
| 2301 | } |
| 2302 | |
| 2303 | void SelectionDAGLowering::visitFCmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2304 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 2305 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 2306 | predicate = FC->getPredicate(); |
| 2307 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 2308 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2309 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 2310 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2311 | ISD::CondCode Condition, FOC, FPC; |
| 2312 | switch (predicate) { |
| 2313 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 2314 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 2315 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 2316 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 2317 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 2318 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 2319 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
Dan Gohman | cba3b44 | 2008-05-01 23:40:44 +0000 | [diff] [blame] | 2320 | case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; |
| 2321 | case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2322 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 2323 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 2324 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 2325 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 2326 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 2327 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 2328 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 2329 | default: |
| 2330 | assert(!"Invalid FCmp predicate value"); |
| 2331 | FOC = FPC = ISD::SETFALSE; |
| 2332 | break; |
| 2333 | } |
| 2334 | if (FiniteOnlyFPMath()) |
| 2335 | Condition = FOC; |
| 2336 | else |
| 2337 | Condition = FPC; |
| 2338 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | void SelectionDAGLowering::visitSelect(User &I) { |
| 2342 | SDOperand Cond = getValue(I.getOperand(0)); |
| 2343 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 2344 | SDOperand FalseVal = getValue(I.getOperand(2)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2345 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 2346 | TrueVal, FalseVal)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2349 | |
| 2350 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 2351 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 2352 | SDOperand N = getValue(I.getOperand(0)); |
| 2353 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2354 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 2355 | } |
| 2356 | |
| 2357 | void SelectionDAGLowering::visitZExt(User &I) { |
| 2358 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2359 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2360 | SDOperand N = getValue(I.getOperand(0)); |
| 2361 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2362 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 2363 | } |
| 2364 | |
| 2365 | void SelectionDAGLowering::visitSExt(User &I) { |
| 2366 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 2367 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 2368 | SDOperand N = getValue(I.getOperand(0)); |
| 2369 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2370 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N)); |
| 2371 | } |
| 2372 | |
| 2373 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 2374 | // FPTrunc is never a no-op cast, no need to check |
| 2375 | SDOperand N = getValue(I.getOperand(0)); |
| 2376 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2377 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0))); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | void SelectionDAGLowering::visitFPExt(User &I){ |
| 2381 | // FPTrunc is never a no-op cast, no need to check |
| 2382 | SDOperand N = getValue(I.getOperand(0)); |
| 2383 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2384 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N)); |
| 2385 | } |
| 2386 | |
| 2387 | void SelectionDAGLowering::visitFPToUI(User &I) { |
| 2388 | // FPToUI is never a no-op cast, no need to check |
| 2389 | SDOperand N = getValue(I.getOperand(0)); |
| 2390 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2391 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N)); |
| 2392 | } |
| 2393 | |
| 2394 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 2395 | // FPToSI is never a no-op cast, no need to check |
| 2396 | SDOperand N = getValue(I.getOperand(0)); |
| 2397 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2398 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N)); |
| 2399 | } |
| 2400 | |
| 2401 | void SelectionDAGLowering::visitUIToFP(User &I) { |
| 2402 | // UIToFP is never a no-op cast, no need to check |
| 2403 | SDOperand N = getValue(I.getOperand(0)); |
| 2404 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2405 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N)); |
| 2406 | } |
| 2407 | |
| 2408 | void SelectionDAGLowering::visitSIToFP(User &I){ |
| 2409 | // UIToFP is never a no-op cast, no need to check |
| 2410 | SDOperand N = getValue(I.getOperand(0)); |
| 2411 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2412 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N)); |
| 2413 | } |
| 2414 | |
| 2415 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 2416 | // What to do depends on the size of the integer and the size of the pointer. |
| 2417 | // We can either truncate, zero extend, or no-op, accordingly. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2418 | SDOperand N = getValue(I.getOperand(0)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 2419 | MVT::ValueType SrcVT = N.getValueType(); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 2420 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2421 | SDOperand Result; |
| 2422 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 2423 | Result = DAG.getNode(ISD::TRUNCATE, DestVT, N); |
| 2424 | else |
| 2425 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 2426 | Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N); |
| 2427 | setValue(&I, Result); |
| 2428 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2429 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2430 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 2431 | // What to do depends on the size of the integer and the size of the pointer. |
| 2432 | // We can either truncate, zero extend, or no-op, accordingly. |
| 2433 | SDOperand N = getValue(I.getOperand(0)); |
| 2434 | MVT::ValueType SrcVT = N.getValueType(); |
| 2435 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 2436 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 2437 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 2438 | else |
| 2439 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 2440 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 2441 | } |
| 2442 | |
| 2443 | void SelectionDAGLowering::visitBitCast(User &I) { |
| 2444 | SDOperand N = getValue(I.getOperand(0)); |
| 2445 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2446 | |
| 2447 | // BitCast assures us that source and destination are the same size so this |
| 2448 | // is either a BIT_CONVERT or a no-op. |
| 2449 | if (DestVT != N.getValueType()) |
| 2450 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types |
| 2451 | else |
| 2452 | setValue(&I, N); // noop cast. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 2455 | void SelectionDAGLowering::visitInsertElement(User &I) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 2456 | SDOperand InVec = getValue(I.getOperand(0)); |
| 2457 | SDOperand InVal = getValue(I.getOperand(1)); |
| 2458 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 2459 | getValue(I.getOperand(2))); |
| 2460 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2461 | setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, |
| 2462 | TLI.getValueType(I.getType()), |
| 2463 | InVec, InVal, InIdx)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 2466 | void SelectionDAGLowering::visitExtractElement(User &I) { |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 2467 | SDOperand InVec = getValue(I.getOperand(0)); |
| 2468 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 2469 | getValue(I.getOperand(1))); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2470 | setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 2471 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 2472 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 2473 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2474 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
| 2475 | SDOperand V1 = getValue(I.getOperand(0)); |
| 2476 | SDOperand V2 = getValue(I.getOperand(1)); |
| 2477 | SDOperand Mask = getValue(I.getOperand(2)); |
| 2478 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2479 | setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, |
| 2480 | TLI.getValueType(I.getType()), |
| 2481 | V1, V2, Mask)); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2485 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 2486 | SDOperand N = getValue(I.getOperand(0)); |
| 2487 | const Type *Ty = I.getOperand(0)->getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2488 | |
| 2489 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 2490 | OI != E; ++OI) { |
| 2491 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2492 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2493 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2494 | if (Field) { |
| 2495 | // N = N + Offset |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 2496 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2497 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2498 | DAG.getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2499 | } |
| 2500 | Ty = StTy->getElementType(Field); |
| 2501 | } else { |
| 2502 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2504 | // If this is a constant subscript, handle it quickly. |
| 2505 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2506 | if (CI->getZExtValue() == 0) continue; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2507 | uint64_t Offs = |
Dale Johannesen | a7ac2bd | 2007-10-01 23:08:35 +0000 | [diff] [blame] | 2508 | TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2509 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
| 2510 | DAG.getIntPtrConstant(Offs)); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2511 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2512 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2513 | |
| 2514 | // N = N + Idx * ElementSize; |
Dale Johannesen | a7ac2bd | 2007-10-01 23:08:35 +0000 | [diff] [blame] | 2515 | uint64_t ElementSize = TD->getABITypeSize(Ty); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2516 | SDOperand IdxN = getValue(Idx); |
| 2517 | |
| 2518 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 2519 | // it. |
| 2520 | if (IdxN.getValueType() < N.getValueType()) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2521 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2522 | } else if (IdxN.getValueType() > N.getValueType()) |
| 2523 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 2524 | |
| 2525 | // If this is a multiply by a power of two, turn it into a shl |
| 2526 | // immediately. This is a very common case. |
| 2527 | if (isPowerOf2_64(ElementSize)) { |
| 2528 | unsigned Amt = Log2_64(ElementSize); |
| 2529 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 2530 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2531 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 2532 | continue; |
| 2533 | } |
| 2534 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2535 | SDOperand Scale = DAG.getIntPtrConstant(ElementSize); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 2536 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 2537 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | setValue(&I, N); |
| 2541 | } |
| 2542 | |
| 2543 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 2544 | // If this is a fixed sized alloca in the entry block of the function, |
| 2545 | // allocate it statically on the stack. |
| 2546 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 2547 | return; // getValue will auto-populate this. |
| 2548 | |
| 2549 | const Type *Ty = I.getAllocatedType(); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 2550 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 2551 | unsigned Align = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 2552 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 2553 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2554 | |
| 2555 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 2556 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 2557 | if (IntPtr < AllocSize.getValueType()) |
| 2558 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 2559 | else if (IntPtr > AllocSize.getValueType()) |
| 2560 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 2562 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2563 | DAG.getIntPtrConstant(TySize)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2564 | |
Evan Cheng | 4515779 | 2007-08-16 23:46:29 +0000 | [diff] [blame] | 2565 | // Handle alignment. If the requested alignment is less than or equal to |
| 2566 | // the stack alignment, ignore it. If the size is greater than or equal to |
| 2567 | // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2568 | unsigned StackAlign = |
| 2569 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
Evan Cheng | 4515779 | 2007-08-16 23:46:29 +0000 | [diff] [blame] | 2570 | if (Align <= StackAlign) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2571 | Align = 0; |
Evan Cheng | 4515779 | 2007-08-16 23:46:29 +0000 | [diff] [blame] | 2572 | |
| 2573 | // Round the size of the allocation up to the stack alignment size |
| 2574 | // by add SA-1 to the size. |
| 2575 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2576 | DAG.getIntPtrConstant(StackAlign-1)); |
Evan Cheng | 4515779 | 2007-08-16 23:46:29 +0000 | [diff] [blame] | 2577 | // Mask out the low bits for alignment purposes. |
| 2578 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2579 | DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2581 | SDOperand Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2582 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(), |
| 2583 | MVT::Other); |
| 2584 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 2585 | setValue(&I, DSA); |
| 2586 | DAG.setRoot(DSA.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Inform the Frame Information that we have just allocated a variable-sized |
| 2589 | // object. |
| 2590 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 2591 | } |
| 2592 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2593 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 2594 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 2596 | SDOperand Root; |
| 2597 | if (I.isVolatile()) |
| 2598 | Root = getRoot(); |
| 2599 | else { |
| 2600 | // Do not serialize non-volatile loads against each other. |
| 2601 | Root = DAG.getRoot(); |
| 2602 | } |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 2603 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2604 | setValue(&I, getLoadFrom(I.getType(), Ptr, I.getOperand(0), |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 2605 | Root, I.isVolatile(), I.getAlignment())); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2609 | const Value *SV, SDOperand Root, |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 2610 | bool isVolatile, |
| 2611 | unsigned Alignment) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2612 | SDOperand L = |
| 2613 | DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SV, 0, |
| 2614 | isVolatile, Alignment); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 2615 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 2616 | if (isVolatile) |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 2617 | DAG.setRoot(L.getValue(1)); |
| 2618 | else |
| 2619 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 2620 | |
| 2621 | return L; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | |
| 2625 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 2626 | Value *SrcV = I.getOperand(0); |
| 2627 | SDOperand Src = getValue(SrcV); |
| 2628 | SDOperand Ptr = getValue(I.getOperand(1)); |
Evan Cheng | 0b4f80e | 2006-12-20 01:27:29 +0000 | [diff] [blame] | 2629 | DAG.setRoot(DAG.getStore(getRoot(), Src, Ptr, I.getOperand(1), 0, |
Christopher Lamb | 95c218a | 2007-04-22 23:15:30 +0000 | [diff] [blame] | 2630 | I.isVolatile(), I.getAlignment())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2633 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2634 | /// node. |
| 2635 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
| 2636 | unsigned Intrinsic) { |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 2637 | bool HasChain = !I.doesNotAccessMemory(); |
| 2638 | bool OnlyLoad = HasChain && I.onlyReadsMemory(); |
| 2639 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2640 | // Build the operand list. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2641 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 2642 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2643 | if (OnlyLoad) { |
| 2644 | // We don't need to serialize loads against other loads. |
| 2645 | Ops.push_back(DAG.getRoot()); |
| 2646 | } else { |
| 2647 | Ops.push_back(getRoot()); |
| 2648 | } |
| 2649 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2650 | |
| 2651 | // Add the intrinsic ID as an integer operand. |
| 2652 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 2653 | |
| 2654 | // Add all operands of the call to the operand list. |
| 2655 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2656 | SDOperand Op = getValue(I.getOperand(i)); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2657 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2658 | "Intrinsic uses a non-legal type?"); |
| 2659 | Ops.push_back(Op); |
| 2660 | } |
| 2661 | |
| 2662 | std::vector<MVT::ValueType> VTs; |
| 2663 | if (I.getType() != Type::VoidTy) { |
| 2664 | MVT::ValueType VT = TLI.getValueType(I.getType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2665 | if (MVT::isVector(VT)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2666 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2667 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 2668 | |
| 2669 | VT = MVT::getVectorType(EltVT, DestTy->getNumElements()); |
| 2670 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2671 | } |
| 2672 | |
| 2673 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
| 2674 | VTs.push_back(VT); |
| 2675 | } |
| 2676 | if (HasChain) |
| 2677 | VTs.push_back(MVT::Other); |
| 2678 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2679 | const MVT::ValueType *VTList = DAG.getNodeValueTypes(VTs); |
| 2680 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2681 | // Create the node. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2682 | SDOperand Result; |
| 2683 | if (!HasChain) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2684 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(), |
| 2685 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2686 | else if (I.getType() != Type::VoidTy) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2687 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(), |
| 2688 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2689 | else |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2690 | Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(), |
| 2691 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 2693 | if (HasChain) { |
| 2694 | SDOperand Chain = Result.getValue(Result.Val->getNumValues()-1); |
| 2695 | if (OnlyLoad) |
| 2696 | PendingLoads.push_back(Chain); |
| 2697 | else |
| 2698 | DAG.setRoot(Chain); |
| 2699 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2700 | if (I.getType() != Type::VoidTy) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2701 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2702 | MVT::ValueType VT = TLI.getValueType(PTy); |
| 2703 | Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2704 | } |
| 2705 | setValue(&I, Result); |
| 2706 | } |
| 2707 | } |
| 2708 | |
Duncan Sands | b4fd45e | 2007-07-06 09:10:03 +0000 | [diff] [blame] | 2709 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2710 | static GlobalVariable *ExtractTypeInfo (Value *V) { |
Duncan Sands | b4fd45e | 2007-07-06 09:10:03 +0000 | [diff] [blame] | 2711 | V = IntrinsicInst::StripPointerCasts(V); |
| 2712 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 2713 | assert ((GV || isa<ConstantPointerNull>(V)) && |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2714 | "TypeInfo must be a global variable or NULL"); |
| 2715 | return GV; |
| 2716 | } |
| 2717 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2718 | /// addCatchInfo - Extract the personality and type infos from an eh.selector |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2719 | /// call, and add them to the specified machine basic block. |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2720 | static void addCatchInfo(CallInst &I, MachineModuleInfo *MMI, |
| 2721 | MachineBasicBlock *MBB) { |
| 2722 | // Inform the MachineModuleInfo of the personality for this landing pad. |
| 2723 | ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); |
| 2724 | assert(CE->getOpcode() == Instruction::BitCast && |
| 2725 | isa<Function>(CE->getOperand(0)) && |
| 2726 | "Personality should be a function"); |
| 2727 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 2728 | |
| 2729 | // Gather all the type infos for this landing pad and pass them along to |
| 2730 | // MachineModuleInfo. |
| 2731 | std::vector<GlobalVariable *> TyInfo; |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2732 | unsigned N = I.getNumOperands(); |
| 2733 | |
| 2734 | for (unsigned i = N - 1; i > 2; --i) { |
| 2735 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { |
| 2736 | unsigned FilterLength = CI->getZExtValue(); |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 2737 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2738 | assert (FirstCatch <= N && "Invalid filter length"); |
| 2739 | |
| 2740 | if (FirstCatch < N) { |
| 2741 | TyInfo.reserve(N - FirstCatch); |
| 2742 | for (unsigned j = FirstCatch; j < N; ++j) |
| 2743 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 2744 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 2745 | TyInfo.clear(); |
| 2746 | } |
| 2747 | |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 2748 | if (!FilterLength) { |
| 2749 | // Cleanup. |
| 2750 | MMI->addCleanup(MBB); |
| 2751 | } else { |
| 2752 | // Filter. |
| 2753 | TyInfo.reserve(FilterLength - 1); |
| 2754 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
| 2755 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
| 2756 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 2757 | TyInfo.clear(); |
| 2758 | } |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2759 | |
| 2760 | N = i; |
| 2761 | } |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2762 | } |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2763 | |
| 2764 | if (N > 3) { |
| 2765 | TyInfo.reserve(N - 3); |
| 2766 | for (unsigned j = 3; j < N; ++j) |
| 2767 | TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2768 | MMI->addCatchTypeInfo(MBB, TyInfo); |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2769 | } |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2772 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 2773 | /// we want to emit this as a call to a named external function, return the name |
| 2774 | /// otherwise lower it and return null. |
| 2775 | const char * |
| 2776 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 2777 | switch (Intrinsic) { |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2778 | default: |
| 2779 | // By default, turn this into a target intrinsic node. |
| 2780 | visitTargetIntrinsic(I, Intrinsic); |
| 2781 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2782 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 2783 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 2784 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 2785 | case Intrinsic::returnaddress: |
| 2786 | setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(), |
| 2787 | getValue(I.getOperand(1)))); |
| 2788 | return 0; |
| 2789 | case Intrinsic::frameaddress: |
| 2790 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(), |
| 2791 | getValue(I.getOperand(1)))); |
| 2792 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2793 | case Intrinsic::setjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 2794 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2795 | break; |
| 2796 | case Intrinsic::longjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 2797 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2798 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2799 | case Intrinsic::memcpy_i32: |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2800 | case Intrinsic::memcpy_i64: { |
| 2801 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 2802 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 2803 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 2804 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 2805 | DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false, |
| 2806 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2807 | return 0; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2808 | } |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2809 | case Intrinsic::memset_i32: |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2810 | case Intrinsic::memset_i64: { |
| 2811 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 2812 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 2813 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 2814 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 2815 | DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align, |
| 2816 | I.getOperand(1), 0)); |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2817 | return 0; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2818 | } |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2819 | case Intrinsic::memmove_i32: |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2820 | case Intrinsic::memmove_i64: { |
| 2821 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 2822 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 2823 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 2824 | unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue(); |
| 2825 | |
| 2826 | // If the source and destination are known to not be aliases, we can |
| 2827 | // lower memmove as memcpy. |
| 2828 | uint64_t Size = -1ULL; |
| 2829 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3)) |
| 2830 | Size = C->getValue(); |
| 2831 | if (AA.alias(I.getOperand(1), Size, I.getOperand(2), Size) == |
| 2832 | AliasAnalysis::NoAlias) { |
| 2833 | DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false, |
| 2834 | I.getOperand(1), 0, I.getOperand(2), 0)); |
| 2835 | return 0; |
| 2836 | } |
| 2837 | |
| 2838 | DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align, |
| 2839 | I.getOperand(1), 0, I.getOperand(2), 0)); |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2840 | return 0; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 2841 | } |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 2842 | case Intrinsic::dbg_stoppoint: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2843 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2844 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2845 | if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2846 | SDOperand Ops[5]; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2848 | Ops[0] = getRoot(); |
| 2849 | Ops[1] = getValue(SPI.getLineValue()); |
| 2850 | Ops[2] = getValue(SPI.getColumnValue()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2851 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2852 | DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 2853 | assert(DD && "Not a debug information descriptor"); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2854 | CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD); |
| 2855 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2856 | Ops[3] = DAG.getString(CompileUnit->getFileName()); |
| 2857 | Ops[4] = DAG.getString(CompileUnit->getDirectory()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 2858 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2859 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops, 5)); |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 2860 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2861 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2862 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2863 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2864 | case Intrinsic::dbg_region_start: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2865 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2866 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2867 | if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) { |
| 2868 | unsigned LabelID = MMI->RecordRegionStart(RSI.getContext()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 2869 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 2870 | DAG.getConstant(LabelID, MVT::i32), |
| 2871 | DAG.getConstant(0, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2874 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2875 | } |
| 2876 | case Intrinsic::dbg_region_end: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2877 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2878 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2879 | if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) { |
| 2880 | unsigned LabelID = MMI->RecordRegionEnd(REI.getContext()); |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 2881 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
| 2882 | DAG.getConstant(LabelID, MVT::i32), |
| 2883 | DAG.getConstant(0, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2886 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2887 | } |
| 2888 | case Intrinsic::dbg_func_start: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2889 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 2890 | if (!MMI) return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2891 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 2892 | Value *SP = FSI.getSubprogram(); |
| 2893 | if (SP && MMI->Verify(SP)) { |
| 2894 | // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is |
| 2895 | // what (most?) gdb expects. |
| 2896 | DebugInfoDesc *DD = MMI->getDescFor(SP); |
| 2897 | assert(DD && "Not a debug information descriptor"); |
| 2898 | SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD); |
| 2899 | const CompileUnitDesc *CompileUnit = Subprogram->getFile(); |
| 2900 | unsigned SrcFile = MMI->RecordSource(CompileUnit->getDirectory(), |
| 2901 | CompileUnit->getFileName()); |
| 2902 | // Record the source line but does create a label. It will be emitted |
| 2903 | // at asm emission time. |
| 2904 | MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2907 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2908 | } |
| 2909 | case Intrinsic::dbg_declare: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2910 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2911 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 2912 | Value *Variable = DI.getVariable(); |
| 2913 | if (MMI && Variable && MMI->Verify(Variable)) |
| 2914 | DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(), |
| 2915 | getValue(DI.getAddress()), getValue(Variable))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2916 | return 0; |
| 2917 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2918 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2919 | case Intrinsic::eh_exception: { |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 2920 | if (!CurMBB->isLandingPad()) { |
| 2921 | // FIXME: Mark exception register as live in. Hack for PR1508. |
| 2922 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 2923 | if (Reg) CurMBB->addLiveIn(Reg); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2924 | } |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 2925 | // Insert the EXCEPTIONADDR instruction. |
| 2926 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 2927 | SDOperand Ops[1]; |
| 2928 | Ops[0] = DAG.getRoot(); |
| 2929 | SDOperand Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1); |
| 2930 | setValue(&I, Op); |
| 2931 | DAG.setRoot(Op.getValue(1)); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2932 | return 0; |
| 2933 | } |
| 2934 | |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2935 | case Intrinsic::eh_selector_i32: |
| 2936 | case Intrinsic::eh_selector_i64: { |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2937 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2938 | MVT::ValueType VT = (Intrinsic == Intrinsic::eh_selector_i32 ? |
| 2939 | MVT::i32 : MVT::i64); |
| 2940 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 2941 | if (MMI) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2942 | if (CurMBB->isLandingPad()) |
| 2943 | addCatchInfo(I, MMI, CurMBB); |
Evan Cheng | e47c333 | 2007-06-27 18:45:32 +0000 | [diff] [blame] | 2944 | else { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2945 | #ifndef NDEBUG |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 2946 | FuncInfo.CatchInfoLost.insert(&I); |
| 2947 | #endif |
Duncan Sands | 9029195 | 2007-07-06 09:18:59 +0000 | [diff] [blame] | 2948 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 2949 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 2950 | if (Reg) CurMBB->addLiveIn(Reg); |
Evan Cheng | e47c333 | 2007-06-27 18:45:32 +0000 | [diff] [blame] | 2951 | } |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Insert the EHSELECTION instruction. |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2954 | SDVTList VTs = DAG.getVTList(VT, MVT::Other); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2955 | SDOperand Ops[2]; |
| 2956 | Ops[0] = getValue(I.getOperand(1)); |
| 2957 | Ops[1] = getRoot(); |
| 2958 | SDOperand Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2); |
| 2959 | setValue(&I, Op); |
| 2960 | DAG.setRoot(Op.getValue(1)); |
Jim Laskey | 7a1de98 | 2007-02-24 09:45:44 +0000 | [diff] [blame] | 2961 | } else { |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2962 | setValue(&I, DAG.getConstant(0, VT)); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2963 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2964 | |
| 2965 | return 0; |
| 2966 | } |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2967 | |
| 2968 | case Intrinsic::eh_typeid_for_i32: |
| 2969 | case Intrinsic::eh_typeid_for_i64: { |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2970 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2971 | MVT::ValueType VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ? |
| 2972 | MVT::i32 : MVT::i64); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2973 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2974 | if (MMI) { |
| 2975 | // Find the type id for the given typeinfo. |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 2976 | GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1)); |
Duncan Sands | 3b34636 | 2007-05-04 17:12:26 +0000 | [diff] [blame] | 2977 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2978 | unsigned TypeID = MMI->getTypeIDFor(GV); |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2979 | setValue(&I, DAG.getConstant(TypeID, VT)); |
Jim Laskey | 7a1de98 | 2007-02-24 09:45:44 +0000 | [diff] [blame] | 2980 | } else { |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 2981 | // Return something different to eh_selector. |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 2982 | setValue(&I, DAG.getConstant(1, VT)); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2983 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2984 | |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 2988 | case Intrinsic::eh_return: { |
| 2989 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 2990 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 2991 | if (MMI) { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 2992 | MMI->setCallsEHReturn(true); |
| 2993 | DAG.setRoot(DAG.getNode(ISD::EH_RETURN, |
| 2994 | MVT::Other, |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 2995 | getControlRoot(), |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 2996 | getValue(I.getOperand(1)), |
| 2997 | getValue(I.getOperand(2)))); |
| 2998 | } else { |
| 2999 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
| 3000 | } |
| 3001 | |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | case Intrinsic::eh_unwind_init: { |
| 3006 | if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) { |
| 3007 | MMI->setCallsUnwindInit(true); |
| 3008 | } |
| 3009 | |
| 3010 | return 0; |
| 3011 | } |
| 3012 | |
| 3013 | case Intrinsic::eh_dwarf_cfa: { |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 3014 | MVT::ValueType VT = getValue(I.getOperand(1)).getValueType(); |
| 3015 | SDOperand CfaArg; |
| 3016 | if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getPointerTy())) |
| 3017 | CfaArg = DAG.getNode(ISD::TRUNCATE, |
| 3018 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
| 3019 | else |
| 3020 | CfaArg = DAG.getNode(ISD::SIGN_EXTEND, |
| 3021 | TLI.getPointerTy(), getValue(I.getOperand(1))); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 3022 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 3023 | SDOperand Offset = DAG.getNode(ISD::ADD, |
| 3024 | TLI.getPointerTy(), |
| 3025 | DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, |
| 3026 | TLI.getPointerTy()), |
| 3027 | CfaArg); |
| 3028 | setValue(&I, DAG.getNode(ISD::ADD, |
| 3029 | TLI.getPointerTy(), |
| 3030 | DAG.getNode(ISD::FRAMEADDR, |
| 3031 | TLI.getPointerTy(), |
| 3032 | DAG.getConstant(0, |
| 3033 | TLI.getPointerTy())), |
| 3034 | Offset)); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 3035 | return 0; |
| 3036 | } |
| 3037 | |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 3038 | case Intrinsic::sqrt: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3039 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 3040 | getValue(I.getOperand(1)).getValueType(), |
| 3041 | getValue(I.getOperand(1)))); |
| 3042 | return 0; |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 3043 | case Intrinsic::powi: |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3044 | setValue(&I, DAG.getNode(ISD::FPOWI, |
| 3045 | getValue(I.getOperand(1)).getValueType(), |
| 3046 | getValue(I.getOperand(1)), |
| 3047 | getValue(I.getOperand(2)))); |
| 3048 | return 0; |
Dan Gohman | ac9385a | 2007-10-12 00:01:22 +0000 | [diff] [blame] | 3049 | case Intrinsic::sin: |
| 3050 | setValue(&I, DAG.getNode(ISD::FSIN, |
| 3051 | getValue(I.getOperand(1)).getValueType(), |
| 3052 | getValue(I.getOperand(1)))); |
| 3053 | return 0; |
| 3054 | case Intrinsic::cos: |
| 3055 | setValue(&I, DAG.getNode(ISD::FCOS, |
| 3056 | getValue(I.getOperand(1)).getValueType(), |
| 3057 | getValue(I.getOperand(1)))); |
| 3058 | return 0; |
| 3059 | case Intrinsic::pow: |
| 3060 | setValue(&I, DAG.getNode(ISD::FPOW, |
| 3061 | getValue(I.getOperand(1)).getValueType(), |
| 3062 | getValue(I.getOperand(1)), |
| 3063 | getValue(I.getOperand(2)))); |
| 3064 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3065 | case Intrinsic::pcmarker: { |
| 3066 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 3067 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 3068 | return 0; |
| 3069 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 3070 | case Intrinsic::readcyclecounter: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3071 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3072 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, |
| 3073 | DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2, |
| 3074 | &Op, 1); |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 3075 | setValue(&I, Tmp); |
| 3076 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 3077 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 3078 | } |
Chris Lattner | c6eb6d7 | 2007-04-10 03:20:39 +0000 | [diff] [blame] | 3079 | case Intrinsic::part_select: { |
Reid Spencer | 3f108cb | 2007-04-05 01:20:18 +0000 | [diff] [blame] | 3080 | // Currently not implemented: just abort |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 3081 | assert(0 && "part_select intrinsic not implemented"); |
| 3082 | abort(); |
| 3083 | } |
| 3084 | case Intrinsic::part_set: { |
| 3085 | // Currently not implemented: just abort |
| 3086 | assert(0 && "part_set intrinsic not implemented"); |
Reid Spencer | 3f108cb | 2007-04-05 01:20:18 +0000 | [diff] [blame] | 3087 | abort(); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 3088 | } |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3089 | case Intrinsic::bswap: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3090 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 3091 | getValue(I.getOperand(1)).getValueType(), |
| 3092 | getValue(I.getOperand(1)))); |
| 3093 | return 0; |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3094 | case Intrinsic::cttz: { |
| 3095 | SDOperand Arg = getValue(I.getOperand(1)); |
| 3096 | MVT::ValueType Ty = Arg.getValueType(); |
| 3097 | SDOperand result = DAG.getNode(ISD::CTTZ, Ty, Arg); |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3098 | setValue(&I, result); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3099 | return 0; |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3100 | } |
| 3101 | case Intrinsic::ctlz: { |
| 3102 | SDOperand Arg = getValue(I.getOperand(1)); |
| 3103 | MVT::ValueType Ty = Arg.getValueType(); |
| 3104 | SDOperand result = DAG.getNode(ISD::CTLZ, Ty, Arg); |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3105 | setValue(&I, result); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3106 | return 0; |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3107 | } |
| 3108 | case Intrinsic::ctpop: { |
| 3109 | SDOperand Arg = getValue(I.getOperand(1)); |
| 3110 | MVT::ValueType Ty = Arg.getValueType(); |
| 3111 | SDOperand result = DAG.getNode(ISD::CTPOP, Ty, Arg); |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3112 | setValue(&I, result); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3113 | return 0; |
Reid Spencer | a4f9c4d | 2007-04-01 07:34:11 +0000 | [diff] [blame] | 3114 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 3115 | case Intrinsic::stacksave: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3116 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3117 | SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, |
| 3118 | DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 3119 | setValue(&I, Tmp); |
| 3120 | DAG.setRoot(Tmp.getValue(1)); |
| 3121 | return 0; |
| 3122 | } |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 3123 | case Intrinsic::stackrestore: { |
| 3124 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 3125 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 3126 | return 0; |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 3127 | } |
Tanya Lattner | 24e5aad | 2007-06-15 22:26:58 +0000 | [diff] [blame] | 3128 | case Intrinsic::var_annotation: |
| 3129 | // Discard annotate attributes |
| 3130 | return 0; |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3131 | |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3132 | case Intrinsic::init_trampoline: { |
| 3133 | const Function *F = |
| 3134 | cast<Function>(IntrinsicInst::StripPointerCasts(I.getOperand(2))); |
| 3135 | |
| 3136 | SDOperand Ops[6]; |
| 3137 | Ops[0] = getRoot(); |
| 3138 | Ops[1] = getValue(I.getOperand(1)); |
| 3139 | Ops[2] = getValue(I.getOperand(2)); |
| 3140 | Ops[3] = getValue(I.getOperand(3)); |
| 3141 | Ops[4] = DAG.getSrcValue(I.getOperand(1)); |
| 3142 | Ops[5] = DAG.getSrcValue(F); |
| 3143 | |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 3144 | SDOperand Tmp = DAG.getNode(ISD::TRAMPOLINE, |
| 3145 | DAG.getNodeValueTypes(TLI.getPointerTy(), |
| 3146 | MVT::Other), 2, |
| 3147 | Ops, 6); |
| 3148 | |
| 3149 | setValue(&I, Tmp); |
| 3150 | DAG.setRoot(Tmp.getValue(1)); |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3151 | return 0; |
| 3152 | } |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 3153 | |
| 3154 | case Intrinsic::gcroot: |
| 3155 | if (GCI) { |
| 3156 | Value *Alloca = I.getOperand(1); |
| 3157 | Constant *TypeMap = cast<Constant>(I.getOperand(2)); |
| 3158 | |
| 3159 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).Val); |
| 3160 | GCI->addStackRoot(FI->getIndex(), TypeMap); |
| 3161 | } |
| 3162 | return 0; |
| 3163 | |
| 3164 | case Intrinsic::gcread: |
| 3165 | case Intrinsic::gcwrite: |
| 3166 | assert(0 && "Collector failed to lower gcread/gcwrite intrinsics!"); |
| 3167 | return 0; |
| 3168 | |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 3169 | case Intrinsic::flt_rounds: { |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 3170 | setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32)); |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 3171 | return 0; |
| 3172 | } |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3173 | |
| 3174 | case Intrinsic::trap: { |
| 3175 | DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot())); |
| 3176 | return 0; |
| 3177 | } |
Evan Cheng | 27b7db5 | 2008-03-08 00:58:38 +0000 | [diff] [blame] | 3178 | case Intrinsic::prefetch: { |
| 3179 | SDOperand Ops[4]; |
| 3180 | Ops[0] = getRoot(); |
| 3181 | Ops[1] = getValue(I.getOperand(1)); |
| 3182 | Ops[2] = getValue(I.getOperand(2)); |
| 3183 | Ops[3] = getValue(I.getOperand(3)); |
| 3184 | DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4)); |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
Andrew Lenharth | 22c5c1b | 2008-02-16 01:24:58 +0000 | [diff] [blame] | 3188 | case Intrinsic::memory_barrier: { |
| 3189 | SDOperand Ops[6]; |
| 3190 | Ops[0] = getRoot(); |
| 3191 | for (int x = 1; x < 6; ++x) |
| 3192 | Ops[x] = getValue(I.getOperand(x)); |
| 3193 | |
| 3194 | DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6)); |
| 3195 | return 0; |
| 3196 | } |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 3197 | case Intrinsic::atomic_lcs: { |
| 3198 | SDOperand Root = getRoot(); |
| 3199 | SDOperand O3 = getValue(I.getOperand(3)); |
| 3200 | SDOperand L = DAG.getAtomic(ISD::ATOMIC_LCS, Root, |
| 3201 | getValue(I.getOperand(1)), |
| 3202 | getValue(I.getOperand(2)), |
| 3203 | O3, O3.getValueType()); |
| 3204 | setValue(&I, L); |
| 3205 | DAG.setRoot(L.getValue(1)); |
| 3206 | return 0; |
| 3207 | } |
| 3208 | case Intrinsic::atomic_las: { |
| 3209 | SDOperand Root = getRoot(); |
| 3210 | SDOperand O2 = getValue(I.getOperand(2)); |
| 3211 | SDOperand L = DAG.getAtomic(ISD::ATOMIC_LAS, Root, |
| 3212 | getValue(I.getOperand(1)), |
| 3213 | O2, O2.getValueType()); |
| 3214 | setValue(&I, L); |
| 3215 | DAG.setRoot(L.getValue(1)); |
| 3216 | return 0; |
| 3217 | } |
| 3218 | case Intrinsic::atomic_swap: { |
| 3219 | SDOperand Root = getRoot(); |
| 3220 | SDOperand O2 = getValue(I.getOperand(2)); |
| 3221 | SDOperand L = DAG.getAtomic(ISD::ATOMIC_SWAP, Root, |
| 3222 | getValue(I.getOperand(1)), |
| 3223 | O2, O2.getValueType()); |
| 3224 | setValue(&I, L); |
| 3225 | DAG.setRoot(L.getValue(1)); |
| 3226 | return 0; |
| 3227 | } |
| 3228 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3233 | void SelectionDAGLowering::LowerCallTo(CallSite CS, SDOperand Callee, |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 3234 | bool IsTailCall, |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3235 | MachineBasicBlock *LandingPad) { |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3236 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3237 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3238 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 3239 | unsigned BeginLabel = 0, EndLabel = 0; |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3240 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3241 | TargetLowering::ArgListTy Args; |
| 3242 | TargetLowering::ArgListEntry Entry; |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3243 | Args.reserve(CS.arg_size()); |
| 3244 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 3245 | i != e; ++i) { |
| 3246 | SDOperand ArgNode = getValue(*i); |
| 3247 | Entry.Node = ArgNode; Entry.Ty = (*i)->getType(); |
Duncan Sands | 4fee703 | 2007-05-07 20:49:28 +0000 | [diff] [blame] | 3248 | |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3249 | unsigned attrInd = i - CS.arg_begin() + 1; |
| 3250 | Entry.isSExt = CS.paramHasAttr(attrInd, ParamAttr::SExt); |
| 3251 | Entry.isZExt = CS.paramHasAttr(attrInd, ParamAttr::ZExt); |
| 3252 | Entry.isInReg = CS.paramHasAttr(attrInd, ParamAttr::InReg); |
| 3253 | Entry.isSRet = CS.paramHasAttr(attrInd, ParamAttr::StructRet); |
| 3254 | Entry.isNest = CS.paramHasAttr(attrInd, ParamAttr::Nest); |
| 3255 | Entry.isByVal = CS.paramHasAttr(attrInd, ParamAttr::ByVal); |
Dale Johannesen | 08e78b1 | 2008-02-22 17:49:45 +0000 | [diff] [blame] | 3256 | Entry.Alignment = CS.getParamAlignment(attrInd); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3257 | Args.push_back(Entry); |
| 3258 | } |
| 3259 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 3260 | if (LandingPad && MMI) { |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3261 | // Insert a label before the invoke call to mark the try range. This can be |
| 3262 | // used to detect deletion of the invoke via the MachineModuleInfo. |
| 3263 | BeginLabel = MMI->NextLabelID(); |
Dale Johannesen | a4091d3 | 2008-04-04 23:48:31 +0000 | [diff] [blame] | 3264 | // Both PendingLoads and PendingExports must be flushed here; |
| 3265 | // this call might not return. |
| 3266 | (void)getRoot(); |
| 3267 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getControlRoot(), |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 3268 | DAG.getConstant(BeginLabel, MVT::i32), |
| 3269 | DAG.getConstant(1, MVT::i32))); |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3270 | } |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3271 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3272 | std::pair<SDOperand,SDOperand> Result = |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3273 | TLI.LowerCallTo(getRoot(), CS.getType(), |
| 3274 | CS.paramHasAttr(0, ParamAttr::SExt), |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 3275 | CS.paramHasAttr(0, ParamAttr::ZExt), |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3276 | FTy->isVarArg(), CS.getCallingConv(), IsTailCall, |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3277 | Callee, Args, DAG); |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3278 | if (CS.getType() != Type::VoidTy) |
| 3279 | setValue(CS.getInstruction(), Result.first); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3280 | DAG.setRoot(Result.second); |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3281 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 3282 | if (LandingPad && MMI) { |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3283 | // Insert a label at the end of the invoke call to mark the try range. This |
| 3284 | // can be used to detect deletion of the invoke via the MachineModuleInfo. |
| 3285 | EndLabel = MMI->NextLabelID(); |
| 3286 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 3287 | DAG.getConstant(EndLabel, MVT::i32), |
| 3288 | DAG.getConstant(1, MVT::i32))); |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3289 | |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3290 | // Inform MachineModuleInfo of range. |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3291 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 3292 | } |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3296 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 3297 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3298 | if (Function *F = I.getCalledFunction()) { |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3299 | if (F->isDeclaration()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3300 | if (unsigned IID = F->getIntrinsicID()) { |
| 3301 | RenameFn = visitIntrinsicCall(I, IID); |
| 3302 | if (!RenameFn) |
| 3303 | return; |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | // Check for well-known libc/libm calls. If the function is internal, it |
| 3308 | // can't be a library call. |
| 3309 | unsigned NameLen = F->getNameLen(); |
| 3310 | if (!F->hasInternalLinkage() && NameLen) { |
| 3311 | const char *NameStr = F->getNameStart(); |
| 3312 | if (NameStr[0] == 'c' && |
| 3313 | ((NameLen == 8 && !strcmp(NameStr, "copysign")) || |
| 3314 | (NameLen == 9 && !strcmp(NameStr, "copysignf")))) { |
| 3315 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 3316 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 3317 | I.getType() == I.getOperand(1)->getType() && |
| 3318 | I.getType() == I.getOperand(2)->getType()) { |
| 3319 | SDOperand LHS = getValue(I.getOperand(1)); |
| 3320 | SDOperand RHS = getValue(I.getOperand(2)); |
| 3321 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(), |
| 3322 | LHS, RHS)); |
| 3323 | return; |
| 3324 | } |
| 3325 | } else if (NameStr[0] == 'f' && |
| 3326 | ((NameLen == 4 && !strcmp(NameStr, "fabs")) || |
Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 3327 | (NameLen == 5 && !strcmp(NameStr, "fabsf")) || |
| 3328 | (NameLen == 5 && !strcmp(NameStr, "fabsl")))) { |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3329 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 3330 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 3331 | I.getType() == I.getOperand(1)->getType()) { |
| 3332 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 3333 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 3334 | return; |
| 3335 | } |
| 3336 | } else if (NameStr[0] == 's' && |
| 3337 | ((NameLen == 3 && !strcmp(NameStr, "sin")) || |
Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 3338 | (NameLen == 4 && !strcmp(NameStr, "sinf")) || |
| 3339 | (NameLen == 4 && !strcmp(NameStr, "sinl")))) { |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3340 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 3341 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 3342 | I.getType() == I.getOperand(1)->getType()) { |
| 3343 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 3344 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 3345 | return; |
| 3346 | } |
| 3347 | } else if (NameStr[0] == 'c' && |
| 3348 | ((NameLen == 3 && !strcmp(NameStr, "cos")) || |
Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 3349 | (NameLen == 4 && !strcmp(NameStr, "cosf")) || |
| 3350 | (NameLen == 4 && !strcmp(NameStr, "cosl")))) { |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3351 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 3352 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 3353 | I.getType() == I.getOperand(1)->getType()) { |
| 3354 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 3355 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 3356 | return; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3357 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 3358 | } |
Chris Lattner | 87b51bc | 2007-09-10 21:15:22 +0000 | [diff] [blame] | 3359 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3360 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 3361 | visitInlineAsm(&I); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3362 | return; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3363 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3364 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 3365 | SDOperand Callee; |
| 3366 | if (!RenameFn) |
| 3367 | Callee = getValue(I.getOperand(0)); |
| 3368 | else |
| 3369 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 3370 | |
Duncan Sands | 6f74b48 | 2007-12-19 09:48:52 +0000 | [diff] [blame] | 3371 | LowerCallTo(&I, Callee, I.isTailCall()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 3374 | |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 3375 | void SelectionDAGLowering::visitGetResult(GetResultInst &I) { |
Dan Gohman | 67780f1 | 2008-04-23 20:25:16 +0000 | [diff] [blame] | 3376 | if (isa<UndefValue>(I.getOperand(0))) { |
Dan Gohman | 3dc34f6 | 2008-04-23 20:21:29 +0000 | [diff] [blame] | 3377 | SDOperand Undef = DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())); |
| 3378 | setValue(&I, Undef); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3379 | return; |
Dan Gohman | 3dc34f6 | 2008-04-23 20:21:29 +0000 | [diff] [blame] | 3380 | } |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3381 | |
| 3382 | // To add support for individual return values with aggregate types, |
| 3383 | // we'd need a way to take a getresult index and determine which |
| 3384 | // values of the Call SDNode are associated with it. |
| 3385 | assert(TLI.getValueType(I.getType(), true) != MVT::Other && |
| 3386 | "Individual return values must not be aggregates!"); |
| 3387 | |
| 3388 | SDOperand Call = getValue(I.getOperand(0)); |
| 3389 | setValue(&I, SDOperand(Call.Val, I.getIndex())); |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3393 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 3394 | /// this value and returns the result as a ValueVT value. This uses |
| 3395 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 3396 | /// If the Flag pointer is NULL, no flag is used. |
| 3397 | SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3398 | SDOperand &Chain, |
| 3399 | SDOperand *Flag) const { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3400 | // Assemble the legal parts into the final values. |
| 3401 | SmallVector<SDOperand, 4> Values(ValueVTs.size()); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3402 | SmallVector<SDOperand, 8> Parts; |
| 3403 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3404 | // Copy the legal parts from the registers. |
| 3405 | MVT::ValueType ValueVT = ValueVTs[Value]; |
| 3406 | unsigned NumRegs = TLI->getNumRegisters(ValueVT); |
| 3407 | MVT::ValueType RegisterVT = RegVTs[Value]; |
| 3408 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3409 | Parts.resize(NumRegs); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3410 | for (unsigned i = 0; i != NumRegs; ++i) { |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3411 | SDOperand P; |
| 3412 | if (Flag == 0) |
| 3413 | P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT); |
| 3414 | else { |
| 3415 | P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3416 | *Flag = P.getValue(2); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3417 | } |
| 3418 | Chain = P.getValue(1); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3419 | Parts[Part+i] = P; |
| 3420 | } |
Chris Lattner | 5df99b3 | 2007-03-25 05:00:54 +0000 | [diff] [blame] | 3421 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3422 | Values[Value] = getCopyFromParts(DAG, &Parts[Part], NumRegs, RegisterVT, |
| 3423 | ValueVT); |
| 3424 | Part += NumRegs; |
| 3425 | } |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3426 | |
| 3427 | if (ValueVTs.size() == 1) |
| 3428 | return Values[0]; |
| 3429 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3430 | return DAG.getNode(ISD::MERGE_VALUES, |
| 3431 | DAG.getVTList(&ValueVTs[0], ValueVTs.size()), |
| 3432 | &Values[0], ValueVTs.size()); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3435 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 3436 | /// specified value into the registers specified by this object. This uses |
| 3437 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3438 | /// If the Flag pointer is NULL, no flag is used. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3439 | void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3440 | SDOperand &Chain, SDOperand *Flag) const { |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3441 | // Get the list of the values's legal parts. |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3442 | unsigned NumRegs = Regs.size(); |
| 3443 | SmallVector<SDOperand, 8> Parts(NumRegs); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3444 | for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3445 | MVT::ValueType ValueVT = ValueVTs[Value]; |
| 3446 | unsigned NumParts = TLI->getNumRegisters(ValueVT); |
| 3447 | MVT::ValueType RegisterVT = RegVTs[Value]; |
| 3448 | |
| 3449 | getCopyToParts(DAG, Val.getValue(Val.ResNo + Value), |
| 3450 | &Parts[Part], NumParts, RegisterVT); |
| 3451 | Part += NumParts; |
| 3452 | } |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3453 | |
| 3454 | // Copy the parts into the registers. |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3455 | SmallVector<SDOperand, 8> Chains(NumRegs); |
| 3456 | for (unsigned i = 0; i != NumRegs; ++i) { |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3457 | SDOperand Part; |
| 3458 | if (Flag == 0) |
| 3459 | Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]); |
| 3460 | else { |
| 3461 | Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 3462 | *Flag = Part.getValue(1); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3463 | } |
| 3464 | Chains[i] = Part.getValue(0); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3465 | } |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3466 | |
Evan Cheng | 33bf38a | 2008-04-28 22:07:13 +0000 | [diff] [blame] | 3467 | if (NumRegs == 1 || Flag) |
| 3468 | // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is |
| 3469 | // flagged to it. That is the CopyToReg nodes and the user are considered |
| 3470 | // a single scheduling unit. If we create a TokenFactor and return it as |
| 3471 | // chain, then the TokenFactor is both a predecessor (operand) of the |
| 3472 | // user as well as a successor (the TF operands are flagged to the user). |
| 3473 | // c1, f1 = CopyToReg |
| 3474 | // c2, f2 = CopyToReg |
| 3475 | // c3 = TokenFactor c1, c2 |
| 3476 | // ... |
| 3477 | // = op c3, ..., f2 |
| 3478 | Chain = Chains[NumRegs-1]; |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3479 | else |
| 3480 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3481 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3482 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3483 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 3484 | /// operand list. This adds the code marker and includes the number of |
| 3485 | /// values added into it. |
| 3486 | void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 3487 | std::vector<SDOperand> &Ops) const { |
Chris Lattner | 4b993b1 | 2007-04-09 00:33:58 +0000 | [diff] [blame] | 3488 | MVT::ValueType IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
| 3489 | Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy)); |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3490 | for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 3491 | unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3492 | MVT::ValueType RegisterVT = RegVTs[Value]; |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 3493 | for (unsigned i = 0; i != NumRegs; ++i) |
| 3494 | Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT)); |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3495 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3496 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3497 | |
| 3498 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 3499 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 3500 | /// register class for the register. Otherwise, return null. |
| 3501 | static const TargetRegisterClass * |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3502 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 3503 | const TargetLowering &TLI, |
| 3504 | const TargetRegisterInfo *TRI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3505 | MVT::ValueType FoundVT = MVT::Other; |
| 3506 | const TargetRegisterClass *FoundRC = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 3507 | for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(), |
| 3508 | E = TRI->regclass_end(); RCI != E; ++RCI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3509 | MVT::ValueType ThisVT = MVT::Other; |
| 3510 | |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3511 | const TargetRegisterClass *RC = *RCI; |
| 3512 | // If none of the the value types for this register class are valid, we |
| 3513 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3514 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 3515 | I != E; ++I) { |
| 3516 | if (TLI.isTypeLegal(*I)) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3517 | // If we have already found this register in a different register class, |
| 3518 | // choose the one with the largest VT specified. For example, on |
| 3519 | // PowerPC, we favor f64 register classes over f32. |
| 3520 | if (FoundVT == MVT::Other || |
| 3521 | MVT::getSizeInBits(FoundVT) < MVT::getSizeInBits(*I)) { |
| 3522 | ThisVT = *I; |
| 3523 | break; |
| 3524 | } |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3525 | } |
| 3526 | } |
| 3527 | |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3528 | if (ThisVT == MVT::Other) continue; |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3529 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3530 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 3531 | // frame pointer in functions that need it (due to them not being taken |
| 3532 | // out of allocation, because a variable sized allocation hasn't been seen |
| 3533 | // yet). This is a slight code pessimization, but should still work. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 3534 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 3535 | E = RC->allocation_order_end(MF); I != E; ++I) |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3536 | if (*I == Reg) { |
| 3537 | // We found a matching register class. Keep looking at others in case |
| 3538 | // we find one with larger registers that this physreg is also in. |
| 3539 | FoundRC = RC; |
| 3540 | FoundVT = ThisVT; |
| 3541 | break; |
| 3542 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 3543 | } |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 3544 | return FoundRC; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 3547 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3548 | namespace { |
| 3549 | /// AsmOperandInfo - This contains information for each constraint that we are |
| 3550 | /// lowering. |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3551 | struct SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { |
| 3552 | /// CallOperand - If this is the result output operand or a clobber |
| 3553 | /// this is null, otherwise it is the incoming operand to the CallInst. |
| 3554 | /// This gets modified as the asm is processed. |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3555 | SDOperand CallOperand; |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3556 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3557 | /// AssignedRegs - If this is a register or register class operand, this |
| 3558 | /// contains the set of register corresponding to the operand. |
| 3559 | RegsForValue AssignedRegs; |
| 3560 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3561 | explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info) |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3562 | : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) { |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3563 | } |
Chris Lattner | 3ff90dc | 2007-04-30 17:16:27 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3565 | /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers |
| 3566 | /// busy in OutputRegs/InputRegs. |
| 3567 | void MarkAllocatedRegs(bool isOutReg, bool isInReg, |
| 3568 | std::set<unsigned> &OutputRegs, |
Chris Lattner | 7cbeb24 | 2008-02-21 04:55:52 +0000 | [diff] [blame] | 3569 | std::set<unsigned> &InputRegs, |
| 3570 | const TargetRegisterInfo &TRI) const { |
| 3571 | if (isOutReg) { |
| 3572 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 3573 | MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI); |
| 3574 | } |
| 3575 | if (isInReg) { |
| 3576 | for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i) |
| 3577 | MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI); |
| 3578 | } |
| 3579 | } |
| 3580 | |
| 3581 | private: |
| 3582 | /// MarkRegAndAliases - Mark the specified register and all aliases in the |
| 3583 | /// specified set. |
| 3584 | static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs, |
| 3585 | const TargetRegisterInfo &TRI) { |
| 3586 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg"); |
| 3587 | Regs.insert(Reg); |
| 3588 | if (const unsigned *Aliases = TRI.getAliasSet(Reg)) |
| 3589 | for (; *Aliases; ++Aliases) |
| 3590 | Regs.insert(*Aliases); |
| 3591 | } |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3592 | }; |
| 3593 | } // end anon namespace. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3594 | |
Chris Lattner | 3ff90dc | 2007-04-30 17:16:27 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | 0fe71e9 | 2008-02-21 19:43:13 +0000 | [diff] [blame] | 3596 | /// GetRegistersForValue - Assign registers (virtual or physical) for the |
| 3597 | /// specified operand. We prefer to assign virtual registers, to allow the |
| 3598 | /// register allocator handle the assignment process. However, if the asm uses |
| 3599 | /// features that we can't model on machineinstrs, we have SDISel do the |
| 3600 | /// allocation. This produces generally horrible, but correct, code. |
| 3601 | /// |
| 3602 | /// OpInfo describes the operand. |
| 3603 | /// HasEarlyClobber is true if there are any early clobber constraints (=&r) |
| 3604 | /// or any explicitly clobbered registers. |
| 3605 | /// Input and OutputRegs are the set of already allocated physical registers. |
| 3606 | /// |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3607 | void SelectionDAGLowering:: |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3608 | GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, bool HasEarlyClobber, |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3609 | std::set<unsigned> &OutputRegs, |
| 3610 | std::set<unsigned> &InputRegs) { |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3611 | // Compute whether this value requires an input register, an output register, |
| 3612 | // or both. |
| 3613 | bool isOutReg = false; |
| 3614 | bool isInReg = false; |
| 3615 | switch (OpInfo.Type) { |
| 3616 | case InlineAsm::isOutput: |
| 3617 | isOutReg = true; |
| 3618 | |
| 3619 | // If this is an early-clobber output, or if there is an input |
| 3620 | // constraint that matches this, we need to reserve the input register |
| 3621 | // so no other inputs allocate to it. |
| 3622 | isInReg = OpInfo.isEarlyClobber || OpInfo.hasMatchingInput; |
| 3623 | break; |
| 3624 | case InlineAsm::isInput: |
| 3625 | isInReg = true; |
| 3626 | isOutReg = false; |
| 3627 | break; |
| 3628 | case InlineAsm::isClobber: |
| 3629 | isOutReg = true; |
| 3630 | isInReg = true; |
| 3631 | break; |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | b606dba | 2008-04-28 06:44:42 +0000 | [diff] [blame] | 3636 | SmallVector<unsigned, 4> Regs; |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3637 | |
| 3638 | // If this is a constraint for a single physreg, or a constraint for a |
| 3639 | // register class, find it. |
| 3640 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 3641 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 3642 | OpInfo.ConstraintVT); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3643 | |
| 3644 | unsigned NumRegs = 1; |
| 3645 | if (OpInfo.ConstraintVT != MVT::Other) |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 3646 | NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3647 | MVT::ValueType RegVT; |
| 3648 | MVT::ValueType ValueVT = OpInfo.ConstraintVT; |
| 3649 | |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3650 | |
| 3651 | // If this is a constraint for a specific physical register, like {r17}, |
| 3652 | // assign it now. |
| 3653 | if (PhysReg.first) { |
| 3654 | if (OpInfo.ConstraintVT == MVT::Other) |
| 3655 | ValueVT = *PhysReg.second->vt_begin(); |
| 3656 | |
| 3657 | // Get the actual register value type. This is important, because the user |
| 3658 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 3659 | // remember that AX is actually i16 to get the right extension. |
| 3660 | RegVT = *PhysReg.second->vt_begin(); |
| 3661 | |
| 3662 | // This is a explicit reference to a physical register. |
| 3663 | Regs.push_back(PhysReg.first); |
| 3664 | |
| 3665 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 3666 | if (NumRegs != 1) { |
| 3667 | TargetRegisterClass::iterator I = PhysReg.second->begin(); |
| 3668 | TargetRegisterClass::iterator E = PhysReg.second->end(); |
| 3669 | for (; *I != PhysReg.first; ++I) |
| 3670 | assert(I != E && "Didn't find reg!"); |
| 3671 | |
| 3672 | // Already added the first reg. |
| 3673 | --NumRegs; ++I; |
| 3674 | for (; NumRegs; --NumRegs, ++I) { |
| 3675 | assert(I != E && "Ran out of registers to allocate!"); |
| 3676 | Regs.push_back(*I); |
| 3677 | } |
| 3678 | } |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3679 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
Chris Lattner | 7cbeb24 | 2008-02-21 04:55:52 +0000 | [diff] [blame] | 3680 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
| 3681 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3682 | return; |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 3686 | // for this reference. |
| 3687 | std::vector<unsigned> RegClassRegs; |
Chris Lattner | c2c28fc | 2007-06-15 19:11:01 +0000 | [diff] [blame] | 3688 | const TargetRegisterClass *RC = PhysReg.second; |
| 3689 | if (RC) { |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3690 | // If this is an early clobber or tied register, our regalloc doesn't know |
| 3691 | // how to maintain the constraint. If it isn't, go ahead and create vreg |
| 3692 | // and let the regalloc do the right thing. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3693 | if (!OpInfo.hasMatchingInput && !OpInfo.isEarlyClobber && |
| 3694 | // If there is some other early clobber and this is an input register, |
| 3695 | // then we are forced to pre-allocate the input reg so it doesn't |
| 3696 | // conflict with the earlyclobber. |
| 3697 | !(OpInfo.Type == InlineAsm::isInput && HasEarlyClobber)) { |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3698 | RegVT = *PhysReg.second->vt_begin(); |
| 3699 | |
| 3700 | if (OpInfo.ConstraintVT == MVT::Other) |
| 3701 | ValueVT = RegVT; |
| 3702 | |
| 3703 | // Create the appropriate number of virtual registers. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 3704 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3705 | for (; NumRegs; --NumRegs) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 3706 | Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second)); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3707 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3708 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT); |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3709 | return; |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | // Otherwise, we can't allocate it. Let the code below figure out how to |
| 3713 | // maintain these constraints. |
| 3714 | RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end()); |
| 3715 | |
| 3716 | } else { |
| 3717 | // This is a reference to a register class that doesn't directly correspond |
| 3718 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 3719 | // registers from the class. |
| 3720 | RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 3721 | OpInfo.ConstraintVT); |
| 3722 | } |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3723 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 3724 | const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo(); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3725 | unsigned NumAllocated = 0; |
| 3726 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 3727 | unsigned Reg = RegClassRegs[i]; |
| 3728 | // See if this register is available. |
| 3729 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 3730 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 3731 | // Make sure we find consecutive registers. |
| 3732 | NumAllocated = 0; |
| 3733 | continue; |
| 3734 | } |
| 3735 | |
| 3736 | // Check to see if this register is allocatable (i.e. don't give out the |
| 3737 | // stack pointer). |
Chris Lattner | c2c28fc | 2007-06-15 19:11:01 +0000 | [diff] [blame] | 3738 | if (RC == 0) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 3739 | RC = isAllocatableRegister(Reg, MF, TLI, TRI); |
Chris Lattner | c2c28fc | 2007-06-15 19:11:01 +0000 | [diff] [blame] | 3740 | if (!RC) { // Couldn't allocate this register. |
| 3741 | // Reset NumAllocated to make sure we return consecutive registers. |
| 3742 | NumAllocated = 0; |
| 3743 | continue; |
| 3744 | } |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
| 3747 | // Okay, this register is good, we can use it. |
| 3748 | ++NumAllocated; |
| 3749 | |
| 3750 | // If we allocated enough consecutive registers, succeed. |
| 3751 | if (NumAllocated == NumRegs) { |
| 3752 | unsigned RegStart = (i-NumAllocated)+1; |
| 3753 | unsigned RegEnd = i+1; |
| 3754 | // Mark all of the allocated registers used. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3755 | for (unsigned i = RegStart; i != RegEnd; ++i) |
| 3756 | Regs.push_back(RegClassRegs[i]); |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3757 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 3758 | OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(), |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3759 | OpInfo.ConstraintVT); |
Chris Lattner | 7cbeb24 | 2008-02-21 04:55:52 +0000 | [diff] [blame] | 3760 | OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI); |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3761 | return; |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3762 | } |
| 3763 | } |
| 3764 | |
| 3765 | // Otherwise, we couldn't allocate enough registers for this. |
Chris Lattner | bf996f1 | 2007-04-30 17:29:31 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3769 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 3770 | /// |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 3771 | void SelectionDAGLowering::visitInlineAsm(CallSite CS) { |
| 3772 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3773 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3774 | /// ConstraintOperands - Information about all of the constraints. |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3775 | std::vector<SDISelAsmOperandInfo> ConstraintOperands; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3776 | |
| 3777 | SDOperand Chain = getRoot(); |
| 3778 | SDOperand Flag; |
| 3779 | |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 3780 | std::set<unsigned> OutputRegs, InputRegs; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3781 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3782 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 3783 | // ConstraintOperands list. |
| 3784 | std::vector<InlineAsm::ConstraintInfo> |
| 3785 | ConstraintInfos = IA->ParseConstraints(); |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3786 | |
| 3787 | // SawEarlyClobber - Keep track of whether we saw an earlyclobber output |
| 3788 | // constraint. If so, we can't let the register allocator allocate any input |
| 3789 | // registers, because it will not know to avoid the earlyclobbered output reg. |
| 3790 | bool SawEarlyClobber = false; |
| 3791 | |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 3792 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
Chris Lattner | acf8b01 | 2008-04-27 23:44:28 +0000 | [diff] [blame] | 3793 | unsigned ResNo = 0; // ResNo - The result number of the next output. |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3794 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3795 | ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i])); |
| 3796 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3797 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3798 | MVT::ValueType OpVT = MVT::Other; |
| 3799 | |
| 3800 | // Compute the value type for each operand. |
| 3801 | switch (OpInfo.Type) { |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3802 | case InlineAsm::isOutput: |
Chris Lattner | acf8b01 | 2008-04-27 23:44:28 +0000 | [diff] [blame] | 3803 | // Indirect outputs just consume an argument. |
| 3804 | if (OpInfo.isIndirect) { |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 3805 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
Chris Lattner | acf8b01 | 2008-04-27 23:44:28 +0000 | [diff] [blame] | 3806 | break; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3807 | } |
Chris Lattner | acf8b01 | 2008-04-27 23:44:28 +0000 | [diff] [blame] | 3808 | // The return value of the call is this value. As such, there is no |
| 3809 | // corresponding argument. |
| 3810 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 3811 | if (const StructType *STy = dyn_cast<StructType>(CS.getType())) { |
| 3812 | OpVT = TLI.getValueType(STy->getElementType(ResNo)); |
| 3813 | } else { |
| 3814 | assert(ResNo == 0 && "Asm only has one result!"); |
| 3815 | OpVT = TLI.getValueType(CS.getType()); |
| 3816 | } |
| 3817 | ++ResNo; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3818 | break; |
| 3819 | case InlineAsm::isInput: |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 3820 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3821 | break; |
| 3822 | case InlineAsm::isClobber: |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3823 | // Nothing to do. |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3824 | break; |
| 3825 | } |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3826 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3827 | // If this is an input or an indirect output, process the call argument. |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3828 | // BasicBlocks are labels, currently appearing only in asm's. |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3829 | if (OpInfo.CallOperandVal) { |
Chris Lattner | 507ffd2 | 2008-04-27 00:16:18 +0000 | [diff] [blame] | 3830 | if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) |
| 3831 | OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3832 | else { |
| 3833 | OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); |
| 3834 | const Type *OpTy = OpInfo.CallOperandVal->getType(); |
| 3835 | // If this is an indirect operand, the operand is a pointer to the |
| 3836 | // accessed type. |
| 3837 | if (OpInfo.isIndirect) |
| 3838 | OpTy = cast<PointerType>(OpTy)->getElementType(); |
| 3839 | |
| 3840 | // If OpTy is not a first-class value, it may be a struct/union that we |
| 3841 | // can tile with integers. |
| 3842 | if (!OpTy->isFirstClassType() && OpTy->isSized()) { |
| 3843 | unsigned BitSize = TD->getTypeSizeInBits(OpTy); |
| 3844 | switch (BitSize) { |
| 3845 | default: break; |
| 3846 | case 1: |
| 3847 | case 8: |
| 3848 | case 16: |
| 3849 | case 32: |
| 3850 | case 64: |
| 3851 | OpTy = IntegerType::get(BitSize); |
| 3852 | break; |
| 3853 | } |
Chris Lattner | 6995cf6 | 2007-04-29 18:58:03 +0000 | [diff] [blame] | 3854 | } |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 3855 | |
| 3856 | OpVT = TLI.getValueType(OpTy, true); |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | OpInfo.ConstraintVT = OpVT; |
Chris Lattner | 2a600be | 2007-04-28 21:01:43 +0000 | [diff] [blame] | 3861 | |
Chris Lattner | 3ff90dc | 2007-04-30 17:16:27 +0000 | [diff] [blame] | 3862 | // Compute the constraint code and ConstraintType to use. |
Chris Lattner | 5a09690 | 2008-04-27 00:37:18 +0000 | [diff] [blame] | 3863 | TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3864 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3865 | // Keep track of whether we see an earlyclobber. |
| 3866 | SawEarlyClobber |= OpInfo.isEarlyClobber; |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 3867 | |
Chris Lattner | 0fe71e9 | 2008-02-21 19:43:13 +0000 | [diff] [blame] | 3868 | // If we see a clobber of a register, it is an early clobber. |
Chris Lattner | 69e6a8d | 2008-02-21 20:54:31 +0000 | [diff] [blame] | 3869 | if (!SawEarlyClobber && |
| 3870 | OpInfo.Type == InlineAsm::isClobber && |
| 3871 | OpInfo.ConstraintType == TargetLowering::C_Register) { |
| 3872 | // Note that we want to ignore things that we don't trick here, like |
| 3873 | // dirflag, fpsr, flags, etc. |
| 3874 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 3875 | TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode, |
| 3876 | OpInfo.ConstraintVT); |
| 3877 | if (PhysReg.first || PhysReg.second) { |
| 3878 | // This is a register we know of. |
| 3879 | SawEarlyClobber = true; |
| 3880 | } |
| 3881 | } |
Chris Lattner | 0fe71e9 | 2008-02-21 19:43:13 +0000 | [diff] [blame] | 3882 | |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 3883 | // If this is a memory input, and if the operand is not indirect, do what we |
| 3884 | // need to to provide an address for the memory input. |
| 3885 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 3886 | !OpInfo.isIndirect) { |
| 3887 | assert(OpInfo.Type == InlineAsm::isInput && |
| 3888 | "Can only indirectify direct input operands!"); |
| 3889 | |
| 3890 | // Memory operands really want the address of the value. If we don't have |
| 3891 | // an indirect input, put it in the constpool if we can, otherwise spill |
| 3892 | // it to a stack slot. |
| 3893 | |
| 3894 | // If the operand is a float, integer, or vector constant, spill to a |
| 3895 | // constant pool entry to get its address. |
| 3896 | Value *OpVal = OpInfo.CallOperandVal; |
| 3897 | if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || |
| 3898 | isa<ConstantVector>(OpVal)) { |
| 3899 | OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal), |
| 3900 | TLI.getPointerTy()); |
| 3901 | } else { |
| 3902 | // Otherwise, create a stack slot and emit a store to it before the |
| 3903 | // asm. |
| 3904 | const Type *Ty = OpVal->getType(); |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 3905 | uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty); |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 3906 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 3907 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3908 | int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 3909 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 3910 | Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0); |
| 3911 | OpInfo.CallOperand = StackSlot; |
| 3912 | } |
| 3913 | |
| 3914 | // There is no longer a Value* corresponding to this operand. |
| 3915 | OpInfo.CallOperandVal = 0; |
| 3916 | // It is now an indirect operand. |
| 3917 | OpInfo.isIndirect = true; |
| 3918 | } |
| 3919 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3920 | // If this constraint is for a specific register, allocate it before |
| 3921 | // anything else. |
| 3922 | if (OpInfo.ConstraintType == TargetLowering::C_Register) |
| 3923 | GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs); |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3924 | } |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3925 | ConstraintInfos.clear(); |
| 3926 | |
| 3927 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3928 | // Second pass - Loop over all of the operands, assigning virtual or physregs |
| 3929 | // to registerclass operands. |
| 3930 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3931 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3932 | |
| 3933 | // C_Register operands have already been allocated, Other/Memory don't need |
| 3934 | // to be. |
| 3935 | if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass) |
| 3936 | GetRegistersForValue(OpInfo, SawEarlyClobber, OutputRegs, InputRegs); |
| 3937 | } |
| 3938 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3939 | // AsmNodeOperands - The operands for the ISD::INLINEASM node. |
| 3940 | std::vector<SDOperand> AsmNodeOperands; |
| 3941 | AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain |
| 3942 | AsmNodeOperands.push_back( |
| 3943 | DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other)); |
| 3944 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 3946 | // Loop over all of the inputs, copying the operand values into the |
| 3947 | // appropriate registers and processing the output regs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3948 | RegsForValue RetValRegs; |
Chris Lattner | 41f6259 | 2008-04-29 04:29:54 +0000 | [diff] [blame] | 3949 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3950 | // IndirectStoresToEmit - The set of stores to emit after the inline asm node. |
| 3951 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
| 3952 | |
| 3953 | for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) { |
Evan Cheng | 5c80760 | 2008-02-26 02:33:44 +0000 | [diff] [blame] | 3954 | SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i]; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3956 | switch (OpInfo.Type) { |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 3957 | case InlineAsm::isOutput: { |
Chris Lattner | c83994e | 2007-04-28 21:03:16 +0000 | [diff] [blame] | 3958 | if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass && |
| 3959 | OpInfo.ConstraintType != TargetLowering::C_Register) { |
Chris Lattner | f2f3cd5 | 2007-04-28 06:08:13 +0000 | [diff] [blame] | 3960 | // Memory output, or 'other' output (e.g. 'X' constraint). |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 3961 | assert(OpInfo.isIndirect && "Memory output must be indirect operand"); |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 3962 | |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 3963 | // Add information to the INLINEASM node to know about this output. |
| 3964 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
Chris Lattner | c90233b | 2007-05-15 01:33:58 +0000 | [diff] [blame] | 3965 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 3966 | TLI.getPointerTy())); |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 3967 | AsmNodeOperands.push_back(OpInfo.CallOperand); |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 3968 | break; |
| 3969 | } |
| 3970 | |
Chris Lattner | 2a600be | 2007-04-28 21:01:43 +0000 | [diff] [blame] | 3971 | // Otherwise, this is a register or register class output. |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 3972 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3973 | // Copy the output from the appropriate register. Find a register that |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3974 | // we can use. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3975 | if (OpInfo.AssignedRegs.Regs.empty()) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3976 | cerr << "Couldn't allocate output reg for contraint '" |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3977 | << OpInfo.ConstraintCode << "'!\n"; |
Chris Lattner | d03f158 | 2006-10-31 07:33:13 +0000 | [diff] [blame] | 3978 | exit(1); |
| 3979 | } |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 3980 | |
Chris Lattner | 41f6259 | 2008-04-29 04:29:54 +0000 | [diff] [blame] | 3981 | // If this is an indirect operand, store through the pointer after the |
| 3982 | // asm. |
| 3983 | if (OpInfo.isIndirect) { |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3984 | IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs, |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 3985 | OpInfo.CallOperandVal)); |
Chris Lattner | 41f6259 | 2008-04-29 04:29:54 +0000 | [diff] [blame] | 3986 | } else { |
| 3987 | // This is the result value of the call. |
| 3988 | assert(CS.getType() != Type::VoidTy && "Bad inline asm!"); |
| 3989 | // Concatenate this output onto the outputs list. |
| 3990 | RetValRegs.append(OpInfo.AssignedRegs); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 3991 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3992 | |
| 3993 | // Add information to the INLINEASM node to know that this register is |
| 3994 | // set. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 3995 | OpInfo.AssignedRegs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, |
| 3996 | AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3997 | break; |
| 3998 | } |
| 3999 | case InlineAsm::isInput: { |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4000 | SDOperand InOperandVal = OpInfo.CallOperand; |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 4001 | |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4002 | if (isdigit(OpInfo.ConstraintCode[0])) { // Matching constraint? |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 4003 | // If this is required to match an output register we have already set, |
| 4004 | // just use its register. |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4005 | unsigned OperandNo = atoi(OpInfo.ConstraintCode.c_str()); |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 4006 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4007 | // Scan until we find the definition we already emitted of this operand. |
| 4008 | // When we find it, create a RegsForValue operand. |
| 4009 | unsigned CurOp = 2; // The first operand. |
| 4010 | for (; OperandNo; --OperandNo) { |
| 4011 | // Advance to the next operand. |
| 4012 | unsigned NumOps = |
| 4013 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
Chris Lattner | a15cf70 | 2006-07-20 19:02:21 +0000 | [diff] [blame] | 4014 | assert(((NumOps & 7) == 2 /*REGDEF*/ || |
| 4015 | (NumOps & 7) == 4 /*MEM*/) && |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4016 | "Skipped past definitions?"); |
| 4017 | CurOp += (NumOps>>3)+1; |
| 4018 | } |
| 4019 | |
| 4020 | unsigned NumOps = |
| 4021 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 4022 | if ((NumOps & 7) == 2 /*REGDEF*/) { |
| 4023 | // Add NumOps>>3 registers to MatchedRegs. |
| 4024 | RegsForValue MatchedRegs; |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4025 | MatchedRegs.TLI = &TLI; |
Dan Gohman | 1fa850b | 2008-05-02 00:03:54 +0000 | [diff] [blame^] | 4026 | MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType()); |
| 4027 | MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType()); |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 4028 | for (unsigned i = 0, e = NumOps>>3; i != e; ++i) { |
| 4029 | unsigned Reg = |
| 4030 | cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg(); |
| 4031 | MatchedRegs.Regs.push_back(Reg); |
| 4032 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4033 | |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 4034 | // Use the produced MatchedRegs object to |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4035 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag); |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 4036 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands); |
| 4037 | break; |
| 4038 | } else { |
| 4039 | assert((NumOps & 7) == 4/*MEM*/ && "Unknown matching constraint!"); |
Chris Lattner | f9853bc | 2008-02-21 05:27:19 +0000 | [diff] [blame] | 4040 | assert((NumOps >> 3) == 1 && "Unexpected number of operands"); |
| 4041 | // Add information to the INLINEASM node to know about this input. |
| 4042 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 4043 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 4044 | TLI.getPointerTy())); |
| 4045 | AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); |
| 4046 | break; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4047 | } |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 4048 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4049 | |
Chris Lattner | 2a600be | 2007-04-28 21:01:43 +0000 | [diff] [blame] | 4050 | if (OpInfo.ConstraintType == TargetLowering::C_Other) { |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4051 | assert(!OpInfo.isIndirect && |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 4052 | "Don't know how to handle indirect other inputs yet!"); |
| 4053 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 4054 | std::vector<SDOperand> Ops; |
| 4055 | TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0], |
| 4056 | Ops, DAG); |
| 4057 | if (Ops.empty()) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4058 | cerr << "Invalid operand for inline asm constraint '" |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4059 | << OpInfo.ConstraintCode << "'!\n"; |
Chris Lattner | 53069fb | 2006-10-31 19:41:18 +0000 | [diff] [blame] | 4060 | exit(1); |
| 4061 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4062 | |
| 4063 | // Add information to the INLINEASM node to know about this input. |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 4064 | unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3); |
Chris Lattner | c90233b | 2007-05-15 01:33:58 +0000 | [diff] [blame] | 4065 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 4066 | TLI.getPointerTy())); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 4067 | AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end()); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4068 | break; |
Chris Lattner | 2a600be | 2007-04-28 21:01:43 +0000 | [diff] [blame] | 4069 | } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) { |
Chris Lattner | 09e4b7e | 2007-04-28 21:12:06 +0000 | [diff] [blame] | 4070 | assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!"); |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 4071 | assert(InOperandVal.getValueType() == TLI.getPointerTy() && |
| 4072 | "Memory operands expect pointer values"); |
| 4073 | |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4074 | // Add information to the INLINEASM node to know about this input. |
| 4075 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
Chris Lattner | c90233b | 2007-05-15 01:33:58 +0000 | [diff] [blame] | 4076 | AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, |
| 4077 | TLI.getPointerTy())); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4078 | AsmNodeOperands.push_back(InOperandVal); |
| 4079 | break; |
| 4080 | } |
| 4081 | |
Chris Lattner | 2a600be | 2007-04-28 21:01:43 +0000 | [diff] [blame] | 4082 | assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || |
| 4083 | OpInfo.ConstraintType == TargetLowering::C_Register) && |
| 4084 | "Unknown constraint type!"); |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4085 | assert(!OpInfo.isIndirect && |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 4086 | "Don't know how to handle indirect register inputs yet!"); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Copy the input into the appropriate registers. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 4089 | assert(!OpInfo.AssignedRegs.Regs.empty() && |
| 4090 | "Couldn't allocate input reg!"); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4091 | |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4092 | OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 4094 | OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, |
| 4095 | AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4096 | break; |
| 4097 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4098 | case InlineAsm::isClobber: { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4099 | // Add the clobbered value to the operand list, so that the register |
| 4100 | // allocator is aware that the physreg got clobbered. |
Chris Lattner | e7cf56a | 2007-04-30 21:11:17 +0000 | [diff] [blame] | 4101 | if (!OpInfo.AssignedRegs.Regs.empty()) |
| 4102 | OpInfo.AssignedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, |
| 4103 | AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4104 | break; |
| 4105 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 4106 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4107 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 4108 | |
| 4109 | // Finish up input operands. |
| 4110 | AsmNodeOperands[0] = Chain; |
| 4111 | if (Flag.Val) AsmNodeOperands.push_back(Flag); |
| 4112 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 4113 | Chain = DAG.getNode(ISD::INLINEASM, |
| 4114 | DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4115 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 4116 | Flag = Chain.getValue(1); |
| 4117 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4118 | // If this asm returns a register value, copy the result from that register |
| 4119 | // and set it as the value of the call. |
Chris Lattner | 3a508c9 | 2007-04-12 06:00:20 +0000 | [diff] [blame] | 4120 | if (!RetValRegs.Regs.empty()) { |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4121 | SDOperand Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag); |
Chris Lattner | 3fb2968 | 2008-04-29 04:48:56 +0000 | [diff] [blame] | 4122 | |
| 4123 | // If any of the results of the inline asm is a vector, it may have the |
| 4124 | // wrong width/num elts. This can happen for register classes that can |
| 4125 | // contain multiple different value types. The preg or vreg allocated may |
| 4126 | // not have the same VT as was expected. Convert it to the right type with |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4127 | // bit_convert. |
Chris Lattner | 3fb2968 | 2008-04-29 04:48:56 +0000 | [diff] [blame] | 4128 | if (const StructType *ResSTy = dyn_cast<StructType>(CS.getType())) { |
| 4129 | for (unsigned i = 0, e = ResSTy->getNumElements(); i != e; ++i) { |
| 4130 | if (MVT::isVector(Val.Val->getValueType(i))) |
| 4131 | Val = DAG.getNode(ISD::BIT_CONVERT, |
| 4132 | TLI.getValueType(ResSTy->getElementType(i)), Val); |
| 4133 | } |
| 4134 | } else { |
| 4135 | if (MVT::isVector(Val.getValueType())) |
| 4136 | Val = DAG.getNode(ISD::BIT_CONVERT, TLI.getValueType(CS.getType()), |
| 4137 | Val); |
Chris Lattner | 3a508c9 | 2007-04-12 06:00:20 +0000 | [diff] [blame] | 4138 | } |
Chris Lattner | 3fb2968 | 2008-04-29 04:48:56 +0000 | [diff] [blame] | 4139 | |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 4140 | setValue(CS.getInstruction(), Val); |
Chris Lattner | 3a508c9 | 2007-04-12 06:00:20 +0000 | [diff] [blame] | 4141 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 4142 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4143 | std::vector<std::pair<SDOperand, Value*> > StoresToEmit; |
| 4144 | |
| 4145 | // Process indirect outputs, first output all of the flagged copies out of |
| 4146 | // physregs. |
| 4147 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 4148 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4149 | Value *Ptr = IndirectStoresToEmit[i].second; |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4150 | SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 4151 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | // Emit the non-flagged stores from the physregs. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4155 | SmallVector<SDOperand, 8> OutChains; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4156 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Chris Lattner | 0c58340 | 2007-04-28 20:49:53 +0000 | [diff] [blame] | 4157 | OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first, |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4158 | getValue(StoresToEmit[i].second), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4159 | StoresToEmit[i].second, 0)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 4160 | if (!OutChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4161 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4162 | &OutChains[0], OutChains.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 4163 | DAG.setRoot(Chain); |
| 4164 | } |
| 4165 | |
| 4166 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4167 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 4168 | SDOperand Src = getValue(I.getOperand(0)); |
| 4169 | |
| 4170 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 4171 | |
| 4172 | if (IntPtr < Src.getValueType()) |
| 4173 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 4174 | else if (IntPtr > Src.getValueType()) |
| 4175 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4176 | |
| 4177 | // Scale the source by the type size. |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 4178 | uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4179 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4180 | Src, DAG.getIntPtrConstant(ElementSize)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4181 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4182 | TargetLowering::ArgListTy Args; |
| 4183 | TargetLowering::ArgListEntry Entry; |
| 4184 | Entry.Node = Src; |
| 4185 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4186 | Args.push_back(Entry); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 4187 | |
| 4188 | std::pair<SDOperand,SDOperand> Result = |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 4189 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, CallingConv::C, |
| 4190 | true, DAG.getExternalSymbol("malloc", IntPtr), Args, DAG); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 4191 | setValue(&I, Result.first); // Pointers always fit in registers |
| 4192 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4196 | TargetLowering::ArgListTy Args; |
| 4197 | TargetLowering::ArgListEntry Entry; |
| 4198 | Entry.Node = getValue(I.getOperand(0)); |
| 4199 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4200 | Args.push_back(Entry); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4201 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 4202 | std::pair<SDOperand,SDOperand> Result = |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 4203 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, |
| 4204 | CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 4205 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 4206 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 4209 | // EmitInstrWithCustomInserter - This method should be implemented by targets |
| 4210 | // that mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 4211 | // instructions are special in various ways, which require special support to |
| 4212 | // insert. The specified MachineInstr is created but not inserted into any |
| 4213 | // basic blocks, and the scheduler passes ownership of it to this method. |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 4214 | MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 4215 | MachineBasicBlock *MBB) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4216 | cerr << "If a target marks an instruction with " |
| 4217 | << "'usesCustomDAGSchedInserter', it must implement " |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 4218 | << "TargetLowering::EmitInstrWithCustomInserter!\n"; |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 4219 | abort(); |
| 4220 | return 0; |
| 4221 | } |
| 4222 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 4223 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4224 | DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), |
| 4225 | getValue(I.getOperand(1)), |
| 4226 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4230 | SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(), |
| 4231 | getValue(I.getOperand(0)), |
| 4232 | DAG.getSrcValue(I.getOperand(0))); |
| 4233 | setValue(&I, V); |
| 4234 | DAG.setRoot(V.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4238 | DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(), |
| 4239 | getValue(I.getOperand(1)), |
| 4240 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4244 | DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), |
| 4245 | getValue(I.getOperand(1)), |
| 4246 | getValue(I.getOperand(2)), |
| 4247 | DAG.getSrcValue(I.getOperand(1)), |
| 4248 | DAG.getSrcValue(I.getOperand(2)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4251 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 4252 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4253 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
| 4254 | /// integrated into SDISel. |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4255 | std::vector<SDOperand> |
| 4256 | TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 4257 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 4258 | std::vector<SDOperand> Ops; |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 4259 | Ops.push_back(DAG.getRoot()); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4260 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 4261 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 4262 | |
| 4263 | // Add one result value for each formal argument. |
| 4264 | std::vector<MVT::ValueType> RetVals; |
Anton Korobeynikov | 6aa279d | 2007-01-28 18:01:49 +0000 | [diff] [blame] | 4265 | unsigned j = 1; |
Anton Korobeynikov | ac2b2cf | 2007-01-28 16:04:40 +0000 | [diff] [blame] | 4266 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 4267 | I != E; ++I, ++j) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4268 | MVT::ValueType VT = getValueType(I->getType()); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4269 | ISD::ArgFlagsTy Flags; |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 4270 | unsigned OriginalAlignment = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4271 | getTargetData()->getABITypeAlignment(I->getType()); |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 4272 | |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4273 | if (F.paramHasAttr(j, ParamAttr::ZExt)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4274 | Flags.setZExt(); |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4275 | if (F.paramHasAttr(j, ParamAttr::SExt)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4276 | Flags.setSExt(); |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4277 | if (F.paramHasAttr(j, ParamAttr::InReg)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4278 | Flags.setInReg(); |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4279 | if (F.paramHasAttr(j, ParamAttr::StructRet)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4280 | Flags.setSRet(); |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4281 | if (F.paramHasAttr(j, ParamAttr::ByVal)) { |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4282 | Flags.setByVal(); |
Rafael Espindola | 594d37e | 2007-08-10 14:44:42 +0000 | [diff] [blame] | 4283 | const PointerType *Ty = cast<PointerType>(I->getType()); |
Duncan Sands | a41d719 | 2008-01-13 21:19:59 +0000 | [diff] [blame] | 4284 | const Type *ElementTy = Ty->getElementType(); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4285 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | a41d719 | 2008-01-13 21:19:59 +0000 | [diff] [blame] | 4286 | unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy); |
Dale Johannesen | 08e78b1 | 2008-02-22 17:49:45 +0000 | [diff] [blame] | 4287 | // For ByVal, alignment should be passed from FE. BE will guess if |
| 4288 | // this info is not there but there are cases it cannot get right. |
| 4289 | if (F.getParamAlignment(j)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4290 | FrameAlign = F.getParamAlignment(j); |
| 4291 | Flags.setByValAlign(FrameAlign); |
| 4292 | Flags.setByValSize(FrameSize); |
Rafael Espindola | 594d37e | 2007-08-10 14:44:42 +0000 | [diff] [blame] | 4293 | } |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 4294 | if (F.paramHasAttr(j, ParamAttr::Nest)) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4295 | Flags.setNest(); |
| 4296 | Flags.setOrigAlign(OriginalAlignment); |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 4297 | |
| 4298 | MVT::ValueType RegisterVT = getRegisterType(VT); |
| 4299 | unsigned NumRegs = getNumRegisters(VT); |
| 4300 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 4301 | RetVals.push_back(RegisterVT); |
Nicolas Geoffray | 9701c8a | 2008-04-14 17:17:14 +0000 | [diff] [blame] | 4302 | ISD::ArgFlagsTy MyFlags = Flags; |
Nicolas Geoffray | c0cb28f | 2008-04-13 13:40:22 +0000 | [diff] [blame] | 4303 | if (NumRegs > 1 && i == 0) |
Nicolas Geoffray | 6ccbbd8 | 2008-04-15 08:08:50 +0000 | [diff] [blame] | 4304 | MyFlags.setSplit(); |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 4305 | // if it isn't first piece, alignment must be 1 |
Nicolas Geoffray | c0cb28f | 2008-04-13 13:40:22 +0000 | [diff] [blame] | 4306 | else if (i > 0) |
Nicolas Geoffray | 9701c8a | 2008-04-14 17:17:14 +0000 | [diff] [blame] | 4307 | MyFlags.setOrigAlign(1); |
| 4308 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4309 | } |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4310 | } |
Evan Cheng | 3b0d286 | 2006-04-25 23:03:35 +0000 | [diff] [blame] | 4311 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 4312 | RetVals.push_back(MVT::Other); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4313 | |
| 4314 | // Create the node. |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 4315 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, |
Chris Lattner | 86ca3ca | 2008-02-13 07:39:09 +0000 | [diff] [blame] | 4316 | DAG.getVTList(&RetVals[0], RetVals.size()), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4317 | &Ops[0], Ops.size()).Val; |
Chris Lattner | 86ca3ca | 2008-02-13 07:39:09 +0000 | [diff] [blame] | 4318 | |
| 4319 | // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but |
| 4320 | // allows exposing the loads that may be part of the argument access to the |
| 4321 | // first DAGCombiner pass. |
| 4322 | SDOperand TmpRes = LowerOperation(SDOperand(Result, 0), DAG); |
| 4323 | |
| 4324 | // The number of results should match up, except that the lowered one may have |
| 4325 | // an extra flag result. |
| 4326 | assert((Result->getNumValues() == TmpRes.Val->getNumValues() || |
| 4327 | (Result->getNumValues()+1 == TmpRes.Val->getNumValues() && |
| 4328 | TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag)) |
| 4329 | && "Lowering produced unexpected number of results!"); |
| 4330 | Result = TmpRes.Val; |
| 4331 | |
Dan Gohman | 27a70be | 2007-07-02 16:18:06 +0000 | [diff] [blame] | 4332 | unsigned NumArgRegs = Result->getNumValues() - 1; |
| 4333 | DAG.setRoot(SDOperand(Result, NumArgRegs)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4334 | |
| 4335 | // Set up the return result vector. |
| 4336 | Ops.clear(); |
| 4337 | unsigned i = 0; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4338 | unsigned Idx = 1; |
| 4339 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 4340 | ++I, ++Idx) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4341 | MVT::ValueType VT = getValueType(I->getType()); |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 4342 | MVT::ValueType PartVT = getRegisterType(VT); |
| 4343 | |
| 4344 | unsigned NumParts = getNumRegisters(VT); |
| 4345 | SmallVector<SDOperand, 4> Parts(NumParts); |
| 4346 | for (unsigned j = 0; j != NumParts; ++j) |
| 4347 | Parts[j] = SDOperand(Result, i++); |
| 4348 | |
| 4349 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4350 | if (F.paramHasAttr(Idx, ParamAttr::SExt)) |
| 4351 | AssertOp = ISD::AssertSext; |
| 4352 | else if (F.paramHasAttr(Idx, ParamAttr::ZExt)) |
| 4353 | AssertOp = ISD::AssertZext; |
| 4354 | |
| 4355 | Ops.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT, |
Chris Lattner | 4468c1f | 2008-03-09 09:38:46 +0000 | [diff] [blame] | 4356 | AssertOp)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4357 | } |
Dan Gohman | 27a70be | 2007-07-02 16:18:06 +0000 | [diff] [blame] | 4358 | assert(i == NumArgRegs && "Argument register count mismatch!"); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4359 | return Ops; |
| 4360 | } |
| 4361 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4362 | |
| 4363 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 4364 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 4365 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 4366 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 4367 | std::pair<SDOperand, SDOperand> |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 4368 | TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 4369 | bool RetSExt, bool RetZExt, bool isVarArg, |
| 4370 | unsigned CallingConv, bool isTailCall, |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4371 | SDOperand Callee, |
| 4372 | ArgListTy &Args, SelectionDAG &DAG) { |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 4373 | SmallVector<SDOperand, 32> Ops; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4374 | Ops.push_back(Chain); // Op#0 - Chain |
| 4375 | Ops.push_back(DAG.getConstant(CallingConv, getPointerTy())); // Op#1 - CC |
| 4376 | Ops.push_back(DAG.getConstant(isVarArg, getPointerTy())); // Op#2 - VarArg |
| 4377 | Ops.push_back(DAG.getConstant(isTailCall, getPointerTy())); // Op#3 - Tail |
| 4378 | Ops.push_back(Callee); |
| 4379 | |
| 4380 | // Handle all of the outgoing arguments. |
| 4381 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4382 | MVT::ValueType VT = getValueType(Args[i].Ty); |
| 4383 | SDOperand Op = Args[i].Node; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4384 | ISD::ArgFlagsTy Flags; |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 4385 | unsigned OriginalAlignment = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4386 | getTargetData()->getABITypeAlignment(Args[i].Ty); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4387 | |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 4388 | if (Args[i].isZExt) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4389 | Flags.setZExt(); |
| 4390 | if (Args[i].isSExt) |
| 4391 | Flags.setSExt(); |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 4392 | if (Args[i].isInReg) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4393 | Flags.setInReg(); |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 4394 | if (Args[i].isSRet) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4395 | Flags.setSRet(); |
Rafael Espindola | 21485be | 2007-08-20 15:18:24 +0000 | [diff] [blame] | 4396 | if (Args[i].isByVal) { |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4397 | Flags.setByVal(); |
Rafael Espindola | 21485be | 2007-08-20 15:18:24 +0000 | [diff] [blame] | 4398 | const PointerType *Ty = cast<PointerType>(Args[i].Ty); |
Duncan Sands | a41d719 | 2008-01-13 21:19:59 +0000 | [diff] [blame] | 4399 | const Type *ElementTy = Ty->getElementType(); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4400 | unsigned FrameAlign = getByValTypeAlignment(ElementTy); |
Duncan Sands | a41d719 | 2008-01-13 21:19:59 +0000 | [diff] [blame] | 4401 | unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy); |
Dale Johannesen | 08e78b1 | 2008-02-22 17:49:45 +0000 | [diff] [blame] | 4402 | // For ByVal, alignment should come from FE. BE will guess if this |
| 4403 | // info is not there but there are cases it cannot get right. |
| 4404 | if (Args[i].Alignment) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4405 | FrameAlign = Args[i].Alignment; |
| 4406 | Flags.setByValAlign(FrameAlign); |
| 4407 | Flags.setByValSize(FrameSize); |
Rafael Espindola | 21485be | 2007-08-20 15:18:24 +0000 | [diff] [blame] | 4408 | } |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 4409 | if (Args[i].isNest) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4410 | Flags.setNest(); |
| 4411 | Flags.setOrigAlign(OriginalAlignment); |
Dan Gohman | 27a70be | 2007-07-02 16:18:06 +0000 | [diff] [blame] | 4412 | |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 4413 | MVT::ValueType PartVT = getRegisterType(VT); |
| 4414 | unsigned NumParts = getNumRegisters(VT); |
| 4415 | SmallVector<SDOperand, 4> Parts(NumParts); |
| 4416 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 4417 | |
| 4418 | if (Args[i].isSExt) |
| 4419 | ExtendKind = ISD::SIGN_EXTEND; |
| 4420 | else if (Args[i].isZExt) |
| 4421 | ExtendKind = ISD::ZERO_EXTEND; |
| 4422 | |
| 4423 | getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind); |
| 4424 | |
| 4425 | for (unsigned i = 0; i != NumParts; ++i) { |
| 4426 | // if it isn't first piece, alignment must be 1 |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4427 | ISD::ArgFlagsTy MyFlags = Flags; |
Nicolas Geoffray | c0cb28f | 2008-04-13 13:40:22 +0000 | [diff] [blame] | 4428 | if (NumParts > 1 && i == 0) |
Nicolas Geoffray | 6ccbbd8 | 2008-04-15 08:08:50 +0000 | [diff] [blame] | 4429 | MyFlags.setSplit(); |
Nicolas Geoffray | c0cb28f | 2008-04-13 13:40:22 +0000 | [diff] [blame] | 4430 | else if (i != 0) |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4431 | MyFlags.setOrigAlign(1); |
Duncan Sands | b988bac | 2008-02-11 20:58:28 +0000 | [diff] [blame] | 4432 | |
| 4433 | Ops.push_back(Parts[i]); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4434 | Ops.push_back(DAG.getArgFlags(MyFlags)); |
Dan Gohman | 27a70be | 2007-07-02 16:18:06 +0000 | [diff] [blame] | 4435 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4438 | // Figure out the result value types. We start by making a list of |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4439 | // the potentially illegal return value types. |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4440 | SmallVector<MVT::ValueType, 4> LoweredRetTys; |
| 4441 | SmallVector<MVT::ValueType, 4> RetTys; |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4442 | ComputeValueVTs(*this, RetTy, RetTys); |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4443 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4444 | // Then we translate that to a list of legal types. |
| 4445 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4446 | MVT::ValueType VT = RetTys[I]; |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4447 | MVT::ValueType RegisterVT = getRegisterType(VT); |
| 4448 | unsigned NumRegs = getNumRegisters(VT); |
| 4449 | for (unsigned i = 0; i != NumRegs; ++i) |
| 4450 | LoweredRetTys.push_back(RegisterVT); |
| 4451 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4452 | |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4453 | LoweredRetTys.push_back(MVT::Other); // Always has a chain. |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4454 | |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4455 | // Create the CALL node. |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 4456 | SDOperand Res = DAG.getNode(ISD::CALL, |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4457 | DAG.getVTList(&LoweredRetTys[0], |
| 4458 | LoweredRetTys.size()), |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 4459 | &Ops[0], Ops.size()); |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4460 | Chain = Res.getValue(LoweredRetTys.size() - 1); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4461 | |
| 4462 | // Gather up the call result into a single value. |
| 4463 | if (RetTy != Type::VoidTy) { |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 4464 | ISD::NodeType AssertOp = ISD::DELETED_NODE; |
| 4465 | |
| 4466 | if (RetSExt) |
| 4467 | AssertOp = ISD::AssertSext; |
| 4468 | else if (RetZExt) |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4469 | AssertOp = ISD::AssertZext; |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 4470 | |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4471 | SmallVector<SDOperand, 4> ReturnValues; |
| 4472 | unsigned RegNo = 0; |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4473 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 4474 | MVT::ValueType VT = RetTys[I]; |
Dan Gohman | ef5d194 | 2008-03-11 21:11:25 +0000 | [diff] [blame] | 4475 | MVT::ValueType RegisterVT = getRegisterType(VT); |
| 4476 | unsigned NumRegs = getNumRegisters(VT); |
| 4477 | unsigned RegNoEnd = NumRegs + RegNo; |
| 4478 | SmallVector<SDOperand, 4> Results; |
| 4479 | for (; RegNo != RegNoEnd; ++RegNo) |
| 4480 | Results.push_back(Res.getValue(RegNo)); |
| 4481 | SDOperand ReturnValue = |
| 4482 | getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT, |
| 4483 | AssertOp); |
| 4484 | ReturnValues.push_back(ReturnValue); |
| 4485 | } |
| 4486 | Res = ReturnValues.size() == 1 ? ReturnValues.front() : |
| 4487 | DAG.getNode(ISD::MERGE_VALUES, |
| 4488 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 4489 | &ReturnValues[0], ReturnValues.size()); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4490 | } |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4491 | |
| 4492 | return std::make_pair(Res, Chain); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 4495 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 4496 | assert(0 && "LowerOperation not implemented for this target!"); |
| 4497 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 4498 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4501 | SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op, |
| 4502 | SelectionDAG &DAG) { |
| 4503 | assert(0 && "CustomPromoteOperation not implemented for this target!"); |
| 4504 | abort(); |
| 4505 | return SDOperand(); |
| 4506 | } |
| 4507 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 4508 | //===----------------------------------------------------------------------===// |
| 4509 | // SelectionDAGISel code |
| 4510 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4511 | |
| 4512 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 4513 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 4516 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4517 | AU.addRequired<AliasAnalysis>(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 4518 | AU.addRequired<CollectorModuleMetadata>(); |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 4519 | AU.setPreservesAll(); |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 4520 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4521 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4522 | |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 4523 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4524 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 4525 | // Get alias analysis for load/store combining. |
| 4526 | AA = &getAnalysis<AliasAnalysis>(); |
| 4527 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4528 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 4529 | if (MF.getFunction()->hasCollector()) |
| 4530 | GCI = &getAnalysis<CollectorModuleMetadata>().get(*MF.getFunction()); |
| 4531 | else |
| 4532 | GCI = 0; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 4533 | RegInfo = &MF.getRegInfo(); |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4534 | DOUT << "\n\n\n=== " << Fn.getName() << "\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4535 | |
| 4536 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 4537 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 4538 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 4539 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator())) |
| 4540 | // Mark landing pad. |
| 4541 | FuncInfo.MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 4542 | |
| 4543 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4544 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4545 | |
Evan Cheng | ad2070c | 2007-02-10 02:43:39 +0000 | [diff] [blame] | 4546 | // Add function live-ins to entry block live-in set. |
| 4547 | BasicBlock *EntryBB = &Fn.getEntryBlock(); |
| 4548 | BB = FuncInfo.MBBMap[EntryBB]; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 4549 | if (!RegInfo->livein_empty()) |
| 4550 | for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(), |
| 4551 | E = RegInfo->livein_end(); I != E; ++I) |
Evan Cheng | ad2070c | 2007-02-10 02:43:39 +0000 | [diff] [blame] | 4552 | BB->addLiveIn(I->first); |
| 4553 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4554 | #ifndef NDEBUG |
| 4555 | assert(FuncInfo.CatchInfoFound.size() == FuncInfo.CatchInfoLost.size() && |
| 4556 | "Not all catch info was assigned to a landing pad!"); |
| 4557 | #endif |
| 4558 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4559 | return true; |
| 4560 | } |
| 4561 | |
Chris Lattner | 6833b06 | 2008-04-28 07:16:35 +0000 | [diff] [blame] | 4562 | void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4563 | SDOperand Op = getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 4564 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4565 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 4566 | "Copy from a reg to the same reg!"); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4567 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg"); |
Dan Gohman | b6f5b00 | 2007-06-28 23:29:44 +0000 | [diff] [blame] | 4568 | |
Dan Gohman | 23ce502 | 2008-04-25 18:27:55 +0000 | [diff] [blame] | 4569 | RegsForValue RFV(TLI, Reg, V->getType()); |
| 4570 | SDOperand Chain = DAG.getEntryNode(); |
| 4571 | RFV.getCopyToRegs(Op, DAG, Chain, 0); |
| 4572 | PendingExports.push_back(Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4575 | void SelectionDAGISel:: |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4576 | LowerArguments(BasicBlock *LLVMBB, SelectionDAGLowering &SDL) { |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4577 | // If this is the entry block, emit arguments. |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4578 | Function &F = *LLVMBB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 4579 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4580 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 4581 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4583 | unsigned a = 0; |
| 4584 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 4585 | AI != E; ++AI, ++a) |
| 4586 | if (!AI->use_empty()) { |
| 4587 | SDL.setValue(AI, Args[a]); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 4588 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4589 | // If this argument is live outside of the entry block, insert a copy from |
| 4590 | // whereever we got it to the vreg that other BB's will reference it as. |
Chris Lattner | 251db18 | 2007-02-25 18:40:32 +0000 | [diff] [blame] | 4591 | DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo.ValueMap.find(AI); |
| 4592 | if (VMI != FuncInfo.ValueMap.end()) { |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4593 | SDL.CopyValueToVirtualRegister(AI, VMI->second); |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4594 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 4595 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4596 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4597 | // Finally, if the target has anything special to do, allow it to do so. |
Chris Lattner | 9664541 | 2006-05-16 06:10:58 +0000 | [diff] [blame] | 4598 | // FIXME: this should insert code into the DAG! |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4599 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4602 | static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB, |
| 4603 | MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4604 | for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I) |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 4605 | if (isSelector(I)) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4606 | // Apply the catch info to DestBB. |
| 4607 | addCatchInfo(cast<CallInst>(*I), MMI, FLI.MBBMap[DestBB]); |
| 4608 | #ifndef NDEBUG |
Duncan Sands | 560a737 | 2007-11-15 09:54:37 +0000 | [diff] [blame] | 4609 | if (!FLI.MBBMap[SrcBB]->isLandingPad()) |
| 4610 | FLI.CatchInfoFound.insert(I); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4611 | #endif |
| 4612 | } |
| 4613 | } |
| 4614 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4615 | /// IsFixedFrameObjectWithPosOffset - Check if object is a fixed frame object and |
| 4616 | /// whether object offset >= 0. |
| 4617 | static bool |
| 4618 | IsFixedFrameObjectWithPosOffset(MachineFrameInfo * MFI, SDOperand Op) { |
| 4619 | if (!isa<FrameIndexSDNode>(Op)) return false; |
| 4620 | |
| 4621 | FrameIndexSDNode * FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op); |
| 4622 | int FrameIdx = FrameIdxNode->getIndex(); |
| 4623 | return MFI->isFixedObjectIndex(FrameIdx) && |
| 4624 | MFI->getObjectOffset(FrameIdx) >= 0; |
| 4625 | } |
| 4626 | |
| 4627 | /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could |
| 4628 | /// possibly be overwritten when lowering the outgoing arguments in a tail |
| 4629 | /// call. Currently the implementation of this call is very conservative and |
| 4630 | /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with |
| 4631 | /// virtual registers would be overwritten by direct lowering. |
| 4632 | static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op, |
| 4633 | MachineFrameInfo * MFI) { |
| 4634 | RegisterSDNode * OpReg = NULL; |
| 4635 | if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS || |
| 4636 | (Op.getOpcode()== ISD::CopyFromReg && |
| 4637 | (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) && |
| 4638 | (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) || |
| 4639 | (Op.getOpcode() == ISD::LOAD && |
| 4640 | IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(1))) || |
| 4641 | (Op.getOpcode() == ISD::MERGE_VALUES && |
| 4642 | Op.getOperand(Op.ResNo).getOpcode() == ISD::LOAD && |
| 4643 | IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(Op.ResNo). |
| 4644 | getOperand(1)))) |
| 4645 | return true; |
| 4646 | return false; |
| 4647 | } |
| 4648 | |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4649 | /// CheckDAGForTailCallsAndFixThem - This Function looks for CALL nodes in the |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 4650 | /// DAG and fixes their tailcall attribute operand. |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4651 | static void CheckDAGForTailCallsAndFixThem(SelectionDAG &DAG, |
| 4652 | TargetLowering& TLI) { |
| 4653 | SDNode * Ret = NULL; |
| 4654 | SDOperand Terminator = DAG.getRoot(); |
| 4655 | |
| 4656 | // Find RET node. |
| 4657 | if (Terminator.getOpcode() == ISD::RET) { |
| 4658 | Ret = Terminator.Val; |
| 4659 | } |
| 4660 | |
| 4661 | // Fix tail call attribute of CALL nodes. |
| 4662 | for (SelectionDAG::allnodes_iterator BE = DAG.allnodes_begin(), |
| 4663 | BI = prior(DAG.allnodes_end()); BI != BE; --BI) { |
| 4664 | if (BI->getOpcode() == ISD::CALL) { |
| 4665 | SDOperand OpRet(Ret, 0); |
| 4666 | SDOperand OpCall(static_cast<SDNode*>(BI), 0); |
| 4667 | bool isMarkedTailCall = |
| 4668 | cast<ConstantSDNode>(OpCall.getOperand(3))->getValue() != 0; |
| 4669 | // If CALL node has tail call attribute set to true and the call is not |
| 4670 | // eligible (no RET or the target rejects) the attribute is fixed to |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 4671 | // false. The TargetLowering::IsEligibleForTailCallOptimization function |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4672 | // must correctly identify tail call optimizable calls. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4673 | if (!isMarkedTailCall) continue; |
| 4674 | if (Ret==NULL || |
| 4675 | !TLI.IsEligibleForTailCallOptimization(OpCall, OpRet, DAG)) { |
| 4676 | // Not eligible. Mark CALL node as non tail call. |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4677 | SmallVector<SDOperand, 32> Ops; |
| 4678 | unsigned idx=0; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4679 | for(SDNode::op_iterator I =OpCall.Val->op_begin(), |
| 4680 | E = OpCall.Val->op_end(); I != E; I++, idx++) { |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4681 | if (idx!=3) |
| 4682 | Ops.push_back(*I); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4683 | else |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4684 | Ops.push_back(DAG.getConstant(false, TLI.getPointerTy())); |
| 4685 | } |
| 4686 | DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size()); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4687 | } else { |
| 4688 | // Look for tail call clobbered arguments. Emit a series of |
| 4689 | // copyto/copyfrom virtual register nodes to protect them. |
| 4690 | SmallVector<SDOperand, 32> Ops; |
| 4691 | SDOperand Chain = OpCall.getOperand(0), InFlag; |
| 4692 | unsigned idx=0; |
| 4693 | for(SDNode::op_iterator I = OpCall.Val->op_begin(), |
| 4694 | E = OpCall.Val->op_end(); I != E; I++, idx++) { |
| 4695 | SDOperand Arg = *I; |
| 4696 | if (idx > 4 && (idx % 2)) { |
| 4697 | bool isByVal = cast<ARG_FLAGSSDNode>(OpCall.getOperand(idx+1))-> |
| 4698 | getArgFlags().isByVal(); |
| 4699 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4700 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 4701 | if (!isByVal && |
| 4702 | IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) { |
| 4703 | MVT::ValueType VT = Arg.getValueType(); |
| 4704 | unsigned VReg = MF.getRegInfo(). |
| 4705 | createVirtualRegister(TLI.getRegClassFor(VT)); |
| 4706 | Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag); |
| 4707 | InFlag = Chain.getValue(1); |
| 4708 | Arg = DAG.getCopyFromReg(Chain, VReg, VT, InFlag); |
| 4709 | Chain = Arg.getValue(1); |
| 4710 | InFlag = Arg.getValue(2); |
| 4711 | } |
| 4712 | } |
| 4713 | Ops.push_back(Arg); |
| 4714 | } |
| 4715 | // Link in chain of CopyTo/CopyFromReg. |
| 4716 | Ops[0] = Chain; |
| 4717 | DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size()); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | } |
| 4722 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4723 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 4724 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4725 | FunctionLoweringInfo &FuncInfo) { |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 4726 | SelectionDAGLowering SDL(DAG, TLI, *AA, FuncInfo, GCI); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4727 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4728 | // Lower any arguments needed in this block if this is the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 4729 | if (LLVMBB == &LLVMBB->getParent()->getEntryBlock()) |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4730 | LowerArguments(LLVMBB, SDL); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4731 | |
| 4732 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 4733 | SDL.setCurrentBasicBlock(BB); |
| 4734 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4735 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 4736 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 4737 | if (MMI && BB->isLandingPad()) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4738 | // Add a label to mark the beginning of the landing pad. Deletion of the |
| 4739 | // landing pad can thus be detected via the MachineModuleInfo. |
| 4740 | unsigned LabelID = MMI->addLandingPad(BB); |
| 4741 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, DAG.getEntryNode(), |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 4742 | DAG.getConstant(LabelID, MVT::i32), |
| 4743 | DAG.getConstant(1, MVT::i32))); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4744 | |
Evan Cheng | e47c333 | 2007-06-27 18:45:32 +0000 | [diff] [blame] | 4745 | // Mark exception register as live in. |
| 4746 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 4747 | if (Reg) BB->addLiveIn(Reg); |
| 4748 | |
| 4749 | // Mark exception selector register as live in. |
| 4750 | Reg = TLI.getExceptionSelectorRegister(); |
| 4751 | if (Reg) BB->addLiveIn(Reg); |
| 4752 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4753 | // FIXME: Hack around an exception handling flaw (PR1508): the personality |
| 4754 | // function and list of typeids logically belong to the invoke (or, if you |
| 4755 | // like, the basic block containing the invoke), and need to be associated |
| 4756 | // with it in the dwarf exception handling tables. Currently however the |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 4757 | // information is provided by an intrinsic (eh.selector) that can be moved |
| 4758 | // to unexpected places by the optimizers: if the unwind edge is critical, |
| 4759 | // then breaking it can result in the intrinsics being in the successor of |
| 4760 | // the landing pad, not the landing pad itself. This results in exceptions |
| 4761 | // not being caught because no typeids are associated with the invoke. |
| 4762 | // This may not be the only way things can go wrong, but it is the only way |
| 4763 | // we try to work around for the moment. |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4764 | BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator()); |
| 4765 | |
| 4766 | if (Br && Br->isUnconditional()) { // Critical edge? |
| 4767 | BasicBlock::iterator I, E; |
| 4768 | for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I) |
Duncan Sands | cf26d7c | 2007-07-04 20:52:51 +0000 | [diff] [blame] | 4769 | if (isSelector(I)) |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | |
| 4772 | if (I == E) |
| 4773 | // No catch info found - try to extract some from the successor. |
| 4774 | copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, FuncInfo); |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4778 | // Lower all of the non-terminator instructions. |
| 4779 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 4780 | I != E; ++I) |
| 4781 | SDL.visit(*I); |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 4782 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4783 | // Ensure that all instructions which are used outside of their defining |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 4784 | // blocks are available as virtual registers. Invoke is handled elsewhere. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4785 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 4786 | if (!I->use_empty() && !isa<PHINode>(I) && !isa<InvokeInst>(I)) { |
Chris Lattner | 9f24ad7 | 2007-02-04 01:35:11 +0000 | [diff] [blame] | 4787 | DenseMap<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4788 | if (VMI != FuncInfo.ValueMap.end()) |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4789 | SDL.CopyValueToVirtualRegister(I, VMI->second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 4793 | // ensure constants are generated when needed. Remember the virtual registers |
| 4794 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 4795 | // directly add them, because expansion might result in multiple MBB's for one |
| 4796 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 4797 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4798 | // |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4799 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4800 | |
| 4801 | // Emit constants only once even if used by multiple PHI nodes. |
| 4802 | std::map<Constant*, unsigned> ConstantsOut; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4803 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4804 | // Vector bool would be better, but vector<bool> is really slow. |
| 4805 | std::vector<unsigned char> SuccsHandled; |
| 4806 | if (TI->getNumSuccessors()) |
| 4807 | SuccsHandled.resize(BB->getParent()->getNumBlockIDs()); |
| 4808 | |
Dan Gohman | 532dc2e | 2007-07-09 20:59:04 +0000 | [diff] [blame] | 4809 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 4810 | // from this block. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4811 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 4812 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4813 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4814 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4816 | // If this terminator has multiple identical successors (common for |
| 4817 | // switches), only handle each succ once. |
| 4818 | unsigned SuccMBBNo = SuccMBB->getNumber(); |
| 4819 | if (SuccsHandled[SuccMBBNo]) continue; |
| 4820 | SuccsHandled[SuccMBBNo] = true; |
| 4821 | |
| 4822 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4823 | PHINode *PN; |
| 4824 | |
| 4825 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 4826 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 4827 | // emitted yet. |
| 4828 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4829 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 4830 | // Ignore dead phi's. |
| 4831 | if (PN->use_empty()) continue; |
| 4832 | |
| 4833 | unsigned Reg; |
| 4834 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 4835 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4836 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 4837 | unsigned &RegOut = ConstantsOut[C]; |
| 4838 | if (RegOut == 0) { |
| 4839 | RegOut = FuncInfo.CreateRegForValue(C); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4840 | SDL.CopyValueToVirtualRegister(C, RegOut); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4841 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4842 | Reg = RegOut; |
| 4843 | } else { |
| 4844 | Reg = FuncInfo.ValueMap[PHIOp]; |
| 4845 | if (Reg == 0) { |
| 4846 | assert(isa<AllocaInst>(PHIOp) && |
| 4847 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 4848 | "Didn't codegen value into a register!??"); |
| 4849 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4850 | SDL.CopyValueToVirtualRegister(PHIOp, Reg); |
Chris Lattner | 7e02151 | 2006-03-31 02:12:18 +0000 | [diff] [blame] | 4851 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4852 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4853 | |
| 4854 | // Remember that this register needs to added to the machine PHI node as |
| 4855 | // the input for this MBB. |
| 4856 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4857 | unsigned NumRegisters = TLI.getNumRegisters(VT); |
Dan Gohman | b9f1019 | 2007-06-21 14:42:22 +0000 | [diff] [blame] | 4858 | for (unsigned i = 0, e = NumRegisters; i != e; ++i) |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4859 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 4860 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4861 | } |
| 4862 | ConstantsOut.clear(); |
| 4863 | |
| 4864 | // Lower the terminator after the copies are emitted. |
Duncan Sands | f19f6bb | 2007-06-13 05:51:31 +0000 | [diff] [blame] | 4865 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4866 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4867 | // Copy over any CaseBlock records that may now exist due to SwitchInst |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4868 | // lowering, as well as any jump table information. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4869 | SwitchCases.clear(); |
| 4870 | SwitchCases = SDL.SwitchCases; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4871 | JTCases.clear(); |
| 4872 | JTCases = SDL.JTCases; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4873 | BitTestCases.clear(); |
| 4874 | BitTestCases = SDL.BitTestCases; |
| 4875 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4876 | // Make sure the root of the DAG is up-to-date. |
Dan Gohman | 86e1ebf | 2008-03-27 19:56:19 +0000 | [diff] [blame] | 4877 | DAG.setRoot(SDL.getControlRoot()); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Check whether calls in this block are real tail calls. Fix up CALL nodes |
| 4880 | // with correct tailcall attribute so that the target can rely on the tailcall |
| 4881 | // attribute indicating whether the call is really eligible for tail call |
| 4882 | // optimization. |
| 4883 | CheckDAGForTailCallsAndFixThem(DAG, TLI); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4886 | void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) { |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 4887 | DOUT << "Lowered selection DAG:\n"; |
| 4888 | DEBUG(DAG.dump()); |
| 4889 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4890 | // Run the DAG combiner in pre-legalize mode. |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 4891 | DAG.Combine(false, *AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4892 | |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 4893 | DOUT << "Optimized lowered selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4894 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4895 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4896 | // Second step, hack on the DAG until it only uses operations and types that |
| 4897 | // the target supports. |
Chris Lattner | 01d029b | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 4898 | #if 0 // Enable this some day. |
| 4899 | DAG.LegalizeTypes(); |
| 4900 | // Someday even later, enable a dag combine pass here. |
| 4901 | #endif |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 4902 | DAG.Legalize(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4903 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4904 | DOUT << "Legalized selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4905 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4906 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4907 | // Run the DAG combiner in post-legalize mode. |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 4908 | DAG.Combine(true, *AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4909 | |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 4910 | DOUT << "Optimized legalized selection DAG:\n"; |
| 4911 | DEBUG(DAG.dump()); |
| 4912 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4913 | if (ViewISelDAGs) DAG.viewGraph(); |
Evan Cheng | 552c4a8 | 2006-04-28 02:09:19 +0000 | [diff] [blame] | 4914 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4915 | // Third, instruction select all of the operations to machine code, adding the |
| 4916 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4917 | InstructionSelectBasicBlock(DAG); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4918 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4919 | DOUT << "Selected machine code:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4920 | DEBUG(BB->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4921 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4922 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4923 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 4924 | FunctionLoweringInfo &FuncInfo) { |
| 4925 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 4926 | { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 4927 | SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4928 | CurDAG = &DAG; |
| 4929 | |
| 4930 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 4931 | // types that are not supported by the target. |
| 4932 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 4933 | |
| 4934 | // Second step, emit the lowered DAG as machine code. |
| 4935 | CodeGenAndEmitDAG(DAG); |
| 4936 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4937 | |
| 4938 | DOUT << "Total amount of phi nodes to update: " |
| 4939 | << PHINodesToUpdate.size() << "\n"; |
| 4940 | DEBUG(for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) |
| 4941 | DOUT << "Node " << i << " : (" << PHINodesToUpdate[i].first |
| 4942 | << ", " << PHINodesToUpdate[i].second << ")\n";); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4943 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4944 | // Next, now that we know what the last MBB the LLVM BB expanded is, update |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4945 | // PHI nodes in successors. |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4946 | if (SwitchCases.empty() && JTCases.empty() && BitTestCases.empty()) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4947 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 4948 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 4949 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4950 | "This is not a machine PHI node that we are updating!"); |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 4951 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[i].second, |
| 4952 | false)); |
| 4953 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4954 | } |
| 4955 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4956 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4957 | |
| 4958 | for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i) { |
| 4959 | // Lower header first, if it wasn't already lowered |
| 4960 | if (!BitTestCases[i].Emitted) { |
| 4961 | SelectionDAG HSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 4962 | CurDAG = &HSDAG; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 4963 | SelectionDAGLowering HSDL(HSDAG, TLI, *AA, FuncInfo, GCI); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4964 | // Set the current basic block to the mbb we wish to insert the code into |
| 4965 | BB = BitTestCases[i].Parent; |
| 4966 | HSDL.setCurrentBasicBlock(BB); |
| 4967 | // Emit the code |
| 4968 | HSDL.visitBitTestHeader(BitTestCases[i]); |
| 4969 | HSDAG.setRoot(HSDL.getRoot()); |
| 4970 | CodeGenAndEmitDAG(HSDAG); |
| 4971 | } |
| 4972 | |
| 4973 | for (unsigned j = 0, ej = BitTestCases[i].Cases.size(); j != ej; ++j) { |
| 4974 | SelectionDAG BSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 4975 | CurDAG = &BSDAG; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 4976 | SelectionDAGLowering BSDL(BSDAG, TLI, *AA, FuncInfo, GCI); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 4977 | // Set the current basic block to the mbb we wish to insert the code into |
| 4978 | BB = BitTestCases[i].Cases[j].ThisBB; |
| 4979 | BSDL.setCurrentBasicBlock(BB); |
| 4980 | // Emit the code |
| 4981 | if (j+1 != ej) |
| 4982 | BSDL.visitBitTestCase(BitTestCases[i].Cases[j+1].ThisBB, |
| 4983 | BitTestCases[i].Reg, |
| 4984 | BitTestCases[i].Cases[j]); |
| 4985 | else |
| 4986 | BSDL.visitBitTestCase(BitTestCases[i].Default, |
| 4987 | BitTestCases[i].Reg, |
| 4988 | BitTestCases[i].Cases[j]); |
| 4989 | |
| 4990 | |
| 4991 | BSDAG.setRoot(BSDL.getRoot()); |
| 4992 | CodeGenAndEmitDAG(BSDAG); |
| 4993 | } |
| 4994 | |
| 4995 | // Update PHI Nodes |
| 4996 | for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 4997 | MachineInstr *PHI = PHINodesToUpdate[pi].first; |
| 4998 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 4999 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 5000 | "This is not a machine PHI node that we are updating!"); |
| 5001 | // This is "default" BB. We have two jumps to it. From "header" BB and |
| 5002 | // from last "case" BB. |
| 5003 | if (PHIBB == BitTestCases[i].Default) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5004 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second, |
| 5005 | false)); |
| 5006 | PHI->addOperand(MachineOperand::CreateMBB(BitTestCases[i].Parent)); |
| 5007 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second, |
| 5008 | false)); |
| 5009 | PHI->addOperand(MachineOperand::CreateMBB(BitTestCases[i].Cases. |
| 5010 | back().ThisBB)); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 5011 | } |
| 5012 | // One of "cases" BB. |
| 5013 | for (unsigned j = 0, ej = BitTestCases[i].Cases.size(); j != ej; ++j) { |
| 5014 | MachineBasicBlock* cBB = BitTestCases[i].Cases[j].ThisBB; |
| 5015 | if (cBB->succ_end() != |
| 5016 | std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5017 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second, |
| 5018 | false)); |
| 5019 | PHI->addOperand(MachineOperand::CreateMBB(cBB)); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 5025 | // If the JumpTable record is filled in, then we need to emit a jump table. |
| 5026 | // Updating the PHI nodes is tricky in this case, since we need to determine |
| 5027 | // whether the PHI is a successor of the range check MBB or the jump table MBB |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5028 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) { |
| 5029 | // Lower header first, if it wasn't already lowered |
| 5030 | if (!JTCases[i].first.Emitted) { |
| 5031 | SelectionDAG HSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 5032 | CurDAG = &HSDAG; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 5033 | SelectionDAGLowering HSDL(HSDAG, TLI, *AA, FuncInfo, GCI); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5034 | // Set the current basic block to the mbb we wish to insert the code into |
| 5035 | BB = JTCases[i].first.HeaderBB; |
| 5036 | HSDL.setCurrentBasicBlock(BB); |
| 5037 | // Emit the code |
| 5038 | HSDL.visitJumpTableHeader(JTCases[i].second, JTCases[i].first); |
| 5039 | HSDAG.setRoot(HSDL.getRoot()); |
| 5040 | CodeGenAndEmitDAG(HSDAG); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 5041 | } |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5042 | |
| 5043 | SelectionDAG JSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 5044 | CurDAG = &JSDAG; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 5045 | SelectionDAGLowering JSDL(JSDAG, TLI, *AA, FuncInfo, GCI); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5046 | // Set the current basic block to the mbb we wish to insert the code into |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5047 | BB = JTCases[i].second.MBB; |
| 5048 | JSDL.setCurrentBasicBlock(BB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5049 | // Emit the code |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5050 | JSDL.visitJumpTable(JTCases[i].second); |
| 5051 | JSDAG.setRoot(JSDL.getRoot()); |
| 5052 | CodeGenAndEmitDAG(JSDAG); |
| 5053 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5054 | // Update PHI Nodes |
| 5055 | for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 5056 | MachineInstr *PHI = PHINodesToUpdate[pi].first; |
| 5057 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 5058 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 5059 | "This is not a machine PHI node that we are updating!"); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 5060 | // "default" BB. We can go there only from header BB. |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 5061 | if (PHIBB == JTCases[i].second.Default) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5062 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second, |
| 5063 | false)); |
| 5064 | PHI->addOperand(MachineOperand::CreateMBB(JTCases[i].first.HeaderBB)); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 5065 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 5066 | // JT BB. Just iterate over successors here |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 5067 | if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5068 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pi].second, |
| 5069 | false)); |
| 5070 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5071 | } |
| 5072 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 5075 | // If the switch block involved a branch to one of the actual successors, we |
| 5076 | // need to update PHI nodes in that block. |
| 5077 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 5078 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 5079 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 5080 | "This is not a machine PHI node that we are updating!"); |
| 5081 | if (BB->isSuccessor(PHI->getParent())) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5082 | PHI->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[i].second, |
| 5083 | false)); |
| 5084 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 5085 | } |
| 5086 | } |
| 5087 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5088 | // If we generated any switch lowering information, build and codegen any |
| 5089 | // additional DAGs necessary. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5090 | for (unsigned i = 0, e = SwitchCases.size(); i != e; ++i) { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 5091 | SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5092 | CurDAG = &SDAG; |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 5093 | SelectionDAGLowering SDL(SDAG, TLI, *AA, FuncInfo, GCI); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5094 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5095 | // Set the current basic block to the mbb we wish to insert the code into |
| 5096 | BB = SwitchCases[i].ThisBB; |
| 5097 | SDL.setCurrentBasicBlock(BB); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5098 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5099 | // Emit the code |
| 5100 | SDL.visitSwitchCase(SwitchCases[i]); |
| 5101 | SDAG.setRoot(SDL.getRoot()); |
| 5102 | CodeGenAndEmitDAG(SDAG); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5103 | |
| 5104 | // Handle any PHI nodes in successors of this chunk, as if we were coming |
| 5105 | // from the original BB before switch expansion. Note that PHI nodes can |
| 5106 | // occur multiple times in PHINodesToUpdate. We have to be very careful to |
| 5107 | // handle them the right number of times. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 5108 | while ((BB = SwitchCases[i].TrueBB)) { // Handle LHS and RHS. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5109 | for (MachineBasicBlock::iterator Phi = BB->begin(); |
| 5110 | Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){ |
| 5111 | // This value for this PHI node is recorded in PHINodesToUpdate, get it. |
| 5112 | for (unsigned pn = 0; ; ++pn) { |
| 5113 | assert(pn != PHINodesToUpdate.size() && "Didn't find PHI entry!"); |
| 5114 | if (PHINodesToUpdate[pn].first == Phi) { |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 5115 | Phi->addOperand(MachineOperand::CreateReg(PHINodesToUpdate[pn]. |
| 5116 | second, false)); |
| 5117 | Phi->addOperand(MachineOperand::CreateMBB(SwitchCases[i].ThisBB)); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5118 | break; |
| 5119 | } |
| 5120 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5121 | } |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5122 | |
| 5123 | // Don't process RHS if same block as LHS. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 5124 | if (BB == SwitchCases[i].FalseBB) |
| 5125 | SwitchCases[i].FalseBB = 0; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 5126 | |
| 5127 | // If we haven't handled the RHS, do so now. Otherwise, we're done. |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 5128 | SwitchCases[i].TrueBB = SwitchCases[i].FalseBB; |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 5129 | SwitchCases[i].FalseBB = 0; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 5130 | } |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 5131 | assert(SwitchCases[i].TrueBB == 0 && SwitchCases[i].FalseBB == 0); |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 5132 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 5133 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 5134 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 5135 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 5136 | //===----------------------------------------------------------------------===// |
| 5137 | /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each |
| 5138 | /// target node in the graph. |
| 5139 | void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) { |
| 5140 | if (ViewSchedDAGs) DAG.viewGraph(); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 5141 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 5142 | RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault(); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 5143 | |
| 5144 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 5145 | Ctor = ISHeuristic; |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 5146 | RegisterScheduler::setDefault(Ctor); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 5147 | } |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 5148 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 5149 | ScheduleDAG *SL = Ctor(this, &DAG, BB); |
Chris Lattner | a3818e6 | 2006-01-21 19:12:11 +0000 | [diff] [blame] | 5150 | BB = SL->Run(); |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 5151 | |
| 5152 | if (ViewSUnitDAGs) SL->viewGraph(); |
| 5153 | |
Evan Cheng | cccf123 | 2006-02-04 06:49:00 +0000 | [diff] [blame] | 5154 | delete SL; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 5155 | } |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 5156 | |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 5157 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 5158 | HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() { |
| 5159 | return new HazardRecognizer(); |
| 5160 | } |
| 5161 | |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5162 | //===----------------------------------------------------------------------===// |
| 5163 | // Helper functions used by the generated instruction selector. |
| 5164 | //===----------------------------------------------------------------------===// |
| 5165 | // Calls to these methods are generated by tblgen. |
| 5166 | |
| 5167 | /// CheckAndMask - The isel is trying to match something like (and X, 255). If |
| 5168 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 5169 | /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value |
| 5170 | /// specified in the .td file (e.g. 255). |
| 5171 | bool SelectionDAGISel::CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, |
Dan Gohman | dc9b3d0 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 5172 | int64_t DesiredMaskS) const { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5173 | const APInt &ActualMask = RHS->getAPIntValue(); |
| 5174 | const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5175 | |
| 5176 | // If the actual mask exactly matches, success! |
| 5177 | if (ActualMask == DesiredMask) |
| 5178 | return true; |
| 5179 | |
| 5180 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5181 | if (ActualMask.intersects(~DesiredMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5182 | return false; |
| 5183 | |
| 5184 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 5185 | // either already zero or is not demanded. Check for known zero input bits. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5186 | APInt NeededMask = DesiredMask & ~ActualMask; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5187 | if (CurDAG->MaskedValueIsZero(LHS, NeededMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5188 | return true; |
| 5189 | |
| 5190 | // TODO: check to see if missing bits are just not demanded. |
| 5191 | |
| 5192 | // Otherwise, this pattern doesn't match. |
| 5193 | return false; |
| 5194 | } |
| 5195 | |
| 5196 | /// CheckOrMask - The isel is trying to match something like (or X, 255). If |
| 5197 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 5198 | /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value |
| 5199 | /// specified in the .td file (e.g. 255). |
| 5200 | bool SelectionDAGISel::CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5201 | int64_t DesiredMaskS) const { |
| 5202 | const APInt &ActualMask = RHS->getAPIntValue(); |
| 5203 | const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5204 | |
| 5205 | // If the actual mask exactly matches, success! |
| 5206 | if (ActualMask == DesiredMask) |
| 5207 | return true; |
| 5208 | |
| 5209 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5210 | if (ActualMask.intersects(~DesiredMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5211 | return false; |
| 5212 | |
| 5213 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 5214 | // either already zero or is not demanded. Check for known zero input bits. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5215 | APInt NeededMask = DesiredMask & ~ActualMask; |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5216 | |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 5217 | APInt KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5218 | CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 5219 | |
| 5220 | // If all the missing bits in the or are already known to be set, match! |
| 5221 | if ((NeededMask & KnownOne) == NeededMask) |
| 5222 | return true; |
| 5223 | |
| 5224 | // TODO: check to see if missing bits are just not demanded. |
| 5225 | |
| 5226 | // Otherwise, this pattern doesn't match. |
| 5227 | return false; |
| 5228 | } |
| 5229 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 5230 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 5231 | /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated |
| 5232 | /// by tblgen. Others should not call it. |
| 5233 | void SelectionDAGISel:: |
| 5234 | SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) { |
| 5235 | std::vector<SDOperand> InOps; |
| 5236 | std::swap(InOps, Ops); |
| 5237 | |
| 5238 | Ops.push_back(InOps[0]); // input chain. |
| 5239 | Ops.push_back(InOps[1]); // input asm string. |
| 5240 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 5241 | unsigned i = 2, e = InOps.size(); |
| 5242 | if (InOps[e-1].getValueType() == MVT::Flag) |
| 5243 | --e; // Don't process a flag operand if it is here. |
| 5244 | |
| 5245 | while (i != e) { |
| 5246 | unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue(); |
| 5247 | if ((Flags & 7) != 4 /*MEM*/) { |
| 5248 | // Just skip over this operand, copying the operands verbatim. |
| 5249 | Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1); |
| 5250 | i += (Flags >> 3) + 1; |
| 5251 | } else { |
| 5252 | assert((Flags >> 3) == 1 && "Memory operand with multiple values?"); |
| 5253 | // Otherwise, this is a memory operand. Ask the target to select it. |
| 5254 | std::vector<SDOperand> SelOps; |
| 5255 | if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 5256 | cerr << "Could not match memory address. Inline asm failure!\n"; |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 5257 | exit(1); |
| 5258 | } |
| 5259 | |
| 5260 | // Add this to the output node. |
Chris Lattner | 4b993b1 | 2007-04-09 00:33:58 +0000 | [diff] [blame] | 5261 | MVT::ValueType IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy(); |
Chris Lattner | 36d4396 | 2006-12-16 21:14:48 +0000 | [diff] [blame] | 5262 | Ops.push_back(DAG.getTargetConstant(4/*MEM*/ | (SelOps.size() << 3), |
Chris Lattner | 4b993b1 | 2007-04-09 00:33:58 +0000 | [diff] [blame] | 5263 | IntPtrTy)); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 5264 | Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); |
| 5265 | i += 2; |
| 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | // Add the flag input back if present. |
| 5270 | if (e != InOps.size()) |
| 5271 | Ops.push_back(InOps.back()); |
| 5272 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 5273 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 5274 | char SelectionDAGISel::ID = 0; |