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" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 31 | #include "llvm/CodeGen/SelectionDAG.h" |
| 32 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 33 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
| 35 | #include "llvm/Target/TargetFrameInfo.h" |
| 36 | #include "llvm/Target/TargetInstrInfo.h" |
| 37 | #include "llvm/Target/TargetLowering.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
| 43 | #include <map> |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 44 | #include <set> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 45 | #include <iostream> |
Jeff Cohen | 7e88103 | 2006-02-24 02:52:40 +0000 | [diff] [blame] | 46 | #include <algorithm> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 49 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 50 | static cl::opt<bool> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 51 | ViewISelDAGs("view-isel-dags", cl::Hidden, |
| 52 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 53 | static cl::opt<bool> |
| 54 | ViewSchedDAGs("view-sched-dags", cl::Hidden, |
| 55 | 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] | 56 | #else |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 57 | static const bool ViewISelDAGs = 0; |
| 58 | static const bool ViewSchedDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +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 | listSchedulingTD // Top-down list scheduler. |
| 69 | }; |
| 70 | |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 71 | namespace { |
| 72 | cl::opt<SchedHeuristics> |
| 73 | ISHeuristic( |
| 74 | "sched", |
| 75 | cl::desc("Choose scheduling style"), |
Evan Cheng | 3f23952 | 2006-01-25 09:12:57 +0000 | [diff] [blame] | 76 | cl::init(defaultScheduling), |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 77 | cl::values( |
Evan Cheng | 3f23952 | 2006-01-25 09:12:57 +0000 | [diff] [blame] | 78 | clEnumValN(defaultScheduling, "default", |
| 79 | "Target preferred scheduling style"), |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 80 | clEnumValN(noScheduling, "none", |
Jim Laskey | 17d52f7 | 2006-01-23 13:34:04 +0000 | [diff] [blame] | 81 | "No scheduling: breadth first sequencing"), |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 82 | clEnumValN(simpleScheduling, "simple", |
| 83 | "Simple two pass scheduling: minimize critical path " |
| 84 | "and maximize processor utilization"), |
| 85 | clEnumValN(simpleNoItinScheduling, "simple-noitin", |
| 86 | "Simple two pass scheduling: Same as simple " |
| 87 | "except using generic latency"), |
Evan Cheng | 3f23952 | 2006-01-25 09:12:57 +0000 | [diff] [blame] | 88 | clEnumValN(listSchedulingBURR, "list-burr", |
Evan Cheng | f0f9c90 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 89 | "Bottom up register reduction list scheduling"), |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 90 | clEnumValN(listSchedulingTD, "list-td", |
| 91 | "Top-down list scheduler"), |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 92 | clEnumValEnd)); |
| 93 | } // namespace |
| 94 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 95 | namespace { |
| 96 | /// RegsForValue - This struct represents the physical registers that a |
| 97 | /// particular value is assigned and the type information about the value. |
| 98 | /// This is needed because values can be promoted into larger registers and |
| 99 | /// expanded into multiple smaller registers than the value. |
| 100 | struct RegsForValue { |
| 101 | /// Regs - This list hold the register (for legal and promoted values) |
| 102 | /// or register set (for expanded values) that the value should be assigned |
| 103 | /// to. |
| 104 | std::vector<unsigned> Regs; |
| 105 | |
| 106 | /// RegVT - The value type of each register. |
| 107 | /// |
| 108 | MVT::ValueType RegVT; |
| 109 | |
| 110 | /// ValueVT - The value type of the LLVM value, which may be promoted from |
| 111 | /// RegVT or made from merging the two expanded parts. |
| 112 | MVT::ValueType ValueVT; |
| 113 | |
| 114 | RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {} |
| 115 | |
| 116 | RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt) |
| 117 | : RegVT(regvt), ValueVT(valuevt) { |
| 118 | Regs.push_back(Reg); |
| 119 | } |
| 120 | RegsForValue(const std::vector<unsigned> ®s, |
| 121 | MVT::ValueType regvt, MVT::ValueType valuevt) |
| 122 | : Regs(regs), RegVT(regvt), ValueVT(valuevt) { |
| 123 | } |
| 124 | |
| 125 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 126 | /// this value and returns the result as a ValueVT value. This uses |
| 127 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 128 | SDOperand getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 129 | SDOperand &Chain, SDOperand &Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 130 | |
| 131 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 132 | /// specified value into the registers specified by this object. This uses |
| 133 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 134 | void getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 135 | SDOperand &Chain, SDOperand &Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 136 | |
| 137 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 138 | /// operand list. This adds the code marker and includes the number of |
| 139 | /// values added into it. |
| 140 | void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 141 | std::vector<SDOperand> &Ops) const; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 142 | }; |
| 143 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 145 | namespace llvm { |
| 146 | //===--------------------------------------------------------------------===// |
| 147 | /// FunctionLoweringInfo - This contains information that is global to a |
| 148 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 149 | class FunctionLoweringInfo { |
| 150 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 151 | TargetLowering &TLI; |
| 152 | Function &Fn; |
| 153 | MachineFunction &MF; |
| 154 | SSARegMap *RegMap; |
| 155 | |
| 156 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 157 | |
| 158 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 159 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 160 | |
| 161 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 162 | /// we must remember which virtual registers hold the values for |
| 163 | /// cross-basic-block values. |
| 164 | std::map<const Value*, unsigned> ValueMap; |
| 165 | |
| 166 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 167 | /// the entry block. This allows the allocas to be efficiently referenced |
| 168 | /// anywhere in the function. |
| 169 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 170 | |
| 171 | unsigned MakeReg(MVT::ValueType VT) { |
| 172 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 173 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 175 | unsigned CreateRegForValue(const Value *V); |
| 176 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 177 | unsigned InitializeRegForValue(const Value *V) { |
| 178 | unsigned &R = ValueMap[V]; |
| 179 | assert(R == 0 && "Already initialized this value register!"); |
| 180 | return R = CreateRegForValue(V); |
| 181 | } |
| 182 | }; |
| 183 | } |
| 184 | |
| 185 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 186 | /// PHI nodes or outside of the basic block that defines it. |
| 187 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 188 | if (isa<PHINode>(I)) return true; |
| 189 | BasicBlock *BB = I->getParent(); |
| 190 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 191 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 192 | return true; |
| 193 | return false; |
| 194 | } |
| 195 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 196 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 197 | /// entry block, return true. |
| 198 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 199 | BasicBlock *Entry = A->getParent()->begin(); |
| 200 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 201 | if (cast<Instruction>(*UI)->getParent() != Entry) |
| 202 | return false; // Use not in entry block. |
| 203 | return true; |
| 204 | } |
| 205 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 206 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 207 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 208 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 209 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 210 | // Create a vreg for each argument register that is not dead and is used |
| 211 | // outside of the entry block for the function. |
| 212 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 213 | AI != E; ++AI) |
| 214 | if (!isOnlyUsedInEntryBlock(AI)) |
| 215 | InitializeRegForValue(AI); |
| 216 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 217 | // Initialize the mapping of values to registers. This is only set up for |
| 218 | // instruction values that are used outside of the block that defines |
| 219 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 220 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 221 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 222 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 223 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 224 | const Type *Ty = AI->getAllocatedType(); |
| 225 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 226 | unsigned Align = |
| 227 | std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 228 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 229 | |
| 230 | // If the alignment of the value is smaller than the size of the value, |
| 231 | // and if the size of the value is particularly small (<= 8 bytes), |
| 232 | // round up to the size of the value for potentially better performance. |
| 233 | // |
| 234 | // FIXME: This could be made better with a preferred alignment hook in |
| 235 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 236 | if (Align < TySize && TySize <= 8) Align = TySize; |
Chris Lattner | 2dfa819 | 2005-10-18 22:11:42 +0000 | [diff] [blame] | 237 | TySize *= CUI->getValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 238 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 239 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 240 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 243 | for (; BB != EB; ++BB) |
| 244 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 245 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 246 | if (!isa<AllocaInst>(I) || |
| 247 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 248 | InitializeRegForValue(I); |
| 249 | |
| 250 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 251 | // also creates the initial PHI MachineInstrs, though none of the input |
| 252 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 253 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 254 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 255 | MBBMap[BB] = MBB; |
| 256 | MF.getBasicBlockList().push_back(MBB); |
| 257 | |
| 258 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 259 | // appropriate. |
| 260 | PHINode *PN; |
| 261 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 262 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 263 | if (!PN->use_empty()) { |
| 264 | unsigned NumElements = |
| 265 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 266 | unsigned PHIReg = ValueMap[PN]; |
| 267 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 268 | for (unsigned i = 0; i != NumElements; ++i) |
| 269 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 270 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 274 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 275 | /// the correctly promoted or expanded types. Assign these registers |
| 276 | /// consecutive vreg numbers and return the first assigned number. |
| 277 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 278 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 279 | |
| 280 | // The number of multiples of registers that we need, to, e.g., split up |
| 281 | // a <2 x int64> -> 4 x i32 registers. |
| 282 | unsigned NumVectorRegs = 1; |
| 283 | |
| 284 | // If this is a packed type, figure out what type it will decompose into |
| 285 | // and how many of the elements it will use. |
| 286 | if (VT == MVT::Vector) { |
| 287 | const PackedType *PTy = cast<PackedType>(V->getType()); |
| 288 | unsigned NumElts = PTy->getNumElements(); |
| 289 | MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType()); |
| 290 | |
| 291 | // Divide the input until we get to a supported size. This will always |
| 292 | // end with a scalar if the target doesn't support vectors. |
| 293 | while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) { |
| 294 | NumElts >>= 1; |
| 295 | NumVectorRegs <<= 1; |
| 296 | } |
Chris Lattner | 6cb7004 | 2006-03-16 23:05:19 +0000 | [diff] [blame] | 297 | if (NumElts == 1) |
| 298 | VT = EltTy; |
| 299 | else |
| 300 | VT = getVectorType(EltTy, NumElts); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // The common case is that we will only create one register for this |
| 304 | // value. If we have that case, create and return the virtual register. |
| 305 | unsigned NV = TLI.getNumElements(VT); |
| 306 | if (NV == 1) { |
| 307 | // If we are promoting this value, pick the next largest supported type. |
| 308 | MVT::ValueType PromotedType = TLI.getTypeToTransformTo(VT); |
| 309 | unsigned Reg = MakeReg(PromotedType); |
| 310 | // If this is a vector of supported or promoted types (e.g. 4 x i16), |
| 311 | // create all of the registers. |
| 312 | for (unsigned i = 1; i != NumVectorRegs; ++i) |
| 313 | MakeReg(PromotedType); |
| 314 | return Reg; |
| 315 | } |
| 316 | |
| 317 | // If this value is represented with multiple target registers, make sure |
| 318 | // to create enough consecutive registers of the right (smaller) type. |
| 319 | unsigned NT = VT-1; // Find the type to use. |
| 320 | while (TLI.getNumElements((MVT::ValueType)NT) != 1) |
| 321 | --NT; |
| 322 | |
| 323 | unsigned R = MakeReg((MVT::ValueType)NT); |
| 324 | for (unsigned i = 1; i != NV*NumVectorRegs; ++i) |
| 325 | MakeReg((MVT::ValueType)NT); |
| 326 | return R; |
| 327 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 328 | |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 331 | /// implementation that is parameterized by a TargetLowering object. |
| 332 | /// Also, targets can overload any lowering method. |
| 333 | /// |
| 334 | namespace llvm { |
| 335 | class SelectionDAGLowering { |
| 336 | MachineBasicBlock *CurMBB; |
| 337 | |
| 338 | std::map<const Value*, SDOperand> NodeMap; |
| 339 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 340 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 341 | /// them up and then emit token factor nodes when possible. This allows us to |
| 342 | /// get simple disambiguation between loads without worrying about alias |
| 343 | /// analysis. |
| 344 | std::vector<SDOperand> PendingLoads; |
| 345 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 346 | public: |
| 347 | // TLI - This is information that describes the available target features we |
| 348 | // need for lowering. This indicates when operations are unavailable, |
| 349 | // implemented with a libcall, etc. |
| 350 | TargetLowering &TLI; |
| 351 | SelectionDAG &DAG; |
| 352 | const TargetData &TD; |
| 353 | |
| 354 | /// FuncInfo - Information about the function as a whole. |
| 355 | /// |
| 356 | FunctionLoweringInfo &FuncInfo; |
| 357 | |
| 358 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 359 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 360 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 361 | FuncInfo(funcinfo) { |
| 362 | } |
| 363 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 364 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 365 | /// |
| 366 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 367 | if (PendingLoads.empty()) |
| 368 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 369 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 370 | if (PendingLoads.size() == 1) { |
| 371 | SDOperand Root = PendingLoads[0]; |
| 372 | DAG.setRoot(Root); |
| 373 | PendingLoads.clear(); |
| 374 | return Root; |
| 375 | } |
| 376 | |
| 377 | // Otherwise, we have to make a token factor node. |
| 378 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 379 | PendingLoads.clear(); |
| 380 | DAG.setRoot(Root); |
| 381 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 384 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 385 | |
| 386 | void visit(unsigned Opcode, User &I) { |
| 387 | switch (Opcode) { |
| 388 | default: assert(0 && "Unknown instruction type encountered!"); |
| 389 | abort(); |
| 390 | // Build the switch statement using the Instruction.def file. |
| 391 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 392 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 393 | #include "llvm/Instruction.def" |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 398 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 399 | SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr, |
| 400 | SDOperand SrcValue, SDOperand Root, |
| 401 | bool isVolatile); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 402 | |
| 403 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 404 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 405 | } |
| 406 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 407 | SDOperand getValue(const Value *V); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 408 | |
| 409 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 410 | SDOperand &N = NodeMap[V]; |
| 411 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 412 | return N = NewN; |
| 413 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 415 | RegsForValue GetRegistersForValue(const std::string &ConstrCode, |
| 416 | MVT::ValueType VT, |
| 417 | bool OutReg, bool InReg, |
| 418 | std::set<unsigned> &OutputRegs, |
| 419 | std::set<unsigned> &InputRegs); |
| 420 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 421 | // Terminator instructions. |
| 422 | void visitRet(ReturnInst &I); |
| 423 | void visitBr(BranchInst &I); |
| 424 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 425 | |
| 426 | // These all get lowered before this pass. |
| 427 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 428 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 429 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 430 | |
| 431 | // |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 432 | void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 433 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 434 | void visitAdd(User &I) { |
| 435 | visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 436 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 437 | void visitSub(User &I); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 438 | void visitMul(User &I) { |
| 439 | visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 441 | void visitDiv(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 442 | const Type *Ty = I.getType(); |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 443 | visitBinary(I, |
| 444 | Ty->isSigned() ? ISD::SDIV : ISD::UDIV, ISD::FDIV, |
| 445 | Ty->isSigned() ? ISD::VSDIV : ISD::VUDIV); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 446 | } |
| 447 | void visitRem(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 448 | const Type *Ty = I.getType(); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 449 | visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 450 | } |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 451 | void visitAnd(User &I) { visitBinary(I, ISD::AND, 0, ISD::VAND); } |
| 452 | void visitOr (User &I) { visitBinary(I, ISD::OR, 0, ISD::VOR); } |
| 453 | void visitXor(User &I) { visitBinary(I, ISD::XOR, 0, ISD::VXOR); } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 454 | void visitShl(User &I) { visitShift(I, ISD::SHL); } |
| 455 | void visitShr(User &I) { |
| 456 | visitShift(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 460 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 461 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 462 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 463 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 464 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 465 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 466 | |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 467 | void visitExtractElement(ExtractElementInst &I); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 468 | void visitInsertElement(InsertElementInst &I); |
| 469 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 470 | void visitGetElementPtr(User &I); |
| 471 | void visitCast(User &I); |
| 472 | void visitSelect(User &I); |
| 473 | // |
| 474 | |
| 475 | void visitMalloc(MallocInst &I); |
| 476 | void visitFree(FreeInst &I); |
| 477 | void visitAlloca(AllocaInst &I); |
| 478 | void visitLoad(LoadInst &I); |
| 479 | void visitStore(StoreInst &I); |
| 480 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 481 | void visitCall(CallInst &I); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 482 | void visitInlineAsm(CallInst &I); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 483 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame^] | 484 | void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 486 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 487 | void visitVAArg(VAArgInst &I); |
| 488 | void visitVAEnd(CallInst &I); |
| 489 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 490 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 492 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 493 | |
| 494 | void visitUserOp1(Instruction &I) { |
| 495 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 496 | abort(); |
| 497 | } |
| 498 | void visitUserOp2(Instruction &I) { |
| 499 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 500 | abort(); |
| 501 | } |
| 502 | }; |
| 503 | } // end namespace llvm |
| 504 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 505 | SDOperand SelectionDAGLowering::getValue(const Value *V) { |
| 506 | SDOperand &N = NodeMap[V]; |
| 507 | if (N.Val) return N; |
| 508 | |
| 509 | const Type *VTy = V->getType(); |
| 510 | MVT::ValueType VT = TLI.getValueType(VTy); |
| 511 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 512 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 513 | visit(CE->getOpcode(), *CE); |
| 514 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 515 | return N; |
| 516 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 517 | return N = DAG.getGlobalAddress(GV, VT); |
| 518 | } else if (isa<ConstantPointerNull>(C)) { |
| 519 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 520 | } else if (isa<UndefValue>(C)) { |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 521 | if (!isa<PackedType>(VTy)) |
| 522 | return N = DAG.getNode(ISD::UNDEF, VT); |
| 523 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 524 | // Create a VBUILD_VECTOR of undef nodes. |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 525 | const PackedType *PTy = cast<PackedType>(VTy); |
| 526 | unsigned NumElements = PTy->getNumElements(); |
| 527 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 528 | |
| 529 | std::vector<SDOperand> Ops; |
| 530 | Ops.assign(NumElements, DAG.getNode(ISD::UNDEF, PVT)); |
| 531 | |
| 532 | // Create a VConstant node with generic Vector type. |
| 533 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 534 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 535 | return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 536 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 537 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
| 538 | } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) { |
| 539 | unsigned NumElements = PTy->getNumElements(); |
| 540 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 541 | |
| 542 | // Now that we know the number and type of the elements, push a |
| 543 | // Constant or ConstantFP node onto the ops list for each element of |
| 544 | // the packed constant. |
| 545 | std::vector<SDOperand> Ops; |
| 546 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) { |
| 547 | if (MVT::isFloatingPoint(PVT)) { |
| 548 | for (unsigned i = 0; i != NumElements; ++i) { |
| 549 | const ConstantFP *El = cast<ConstantFP>(CP->getOperand(i)); |
| 550 | Ops.push_back(DAG.getConstantFP(El->getValue(), PVT)); |
| 551 | } |
| 552 | } else { |
| 553 | for (unsigned i = 0; i != NumElements; ++i) { |
| 554 | const ConstantIntegral *El = |
| 555 | cast<ConstantIntegral>(CP->getOperand(i)); |
| 556 | Ops.push_back(DAG.getConstant(El->getRawValue(), PVT)); |
| 557 | } |
| 558 | } |
| 559 | } else { |
| 560 | assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!"); |
| 561 | SDOperand Op; |
| 562 | if (MVT::isFloatingPoint(PVT)) |
| 563 | Op = DAG.getConstantFP(0, PVT); |
| 564 | else |
| 565 | Op = DAG.getConstant(0, PVT); |
| 566 | Ops.assign(NumElements, Op); |
| 567 | } |
| 568 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 569 | // Create a VBUILD_VECTOR node with generic Vector type. |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 570 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 571 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 572 | return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 573 | } else { |
| 574 | // Canonicalize all constant ints to be unsigned. |
| 575 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 580 | std::map<const AllocaInst*, int>::iterator SI = |
| 581 | FuncInfo.StaticAllocaMap.find(AI); |
| 582 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 583 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 584 | } |
| 585 | |
| 586 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 587 | FuncInfo.ValueMap.find(V); |
| 588 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
| 589 | |
| 590 | unsigned InReg = VMI->second; |
| 591 | |
| 592 | // If this type is not legal, make it so now. |
| 593 | if (VT == MVT::Vector) { |
| 594 | // FIXME: We only handle legal vectors right now. We need a VBUILD_VECTOR |
| 595 | const PackedType *PTy = cast<PackedType>(VTy); |
| 596 | unsigned NumElements = PTy->getNumElements(); |
| 597 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 598 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
| 599 | assert(TLI.isTypeLegal(TVT) && |
| 600 | "FIXME: Cannot handle illegal vector types here yet!"); |
| 601 | VT = TVT; |
| 602 | } |
| 603 | |
| 604 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 605 | |
| 606 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 607 | if (DestVT < VT) { |
| 608 | // Source must be expanded. This input value is actually coming from the |
| 609 | // register pair VMI->second and VMI->second+1. |
| 610 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 611 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 612 | } else { |
| 613 | if (DestVT > VT) { // Promotion case |
| 614 | if (MVT::isFloatingPoint(VT)) |
| 615 | N = DAG.getNode(ISD::FP_ROUND, VT, N); |
| 616 | else |
| 617 | N = DAG.getNode(ISD::TRUNCATE, VT, N); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return N; |
| 622 | } |
| 623 | |
| 624 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 625 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 626 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 627 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 628 | return; |
| 629 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 630 | std::vector<SDOperand> NewValues; |
| 631 | NewValues.push_back(getRoot()); |
| 632 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 633 | SDOperand RetOp = getValue(I.getOperand(i)); |
| 634 | |
| 635 | // If this is an integer return value, we need to promote it ourselves to |
| 636 | // the full width of a register, since LegalizeOp will use ANY_EXTEND rather |
| 637 | // than sign/zero. |
| 638 | if (MVT::isInteger(RetOp.getValueType()) && |
| 639 | RetOp.getValueType() < MVT::i64) { |
| 640 | MVT::ValueType TmpVT; |
| 641 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 642 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
| 643 | else |
| 644 | TmpVT = MVT::i32; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 645 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 646 | if (I.getOperand(i)->getType()->isSigned()) |
| 647 | RetOp = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, RetOp); |
| 648 | else |
| 649 | RetOp = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, RetOp); |
| 650 | } |
| 651 | NewValues.push_back(RetOp); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 652 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 653 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, NewValues)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 657 | // Update machine-CFG edges. |
| 658 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 659 | |
| 660 | // Figure out which block is immediately after the current one. |
| 661 | MachineBasicBlock *NextBlock = 0; |
| 662 | MachineFunction::iterator BBI = CurMBB; |
| 663 | if (++BBI != CurMBB->getParent()->end()) |
| 664 | NextBlock = BBI; |
| 665 | |
| 666 | if (I.isUnconditional()) { |
| 667 | // If this is not a fall-through branch, emit the branch. |
| 668 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 669 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 670 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 671 | } else { |
| 672 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 673 | |
| 674 | SDOperand Cond = getValue(I.getCondition()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 675 | if (Succ1MBB == NextBlock) { |
| 676 | // If the condition is false, fall through. This means we should branch |
| 677 | // if the condition is true to Succ #0. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 678 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 679 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 680 | } else if (Succ0MBB == NextBlock) { |
| 681 | // If the condition is true, fall through. This means we should branch if |
| 682 | // the condition is false to Succ #1. Invert the condition first. |
| 683 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 684 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 685 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 686 | Cond, DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 687 | } else { |
Chris Lattner | e7ccd4a | 2005-04-09 03:30:29 +0000 | [diff] [blame] | 688 | std::vector<SDOperand> Ops; |
| 689 | Ops.push_back(getRoot()); |
Evan Cheng | 298ebf2 | 2006-02-16 08:27:56 +0000 | [diff] [blame] | 690 | // If the false case is the current basic block, then this is a self |
| 691 | // loop. We do not want to emit "Loop: ... brcond Out; br Loop", as it |
| 692 | // adds an extra instruction in the loop. Instead, invert the |
| 693 | // condition and emit "Loop: ... br!cond Loop; br Out. |
| 694 | if (CurMBB == Succ1MBB) { |
| 695 | std::swap(Succ0MBB, Succ1MBB); |
| 696 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 697 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
| 698 | } |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 699 | SDOperand True = DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), Cond, |
| 700 | DAG.getBasicBlock(Succ0MBB)); |
| 701 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, True, |
| 702 | DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 707 | void SelectionDAGLowering::visitSub(User &I) { |
| 708 | // -0.0 - X --> fneg |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 709 | if (I.getType()->isFloatingPoint()) { |
| 710 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 711 | if (CFP->isExactlyValue(-0.0)) { |
| 712 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 713 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 714 | return; |
| 715 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 716 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 717 | visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 720 | void SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp, |
| 721 | unsigned VecOp) { |
| 722 | const Type *Ty = I.getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 723 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 724 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 725 | |
Chris Lattner | b67eb91 | 2005-11-19 18:40:42 +0000 | [diff] [blame] | 726 | if (Ty->isIntegral()) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 727 | setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2)); |
| 728 | } else if (Ty->isFloatingPoint()) { |
| 729 | setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2)); |
| 730 | } else { |
| 731 | const PackedType *PTy = cast<PackedType>(Ty); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 732 | SDOperand Num = DAG.getConstant(PTy->getNumElements(), MVT::i32); |
| 733 | SDOperand Typ = DAG.getValueType(TLI.getValueType(PTy->getElementType())); |
| 734 | setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ)); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 735 | } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 737 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 738 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 739 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 740 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 741 | |
| 742 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 743 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 744 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 745 | } |
| 746 | |
| 747 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 748 | ISD::CondCode UnsignedOpcode) { |
| 749 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 750 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 751 | ISD::CondCode Opcode = SignedOpcode; |
| 752 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 753 | Opcode = UnsignedOpcode; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 754 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | void SelectionDAGLowering::visitSelect(User &I) { |
| 758 | SDOperand Cond = getValue(I.getOperand(0)); |
| 759 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 760 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 761 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 762 | TrueVal, FalseVal)); |
| 763 | } |
| 764 | |
| 765 | void SelectionDAGLowering::visitCast(User &I) { |
| 766 | SDOperand N = getValue(I.getOperand(0)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 767 | MVT::ValueType SrcVT = N.getValueType(); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 768 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 769 | |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 770 | if (DestVT == MVT::Vector) { |
| 771 | // This is a cast to a vector from something else. This is always a bit |
| 772 | // convert. Get information about the input vector. |
| 773 | const PackedType *DestTy = cast<PackedType>(I.getType()); |
| 774 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 775 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N, |
| 776 | DAG.getConstant(DestTy->getNumElements(),MVT::i32), |
| 777 | DAG.getValueType(EltVT))); |
| 778 | } else if (SrcVT == DestVT) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 779 | setValue(&I, N); // noop cast. |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 780 | } else if (DestVT == MVT::i1) { |
Chris Lattner | ef311aa | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 781 | // Cast to bool is a comparison against zero, not truncation to zero. |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 782 | SDOperand Zero = isInteger(SrcVT) ? DAG.getConstant(0, N.getValueType()) : |
Chris Lattner | ef311aa | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 783 | DAG.getConstantFP(0.0, N.getValueType()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 784 | setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE)); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 785 | } else if (isInteger(SrcVT)) { |
| 786 | if (isInteger(DestVT)) { // Int -> Int cast |
| 787 | if (DestVT < SrcVT) // Truncating cast? |
| 788 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 789 | else if (I.getOperand(0)->getType()->isSigned()) |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 790 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 791 | else |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 792 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
Chris Lattner | 7e35890 | 2006-03-22 22:20:49 +0000 | [diff] [blame] | 793 | } else if (isFloatingPoint(DestVT)) { // Int -> FP cast |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 794 | if (I.getOperand(0)->getType()->isSigned()) |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 795 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 796 | else |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 797 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 798 | } else { |
| 799 | assert(0 && "Unknown cast!"); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 801 | } else if (isFloatingPoint(SrcVT)) { |
| 802 | if (isFloatingPoint(DestVT)) { // FP -> FP cast |
| 803 | if (DestVT < SrcVT) // Rounding cast? |
| 804 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 805 | else |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 806 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 807 | } else if (isInteger(DestVT)) { // FP -> Int cast. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 808 | if (I.getType()->isSigned()) |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 809 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 810 | else |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 811 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 812 | } else { |
| 813 | assert(0 && "Unknown cast!"); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 814 | } |
| 815 | } else { |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 816 | assert(SrcVT == MVT::Vector && "Unknown cast!"); |
| 817 | assert(DestVT != MVT::Vector && "Casts to vector already handled!"); |
| 818 | // This is a cast from a vector to something else. This is always a bit |
| 819 | // convert. Get information about the input vector. |
| 820 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 824 | void SelectionDAGLowering::visitInsertElement(InsertElementInst &I) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 825 | SDOperand InVec = getValue(I.getOperand(0)); |
| 826 | SDOperand InVal = getValue(I.getOperand(1)); |
| 827 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 828 | getValue(I.getOperand(2))); |
| 829 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 830 | SDOperand Num = *(InVec.Val->op_end()-2); |
| 831 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 832 | setValue(&I, DAG.getNode(ISD::VINSERT_VECTOR_ELT, MVT::Vector, |
| 833 | InVec, InVal, InIdx, Num, Typ)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 836 | void SelectionDAGLowering::visitExtractElement(ExtractElementInst &I) { |
| 837 | SDOperand InVec = getValue(I.getOperand(0)); |
| 838 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 839 | getValue(I.getOperand(1))); |
| 840 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 841 | setValue(&I, DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, |
| 842 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 843 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 845 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 846 | SDOperand N = getValue(I.getOperand(0)); |
| 847 | const Type *Ty = I.getOperand(0)->getType(); |
| 848 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 849 | |
| 850 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 851 | OI != E; ++OI) { |
| 852 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 853 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 854 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 855 | if (Field) { |
| 856 | // N = N + Offset |
| 857 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 858 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 859 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 860 | } |
| 861 | Ty = StTy->getElementType(Field); |
| 862 | } else { |
| 863 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 865 | // If this is a constant subscript, handle it quickly. |
| 866 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 867 | if (CI->getRawValue() == 0) continue; |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 869 | uint64_t Offs; |
| 870 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 871 | Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 872 | else |
| 873 | Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 874 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 875 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 876 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 877 | |
| 878 | // N = N + Idx * ElementSize; |
| 879 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 880 | SDOperand IdxN = getValue(Idx); |
| 881 | |
| 882 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 883 | // it. |
| 884 | if (IdxN.getValueType() < N.getValueType()) { |
| 885 | if (Idx->getType()->isSigned()) |
| 886 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
| 887 | else |
| 888 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN); |
| 889 | } else if (IdxN.getValueType() > N.getValueType()) |
| 890 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 891 | |
| 892 | // If this is a multiply by a power of two, turn it into a shl |
| 893 | // immediately. This is a very common case. |
| 894 | if (isPowerOf2_64(ElementSize)) { |
| 895 | unsigned Amt = Log2_64(ElementSize); |
| 896 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 897 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 898 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 903 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 904 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | setValue(&I, N); |
| 908 | } |
| 909 | |
| 910 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 911 | // If this is a fixed sized alloca in the entry block of the function, |
| 912 | // allocate it statically on the stack. |
| 913 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 914 | return; // getValue will auto-populate this. |
| 915 | |
| 916 | const Type *Ty = I.getAllocatedType(); |
| 917 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 918 | unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 919 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 920 | |
| 921 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 922 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 923 | if (IntPtr < AllocSize.getValueType()) |
| 924 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 925 | else if (IntPtr > AllocSize.getValueType()) |
| 926 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 928 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 929 | getIntPtrConstant(TySize)); |
| 930 | |
| 931 | // Handle alignment. If the requested alignment is less than or equal to the |
| 932 | // stack alignment, ignore it and round the size of the allocation up to the |
| 933 | // stack alignment size. If the size is greater than the stack alignment, we |
| 934 | // note this in the DYNAMIC_STACKALLOC node. |
| 935 | unsigned StackAlign = |
| 936 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 937 | if (Align <= StackAlign) { |
| 938 | Align = 0; |
| 939 | // Add SA-1 to the size. |
| 940 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 941 | getIntPtrConstant(StackAlign-1)); |
| 942 | // Mask out the low bits for alignment purposes. |
| 943 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 944 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 945 | } |
| 946 | |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 947 | std::vector<MVT::ValueType> VTs; |
| 948 | VTs.push_back(AllocSize.getValueType()); |
| 949 | VTs.push_back(MVT::Other); |
| 950 | std::vector<SDOperand> Ops; |
| 951 | Ops.push_back(getRoot()); |
| 952 | Ops.push_back(AllocSize); |
| 953 | Ops.push_back(getIntPtrConstant(Align)); |
| 954 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 955 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 956 | |
| 957 | // Inform the Frame Information that we have just allocated a variable-sized |
| 958 | // object. |
| 959 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 960 | } |
| 961 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 962 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 963 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 964 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 965 | SDOperand Root; |
| 966 | if (I.isVolatile()) |
| 967 | Root = getRoot(); |
| 968 | else { |
| 969 | // Do not serialize non-volatile loads against each other. |
| 970 | Root = DAG.getRoot(); |
| 971 | } |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 972 | |
| 973 | setValue(&I, getLoadFrom(I.getType(), Ptr, DAG.getSrcValue(I.getOperand(0)), |
| 974 | Root, I.isVolatile())); |
| 975 | } |
| 976 | |
| 977 | SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr, |
| 978 | SDOperand SrcValue, SDOperand Root, |
| 979 | bool isVolatile) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 980 | SDOperand L; |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 981 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 982 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 983 | L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr, SrcValue); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 984 | } else { |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 985 | L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SrcValue); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 986 | } |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 987 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 988 | if (isVolatile) |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 989 | DAG.setRoot(L.getValue(1)); |
| 990 | else |
| 991 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 992 | |
| 993 | return L; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
| 997 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 998 | Value *SrcV = I.getOperand(0); |
| 999 | SDOperand Src = getValue(SrcV); |
| 1000 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | 369e6db | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 1001 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 1002 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame^] | 1005 | /// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot |
| 1006 | /// access memory and has no other side effects at all. |
| 1007 | static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) { |
| 1008 | #define GET_NO_MEMORY_INTRINSICS |
| 1009 | #include "llvm/Intrinsics.gen" |
| 1010 | #undef GET_NO_MEMORY_INTRINSICS |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
| 1014 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 1015 | /// node. |
| 1016 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
| 1017 | unsigned Intrinsic) { |
| 1018 | bool HasChain = IntrinsicCannotAccessMemory(Intrinsic); |
| 1019 | |
| 1020 | // Build the operand list. |
| 1021 | std::vector<SDOperand> Ops; |
| 1022 | if (HasChain) // If this intrinsic has side-effects, chainify it. |
| 1023 | Ops.push_back(getRoot()); |
| 1024 | |
| 1025 | // Add the intrinsic ID as an integer operand. |
| 1026 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 1027 | |
| 1028 | // Add all operands of the call to the operand list. |
| 1029 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1030 | SDOperand Op = getValue(I.getOperand(i)); |
| 1031 | |
| 1032 | // If this is a vector type, force it to the right packed type. |
| 1033 | if (Op.getValueType() == MVT::Vector) { |
| 1034 | const PackedType *OpTy = cast<PackedType>(I.getOperand(i)->getType()); |
| 1035 | MVT::ValueType EltVT = TLI.getValueType(OpTy->getElementType()); |
| 1036 | |
| 1037 | MVT::ValueType VVT = MVT::getVectorType(EltVT, OpTy->getNumElements()); |
| 1038 | assert(VVT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 1039 | Op = DAG.getNode(ISD::VBIT_CONVERT, VVT, Op); |
| 1040 | } |
| 1041 | |
| 1042 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 1043 | "Intrinsic uses a non-legal type?"); |
| 1044 | Ops.push_back(Op); |
| 1045 | } |
| 1046 | |
| 1047 | std::vector<MVT::ValueType> VTs; |
| 1048 | if (I.getType() != Type::VoidTy) { |
| 1049 | MVT::ValueType VT = TLI.getValueType(I.getType()); |
| 1050 | if (VT == MVT::Vector) { |
| 1051 | const PackedType *DestTy = cast<PackedType>(I.getType()); |
| 1052 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 1053 | |
| 1054 | VT = MVT::getVectorType(EltVT, DestTy->getNumElements()); |
| 1055 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 1056 | } |
| 1057 | |
| 1058 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
| 1059 | VTs.push_back(VT); |
| 1060 | } |
| 1061 | if (HasChain) |
| 1062 | VTs.push_back(MVT::Other); |
| 1063 | |
| 1064 | // Create the node. |
| 1065 | SDOperand Result = DAG.getNode(ISD::INTRINSIC, VTs, Ops); |
| 1066 | |
| 1067 | if (HasChain) |
| 1068 | DAG.setRoot(Result.getValue(Result.Val->getNumValues()-1)); |
| 1069 | if (I.getType() != Type::VoidTy) { |
| 1070 | if (const PackedType *PTy = dyn_cast<PackedType>(I.getType())) { |
| 1071 | MVT::ValueType EVT = TLI.getValueType(PTy->getElementType()); |
| 1072 | Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result, |
| 1073 | DAG.getConstant(PTy->getNumElements(), MVT::i32), |
| 1074 | DAG.getValueType(EVT)); |
| 1075 | } |
| 1076 | setValue(&I, Result); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1080 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 1081 | /// we want to emit this as a call to a named external function, return the name |
| 1082 | /// otherwise lower it and return null. |
| 1083 | const char * |
| 1084 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 1085 | switch (Intrinsic) { |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame^] | 1086 | default: |
| 1087 | // By default, turn this into a target intrinsic node. |
| 1088 | visitTargetIntrinsic(I, Intrinsic); |
| 1089 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1090 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 1091 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 1092 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 1093 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0; |
| 1094 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return 0; |
| 1095 | case Intrinsic::setjmp: |
| 1096 | return "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 1097 | break; |
| 1098 | case Intrinsic::longjmp: |
| 1099 | return "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 1100 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 1101 | case Intrinsic::memcpy_i32: |
| 1102 | case Intrinsic::memcpy_i64: |
| 1103 | visitMemIntrinsic(I, ISD::MEMCPY); |
| 1104 | return 0; |
| 1105 | case Intrinsic::memset_i32: |
| 1106 | case Intrinsic::memset_i64: |
| 1107 | visitMemIntrinsic(I, ISD::MEMSET); |
| 1108 | return 0; |
| 1109 | case Intrinsic::memmove_i32: |
| 1110 | case Intrinsic::memmove_i64: |
| 1111 | visitMemIntrinsic(I, ISD::MEMMOVE); |
| 1112 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 1114 | case Intrinsic::dbg_stoppoint: { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1115 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1116 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
| 1117 | if (DebugInfo && DebugInfo->Verify(SPI.getContext())) { |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1118 | std::vector<SDOperand> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1119 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1120 | Ops.push_back(getRoot()); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1121 | Ops.push_back(getValue(SPI.getLineValue())); |
| 1122 | Ops.push_back(getValue(SPI.getColumnValue())); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1123 | |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1124 | DebugInfoDesc *DD = DebugInfo->getDescFor(SPI.getContext()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1125 | assert(DD && "Not a debug information descriptor"); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1126 | CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD); |
| 1127 | |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 1128 | Ops.push_back(DAG.getString(CompileUnit->getFileName())); |
| 1129 | Ops.push_back(DAG.getString(CompileUnit->getDirectory())); |
| 1130 | |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1131 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops)); |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 1132 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 1134 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1135 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1136 | case Intrinsic::dbg_region_start: { |
| 1137 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 1138 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
| 1139 | if (DebugInfo && DebugInfo->Verify(RSI.getContext())) { |
| 1140 | std::vector<SDOperand> Ops; |
| 1141 | |
| 1142 | unsigned LabelID = DebugInfo->RecordRegionStart(RSI.getContext()); |
| 1143 | |
| 1144 | Ops.push_back(getRoot()); |
| 1145 | Ops.push_back(DAG.getConstant(LabelID, MVT::i32)); |
| 1146 | |
| 1147 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops)); |
| 1148 | } |
| 1149 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 1150 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1151 | } |
| 1152 | case Intrinsic::dbg_region_end: { |
| 1153 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 1154 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
| 1155 | if (DebugInfo && DebugInfo->Verify(REI.getContext())) { |
| 1156 | std::vector<SDOperand> Ops; |
| 1157 | |
| 1158 | unsigned LabelID = DebugInfo->RecordRegionEnd(REI.getContext()); |
| 1159 | |
| 1160 | Ops.push_back(getRoot()); |
| 1161 | Ops.push_back(DAG.getConstant(LabelID, MVT::i32)); |
| 1162 | |
| 1163 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops)); |
| 1164 | } |
| 1165 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 1166 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1167 | } |
| 1168 | case Intrinsic::dbg_func_start: { |
| 1169 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 1170 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
| 1171 | if (DebugInfo && DebugInfo->Verify(FSI.getSubprogram())) { |
| 1172 | std::vector<SDOperand> Ops; |
| 1173 | |
| 1174 | unsigned LabelID = DebugInfo->RecordRegionStart(FSI.getSubprogram()); |
| 1175 | |
| 1176 | Ops.push_back(getRoot()); |
| 1177 | Ops.push_back(DAG.getConstant(LabelID, MVT::i32)); |
| 1178 | |
| 1179 | DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops)); |
| 1180 | } |
| 1181 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 1182 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 1183 | } |
| 1184 | case Intrinsic::dbg_declare: { |
| 1185 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
| 1186 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
| 1187 | if (DebugInfo && DebugInfo->Verify(DI.getVariable())) { |
| 1188 | std::vector<SDOperand> Ops; |
| 1189 | |
| 1190 | SDOperand AllocaOp = getValue(I.getOperand(1)); |
| 1191 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AllocaOp)) { |
| 1192 | DebugInfo->RecordVariable(DI.getVariable(), FI->getIndex()); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | return 0; |
| 1197 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1198 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 1199 | case Intrinsic::isunordered_f32: |
| 1200 | case Intrinsic::isunordered_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1201 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 1202 | getValue(I.getOperand(2)), ISD::SETUO)); |
| 1203 | return 0; |
| 1204 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 1205 | case Intrinsic::sqrt_f32: |
| 1206 | case Intrinsic::sqrt_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1207 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 1208 | getValue(I.getOperand(1)).getValueType(), |
| 1209 | getValue(I.getOperand(1)))); |
| 1210 | return 0; |
| 1211 | case Intrinsic::pcmarker: { |
| 1212 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 1213 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 1214 | return 0; |
| 1215 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 1216 | case Intrinsic::readcyclecounter: { |
| 1217 | std::vector<MVT::ValueType> VTs; |
| 1218 | VTs.push_back(MVT::i64); |
| 1219 | VTs.push_back(MVT::Other); |
| 1220 | std::vector<SDOperand> Ops; |
| 1221 | Ops.push_back(getRoot()); |
| 1222 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops); |
| 1223 | setValue(&I, Tmp); |
| 1224 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1225 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 1226 | } |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 1227 | case Intrinsic::bswap_i16: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 1228 | case Intrinsic::bswap_i32: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 1229 | case Intrinsic::bswap_i64: |
| 1230 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 1231 | getValue(I.getOperand(1)).getValueType(), |
| 1232 | getValue(I.getOperand(1)))); |
| 1233 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 1234 | case Intrinsic::cttz_i8: |
| 1235 | case Intrinsic::cttz_i16: |
| 1236 | case Intrinsic::cttz_i32: |
| 1237 | case Intrinsic::cttz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1238 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 1239 | getValue(I.getOperand(1)).getValueType(), |
| 1240 | getValue(I.getOperand(1)))); |
| 1241 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 1242 | case Intrinsic::ctlz_i8: |
| 1243 | case Intrinsic::ctlz_i16: |
| 1244 | case Intrinsic::ctlz_i32: |
| 1245 | case Intrinsic::ctlz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1246 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 1247 | getValue(I.getOperand(1)).getValueType(), |
| 1248 | getValue(I.getOperand(1)))); |
| 1249 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 1250 | case Intrinsic::ctpop_i8: |
| 1251 | case Intrinsic::ctpop_i16: |
| 1252 | case Intrinsic::ctpop_i32: |
| 1253 | case Intrinsic::ctpop_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1254 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 1255 | getValue(I.getOperand(1)).getValueType(), |
| 1256 | getValue(I.getOperand(1)))); |
| 1257 | return 0; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1258 | case Intrinsic::stacksave: { |
| 1259 | std::vector<MVT::ValueType> VTs; |
| 1260 | VTs.push_back(TLI.getPointerTy()); |
| 1261 | VTs.push_back(MVT::Other); |
| 1262 | std::vector<SDOperand> Ops; |
| 1263 | Ops.push_back(getRoot()); |
| 1264 | SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, VTs, Ops); |
| 1265 | setValue(&I, Tmp); |
| 1266 | DAG.setRoot(Tmp.getValue(1)); |
| 1267 | return 0; |
| 1268 | } |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 1269 | case Intrinsic::stackrestore: { |
| 1270 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 1271 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1272 | return 0; |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 1273 | } |
Chris Lattner | ac22c83 | 2005-12-12 22:51:16 +0000 | [diff] [blame] | 1274 | case Intrinsic::prefetch: |
| 1275 | // FIXME: Currently discarding prefetches. |
| 1276 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1281 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1282 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1283 | if (Function *F = I.getCalledFunction()) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1284 | if (F->isExternal()) |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1285 | if (unsigned IID = F->getIntrinsicID()) { |
| 1286 | RenameFn = visitIntrinsicCall(I, IID); |
| 1287 | if (!RenameFn) |
| 1288 | return; |
| 1289 | } else { // Not an LLVM intrinsic. |
| 1290 | const std::string &Name = F->getName(); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 1291 | if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) { |
| 1292 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 1293 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1294 | I.getType() == I.getOperand(1)->getType() && |
| 1295 | I.getType() == I.getOperand(2)->getType()) { |
| 1296 | SDOperand LHS = getValue(I.getOperand(1)); |
| 1297 | SDOperand RHS = getValue(I.getOperand(2)); |
| 1298 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(), |
| 1299 | LHS, RHS)); |
| 1300 | return; |
| 1301 | } |
| 1302 | } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1303 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1304 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1305 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1306 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1307 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 1308 | return; |
| 1309 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1310 | } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1311 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1312 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 1313 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1314 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1315 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 1316 | return; |
| 1317 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1318 | } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1319 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1320 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 1321 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1322 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1323 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 1324 | return; |
| 1325 | } |
| 1326 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 1327 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1328 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 1329 | visitInlineAsm(I); |
| 1330 | return; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1331 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1333 | SDOperand Callee; |
| 1334 | if (!RenameFn) |
| 1335 | Callee = getValue(I.getOperand(0)); |
| 1336 | else |
| 1337 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1338 | std::vector<std::pair<SDOperand, const Type*> > Args; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1339 | Args.reserve(I.getNumOperands()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1340 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1341 | Value *Arg = I.getOperand(i); |
| 1342 | SDOperand ArgNode = getValue(Arg); |
| 1343 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 1344 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1345 | |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1346 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 1347 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1349 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 9092fa3 | 2005-05-12 19:56:57 +0000 | [diff] [blame] | 1350 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(), |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1351 | I.isTailCall(), Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1352 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1353 | setValue(&I, Result.first); |
| 1354 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1357 | SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1358 | SDOperand &Chain, SDOperand &Flag)const{ |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1359 | SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag); |
| 1360 | Chain = Val.getValue(1); |
| 1361 | Flag = Val.getValue(2); |
| 1362 | |
| 1363 | // If the result was expanded, copy from the top part. |
| 1364 | if (Regs.size() > 1) { |
| 1365 | assert(Regs.size() == 2 && |
| 1366 | "Cannot expand to more than 2 elts yet!"); |
| 1367 | SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag); |
| 1368 | Chain = Val.getValue(1); |
| 1369 | Flag = Val.getValue(2); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1370 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 1371 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi); |
| 1372 | else |
| 1373 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1374 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1376 | // Otherwise, if the return value was promoted, truncate it to the |
| 1377 | // appropriate type. |
| 1378 | if (RegVT == ValueVT) |
| 1379 | return Val; |
| 1380 | |
| 1381 | if (MVT::isInteger(RegVT)) |
| 1382 | return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 1383 | else |
| 1384 | return DAG.getNode(ISD::FP_ROUND, ValueVT, Val); |
| 1385 | } |
| 1386 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1387 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 1388 | /// specified value into the registers specified by this object. This uses |
| 1389 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 1390 | void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1391 | SDOperand &Chain, SDOperand &Flag) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1392 | if (Regs.size() == 1) { |
| 1393 | // If there is a single register and the types differ, this must be |
| 1394 | // a promotion. |
| 1395 | if (RegVT != ValueVT) { |
| 1396 | if (MVT::isInteger(RegVT)) |
| 1397 | Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val); |
| 1398 | else |
| 1399 | Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val); |
| 1400 | } |
| 1401 | Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag); |
| 1402 | Flag = Chain.getValue(1); |
| 1403 | } else { |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1404 | std::vector<unsigned> R(Regs); |
| 1405 | if (!DAG.getTargetLoweringInfo().isLittleEndian()) |
| 1406 | std::reverse(R.begin(), R.end()); |
| 1407 | |
| 1408 | for (unsigned i = 0, e = R.size(); i != e; ++i) { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1409 | SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val, |
| 1410 | DAG.getConstant(i, MVT::i32)); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1411 | Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1412 | Flag = Chain.getValue(1); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1417 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 1418 | /// operand list. This adds the code marker and includes the number of |
| 1419 | /// values added into it. |
| 1420 | void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 1421 | std::vector<SDOperand> &Ops) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1422 | Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32)); |
| 1423 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 1424 | Ops.push_back(DAG.getRegister(Regs[i], RegVT)); |
| 1425 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1426 | |
| 1427 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 1428 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 1429 | /// register class for the register. Otherwise, return null. |
| 1430 | static const TargetRegisterClass * |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 1431 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 1432 | const TargetLowering &TLI, const MRegisterInfo *MRI) { |
| 1433 | for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(), |
| 1434 | E = MRI->regclass_end(); RCI != E; ++RCI) { |
| 1435 | const TargetRegisterClass *RC = *RCI; |
| 1436 | // If none of the the value types for this register class are valid, we |
| 1437 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
| 1438 | bool isLegal = false; |
| 1439 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 1440 | I != E; ++I) { |
| 1441 | if (TLI.isTypeLegal(*I)) { |
| 1442 | isLegal = true; |
| 1443 | break; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | if (!isLegal) continue; |
| 1448 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1449 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 1450 | // frame pointer in functions that need it (due to them not being taken |
| 1451 | // out of allocation, because a variable sized allocation hasn't been seen |
| 1452 | // yet). This is a slight code pessimization, but should still work. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 1453 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 1454 | E = RC->allocation_order_end(MF); I != E; ++I) |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1455 | if (*I == Reg) |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 1456 | return RC; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1457 | } |
| 1458 | return 0; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | RegsForValue SelectionDAGLowering:: |
| 1462 | GetRegistersForValue(const std::string &ConstrCode, |
| 1463 | MVT::ValueType VT, bool isOutReg, bool isInReg, |
| 1464 | std::set<unsigned> &OutputRegs, |
| 1465 | std::set<unsigned> &InputRegs) { |
| 1466 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 1467 | TLI.getRegForInlineAsmConstraint(ConstrCode, VT); |
| 1468 | std::vector<unsigned> Regs; |
| 1469 | |
| 1470 | unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1; |
| 1471 | MVT::ValueType RegVT; |
| 1472 | MVT::ValueType ValueVT = VT; |
| 1473 | |
| 1474 | if (PhysReg.first) { |
| 1475 | if (VT == MVT::Other) |
| 1476 | ValueVT = *PhysReg.second->vt_begin(); |
| 1477 | RegVT = VT; |
| 1478 | |
| 1479 | // This is a explicit reference to a physical register. |
| 1480 | Regs.push_back(PhysReg.first); |
| 1481 | |
| 1482 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 1483 | if (NumRegs != 1) { |
| 1484 | RegVT = *PhysReg.second->vt_begin(); |
| 1485 | TargetRegisterClass::iterator I = PhysReg.second->begin(); |
| 1486 | TargetRegisterClass::iterator E = PhysReg.second->end(); |
| 1487 | for (; *I != PhysReg.first; ++I) |
| 1488 | assert(I != E && "Didn't find reg!"); |
| 1489 | |
| 1490 | // Already added the first reg. |
| 1491 | --NumRegs; ++I; |
| 1492 | for (; NumRegs; --NumRegs, ++I) { |
| 1493 | assert(I != E && "Ran out of registers to allocate!"); |
| 1494 | Regs.push_back(*I); |
| 1495 | } |
| 1496 | } |
| 1497 | return RegsForValue(Regs, RegVT, ValueVT); |
| 1498 | } |
| 1499 | |
| 1500 | // This is a reference to a register class. Allocate NumRegs consecutive, |
| 1501 | // available, registers from the class. |
| 1502 | std::vector<unsigned> RegClassRegs = |
| 1503 | TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT); |
| 1504 | |
| 1505 | const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo(); |
| 1506 | MachineFunction &MF = *CurMBB->getParent(); |
| 1507 | unsigned NumAllocated = 0; |
| 1508 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 1509 | unsigned Reg = RegClassRegs[i]; |
| 1510 | // See if this register is available. |
| 1511 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 1512 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 1513 | // Make sure we find consecutive registers. |
| 1514 | NumAllocated = 0; |
| 1515 | continue; |
| 1516 | } |
| 1517 | |
| 1518 | // Check to see if this register is allocatable (i.e. don't give out the |
| 1519 | // stack pointer). |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 1520 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1521 | if (!RC) { |
| 1522 | // Make sure we find consecutive registers. |
| 1523 | NumAllocated = 0; |
| 1524 | continue; |
| 1525 | } |
| 1526 | |
| 1527 | // Okay, this register is good, we can use it. |
| 1528 | ++NumAllocated; |
| 1529 | |
| 1530 | // If we allocated enough consecutive |
| 1531 | if (NumAllocated == NumRegs) { |
| 1532 | unsigned RegStart = (i-NumAllocated)+1; |
| 1533 | unsigned RegEnd = i+1; |
| 1534 | // Mark all of the allocated registers used. |
| 1535 | for (unsigned i = RegStart; i != RegEnd; ++i) { |
| 1536 | unsigned Reg = RegClassRegs[i]; |
| 1537 | Regs.push_back(Reg); |
| 1538 | if (isOutReg) OutputRegs.insert(Reg); // Mark reg used. |
| 1539 | if (isInReg) InputRegs.insert(Reg); // Mark reg used. |
| 1540 | } |
| 1541 | |
| 1542 | return RegsForValue(Regs, *RC->vt_begin(), VT); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Otherwise, we couldn't allocate enough registers for this. |
| 1547 | return RegsForValue(); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1551 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 1552 | /// |
| 1553 | void SelectionDAGLowering::visitInlineAsm(CallInst &I) { |
| 1554 | InlineAsm *IA = cast<InlineAsm>(I.getOperand(0)); |
| 1555 | |
| 1556 | SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 1557 | MVT::Other); |
| 1558 | |
| 1559 | // Note, we treat inline asms both with and without side-effects as the same. |
| 1560 | // If an inline asm doesn't have side effects and doesn't access memory, we |
| 1561 | // could not choose to not chain it. |
| 1562 | bool hasSideEffects = IA->hasSideEffects(); |
| 1563 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1564 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1565 | std::vector<MVT::ValueType> ConstraintVTs; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1566 | |
| 1567 | /// AsmNodeOperands - A list of pairs. The first element is a register, the |
| 1568 | /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set |
| 1569 | /// if it is a def of that register. |
| 1570 | std::vector<SDOperand> AsmNodeOperands; |
| 1571 | AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain |
| 1572 | AsmNodeOperands.push_back(AsmStr); |
| 1573 | |
| 1574 | SDOperand Chain = getRoot(); |
| 1575 | SDOperand Flag; |
| 1576 | |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1577 | // We fully assign registers here at isel time. This is not optimal, but |
| 1578 | // should work. For register classes that correspond to LLVM classes, we |
| 1579 | // could let the LLVM RA do its thing, but we currently don't. Do a prepass |
| 1580 | // over the constraints, collecting fixed registers that we know we can't use. |
| 1581 | std::set<unsigned> OutputRegs, InputRegs; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1582 | unsigned OpNum = 1; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1583 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 1584 | assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!"); |
| 1585 | std::string &ConstraintCode = Constraints[i].Codes[0]; |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1587 | MVT::ValueType OpVT; |
| 1588 | |
| 1589 | // Compute the value type for each operand and add it to ConstraintVTs. |
| 1590 | switch (Constraints[i].Type) { |
| 1591 | case InlineAsm::isOutput: |
| 1592 | if (!Constraints[i].isIndirectOutput) { |
| 1593 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
| 1594 | OpVT = TLI.getValueType(I.getType()); |
| 1595 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 1596 | const Type *OpTy = I.getOperand(OpNum)->getType(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1597 | OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType()); |
| 1598 | OpNum++; // Consumes a call operand. |
| 1599 | } |
| 1600 | break; |
| 1601 | case InlineAsm::isInput: |
| 1602 | OpVT = TLI.getValueType(I.getOperand(OpNum)->getType()); |
| 1603 | OpNum++; // Consumes a call operand. |
| 1604 | break; |
| 1605 | case InlineAsm::isClobber: |
| 1606 | OpVT = MVT::Other; |
| 1607 | break; |
| 1608 | } |
| 1609 | |
| 1610 | ConstraintVTs.push_back(OpVT); |
| 1611 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1612 | if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0) |
| 1613 | continue; // Not assigned a fixed reg. |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1615 | // Build a list of regs that this operand uses. This always has a single |
| 1616 | // element for promoted/expanded operands. |
| 1617 | RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT, |
| 1618 | false, false, |
| 1619 | OutputRegs, InputRegs); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1620 | |
| 1621 | switch (Constraints[i].Type) { |
| 1622 | case InlineAsm::isOutput: |
| 1623 | // We can't assign any other output to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1624 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1625 | // If this is an early-clobber output, it cannot be assigned to the same |
| 1626 | // value as the input reg. |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 1627 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1628 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1629 | break; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1630 | case InlineAsm::isInput: |
| 1631 | // We can't assign any other input to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1632 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1633 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1634 | case InlineAsm::isClobber: |
| 1635 | // Clobbered regs cannot be used as inputs or outputs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1636 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
| 1637 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1638 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 1642 | // Loop over all of the inputs, copying the operand values into the |
| 1643 | // appropriate registers and processing the output regs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1644 | RegsForValue RetValRegs; |
| 1645 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1646 | OpNum = 1; |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1648 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1649 | assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!"); |
| 1650 | std::string &ConstraintCode = Constraints[i].Codes[0]; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1652 | switch (Constraints[i].Type) { |
| 1653 | case InlineAsm::isOutput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 1654 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 1655 | if (ConstraintCode.size() == 1) // not a physreg name. |
| 1656 | CTy = TLI.getConstraintType(ConstraintCode[0]); |
| 1657 | |
| 1658 | if (CTy == TargetLowering::C_Memory) { |
| 1659 | // Memory output. |
| 1660 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
| 1661 | |
| 1662 | // Check that the operand (the address to store to) isn't a float. |
| 1663 | if (!MVT::isInteger(InOperandVal.getValueType())) |
| 1664 | assert(0 && "MATCH FAIL!"); |
| 1665 | |
| 1666 | if (!Constraints[i].isIndirectOutput) |
| 1667 | assert(0 && "MATCH FAIL!"); |
| 1668 | |
| 1669 | OpNum++; // Consumes a call operand. |
| 1670 | |
| 1671 | // Extend/truncate to the right pointer type if needed. |
| 1672 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 1673 | if (InOperandVal.getValueType() < PtrType) |
| 1674 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 1675 | else if (InOperandVal.getValueType() > PtrType) |
| 1676 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 1677 | |
| 1678 | // Add information to the INLINEASM node to know about this output. |
| 1679 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 1680 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 1681 | AsmNodeOperands.push_back(InOperandVal); |
| 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | // Otherwise, this is a register output. |
| 1686 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 1687 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1688 | // If this is an early-clobber output, or if there is an input |
| 1689 | // constraint that matches this, we need to reserve the input register |
| 1690 | // so no other inputs allocate to it. |
| 1691 | bool UsesInputRegister = false; |
| 1692 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
| 1693 | UsesInputRegister = true; |
| 1694 | |
| 1695 | // Copy the output from the appropriate register. Find a register that |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1696 | // we can use. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1697 | RegsForValue Regs = |
| 1698 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 1699 | true, UsesInputRegister, |
| 1700 | OutputRegs, InputRegs); |
| 1701 | assert(!Regs.Regs.empty() && "Couldn't allocate output reg!"); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1702 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1703 | if (!Constraints[i].isIndirectOutput) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1704 | assert(RetValRegs.Regs.empty() && |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1705 | "Cannot have multiple output constraints yet!"); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1706 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1707 | RetValRegs = Regs; |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1708 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 1709 | IndirectStoresToEmit.push_back(std::make_pair(Regs, |
| 1710 | I.getOperand(OpNum))); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1711 | OpNum++; // Consumes a call operand. |
| 1712 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Add information to the INLINEASM node to know that this register is |
| 1715 | // set. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1716 | Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1717 | break; |
| 1718 | } |
| 1719 | case InlineAsm::isInput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 1720 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 1721 | OpNum++; // Consumes a call operand. |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 1723 | if (isdigit(ConstraintCode[0])) { // Matching constraint? |
| 1724 | // If this is required to match an output register we have already set, |
| 1725 | // just use its register. |
| 1726 | unsigned OperandNo = atoi(ConstraintCode.c_str()); |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1728 | // Scan until we find the definition we already emitted of this operand. |
| 1729 | // When we find it, create a RegsForValue operand. |
| 1730 | unsigned CurOp = 2; // The first operand. |
| 1731 | for (; OperandNo; --OperandNo) { |
| 1732 | // Advance to the next operand. |
| 1733 | unsigned NumOps = |
| 1734 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
| 1735 | assert((NumOps & 7) == 2 /*REGDEF*/ && |
| 1736 | "Skipped past definitions?"); |
| 1737 | CurOp += (NumOps>>3)+1; |
| 1738 | } |
| 1739 | |
| 1740 | unsigned NumOps = |
| 1741 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
| 1742 | assert((NumOps & 7) == 2 /*REGDEF*/ && |
| 1743 | "Skipped past definitions?"); |
| 1744 | |
| 1745 | // Add NumOps>>3 registers to MatchedRegs. |
| 1746 | RegsForValue MatchedRegs; |
| 1747 | MatchedRegs.ValueVT = InOperandVal.getValueType(); |
| 1748 | MatchedRegs.RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 1749 | for (unsigned i = 0, e = NumOps>>3; i != e; ++i) { |
| 1750 | unsigned Reg=cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg(); |
| 1751 | MatchedRegs.Regs.push_back(Reg); |
| 1752 | } |
| 1753 | |
| 1754 | // Use the produced MatchedRegs object to |
| 1755 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag); |
| 1756 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1757 | break; |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 1758 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 1759 | |
| 1760 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 1761 | if (ConstraintCode.size() == 1) // not a physreg name. |
| 1762 | CTy = TLI.getConstraintType(ConstraintCode[0]); |
| 1763 | |
| 1764 | if (CTy == TargetLowering::C_Other) { |
| 1765 | if (!TLI.isOperandValidForConstraint(InOperandVal, ConstraintCode[0])) |
| 1766 | assert(0 && "MATCH FAIL!"); |
| 1767 | |
| 1768 | // Add information to the INLINEASM node to know about this input. |
| 1769 | unsigned ResOpType = 3 /*IMM*/ | (1 << 3); |
| 1770 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 1771 | AsmNodeOperands.push_back(InOperandVal); |
| 1772 | break; |
| 1773 | } else if (CTy == TargetLowering::C_Memory) { |
| 1774 | // Memory input. |
| 1775 | |
| 1776 | // Check that the operand isn't a float. |
| 1777 | if (!MVT::isInteger(InOperandVal.getValueType())) |
| 1778 | assert(0 && "MATCH FAIL!"); |
| 1779 | |
| 1780 | // Extend/truncate to the right pointer type if needed. |
| 1781 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 1782 | if (InOperandVal.getValueType() < PtrType) |
| 1783 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 1784 | else if (InOperandVal.getValueType() > PtrType) |
| 1785 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 1786 | |
| 1787 | // Add information to the INLINEASM node to know about this input. |
| 1788 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 1789 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 1790 | AsmNodeOperands.push_back(InOperandVal); |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 1795 | |
| 1796 | // Copy the input into the appropriate registers. |
| 1797 | RegsForValue InRegs = |
| 1798 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 1799 | false, true, OutputRegs, InputRegs); |
| 1800 | // FIXME: should be match fail. |
| 1801 | assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!"); |
| 1802 | |
| 1803 | InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag); |
| 1804 | |
| 1805 | InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1806 | break; |
| 1807 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1808 | case InlineAsm::isClobber: { |
| 1809 | RegsForValue ClobberedRegs = |
| 1810 | GetRegistersForValue(ConstraintCode, MVT::Other, false, false, |
| 1811 | OutputRegs, InputRegs); |
| 1812 | // Add the clobbered value to the operand list, so that the register |
| 1813 | // allocator is aware that the physreg got clobbered. |
| 1814 | if (!ClobberedRegs.Regs.empty()) |
| 1815 | ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1818 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1819 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Finish up input operands. |
| 1822 | AsmNodeOperands[0] = Chain; |
| 1823 | if (Flag.Val) AsmNodeOperands.push_back(Flag); |
| 1824 | |
| 1825 | std::vector<MVT::ValueType> VTs; |
| 1826 | VTs.push_back(MVT::Other); |
| 1827 | VTs.push_back(MVT::Flag); |
| 1828 | Chain = DAG.getNode(ISD::INLINEASM, VTs, AsmNodeOperands); |
| 1829 | Flag = Chain.getValue(1); |
| 1830 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1831 | // If this asm returns a register value, copy the result from that register |
| 1832 | // and set it as the value of the call. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1833 | if (!RetValRegs.Regs.empty()) |
| 1834 | setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1836 | std::vector<std::pair<SDOperand, Value*> > StoresToEmit; |
| 1837 | |
| 1838 | // Process indirect outputs, first output all of the flagged copies out of |
| 1839 | // physregs. |
| 1840 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1841 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1842 | Value *Ptr = IndirectStoresToEmit[i].second; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 1843 | SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag); |
| 1844 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // Emit the non-flagged stores from the physregs. |
| 1848 | std::vector<SDOperand> OutChains; |
| 1849 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
| 1850 | OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1851 | StoresToEmit[i].first, |
| 1852 | getValue(StoresToEmit[i].second), |
| 1853 | DAG.getSrcValue(StoresToEmit[i].second))); |
| 1854 | if (!OutChains.empty()) |
| 1855 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1856 | DAG.setRoot(Chain); |
| 1857 | } |
| 1858 | |
| 1859 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1860 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 1861 | SDOperand Src = getValue(I.getOperand(0)); |
| 1862 | |
| 1863 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1864 | |
| 1865 | if (IntPtr < Src.getValueType()) |
| 1866 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 1867 | else if (IntPtr > Src.getValueType()) |
| 1868 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1869 | |
| 1870 | // Scale the source by the type size. |
| 1871 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 1872 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 1873 | Src, getIntPtrConstant(ElementSize)); |
| 1874 | |
| 1875 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1876 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1877 | |
| 1878 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1879 | TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1880 | DAG.getExternalSymbol("malloc", IntPtr), |
| 1881 | Args, DAG); |
| 1882 | setValue(&I, Result.first); // Pointers always fit in registers |
| 1883 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 1887 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1888 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 1889 | TLI.getTargetData().getIntPtrType())); |
| 1890 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1891 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1892 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1893 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 1894 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 1897 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 1898 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 1899 | // instructions are special in various ways, which require special support to |
| 1900 | // insert. The specified MachineInstr is created but not inserted into any |
| 1901 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 1902 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1903 | MachineBasicBlock *MBB) { |
| 1904 | std::cerr << "If a target marks an instruction with " |
| 1905 | "'usesCustomDAGSchedInserter', it must implement " |
| 1906 | "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
| 1907 | abort(); |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1911 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1912 | DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), |
| 1913 | getValue(I.getOperand(1)), |
| 1914 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1918 | SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(), |
| 1919 | getValue(I.getOperand(0)), |
| 1920 | DAG.getSrcValue(I.getOperand(0))); |
| 1921 | setValue(&I, V); |
| 1922 | DAG.setRoot(V.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1926 | DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(), |
| 1927 | getValue(I.getOperand(1)), |
| 1928 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1932 | DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), |
| 1933 | getValue(I.getOperand(1)), |
| 1934 | getValue(I.getOperand(2)), |
| 1935 | DAG.getSrcValue(I.getOperand(1)), |
| 1936 | DAG.getSrcValue(I.getOperand(2)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1939 | // It is always conservatively correct for llvm.returnaddress and |
| 1940 | // llvm.frameaddress to return 0. |
| 1941 | std::pair<SDOperand, SDOperand> |
| 1942 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 1943 | unsigned Depth, SelectionDAG &DAG) { |
| 1944 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 1947 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1948 | assert(0 && "LowerOperation not implemented for this target!"); |
| 1949 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1950 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 1953 | SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op, |
| 1954 | SelectionDAG &DAG) { |
| 1955 | assert(0 && "CustomPromoteOperation not implemented for this target!"); |
| 1956 | abort(); |
| 1957 | return SDOperand(); |
| 1958 | } |
| 1959 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1960 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 1961 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 1962 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1963 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1964 | setValue(&I, Result.first); |
| 1965 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 1968 | /// getMemsetValue - Vectorized representation of the memset value |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 1969 | /// operand. |
| 1970 | static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT, |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 1971 | SelectionDAG &DAG) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 1972 | MVT::ValueType CurVT = VT; |
| 1973 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { |
| 1974 | uint64_t Val = C->getValue() & 255; |
| 1975 | unsigned Shift = 8; |
| 1976 | while (CurVT != MVT::i8) { |
| 1977 | Val = (Val << Shift) | Val; |
| 1978 | Shift <<= 1; |
| 1979 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 1980 | } |
| 1981 | return DAG.getConstant(Val, VT); |
| 1982 | } else { |
| 1983 | Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value); |
| 1984 | unsigned Shift = 8; |
| 1985 | while (CurVT != MVT::i8) { |
| 1986 | Value = |
| 1987 | DAG.getNode(ISD::OR, VT, |
| 1988 | DAG.getNode(ISD::SHL, VT, Value, |
| 1989 | DAG.getConstant(Shift, MVT::i8)), Value); |
| 1990 | Shift <<= 1; |
| 1991 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | return Value; |
| 1995 | } |
| 1996 | } |
| 1997 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 1998 | /// getMemsetStringVal - Similar to getMemsetValue. Except this is only |
| 1999 | /// used when a memcpy is turned into a memset when the source is a constant |
| 2000 | /// string ptr. |
| 2001 | static SDOperand getMemsetStringVal(MVT::ValueType VT, |
| 2002 | SelectionDAG &DAG, TargetLowering &TLI, |
| 2003 | std::string &Str, unsigned Offset) { |
| 2004 | MVT::ValueType CurVT = VT; |
| 2005 | uint64_t Val = 0; |
| 2006 | unsigned MSB = getSizeInBits(VT) / 8; |
| 2007 | if (TLI.isLittleEndian()) |
| 2008 | Offset = Offset + MSB - 1; |
| 2009 | for (unsigned i = 0; i != MSB; ++i) { |
| 2010 | Val = (Val << 8) | Str[Offset]; |
| 2011 | Offset += TLI.isLittleEndian() ? -1 : 1; |
| 2012 | } |
| 2013 | return DAG.getConstant(Val, VT); |
| 2014 | } |
| 2015 | |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2016 | /// getMemBasePlusOffset - Returns base and offset node for the |
| 2017 | static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset, |
| 2018 | SelectionDAG &DAG, TargetLowering &TLI) { |
| 2019 | MVT::ValueType VT = Base.getValueType(); |
| 2020 | return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT)); |
| 2021 | } |
| 2022 | |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 2023 | /// MeetsMaxMemopRequirement - Determines if the number of memory ops required |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2024 | /// to replace the memset / memcpy is below the threshold. It also returns the |
| 2025 | /// types of the sequence of memory ops to perform memset / memcpy. |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 2026 | static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps, |
| 2027 | unsigned Limit, uint64_t Size, |
| 2028 | unsigned Align, TargetLowering &TLI) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2029 | MVT::ValueType VT; |
| 2030 | |
| 2031 | if (TLI.allowsUnalignedMemoryAccesses()) { |
| 2032 | VT = MVT::i64; |
| 2033 | } else { |
| 2034 | switch (Align & 7) { |
| 2035 | case 0: |
| 2036 | VT = MVT::i64; |
| 2037 | break; |
| 2038 | case 4: |
| 2039 | VT = MVT::i32; |
| 2040 | break; |
| 2041 | case 2: |
| 2042 | VT = MVT::i16; |
| 2043 | break; |
| 2044 | default: |
| 2045 | VT = MVT::i8; |
| 2046 | break; |
| 2047 | } |
| 2048 | } |
| 2049 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2050 | MVT::ValueType LVT = MVT::i64; |
| 2051 | while (!TLI.isTypeLegal(LVT)) |
| 2052 | LVT = (MVT::ValueType)((unsigned)LVT - 1); |
| 2053 | assert(MVT::isInteger(LVT)); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2054 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2055 | if (VT > LVT) |
| 2056 | VT = LVT; |
| 2057 | |
Evan Cheng | dea7245 | 2006-02-14 23:05:54 +0000 | [diff] [blame] | 2058 | unsigned NumMemOps = 0; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2059 | while (Size != 0) { |
| 2060 | unsigned VTSize = getSizeInBits(VT) / 8; |
| 2061 | while (VTSize > Size) { |
| 2062 | VT = (MVT::ValueType)((unsigned)VT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2063 | VTSize >>= 1; |
| 2064 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2065 | assert(MVT::isInteger(VT)); |
| 2066 | |
| 2067 | if (++NumMemOps > Limit) |
| 2068 | return false; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2069 | MemOps.push_back(VT); |
| 2070 | Size -= VTSize; |
| 2071 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2072 | |
| 2073 | return true; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 2076 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2077 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 2078 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 2079 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 2080 | SDOperand Op4 = getValue(I.getOperand(4)); |
| 2081 | unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue(); |
| 2082 | if (Align == 0) Align = 1; |
| 2083 | |
| 2084 | if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) { |
| 2085 | std::vector<MVT::ValueType> MemOps; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Expand memset / memcpy to a series of load / store ops |
| 2088 | // if the size operand falls below a certain threshold. |
| 2089 | std::vector<SDOperand> OutChains; |
| 2090 | switch (Op) { |
Evan Cheng | ac940ab | 2006-02-14 19:45:56 +0000 | [diff] [blame] | 2091 | default: break; // Do nothing for now. |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2092 | case ISD::MEMSET: { |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 2093 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(), |
| 2094 | Size->getValue(), Align, TLI)) { |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 2095 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2096 | unsigned Offset = 0; |
| 2097 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 2098 | MVT::ValueType VT = MemOps[i]; |
| 2099 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 2100 | SDOperand Value = getMemsetValue(Op2, VT, DAG); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2101 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, getRoot(), |
| 2102 | Value, |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2103 | getMemBasePlusOffset(Op1, Offset, DAG, TLI), |
| 2104 | DAG.getSrcValue(I.getOperand(1), Offset)); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2105 | OutChains.push_back(Store); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2106 | Offset += VTSize; |
| 2107 | } |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2108 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2109 | break; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2110 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2111 | case ISD::MEMCPY: { |
| 2112 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(), |
| 2113 | Size->getValue(), Align, TLI)) { |
| 2114 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2115 | unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2116 | GlobalAddressSDNode *G = NULL; |
| 2117 | std::string Str; |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2118 | bool CopyFromStr = false; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2119 | |
| 2120 | if (Op2.getOpcode() == ISD::GlobalAddress) |
| 2121 | G = cast<GlobalAddressSDNode>(Op2); |
| 2122 | else if (Op2.getOpcode() == ISD::ADD && |
| 2123 | Op2.getOperand(0).getOpcode() == ISD::GlobalAddress && |
| 2124 | Op2.getOperand(1).getOpcode() == ISD::Constant) { |
| 2125 | G = cast<GlobalAddressSDNode>(Op2.getOperand(0)); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2126 | SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue(); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2127 | } |
| 2128 | if (G) { |
| 2129 | GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal()); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2130 | if (GV) { |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 2131 | Str = GV->getStringValue(false); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2132 | if (!Str.empty()) { |
| 2133 | CopyFromStr = true; |
| 2134 | SrcOff += SrcDelta; |
| 2135 | } |
| 2136 | } |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2139 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 2140 | MVT::ValueType VT = MemOps[i]; |
| 2141 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2142 | SDOperand Value, Chain, Store; |
| 2143 | |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 2144 | if (CopyFromStr) { |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2145 | Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff); |
| 2146 | Chain = getRoot(); |
| 2147 | Store = |
| 2148 | DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 2149 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
| 2150 | DAG.getSrcValue(I.getOperand(1), DstOff)); |
| 2151 | } else { |
| 2152 | Value = DAG.getLoad(VT, getRoot(), |
| 2153 | getMemBasePlusOffset(Op2, SrcOff, DAG, TLI), |
| 2154 | DAG.getSrcValue(I.getOperand(2), SrcOff)); |
| 2155 | Chain = Value.getValue(1); |
| 2156 | Store = |
| 2157 | DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, |
| 2158 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
| 2159 | DAG.getSrcValue(I.getOperand(1), DstOff)); |
| 2160 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2161 | OutChains.push_back(Store); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 2162 | SrcOff += VTSize; |
| 2163 | DstOff += VTSize; |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | break; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | if (!OutChains.empty()) { |
| 2171 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains)); |
| 2172 | return; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 2176 | std::vector<SDOperand> Ops; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 2177 | Ops.push_back(getRoot()); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 2178 | Ops.push_back(Op1); |
| 2179 | Ops.push_back(Op2); |
| 2180 | Ops.push_back(Op3); |
| 2181 | Ops.push_back(Op4); |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 2182 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 2185 | //===----------------------------------------------------------------------===// |
| 2186 | // SelectionDAGISel code |
| 2187 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2188 | |
| 2189 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 2190 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 2191 | } |
| 2192 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 2193 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 2194 | // FIXME: we only modify the CFG to split critical edges. This |
| 2195 | // updates dom and loop info. |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 2196 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2197 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2198 | |
| 2199 | /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset, |
| 2200 | /// casting to the type of GEPI. |
| 2201 | static Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI, |
| 2202 | Value *Ptr, Value *PtrOffset) { |
| 2203 | if (V) return V; // Already computed. |
| 2204 | |
| 2205 | BasicBlock::iterator InsertPt; |
| 2206 | if (BB == GEPI->getParent()) { |
| 2207 | // If insert into the GEP's block, insert right after the GEP. |
| 2208 | InsertPt = GEPI; |
| 2209 | ++InsertPt; |
| 2210 | } else { |
| 2211 | // Otherwise, insert at the top of BB, after any PHI nodes |
| 2212 | InsertPt = BB->begin(); |
| 2213 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 2214 | } |
| 2215 | |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 2216 | // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into |
| 2217 | // BB so that there is only one value live across basic blocks (the cast |
| 2218 | // operand). |
| 2219 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 2220 | if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType())) |
| 2221 | Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt); |
| 2222 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2223 | // Add the offset, cast it to the right type. |
| 2224 | Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt); |
| 2225 | Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt); |
| 2226 | return V = Ptr; |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction |
| 2231 | /// selection, we want to be a bit careful about some things. In particular, if |
| 2232 | /// we have a GEP instruction that is used in a different block than it is |
| 2233 | /// defined, the addressing expression of the GEP cannot be folded into loads or |
| 2234 | /// stores that use it. In this case, decompose the GEP and move constant |
| 2235 | /// indices into blocks that use it. |
| 2236 | static void OptimizeGEPExpression(GetElementPtrInst *GEPI, |
| 2237 | const TargetData &TD) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2238 | // If this GEP is only used inside the block it is defined in, there is no |
| 2239 | // need to rewrite it. |
| 2240 | bool isUsedOutsideDefBB = false; |
| 2241 | BasicBlock *DefBB = GEPI->getParent(); |
| 2242 | for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); |
| 2243 | UI != E; ++UI) { |
| 2244 | if (cast<Instruction>(*UI)->getParent() != DefBB) { |
| 2245 | isUsedOutsideDefBB = true; |
| 2246 | break; |
| 2247 | } |
| 2248 | } |
| 2249 | if (!isUsedOutsideDefBB) return; |
| 2250 | |
| 2251 | // If this GEP has no non-zero constant indices, there is nothing we can do, |
| 2252 | // ignore it. |
| 2253 | bool hasConstantIndex = false; |
| 2254 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 2255 | E = GEPI->op_end(); OI != E; ++OI) { |
| 2256 | if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) |
| 2257 | if (CI->getRawValue()) { |
| 2258 | hasConstantIndex = true; |
| 2259 | break; |
| 2260 | } |
| 2261 | } |
Chris Lattner | 3802c25 | 2005-12-11 09:05:13 +0000 | [diff] [blame] | 2262 | // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses. |
| 2263 | if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the |
| 2266 | // constant offset (which we now know is non-zero) and deal with it later. |
| 2267 | uint64_t ConstantOffset = 0; |
| 2268 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 2269 | Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI); |
| 2270 | const Type *Ty = GEPI->getOperand(0)->getType(); |
| 2271 | |
| 2272 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 2273 | E = GEPI->op_end(); OI != E; ++OI) { |
| 2274 | Value *Idx = *OI; |
| 2275 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 2276 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 2277 | if (Field) |
| 2278 | ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 2279 | Ty = StTy->getElementType(Field); |
| 2280 | } else { |
| 2281 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 2282 | |
| 2283 | // Handle constant subscripts. |
| 2284 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 2285 | if (CI->getRawValue() == 0) continue; |
| 2286 | |
| 2287 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 2288 | ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 2289 | else |
| 2290 | ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 2291 | continue; |
| 2292 | } |
| 2293 | |
| 2294 | // Ptr = Ptr + Idx * ElementSize; |
| 2295 | |
| 2296 | // Cast Idx to UIntPtrTy if needed. |
| 2297 | Idx = new CastInst(Idx, UIntPtrTy, "", GEPI); |
| 2298 | |
| 2299 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 2300 | // Mask off bits that should not be set. |
| 2301 | ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 2302 | Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize); |
| 2303 | |
| 2304 | // Multiply by the element size and add to the base. |
| 2305 | Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI); |
| 2306 | Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI); |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | // Make sure that the offset fits in uintptr_t. |
| 2311 | ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 2312 | Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset); |
| 2313 | |
| 2314 | // Okay, we have now emitted all of the variable index parts to the BB that |
| 2315 | // the GEP is defined in. Loop over all of the using instructions, inserting |
| 2316 | // 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] | 2317 | // instruction to use the newly computed value, making GEPI dead. When the |
| 2318 | // user is a load or store instruction address, we emit the add into the user |
| 2319 | // block, otherwise we use a canonical version right next to the gep (these |
| 2320 | // won't be foldable as addresses, so we might as well share the computation). |
| 2321 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2322 | std::map<BasicBlock*,Value*> InsertedExprs; |
| 2323 | while (!GEPI->use_empty()) { |
| 2324 | Instruction *User = cast<Instruction>(GEPI->use_back()); |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 2325 | |
| 2326 | // If this use is not foldable into the addressing mode, use a version |
| 2327 | // emitted in the GEP block. |
| 2328 | Value *NewVal; |
| 2329 | if (!isa<LoadInst>(User) && |
| 2330 | (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) { |
| 2331 | NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, |
| 2332 | Ptr, PtrOffset); |
| 2333 | } else { |
| 2334 | // Otherwise, insert the code in the User's block so it can be folded into |
| 2335 | // any users in that block. |
| 2336 | NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2337 | User->getParent(), GEPI, |
| 2338 | Ptr, PtrOffset); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2339 | } |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 2340 | User->replaceUsesOfWith(GEPI, NewVal); |
| 2341 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2342 | |
| 2343 | // Finally, the GEP is dead, remove it. |
| 2344 | GEPI->eraseFromParent(); |
| 2345 | } |
| 2346 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2347 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 2348 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 2349 | RegMap = MF.getSSARegMap(); |
| 2350 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 2351 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2352 | // First, split all critical edges for PHI nodes with incoming values that are |
| 2353 | // constants, this way the load of the constant into a vreg will not be placed |
| 2354 | // into MBBs that are used some other way. |
| 2355 | // |
| 2356 | // In this pass we also look for GEP instructions that are used across basic |
| 2357 | // blocks and rewrites them to improve basic-block-at-a-time selection. |
| 2358 | // |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 2359 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 2360 | PHINode *PN; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2361 | BasicBlock::iterator BBI; |
| 2362 | for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI) |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 2363 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 2364 | if (isa<Constant>(PN->getIncomingValue(i))) |
| 2365 | SplitCriticalEdge(PN->getIncomingBlock(i), BB); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 2366 | |
| 2367 | for (BasicBlock::iterator E = BB->end(); BBI != E; ) |
| 2368 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++)) |
| 2369 | OptimizeGEPExpression(GEPI, TLI.getTargetData()); |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 2370 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2372 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 2373 | |
| 2374 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 2375 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2376 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2377 | return true; |
| 2378 | } |
| 2379 | |
| 2380 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2381 | SDOperand SelectionDAGISel:: |
| 2382 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 2383 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 2384 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2385 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 2386 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2387 | |
| 2388 | // If this type is not legal, we must make sure to not create an invalid |
| 2389 | // register use. |
| 2390 | MVT::ValueType SrcVT = Op.getValueType(); |
| 2391 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
| 2392 | SelectionDAG &DAG = SDL.DAG; |
| 2393 | if (SrcVT == DestVT) { |
| 2394 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 2395 | } else if (SrcVT == MVT::Vector) { |
| 2396 | // FIXME: THIS DOES NOT SUPPORT PROMOTED/EXPANDED ELEMENTS! |
| 2397 | |
| 2398 | // Figure out the right, legal destination reg to copy into. |
| 2399 | const PackedType *PTy = cast<PackedType>(V->getType()); |
| 2400 | unsigned NumElts = PTy->getNumElements(); |
| 2401 | MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType()); |
| 2402 | |
| 2403 | unsigned NumVectorRegs = 1; |
| 2404 | |
| 2405 | // Divide the input until we get to a supported size. This will always |
| 2406 | // end with a scalar if the target doesn't support vectors. |
| 2407 | while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) { |
| 2408 | NumElts >>= 1; |
| 2409 | NumVectorRegs <<= 1; |
| 2410 | } |
| 2411 | |
| 2412 | MVT::ValueType VT; |
| 2413 | if (NumElts == 1) |
| 2414 | VT = EltTy; |
| 2415 | else |
| 2416 | VT = getVectorType(EltTy, NumElts); |
| 2417 | |
| 2418 | // FIXME: THIS ASSUMES THAT THE INPUT VECTOR WILL BE LEGAL! |
| 2419 | Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op); |
| 2420 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2421 | } else if (SrcVT < DestVT) { |
| 2422 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 2423 | if (MVT::isFloatingPoint(SrcVT)) |
| 2424 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 2425 | else |
Chris Lattner | fab0887 | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 2426 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2427 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 2428 | } else { |
| 2429 | // The src value is expanded into multiple registers. |
| 2430 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 2431 | Op, DAG.getConstant(0, MVT::i32)); |
| 2432 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 2433 | Op, DAG.getConstant(1, MVT::i32)); |
| 2434 | Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo); |
| 2435 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 2436 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 2439 | void SelectionDAGISel:: |
| 2440 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 2441 | std::vector<SDOperand> &UnorderedChains) { |
| 2442 | // If this is the entry block, emit arguments. |
| 2443 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 2444 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2445 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 2446 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2448 | unsigned a = 0; |
| 2449 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 2450 | AI != E; ++AI, ++a) |
| 2451 | if (!AI->use_empty()) { |
| 2452 | SDL.setValue(AI, Args[a]); |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2454 | // If this argument is live outside of the entry block, insert a copy from |
| 2455 | // whereever we got it to the vreg that other BB's will reference it as. |
| 2456 | if (FuncInfo.ValueMap.count(AI)) { |
| 2457 | SDOperand Copy = |
| 2458 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 2459 | UnorderedChains.push_back(Copy); |
| 2460 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 2461 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2462 | |
| 2463 | // Next, if the function has live ins that need to be copied into vregs, |
| 2464 | // emit the copies now, into the top of the block. |
| 2465 | MachineFunction &MF = SDL.DAG.getMachineFunction(); |
| 2466 | if (MF.livein_begin() != MF.livein_end()) { |
| 2467 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 2468 | const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo(); |
| 2469 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 2470 | E = MF.livein_end(); LI != E; ++LI) |
| 2471 | if (LI->second) |
| 2472 | MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 2473 | LI->first, RegMap->getRegClass(LI->second)); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 2474 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2475 | |
| 2476 | // Finally, if the target has anything special to do, allow it to do so. |
| 2477 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2481 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 2482 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 2483 | FunctionLoweringInfo &FuncInfo) { |
| 2484 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2485 | |
| 2486 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 2488 | // Lower any arguments needed in this block if this is the entry block. |
| 2489 | if (LLVMBB == &LLVMBB->getParent()->front()) |
| 2490 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2491 | |
| 2492 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 2493 | SDL.setCurrentBasicBlock(BB); |
| 2494 | |
| 2495 | // Lower all of the non-terminator instructions. |
| 2496 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 2497 | I != E; ++I) |
| 2498 | SDL.visit(*I); |
| 2499 | |
| 2500 | // Ensure that all instructions which are used outside of their defining |
| 2501 | // blocks are available as virtual registers. |
| 2502 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 2503 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 2504 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2505 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2506 | UnorderedChains.push_back( |
| 2507 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 2511 | // ensure constants are generated when needed. Remember the virtual registers |
| 2512 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 2513 | // directly add them, because expansion might result in multiple MBB's for one |
| 2514 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 2515 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2516 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2517 | |
| 2518 | // Emit constants only once even if used by multiple PHI nodes. |
| 2519 | std::map<Constant*, unsigned> ConstantsOut; |
| 2520 | |
| 2521 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 2522 | // this block. |
| 2523 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 2524 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 2525 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 2526 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 2527 | PHINode *PN; |
| 2528 | |
| 2529 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 2530 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 2531 | // emitted yet. |
| 2532 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 2533 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 2534 | if (!PN->use_empty()) { |
| 2535 | unsigned Reg; |
| 2536 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 2537 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 2538 | unsigned &RegOut = ConstantsOut[C]; |
| 2539 | if (RegOut == 0) { |
| 2540 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2541 | UnorderedChains.push_back( |
| 2542 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 2543 | } |
| 2544 | Reg = RegOut; |
| 2545 | } else { |
| 2546 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 2547 | if (Reg == 0) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2548 | assert(isa<AllocaInst>(PHIOp) && |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 2549 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 2550 | "Didn't codegen value into a register!??"); |
| 2551 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2552 | UnorderedChains.push_back( |
| 2553 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 2554 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2555 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2556 | |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 2557 | // Remember that this register needs to added to the machine PHI node as |
| 2558 | // the input for this MBB. |
| 2559 | unsigned NumElements = |
| 2560 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 2561 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 2562 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2563 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2564 | } |
| 2565 | ConstantsOut.clear(); |
| 2566 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2567 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 2568 | if (!UnorderedChains.empty()) { |
Chris Lattner | 7436b57 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 2569 | SDOperand Root = SDL.getRoot(); |
| 2570 | if (Root.getOpcode() != ISD::EntryToken) { |
| 2571 | unsigned i = 0, e = UnorderedChains.size(); |
| 2572 | for (; i != e; ++i) { |
| 2573 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 2574 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 2575 | break; // Don't add the root if we already indirectly depend on it. |
| 2576 | } |
| 2577 | |
| 2578 | if (i == e) |
| 2579 | UnorderedChains.push_back(Root); |
| 2580 | } |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 2581 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 2582 | } |
| 2583 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2584 | // Lower the terminator after the copies are emitted. |
| 2585 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 2586 | |
| 2587 | // Make sure the root of the DAG is up-to-date. |
| 2588 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 2592 | FunctionLoweringInfo &FuncInfo) { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 2593 | SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2594 | CurDAG = &DAG; |
| 2595 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 2596 | |
| 2597 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 2598 | // types that are not supported by the target. |
| 2599 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 2600 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 2601 | // Run the DAG combiner in pre-legalize mode. |
| 2602 | DAG.Combine(false); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 2603 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2604 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 2605 | DEBUG(DAG.dump()); |
| 2606 | |
| 2607 | // Second step, hack on the DAG until it only uses operations and types that |
| 2608 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 2609 | DAG.Legalize(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2610 | |
| 2611 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 2612 | DEBUG(DAG.dump()); |
| 2613 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 2614 | // Run the DAG combiner in post-legalize mode. |
| 2615 | DAG.Combine(true); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 2616 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 2617 | if (ViewISelDAGs) DAG.viewGraph(); |
Chris Lattner | d48050a | 2005-10-05 06:09:10 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 2619 | // Third, instruction select all of the operations to machine code, adding the |
| 2620 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2621 | InstructionSelectBasicBlock(DAG); |
| 2622 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2623 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 2624 | DEBUG(BB->dump()); |
| 2625 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 2626 | // 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] | 2627 | // PHI nodes in successors. |
| 2628 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 2629 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 2630 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 2631 | "This is not a machine PHI node that we are updating!"); |
| 2632 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 2633 | PHI->addMachineBasicBlockOperand(BB); |
| 2634 | } |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 2635 | |
| 2636 | // Finally, add the CFG edges from the last selected MBB to the successor |
| 2637 | // MBBs. |
| 2638 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 2639 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 2640 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)]; |
| 2641 | BB->addSuccessor(Succ0MBB); |
| 2642 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2643 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 2644 | |
| 2645 | //===----------------------------------------------------------------------===// |
| 2646 | /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each |
| 2647 | /// target node in the graph. |
| 2648 | void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) { |
| 2649 | if (ViewSchedDAGs) DAG.viewGraph(); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2650 | ScheduleDAG *SL = NULL; |
| 2651 | |
| 2652 | switch (ISHeuristic) { |
| 2653 | default: assert(0 && "Unrecognized scheduling heuristic"); |
Evan Cheng | 3f23952 | 2006-01-25 09:12:57 +0000 | [diff] [blame] | 2654 | case defaultScheduling: |
| 2655 | if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) |
| 2656 | SL = createSimpleDAGScheduler(noScheduling, DAG, BB); |
| 2657 | else /* TargetLowering::SchedulingForRegPressure */ |
| 2658 | SL = createBURRListDAGScheduler(DAG, BB); |
| 2659 | break; |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2660 | case noScheduling: |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 2661 | SL = createBFS_DAGScheduler(DAG, BB); |
| 2662 | break; |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2663 | case simpleScheduling: |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 2664 | SL = createSimpleDAGScheduler(false, DAG, BB); |
| 2665 | break; |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2666 | case simpleNoItinScheduling: |
Chris Lattner | 20a4921 | 2006-03-10 07:49:12 +0000 | [diff] [blame] | 2667 | SL = createSimpleDAGScheduler(true, DAG, BB); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2668 | break; |
Evan Cheng | f0f9c90 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 2669 | case listSchedulingBURR: |
| 2670 | SL = createBURRListDAGScheduler(DAG, BB); |
Chris Lattner | a5de484 | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 2671 | break; |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 2672 | case listSchedulingTD: |
Chris Lattner | b0d21ef | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 2673 | SL = createTDListDAGScheduler(DAG, BB, CreateTargetHazardRecognizer()); |
Chris Lattner | a5de484 | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 2674 | break; |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 2675 | } |
Chris Lattner | a3818e6 | 2006-01-21 19:12:11 +0000 | [diff] [blame] | 2676 | BB = SL->Run(); |
Evan Cheng | cccf123 | 2006-02-04 06:49:00 +0000 | [diff] [blame] | 2677 | delete SL; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 2678 | } |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | b0d21ef | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 2680 | HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() { |
| 2681 | return new HazardRecognizer(); |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 2684 | /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated |
| 2685 | /// by tblgen. Others should not call it. |
| 2686 | void SelectionDAGISel:: |
| 2687 | SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) { |
| 2688 | std::vector<SDOperand> InOps; |
| 2689 | std::swap(InOps, Ops); |
| 2690 | |
| 2691 | Ops.push_back(InOps[0]); // input chain. |
| 2692 | Ops.push_back(InOps[1]); // input asm string. |
| 2693 | |
| 2694 | const char *AsmStr = cast<ExternalSymbolSDNode>(InOps[1])->getSymbol(); |
| 2695 | unsigned i = 2, e = InOps.size(); |
| 2696 | if (InOps[e-1].getValueType() == MVT::Flag) |
| 2697 | --e; // Don't process a flag operand if it is here. |
| 2698 | |
| 2699 | while (i != e) { |
| 2700 | unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue(); |
| 2701 | if ((Flags & 7) != 4 /*MEM*/) { |
| 2702 | // Just skip over this operand, copying the operands verbatim. |
| 2703 | Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1); |
| 2704 | i += (Flags >> 3) + 1; |
| 2705 | } else { |
| 2706 | assert((Flags >> 3) == 1 && "Memory operand with multiple values?"); |
| 2707 | // Otherwise, this is a memory operand. Ask the target to select it. |
| 2708 | std::vector<SDOperand> SelOps; |
| 2709 | if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) { |
| 2710 | std::cerr << "Could not match memory address. Inline asm failure!\n"; |
| 2711 | exit(1); |
| 2712 | } |
| 2713 | |
| 2714 | // Add this to the output node. |
| 2715 | Ops.push_back(DAG.getConstant(4/*MEM*/ | (SelOps.size() << 3), MVT::i32)); |
| 2716 | Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); |
| 2717 | i += 2; |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | // Add the flag input back if present. |
| 2722 | if (e != InOps.size()) |
| 2723 | Ops.push_back(InOps.back()); |
| 2724 | } |