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