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