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