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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ScheduleDAG.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalVariable.h" |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 23 | #include "llvm/InlineAsm.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Intrinsics.h" |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 26 | #include "llvm/IntrinsicInst.h" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SelectionDAG.h" |
| 34 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 35 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetAsmInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetData.h" |
| 38 | #include "llvm/Target/TargetFrameInfo.h" |
| 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetLowering.h" |
| 41 | #include "llvm/Target/TargetMachine.h" |
Vladimir Prus | 1247291 | 2006-05-23 13:43:15 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
Jeff Cohen | 7e88103 | 2006-02-24 02:52:40 +0000 | [diff] [blame] | 47 | #include <algorithm> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 50 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 51 | static cl::opt<bool> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 52 | ViewISelDAGs("view-isel-dags", cl::Hidden, |
| 53 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 54 | static cl::opt<bool> |
| 55 | ViewSchedDAGs("view-sched-dags", cl::Hidden, |
| 56 | cl::desc("Pop up a window to show sched dags as they are processed")); |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 57 | #else |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 58 | static const bool ViewISelDAGs = 0, ViewSchedDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 61 | |
| 62 | //===---------------------------------------------------------------------===// |
| 63 | /// |
| 64 | /// RegisterScheduler class - Track the registration of instruction schedulers. |
| 65 | /// |
| 66 | //===---------------------------------------------------------------------===// |
| 67 | MachinePassRegistry RegisterScheduler::Registry; |
| 68 | |
| 69 | //===---------------------------------------------------------------------===// |
| 70 | /// |
| 71 | /// ISHeuristic command line option for instruction schedulers. |
| 72 | /// |
| 73 | //===---------------------------------------------------------------------===// |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 74 | namespace { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 75 | cl::opt<RegisterScheduler::FunctionPassCtor, false, |
| 76 | RegisterPassParser<RegisterScheduler> > |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 77 | ISHeuristic("sched", |
Chris Lattner | 3700f90 | 2006-08-03 00:18:59 +0000 | [diff] [blame] | 78 | cl::init(&createDefaultScheduler), |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 79 | cl::desc("Instruction schedulers available:")); |
| 80 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 81 | static RegisterScheduler |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 82 | defaultListDAGScheduler("default", " Best scheduler for the target", |
| 83 | createDefaultScheduler); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 86 | namespace { |
| 87 | /// RegsForValue - This struct represents the physical registers that a |
| 88 | /// particular value is assigned and the type information about the value. |
| 89 | /// This is needed because values can be promoted into larger registers and |
| 90 | /// expanded into multiple smaller registers than the value. |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 91 | struct VISIBILITY_HIDDEN RegsForValue { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 92 | /// Regs - This list hold the register (for legal and promoted values) |
| 93 | /// or register set (for expanded values) that the value should be assigned |
| 94 | /// to. |
| 95 | std::vector<unsigned> Regs; |
| 96 | |
| 97 | /// RegVT - The value type of each register. |
| 98 | /// |
| 99 | MVT::ValueType RegVT; |
| 100 | |
| 101 | /// ValueVT - The value type of the LLVM value, which may be promoted from |
| 102 | /// RegVT or made from merging the two expanded parts. |
| 103 | MVT::ValueType ValueVT; |
| 104 | |
| 105 | RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {} |
| 106 | |
| 107 | RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt) |
| 108 | : RegVT(regvt), ValueVT(valuevt) { |
| 109 | Regs.push_back(Reg); |
| 110 | } |
| 111 | RegsForValue(const std::vector<unsigned> ®s, |
| 112 | MVT::ValueType regvt, MVT::ValueType valuevt) |
| 113 | : Regs(regs), RegVT(regvt), ValueVT(valuevt) { |
| 114 | } |
| 115 | |
| 116 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 117 | /// this value and returns the result as a ValueVT value. This uses |
| 118 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 119 | SDOperand getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 120 | SDOperand &Chain, SDOperand &Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 121 | |
| 122 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 123 | /// specified value into the registers specified by this object. This uses |
| 124 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 125 | void getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 126 | SDOperand &Chain, SDOperand &Flag, |
| 127 | MVT::ValueType PtrVT) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 128 | |
| 129 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 130 | /// operand list. This adds the code marker and includes the number of |
| 131 | /// values added into it. |
| 132 | void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 133 | std::vector<SDOperand> &Ops) const; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 134 | }; |
| 135 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 137 | namespace llvm { |
| 138 | //===--------------------------------------------------------------------===// |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 139 | /// createDefaultScheduler - This creates an instruction scheduler appropriate |
| 140 | /// for the target. |
| 141 | ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS, |
| 142 | SelectionDAG *DAG, |
| 143 | MachineBasicBlock *BB) { |
| 144 | TargetLowering &TLI = IS->getTargetLowering(); |
| 145 | |
| 146 | if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) { |
| 147 | return createTDListDAGScheduler(IS, DAG, BB); |
| 148 | } else { |
| 149 | assert(TLI.getSchedulingPreference() == |
| 150 | TargetLowering::SchedulingForRegPressure && "Unknown sched type!"); |
| 151 | return createBURRListDAGScheduler(IS, DAG, BB); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | //===--------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 157 | /// FunctionLoweringInfo - This contains information that is global to a |
| 158 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 159 | class FunctionLoweringInfo { |
| 160 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 161 | TargetLowering &TLI; |
| 162 | Function &Fn; |
| 163 | MachineFunction &MF; |
| 164 | SSARegMap *RegMap; |
| 165 | |
| 166 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 167 | |
| 168 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 169 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 170 | |
| 171 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 172 | /// we must remember which virtual registers hold the values for |
| 173 | /// cross-basic-block values. |
| 174 | std::map<const Value*, unsigned> ValueMap; |
| 175 | |
| 176 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 177 | /// the entry block. This allows the allocas to be efficiently referenced |
| 178 | /// anywhere in the function. |
| 179 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 180 | |
| 181 | unsigned MakeReg(MVT::ValueType VT) { |
| 182 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 183 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 184 | |
| 185 | /// isExportedInst - Return true if the specified value is an instruction |
| 186 | /// exported from its block. |
| 187 | bool isExportedInst(const Value *V) { |
| 188 | return ValueMap.count(V); |
| 189 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 191 | unsigned CreateRegForValue(const Value *V); |
| 192 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 193 | unsigned InitializeRegForValue(const Value *V) { |
| 194 | unsigned &R = ValueMap[V]; |
| 195 | assert(R == 0 && "Already initialized this value register!"); |
| 196 | return R = CreateRegForValue(V); |
| 197 | } |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 202 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 203 | /// switch instruction, which may expand to multiple basic blocks. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 204 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 205 | if (isa<PHINode>(I)) return true; |
| 206 | BasicBlock *BB = I->getParent(); |
| 207 | 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] | 208 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 209 | // FIXME: Remove switchinst special case. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 210 | isa<SwitchInst>(*UI)) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 211 | return true; |
| 212 | return false; |
| 213 | } |
| 214 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 215 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 216 | /// entry block, return true. This includes arguments used by switches, since |
| 217 | /// the switch may expand into multiple basic blocks. |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 218 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 219 | BasicBlock *Entry = A->getParent()->begin(); |
| 220 | 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] | 221 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 222 | return false; // Use not in entry block. |
| 223 | return true; |
| 224 | } |
| 225 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 226 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 227 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 228 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 229 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 230 | // Create a vreg for each argument register that is not dead and is used |
| 231 | // outside of the entry block for the function. |
| 232 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 233 | AI != E; ++AI) |
| 234 | if (!isOnlyUsedInEntryBlock(AI)) |
| 235 | InitializeRegForValue(AI); |
| 236 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 237 | // Initialize the mapping of values to registers. This is only set up for |
| 238 | // instruction values that are used outside of the block that defines |
| 239 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 240 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 241 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 242 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 243 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 244 | const Type *Ty = AI->getAllocatedType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 245 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 246 | unsigned Align = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 247 | std::max((unsigned)TLI.getTargetData()->getTypeAlignment(Ty), |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 248 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 249 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 250 | // If the alignment of the value is smaller than the size of the |
| 251 | // value, and if the size of the value is particularly small |
| 252 | // (<= 8 bytes), round up to the size of the value for potentially |
| 253 | // better performance. |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 254 | // |
| 255 | // FIXME: This could be made better with a preferred alignment hook in |
| 256 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 257 | if (Align < TySize && TySize <= 8) Align = TySize; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 258 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 259 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 260 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 261 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 264 | for (; BB != EB; ++BB) |
| 265 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 266 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 267 | if (!isa<AllocaInst>(I) || |
| 268 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 269 | InitializeRegForValue(I); |
| 270 | |
| 271 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 272 | // also creates the initial PHI MachineInstrs, though none of the input |
| 273 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 274 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 275 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 276 | MBBMap[BB] = MBB; |
| 277 | MF.getBasicBlockList().push_back(MBB); |
| 278 | |
| 279 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 280 | // appropriate. |
| 281 | PHINode *PN; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 282 | for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){ |
| 283 | if (PN->use_empty()) continue; |
| 284 | |
| 285 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
| 286 | unsigned NumElements; |
| 287 | if (VT != MVT::Vector) |
| 288 | NumElements = TLI.getNumElements(VT); |
| 289 | else { |
| 290 | MVT::ValueType VT1,VT2; |
| 291 | NumElements = |
| 292 | TLI.getPackedTypeBreakdown(cast<PackedType>(PN->getType()), |
| 293 | VT1, VT2); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 294 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 295 | unsigned PHIReg = ValueMap[PN]; |
| 296 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 297 | const TargetInstrInfo *TII = TLI.getTargetMachine().getInstrInfo(); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 298 | for (unsigned i = 0; i != NumElements; ++i) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 299 | BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 304 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 305 | /// the correctly promoted or expanded types. Assign these registers |
| 306 | /// consecutive vreg numbers and return the first assigned number. |
| 307 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 308 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 309 | |
| 310 | // The number of multiples of registers that we need, to, e.g., split up |
| 311 | // a <2 x int64> -> 4 x i32 registers. |
| 312 | unsigned NumVectorRegs = 1; |
| 313 | |
| 314 | // If this is a packed type, figure out what type it will decompose into |
| 315 | // and how many of the elements it will use. |
| 316 | if (VT == MVT::Vector) { |
| 317 | const PackedType *PTy = cast<PackedType>(V->getType()); |
| 318 | unsigned NumElts = PTy->getNumElements(); |
| 319 | MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType()); |
| 320 | |
| 321 | // Divide the input until we get to a supported size. This will always |
| 322 | // end with a scalar if the target doesn't support vectors. |
| 323 | while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) { |
| 324 | NumElts >>= 1; |
| 325 | NumVectorRegs <<= 1; |
| 326 | } |
Chris Lattner | 6cb7004 | 2006-03-16 23:05:19 +0000 | [diff] [blame] | 327 | if (NumElts == 1) |
| 328 | VT = EltTy; |
| 329 | else |
| 330 | VT = getVectorType(EltTy, NumElts); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // The common case is that we will only create one register for this |
| 334 | // value. If we have that case, create and return the virtual register. |
| 335 | unsigned NV = TLI.getNumElements(VT); |
| 336 | if (NV == 1) { |
| 337 | // If we are promoting this value, pick the next largest supported type. |
| 338 | MVT::ValueType PromotedType = TLI.getTypeToTransformTo(VT); |
| 339 | unsigned Reg = MakeReg(PromotedType); |
| 340 | // If this is a vector of supported or promoted types (e.g. 4 x i16), |
| 341 | // create all of the registers. |
| 342 | for (unsigned i = 1; i != NumVectorRegs; ++i) |
| 343 | MakeReg(PromotedType); |
| 344 | return Reg; |
| 345 | } |
| 346 | |
| 347 | // If this value is represented with multiple target registers, make sure |
| 348 | // to create enough consecutive registers of the right (smaller) type. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 349 | VT = TLI.getTypeToExpandTo(VT); |
| 350 | unsigned R = MakeReg(VT); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 351 | for (unsigned i = 1; i != NV*NumVectorRegs; ++i) |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 352 | MakeReg(VT); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 353 | return R; |
| 354 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 355 | |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 358 | /// implementation that is parameterized by a TargetLowering object. |
| 359 | /// Also, targets can overload any lowering method. |
| 360 | /// |
| 361 | namespace llvm { |
| 362 | class SelectionDAGLowering { |
| 363 | MachineBasicBlock *CurMBB; |
| 364 | |
| 365 | std::map<const Value*, SDOperand> NodeMap; |
| 366 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 367 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 368 | /// them up and then emit token factor nodes when possible. This allows us to |
| 369 | /// get simple disambiguation between loads without worrying about alias |
| 370 | /// analysis. |
| 371 | std::vector<SDOperand> PendingLoads; |
| 372 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 373 | /// Case - A pair of values to record the Value for a switch case, and the |
| 374 | /// case's target basic block. |
| 375 | typedef std::pair<Constant*, MachineBasicBlock*> Case; |
| 376 | typedef std::vector<Case>::iterator CaseItr; |
| 377 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
| 378 | |
| 379 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 380 | /// of conditional branches. |
| 381 | struct CaseRec { |
| 382 | CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) : |
| 383 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 384 | |
| 385 | /// CaseBB - The MBB in which to emit the compare and branch |
| 386 | MachineBasicBlock *CaseBB; |
| 387 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 388 | /// greater-than-or-equal-to these Constants. |
| 389 | Constant *LT; |
| 390 | Constant *GE; |
| 391 | /// Range - A pair of iterators representing the range of case values to be |
| 392 | /// processed at this point in the binary search tree. |
| 393 | CaseRange Range; |
| 394 | }; |
| 395 | |
| 396 | /// The comparison function for sorting Case values. |
| 397 | struct CaseCmp { |
| 398 | bool operator () (const Case& C1, const Case& C2) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 399 | assert(isa<ConstantInt>(C1.first) && isa<ConstantInt>(C2.first)); |
Reid Spencer | 8c57dfb | 2007-01-03 04:25:33 +0000 | [diff] [blame] | 400 | return cast<const ConstantInt>(C1.first)->getSExtValue() < |
| 401 | cast<const ConstantInt>(C2.first)->getSExtValue(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 402 | } |
| 403 | }; |
| 404 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 405 | public: |
| 406 | // TLI - This is information that describes the available target features we |
| 407 | // need for lowering. This indicates when operations are unavailable, |
| 408 | // implemented with a libcall, etc. |
| 409 | TargetLowering &TLI; |
| 410 | SelectionDAG &DAG; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 411 | const TargetData *TD; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 412 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 413 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 414 | /// SwitchInst code generation information. |
| 415 | std::vector<SelectionDAGISel::CaseBlock> SwitchCases; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 416 | SelectionDAGISel::JumpTable JT; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 418 | /// FuncInfo - Information about the function as a whole. |
| 419 | /// |
| 420 | FunctionLoweringInfo &FuncInfo; |
| 421 | |
| 422 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 423 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 424 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 425 | JT(0,0,0,0), FuncInfo(funcinfo) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 428 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 429 | /// |
| 430 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 431 | if (PendingLoads.empty()) |
| 432 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 433 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 434 | if (PendingLoads.size() == 1) { |
| 435 | SDOperand Root = PendingLoads[0]; |
| 436 | DAG.setRoot(Root); |
| 437 | PendingLoads.clear(); |
| 438 | return Root; |
| 439 | } |
| 440 | |
| 441 | // Otherwise, we have to make a token factor node. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 442 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 443 | &PendingLoads[0], PendingLoads.size()); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 444 | PendingLoads.clear(); |
| 445 | DAG.setRoot(Root); |
| 446 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 449 | SDOperand CopyValueToVirtualRegister(Value *V, unsigned Reg); |
| 450 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 451 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 452 | |
| 453 | void visit(unsigned Opcode, User &I) { |
Chris Lattner | 1e7aa5c | 2006-11-10 04:41:34 +0000 | [diff] [blame] | 454 | // Note: this doesn't use InstVisitor, because it has to work with |
| 455 | // ConstantExpr's in addition to instructions. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 456 | switch (Opcode) { |
| 457 | default: assert(0 && "Unknown instruction type encountered!"); |
| 458 | abort(); |
| 459 | // Build the switch statement using the Instruction.def file. |
| 460 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 461 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 462 | #include "llvm/Instruction.def" |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 467 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 468 | SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 469 | const Value *SV, SDOperand Root, |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 470 | bool isVolatile); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 471 | |
| 472 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 473 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 474 | } |
| 475 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 476 | SDOperand getValue(const Value *V); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 477 | |
| 478 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 479 | SDOperand &N = NodeMap[V]; |
| 480 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 481 | return N = NewN; |
| 482 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 484 | RegsForValue GetRegistersForValue(const std::string &ConstrCode, |
| 485 | MVT::ValueType VT, |
| 486 | bool OutReg, bool InReg, |
| 487 | std::set<unsigned> &OutputRegs, |
| 488 | std::set<unsigned> &InputRegs); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 490 | void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB, |
| 491 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
| 492 | unsigned Opc); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 493 | bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 494 | void ExportFromCurrentBlock(Value *V); |
| 495 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 496 | // Terminator instructions. |
| 497 | void visitRet(ReturnInst &I); |
| 498 | void visitBr(BranchInst &I); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 499 | void visitSwitch(SwitchInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 500 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 501 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 502 | // Helper for visitSwitch |
| 503 | void visitSwitchCase(SelectionDAGISel::CaseBlock &CB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 504 | void visitJumpTable(SelectionDAGISel::JumpTable &JT); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 506 | // These all get lowered before this pass. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 507 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 508 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 509 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 510 | void visitIntBinary(User &I, unsigned IntOp, unsigned VecOp); |
| 511 | void visitFPBinary(User &I, unsigned FPOp, unsigned VecOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 512 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 513 | void visitAdd(User &I) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 514 | if (I.getType()->isFloatingPoint()) |
| 515 | visitFPBinary(I, ISD::FADD, ISD::VADD); |
| 516 | else |
| 517 | visitIntBinary(I, ISD::ADD, ISD::VADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 518 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 519 | void visitSub(User &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 520 | void visitMul(User &I) { |
| 521 | if (I.getType()->isFloatingPoint()) |
| 522 | visitFPBinary(I, ISD::FMUL, ISD::VMUL); |
| 523 | else |
| 524 | visitIntBinary(I, ISD::MUL, ISD::VMUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 525 | } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 526 | void visitURem(User &I) { visitIntBinary(I, ISD::UREM, 0); } |
| 527 | void visitSRem(User &I) { visitIntBinary(I, ISD::SREM, 0); } |
| 528 | void visitFRem(User &I) { visitFPBinary (I, ISD::FREM, 0); } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 529 | void visitUDiv(User &I) { visitIntBinary(I, ISD::UDIV, ISD::VUDIV); } |
| 530 | void visitSDiv(User &I) { visitIntBinary(I, ISD::SDIV, ISD::VSDIV); } |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 531 | void visitFDiv(User &I) { visitFPBinary (I, ISD::FDIV, ISD::VSDIV); } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 532 | void visitAnd(User &I) { visitIntBinary(I, ISD::AND, ISD::VAND); } |
| 533 | void visitOr (User &I) { visitIntBinary(I, ISD::OR, ISD::VOR); } |
| 534 | void visitXor(User &I) { visitIntBinary(I, ISD::XOR, ISD::VXOR); } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 535 | void visitShl(User &I) { visitShift(I, ISD::SHL); } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 536 | void visitLShr(User &I) { visitShift(I, ISD::SRL); } |
| 537 | void visitAShr(User &I) { visitShift(I, ISD::SRA); } |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 538 | void visitICmp(User &I); |
| 539 | void visitFCmp(User &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 540 | // Visit the conversion instructions |
| 541 | void visitTrunc(User &I); |
| 542 | void visitZExt(User &I); |
| 543 | void visitSExt(User &I); |
| 544 | void visitFPTrunc(User &I); |
| 545 | void visitFPExt(User &I); |
| 546 | void visitFPToUI(User &I); |
| 547 | void visitFPToSI(User &I); |
| 548 | void visitUIToFP(User &I); |
| 549 | void visitSIToFP(User &I); |
| 550 | void visitPtrToInt(User &I); |
| 551 | void visitIntToPtr(User &I); |
| 552 | void visitBitCast(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 554 | void visitExtractElement(User &I); |
| 555 | void visitInsertElement(User &I); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 556 | void visitShuffleVector(User &I); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 558 | void visitGetElementPtr(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 559 | void visitSelect(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 560 | |
| 561 | void visitMalloc(MallocInst &I); |
| 562 | void visitFree(FreeInst &I); |
| 563 | void visitAlloca(AllocaInst &I); |
| 564 | void visitLoad(LoadInst &I); |
| 565 | void visitStore(StoreInst &I); |
| 566 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 567 | void visitCall(CallInst &I); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 568 | void visitInlineAsm(CallInst &I); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 569 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 570 | void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 572 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 573 | void visitVAArg(VAArgInst &I); |
| 574 | void visitVAEnd(CallInst &I); |
| 575 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 576 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 578 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 579 | |
| 580 | void visitUserOp1(Instruction &I) { |
| 581 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 582 | abort(); |
| 583 | } |
| 584 | void visitUserOp2(Instruction &I) { |
| 585 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 586 | abort(); |
| 587 | } |
| 588 | }; |
| 589 | } // end namespace llvm |
| 590 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 591 | SDOperand SelectionDAGLowering::getValue(const Value *V) { |
| 592 | SDOperand &N = NodeMap[V]; |
| 593 | if (N.Val) return N; |
| 594 | |
| 595 | const Type *VTy = V->getType(); |
| 596 | MVT::ValueType VT = TLI.getValueType(VTy); |
| 597 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 598 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 599 | visit(CE->getOpcode(), *CE); |
| 600 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 601 | return N; |
| 602 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 603 | return N = DAG.getGlobalAddress(GV, VT); |
| 604 | } else if (isa<ConstantPointerNull>(C)) { |
| 605 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 606 | } else if (isa<UndefValue>(C)) { |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 607 | if (!isa<PackedType>(VTy)) |
| 608 | return N = DAG.getNode(ISD::UNDEF, VT); |
| 609 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 610 | // Create a VBUILD_VECTOR of undef nodes. |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 611 | const PackedType *PTy = cast<PackedType>(VTy); |
| 612 | unsigned NumElements = PTy->getNumElements(); |
| 613 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 614 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 615 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 616 | Ops.assign(NumElements, DAG.getNode(ISD::UNDEF, PVT)); |
| 617 | |
| 618 | // Create a VConstant node with generic Vector type. |
| 619 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 620 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 621 | return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 622 | &Ops[0], Ops.size()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 623 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 624 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
| 625 | } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) { |
| 626 | unsigned NumElements = PTy->getNumElements(); |
| 627 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 628 | |
| 629 | // Now that we know the number and type of the elements, push a |
| 630 | // Constant or ConstantFP node onto the ops list for each element of |
| 631 | // the packed constant. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 632 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 633 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) { |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 634 | for (unsigned i = 0; i != NumElements; ++i) |
| 635 | Ops.push_back(getValue(CP->getOperand(i))); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 636 | } else { |
| 637 | assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!"); |
| 638 | SDOperand Op; |
| 639 | if (MVT::isFloatingPoint(PVT)) |
| 640 | Op = DAG.getConstantFP(0, PVT); |
| 641 | else |
| 642 | Op = DAG.getConstant(0, PVT); |
| 643 | Ops.assign(NumElements, Op); |
| 644 | } |
| 645 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 646 | // Create a VBUILD_VECTOR node with generic Vector type. |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 647 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 648 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 649 | return N = DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector,&Ops[0],Ops.size()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 650 | } else { |
| 651 | // Canonicalize all constant ints to be unsigned. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 652 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getZExtValue(),VT); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 657 | std::map<const AllocaInst*, int>::iterator SI = |
| 658 | FuncInfo.StaticAllocaMap.find(AI); |
| 659 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 660 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 661 | } |
| 662 | |
| 663 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 664 | FuncInfo.ValueMap.find(V); |
| 665 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
| 666 | |
| 667 | unsigned InReg = VMI->second; |
| 668 | |
| 669 | // If this type is not legal, make it so now. |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 670 | if (VT != MVT::Vector) { |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 671 | if (TLI.getTypeAction(VT) == TargetLowering::Expand) { |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 672 | // Source must be expanded. This input value is actually coming from the |
| 673 | // register pair VMI->second and VMI->second+1. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 674 | MVT::ValueType DestVT = TLI.getTypeToExpandTo(VT); |
| 675 | unsigned NumVals = TLI.getNumElements(VT); |
| 676 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 677 | if (NumVals == 1) |
| 678 | N = DAG.getNode(ISD::BIT_CONVERT, VT, N); |
| 679 | else { |
| 680 | assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!"); |
| 681 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 682 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 683 | } |
| 684 | } else { |
| 685 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 686 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 687 | if (TLI.getTypeAction(VT) == TargetLowering::Promote) // Promotion case |
| 688 | N = MVT::isFloatingPoint(VT) |
| 689 | ? DAG.getNode(ISD::FP_ROUND, VT, N) |
| 690 | : DAG.getNode(ISD::TRUNCATE, VT, N); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 691 | } |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 692 | } else { |
| 693 | // Otherwise, if this is a vector, make it available as a generic vector |
| 694 | // here. |
| 695 | MVT::ValueType PTyElementVT, PTyLegalElementVT; |
Chris Lattner | 2e2ef95 | 2006-04-05 06:54:42 +0000 | [diff] [blame] | 696 | const PackedType *PTy = cast<PackedType>(VTy); |
| 697 | unsigned NE = TLI.getPackedTypeBreakdown(PTy, PTyElementVT, |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 698 | PTyLegalElementVT); |
| 699 | |
| 700 | // Build a VBUILD_VECTOR with the input registers. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 701 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 702 | if (PTyElementVT == PTyLegalElementVT) { |
| 703 | // If the value types are legal, just VBUILD the CopyFromReg nodes. |
| 704 | for (unsigned i = 0; i != NE; ++i) |
| 705 | Ops.push_back(DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 706 | PTyElementVT)); |
| 707 | } else if (PTyElementVT < PTyLegalElementVT) { |
| 708 | // If the register was promoted, use TRUNCATE of FP_ROUND as appropriate. |
| 709 | for (unsigned i = 0; i != NE; ++i) { |
| 710 | SDOperand Op = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 711 | PTyElementVT); |
| 712 | if (MVT::isFloatingPoint(PTyElementVT)) |
| 713 | Op = DAG.getNode(ISD::FP_ROUND, PTyElementVT, Op); |
| 714 | else |
| 715 | Op = DAG.getNode(ISD::TRUNCATE, PTyElementVT, Op); |
| 716 | Ops.push_back(Op); |
| 717 | } |
| 718 | } else { |
| 719 | // If the register was expanded, use BUILD_PAIR. |
| 720 | assert((NE & 1) == 0 && "Must expand into a multiple of 2 elements!"); |
| 721 | for (unsigned i = 0; i != NE/2; ++i) { |
| 722 | SDOperand Op0 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 723 | PTyElementVT); |
| 724 | SDOperand Op1 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 725 | PTyElementVT); |
| 726 | Ops.push_back(DAG.getNode(ISD::BUILD_PAIR, VT, Op0, Op1)); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | Ops.push_back(DAG.getConstant(NE, MVT::i32)); |
| 731 | Ops.push_back(DAG.getValueType(PTyLegalElementVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 732 | N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 2e2ef95 | 2006-04-05 06:54:42 +0000 | [diff] [blame] | 733 | |
| 734 | // Finally, use a VBIT_CONVERT to make this available as the appropriate |
| 735 | // vector type. |
| 736 | N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N, |
| 737 | DAG.getConstant(PTy->getNumElements(), |
| 738 | MVT::i32), |
| 739 | DAG.getValueType(TLI.getValueType(PTy->getElementType()))); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | return N; |
| 743 | } |
| 744 | |
| 745 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 746 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 747 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 748 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 749 | return; |
| 750 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 751 | SmallVector<SDOperand, 8> NewValues; |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 752 | NewValues.push_back(getRoot()); |
| 753 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 754 | SDOperand RetOp = getValue(I.getOperand(i)); |
| 755 | |
| 756 | // If this is an integer return value, we need to promote it ourselves to |
| 757 | // the full width of a register, since LegalizeOp will use ANY_EXTEND rather |
| 758 | // than sign/zero. |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 759 | // FIXME: C calling convention requires the return type to be promoted to |
| 760 | // at least 32-bit. But this is not necessary for non-C calling conventions. |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 761 | if (MVT::isInteger(RetOp.getValueType()) && |
| 762 | RetOp.getValueType() < MVT::i64) { |
| 763 | MVT::ValueType TmpVT; |
| 764 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 765 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
| 766 | else |
| 767 | TmpVT = MVT::i32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 768 | const FunctionType *FTy = I.getParent()->getParent()->getFunctionType(); |
Reid Spencer | 376dd21 | 2007-01-03 05:03:05 +0000 | [diff] [blame^] | 769 | ISD::NodeType ExtendKind = ISD::ZERO_EXTEND; // FIXME: ANY_EXTEND? |
Reid Spencer | 8c57dfb | 2007-01-03 04:25:33 +0000 | [diff] [blame] | 770 | if (FTy->paramHasAttr(0, FunctionType::SExtAttribute)) |
| 771 | ExtendKind = ISD::SIGN_EXTEND; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 772 | if (FTy->paramHasAttr(0, FunctionType::ZExtAttribute)) |
| 773 | ExtendKind = ISD::ZERO_EXTEND; |
Reid Spencer | 376dd21 | 2007-01-03 05:03:05 +0000 | [diff] [blame^] | 774 | RetOp = DAG.getNode(ExtendKind, TmpVT, RetOp); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 775 | } |
| 776 | NewValues.push_back(RetOp); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 777 | NewValues.push_back(DAG.getConstant(false, MVT::i32)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 778 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 779 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, |
| 780 | &NewValues[0], NewValues.size())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 783 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 784 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 785 | /// CopyTo/FromReg. |
| 786 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 787 | // No need to export constants. |
| 788 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
| 789 | |
| 790 | // Already exported? |
| 791 | if (FuncInfo.isExportedInst(V)) return; |
| 792 | |
| 793 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 794 | PendingLoads.push_back(CopyValueToVirtualRegister(V, Reg)); |
| 795 | } |
| 796 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 797 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 798 | const BasicBlock *FromBB) { |
| 799 | // The operands of the setcc have to be in this block. We don't know |
| 800 | // how to export them from some other block. |
| 801 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 802 | // Can export from current BB. |
| 803 | if (VI->getParent() == FromBB) |
| 804 | return true; |
| 805 | |
| 806 | // Is already exported, noop. |
| 807 | return FuncInfo.isExportedInst(V); |
| 808 | } |
| 809 | |
| 810 | // If this is an argument, we can export it if the BB is the entry block or |
| 811 | // if it is already exported. |
| 812 | if (isa<Argument>(V)) { |
| 813 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 814 | return true; |
| 815 | |
| 816 | // Otherwise, can only export this if it is already exported. |
| 817 | return FuncInfo.isExportedInst(V); |
| 818 | } |
| 819 | |
| 820 | // Otherwise, constants can always be exported. |
| 821 | return true; |
| 822 | } |
| 823 | |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 824 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 825 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 826 | return I->getParent() == BB; |
| 827 | return true; |
| 828 | } |
| 829 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 830 | /// FindMergedConditions - If Cond is an expression like |
| 831 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 832 | MachineBasicBlock *TBB, |
| 833 | MachineBasicBlock *FBB, |
| 834 | MachineBasicBlock *CurBB, |
| 835 | unsigned Opc) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 836 | // 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] | 837 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 838 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 839 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
| 840 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 841 | BOp->getParent() != CurBB->getBasicBlock() || |
| 842 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 843 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 844 | const BasicBlock *BB = CurBB->getBasicBlock(); |
| 845 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 846 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Cond)) |
| 847 | if ((II->getIntrinsicID() == Intrinsic::isunordered_f32 || |
| 848 | II->getIntrinsicID() == Intrinsic::isunordered_f64) && |
| 849 | // The operands of the setcc have to be in this block. We don't know |
| 850 | // how to export them from some other block. If this is the first |
| 851 | // block of the sequence, no exporting is needed. |
| 852 | (CurBB == CurMBB || |
| 853 | (isExportableFromCurrentBlock(II->getOperand(1), BB) && |
| 854 | isExportableFromCurrentBlock(II->getOperand(2), BB)))) { |
| 855 | SelectionDAGISel::CaseBlock CB(ISD::SETUO, II->getOperand(1), |
| 856 | II->getOperand(2), TBB, FBB, CurBB); |
| 857 | SwitchCases.push_back(CB); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 862 | // If the leaf of the tree is a comparison, merge the condition into |
| 863 | // the caseblock. |
| 864 | if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) && |
| 865 | // 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] | 866 | // how to export them from some other block. If this is the first block |
| 867 | // of the sequence, no exporting is needed. |
| 868 | (CurBB == CurMBB || |
| 869 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 870 | isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 871 | BOp = cast<Instruction>(Cond); |
| 872 | ISD::CondCode Condition; |
| 873 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
| 874 | switch (IC->getPredicate()) { |
| 875 | default: assert(0 && "Unknown icmp predicate opcode!"); |
| 876 | case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break; |
| 877 | case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break; |
| 878 | case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break; |
| 879 | case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break; |
| 880 | case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break; |
| 881 | case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break; |
| 882 | case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break; |
| 883 | case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break; |
| 884 | case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break; |
| 885 | case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break; |
| 886 | } |
| 887 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
| 888 | ISD::CondCode FPC, FOC; |
| 889 | switch (FC->getPredicate()) { |
| 890 | default: assert(0 && "Unknown fcmp predicate opcode!"); |
| 891 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 892 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 893 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 894 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 895 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 896 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 897 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 898 | case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break; |
| 899 | case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break; |
| 900 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 901 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 902 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 903 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 904 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 905 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 906 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 907 | } |
| 908 | if (FiniteOnlyFPMath()) |
| 909 | Condition = FOC; |
| 910 | else |
| 911 | Condition = FPC; |
| 912 | } else { |
| 913 | assert(0 && "Unknown compare instruction"); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 916 | SelectionDAGISel::CaseBlock CB(Condition, BOp->getOperand(0), |
| 917 | BOp->getOperand(1), TBB, FBB, CurBB); |
| 918 | SwitchCases.push_back(CB); |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | // Create a CaseBlock record representing this branch. |
| 923 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, Cond, ConstantBool::getTrue(), |
| 924 | TBB, FBB, CurBB); |
| 925 | SwitchCases.push_back(CB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 929 | |
| 930 | // Create TmpBB after CurBB. |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 931 | MachineFunction::iterator BBI = CurBB; |
| 932 | MachineBasicBlock *TmpBB = new MachineBasicBlock(CurBB->getBasicBlock()); |
| 933 | CurBB->getParent()->getBasicBlockList().insert(++BBI, TmpBB); |
| 934 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 935 | if (Opc == Instruction::Or) { |
| 936 | // Codegen X | Y as: |
| 937 | // jmp_if_X TBB |
| 938 | // jmp TmpBB |
| 939 | // TmpBB: |
| 940 | // jmp_if_Y TBB |
| 941 | // jmp FBB |
| 942 | // |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 943 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 944 | // Emit the LHS condition. |
| 945 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
| 946 | |
| 947 | // Emit the RHS condition into TmpBB. |
| 948 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 949 | } else { |
| 950 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 951 | // Codegen X & Y as: |
| 952 | // jmp_if_X TmpBB |
| 953 | // jmp FBB |
| 954 | // TmpBB: |
| 955 | // jmp_if_Y TBB |
| 956 | // jmp FBB |
| 957 | // |
| 958 | // This requires creation of TmpBB after CurBB. |
| 959 | |
| 960 | // Emit the LHS condition. |
| 961 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
| 962 | |
| 963 | // Emit the RHS condition into TmpBB. |
| 964 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 965 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 968 | /// If the set of cases should be emitted as a series of branches, return true. |
| 969 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 970 | /// false. |
| 971 | static bool |
| 972 | ShouldEmitAsBranches(const std::vector<SelectionDAGISel::CaseBlock> &Cases) { |
| 973 | if (Cases.size() != 2) return true; |
| 974 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 975 | // If this is two comparisons of the same values or'd or and'd together, they |
| 976 | // will get folded into a single comparison, so don't emit two blocks. |
| 977 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 978 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 979 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 980 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 981 | return false; |
| 982 | } |
| 983 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 987 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 988 | // Update machine-CFG edges. |
| 989 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 990 | |
| 991 | // Figure out which block is immediately after the current one. |
| 992 | MachineBasicBlock *NextBlock = 0; |
| 993 | MachineFunction::iterator BBI = CurMBB; |
| 994 | if (++BBI != CurMBB->getParent()->end()) |
| 995 | NextBlock = BBI; |
| 996 | |
| 997 | if (I.isUnconditional()) { |
| 998 | // If this is not a fall-through branch, emit the branch. |
| 999 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1000 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 1001 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1003 | // Update machine-CFG edges. |
| 1004 | CurMBB->addSuccessor(Succ0MBB); |
| 1005 | |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | // If this condition is one of the special cases we handle, do special stuff |
| 1010 | // now. |
| 1011 | Value *CondVal = I.getCondition(); |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1012 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1013 | |
| 1014 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1015 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1016 | // For example, instead of something like: |
| 1017 | // cmp A, B |
| 1018 | // C = seteq |
| 1019 | // cmp D, E |
| 1020 | // F = setle |
| 1021 | // or C, F |
| 1022 | // jnz foo |
| 1023 | // Emit: |
| 1024 | // cmp A, B |
| 1025 | // je foo |
| 1026 | // cmp D, E |
| 1027 | // jle foo |
| 1028 | // |
| 1029 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
| 1030 | if (BOp->hasOneUse() && |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1031 | (BOp->getOpcode() == Instruction::And || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1032 | BOp->getOpcode() == Instruction::Or)) { |
| 1033 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1034 | // If the compares in later blocks need to use values not currently |
| 1035 | // exported from this block, export them now. This block should always |
| 1036 | // be the first entry. |
| 1037 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
| 1038 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1039 | // Allow some cases to be rejected. |
| 1040 | if (ShouldEmitAsBranches(SwitchCases)) { |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1041 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1042 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1043 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1044 | } |
| 1045 | |
| 1046 | // Emit the branch for this block. |
| 1047 | visitSwitchCase(SwitchCases[0]); |
| 1048 | SwitchCases.erase(SwitchCases.begin()); |
| 1049 | return; |
Chris Lattner | 5a145f0 | 2006-10-29 18:23:37 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1052 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1053 | // SwitchCases. |
| 1054 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1055 | CurMBB->getParent()->getBasicBlockList().erase(SwitchCases[i].ThisBB); |
| 1056 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1057 | SwitchCases.clear(); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Create a CaseBlock record representing this branch. |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1062 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, CondVal, ConstantBool::getTrue(), |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1063 | Succ0MBB, Succ1MBB, CurMBB); |
| 1064 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1065 | // cond branch. |
| 1066 | visitSwitchCase(CB); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1069 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1070 | /// the binary search tree resulting from lowering a switch instruction. |
| 1071 | void SelectionDAGLowering::visitSwitchCase(SelectionDAGISel::CaseBlock &CB) { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1072 | SDOperand Cond; |
| 1073 | SDOperand CondLHS = getValue(CB.CmpLHS); |
| 1074 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1075 | // Build the setcc now, fold "(X == true)" to X and "(X == false)" to !X to |
| 1076 | // handle common cases produced by branch lowering. |
| 1077 | if (CB.CmpRHS == ConstantBool::getTrue() && CB.CC == ISD::SETEQ) |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1078 | Cond = CondLHS; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1079 | else if (CB.CmpRHS == ConstantBool::getFalse() && CB.CC == ISD::SETEQ) { |
| 1080 | SDOperand True = DAG.getConstant(1, CondLHS.getValueType()); |
| 1081 | Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True); |
| 1082 | } else |
| 1083 | Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1086 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1087 | MachineBasicBlock *NextBlock = 0; |
| 1088 | MachineFunction::iterator BBI = CurMBB; |
| 1089 | if (++BBI != CurMBB->getParent()->end()) |
| 1090 | NextBlock = BBI; |
| 1091 | |
| 1092 | // If the lhs block is the next block, invert the condition so that we can |
| 1093 | // fall through to the lhs instead of the rhs block. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1094 | if (CB.TrueBB == NextBlock) { |
| 1095 | std::swap(CB.TrueBB, CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1096 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 1097 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
| 1098 | } |
| 1099 | SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), Cond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1100 | DAG.getBasicBlock(CB.TrueBB)); |
| 1101 | if (CB.FalseBB == NextBlock) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1102 | DAG.setRoot(BrCond); |
| 1103 | else |
| 1104 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1105 | DAG.getBasicBlock(CB.FalseBB))); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1106 | // Update successor info |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1107 | CurMBB->addSuccessor(CB.TrueBB); |
| 1108 | CurMBB->addSuccessor(CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1111 | void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1112 | // Emit the code for the jump table |
| 1113 | MVT::ValueType PTy = TLI.getPointerTy(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1114 | SDOperand Index = DAG.getCopyFromReg(getRoot(), JT.Reg, PTy); |
| 1115 | SDOperand Table = DAG.getJumpTable(JT.JTI, PTy); |
| 1116 | DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1), |
| 1117 | Table, Index)); |
| 1118 | return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1121 | void SelectionDAGLowering::visitSwitch(SwitchInst &I) { |
| 1122 | // Figure out which block is immediately after the current one. |
| 1123 | MachineBasicBlock *NextBlock = 0; |
| 1124 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1125 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1126 | if (++BBI != CurMBB->getParent()->end()) |
| 1127 | NextBlock = BBI; |
| 1128 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1129 | MachineBasicBlock *Default = FuncInfo.MBBMap[I.getDefaultDest()]; |
| 1130 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1131 | // If there is only the default destination, branch to it if it is not the |
| 1132 | // next basic block. Otherwise, just fall through. |
| 1133 | if (I.getNumOperands() == 2) { |
| 1134 | // Update machine-CFG edges. |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1135 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1136 | // If this is not a fall-through branch, emit the branch. |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1137 | if (Default != NextBlock) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1138 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1139 | DAG.getBasicBlock(Default))); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1141 | CurMBB->addSuccessor(Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | // If there are any non-default case statements, create a vector of Cases |
| 1146 | // representing each one, and sort the vector so that we can efficiently |
| 1147 | // create a binary search tree from them. |
| 1148 | std::vector<Case> Cases; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1149 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1150 | for (unsigned i = 1; i < I.getNumSuccessors(); ++i) { |
| 1151 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[I.getSuccessor(i)]; |
| 1152 | Cases.push_back(Case(I.getSuccessorValue(i), SMBB)); |
| 1153 | } |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1154 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1155 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1156 | |
| 1157 | // Get the Value to be switched on and default basic blocks, which will be |
| 1158 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 1159 | // search tree. |
| 1160 | Value *SV = I.getOperand(0); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Get the MachineFunction which holds the current MBB. This is used during |
| 1163 | // emission of jump tables, and when inserting any additional MBBs necessary |
| 1164 | // to represent the switch. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1165 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1166 | const BasicBlock *LLVMBB = CurMBB->getBasicBlock(); |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1167 | |
| 1168 | // If the switch has few cases (two or less) emit a series of specific |
| 1169 | // tests. |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 1170 | if (Cases.size() < 3) { |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1171 | // TODO: If any two of the cases has the same destination, and if one value |
| 1172 | // is the same as the other, but has one bit unset that the other has set, |
| 1173 | // use bit manipulation to do two compares at once. For example: |
| 1174 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
| 1175 | |
Chris Lattner | b354343 | 2006-10-23 18:38:22 +0000 | [diff] [blame] | 1176 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1177 | if (NextBlock && Default != NextBlock && Cases.back().second != NextBlock) { |
| 1178 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1179 | // branches in this order. See if rearranging a case value would help. |
| 1180 | for (unsigned i = 0, e = Cases.size()-1; i != e; ++i) { |
| 1181 | if (Cases[i].second == NextBlock) { |
| 1182 | std::swap(Cases[i], Cases.back()); |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1188 | // Create a CaseBlock record representing a conditional branch to |
| 1189 | // the Case's target mbb if the value being switched on SV is equal |
| 1190 | // to C. |
| 1191 | MachineBasicBlock *CurBlock = CurMBB; |
| 1192 | for (unsigned i = 0, e = Cases.size(); i != e; ++i) { |
| 1193 | MachineBasicBlock *FallThrough; |
| 1194 | if (i != e-1) { |
| 1195 | FallThrough = new MachineBasicBlock(CurMBB->getBasicBlock()); |
| 1196 | CurMF->getBasicBlockList().insert(BBI, FallThrough); |
| 1197 | } else { |
| 1198 | // If the last case doesn't match, go to the default block. |
| 1199 | FallThrough = Default; |
| 1200 | } |
| 1201 | |
| 1202 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, SV, Cases[i].first, |
| 1203 | Cases[i].second, FallThrough, CurBlock); |
| 1204 | |
| 1205 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1206 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1207 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1208 | // before the next MBB. |
| 1209 | if (CurBlock == CurMBB) |
| 1210 | visitSwitchCase(CB); |
| 1211 | else |
| 1212 | SwitchCases.push_back(CB); |
| 1213 | |
| 1214 | CurBlock = FallThrough; |
| 1215 | } |
| 1216 | return; |
| 1217 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1218 | |
Nate Begeman | 17c275f | 2006-05-08 16:51:36 +0000 | [diff] [blame] | 1219 | // If the switch has more than 5 blocks, and at least 31.25% dense, and the |
| 1220 | // target supports indirect branches, then emit a jump table rather than |
| 1221 | // lowering the switch to a binary tree of conditional branches. |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1222 | if ((TLI.isOperationLegal(ISD::BR_JT, MVT::Other) || |
| 1223 | TLI.isOperationLegal(ISD::BRIND, MVT::Other)) && |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1224 | Cases.size() > 5) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1225 | uint64_t First =cast<ConstantIntegral>(Cases.front().first)->getZExtValue(); |
| 1226 | uint64_t Last = cast<ConstantIntegral>(Cases.back().first)->getZExtValue(); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1227 | double Density = (double)Cases.size() / (double)((Last - First) + 1ULL); |
| 1228 | |
Nate Begeman | 17c275f | 2006-05-08 16:51:36 +0000 | [diff] [blame] | 1229 | if (Density >= 0.3125) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1230 | // Create a new basic block to hold the code for loading the address |
| 1231 | // of the jump table, and jumping to it. Update successor information; |
| 1232 | // we will either branch to the default case for the switch, or the jump |
| 1233 | // table. |
| 1234 | MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB); |
| 1235 | CurMF->getBasicBlockList().insert(BBI, JumpTableBB); |
| 1236 | CurMBB->addSuccessor(Default); |
| 1237 | CurMBB->addSuccessor(JumpTableBB); |
| 1238 | |
| 1239 | // Subtract the lowest switch case value from the value being switched on |
| 1240 | // and conditional branch to default mbb if the result is greater than the |
| 1241 | // difference between smallest and largest cases. |
| 1242 | SDOperand SwitchOp = getValue(SV); |
| 1243 | MVT::ValueType VT = SwitchOp.getValueType(); |
| 1244 | SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1245 | DAG.getConstant(First, VT)); |
| 1246 | |
| 1247 | // The SDNode we just created, which holds the value being switched on |
| 1248 | // minus the the smallest case value, needs to be copied to a virtual |
| 1249 | // register so it can be used as an index into the jump table in a |
| 1250 | // subsequent basic block. This value may be smaller or larger than the |
| 1251 | // target's pointer type, and therefore require extension or truncating. |
| 1252 | if (VT > TLI.getPointerTy()) |
| 1253 | SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB); |
| 1254 | else |
| 1255 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1256 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1257 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1258 | SDOperand CopyTo = DAG.getCopyToReg(getRoot(), JumpTableReg, SwitchOp); |
| 1259 | |
| 1260 | // Emit the range check for the jump table, and branch to the default |
| 1261 | // block for the switch statement if the value being switched on exceeds |
| 1262 | // the largest case in the switch. |
| 1263 | SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultTy(), SUB, |
| 1264 | DAG.getConstant(Last-First,VT), ISD::SETUGT); |
| 1265 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP, |
| 1266 | DAG.getBasicBlock(Default))); |
| 1267 | |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1268 | // Build a vector of destination BBs, corresponding to each target |
| 1269 | // of the jump table. If the value of the jump table slot corresponds to |
| 1270 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1271 | // the default BB. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1272 | std::vector<MachineBasicBlock*> DestBBs; |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1273 | uint64_t TEI = First; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1274 | for (CaseItr ii = Cases.begin(), ee = Cases.end(); ii != ee; ++TEI) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1275 | if (cast<ConstantIntegral>(ii->first)->getZExtValue() == TEI) { |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1276 | DestBBs.push_back(ii->second); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1277 | ++ii; |
| 1278 | } else { |
| 1279 | DestBBs.push_back(Default); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1280 | } |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 1282 | // Update successor info. Add one edge to each unique successor. |
| 1283 | // Vector bool would be better, but vector<bool> is really slow. |
| 1284 | std::vector<unsigned char> SuccsHandled; |
| 1285 | SuccsHandled.resize(CurMBB->getParent()->getNumBlockIDs()); |
| 1286 | |
Chris Lattner | c66764c | 2006-09-10 06:36:57 +0000 | [diff] [blame] | 1287 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 1288 | E = DestBBs.end(); I != E; ++I) { |
| 1289 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1290 | SuccsHandled[(*I)->getNumber()] = true; |
| 1291 | JumpTableBB->addSuccessor(*I); |
| 1292 | } |
| 1293 | } |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Create a jump table index for this jump table, or return an existing |
| 1296 | // one. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1297 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
| 1298 | |
| 1299 | // Set the jump table information so that we can codegen it as a second |
| 1300 | // MachineBasicBlock |
| 1301 | JT.Reg = JumpTableReg; |
| 1302 | JT.JTI = JTI; |
| 1303 | JT.MBB = JumpTableBB; |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 1304 | JT.Default = Default; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1308 | |
| 1309 | // Push the initial CaseRec onto the worklist |
| 1310 | std::vector<CaseRec> CaseVec; |
| 1311 | CaseVec.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 1312 | |
| 1313 | while (!CaseVec.empty()) { |
| 1314 | // Grab a record representing a case range to process off the worklist |
| 1315 | CaseRec CR = CaseVec.back(); |
| 1316 | CaseVec.pop_back(); |
| 1317 | |
| 1318 | // Size is the number of Cases represented by this range. If Size is 1, |
| 1319 | // then we are processing a leaf of the binary search tree. Otherwise, |
| 1320 | // we need to pick a pivot, and push left and right ranges onto the |
| 1321 | // worklist. |
| 1322 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1323 | |
| 1324 | if (Size == 1) { |
| 1325 | // Create a CaseBlock record representing a conditional branch to |
| 1326 | // the Case's target mbb if the value being switched on SV is equal |
| 1327 | // to C. Otherwise, branch to default. |
| 1328 | Constant *C = CR.Range.first->first; |
| 1329 | MachineBasicBlock *Target = CR.Range.first->second; |
| 1330 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, SV, C, Target, Default, |
| 1331 | CR.CaseBB); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1332 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1333 | // If the MBB representing the leaf node is the current MBB, then just |
| 1334 | // call visitSwitchCase to emit the code into the current block. |
| 1335 | // Otherwise, push the CaseBlock onto the vector to be later processed |
| 1336 | // by SDISel, and insert the node's MBB before the next MBB. |
| 1337 | if (CR.CaseBB == CurMBB) |
| 1338 | visitSwitchCase(CB); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1339 | else |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1340 | SwitchCases.push_back(CB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1341 | } else { |
| 1342 | // split case range at pivot |
| 1343 | CaseItr Pivot = CR.Range.first + (Size / 2); |
| 1344 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1345 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1346 | Constant *C = Pivot->first; |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1347 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1348 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1349 | // We know that we branch to the LHS if the Value being switched on is |
| 1350 | // less than the Pivot value, C. We use this to optimize our binary |
| 1351 | // tree a bit, by recognizing that if SV is greater than or equal to the |
| 1352 | // LHS's Case Value, and that Case Value is exactly one less than the |
| 1353 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1354 | // rather than creating a leaf node for it. |
| 1355 | if ((LHSR.second - LHSR.first) == 1 && |
| 1356 | LHSR.first->first == CR.GE && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1357 | cast<ConstantIntegral>(C)->getZExtValue() == |
| 1358 | (cast<ConstantIntegral>(CR.GE)->getZExtValue() + 1ULL)) { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1359 | TrueBB = LHSR.first->second; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1360 | } else { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1361 | TrueBB = new MachineBasicBlock(LLVMBB); |
| 1362 | CurMF->getBasicBlockList().insert(BBI, TrueBB); |
| 1363 | CaseVec.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1364 | } |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1365 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1366 | // Similar to the optimization above, if the Value being switched on is |
| 1367 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1368 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1369 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1370 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1371 | cast<ConstantIntegral>(RHSR.first->first)->getZExtValue() == |
| 1372 | (cast<ConstantIntegral>(CR.LT)->getZExtValue() - 1ULL)) { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1373 | FalseBB = RHSR.first->second; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1374 | } else { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1375 | FalseBB = new MachineBasicBlock(LLVMBB); |
| 1376 | CurMF->getBasicBlockList().insert(BBI, FalseBB); |
| 1377 | CaseVec.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1378 | } |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1379 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1380 | // Create a CaseBlock record representing a conditional branch to |
| 1381 | // the LHS node if the value being switched on SV is less than C. |
| 1382 | // Otherwise, branch to LHS. |
Reid Spencer | 8c57dfb | 2007-01-03 04:25:33 +0000 | [diff] [blame] | 1383 | ISD::CondCode CC = ISD::SETLT; |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1384 | SelectionDAGISel::CaseBlock CB(CC, SV, C, TrueBB, FalseBB, CR.CaseBB); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1385 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1386 | if (CR.CaseBB == CurMBB) |
| 1387 | visitSwitchCase(CB); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1388 | else |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1389 | SwitchCases.push_back(CB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 1394 | void SelectionDAGLowering::visitSub(User &I) { |
| 1395 | // -0.0 - X --> fneg |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1396 | if (I.getType()->isFloatingPoint()) { |
| 1397 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 1398 | if (CFP->isExactlyValue(-0.0)) { |
| 1399 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 1400 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 1401 | return; |
| 1402 | } |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1403 | visitFPBinary(I, ISD::FSUB, ISD::VSUB); |
| 1404 | } else |
| 1405 | visitIntBinary(I, ISD::SUB, ISD::VSUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1408 | void |
| 1409 | SelectionDAGLowering::visitIntBinary(User &I, unsigned IntOp, unsigned VecOp) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1410 | const Type *Ty = I.getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1411 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1412 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 1413 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1414 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1415 | SDOperand Num = DAG.getConstant(PTy->getNumElements(), MVT::i32); |
| 1416 | SDOperand Typ = DAG.getValueType(TLI.getValueType(PTy->getElementType())); |
| 1417 | setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1418 | } else { |
| 1419 | setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2)); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | void |
| 1424 | SelectionDAGLowering::visitFPBinary(User &I, unsigned FPOp, unsigned VecOp) { |
| 1425 | const Type *Ty = I.getType(); |
| 1426 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1427 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 1428 | |
| 1429 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
| 1430 | SDOperand Num = DAG.getConstant(PTy->getNumElements(), MVT::i32); |
| 1431 | SDOperand Typ = DAG.getValueType(TLI.getValueType(PTy->getElementType())); |
| 1432 | setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ)); |
| 1433 | } else { |
| 1434 | setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2)); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1435 | } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 1436 | } |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 1437 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 1438 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 1439 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1440 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 1441 | |
| 1442 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 1443 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1444 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 1445 | } |
| 1446 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1447 | void SelectionDAGLowering::visitICmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1448 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 1449 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 1450 | predicate = IC->getPredicate(); |
| 1451 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 1452 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 1453 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1454 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1455 | ISD::CondCode Opcode; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1456 | switch (predicate) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1457 | case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break; |
| 1458 | case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break; |
| 1459 | case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break; |
| 1460 | case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break; |
| 1461 | case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break; |
| 1462 | case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break; |
| 1463 | case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break; |
| 1464 | case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break; |
| 1465 | case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break; |
| 1466 | case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break; |
| 1467 | default: |
| 1468 | assert(!"Invalid ICmp predicate value"); |
| 1469 | Opcode = ISD::SETEQ; |
| 1470 | break; |
| 1471 | } |
| 1472 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
| 1473 | } |
| 1474 | |
| 1475 | void SelectionDAGLowering::visitFCmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1476 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 1477 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 1478 | predicate = FC->getPredicate(); |
| 1479 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 1480 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1481 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1482 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1483 | ISD::CondCode Condition, FOC, FPC; |
| 1484 | switch (predicate) { |
| 1485 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1486 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1487 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1488 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1489 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1490 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1491 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1492 | case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break; |
| 1493 | case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break; |
| 1494 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1495 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1496 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1497 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1498 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1499 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1500 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1501 | default: |
| 1502 | assert(!"Invalid FCmp predicate value"); |
| 1503 | FOC = FPC = ISD::SETFALSE; |
| 1504 | break; |
| 1505 | } |
| 1506 | if (FiniteOnlyFPMath()) |
| 1507 | Condition = FOC; |
| 1508 | else |
| 1509 | Condition = FPC; |
| 1510 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | void SelectionDAGLowering::visitSelect(User &I) { |
| 1514 | SDOperand Cond = getValue(I.getOperand(0)); |
| 1515 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 1516 | SDOperand FalseVal = getValue(I.getOperand(2)); |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 1517 | if (!isa<PackedType>(I.getType())) { |
| 1518 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 1519 | TrueVal, FalseVal)); |
| 1520 | } else { |
| 1521 | setValue(&I, DAG.getNode(ISD::VSELECT, MVT::Vector, Cond, TrueVal, FalseVal, |
| 1522 | *(TrueVal.Val->op_end()-2), |
| 1523 | *(TrueVal.Val->op_end()-1))); |
| 1524 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1527 | |
| 1528 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 1529 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 1530 | SDOperand N = getValue(I.getOperand(0)); |
| 1531 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1532 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 1533 | } |
| 1534 | |
| 1535 | void SelectionDAGLowering::visitZExt(User &I) { |
| 1536 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 1537 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 1538 | SDOperand N = getValue(I.getOperand(0)); |
| 1539 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1540 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 1541 | } |
| 1542 | |
| 1543 | void SelectionDAGLowering::visitSExt(User &I) { |
| 1544 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 1545 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 1546 | SDOperand N = getValue(I.getOperand(0)); |
| 1547 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1548 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N)); |
| 1549 | } |
| 1550 | |
| 1551 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 1552 | // FPTrunc is never a no-op cast, no need to check |
| 1553 | SDOperand N = getValue(I.getOperand(0)); |
| 1554 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1555 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N)); |
| 1556 | } |
| 1557 | |
| 1558 | void SelectionDAGLowering::visitFPExt(User &I){ |
| 1559 | // FPTrunc is never a no-op cast, no need to check |
| 1560 | SDOperand N = getValue(I.getOperand(0)); |
| 1561 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1562 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N)); |
| 1563 | } |
| 1564 | |
| 1565 | void SelectionDAGLowering::visitFPToUI(User &I) { |
| 1566 | // FPToUI is never a no-op cast, no need to check |
| 1567 | SDOperand N = getValue(I.getOperand(0)); |
| 1568 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1569 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N)); |
| 1570 | } |
| 1571 | |
| 1572 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 1573 | // FPToSI is never a no-op cast, no need to check |
| 1574 | SDOperand N = getValue(I.getOperand(0)); |
| 1575 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1576 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N)); |
| 1577 | } |
| 1578 | |
| 1579 | void SelectionDAGLowering::visitUIToFP(User &I) { |
| 1580 | // UIToFP is never a no-op cast, no need to check |
| 1581 | SDOperand N = getValue(I.getOperand(0)); |
| 1582 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1583 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N)); |
| 1584 | } |
| 1585 | |
| 1586 | void SelectionDAGLowering::visitSIToFP(User &I){ |
| 1587 | // UIToFP is never a no-op cast, no need to check |
| 1588 | SDOperand N = getValue(I.getOperand(0)); |
| 1589 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1590 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N)); |
| 1591 | } |
| 1592 | |
| 1593 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 1594 | // What to do depends on the size of the integer and the size of the pointer. |
| 1595 | // We can either truncate, zero extend, or no-op, accordingly. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1596 | SDOperand N = getValue(I.getOperand(0)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1597 | MVT::ValueType SrcVT = N.getValueType(); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1598 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1599 | SDOperand Result; |
| 1600 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 1601 | Result = DAG.getNode(ISD::TRUNCATE, DestVT, N); |
| 1602 | else |
| 1603 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 1604 | Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N); |
| 1605 | setValue(&I, Result); |
| 1606 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1607 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1608 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 1609 | // What to do depends on the size of the integer and the size of the pointer. |
| 1610 | // We can either truncate, zero extend, or no-op, accordingly. |
| 1611 | SDOperand N = getValue(I.getOperand(0)); |
| 1612 | MVT::ValueType SrcVT = N.getValueType(); |
| 1613 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1614 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 1615 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 1616 | else |
| 1617 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 1618 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 1619 | } |
| 1620 | |
| 1621 | void SelectionDAGLowering::visitBitCast(User &I) { |
| 1622 | SDOperand N = getValue(I.getOperand(0)); |
| 1623 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1624 | if (DestVT == MVT::Vector) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1625 | // This is a cast to a vector from something else. |
| 1626 | // Get information about the output vector. |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1627 | const PackedType *DestTy = cast<PackedType>(I.getType()); |
| 1628 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 1629 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N, |
| 1630 | DAG.getConstant(DestTy->getNumElements(),MVT::i32), |
| 1631 | DAG.getValueType(EltVT))); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1632 | return; |
| 1633 | } |
| 1634 | MVT::ValueType SrcVT = N.getValueType(); |
| 1635 | if (SrcVT == MVT::Vector) { |
| 1636 | // This is a cast from a vctor to something else. |
| 1637 | // Get information about the input vector. |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1638 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1639 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1640 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1641 | |
| 1642 | // BitCast assures us that source and destination are the same size so this |
| 1643 | // is either a BIT_CONVERT or a no-op. |
| 1644 | if (DestVT != N.getValueType()) |
| 1645 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types |
| 1646 | else |
| 1647 | setValue(&I, N); // noop cast. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 1650 | void SelectionDAGLowering::visitInsertElement(User &I) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1651 | SDOperand InVec = getValue(I.getOperand(0)); |
| 1652 | SDOperand InVal = getValue(I.getOperand(1)); |
| 1653 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 1654 | getValue(I.getOperand(2))); |
| 1655 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1656 | SDOperand Num = *(InVec.Val->op_end()-2); |
| 1657 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 1658 | setValue(&I, DAG.getNode(ISD::VINSERT_VECTOR_ELT, MVT::Vector, |
| 1659 | InVec, InVal, InIdx, Num, Typ)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 1662 | void SelectionDAGLowering::visitExtractElement(User &I) { |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1663 | SDOperand InVec = getValue(I.getOperand(0)); |
| 1664 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 1665 | getValue(I.getOperand(1))); |
| 1666 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 1667 | setValue(&I, DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, |
| 1668 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 1669 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1670 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1671 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
| 1672 | SDOperand V1 = getValue(I.getOperand(0)); |
| 1673 | SDOperand V2 = getValue(I.getOperand(1)); |
| 1674 | SDOperand Mask = getValue(I.getOperand(2)); |
| 1675 | |
| 1676 | SDOperand Num = *(V1.Val->op_end()-2); |
| 1677 | SDOperand Typ = *(V2.Val->op_end()-1); |
| 1678 | setValue(&I, DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
| 1679 | V1, V2, Mask, Num, Typ)); |
| 1680 | } |
| 1681 | |
| 1682 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1683 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 1684 | SDOperand N = getValue(I.getOperand(0)); |
| 1685 | const Type *Ty = I.getOperand(0)->getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1686 | |
| 1687 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 1688 | OI != E; ++OI) { |
| 1689 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1690 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1691 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1692 | if (Field) { |
| 1693 | // N = N + Offset |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1694 | uint64_t Offset = TD->getStructLayout(StTy)->MemberOffsets[Field]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1695 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 1696 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1697 | } |
| 1698 | Ty = StTy->getElementType(Field); |
| 1699 | } else { |
| 1700 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1702 | // If this is a constant subscript, handle it quickly. |
| 1703 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1704 | if (CI->getZExtValue() == 0) continue; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 1705 | uint64_t Offs = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1706 | TD->getTypeSize(Ty)*cast<ConstantInt>(CI)->getZExtValue(); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1707 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 1708 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1709 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1710 | |
| 1711 | // N = N + Idx * ElementSize; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1712 | uint64_t ElementSize = TD->getTypeSize(Ty); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1713 | SDOperand IdxN = getValue(Idx); |
| 1714 | |
| 1715 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 1716 | // it. |
| 1717 | if (IdxN.getValueType() < N.getValueType()) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 1718 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1719 | } else if (IdxN.getValueType() > N.getValueType()) |
| 1720 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 1721 | |
| 1722 | // If this is a multiply by a power of two, turn it into a shl |
| 1723 | // immediately. This is a very common case. |
| 1724 | if (isPowerOf2_64(ElementSize)) { |
| 1725 | unsigned Amt = Log2_64(ElementSize); |
| 1726 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 1727 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1728 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 1729 | continue; |
| 1730 | } |
| 1731 | |
| 1732 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 1733 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 1734 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | setValue(&I, N); |
| 1738 | } |
| 1739 | |
| 1740 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 1741 | // If this is a fixed sized alloca in the entry block of the function, |
| 1742 | // allocate it statically on the stack. |
| 1743 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 1744 | return; // getValue will auto-populate this. |
| 1745 | |
| 1746 | const Type *Ty = I.getAllocatedType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1747 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
| 1748 | unsigned Align = std::max((unsigned)TLI.getTargetData()->getTypeAlignment(Ty), |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 1749 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1750 | |
| 1751 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1752 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1753 | if (IntPtr < AllocSize.getValueType()) |
| 1754 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 1755 | else if (IntPtr > AllocSize.getValueType()) |
| 1756 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1758 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1759 | getIntPtrConstant(TySize)); |
| 1760 | |
| 1761 | // Handle alignment. If the requested alignment is less than or equal to the |
| 1762 | // stack alignment, ignore it and round the size of the allocation up to the |
| 1763 | // stack alignment size. If the size is greater than the stack alignment, we |
| 1764 | // note this in the DYNAMIC_STACKALLOC node. |
| 1765 | unsigned StackAlign = |
| 1766 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1767 | if (Align <= StackAlign) { |
| 1768 | Align = 0; |
| 1769 | // Add SA-1 to the size. |
| 1770 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 1771 | getIntPtrConstant(StackAlign-1)); |
| 1772 | // Mask out the low bits for alignment purposes. |
| 1773 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 1774 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 1775 | } |
| 1776 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1777 | SDOperand Ops[] = { getRoot(), AllocSize, getIntPtrConstant(Align) }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1778 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(), |
| 1779 | MVT::Other); |
| 1780 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1781 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 1782 | |
| 1783 | // Inform the Frame Information that we have just allocated a variable-sized |
| 1784 | // object. |
| 1785 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 1786 | } |
| 1787 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1788 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 1789 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1791 | SDOperand Root; |
| 1792 | if (I.isVolatile()) |
| 1793 | Root = getRoot(); |
| 1794 | else { |
| 1795 | // Do not serialize non-volatile loads against each other. |
| 1796 | Root = DAG.getRoot(); |
| 1797 | } |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1798 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1799 | setValue(&I, getLoadFrom(I.getType(), Ptr, I.getOperand(0), |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1800 | Root, I.isVolatile())); |
| 1801 | } |
| 1802 | |
| 1803 | SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1804 | const Value *SV, SDOperand Root, |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1805 | bool isVolatile) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1806 | SDOperand L; |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 1807 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 1808 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1809 | L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr, |
| 1810 | DAG.getSrcValue(SV)); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1811 | } else { |
Evan Cheng | 0b4f80e | 2006-12-20 01:27:29 +0000 | [diff] [blame] | 1812 | L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SV, 0, isVolatile); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1813 | } |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1815 | if (isVolatile) |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1816 | DAG.setRoot(L.getValue(1)); |
| 1817 | else |
| 1818 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1819 | |
| 1820 | return L; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 1825 | Value *SrcV = I.getOperand(0); |
| 1826 | SDOperand Src = getValue(SrcV); |
| 1827 | SDOperand Ptr = getValue(I.getOperand(1)); |
Evan Cheng | 0b4f80e | 2006-12-20 01:27:29 +0000 | [diff] [blame] | 1828 | DAG.setRoot(DAG.getStore(getRoot(), Src, Ptr, I.getOperand(1), 0, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1829 | I.isVolatile())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1832 | /// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot |
| 1833 | /// access memory and has no other side effects at all. |
| 1834 | static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) { |
| 1835 | #define GET_NO_MEMORY_INTRINSICS |
| 1836 | #include "llvm/Intrinsics.gen" |
| 1837 | #undef GET_NO_MEMORY_INTRINSICS |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 1841 | // IntrinsicOnlyReadsMemory - Return true if the specified intrinsic doesn't |
| 1842 | // have any side-effects or if it only reads memory. |
| 1843 | static bool IntrinsicOnlyReadsMemory(unsigned IntrinsicID) { |
| 1844 | #define GET_SIDE_EFFECT_INFO |
| 1845 | #include "llvm/Intrinsics.gen" |
| 1846 | #undef GET_SIDE_EFFECT_INFO |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1850 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 1851 | /// node. |
| 1852 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
| 1853 | unsigned Intrinsic) { |
Chris Lattner | 7255a54 | 2006-03-24 22:49:42 +0000 | [diff] [blame] | 1854 | bool HasChain = !IntrinsicCannotAccessMemory(Intrinsic); |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 1855 | bool OnlyLoad = HasChain && IntrinsicOnlyReadsMemory(Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Build the operand list. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1858 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 1859 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 1860 | if (OnlyLoad) { |
| 1861 | // We don't need to serialize loads against other loads. |
| 1862 | Ops.push_back(DAG.getRoot()); |
| 1863 | } else { |
| 1864 | Ops.push_back(getRoot()); |
| 1865 | } |
| 1866 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1867 | |
| 1868 | // Add the intrinsic ID as an integer operand. |
| 1869 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 1870 | |
| 1871 | // Add all operands of the call to the operand list. |
| 1872 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1873 | SDOperand Op = getValue(I.getOperand(i)); |
| 1874 | |
| 1875 | // If this is a vector type, force it to the right packed type. |
| 1876 | if (Op.getValueType() == MVT::Vector) { |
| 1877 | const PackedType *OpTy = cast<PackedType>(I.getOperand(i)->getType()); |
| 1878 | MVT::ValueType EltVT = TLI.getValueType(OpTy->getElementType()); |
| 1879 | |
| 1880 | MVT::ValueType VVT = MVT::getVectorType(EltVT, OpTy->getNumElements()); |
| 1881 | assert(VVT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 1882 | Op = DAG.getNode(ISD::VBIT_CONVERT, VVT, Op); |
| 1883 | } |
| 1884 | |
| 1885 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 1886 | "Intrinsic uses a non-legal type?"); |
| 1887 | Ops.push_back(Op); |
| 1888 | } |
| 1889 | |
| 1890 | std::vector<MVT::ValueType> VTs; |
| 1891 | if (I.getType() != Type::VoidTy) { |
| 1892 | MVT::ValueType VT = TLI.getValueType(I.getType()); |
| 1893 | if (VT == MVT::Vector) { |
| 1894 | const PackedType *DestTy = cast<PackedType>(I.getType()); |
| 1895 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 1896 | |
| 1897 | VT = MVT::getVectorType(EltVT, DestTy->getNumElements()); |
| 1898 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 1899 | } |
| 1900 | |
| 1901 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
| 1902 | VTs.push_back(VT); |
| 1903 | } |
| 1904 | if (HasChain) |
| 1905 | VTs.push_back(MVT::Other); |
| 1906 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1907 | const MVT::ValueType *VTList = DAG.getNodeValueTypes(VTs); |
| 1908 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1909 | // Create the node. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1910 | SDOperand Result; |
| 1911 | if (!HasChain) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1912 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(), |
| 1913 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1914 | else if (I.getType() != Type::VoidTy) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1915 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(), |
| 1916 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1917 | else |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1918 | Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(), |
| 1919 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1920 | |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 1921 | if (HasChain) { |
| 1922 | SDOperand Chain = Result.getValue(Result.Val->getNumValues()-1); |
| 1923 | if (OnlyLoad) |
| 1924 | PendingLoads.push_back(Chain); |
| 1925 | else |
| 1926 | DAG.setRoot(Chain); |
| 1927 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1928 | if (I.getType() != Type::VoidTy) { |
| 1929 | if (const PackedType *PTy = dyn_cast<PackedType>(I.getType())) { |
| 1930 | MVT::ValueType EVT = TLI.getValueType(PTy->getElementType()); |
| 1931 | Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result, |
| 1932 | DAG.getConstant(PTy->getNumElements(), MVT::i32), |
| 1933 | DAG.getValueType(EVT)); |
| 1934 | } |
| 1935 | setValue(&I, Result); |
| 1936 | } |
| 1937 | } |
| 1938 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1939 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 1940 | /// we want to emit this as a call to a named external function, return the name |
| 1941 | /// otherwise lower it and return null. |
| 1942 | const char * |
| 1943 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 1944 | switch (Intrinsic) { |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1945 | default: |
| 1946 | // By default, turn this into a target intrinsic node. |
| 1947 | visitTargetIntrinsic(I, Intrinsic); |
| 1948 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1949 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 1950 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 1951 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 1952 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0; |
| 1953 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return 0; |
| 1954 | case Intrinsic::setjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 1955 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1956 | break; |
| 1957 | case Intrinsic::longjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 1958 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1959 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 1960 | case Intrinsic::memcpy_i32: |
| 1961 | case Intrinsic::memcpy_i64: |
| 1962 | visitMemIntrinsic(I, ISD::MEMCPY); |
| 1963 | return 0; |
| 1964 | case Intrinsic::memset_i32: |
| 1965 | case Intrinsic::memset_i64: |
| 1966 | visitMemIntrinsic(I, ISD::MEMSET); |
| 1967 | return 0; |
| 1968 | case Intrinsic::memmove_i32: |
| 1969 | case Intrinsic::memmove_i64: |
| 1970 | visitMemIntrinsic(I, ISD::MEMMOVE); |
| 1971 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1972 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 1973 | case Intrinsic::dbg_stoppoint: { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1974 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1975 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 1976 | if (DebugInfo && SPI.getContext() && DebugInfo->Verify(SPI.getContext())) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1977 | SDOperand Ops[5]; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1979 | Ops[0] = getRoot(); |
| 1980 | Ops[1] = getValue(SPI.getLineValue()); |
| 1981 | Ops[2] = getValue(SPI.getColumnValue()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1982 | |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1983 | DebugInfoDesc *DD = DebugInfo->getDescFor(SPI.getContext()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1984 | assert(DD && "Not a debug information descriptor"); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1985 | CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD); |
| 1986 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1987 | Ops[3] = DAG.getString(CompileUnit->getFileName()); |
| 1988 | Ops[4] = DAG.getString(CompileUnit->getDirectory()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1989 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1990 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops, 5)); |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 1991 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 1993 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1994 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1995 | case Intrinsic::dbg_region_start: { |
| 1996 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 1997 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 1998 | if (DebugInfo && RSI.getContext() && DebugInfo->Verify(RSI.getContext())) { |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1999 | unsigned LabelID = DebugInfo->RecordRegionStart(RSI.getContext()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2000 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, getRoot(), |
| 2001 | DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2004 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2005 | } |
| 2006 | case Intrinsic::dbg_region_end: { |
| 2007 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 2008 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 2009 | if (DebugInfo && REI.getContext() && DebugInfo->Verify(REI.getContext())) { |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2010 | unsigned LabelID = DebugInfo->RecordRegionEnd(REI.getContext()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2011 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, |
| 2012 | getRoot(), DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2015 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2016 | } |
| 2017 | case Intrinsic::dbg_func_start: { |
| 2018 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 2019 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 2020 | if (DebugInfo && FSI.getSubprogram() && |
| 2021 | DebugInfo->Verify(FSI.getSubprogram())) { |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2022 | unsigned LabelID = DebugInfo->RecordRegionStart(FSI.getSubprogram()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2023 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, |
| 2024 | getRoot(), DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2027 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2028 | } |
| 2029 | case Intrinsic::dbg_declare: { |
| 2030 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 2031 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Jim Laskey | bf7637d | 2006-03-28 13:45:20 +0000 | [diff] [blame] | 2032 | if (DebugInfo && DI.getVariable() && DebugInfo->Verify(DI.getVariable())) { |
Jim Laskey | 0892cee | 2006-03-24 09:50:27 +0000 | [diff] [blame] | 2033 | SDOperand AddressOp = getValue(DI.getAddress()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2034 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AddressOp)) |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2035 | DebugInfo->RecordVariable(DI.getVariable(), FI->getIndex()); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | return 0; |
| 2039 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2040 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2041 | case Intrinsic::isunordered_f32: |
| 2042 | case Intrinsic::isunordered_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2043 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 2044 | getValue(I.getOperand(2)), ISD::SETUO)); |
| 2045 | return 0; |
| 2046 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2047 | case Intrinsic::sqrt_f32: |
| 2048 | case Intrinsic::sqrt_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2049 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 2050 | getValue(I.getOperand(1)).getValueType(), |
| 2051 | getValue(I.getOperand(1)))); |
| 2052 | return 0; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2053 | case Intrinsic::powi_f32: |
| 2054 | case Intrinsic::powi_f64: |
| 2055 | setValue(&I, DAG.getNode(ISD::FPOWI, |
| 2056 | getValue(I.getOperand(1)).getValueType(), |
| 2057 | getValue(I.getOperand(1)), |
| 2058 | getValue(I.getOperand(2)))); |
| 2059 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2060 | case Intrinsic::pcmarker: { |
| 2061 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 2062 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 2063 | return 0; |
| 2064 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2065 | case Intrinsic::readcyclecounter: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2066 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2067 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, |
| 2068 | DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2, |
| 2069 | &Op, 1); |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2070 | setValue(&I, Tmp); |
| 2071 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2072 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2073 | } |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2074 | case Intrinsic::bswap_i16: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2075 | case Intrinsic::bswap_i32: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2076 | case Intrinsic::bswap_i64: |
| 2077 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 2078 | getValue(I.getOperand(1)).getValueType(), |
| 2079 | getValue(I.getOperand(1)))); |
| 2080 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2081 | case Intrinsic::cttz_i8: |
| 2082 | case Intrinsic::cttz_i16: |
| 2083 | case Intrinsic::cttz_i32: |
| 2084 | case Intrinsic::cttz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2085 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 2086 | getValue(I.getOperand(1)).getValueType(), |
| 2087 | getValue(I.getOperand(1)))); |
| 2088 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2089 | case Intrinsic::ctlz_i8: |
| 2090 | case Intrinsic::ctlz_i16: |
| 2091 | case Intrinsic::ctlz_i32: |
| 2092 | case Intrinsic::ctlz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2093 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 2094 | getValue(I.getOperand(1)).getValueType(), |
| 2095 | getValue(I.getOperand(1)))); |
| 2096 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2097 | case Intrinsic::ctpop_i8: |
| 2098 | case Intrinsic::ctpop_i16: |
| 2099 | case Intrinsic::ctpop_i32: |
| 2100 | case Intrinsic::ctpop_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2101 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 2102 | getValue(I.getOperand(1)).getValueType(), |
| 2103 | getValue(I.getOperand(1)))); |
| 2104 | return 0; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2105 | case Intrinsic::stacksave: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2106 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2107 | SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, |
| 2108 | DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2109 | setValue(&I, Tmp); |
| 2110 | DAG.setRoot(Tmp.getValue(1)); |
| 2111 | return 0; |
| 2112 | } |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 2113 | case Intrinsic::stackrestore: { |
| 2114 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 2115 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2116 | return 0; |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 2117 | } |
Chris Lattner | ac22c83 | 2005-12-12 22:51:16 +0000 | [diff] [blame] | 2118 | case Intrinsic::prefetch: |
| 2119 | // FIXME: Currently discarding prefetches. |
| 2120 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2125 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 2126 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2127 | if (Function *F = I.getCalledFunction()) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 2128 | if (F->isExternal()) |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2129 | if (unsigned IID = F->getIntrinsicID()) { |
| 2130 | RenameFn = visitIntrinsicCall(I, IID); |
| 2131 | if (!RenameFn) |
| 2132 | return; |
| 2133 | } else { // Not an LLVM intrinsic. |
| 2134 | const std::string &Name = F->getName(); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2135 | if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) { |
| 2136 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 2137 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 2138 | I.getType() == I.getOperand(1)->getType() && |
| 2139 | I.getType() == I.getOperand(2)->getType()) { |
| 2140 | SDOperand LHS = getValue(I.getOperand(1)); |
| 2141 | SDOperand RHS = getValue(I.getOperand(2)); |
| 2142 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(), |
| 2143 | LHS, RHS)); |
| 2144 | return; |
| 2145 | } |
| 2146 | } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 2147 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2148 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 2149 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2150 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 2151 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 2152 | return; |
| 2153 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2154 | } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2155 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2156 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 2157 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2158 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2159 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 2160 | return; |
| 2161 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2162 | } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2163 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2164 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 2165 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2166 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2167 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 2168 | return; |
| 2169 | } |
| 2170 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 2171 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2172 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 2173 | visitInlineAsm(I); |
| 2174 | return; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2175 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2176 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2177 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 2178 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 2179 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 2180 | SDOperand Callee; |
| 2181 | if (!RenameFn) |
| 2182 | Callee = getValue(I.getOperand(0)); |
| 2183 | else |
| 2184 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2185 | TargetLowering::ArgListTy Args; |
| 2186 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2187 | Args.reserve(I.getNumOperands()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2188 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2189 | Value *Arg = I.getOperand(i); |
| 2190 | SDOperand ArgNode = getValue(Arg); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2191 | Entry.Node = ArgNode; Entry.Ty = Arg->getType(); |
| 2192 | Entry.isSigned = FTy->paramHasAttr(i, FunctionType::SExtAttribute); |
| 2193 | Args.push_back(Entry); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2194 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2195 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2196 | std::pair<SDOperand,SDOperand> Result = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2197 | TLI.LowerCallTo(getRoot(), I.getType(), |
| 2198 | FTy->paramHasAttr(0,FunctionType::SExtAttribute), |
| 2199 | FTy->isVarArg(), I.getCallingConv(), I.isTailCall(), |
| 2200 | Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2201 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2202 | setValue(&I, Result.first); |
| 2203 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2206 | SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2207 | SDOperand &Chain, SDOperand &Flag)const{ |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2208 | SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag); |
| 2209 | Chain = Val.getValue(1); |
| 2210 | Flag = Val.getValue(2); |
| 2211 | |
| 2212 | // If the result was expanded, copy from the top part. |
| 2213 | if (Regs.size() > 1) { |
| 2214 | assert(Regs.size() == 2 && |
| 2215 | "Cannot expand to more than 2 elts yet!"); |
| 2216 | SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag); |
Evan Cheng | 693163e | 2006-10-04 22:23:53 +0000 | [diff] [blame] | 2217 | Chain = Hi.getValue(1); |
| 2218 | Flag = Hi.getValue(2); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2219 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 2220 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi); |
| 2221 | else |
| 2222 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2223 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2224 | |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2225 | // Otherwise, if the return value was promoted or extended, truncate it to the |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2226 | // appropriate type. |
| 2227 | if (RegVT == ValueVT) |
| 2228 | return Val; |
| 2229 | |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2230 | if (MVT::isInteger(RegVT)) { |
| 2231 | if (ValueVT < RegVT) |
| 2232 | return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 2233 | else |
| 2234 | return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val); |
| 2235 | } else { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2236 | return DAG.getNode(ISD::FP_ROUND, ValueVT, Val); |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2237 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2240 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 2241 | /// specified value into the registers specified by this object. This uses |
| 2242 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 2243 | void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2244 | SDOperand &Chain, SDOperand &Flag, |
| 2245 | MVT::ValueType PtrVT) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2246 | if (Regs.size() == 1) { |
| 2247 | // If there is a single register and the types differ, this must be |
| 2248 | // a promotion. |
| 2249 | if (RegVT != ValueVT) { |
Chris Lattner | 0c48fd4 | 2006-06-08 18:27:11 +0000 | [diff] [blame] | 2250 | if (MVT::isInteger(RegVT)) { |
| 2251 | if (RegVT < ValueVT) |
| 2252 | Val = DAG.getNode(ISD::TRUNCATE, RegVT, Val); |
| 2253 | else |
| 2254 | Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val); |
| 2255 | } else |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2256 | Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val); |
| 2257 | } |
| 2258 | Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag); |
| 2259 | Flag = Chain.getValue(1); |
| 2260 | } else { |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2261 | std::vector<unsigned> R(Regs); |
| 2262 | if (!DAG.getTargetLoweringInfo().isLittleEndian()) |
| 2263 | std::reverse(R.begin(), R.end()); |
| 2264 | |
| 2265 | for (unsigned i = 0, e = R.size(); i != e; ++i) { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2266 | SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2267 | DAG.getConstant(i, PtrVT)); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2268 | Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2269 | Flag = Chain.getValue(1); |
| 2270 | } |
| 2271 | } |
| 2272 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2273 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2274 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 2275 | /// operand list. This adds the code marker and includes the number of |
| 2276 | /// values added into it. |
| 2277 | void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2278 | std::vector<SDOperand> &Ops) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2279 | Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32)); |
| 2280 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 2281 | Ops.push_back(DAG.getRegister(Regs[i], RegVT)); |
| 2282 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2283 | |
| 2284 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 2285 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 2286 | /// register class for the register. Otherwise, return null. |
| 2287 | static const TargetRegisterClass * |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2288 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 2289 | const TargetLowering &TLI, const MRegisterInfo *MRI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2290 | MVT::ValueType FoundVT = MVT::Other; |
| 2291 | const TargetRegisterClass *FoundRC = 0; |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2292 | for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(), |
| 2293 | E = MRI->regclass_end(); RCI != E; ++RCI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2294 | MVT::ValueType ThisVT = MVT::Other; |
| 2295 | |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2296 | const TargetRegisterClass *RC = *RCI; |
| 2297 | // If none of the the value types for this register class are valid, we |
| 2298 | // 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] | 2299 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 2300 | I != E; ++I) { |
| 2301 | if (TLI.isTypeLegal(*I)) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2302 | // If we have already found this register in a different register class, |
| 2303 | // choose the one with the largest VT specified. For example, on |
| 2304 | // PowerPC, we favor f64 register classes over f32. |
| 2305 | if (FoundVT == MVT::Other || |
| 2306 | MVT::getSizeInBits(FoundVT) < MVT::getSizeInBits(*I)) { |
| 2307 | ThisVT = *I; |
| 2308 | break; |
| 2309 | } |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2313 | if (ThisVT == MVT::Other) continue; |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2315 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 2316 | // frame pointer in functions that need it (due to them not being taken |
| 2317 | // out of allocation, because a variable sized allocation hasn't been seen |
| 2318 | // yet). This is a slight code pessimization, but should still work. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2319 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 2320 | E = RC->allocation_order_end(MF); I != E; ++I) |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2321 | if (*I == Reg) { |
| 2322 | // We found a matching register class. Keep looking at others in case |
| 2323 | // we find one with larger registers that this physreg is also in. |
| 2324 | FoundRC = RC; |
| 2325 | FoundVT = ThisVT; |
| 2326 | break; |
| 2327 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2328 | } |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2329 | return FoundRC; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | RegsForValue SelectionDAGLowering:: |
| 2333 | GetRegistersForValue(const std::string &ConstrCode, |
| 2334 | MVT::ValueType VT, bool isOutReg, bool isInReg, |
| 2335 | std::set<unsigned> &OutputRegs, |
| 2336 | std::set<unsigned> &InputRegs) { |
| 2337 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 2338 | TLI.getRegForInlineAsmConstraint(ConstrCode, VT); |
| 2339 | std::vector<unsigned> Regs; |
| 2340 | |
| 2341 | unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1; |
| 2342 | MVT::ValueType RegVT; |
| 2343 | MVT::ValueType ValueVT = VT; |
| 2344 | |
Chris Lattner | 2a82160 | 2006-11-02 01:41:49 +0000 | [diff] [blame] | 2345 | // If this is a constraint for a specific physical register, like {r17}, |
| 2346 | // assign it now. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2347 | if (PhysReg.first) { |
| 2348 | if (VT == MVT::Other) |
| 2349 | ValueVT = *PhysReg.second->vt_begin(); |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2350 | |
| 2351 | // Get the actual register value type. This is important, because the user |
| 2352 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 2353 | // remember that AX is actually i16 to get the right extension. |
| 2354 | RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2355 | |
| 2356 | // This is a explicit reference to a physical register. |
| 2357 | Regs.push_back(PhysReg.first); |
| 2358 | |
| 2359 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 2360 | if (NumRegs != 1) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2361 | TargetRegisterClass::iterator I = PhysReg.second->begin(); |
| 2362 | TargetRegisterClass::iterator E = PhysReg.second->end(); |
| 2363 | for (; *I != PhysReg.first; ++I) |
| 2364 | assert(I != E && "Didn't find reg!"); |
| 2365 | |
| 2366 | // Already added the first reg. |
| 2367 | --NumRegs; ++I; |
| 2368 | for (; NumRegs; --NumRegs, ++I) { |
| 2369 | assert(I != E && "Ran out of registers to allocate!"); |
| 2370 | Regs.push_back(*I); |
| 2371 | } |
| 2372 | } |
| 2373 | return RegsForValue(Regs, RegVT, ValueVT); |
| 2374 | } |
| 2375 | |
Chris Lattner | 2a82160 | 2006-11-02 01:41:49 +0000 | [diff] [blame] | 2376 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 2377 | // for this reference. |
| 2378 | std::vector<unsigned> RegClassRegs; |
| 2379 | if (PhysReg.second) { |
| 2380 | // If this is an early clobber or tied register, our regalloc doesn't know |
| 2381 | // how to maintain the constraint. If it isn't, go ahead and create vreg |
| 2382 | // and let the regalloc do the right thing. |
| 2383 | if (!isOutReg || !isInReg) { |
| 2384 | if (VT == MVT::Other) |
| 2385 | ValueVT = *PhysReg.second->vt_begin(); |
| 2386 | RegVT = *PhysReg.second->vt_begin(); |
| 2387 | |
| 2388 | // Create the appropriate number of virtual registers. |
| 2389 | SSARegMap *RegMap = DAG.getMachineFunction().getSSARegMap(); |
| 2390 | for (; NumRegs; --NumRegs) |
| 2391 | Regs.push_back(RegMap->createVirtualRegister(PhysReg.second)); |
| 2392 | |
| 2393 | return RegsForValue(Regs, RegVT, ValueVT); |
| 2394 | } |
| 2395 | |
| 2396 | // Otherwise, we can't allocate it. Let the code below figure out how to |
| 2397 | // maintain these constraints. |
| 2398 | RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end()); |
| 2399 | |
| 2400 | } else { |
| 2401 | // This is a reference to a register class that doesn't directly correspond |
| 2402 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 2403 | // registers from the class. |
| 2404 | RegClassRegs = TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT); |
| 2405 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2406 | |
| 2407 | const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo(); |
| 2408 | MachineFunction &MF = *CurMBB->getParent(); |
| 2409 | unsigned NumAllocated = 0; |
| 2410 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 2411 | unsigned Reg = RegClassRegs[i]; |
| 2412 | // See if this register is available. |
| 2413 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 2414 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 2415 | // Make sure we find consecutive registers. |
| 2416 | NumAllocated = 0; |
| 2417 | continue; |
| 2418 | } |
| 2419 | |
| 2420 | // Check to see if this register is allocatable (i.e. don't give out the |
| 2421 | // stack pointer). |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2422 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2423 | if (!RC) { |
| 2424 | // Make sure we find consecutive registers. |
| 2425 | NumAllocated = 0; |
| 2426 | continue; |
| 2427 | } |
| 2428 | |
| 2429 | // Okay, this register is good, we can use it. |
| 2430 | ++NumAllocated; |
| 2431 | |
| 2432 | // If we allocated enough consecutive |
| 2433 | if (NumAllocated == NumRegs) { |
| 2434 | unsigned RegStart = (i-NumAllocated)+1; |
| 2435 | unsigned RegEnd = i+1; |
| 2436 | // Mark all of the allocated registers used. |
| 2437 | for (unsigned i = RegStart; i != RegEnd; ++i) { |
| 2438 | unsigned Reg = RegClassRegs[i]; |
| 2439 | Regs.push_back(Reg); |
| 2440 | if (isOutReg) OutputRegs.insert(Reg); // Mark reg used. |
| 2441 | if (isInReg) InputRegs.insert(Reg); // Mark reg used. |
| 2442 | } |
| 2443 | |
| 2444 | return RegsForValue(Regs, *RC->vt_begin(), VT); |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | // Otherwise, we couldn't allocate enough registers for this. |
| 2449 | return RegsForValue(); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2452 | |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2453 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 2454 | /// |
| 2455 | void SelectionDAGLowering::visitInlineAsm(CallInst &I) { |
| 2456 | InlineAsm *IA = cast<InlineAsm>(I.getOperand(0)); |
| 2457 | |
| 2458 | SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 2459 | MVT::Other); |
| 2460 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2461 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2462 | std::vector<MVT::ValueType> ConstraintVTs; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2463 | |
| 2464 | /// AsmNodeOperands - A list of pairs. The first element is a register, the |
| 2465 | /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set |
| 2466 | /// if it is a def of that register. |
| 2467 | std::vector<SDOperand> AsmNodeOperands; |
| 2468 | AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain |
| 2469 | AsmNodeOperands.push_back(AsmStr); |
| 2470 | |
| 2471 | SDOperand Chain = getRoot(); |
| 2472 | SDOperand Flag; |
| 2473 | |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2474 | // We fully assign registers here at isel time. This is not optimal, but |
| 2475 | // should work. For register classes that correspond to LLVM classes, we |
| 2476 | // could let the LLVM RA do its thing, but we currently don't. Do a prepass |
| 2477 | // over the constraints, collecting fixed registers that we know we can't use. |
| 2478 | std::set<unsigned> OutputRegs, InputRegs; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2479 | unsigned OpNum = 1; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2480 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 2481 | assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!"); |
| 2482 | std::string &ConstraintCode = Constraints[i].Codes[0]; |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2483 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2484 | MVT::ValueType OpVT; |
| 2485 | |
| 2486 | // Compute the value type for each operand and add it to ConstraintVTs. |
| 2487 | switch (Constraints[i].Type) { |
| 2488 | case InlineAsm::isOutput: |
| 2489 | if (!Constraints[i].isIndirectOutput) { |
| 2490 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
| 2491 | OpVT = TLI.getValueType(I.getType()); |
| 2492 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2493 | const Type *OpTy = I.getOperand(OpNum)->getType(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2494 | OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType()); |
| 2495 | OpNum++; // Consumes a call operand. |
| 2496 | } |
| 2497 | break; |
| 2498 | case InlineAsm::isInput: |
| 2499 | OpVT = TLI.getValueType(I.getOperand(OpNum)->getType()); |
| 2500 | OpNum++; // Consumes a call operand. |
| 2501 | break; |
| 2502 | case InlineAsm::isClobber: |
| 2503 | OpVT = MVT::Other; |
| 2504 | break; |
| 2505 | } |
| 2506 | |
| 2507 | ConstraintVTs.push_back(OpVT); |
| 2508 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2509 | if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0) |
| 2510 | continue; // Not assigned a fixed reg. |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2511 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2512 | // Build a list of regs that this operand uses. This always has a single |
| 2513 | // element for promoted/expanded operands. |
| 2514 | RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT, |
| 2515 | false, false, |
| 2516 | OutputRegs, InputRegs); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2517 | |
| 2518 | switch (Constraints[i].Type) { |
| 2519 | case InlineAsm::isOutput: |
| 2520 | // We can't assign any other output to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2521 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2522 | // If this is an early-clobber output, it cannot be assigned to the same |
| 2523 | // value as the input reg. |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2524 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2525 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2526 | break; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2527 | case InlineAsm::isInput: |
| 2528 | // We can't assign any other input to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2529 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2530 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2531 | case InlineAsm::isClobber: |
| 2532 | // Clobbered regs cannot be used as inputs or outputs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2533 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
| 2534 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2535 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2536 | } |
| 2537 | } |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2538 | |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 2539 | // Loop over all of the inputs, copying the operand values into the |
| 2540 | // appropriate registers and processing the output regs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2541 | RegsForValue RetValRegs; |
| 2542 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2543 | OpNum = 1; |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2545 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2546 | assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!"); |
| 2547 | std::string &ConstraintCode = Constraints[i].Codes[0]; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2549 | switch (Constraints[i].Type) { |
| 2550 | case InlineAsm::isOutput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2551 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 2552 | if (ConstraintCode.size() == 1) // not a physreg name. |
| 2553 | CTy = TLI.getConstraintType(ConstraintCode[0]); |
| 2554 | |
| 2555 | if (CTy == TargetLowering::C_Memory) { |
| 2556 | // Memory output. |
| 2557 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
| 2558 | |
| 2559 | // Check that the operand (the address to store to) isn't a float. |
| 2560 | if (!MVT::isInteger(InOperandVal.getValueType())) |
| 2561 | assert(0 && "MATCH FAIL!"); |
| 2562 | |
| 2563 | if (!Constraints[i].isIndirectOutput) |
| 2564 | assert(0 && "MATCH FAIL!"); |
| 2565 | |
| 2566 | OpNum++; // Consumes a call operand. |
| 2567 | |
| 2568 | // Extend/truncate to the right pointer type if needed. |
| 2569 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 2570 | if (InOperandVal.getValueType() < PtrType) |
| 2571 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 2572 | else if (InOperandVal.getValueType() > PtrType) |
| 2573 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 2574 | |
| 2575 | // Add information to the INLINEASM node to know about this output. |
| 2576 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 2577 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 2578 | AsmNodeOperands.push_back(InOperandVal); |
| 2579 | break; |
| 2580 | } |
| 2581 | |
| 2582 | // Otherwise, this is a register output. |
| 2583 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 2584 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2585 | // If this is an early-clobber output, or if there is an input |
| 2586 | // constraint that matches this, we need to reserve the input register |
| 2587 | // so no other inputs allocate to it. |
| 2588 | bool UsesInputRegister = false; |
| 2589 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
| 2590 | UsesInputRegister = true; |
| 2591 | |
| 2592 | // Copy the output from the appropriate register. Find a register that |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2593 | // we can use. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2594 | RegsForValue Regs = |
| 2595 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 2596 | true, UsesInputRegister, |
| 2597 | OutputRegs, InputRegs); |
Chris Lattner | d03f158 | 2006-10-31 07:33:13 +0000 | [diff] [blame] | 2598 | if (Regs.Regs.empty()) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2599 | cerr << "Couldn't allocate output reg for contraint '" |
| 2600 | << ConstraintCode << "'!\n"; |
Chris Lattner | d03f158 | 2006-10-31 07:33:13 +0000 | [diff] [blame] | 2601 | exit(1); |
| 2602 | } |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2603 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2604 | if (!Constraints[i].isIndirectOutput) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2605 | assert(RetValRegs.Regs.empty() && |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2606 | "Cannot have multiple output constraints yet!"); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2607 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2608 | RetValRegs = Regs; |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2609 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2610 | IndirectStoresToEmit.push_back(std::make_pair(Regs, |
| 2611 | I.getOperand(OpNum))); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2612 | OpNum++; // Consumes a call operand. |
| 2613 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2614 | |
| 2615 | // Add information to the INLINEASM node to know that this register is |
| 2616 | // set. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2617 | Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | } |
| 2620 | case InlineAsm::isInput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2621 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2622 | OpNum++; // Consumes a call operand. |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2624 | if (isdigit(ConstraintCode[0])) { // Matching constraint? |
| 2625 | // If this is required to match an output register we have already set, |
| 2626 | // just use its register. |
| 2627 | unsigned OperandNo = atoi(ConstraintCode.c_str()); |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2629 | // Scan until we find the definition we already emitted of this operand. |
| 2630 | // When we find it, create a RegsForValue operand. |
| 2631 | unsigned CurOp = 2; // The first operand. |
| 2632 | for (; OperandNo; --OperandNo) { |
| 2633 | // Advance to the next operand. |
| 2634 | unsigned NumOps = |
| 2635 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
Chris Lattner | a15cf70 | 2006-07-20 19:02:21 +0000 | [diff] [blame] | 2636 | assert(((NumOps & 7) == 2 /*REGDEF*/ || |
| 2637 | (NumOps & 7) == 4 /*MEM*/) && |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2638 | "Skipped past definitions?"); |
| 2639 | CurOp += (NumOps>>3)+1; |
| 2640 | } |
| 2641 | |
| 2642 | unsigned NumOps = |
| 2643 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
| 2644 | assert((NumOps & 7) == 2 /*REGDEF*/ && |
| 2645 | "Skipped past definitions?"); |
| 2646 | |
| 2647 | // Add NumOps>>3 registers to MatchedRegs. |
| 2648 | RegsForValue MatchedRegs; |
| 2649 | MatchedRegs.ValueVT = InOperandVal.getValueType(); |
| 2650 | MatchedRegs.RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 2651 | for (unsigned i = 0, e = NumOps>>3; i != e; ++i) { |
| 2652 | unsigned Reg=cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg(); |
| 2653 | MatchedRegs.Regs.push_back(Reg); |
| 2654 | } |
| 2655 | |
| 2656 | // Use the produced MatchedRegs object to |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2657 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag, |
| 2658 | TLI.getPointerTy()); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2659 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2660 | break; |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2661 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 2662 | |
| 2663 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 2664 | if (ConstraintCode.size() == 1) // not a physreg name. |
| 2665 | CTy = TLI.getConstraintType(ConstraintCode[0]); |
| 2666 | |
| 2667 | if (CTy == TargetLowering::C_Other) { |
Chris Lattner | 53069fb | 2006-10-31 19:41:18 +0000 | [diff] [blame] | 2668 | InOperandVal = TLI.isOperandValidForConstraint(InOperandVal, |
| 2669 | ConstraintCode[0], DAG); |
| 2670 | if (!InOperandVal.Val) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2671 | cerr << "Invalid operand for inline asm constraint '" |
| 2672 | << ConstraintCode << "'!\n"; |
Chris Lattner | 53069fb | 2006-10-31 19:41:18 +0000 | [diff] [blame] | 2673 | exit(1); |
| 2674 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Add information to the INLINEASM node to know about this input. |
| 2677 | unsigned ResOpType = 3 /*IMM*/ | (1 << 3); |
| 2678 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 2679 | AsmNodeOperands.push_back(InOperandVal); |
| 2680 | break; |
| 2681 | } else if (CTy == TargetLowering::C_Memory) { |
| 2682 | // Memory input. |
| 2683 | |
| 2684 | // Check that the operand isn't a float. |
| 2685 | if (!MVT::isInteger(InOperandVal.getValueType())) |
| 2686 | assert(0 && "MATCH FAIL!"); |
| 2687 | |
| 2688 | // Extend/truncate to the right pointer type if needed. |
| 2689 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 2690 | if (InOperandVal.getValueType() < PtrType) |
| 2691 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 2692 | else if (InOperandVal.getValueType() > PtrType) |
| 2693 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 2694 | |
| 2695 | // Add information to the INLINEASM node to know about this input. |
| 2696 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 2697 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 2698 | AsmNodeOperands.push_back(InOperandVal); |
| 2699 | break; |
| 2700 | } |
| 2701 | |
| 2702 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 2703 | |
| 2704 | // Copy the input into the appropriate registers. |
| 2705 | RegsForValue InRegs = |
| 2706 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 2707 | false, true, OutputRegs, InputRegs); |
| 2708 | // FIXME: should be match fail. |
| 2709 | assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!"); |
| 2710 | |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2711 | InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag, TLI.getPointerTy()); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 2712 | |
| 2713 | InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2714 | break; |
| 2715 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2716 | case InlineAsm::isClobber: { |
| 2717 | RegsForValue ClobberedRegs = |
| 2718 | GetRegistersForValue(ConstraintCode, MVT::Other, false, false, |
| 2719 | OutputRegs, InputRegs); |
| 2720 | // Add the clobbered value to the operand list, so that the register |
| 2721 | // allocator is aware that the physreg got clobbered. |
| 2722 | if (!ClobberedRegs.Regs.empty()) |
| 2723 | ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2724 | break; |
| 2725 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2726 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2727 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2728 | |
| 2729 | // Finish up input operands. |
| 2730 | AsmNodeOperands[0] = Chain; |
| 2731 | if (Flag.Val) AsmNodeOperands.push_back(Flag); |
| 2732 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2733 | Chain = DAG.getNode(ISD::INLINEASM, |
| 2734 | DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2735 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2736 | Flag = Chain.getValue(1); |
| 2737 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2738 | // If this asm returns a register value, copy the result from that register |
| 2739 | // and set it as the value of the call. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2740 | if (!RetValRegs.Regs.empty()) |
| 2741 | setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2742 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2743 | std::vector<std::pair<SDOperand, Value*> > StoresToEmit; |
| 2744 | |
| 2745 | // Process indirect outputs, first output all of the flagged copies out of |
| 2746 | // physregs. |
| 2747 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2748 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2749 | Value *Ptr = IndirectStoresToEmit[i].second; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2750 | SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag); |
| 2751 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | // Emit the non-flagged stores from the physregs. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2755 | SmallVector<SDOperand, 8> OutChains; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2756 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 2757 | OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first, |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2758 | getValue(StoresToEmit[i].second), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2759 | StoresToEmit[i].second, 0)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2760 | if (!OutChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2761 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 2762 | &OutChains[0], OutChains.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2763 | DAG.setRoot(Chain); |
| 2764 | } |
| 2765 | |
| 2766 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2767 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 2768 | SDOperand Src = getValue(I.getOperand(0)); |
| 2769 | |
| 2770 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 2771 | |
| 2772 | if (IntPtr < Src.getValueType()) |
| 2773 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 2774 | else if (IntPtr > Src.getValueType()) |
| 2775 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2776 | |
| 2777 | // Scale the source by the type size. |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2778 | uint64_t ElementSize = TD->getTypeSize(I.getType()->getElementType()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2779 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 2780 | Src, getIntPtrConstant(ElementSize)); |
| 2781 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2782 | TargetLowering::ArgListTy Args; |
| 2783 | TargetLowering::ArgListEntry Entry; |
| 2784 | Entry.Node = Src; |
| 2785 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 2786 | Entry.isSigned = false; |
| 2787 | Args.push_back(Entry); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2788 | |
| 2789 | std::pair<SDOperand,SDOperand> Result = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2790 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2791 | DAG.getExternalSymbol("malloc", IntPtr), |
| 2792 | Args, DAG); |
| 2793 | setValue(&I, Result.first); // Pointers always fit in registers |
| 2794 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2798 | TargetLowering::ArgListTy Args; |
| 2799 | TargetLowering::ArgListEntry Entry; |
| 2800 | Entry.Node = getValue(I.getOperand(0)); |
| 2801 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
| 2802 | Entry.isSigned = false; |
| 2803 | Args.push_back(Entry); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2804 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2805 | std::pair<SDOperand,SDOperand> Result = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2806 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 2807 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 2808 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 2811 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 2812 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 2813 | // instructions are special in various ways, which require special support to |
| 2814 | // insert. The specified MachineInstr is created but not inserted into any |
| 2815 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 2816 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 2817 | MachineBasicBlock *MBB) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2818 | cerr << "If a target marks an instruction with " |
| 2819 | << "'usesCustomDAGSchedInserter', it must implement " |
| 2820 | << "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 2821 | abort(); |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 2825 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2826 | DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), |
| 2827 | getValue(I.getOperand(1)), |
| 2828 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2832 | SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(), |
| 2833 | getValue(I.getOperand(0)), |
| 2834 | DAG.getSrcValue(I.getOperand(0))); |
| 2835 | setValue(&I, V); |
| 2836 | DAG.setRoot(V.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2840 | DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(), |
| 2841 | getValue(I.getOperand(1)), |
| 2842 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2846 | DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), |
| 2847 | getValue(I.getOperand(1)), |
| 2848 | getValue(I.getOperand(2)), |
| 2849 | DAG.getSrcValue(I.getOperand(1)), |
| 2850 | DAG.getSrcValue(I.getOperand(2)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 2853 | /// ExpandScalarFormalArgs - Recursively expand the formal_argument node, either |
| 2854 | /// bit_convert it or join a pair of them with a BUILD_PAIR when appropriate. |
| 2855 | static SDOperand ExpandScalarFormalArgs(MVT::ValueType VT, SDNode *Arg, |
| 2856 | unsigned &i, SelectionDAG &DAG, |
| 2857 | TargetLowering &TLI) { |
| 2858 | if (TLI.getTypeAction(VT) != TargetLowering::Expand) |
| 2859 | return SDOperand(Arg, i++); |
| 2860 | |
| 2861 | MVT::ValueType EVT = TLI.getTypeToTransformTo(VT); |
| 2862 | unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT); |
| 2863 | if (NumVals == 1) { |
| 2864 | return DAG.getNode(ISD::BIT_CONVERT, VT, |
| 2865 | ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI)); |
| 2866 | } else if (NumVals == 2) { |
| 2867 | SDOperand Lo = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI); |
| 2868 | SDOperand Hi = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI); |
| 2869 | if (!TLI.isLittleEndian()) |
| 2870 | std::swap(Lo, Hi); |
| 2871 | return DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
| 2872 | } else { |
| 2873 | // Value scalarized into many values. Unimp for now. |
| 2874 | assert(0 && "Cannot expand i64 -> i16 yet!"); |
| 2875 | } |
| 2876 | return SDOperand(); |
| 2877 | } |
| 2878 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2879 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 2880 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 2881 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
| 2882 | /// integrated into SDISel. |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2883 | std::vector<SDOperand> |
| 2884 | TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 2885 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 2886 | std::vector<SDOperand> Ops; |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2887 | Ops.push_back(DAG.getRoot()); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2888 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 2889 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 2890 | |
| 2891 | // Add one result value for each formal argument. |
| 2892 | std::vector<MVT::ValueType> RetVals; |
| 2893 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 2894 | MVT::ValueType VT = getValueType(I->getType()); |
| 2895 | |
| 2896 | switch (getTypeAction(VT)) { |
| 2897 | default: assert(0 && "Unknown type action!"); |
| 2898 | case Legal: |
| 2899 | RetVals.push_back(VT); |
| 2900 | break; |
| 2901 | case Promote: |
| 2902 | RetVals.push_back(getTypeToTransformTo(VT)); |
| 2903 | break; |
| 2904 | case Expand: |
| 2905 | if (VT != MVT::Vector) { |
| 2906 | // If this is a large integer, it needs to be broken up into small |
| 2907 | // integers. Figure out what the destination type is and how many small |
| 2908 | // integers it turns into. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 2909 | MVT::ValueType NVT = getTypeToExpandTo(VT); |
| 2910 | unsigned NumVals = getNumElements(VT); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2911 | for (unsigned i = 0; i != NumVals; ++i) |
| 2912 | RetVals.push_back(NVT); |
| 2913 | } else { |
| 2914 | // Otherwise, this is a vector type. We only support legal vectors |
| 2915 | // right now. |
| 2916 | unsigned NumElems = cast<PackedType>(I->getType())->getNumElements(); |
| 2917 | const Type *EltTy = cast<PackedType>(I->getType())->getElementType(); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 2918 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2919 | // Figure out if there is a Packed type corresponding to this Vector |
| 2920 | // type. If so, convert to the packed type. |
| 2921 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
| 2922 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
| 2923 | RetVals.push_back(TVT); |
| 2924 | } else { |
| 2925 | assert(0 && "Don't support illegal by-val vector arguments yet!"); |
| 2926 | } |
| 2927 | } |
| 2928 | break; |
| 2929 | } |
| 2930 | } |
Evan Cheng | 3b0d286 | 2006-04-25 23:03:35 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2932 | RetVals.push_back(MVT::Other); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2933 | |
| 2934 | // Create the node. |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2935 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, |
| 2936 | DAG.getNodeValueTypes(RetVals), RetVals.size(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2937 | &Ops[0], Ops.size()).Val; |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 2938 | |
| 2939 | DAG.setRoot(SDOperand(Result, Result->getNumValues()-1)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Set up the return result vector. |
| 2942 | Ops.clear(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2943 | const FunctionType *FTy = F.getFunctionType(); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2944 | unsigned i = 0; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2945 | unsigned Idx = 1; |
| 2946 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 2947 | ++I, ++Idx) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2948 | MVT::ValueType VT = getValueType(I->getType()); |
| 2949 | |
| 2950 | switch (getTypeAction(VT)) { |
| 2951 | default: assert(0 && "Unknown type action!"); |
| 2952 | case Legal: |
| 2953 | Ops.push_back(SDOperand(Result, i++)); |
| 2954 | break; |
| 2955 | case Promote: { |
| 2956 | SDOperand Op(Result, i++); |
| 2957 | if (MVT::isInteger(VT)) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2958 | unsigned AssertOp = ISD::AssertSext; |
| 2959 | if (FTy->paramHasAttr(Idx, FunctionType::ZExtAttribute)) |
| 2960 | AssertOp = ISD::AssertZext; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2961 | Op = DAG.getNode(AssertOp, Op.getValueType(), Op, DAG.getValueType(VT)); |
| 2962 | Op = DAG.getNode(ISD::TRUNCATE, VT, Op); |
| 2963 | } else { |
| 2964 | assert(MVT::isFloatingPoint(VT) && "Not int or FP?"); |
| 2965 | Op = DAG.getNode(ISD::FP_ROUND, VT, Op); |
| 2966 | } |
| 2967 | Ops.push_back(Op); |
| 2968 | break; |
| 2969 | } |
| 2970 | case Expand: |
| 2971 | if (VT != MVT::Vector) { |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 2972 | // If this is a large integer or a floating point node that needs to be |
| 2973 | // expanded, it needs to be reassembled from small integers. Figure out |
| 2974 | // what the source elt type is and how many small integers it is. |
| 2975 | Ops.push_back(ExpandScalarFormalArgs(VT, Result, i, DAG, *this)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2976 | } else { |
| 2977 | // Otherwise, this is a vector type. We only support legal vectors |
| 2978 | // right now. |
Evan Cheng | 020c41f | 2006-04-28 05:25:15 +0000 | [diff] [blame] | 2979 | const PackedType *PTy = cast<PackedType>(I->getType()); |
| 2980 | unsigned NumElems = PTy->getNumElements(); |
| 2981 | const Type *EltTy = PTy->getElementType(); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2983 | // Figure out if there is a Packed type corresponding to this Vector |
| 2984 | // type. If so, convert to the packed type. |
| 2985 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 2986 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
Evan Cheng | 020c41f | 2006-04-28 05:25:15 +0000 | [diff] [blame] | 2987 | SDOperand N = SDOperand(Result, i++); |
| 2988 | // Handle copies from generic vectors to registers. |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 2989 | N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N, |
| 2990 | DAG.getConstant(NumElems, MVT::i32), |
| 2991 | DAG.getValueType(getValueType(EltTy))); |
| 2992 | Ops.push_back(N); |
| 2993 | } else { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2994 | assert(0 && "Don't support illegal by-val vector arguments yet!"); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 2995 | abort(); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | break; |
| 2999 | } |
| 3000 | } |
| 3001 | return Ops; |
| 3002 | } |
| 3003 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3004 | |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3005 | /// ExpandScalarCallArgs - Recursively expand call argument node by |
| 3006 | /// bit_converting it or extract a pair of elements from the larger node. |
| 3007 | static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg, |
| 3008 | bool isSigned, |
| 3009 | SmallVector<SDOperand, 32> &Ops, |
| 3010 | SelectionDAG &DAG, |
| 3011 | TargetLowering &TLI) { |
| 3012 | if (TLI.getTypeAction(VT) != TargetLowering::Expand) { |
| 3013 | Ops.push_back(Arg); |
| 3014 | Ops.push_back(DAG.getConstant(isSigned, MVT::i32)); |
| 3015 | return; |
| 3016 | } |
| 3017 | |
| 3018 | MVT::ValueType EVT = TLI.getTypeToTransformTo(VT); |
| 3019 | unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT); |
| 3020 | if (NumVals == 1) { |
| 3021 | Arg = DAG.getNode(ISD::BIT_CONVERT, EVT, Arg); |
| 3022 | ExpandScalarCallArgs(EVT, Arg, isSigned, Ops, DAG, TLI); |
| 3023 | } else if (NumVals == 2) { |
| 3024 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg, |
| 3025 | DAG.getConstant(0, TLI.getPointerTy())); |
| 3026 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg, |
| 3027 | DAG.getConstant(1, TLI.getPointerTy())); |
| 3028 | if (!TLI.isLittleEndian()) |
| 3029 | std::swap(Lo, Hi); |
| 3030 | ExpandScalarCallArgs(EVT, Lo, isSigned, Ops, DAG, TLI); |
| 3031 | ExpandScalarCallArgs(EVT, Hi, isSigned, Ops, DAG, TLI); |
| 3032 | } else { |
| 3033 | // Value scalarized into many values. Unimp for now. |
| 3034 | assert(0 && "Cannot expand i64 -> i16 yet!"); |
| 3035 | } |
| 3036 | } |
| 3037 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3038 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 3039 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 3040 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 3041 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 3042 | std::pair<SDOperand, SDOperand> |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3043 | TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 3044 | bool RetTyIsSigned, bool isVarArg, |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3045 | unsigned CallingConv, bool isTailCall, |
| 3046 | SDOperand Callee, |
| 3047 | ArgListTy &Args, SelectionDAG &DAG) { |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3048 | SmallVector<SDOperand, 32> Ops; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3049 | Ops.push_back(Chain); // Op#0 - Chain |
| 3050 | Ops.push_back(DAG.getConstant(CallingConv, getPointerTy())); // Op#1 - CC |
| 3051 | Ops.push_back(DAG.getConstant(isVarArg, getPointerTy())); // Op#2 - VarArg |
| 3052 | Ops.push_back(DAG.getConstant(isTailCall, getPointerTy())); // Op#3 - Tail |
| 3053 | Ops.push_back(Callee); |
| 3054 | |
| 3055 | // Handle all of the outgoing arguments. |
| 3056 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3057 | MVT::ValueType VT = getValueType(Args[i].Ty); |
| 3058 | SDOperand Op = Args[i].Node; |
| 3059 | bool isSigned = Args[i].isSigned; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3060 | switch (getTypeAction(VT)) { |
| 3061 | default: assert(0 && "Unknown type action!"); |
| 3062 | case Legal: |
| 3063 | Ops.push_back(Op); |
Evan Cheng | d61c482 | 2006-05-26 23:13:20 +0000 | [diff] [blame] | 3064 | Ops.push_back(DAG.getConstant(isSigned, MVT::i32)); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3065 | break; |
| 3066 | case Promote: |
| 3067 | if (MVT::isInteger(VT)) { |
Evan Cheng | f6d62c2 | 2006-05-25 00:55:32 +0000 | [diff] [blame] | 3068 | unsigned ExtOp = isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3069 | Op = DAG.getNode(ExtOp, getTypeToTransformTo(VT), Op); |
| 3070 | } else { |
| 3071 | assert(MVT::isFloatingPoint(VT) && "Not int or FP?"); |
| 3072 | Op = DAG.getNode(ISD::FP_EXTEND, getTypeToTransformTo(VT), Op); |
| 3073 | } |
| 3074 | Ops.push_back(Op); |
Evan Cheng | d61c482 | 2006-05-26 23:13:20 +0000 | [diff] [blame] | 3075 | Ops.push_back(DAG.getConstant(isSigned, MVT::i32)); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3076 | break; |
| 3077 | case Expand: |
| 3078 | if (VT != MVT::Vector) { |
| 3079 | // If this is a large integer, it needs to be broken down into small |
| 3080 | // integers. Figure out what the source elt type is and how many small |
| 3081 | // integers it is. |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3082 | ExpandScalarCallArgs(VT, Op, isSigned, Ops, DAG, *this); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3083 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3084 | // Otherwise, this is a vector type. We only support legal vectors |
| 3085 | // right now. |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3086 | const PackedType *PTy = cast<PackedType>(Args[i].Ty); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3087 | unsigned NumElems = PTy->getNumElements(); |
| 3088 | const Type *EltTy = PTy->getElementType(); |
| 3089 | |
| 3090 | // Figure out if there is a Packed type corresponding to this Vector |
| 3091 | // type. If so, convert to the packed type. |
| 3092 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
Chris Lattner | 1b8daae | 2006-05-17 20:43:21 +0000 | [diff] [blame] | 3093 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
| 3094 | // Insert a VBIT_CONVERT of the MVT::Vector type to the packed type. |
| 3095 | Op = DAG.getNode(ISD::VBIT_CONVERT, TVT, Op); |
| 3096 | Ops.push_back(Op); |
Evan Cheng | d61c482 | 2006-05-26 23:13:20 +0000 | [diff] [blame] | 3097 | Ops.push_back(DAG.getConstant(isSigned, MVT::i32)); |
Chris Lattner | 1b8daae | 2006-05-17 20:43:21 +0000 | [diff] [blame] | 3098 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3099 | assert(0 && "Don't support illegal by-val vector call args yet!"); |
| 3100 | abort(); |
| 3101 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3102 | } |
| 3103 | break; |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | // Figure out the result value types. |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3108 | SmallVector<MVT::ValueType, 4> RetTys; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3109 | |
| 3110 | if (RetTy != Type::VoidTy) { |
| 3111 | MVT::ValueType VT = getValueType(RetTy); |
| 3112 | switch (getTypeAction(VT)) { |
| 3113 | default: assert(0 && "Unknown type action!"); |
| 3114 | case Legal: |
| 3115 | RetTys.push_back(VT); |
| 3116 | break; |
| 3117 | case Promote: |
| 3118 | RetTys.push_back(getTypeToTransformTo(VT)); |
| 3119 | break; |
| 3120 | case Expand: |
| 3121 | if (VT != MVT::Vector) { |
| 3122 | // If this is a large integer, it needs to be reassembled from small |
| 3123 | // integers. Figure out what the source elt type is and how many small |
| 3124 | // integers it is. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 3125 | MVT::ValueType NVT = getTypeToExpandTo(VT); |
| 3126 | unsigned NumVals = getNumElements(VT); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3127 | for (unsigned i = 0; i != NumVals; ++i) |
| 3128 | RetTys.push_back(NVT); |
| 3129 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3130 | // Otherwise, this is a vector type. We only support legal vectors |
| 3131 | // right now. |
| 3132 | const PackedType *PTy = cast<PackedType>(RetTy); |
| 3133 | unsigned NumElems = PTy->getNumElements(); |
| 3134 | const Type *EltTy = PTy->getElementType(); |
| 3135 | |
| 3136 | // Figure out if there is a Packed type corresponding to this Vector |
| 3137 | // type. If so, convert to the packed type. |
| 3138 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
| 3139 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
| 3140 | RetTys.push_back(TVT); |
| 3141 | } else { |
| 3142 | assert(0 && "Don't support illegal by-val vector call results yet!"); |
| 3143 | abort(); |
| 3144 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | RetTys.push_back(MVT::Other); // Always has a chain. |
| 3150 | |
| 3151 | // Finally, create the CALL node. |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3152 | SDOperand Res = DAG.getNode(ISD::CALL, |
| 3153 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 3154 | &Ops[0], Ops.size()); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3155 | |
| 3156 | // This returns a pair of operands. The first element is the |
| 3157 | // return value for the function (if RetTy is not VoidTy). The second |
| 3158 | // element is the outgoing token chain. |
| 3159 | SDOperand ResVal; |
| 3160 | if (RetTys.size() != 1) { |
| 3161 | MVT::ValueType VT = getValueType(RetTy); |
| 3162 | if (RetTys.size() == 2) { |
| 3163 | ResVal = Res; |
| 3164 | |
| 3165 | // If this value was promoted, truncate it down. |
| 3166 | if (ResVal.getValueType() != VT) { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3167 | if (VT == MVT::Vector) { |
| 3168 | // Insert a VBITCONVERT to convert from the packed result type to the |
| 3169 | // MVT::Vector type. |
| 3170 | unsigned NumElems = cast<PackedType>(RetTy)->getNumElements(); |
| 3171 | const Type *EltTy = cast<PackedType>(RetTy)->getElementType(); |
| 3172 | |
| 3173 | // Figure out if there is a Packed type corresponding to this Vector |
| 3174 | // type. If so, convert to the packed type. |
| 3175 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
| 3176 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3177 | // Insert a VBIT_CONVERT of the FORMAL_ARGUMENTS to a |
| 3178 | // "N x PTyElementVT" MVT::Vector type. |
| 3179 | ResVal = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, ResVal, |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 3180 | DAG.getConstant(NumElems, MVT::i32), |
| 3181 | DAG.getValueType(getValueType(EltTy))); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3182 | } else { |
| 3183 | abort(); |
| 3184 | } |
| 3185 | } else if (MVT::isInteger(VT)) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3186 | unsigned AssertOp = ISD::AssertSext; |
| 3187 | if (!RetTyIsSigned) |
| 3188 | AssertOp = ISD::AssertZext; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3189 | ResVal = DAG.getNode(AssertOp, ResVal.getValueType(), ResVal, |
| 3190 | DAG.getValueType(VT)); |
| 3191 | ResVal = DAG.getNode(ISD::TRUNCATE, VT, ResVal); |
| 3192 | } else { |
| 3193 | assert(MVT::isFloatingPoint(VT)); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 3194 | if (getTypeAction(VT) == Expand) |
| 3195 | ResVal = DAG.getNode(ISD::BIT_CONVERT, VT, ResVal); |
| 3196 | else |
| 3197 | ResVal = DAG.getNode(ISD::FP_ROUND, VT, ResVal); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3198 | } |
| 3199 | } |
| 3200 | } else if (RetTys.size() == 3) { |
| 3201 | ResVal = DAG.getNode(ISD::BUILD_PAIR, VT, |
| 3202 | Res.getValue(0), Res.getValue(1)); |
| 3203 | |
| 3204 | } else { |
| 3205 | assert(0 && "Case not handled yet!"); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | return std::make_pair(ResVal, Res.getValue(Res.Val->getNumValues()-1)); |
| 3210 | } |
| 3211 | |
| 3212 | |
| 3213 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3214 | // It is always conservatively correct for llvm.returnaddress and |
| 3215 | // llvm.frameaddress to return 0. |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3216 | // |
| 3217 | // FIXME: Change this to insert a FRAMEADDR/RETURNADDR node, and have that be |
| 3218 | // expanded to 0 if the target wants. |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3219 | std::pair<SDOperand, SDOperand> |
| 3220 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 3221 | unsigned Depth, SelectionDAG &DAG) { |
| 3222 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 3225 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 3226 | assert(0 && "LowerOperation not implemented for this target!"); |
| 3227 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 3228 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3231 | SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op, |
| 3232 | SelectionDAG &DAG) { |
| 3233 | assert(0 && "CustomPromoteOperation not implemented for this target!"); |
| 3234 | abort(); |
| 3235 | return SDOperand(); |
| 3236 | } |
| 3237 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3238 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3239 | unsigned Depth = (unsigned)cast<ConstantInt>(I.getOperand(1))->getZExtValue(); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3240 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 3241 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3242 | setValue(&I, Result.first); |
| 3243 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3246 | /// getMemsetValue - Vectorized representation of the memset value |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3247 | /// operand. |
| 3248 | static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT, |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 3249 | SelectionDAG &DAG) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3250 | MVT::ValueType CurVT = VT; |
| 3251 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { |
| 3252 | uint64_t Val = C->getValue() & 255; |
| 3253 | unsigned Shift = 8; |
| 3254 | while (CurVT != MVT::i8) { |
| 3255 | Val = (Val << Shift) | Val; |
| 3256 | Shift <<= 1; |
| 3257 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3258 | } |
| 3259 | return DAG.getConstant(Val, VT); |
| 3260 | } else { |
| 3261 | Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value); |
| 3262 | unsigned Shift = 8; |
| 3263 | while (CurVT != MVT::i8) { |
| 3264 | Value = |
| 3265 | DAG.getNode(ISD::OR, VT, |
| 3266 | DAG.getNode(ISD::SHL, VT, Value, |
| 3267 | DAG.getConstant(Shift, MVT::i8)), Value); |
| 3268 | Shift <<= 1; |
| 3269 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | return Value; |
| 3273 | } |
| 3274 | } |
| 3275 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3276 | /// getMemsetStringVal - Similar to getMemsetValue. Except this is only |
| 3277 | /// used when a memcpy is turned into a memset when the source is a constant |
| 3278 | /// string ptr. |
| 3279 | static SDOperand getMemsetStringVal(MVT::ValueType VT, |
| 3280 | SelectionDAG &DAG, TargetLowering &TLI, |
| 3281 | std::string &Str, unsigned Offset) { |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3282 | uint64_t Val = 0; |
| 3283 | unsigned MSB = getSizeInBits(VT) / 8; |
| 3284 | if (TLI.isLittleEndian()) |
| 3285 | Offset = Offset + MSB - 1; |
| 3286 | for (unsigned i = 0; i != MSB; ++i) { |
Evan Cheng | a5a57d6 | 2006-11-29 01:38:07 +0000 | [diff] [blame] | 3287 | Val = (Val << 8) | (unsigned char)Str[Offset]; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3288 | Offset += TLI.isLittleEndian() ? -1 : 1; |
| 3289 | } |
| 3290 | return DAG.getConstant(Val, VT); |
| 3291 | } |
| 3292 | |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3293 | /// getMemBasePlusOffset - Returns base and offset node for the |
| 3294 | static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset, |
| 3295 | SelectionDAG &DAG, TargetLowering &TLI) { |
| 3296 | MVT::ValueType VT = Base.getValueType(); |
| 3297 | return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT)); |
| 3298 | } |
| 3299 | |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3300 | /// MeetsMaxMemopRequirement - Determines if the number of memory ops required |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3301 | /// to replace the memset / memcpy is below the threshold. It also returns the |
| 3302 | /// types of the sequence of memory ops to perform memset / memcpy. |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3303 | static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps, |
| 3304 | unsigned Limit, uint64_t Size, |
| 3305 | unsigned Align, TargetLowering &TLI) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3306 | MVT::ValueType VT; |
| 3307 | |
| 3308 | if (TLI.allowsUnalignedMemoryAccesses()) { |
| 3309 | VT = MVT::i64; |
| 3310 | } else { |
| 3311 | switch (Align & 7) { |
| 3312 | case 0: |
| 3313 | VT = MVT::i64; |
| 3314 | break; |
| 3315 | case 4: |
| 3316 | VT = MVT::i32; |
| 3317 | break; |
| 3318 | case 2: |
| 3319 | VT = MVT::i16; |
| 3320 | break; |
| 3321 | default: |
| 3322 | VT = MVT::i8; |
| 3323 | break; |
| 3324 | } |
| 3325 | } |
| 3326 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3327 | MVT::ValueType LVT = MVT::i64; |
| 3328 | while (!TLI.isTypeLegal(LVT)) |
| 3329 | LVT = (MVT::ValueType)((unsigned)LVT - 1); |
| 3330 | assert(MVT::isInteger(LVT)); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3331 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3332 | if (VT > LVT) |
| 3333 | VT = LVT; |
| 3334 | |
Evan Cheng | dea7245 | 2006-02-14 23:05:54 +0000 | [diff] [blame] | 3335 | unsigned NumMemOps = 0; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3336 | while (Size != 0) { |
| 3337 | unsigned VTSize = getSizeInBits(VT) / 8; |
| 3338 | while (VTSize > Size) { |
| 3339 | VT = (MVT::ValueType)((unsigned)VT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3340 | VTSize >>= 1; |
| 3341 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3342 | assert(MVT::isInteger(VT)); |
| 3343 | |
| 3344 | if (++NumMemOps > Limit) |
| 3345 | return false; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3346 | MemOps.push_back(VT); |
| 3347 | Size -= VTSize; |
| 3348 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3349 | |
| 3350 | return true; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 3353 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3354 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 3355 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 3356 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 3357 | SDOperand Op4 = getValue(I.getOperand(4)); |
| 3358 | unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue(); |
| 3359 | if (Align == 0) Align = 1; |
| 3360 | |
| 3361 | if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) { |
| 3362 | std::vector<MVT::ValueType> MemOps; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3363 | |
| 3364 | // Expand memset / memcpy to a series of load / store ops |
| 3365 | // if the size operand falls below a certain threshold. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3366 | SmallVector<SDOperand, 8> OutChains; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3367 | switch (Op) { |
Evan Cheng | ac940ab | 2006-02-14 19:45:56 +0000 | [diff] [blame] | 3368 | default: break; // Do nothing for now. |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3369 | case ISD::MEMSET: { |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3370 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(), |
| 3371 | Size->getValue(), Align, TLI)) { |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3372 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3373 | unsigned Offset = 0; |
| 3374 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 3375 | MVT::ValueType VT = MemOps[i]; |
| 3376 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 3377 | SDOperand Value = getMemsetValue(Op2, VT, DAG); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3378 | SDOperand Store = DAG.getStore(getRoot(), Value, |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3379 | getMemBasePlusOffset(Op1, Offset, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3380 | I.getOperand(1), Offset); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3381 | OutChains.push_back(Store); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3382 | Offset += VTSize; |
| 3383 | } |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3384 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3385 | break; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3386 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3387 | case ISD::MEMCPY: { |
| 3388 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(), |
| 3389 | Size->getValue(), Align, TLI)) { |
| 3390 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3391 | unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3392 | GlobalAddressSDNode *G = NULL; |
| 3393 | std::string Str; |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3394 | bool CopyFromStr = false; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3395 | |
| 3396 | if (Op2.getOpcode() == ISD::GlobalAddress) |
| 3397 | G = cast<GlobalAddressSDNode>(Op2); |
| 3398 | else if (Op2.getOpcode() == ISD::ADD && |
| 3399 | Op2.getOperand(0).getOpcode() == ISD::GlobalAddress && |
| 3400 | Op2.getOperand(1).getOpcode() == ISD::Constant) { |
| 3401 | G = cast<GlobalAddressSDNode>(Op2.getOperand(0)); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3402 | SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue(); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3403 | } |
| 3404 | if (G) { |
| 3405 | GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal()); |
Evan Cheng | f3e486e | 2006-11-29 01:58:12 +0000 | [diff] [blame] | 3406 | if (GV && GV->isConstant()) { |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 3407 | Str = GV->getStringValue(false); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3408 | if (!Str.empty()) { |
| 3409 | CopyFromStr = true; |
| 3410 | SrcOff += SrcDelta; |
| 3411 | } |
| 3412 | } |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3415 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 3416 | MVT::ValueType VT = MemOps[i]; |
| 3417 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3418 | SDOperand Value, Chain, Store; |
| 3419 | |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3420 | if (CopyFromStr) { |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3421 | Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff); |
| 3422 | Chain = getRoot(); |
| 3423 | Store = |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3424 | DAG.getStore(Chain, Value, |
| 3425 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3426 | I.getOperand(1), DstOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3427 | } else { |
| 3428 | Value = DAG.getLoad(VT, getRoot(), |
| 3429 | getMemBasePlusOffset(Op2, SrcOff, DAG, TLI), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3430 | I.getOperand(2), SrcOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3431 | Chain = Value.getValue(1); |
| 3432 | Store = |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3433 | DAG.getStore(Chain, Value, |
| 3434 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3435 | I.getOperand(1), DstOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3436 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3437 | OutChains.push_back(Store); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3438 | SrcOff += VTSize; |
| 3439 | DstOff += VTSize; |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3440 | } |
| 3441 | } |
| 3442 | break; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | if (!OutChains.empty()) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3447 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3448 | &OutChains[0], OutChains.size())); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3449 | return; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3453 | DAG.setRoot(DAG.getNode(Op, MVT::Other, getRoot(), Op1, Op2, Op3, Op4)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 3456 | //===----------------------------------------------------------------------===// |
| 3457 | // SelectionDAGISel code |
| 3458 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3459 | |
| 3460 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 3461 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 3462 | } |
| 3463 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 3464 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 3465 | // FIXME: we only modify the CFG to split critical edges. This |
| 3466 | // updates dom and loop info. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 3467 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 3468 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3471 | /// OptimizeNoopCopyExpression - We have determined that the specified cast |
| 3472 | /// instruction is a noop copy (e.g. it's casting from one pointer type to |
| 3473 | /// another, int->uint, or int->sbyte on PPC. |
| 3474 | /// |
| 3475 | /// Return true if any changes are made. |
| 3476 | static bool OptimizeNoopCopyExpression(CastInst *CI) { |
| 3477 | BasicBlock *DefBB = CI->getParent(); |
| 3478 | |
| 3479 | /// InsertedCasts - Only insert a cast in each block once. |
| 3480 | std::map<BasicBlock*, CastInst*> InsertedCasts; |
| 3481 | |
| 3482 | bool MadeChange = false; |
| 3483 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
| 3484 | UI != E; ) { |
| 3485 | Use &TheUse = UI.getUse(); |
| 3486 | Instruction *User = cast<Instruction>(*UI); |
| 3487 | |
| 3488 | // Figure out which BB this cast is used in. For PHI's this is the |
| 3489 | // appropriate predecessor block. |
| 3490 | BasicBlock *UserBB = User->getParent(); |
| 3491 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 3492 | unsigned OpVal = UI.getOperandNo()/2; |
| 3493 | UserBB = PN->getIncomingBlock(OpVal); |
| 3494 | } |
| 3495 | |
| 3496 | // Preincrement use iterator so we don't invalidate it. |
| 3497 | ++UI; |
| 3498 | |
| 3499 | // If this user is in the same block as the cast, don't change the cast. |
| 3500 | if (UserBB == DefBB) continue; |
| 3501 | |
| 3502 | // If we have already inserted a cast into this block, use it. |
| 3503 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 3504 | |
| 3505 | if (!InsertedCast) { |
| 3506 | BasicBlock::iterator InsertPt = UserBB->begin(); |
| 3507 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 3508 | |
| 3509 | InsertedCast = |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 3510 | CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
| 3511 | InsertPt); |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3512 | MadeChange = true; |
| 3513 | } |
| 3514 | |
| 3515 | // Replace a use of the cast with a use of the new casat. |
| 3516 | TheUse = InsertedCast; |
| 3517 | } |
| 3518 | |
| 3519 | // If we removed all uses, nuke the cast. |
| 3520 | if (CI->use_empty()) |
| 3521 | CI->eraseFromParent(); |
| 3522 | |
| 3523 | return MadeChange; |
| 3524 | } |
| 3525 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3526 | /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset, |
| 3527 | /// casting to the type of GEPI. |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3528 | static Instruction *InsertGEPComputeCode(Instruction *&V, BasicBlock *BB, |
| 3529 | Instruction *GEPI, Value *Ptr, |
| 3530 | Value *PtrOffset) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3531 | if (V) return V; // Already computed. |
| 3532 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3533 | // Figure out the insertion point |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3534 | BasicBlock::iterator InsertPt; |
| 3535 | if (BB == GEPI->getParent()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3536 | // If GEP is already inserted into BB, insert right after the GEP. |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3537 | InsertPt = GEPI; |
| 3538 | ++InsertPt; |
| 3539 | } else { |
| 3540 | // Otherwise, insert at the top of BB, after any PHI nodes |
| 3541 | InsertPt = BB->begin(); |
| 3542 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 3543 | } |
| 3544 | |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 3545 | // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into |
| 3546 | // BB so that there is only one value live across basic blocks (the cast |
| 3547 | // operand). |
| 3548 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 3549 | if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType())) |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 3550 | Ptr = CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), |
| 3551 | "", InsertPt); |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 3552 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3553 | // Add the offset, cast it to the right type. |
| 3554 | Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3555 | // Ptr is an integer type, GEPI is pointer type ==> IntToPtr |
| 3556 | return V = CastInst::create(Instruction::IntToPtr, Ptr, GEPI->getType(), |
| 3557 | "", InsertPt); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3560 | /// ReplaceUsesOfGEPInst - Replace all uses of RepPtr with inserted code to |
| 3561 | /// compute its value. The RepPtr value can be computed with Ptr+PtrOffset. One |
| 3562 | /// trivial way of doing this would be to evaluate Ptr+PtrOffset in RepPtr's |
| 3563 | /// block, then ReplaceAllUsesWith'ing everything. However, we would prefer to |
| 3564 | /// sink PtrOffset into user blocks where doing so will likely allow us to fold |
| 3565 | /// the constant add into a load or store instruction. Additionally, if a user |
| 3566 | /// is a pointer-pointer cast, we look through it to find its users. |
| 3567 | static void ReplaceUsesOfGEPInst(Instruction *RepPtr, Value *Ptr, |
| 3568 | Constant *PtrOffset, BasicBlock *DefBB, |
| 3569 | GetElementPtrInst *GEPI, |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3570 | std::map<BasicBlock*,Instruction*> &InsertedExprs) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3571 | while (!RepPtr->use_empty()) { |
| 3572 | Instruction *User = cast<Instruction>(RepPtr->use_back()); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3573 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3574 | // If the user is a Pointer-Pointer cast, recurse. Only BitCast can be |
| 3575 | // used for a Pointer-Pointer cast. |
| 3576 | if (isa<BitCastInst>(User)) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3577 | ReplaceUsesOfGEPInst(User, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3578 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3579 | // Drop the use of RepPtr. The cast is dead. Don't delete it now, else we |
| 3580 | // could invalidate an iterator. |
| 3581 | User->setOperand(0, UndefValue::get(RepPtr->getType())); |
| 3582 | continue; |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3585 | // If this is a load of the pointer, or a store through the pointer, emit |
| 3586 | // the increment into the load/store block. |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3587 | Instruction *NewVal; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3588 | if (isa<LoadInst>(User) || |
| 3589 | (isa<StoreInst>(User) && User->getOperand(0) != RepPtr)) { |
| 3590 | NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], |
| 3591 | User->getParent(), GEPI, |
| 3592 | Ptr, PtrOffset); |
| 3593 | } else { |
| 3594 | // If this use is not foldable into the addressing mode, use a version |
| 3595 | // emitted in the GEP block. |
| 3596 | NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, |
| 3597 | Ptr, PtrOffset); |
| 3598 | } |
| 3599 | |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3600 | if (GEPI->getType() != RepPtr->getType()) { |
| 3601 | BasicBlock::iterator IP = NewVal; |
| 3602 | ++IP; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3603 | // NewVal must be a GEP which must be pointer type, so BitCast |
| 3604 | NewVal = new BitCastInst(NewVal, RepPtr->getType(), "", IP); |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3605 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3606 | User->replaceUsesOfWith(RepPtr, NewVal); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3609 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3610 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3611 | /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction |
| 3612 | /// selection, we want to be a bit careful about some things. In particular, if |
| 3613 | /// we have a GEP instruction that is used in a different block than it is |
| 3614 | /// defined, the addressing expression of the GEP cannot be folded into loads or |
| 3615 | /// stores that use it. In this case, decompose the GEP and move constant |
| 3616 | /// indices into blocks that use it. |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3617 | static bool OptimizeGEPExpression(GetElementPtrInst *GEPI, |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3618 | const TargetData *TD) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3619 | // If this GEP is only used inside the block it is defined in, there is no |
| 3620 | // need to rewrite it. |
| 3621 | bool isUsedOutsideDefBB = false; |
| 3622 | BasicBlock *DefBB = GEPI->getParent(); |
| 3623 | for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); |
| 3624 | UI != E; ++UI) { |
| 3625 | if (cast<Instruction>(*UI)->getParent() != DefBB) { |
| 3626 | isUsedOutsideDefBB = true; |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3630 | if (!isUsedOutsideDefBB) return false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3631 | |
| 3632 | // If this GEP has no non-zero constant indices, there is nothing we can do, |
| 3633 | // ignore it. |
| 3634 | bool hasConstantIndex = false; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3635 | bool hasVariableIndex = false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3636 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 3637 | E = GEPI->op_end(); OI != E; ++OI) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3638 | if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3639 | if (CI->getZExtValue()) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3640 | hasConstantIndex = true; |
| 3641 | break; |
| 3642 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3643 | } else { |
| 3644 | hasVariableIndex = true; |
| 3645 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3646 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3647 | |
| 3648 | // If this is a "GEP X, 0, 0, 0", turn this into a cast. |
| 3649 | if (!hasConstantIndex && !hasVariableIndex) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3650 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 3651 | Value *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3652 | GEPI->getName(), GEPI); |
| 3653 | GEPI->replaceAllUsesWith(NC); |
| 3654 | GEPI->eraseFromParent(); |
| 3655 | return true; |
| 3656 | } |
| 3657 | |
Chris Lattner | 3802c25 | 2005-12-11 09:05:13 +0000 | [diff] [blame] | 3658 | // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses. |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3659 | if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) |
| 3660 | return false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3661 | |
| 3662 | // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the |
| 3663 | // constant offset (which we now know is non-zero) and deal with it later. |
| 3664 | uint64_t ConstantOffset = 0; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3665 | const Type *UIntPtrTy = TD->getIntPtrType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3666 | Value *Ptr = new PtrToIntInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3667 | const Type *Ty = GEPI->getOperand(0)->getType(); |
| 3668 | |
| 3669 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 3670 | E = GEPI->op_end(); OI != E; ++OI) { |
| 3671 | Value *Idx = *OI; |
| 3672 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3673 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3674 | if (Field) |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3675 | ConstantOffset += TD->getStructLayout(StTy)->MemberOffsets[Field]; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3676 | Ty = StTy->getElementType(Field); |
| 3677 | } else { |
| 3678 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 3679 | |
| 3680 | // Handle constant subscripts. |
| 3681 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3682 | if (CI->getZExtValue() == 0) continue; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3683 | ConstantOffset += (int64_t)TD->getTypeSize(Ty)*CI->getSExtValue(); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3684 | continue; |
| 3685 | } |
| 3686 | |
| 3687 | // Ptr = Ptr + Idx * ElementSize; |
| 3688 | |
| 3689 | // Cast Idx to UIntPtrTy if needed. |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 3690 | Idx = CastInst::createIntegerCast(Idx, UIntPtrTy, true/*SExt*/, "", GEPI); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3691 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3692 | uint64_t ElementSize = TD->getTypeSize(Ty); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3693 | // Mask off bits that should not be set. |
| 3694 | ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3695 | Constant *SizeCst = ConstantInt::get(UIntPtrTy, ElementSize); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3696 | |
| 3697 | // Multiply by the element size and add to the base. |
| 3698 | Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI); |
| 3699 | Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI); |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | // Make sure that the offset fits in uintptr_t. |
| 3704 | ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3705 | Constant *PtrOffset = ConstantInt::get(UIntPtrTy, ConstantOffset); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3706 | |
| 3707 | // Okay, we have now emitted all of the variable index parts to the BB that |
| 3708 | // the GEP is defined in. Loop over all of the using instructions, inserting |
| 3709 | // an "add Ptr, ConstantOffset" into each block that uses it and update the |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 3710 | // instruction to use the newly computed value, making GEPI dead. When the |
| 3711 | // user is a load or store instruction address, we emit the add into the user |
| 3712 | // block, otherwise we use a canonical version right next to the gep (these |
| 3713 | // won't be foldable as addresses, so we might as well share the computation). |
| 3714 | |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3715 | std::map<BasicBlock*,Instruction*> InsertedExprs; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3716 | ReplaceUsesOfGEPInst(GEPI, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3717 | |
| 3718 | // Finally, the GEP is dead, remove it. |
| 3719 | GEPI->eraseFromParent(); |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3720 | |
| 3721 | return true; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 3724 | |
| 3725 | /// SplitEdgeNicely - Split the critical edge from TI to it's specified |
| 3726 | /// successor if it will improve codegen. We only do this if the successor has |
| 3727 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 3728 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 3729 | /// instead of introducing a new block. |
| 3730 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) { |
| 3731 | BasicBlock *TIBB = TI->getParent(); |
| 3732 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 3733 | assert(isa<PHINode>(Dest->begin()) && |
| 3734 | "This should only be called if Dest has a PHI!"); |
| 3735 | |
| 3736 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 3737 | /// PHIs in Dest that TI would provide. |
| 3738 | std::vector<Value*> TIPHIValues; |
| 3739 | |
| 3740 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 3741 | // this terminator. |
| 3742 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 3743 | BasicBlock *Pred = *PI; |
| 3744 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 3745 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 3746 | if (!PredBr || !PredBr->isUnconditional() || |
| 3747 | // Must be empty other than the branch. |
| 3748 | &Pred->front() != PredBr) |
| 3749 | continue; |
| 3750 | |
| 3751 | // Finally, since we know that Dest has phi nodes in it, we have to make |
| 3752 | // sure that jumping to Pred will have the same affect as going to Dest in |
| 3753 | // terms of PHI values. |
| 3754 | PHINode *PN; |
| 3755 | unsigned PHINo = 0; |
| 3756 | bool FoundMatch = true; |
| 3757 | for (BasicBlock::iterator I = Dest->begin(); |
| 3758 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 3759 | if (PHINo == TIPHIValues.size()) |
| 3760 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
| 3761 | |
| 3762 | // If the PHI entry doesn't work, we can't use this pred. |
| 3763 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 3764 | FoundMatch = false; |
| 3765 | break; |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | // If we found a workable predecessor, change TI to branch to Succ. |
| 3770 | if (FoundMatch) { |
| 3771 | Dest->removePredecessor(TIBB); |
| 3772 | TI->setSuccessor(SuccNum, Pred); |
| 3773 | return; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | SplitCriticalEdge(TI, SuccNum, P, true); |
| 3778 | } |
| 3779 | |
| 3780 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3781 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 3782 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 3783 | RegMap = MF.getSSARegMap(); |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3784 | DOUT << "\n\n\n=== " << Fn.getName() << "\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3785 | |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 3786 | // First, split all critical edges. |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3787 | // |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3788 | // In this pass we also look for GEP and cast instructions that are used |
| 3789 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 3790 | // selection. |
| 3791 | // |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3792 | bool MadeChange = true; |
| 3793 | while (MadeChange) { |
| 3794 | MadeChange = false; |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 3795 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 3796 | // Split all critical edges where the dest block has a PHI. |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 3797 | TerminatorInst *BBTI = BB->getTerminator(); |
| 3798 | if (BBTI->getNumSuccessors() > 1) { |
| 3799 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 3800 | if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) && |
| 3801 | isCriticalEdge(BBTI, i, true)) |
| 3802 | SplitEdgeNicely(BBTI, i, this); |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3805 | |
Chris Lattner | 57f9a43 | 2006-09-28 06:17:10 +0000 | [diff] [blame] | 3806 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3807 | Instruction *I = BBI++; |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 3808 | |
| 3809 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 3810 | // If we found an inline asm expession, and if the target knows how to |
| 3811 | // lower it to normal LLVM code, do so now. |
| 3812 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 3813 | if (const TargetAsmInfo *TAI = |
| 3814 | TLI.getTargetMachine().getTargetAsmInfo()) { |
| 3815 | if (TAI->ExpandInlineAsm(CI)) |
| 3816 | BBI = BB->begin(); |
| 3817 | } |
| 3818 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3819 | MadeChange |= OptimizeGEPExpression(GEPI, TLI.getTargetData()); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3820 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | c970f06 | 2006-09-13 06:02:42 +0000 | [diff] [blame] | 3821 | // If the source of the cast is a constant, then this should have |
| 3822 | // already been constant folded. The only reason NOT to constant fold |
| 3823 | // it is if something (e.g. LSR) was careful to place the constant |
| 3824 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 3825 | // the address of globals out of a loop). If this is the case, we don't |
| 3826 | // want to forward-subst the cast. |
| 3827 | if (isa<Constant>(CI->getOperand(0))) |
| 3828 | continue; |
| 3829 | |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3830 | // If this is a noop copy, sink it into user blocks to reduce the number |
| 3831 | // of virtual registers that must be created and coallesced. |
| 3832 | MVT::ValueType SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 3833 | MVT::ValueType DstVT = TLI.getValueType(CI->getType()); |
| 3834 | |
| 3835 | // This is an fp<->int conversion? |
| 3836 | if (MVT::isInteger(SrcVT) != MVT::isInteger(DstVT)) |
| 3837 | continue; |
| 3838 | |
| 3839 | // If this is an extension, it will be a zero or sign extension, which |
| 3840 | // isn't a noop. |
| 3841 | if (SrcVT < DstVT) continue; |
| 3842 | |
| 3843 | // If these values will be promoted, find out what they will be promoted |
| 3844 | // to. This helps us consider truncates on PPC as noop copies when they |
| 3845 | // are. |
| 3846 | if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) |
| 3847 | SrcVT = TLI.getTypeToTransformTo(SrcVT); |
| 3848 | if (TLI.getTypeAction(DstVT) == TargetLowering::Promote) |
| 3849 | DstVT = TLI.getTypeToTransformTo(DstVT); |
| 3850 | |
| 3851 | // If, after promotion, these are the same types, this is a noop copy. |
| 3852 | if (SrcVT == DstVT) |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3853 | MadeChange |= OptimizeNoopCopyExpression(CI); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 3856 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3857 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 3858 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3859 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 3860 | |
| 3861 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 3862 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3863 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3864 | return true; |
| 3865 | } |
| 3866 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3867 | SDOperand SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, |
| 3868 | unsigned Reg) { |
| 3869 | SDOperand Op = getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 3870 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3871 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 3872 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3873 | |
| 3874 | // If this type is not legal, we must make sure to not create an invalid |
| 3875 | // register use. |
| 3876 | MVT::ValueType SrcVT = Op.getValueType(); |
| 3877 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3878 | if (SrcVT == DestVT) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3879 | return DAG.getCopyToReg(getRoot(), Reg, Op); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 3880 | } else if (SrcVT == MVT::Vector) { |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3881 | // Handle copies from generic vectors to registers. |
| 3882 | MVT::ValueType PTyElementVT, PTyLegalElementVT; |
| 3883 | unsigned NE = TLI.getPackedTypeBreakdown(cast<PackedType>(V->getType()), |
| 3884 | PTyElementVT, PTyLegalElementVT); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3886 | // Insert a VBIT_CONVERT of the input vector to a "N x PTyElementVT" |
| 3887 | // MVT::Vector type. |
| 3888 | Op = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Op, |
| 3889 | DAG.getConstant(NE, MVT::i32), |
| 3890 | DAG.getValueType(PTyElementVT)); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 3891 | |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3892 | // Loop over all of the elements of the resultant vector, |
| 3893 | // VEXTRACT_VECTOR_ELT'ing them, converting them to PTyLegalElementVT, then |
| 3894 | // copying them into output registers. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3895 | SmallVector<SDOperand, 8> OutChains; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3896 | SDOperand Root = getRoot(); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3897 | for (unsigned i = 0; i != NE; ++i) { |
| 3898 | SDOperand Elt = DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, PTyElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3899 | Op, DAG.getConstant(i, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3900 | if (PTyElementVT == PTyLegalElementVT) { |
| 3901 | // Elements are legal. |
| 3902 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt)); |
| 3903 | } else if (PTyLegalElementVT > PTyElementVT) { |
| 3904 | // Elements are promoted. |
| 3905 | if (MVT::isFloatingPoint(PTyLegalElementVT)) |
| 3906 | Elt = DAG.getNode(ISD::FP_EXTEND, PTyLegalElementVT, Elt); |
| 3907 | else |
| 3908 | Elt = DAG.getNode(ISD::ANY_EXTEND, PTyLegalElementVT, Elt); |
| 3909 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt)); |
| 3910 | } else { |
| 3911 | // Elements are expanded. |
| 3912 | // The src value is expanded into multiple registers. |
| 3913 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3914 | Elt, DAG.getConstant(0, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3915 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3916 | Elt, DAG.getConstant(1, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 3917 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Lo)); |
| 3918 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Hi)); |
| 3919 | } |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 3920 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3921 | return DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3922 | &OutChains[0], OutChains.size()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 3923 | } else if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3924 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 3925 | if (MVT::isFloatingPoint(SrcVT)) |
| 3926 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 3927 | else |
Chris Lattner | fab0887 | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 3928 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3929 | return DAG.getCopyToReg(getRoot(), Reg, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3930 | } else { |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 3931 | DestVT = TLI.getTypeToExpandTo(SrcVT); |
| 3932 | unsigned NumVals = TLI.getNumElements(SrcVT); |
| 3933 | if (NumVals == 1) |
| 3934 | return DAG.getCopyToReg(getRoot(), Reg, |
| 3935 | DAG.getNode(ISD::BIT_CONVERT, DestVT, Op)); |
| 3936 | assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3937 | // The src value is expanded into multiple registers. |
| 3938 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3939 | Op, DAG.getConstant(0, TLI.getPointerTy())); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3940 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3941 | Op, DAG.getConstant(1, TLI.getPointerTy())); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3942 | Op = DAG.getCopyToReg(getRoot(), Reg, Lo); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3943 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 3944 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 3947 | void SelectionDAGISel:: |
| 3948 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 3949 | std::vector<SDOperand> &UnorderedChains) { |
| 3950 | // If this is the entry block, emit arguments. |
| 3951 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 3952 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3953 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 3954 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3956 | unsigned a = 0; |
| 3957 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 3958 | AI != E; ++AI, ++a) |
| 3959 | if (!AI->use_empty()) { |
| 3960 | SDL.setValue(AI, Args[a]); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 3961 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3962 | // If this argument is live outside of the entry block, insert a copy from |
| 3963 | // whereever we got it to the vreg that other BB's will reference it as. |
| 3964 | if (FuncInfo.ValueMap.count(AI)) { |
| 3965 | SDOperand Copy = |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 3966 | SDL.CopyValueToVirtualRegister(AI, FuncInfo.ValueMap[AI]); |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3967 | UnorderedChains.push_back(Copy); |
| 3968 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 3969 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3970 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3971 | // 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] | 3972 | // FIXME: this should insert code into the DAG! |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3973 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3976 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 3977 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 3978 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3979 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 3980 | |
| 3981 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3982 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 3983 | // Lower any arguments needed in this block if this is the entry block. |
| 3984 | if (LLVMBB == &LLVMBB->getParent()->front()) |
| 3985 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3986 | |
| 3987 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 3988 | SDL.setCurrentBasicBlock(BB); |
| 3989 | |
| 3990 | // Lower all of the non-terminator instructions. |
| 3991 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 3992 | I != E; ++I) |
| 3993 | SDL.visit(*I); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 3994 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3995 | // Ensure that all instructions which are used outside of their defining |
| 3996 | // blocks are available as virtual registers. |
| 3997 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 3998 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 3999 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4000 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4001 | UnorderedChains.push_back( |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4002 | SDL.CopyValueToVirtualRegister(I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 4006 | // ensure constants are generated when needed. Remember the virtual registers |
| 4007 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 4008 | // directly add them, because expansion might result in multiple MBB's for one |
| 4009 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 4010 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4011 | // |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4012 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4013 | |
| 4014 | // Emit constants only once even if used by multiple PHI nodes. |
| 4015 | std::map<Constant*, unsigned> ConstantsOut; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4016 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4017 | // Vector bool would be better, but vector<bool> is really slow. |
| 4018 | std::vector<unsigned char> SuccsHandled; |
| 4019 | if (TI->getNumSuccessors()) |
| 4020 | SuccsHandled.resize(BB->getParent()->getNumBlockIDs()); |
| 4021 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4022 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 4023 | // this block. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4024 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 4025 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4026 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4027 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4028 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4029 | // If this terminator has multiple identical successors (common for |
| 4030 | // switches), only handle each succ once. |
| 4031 | unsigned SuccMBBNo = SuccMBB->getNumber(); |
| 4032 | if (SuccsHandled[SuccMBBNo]) continue; |
| 4033 | SuccsHandled[SuccMBBNo] = true; |
| 4034 | |
| 4035 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4036 | PHINode *PN; |
| 4037 | |
| 4038 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 4039 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 4040 | // emitted yet. |
| 4041 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4042 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 4043 | // Ignore dead phi's. |
| 4044 | if (PN->use_empty()) continue; |
| 4045 | |
| 4046 | unsigned Reg; |
| 4047 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 4048 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4049 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 4050 | unsigned &RegOut = ConstantsOut[C]; |
| 4051 | if (RegOut == 0) { |
| 4052 | RegOut = FuncInfo.CreateRegForValue(C); |
| 4053 | UnorderedChains.push_back( |
| 4054 | SDL.CopyValueToVirtualRegister(C, RegOut)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4055 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4056 | Reg = RegOut; |
| 4057 | } else { |
| 4058 | Reg = FuncInfo.ValueMap[PHIOp]; |
| 4059 | if (Reg == 0) { |
| 4060 | assert(isa<AllocaInst>(PHIOp) && |
| 4061 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 4062 | "Didn't codegen value into a register!??"); |
| 4063 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
| 4064 | UnorderedChains.push_back( |
| 4065 | SDL.CopyValueToVirtualRegister(PHIOp, Reg)); |
Chris Lattner | 7e02151 | 2006-03-31 02:12:18 +0000 | [diff] [blame] | 4066 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4067 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4068 | |
| 4069 | // Remember that this register needs to added to the machine PHI node as |
| 4070 | // the input for this MBB. |
| 4071 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
| 4072 | unsigned NumElements; |
| 4073 | if (VT != MVT::Vector) |
| 4074 | NumElements = TLI.getNumElements(VT); |
| 4075 | else { |
| 4076 | MVT::ValueType VT1,VT2; |
| 4077 | NumElements = |
| 4078 | TLI.getPackedTypeBreakdown(cast<PackedType>(PN->getType()), |
| 4079 | VT1, VT2); |
| 4080 | } |
| 4081 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 4082 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 4083 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4084 | } |
| 4085 | ConstantsOut.clear(); |
| 4086 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4087 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 4088 | if (!UnorderedChains.empty()) { |
Chris Lattner | 7436b57 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 4089 | SDOperand Root = SDL.getRoot(); |
| 4090 | if (Root.getOpcode() != ISD::EntryToken) { |
| 4091 | unsigned i = 0, e = UnorderedChains.size(); |
| 4092 | for (; i != e; ++i) { |
| 4093 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 4094 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 4095 | break; // Don't add the root if we already indirectly depend on it. |
| 4096 | } |
| 4097 | |
| 4098 | if (i == e) |
| 4099 | UnorderedChains.push_back(Root); |
| 4100 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4101 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4102 | &UnorderedChains[0], UnorderedChains.size())); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4105 | // Lower the terminator after the copies are emitted. |
| 4106 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4107 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4108 | // Copy over any CaseBlock records that may now exist due to SwitchInst |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4109 | // lowering, as well as any jump table information. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4110 | SwitchCases.clear(); |
| 4111 | SwitchCases = SDL.SwitchCases; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4112 | JT = SDL.JT; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4113 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4114 | // Make sure the root of the DAG is up-to-date. |
| 4115 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4118 | void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) { |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4119 | // Get alias analysis for load/store combining. |
| 4120 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 4121 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4122 | // Run the DAG combiner in pre-legalize mode. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4123 | DAG.Combine(false, AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4124 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4125 | DOUT << "Lowered selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4126 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4127 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4128 | // Second step, hack on the DAG until it only uses operations and types that |
| 4129 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 4130 | DAG.Legalize(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4131 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4132 | DOUT << "Legalized selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4133 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4134 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4135 | // Run the DAG combiner in post-legalize mode. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4136 | DAG.Combine(true, AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4137 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4138 | if (ViewISelDAGs) DAG.viewGraph(); |
Evan Cheng | 552c4a8 | 2006-04-28 02:09:19 +0000 | [diff] [blame] | 4139 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4140 | // Third, instruction select all of the operations to machine code, adding the |
| 4141 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4142 | InstructionSelectBasicBlock(DAG); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4143 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4144 | DOUT << "Selected machine code:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4145 | DEBUG(BB->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4146 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4147 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4148 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 4149 | FunctionLoweringInfo &FuncInfo) { |
| 4150 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 4151 | { |
| 4152 | SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>()); |
| 4153 | CurDAG = &DAG; |
| 4154 | |
| 4155 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 4156 | // types that are not supported by the target. |
| 4157 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 4158 | |
| 4159 | // Second step, emit the lowered DAG as machine code. |
| 4160 | CodeGenAndEmitDAG(DAG); |
| 4161 | } |
| 4162 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4163 | // 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] | 4164 | // PHI nodes in successors. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4165 | if (SwitchCases.empty() && JT.Reg == 0) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4166 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 4167 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 4168 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4169 | "This is not a machine PHI node that we are updating!"); |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4170 | PHI->addRegOperand(PHINodesToUpdate[i].second, false); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4171 | PHI->addMachineBasicBlockOperand(BB); |
| 4172 | } |
| 4173 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4174 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4175 | |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 4176 | // If the JumpTable record is filled in, then we need to emit a jump table. |
| 4177 | // Updating the PHI nodes is tricky in this case, since we need to determine |
| 4178 | // whether the PHI is a successor of the range check MBB or the jump table MBB |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4179 | if (JT.Reg) { |
| 4180 | assert(SwitchCases.empty() && "Cannot have jump table and lowered switch"); |
| 4181 | SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>()); |
| 4182 | CurDAG = &SDAG; |
| 4183 | SelectionDAGLowering SDL(SDAG, TLI, FuncInfo); |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 4184 | MachineBasicBlock *RangeBB = BB; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4185 | // Set the current basic block to the mbb we wish to insert the code into |
| 4186 | BB = JT.MBB; |
| 4187 | SDL.setCurrentBasicBlock(BB); |
| 4188 | // Emit the code |
| 4189 | SDL.visitJumpTable(JT); |
| 4190 | SDAG.setRoot(SDL.getRoot()); |
| 4191 | CodeGenAndEmitDAG(SDAG); |
| 4192 | // Update PHI Nodes |
| 4193 | for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 4194 | MachineInstr *PHI = PHINodesToUpdate[pi].first; |
| 4195 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 4196 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4197 | "This is not a machine PHI node that we are updating!"); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 4198 | if (PHIBB == JT.Default) { |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4199 | PHI->addRegOperand(PHINodesToUpdate[pi].second, false); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 4200 | PHI->addMachineBasicBlockOperand(RangeBB); |
| 4201 | } |
| 4202 | if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) { |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4203 | PHI->addRegOperand(PHINodesToUpdate[pi].second, false); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 4204 | PHI->addMachineBasicBlockOperand(BB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4205 | } |
| 4206 | } |
| 4207 | return; |
| 4208 | } |
| 4209 | |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 4210 | // If the switch block involved a branch to one of the actual successors, we |
| 4211 | // need to update PHI nodes in that block. |
| 4212 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 4213 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 4214 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4215 | "This is not a machine PHI node that we are updating!"); |
| 4216 | if (BB->isSuccessor(PHI->getParent())) { |
| 4217 | PHI->addRegOperand(PHINodesToUpdate[i].second, false); |
| 4218 | PHI->addMachineBasicBlockOperand(BB); |
| 4219 | } |
| 4220 | } |
| 4221 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4222 | // If we generated any switch lowering information, build and codegen any |
| 4223 | // additional DAGs necessary. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4224 | for (unsigned i = 0, e = SwitchCases.size(); i != e; ++i) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4225 | SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>()); |
| 4226 | CurDAG = &SDAG; |
| 4227 | SelectionDAGLowering SDL(SDAG, TLI, FuncInfo); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4228 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4229 | // Set the current basic block to the mbb we wish to insert the code into |
| 4230 | BB = SwitchCases[i].ThisBB; |
| 4231 | SDL.setCurrentBasicBlock(BB); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4232 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4233 | // Emit the code |
| 4234 | SDL.visitSwitchCase(SwitchCases[i]); |
| 4235 | SDAG.setRoot(SDL.getRoot()); |
| 4236 | CodeGenAndEmitDAG(SDAG); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4237 | |
| 4238 | // Handle any PHI nodes in successors of this chunk, as if we were coming |
| 4239 | // from the original BB before switch expansion. Note that PHI nodes can |
| 4240 | // occur multiple times in PHINodesToUpdate. We have to be very careful to |
| 4241 | // handle them the right number of times. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4242 | while ((BB = SwitchCases[i].TrueBB)) { // Handle LHS and RHS. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4243 | for (MachineBasicBlock::iterator Phi = BB->begin(); |
| 4244 | Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){ |
| 4245 | // This value for this PHI node is recorded in PHINodesToUpdate, get it. |
| 4246 | for (unsigned pn = 0; ; ++pn) { |
| 4247 | assert(pn != PHINodesToUpdate.size() && "Didn't find PHI entry!"); |
| 4248 | if (PHINodesToUpdate[pn].first == Phi) { |
| 4249 | Phi->addRegOperand(PHINodesToUpdate[pn].second, false); |
| 4250 | Phi->addMachineBasicBlockOperand(SwitchCases[i].ThisBB); |
| 4251 | break; |
| 4252 | } |
| 4253 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4254 | } |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4255 | |
| 4256 | // Don't process RHS if same block as LHS. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4257 | if (BB == SwitchCases[i].FalseBB) |
| 4258 | SwitchCases[i].FalseBB = 0; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4259 | |
| 4260 | // 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] | 4261 | SwitchCases[i].TrueBB = SwitchCases[i].FalseBB; |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4262 | SwitchCases[i].FalseBB = 0; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4263 | } |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4264 | assert(SwitchCases[i].TrueBB == 0 && SwitchCases[i].FalseBB == 0); |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4265 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4266 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4267 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4268 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4269 | //===----------------------------------------------------------------------===// |
| 4270 | /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each |
| 4271 | /// target node in the graph. |
| 4272 | void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) { |
| 4273 | if (ViewSchedDAGs) DAG.viewGraph(); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 4274 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 4275 | RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault(); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4276 | |
| 4277 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 4278 | Ctor = ISHeuristic; |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 4279 | RegisterScheduler::setDefault(Ctor); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 4280 | } |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4281 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4282 | ScheduleDAG *SL = Ctor(this, &DAG, BB); |
Chris Lattner | a3818e6 | 2006-01-21 19:12:11 +0000 | [diff] [blame] | 4283 | BB = SL->Run(); |
Evan Cheng | cccf123 | 2006-02-04 06:49:00 +0000 | [diff] [blame] | 4284 | delete SL; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4285 | } |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4286 | |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 4287 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4288 | HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() { |
| 4289 | return new HazardRecognizer(); |
| 4290 | } |
| 4291 | |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 4292 | //===----------------------------------------------------------------------===// |
| 4293 | // Helper functions used by the generated instruction selector. |
| 4294 | //===----------------------------------------------------------------------===// |
| 4295 | // Calls to these methods are generated by tblgen. |
| 4296 | |
| 4297 | /// CheckAndMask - The isel is trying to match something like (and X, 255). If |
| 4298 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 4299 | /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value |
| 4300 | /// specified in the .td file (e.g. 255). |
| 4301 | bool SelectionDAGISel::CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, |
| 4302 | int64_t DesiredMaskS) { |
| 4303 | uint64_t ActualMask = RHS->getValue(); |
| 4304 | uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType()); |
| 4305 | |
| 4306 | // If the actual mask exactly matches, success! |
| 4307 | if (ActualMask == DesiredMask) |
| 4308 | return true; |
| 4309 | |
| 4310 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
| 4311 | if (ActualMask & ~DesiredMask) |
| 4312 | return false; |
| 4313 | |
| 4314 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 4315 | // either already zero or is not demanded. Check for known zero input bits. |
| 4316 | uint64_t NeededMask = DesiredMask & ~ActualMask; |
| 4317 | if (getTargetLowering().MaskedValueIsZero(LHS, NeededMask)) |
| 4318 | return true; |
| 4319 | |
| 4320 | // TODO: check to see if missing bits are just not demanded. |
| 4321 | |
| 4322 | // Otherwise, this pattern doesn't match. |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
| 4326 | /// CheckOrMask - The isel is trying to match something like (or X, 255). If |
| 4327 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 4328 | /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value |
| 4329 | /// specified in the .td file (e.g. 255). |
| 4330 | bool SelectionDAGISel::CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, |
| 4331 | int64_t DesiredMaskS) { |
| 4332 | uint64_t ActualMask = RHS->getValue(); |
| 4333 | uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType()); |
| 4334 | |
| 4335 | // If the actual mask exactly matches, success! |
| 4336 | if (ActualMask == DesiredMask) |
| 4337 | return true; |
| 4338 | |
| 4339 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
| 4340 | if (ActualMask & ~DesiredMask) |
| 4341 | return false; |
| 4342 | |
| 4343 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 4344 | // either already zero or is not demanded. Check for known zero input bits. |
| 4345 | uint64_t NeededMask = DesiredMask & ~ActualMask; |
| 4346 | |
| 4347 | uint64_t KnownZero, KnownOne; |
| 4348 | getTargetLowering().ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne); |
| 4349 | |
| 4350 | // If all the missing bits in the or are already known to be set, match! |
| 4351 | if ((NeededMask & KnownOne) == NeededMask) |
| 4352 | return true; |
| 4353 | |
| 4354 | // TODO: check to see if missing bits are just not demanded. |
| 4355 | |
| 4356 | // Otherwise, this pattern doesn't match. |
| 4357 | return false; |
| 4358 | } |
| 4359 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4360 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4361 | /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated |
| 4362 | /// by tblgen. Others should not call it. |
| 4363 | void SelectionDAGISel:: |
| 4364 | SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) { |
| 4365 | std::vector<SDOperand> InOps; |
| 4366 | std::swap(InOps, Ops); |
| 4367 | |
| 4368 | Ops.push_back(InOps[0]); // input chain. |
| 4369 | Ops.push_back(InOps[1]); // input asm string. |
| 4370 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4371 | unsigned i = 2, e = InOps.size(); |
| 4372 | if (InOps[e-1].getValueType() == MVT::Flag) |
| 4373 | --e; // Don't process a flag operand if it is here. |
| 4374 | |
| 4375 | while (i != e) { |
| 4376 | unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue(); |
| 4377 | if ((Flags & 7) != 4 /*MEM*/) { |
| 4378 | // Just skip over this operand, copying the operands verbatim. |
| 4379 | Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1); |
| 4380 | i += (Flags >> 3) + 1; |
| 4381 | } else { |
| 4382 | assert((Flags >> 3) == 1 && "Memory operand with multiple values?"); |
| 4383 | // Otherwise, this is a memory operand. Ask the target to select it. |
| 4384 | std::vector<SDOperand> SelOps; |
| 4385 | if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4386 | cerr << "Could not match memory address. Inline asm failure!\n"; |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4387 | exit(1); |
| 4388 | } |
| 4389 | |
| 4390 | // Add this to the output node. |
Chris Lattner | 36d4396 | 2006-12-16 21:14:48 +0000 | [diff] [blame] | 4391 | Ops.push_back(DAG.getTargetConstant(4/*MEM*/ | (SelOps.size() << 3), |
| 4392 | MVT::i32)); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4393 | Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); |
| 4394 | i += 2; |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | // Add the flag input back if present. |
| 4399 | if (e != InOps.size()) |
| 4400 | Ops.push_back(InOps.back()); |
| 4401 | } |