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" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 16 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Intrinsics.h" |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 28 | #include "llvm/CodeGen/SelectionDAG.h" |
| 29 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 30 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
| 32 | #include "llvm/Target/TargetFrameInfo.h" |
| 33 | #include "llvm/Target/TargetInstrInfo.h" |
| 34 | #include "llvm/Target/TargetLowering.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include <map> |
| 41 | #include <iostream> |
| 42 | using namespace llvm; |
| 43 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 44 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | ViewDAGs("view-isel-dags", cl::Hidden, |
| 47 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 48 | #else |
Chris Lattner | a639a43 | 2005-09-02 07:09:28 +0000 | [diff] [blame] | 49 | static const bool ViewDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 52 | namespace llvm { |
| 53 | //===--------------------------------------------------------------------===// |
| 54 | /// FunctionLoweringInfo - This contains information that is global to a |
| 55 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 56 | class FunctionLoweringInfo { |
| 57 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 58 | TargetLowering &TLI; |
| 59 | Function &Fn; |
| 60 | MachineFunction &MF; |
| 61 | SSARegMap *RegMap; |
| 62 | |
| 63 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 64 | |
| 65 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 66 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 67 | |
| 68 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 69 | /// we must remember which virtual registers hold the values for |
| 70 | /// cross-basic-block values. |
| 71 | std::map<const Value*, unsigned> ValueMap; |
| 72 | |
| 73 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 74 | /// the entry block. This allows the allocas to be efficiently referenced |
| 75 | /// anywhere in the function. |
| 76 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 77 | |
| 78 | unsigned MakeReg(MVT::ValueType VT) { |
| 79 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 80 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 82 | unsigned CreateRegForValue(const Value *V) { |
| 83 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 84 | // The common case is that we will only create one register for this |
| 85 | // value. If we have that case, create and return the virtual register. |
| 86 | unsigned NV = TLI.getNumElements(VT); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 87 | if (NV == 1) { |
| 88 | // If we are promoting this value, pick the next largest supported type. |
Chris Lattner | 98e5c0e | 2005-01-16 01:11:19 +0000 | [diff] [blame] | 89 | return MakeReg(TLI.getTypeToTransformTo(VT)); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 90 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 92 | // If this value is represented with multiple target registers, make sure |
| 93 | // to create enough consequtive registers of the right (smaller) type. |
| 94 | unsigned NT = VT-1; // Find the type to use. |
| 95 | while (TLI.getNumElements((MVT::ValueType)NT) != 1) |
| 96 | --NT; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 98 | unsigned R = MakeReg((MVT::ValueType)NT); |
| 99 | for (unsigned i = 1; i != NV; ++i) |
| 100 | MakeReg((MVT::ValueType)NT); |
| 101 | return R; |
| 102 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 104 | unsigned InitializeRegForValue(const Value *V) { |
| 105 | unsigned &R = ValueMap[V]; |
| 106 | assert(R == 0 && "Already initialized this value register!"); |
| 107 | return R = CreateRegForValue(V); |
| 108 | } |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 113 | /// PHI nodes or outside of the basic block that defines it. |
| 114 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 115 | if (isa<PHINode>(I)) return true; |
| 116 | BasicBlock *BB = I->getParent(); |
| 117 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 118 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 119 | return true; |
| 120 | return false; |
| 121 | } |
| 122 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 123 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 124 | /// entry block, return true. |
| 125 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 126 | BasicBlock *Entry = A->getParent()->begin(); |
| 127 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 128 | if (cast<Instruction>(*UI)->getParent() != Entry) |
| 129 | return false; // Use not in entry block. |
| 130 | return true; |
| 131 | } |
| 132 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 133 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 134 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 135 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 136 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 137 | // Create a vreg for each argument register that is not dead and is used |
| 138 | // outside of the entry block for the function. |
| 139 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 140 | AI != E; ++AI) |
| 141 | if (!isOnlyUsedInEntryBlock(AI)) |
| 142 | InitializeRegForValue(AI); |
| 143 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 144 | // Initialize the mapping of values to registers. This is only set up for |
| 145 | // instruction values that are used outside of the block that defines |
| 146 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 147 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 148 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 149 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 150 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 151 | const Type *Ty = AI->getAllocatedType(); |
| 152 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 153 | unsigned Align = |
| 154 | std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 155 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 156 | |
| 157 | // If the alignment of the value is smaller than the size of the value, |
| 158 | // and if the size of the value is particularly small (<= 8 bytes), |
| 159 | // round up to the size of the value for potentially better performance. |
| 160 | // |
| 161 | // FIXME: This could be made better with a preferred alignment hook in |
| 162 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 163 | if (Align < TySize && TySize <= 8) Align = TySize; |
Chris Lattner | 2dfa819 | 2005-10-18 22:11:42 +0000 | [diff] [blame] | 164 | TySize *= CUI->getValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 165 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 166 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 167 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 170 | for (; BB != EB; ++BB) |
| 171 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 172 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 173 | if (!isa<AllocaInst>(I) || |
| 174 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 175 | InitializeRegForValue(I); |
| 176 | |
| 177 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 178 | // also creates the initial PHI MachineInstrs, though none of the input |
| 179 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 180 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 181 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 182 | MBBMap[BB] = MBB; |
| 183 | MF.getBasicBlockList().push_back(MBB); |
| 184 | |
| 185 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 186 | // appropriate. |
| 187 | PHINode *PN; |
| 188 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 189 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 190 | if (!PN->use_empty()) { |
| 191 | unsigned NumElements = |
| 192 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 193 | unsigned PHIReg = ValueMap[PN]; |
| 194 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 195 | for (unsigned i = 0; i != NumElements; ++i) |
| 196 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 197 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 205 | /// implementation that is parameterized by a TargetLowering object. |
| 206 | /// Also, targets can overload any lowering method. |
| 207 | /// |
| 208 | namespace llvm { |
| 209 | class SelectionDAGLowering { |
| 210 | MachineBasicBlock *CurMBB; |
| 211 | |
| 212 | std::map<const Value*, SDOperand> NodeMap; |
| 213 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 214 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 215 | /// them up and then emit token factor nodes when possible. This allows us to |
| 216 | /// get simple disambiguation between loads without worrying about alias |
| 217 | /// analysis. |
| 218 | std::vector<SDOperand> PendingLoads; |
| 219 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 220 | public: |
| 221 | // TLI - This is information that describes the available target features we |
| 222 | // need for lowering. This indicates when operations are unavailable, |
| 223 | // implemented with a libcall, etc. |
| 224 | TargetLowering &TLI; |
| 225 | SelectionDAG &DAG; |
| 226 | const TargetData &TD; |
| 227 | |
| 228 | /// FuncInfo - Information about the function as a whole. |
| 229 | /// |
| 230 | FunctionLoweringInfo &FuncInfo; |
| 231 | |
| 232 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 233 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 234 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 235 | FuncInfo(funcinfo) { |
| 236 | } |
| 237 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 238 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 239 | /// |
| 240 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 241 | if (PendingLoads.empty()) |
| 242 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 243 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 244 | if (PendingLoads.size() == 1) { |
| 245 | SDOperand Root = PendingLoads[0]; |
| 246 | DAG.setRoot(Root); |
| 247 | PendingLoads.clear(); |
| 248 | return Root; |
| 249 | } |
| 250 | |
| 251 | // Otherwise, we have to make a token factor node. |
| 252 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 253 | PendingLoads.clear(); |
| 254 | DAG.setRoot(Root); |
| 255 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 258 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 259 | |
| 260 | void visit(unsigned Opcode, User &I) { |
| 261 | switch (Opcode) { |
| 262 | default: assert(0 && "Unknown instruction type encountered!"); |
| 263 | abort(); |
| 264 | // Build the switch statement using the Instruction.def file. |
| 265 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 266 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 267 | #include "llvm/Instruction.def" |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 272 | |
| 273 | |
| 274 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 275 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 276 | } |
| 277 | |
| 278 | SDOperand getValue(const Value *V) { |
| 279 | SDOperand &N = NodeMap[V]; |
| 280 | if (N.Val) return N; |
| 281 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 282 | const Type *VTy = V->getType(); |
| 283 | MVT::ValueType VT = TLI.getValueType(VTy); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 284 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) |
| 285 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 286 | visit(CE->getOpcode(), *CE); |
| 287 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 288 | return N; |
| 289 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 290 | return N = DAG.getGlobalAddress(GV, VT); |
| 291 | } else if (isa<ConstantPointerNull>(C)) { |
| 292 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 293 | } else if (isa<UndefValue>(C)) { |
Nate Begeman | b882752 | 2005-04-12 23:12:17 +0000 | [diff] [blame] | 294 | return N = DAG.getNode(ISD::UNDEF, VT); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 295 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 296 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 297 | } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) { |
| 298 | unsigned NumElements = PTy->getNumElements(); |
| 299 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 300 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
| 301 | |
| 302 | // Now that we know the number and type of the elements, push a |
| 303 | // Constant or ConstantFP node onto the ops list for each element of |
| 304 | // the packed constant. |
| 305 | std::vector<SDOperand> Ops; |
Chris Lattner | 3b841e9 | 2005-12-21 02:43:26 +0000 | [diff] [blame] | 306 | if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) { |
| 307 | if (MVT::isFloatingPoint(PVT)) { |
| 308 | for (unsigned i = 0; i != NumElements; ++i) { |
| 309 | const ConstantFP *El = cast<ConstantFP>(CP->getOperand(i)); |
| 310 | Ops.push_back(DAG.getConstantFP(El->getValue(), PVT)); |
| 311 | } |
| 312 | } else { |
| 313 | for (unsigned i = 0; i != NumElements; ++i) { |
| 314 | const ConstantIntegral *El = |
| 315 | cast<ConstantIntegral>(CP->getOperand(i)); |
| 316 | Ops.push_back(DAG.getConstant(El->getRawValue(), PVT)); |
| 317 | } |
| 318 | } |
| 319 | } else { |
| 320 | assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!"); |
| 321 | SDOperand Op; |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 322 | if (MVT::isFloatingPoint(PVT)) |
Chris Lattner | 3b841e9 | 2005-12-21 02:43:26 +0000 | [diff] [blame] | 323 | Op = DAG.getConstantFP(0, PVT); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 324 | else |
Chris Lattner | 3b841e9 | 2005-12-21 02:43:26 +0000 | [diff] [blame] | 325 | Op = DAG.getConstant(0, PVT); |
| 326 | Ops.assign(NumElements, Op); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 327 | } |
Chris Lattner | 3b841e9 | 2005-12-21 02:43:26 +0000 | [diff] [blame] | 328 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 329 | // Handle the case where we have a 1-element vector, in which |
| 330 | // case we want to immediately turn it into a scalar constant. |
Nate Begeman | cc827e6 | 2005-12-07 19:48:11 +0000 | [diff] [blame] | 331 | if (Ops.size() == 1) { |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 332 | return N = Ops[0]; |
Nate Begeman | cc827e6 | 2005-12-07 19:48:11 +0000 | [diff] [blame] | 333 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 334 | return N = DAG.getNode(ISD::ConstantVec, TVT, Ops); |
| 335 | } else { |
| 336 | // If the packed type isn't legal, then create a ConstantVec node with |
| 337 | // generic Vector type instead. |
| 338 | return N = DAG.getNode(ISD::ConstantVec, MVT::Vector, Ops); |
| 339 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 340 | } else { |
| 341 | // Canonicalize all constant ints to be unsigned. |
| 342 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 343 | } |
| 344 | |
| 345 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 346 | std::map<const AllocaInst*, int>::iterator SI = |
| 347 | FuncInfo.StaticAllocaMap.find(AI); |
| 348 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 349 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 350 | } |
| 351 | |
| 352 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 353 | FuncInfo.ValueMap.find(V); |
| 354 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
Chris Lattner | c8ea3c4 | 2005-01-16 02:23:07 +0000 | [diff] [blame] | 355 | |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 356 | unsigned InReg = VMI->second; |
| 357 | |
| 358 | // If this type is not legal, make it so now. |
| 359 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 360 | |
| 361 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 362 | if (DestVT < VT) { |
| 363 | // Source must be expanded. This input value is actually coming from the |
| 364 | // register pair VMI->second and VMI->second+1. |
| 365 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 366 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 367 | } else { |
| 368 | if (DestVT > VT) { // Promotion case |
| 369 | if (MVT::isFloatingPoint(VT)) |
| 370 | N = DAG.getNode(ISD::FP_ROUND, VT, N); |
| 371 | else |
| 372 | N = DAG.getNode(ISD::TRUNCATE, VT, N); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return N; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 380 | SDOperand &N = NodeMap[V]; |
| 381 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 382 | return N = NewN; |
| 383 | } |
| 384 | |
| 385 | // Terminator instructions. |
| 386 | void visitRet(ReturnInst &I); |
| 387 | void visitBr(BranchInst &I); |
| 388 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 389 | |
| 390 | // These all get lowered before this pass. |
Robert Bocchino | c0f4cd9 | 2006-01-10 19:04:57 +0000 | [diff] [blame] | 391 | void visitExtractElement(ExtractElementInst &I) { assert(0 && "TODO"); } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 392 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 393 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 394 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 395 | |
| 396 | // |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 397 | void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 398 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 399 | void visitAdd(User &I) { |
| 400 | visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 401 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 402 | void visitSub(User &I); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 403 | void visitMul(User &I) { |
| 404 | visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 405 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 406 | void visitDiv(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 407 | const Type *Ty = I.getType(); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 408 | visitBinary(I, Ty->isSigned() ? ISD::SDIV : ISD::UDIV, ISD::FDIV, 0); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 409 | } |
| 410 | void visitRem(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 411 | const Type *Ty = I.getType(); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 412 | visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 413 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 414 | void visitAnd(User &I) { visitBinary(I, ISD::AND, 0, 0); } |
| 415 | void visitOr (User &I) { visitBinary(I, ISD::OR, 0, 0); } |
| 416 | void visitXor(User &I) { visitBinary(I, ISD::XOR, 0, 0); } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 417 | void visitShl(User &I) { visitShift(I, ISD::SHL); } |
| 418 | void visitShr(User &I) { |
| 419 | visitShift(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 423 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 424 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 425 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 426 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 427 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 428 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 429 | |
| 430 | void visitGetElementPtr(User &I); |
| 431 | void visitCast(User &I); |
| 432 | void visitSelect(User &I); |
| 433 | // |
| 434 | |
| 435 | void visitMalloc(MallocInst &I); |
| 436 | void visitFree(FreeInst &I); |
| 437 | void visitAlloca(AllocaInst &I); |
| 438 | void visitLoad(LoadInst &I); |
| 439 | void visitStore(StoreInst &I); |
| 440 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 441 | void visitCall(CallInst &I); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 442 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 444 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 445 | void visitVAArg(VAArgInst &I); |
| 446 | void visitVAEnd(CallInst &I); |
| 447 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 448 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 450 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 451 | |
| 452 | void visitUserOp1(Instruction &I) { |
| 453 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 454 | abort(); |
| 455 | } |
| 456 | void visitUserOp2(Instruction &I) { |
| 457 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 458 | abort(); |
| 459 | } |
| 460 | }; |
| 461 | } // end namespace llvm |
| 462 | |
| 463 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 464 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 465 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | SDOperand Op1 = getValue(I.getOperand(0)); |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 470 | MVT::ValueType TmpVT; |
| 471 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 472 | switch (Op1.getValueType()) { |
| 473 | default: assert(0 && "Unknown value type!"); |
| 474 | case MVT::i1: |
| 475 | case MVT::i8: |
| 476 | case MVT::i16: |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 477 | case MVT::i32: |
| 478 | // If this is a machine where 32-bits is legal or expanded, promote to |
| 479 | // 32-bits, otherwise, promote to 64-bits. |
| 480 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 481 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 482 | else |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 483 | TmpVT = MVT::i32; |
| 484 | |
| 485 | // Extend integer types to result type. |
| 486 | if (I.getOperand(0)->getType()->isSigned()) |
| 487 | Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1); |
| 488 | else |
| 489 | Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 490 | break; |
| 491 | case MVT::f32: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 492 | case MVT::i64: |
| 493 | case MVT::f64: |
| 494 | break; // No extension needed! |
| 495 | } |
Nate Begeman | 4a95945 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 496 | // Allow targets to lower this further to meet ABI requirements |
| 497 | DAG.setRoot(TLI.LowerReturnTo(getRoot(), Op1, DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 501 | // Update machine-CFG edges. |
| 502 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 503 | |
| 504 | // Figure out which block is immediately after the current one. |
| 505 | MachineBasicBlock *NextBlock = 0; |
| 506 | MachineFunction::iterator BBI = CurMBB; |
| 507 | if (++BBI != CurMBB->getParent()->end()) |
| 508 | NextBlock = BBI; |
| 509 | |
| 510 | if (I.isUnconditional()) { |
| 511 | // If this is not a fall-through branch, emit the branch. |
| 512 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 513 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 514 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 515 | } else { |
| 516 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 517 | |
| 518 | SDOperand Cond = getValue(I.getCondition()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 519 | if (Succ1MBB == NextBlock) { |
| 520 | // If the condition is false, fall through. This means we should branch |
| 521 | // if the condition is true to Succ #0. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 522 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 523 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 524 | } else if (Succ0MBB == NextBlock) { |
| 525 | // If the condition is true, fall through. This means we should branch if |
| 526 | // the condition is false to Succ #1. Invert the condition first. |
| 527 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 528 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 529 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 530 | Cond, DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 531 | } else { |
Chris Lattner | e7ccd4a | 2005-04-09 03:30:29 +0000 | [diff] [blame] | 532 | std::vector<SDOperand> Ops; |
| 533 | Ops.push_back(getRoot()); |
| 534 | Ops.push_back(Cond); |
| 535 | Ops.push_back(DAG.getBasicBlock(Succ0MBB)); |
| 536 | Ops.push_back(DAG.getBasicBlock(Succ1MBB)); |
| 537 | DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 542 | void SelectionDAGLowering::visitSub(User &I) { |
| 543 | // -0.0 - X --> fneg |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 544 | if (I.getType()->isFloatingPoint()) { |
| 545 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 546 | if (CFP->isExactlyValue(-0.0)) { |
| 547 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 548 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 549 | return; |
| 550 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 551 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 552 | visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 555 | void SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp, |
| 556 | unsigned VecOp) { |
| 557 | const Type *Ty = I.getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 558 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 559 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 560 | |
Chris Lattner | b67eb91 | 2005-11-19 18:40:42 +0000 | [diff] [blame] | 561 | if (Ty->isIntegral()) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 562 | setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2)); |
| 563 | } else if (Ty->isFloatingPoint()) { |
| 564 | setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2)); |
| 565 | } else { |
| 566 | const PackedType *PTy = cast<PackedType>(Ty); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 567 | unsigned NumElements = PTy->getNumElements(); |
| 568 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 569 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 570 | |
| 571 | // Immediately scalarize packed types containing only one element, so that |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 572 | // the Legalize pass does not have to deal with them. Similarly, if the |
| 573 | // abstract vector is going to turn into one that the target natively |
| 574 | // supports, generate that type now so that Legalize doesn't have to deal |
| 575 | // with that either. These steps ensure that Legalize only has to handle |
| 576 | // vector types in its Expand case. |
| 577 | unsigned Opc = MVT::isFloatingPoint(PVT) ? FPOp : IntOp; |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 578 | if (NumElements == 1) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 579 | setValue(&I, DAG.getNode(Opc, PVT, Op1, Op2)); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 580 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 581 | setValue(&I, DAG.getNode(Opc, TVT, Op1, Op2)); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 582 | } else { |
| 583 | SDOperand Num = DAG.getConstant(NumElements, MVT::i32); |
| 584 | SDOperand Typ = DAG.getValueType(PVT); |
Nate Begeman | ab48be3 | 2005-11-22 18:16:00 +0000 | [diff] [blame] | 585 | setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ)); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 586 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 587 | } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 588 | } |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 589 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 590 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 591 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 592 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 593 | |
| 594 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 595 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 596 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 597 | } |
| 598 | |
| 599 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 600 | ISD::CondCode UnsignedOpcode) { |
| 601 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 602 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 603 | ISD::CondCode Opcode = SignedOpcode; |
| 604 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 605 | Opcode = UnsignedOpcode; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 606 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | void SelectionDAGLowering::visitSelect(User &I) { |
| 610 | SDOperand Cond = getValue(I.getOperand(0)); |
| 611 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 612 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 613 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 614 | TrueVal, FalseVal)); |
| 615 | } |
| 616 | |
| 617 | void SelectionDAGLowering::visitCast(User &I) { |
| 618 | SDOperand N = getValue(I.getOperand(0)); |
| 619 | MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType()); |
| 620 | MVT::ValueType DestTy = TLI.getValueType(I.getType()); |
| 621 | |
| 622 | if (N.getValueType() == DestTy) { |
| 623 | setValue(&I, N); // noop cast. |
Chris Lattner | ef311aa | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 624 | } else if (DestTy == MVT::i1) { |
| 625 | // Cast to bool is a comparison against zero, not truncation to zero. |
| 626 | SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) : |
| 627 | DAG.getConstantFP(0.0, N.getValueType()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 628 | setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 629 | } else if (isInteger(SrcTy)) { |
| 630 | if (isInteger(DestTy)) { // Int -> Int cast |
| 631 | if (DestTy < SrcTy) // Truncating cast? |
| 632 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N)); |
| 633 | else if (I.getOperand(0)->getType()->isSigned()) |
| 634 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N)); |
| 635 | else |
| 636 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N)); |
| 637 | } else { // Int -> FP cast |
| 638 | if (I.getOperand(0)->getType()->isSigned()) |
| 639 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N)); |
| 640 | else |
| 641 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N)); |
| 642 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 643 | } else { |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 644 | assert(isFloatingPoint(SrcTy) && "Unknown value type!"); |
| 645 | if (isFloatingPoint(DestTy)) { // FP -> FP cast |
| 646 | if (DestTy < SrcTy) // Rounding cast? |
| 647 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N)); |
| 648 | else |
| 649 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N)); |
| 650 | } else { // FP -> Int cast. |
| 651 | if (I.getType()->isSigned()) |
| 652 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N)); |
| 653 | else |
| 654 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N)); |
| 655 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 660 | SDOperand N = getValue(I.getOperand(0)); |
| 661 | const Type *Ty = I.getOperand(0)->getType(); |
| 662 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 663 | |
| 664 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 665 | OI != E; ++OI) { |
| 666 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 667 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 668 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 669 | if (Field) { |
| 670 | // N = N + Offset |
| 671 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 672 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 673 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 674 | } |
| 675 | Ty = StTy->getElementType(Field); |
| 676 | } else { |
| 677 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 679 | // If this is a constant subscript, handle it quickly. |
| 680 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 681 | if (CI->getRawValue() == 0) continue; |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 683 | uint64_t Offs; |
| 684 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 685 | Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 686 | else |
| 687 | Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 688 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 689 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 690 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 691 | |
| 692 | // N = N + Idx * ElementSize; |
| 693 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 694 | SDOperand IdxN = getValue(Idx); |
| 695 | |
| 696 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 697 | // it. |
| 698 | if (IdxN.getValueType() < N.getValueType()) { |
| 699 | if (Idx->getType()->isSigned()) |
| 700 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
| 701 | else |
| 702 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN); |
| 703 | } else if (IdxN.getValueType() > N.getValueType()) |
| 704 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 705 | |
| 706 | // If this is a multiply by a power of two, turn it into a shl |
| 707 | // immediately. This is a very common case. |
| 708 | if (isPowerOf2_64(ElementSize)) { |
| 709 | unsigned Amt = Log2_64(ElementSize); |
| 710 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 711 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 712 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 717 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 718 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | setValue(&I, N); |
| 722 | } |
| 723 | |
| 724 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 725 | // If this is a fixed sized alloca in the entry block of the function, |
| 726 | // allocate it statically on the stack. |
| 727 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 728 | return; // getValue will auto-populate this. |
| 729 | |
| 730 | const Type *Ty = I.getAllocatedType(); |
| 731 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 732 | unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 733 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 734 | |
| 735 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 736 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 737 | if (IntPtr < AllocSize.getValueType()) |
| 738 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 739 | else if (IntPtr > AllocSize.getValueType()) |
| 740 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 742 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 743 | getIntPtrConstant(TySize)); |
| 744 | |
| 745 | // Handle alignment. If the requested alignment is less than or equal to the |
| 746 | // stack alignment, ignore it and round the size of the allocation up to the |
| 747 | // stack alignment size. If the size is greater than the stack alignment, we |
| 748 | // note this in the DYNAMIC_STACKALLOC node. |
| 749 | unsigned StackAlign = |
| 750 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 751 | if (Align <= StackAlign) { |
| 752 | Align = 0; |
| 753 | // Add SA-1 to the size. |
| 754 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 755 | getIntPtrConstant(StackAlign-1)); |
| 756 | // Mask out the low bits for alignment purposes. |
| 757 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 758 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 759 | } |
| 760 | |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 761 | std::vector<MVT::ValueType> VTs; |
| 762 | VTs.push_back(AllocSize.getValueType()); |
| 763 | VTs.push_back(MVT::Other); |
| 764 | std::vector<SDOperand> Ops; |
| 765 | Ops.push_back(getRoot()); |
| 766 | Ops.push_back(AllocSize); |
| 767 | Ops.push_back(getIntPtrConstant(Align)); |
| 768 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 769 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 770 | |
| 771 | // Inform the Frame Information that we have just allocated a variable-sized |
| 772 | // object. |
| 773 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 774 | } |
| 775 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 776 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 777 | /// global into a string value. Return an empty string if we can't do it. |
| 778 | /// |
| 779 | static std::string getStringValue(Value *V, unsigned Offset = 0) { |
| 780 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 781 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 782 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 783 | if (Init->isString()) { |
| 784 | std::string Result = Init->getAsString(); |
| 785 | if (Offset < Result.size()) { |
| 786 | // If we are pointing INTO The string, erase the beginning... |
| 787 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 788 | |
| 789 | // Take off the null terminator, and any string fragments after it. |
| 790 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 791 | if (NullPos != std::string::npos) |
| 792 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 793 | return Result; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } else if (Constant *C = dyn_cast<Constant>(V)) { |
| 798 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 799 | return getStringValue(GV, Offset); |
| 800 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 801 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 802 | // Turn a gep into the specified offset. |
| 803 | if (CE->getNumOperands() == 3 && |
| 804 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 805 | isa<ConstantInt>(CE->getOperand(2))) { |
| 806 | return getStringValue(CE->getOperand(0), |
| 807 | Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue()); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | return ""; |
| 813 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 814 | |
| 815 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 816 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 817 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 818 | SDOperand Root; |
| 819 | if (I.isVolatile()) |
| 820 | Root = getRoot(); |
| 821 | else { |
| 822 | // Do not serialize non-volatile loads against each other. |
| 823 | Root = DAG.getRoot(); |
| 824 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 825 | |
| 826 | const Type *Ty = I.getType(); |
| 827 | SDOperand L; |
| 828 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 829 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 830 | unsigned NumElements = PTy->getNumElements(); |
| 831 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 832 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 833 | |
| 834 | // Immediately scalarize packed types containing only one element, so that |
| 835 | // the Legalize pass does not have to deal with them. |
| 836 | if (NumElements == 1) { |
| 837 | L = DAG.getLoad(PVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0))); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 838 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 839 | L = DAG.getLoad(TVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0))); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 840 | } else { |
| 841 | L = DAG.getVecLoad(NumElements, PVT, Root, Ptr, |
| 842 | DAG.getSrcValue(I.getOperand(0))); |
| 843 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 844 | } else { |
| 845 | L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, |
| 846 | DAG.getSrcValue(I.getOperand(0))); |
| 847 | } |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 848 | setValue(&I, L); |
| 849 | |
| 850 | if (I.isVolatile()) |
| 851 | DAG.setRoot(L.getValue(1)); |
| 852 | else |
| 853 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | |
| 857 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 858 | Value *SrcV = I.getOperand(0); |
| 859 | SDOperand Src = getValue(SrcV); |
| 860 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | 369e6db | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 861 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 862 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 865 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 866 | /// we want to emit this as a call to a named external function, return the name |
| 867 | /// otherwise lower it and return null. |
| 868 | const char * |
| 869 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 870 | switch (Intrinsic) { |
| 871 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 872 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 873 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 874 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0; |
| 875 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return 0; |
| 876 | case Intrinsic::setjmp: |
| 877 | return "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 878 | break; |
| 879 | case Intrinsic::longjmp: |
| 880 | return "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 881 | break; |
| 882 | case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return 0; |
| 883 | case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return 0; |
| 884 | case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return 0; |
| 885 | |
| 886 | case Intrinsic::readport: |
| 887 | case Intrinsic::readio: { |
| 888 | std::vector<MVT::ValueType> VTs; |
| 889 | VTs.push_back(TLI.getValueType(I.getType())); |
| 890 | VTs.push_back(MVT::Other); |
| 891 | std::vector<SDOperand> Ops; |
| 892 | Ops.push_back(getRoot()); |
| 893 | Ops.push_back(getValue(I.getOperand(1))); |
| 894 | SDOperand Tmp = DAG.getNode(Intrinsic == Intrinsic::readport ? |
| 895 | ISD::READPORT : ISD::READIO, VTs, Ops); |
| 896 | |
| 897 | setValue(&I, Tmp); |
| 898 | DAG.setRoot(Tmp.getValue(1)); |
| 899 | return 0; |
| 900 | } |
| 901 | case Intrinsic::writeport: |
| 902 | case Intrinsic::writeio: |
| 903 | DAG.setRoot(DAG.getNode(Intrinsic == Intrinsic::writeport ? |
| 904 | ISD::WRITEPORT : ISD::WRITEIO, MVT::Other, |
| 905 | getRoot(), getValue(I.getOperand(1)), |
| 906 | getValue(I.getOperand(2)))); |
| 907 | return 0; |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 908 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 909 | case Intrinsic::dbg_stoppoint: { |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 910 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 911 | return "llvm_debugger_stop"; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 912 | |
| 913 | std::string fname = "<unknown>"; |
| 914 | std::vector<SDOperand> Ops; |
| 915 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 916 | // Input Chain |
| 917 | Ops.push_back(getRoot()); |
| 918 | |
| 919 | // line number |
| 920 | Ops.push_back(getValue(I.getOperand(2))); |
| 921 | |
| 922 | // column |
| 923 | Ops.push_back(getValue(I.getOperand(3))); |
| 924 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 925 | // filename/working dir |
| 926 | // Pull the filename out of the the compilation unit. |
| 927 | const GlobalVariable *cunit = dyn_cast<GlobalVariable>(I.getOperand(4)); |
| 928 | if (cunit && cunit->hasInitializer()) { |
| 929 | if (ConstantStruct *CS = |
| 930 | dyn_cast<ConstantStruct>(cunit->getInitializer())) { |
| 931 | if (CS->getNumOperands() > 0) { |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 932 | Ops.push_back(DAG.getString(getStringValue(CS->getOperand(3)))); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 933 | Ops.push_back(DAG.getString(getStringValue(CS->getOperand(4)))); |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | if (Ops.size() == 5) // Found filename/workingdir. |
| 939 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops)); |
Chris Lattner | d67b3a8 | 2005-12-03 18:50:48 +0000 | [diff] [blame] | 940 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 941 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 942 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 943 | case Intrinsic::dbg_region_start: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 944 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 945 | return "llvm_dbg_region_start"; |
| 946 | if (I.getType() != Type::VoidTy) |
| 947 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 948 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 949 | case Intrinsic::dbg_region_end: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 950 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 951 | return "llvm_dbg_region_end"; |
| 952 | if (I.getType() != Type::VoidTy) |
| 953 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 954 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 955 | case Intrinsic::dbg_func_start: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 956 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 957 | return "llvm_dbg_subprogram"; |
| 958 | if (I.getType() != Type::VoidTy) |
| 959 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 960 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 961 | case Intrinsic::dbg_declare: |
| 962 | if (I.getType() != Type::VoidTy) |
| 963 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 964 | return 0; |
| 965 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame^] | 966 | case Intrinsic::isunordered_f32: |
| 967 | case Intrinsic::isunordered_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 968 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 969 | getValue(I.getOperand(2)), ISD::SETUO)); |
| 970 | return 0; |
| 971 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame^] | 972 | case Intrinsic::sqrt_f32: |
| 973 | case Intrinsic::sqrt_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 974 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 975 | getValue(I.getOperand(1)).getValueType(), |
| 976 | getValue(I.getOperand(1)))); |
| 977 | return 0; |
| 978 | case Intrinsic::pcmarker: { |
| 979 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 980 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 981 | return 0; |
| 982 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 983 | case Intrinsic::readcyclecounter: { |
| 984 | std::vector<MVT::ValueType> VTs; |
| 985 | VTs.push_back(MVT::i64); |
| 986 | VTs.push_back(MVT::Other); |
| 987 | std::vector<SDOperand> Ops; |
| 988 | Ops.push_back(getRoot()); |
| 989 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops); |
| 990 | setValue(&I, Tmp); |
| 991 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 992 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 993 | } |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 994 | case Intrinsic::bswap_i16: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 995 | case Intrinsic::bswap_i32: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 996 | case Intrinsic::bswap_i64: |
| 997 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 998 | getValue(I.getOperand(1)).getValueType(), |
| 999 | getValue(I.getOperand(1)))); |
| 1000 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame^] | 1001 | case Intrinsic::cttz_i8: |
| 1002 | case Intrinsic::cttz_i16: |
| 1003 | case Intrinsic::cttz_i32: |
| 1004 | case Intrinsic::cttz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1005 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 1006 | getValue(I.getOperand(1)).getValueType(), |
| 1007 | getValue(I.getOperand(1)))); |
| 1008 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame^] | 1009 | case Intrinsic::ctlz_i8: |
| 1010 | case Intrinsic::ctlz_i16: |
| 1011 | case Intrinsic::ctlz_i32: |
| 1012 | case Intrinsic::ctlz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1013 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 1014 | getValue(I.getOperand(1)).getValueType(), |
| 1015 | getValue(I.getOperand(1)))); |
| 1016 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame^] | 1017 | case Intrinsic::ctpop_i8: |
| 1018 | case Intrinsic::ctpop_i16: |
| 1019 | case Intrinsic::ctpop_i32: |
| 1020 | case Intrinsic::ctpop_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1021 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 1022 | getValue(I.getOperand(1)).getValueType(), |
| 1023 | getValue(I.getOperand(1)))); |
| 1024 | return 0; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1025 | case Intrinsic::stacksave: { |
| 1026 | std::vector<MVT::ValueType> VTs; |
| 1027 | VTs.push_back(TLI.getPointerTy()); |
| 1028 | VTs.push_back(MVT::Other); |
| 1029 | std::vector<SDOperand> Ops; |
| 1030 | Ops.push_back(getRoot()); |
| 1031 | SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, VTs, Ops); |
| 1032 | setValue(&I, Tmp); |
| 1033 | DAG.setRoot(Tmp.getValue(1)); |
| 1034 | return 0; |
| 1035 | } |
Chris Lattner | e8f7a4b | 2006-01-13 02:24:42 +0000 | [diff] [blame] | 1036 | case Intrinsic::stackrestore: |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1037 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, DAG.getRoot(), |
| 1038 | getValue(I.getOperand(1)))); |
| 1039 | return 0; |
Chris Lattner | ac22c83 | 2005-12-12 22:51:16 +0000 | [diff] [blame] | 1040 | case Intrinsic::prefetch: |
| 1041 | // FIXME: Currently discarding prefetches. |
| 1042 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1043 | default: |
| 1044 | std::cerr << I; |
| 1045 | assert(0 && "This intrinsic is not implemented yet!"); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1051 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1052 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1053 | if (Function *F = I.getCalledFunction()) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1054 | if (F->isExternal()) |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1055 | if (unsigned IID = F->getIntrinsicID()) { |
| 1056 | RenameFn = visitIntrinsicCall(I, IID); |
| 1057 | if (!RenameFn) |
| 1058 | return; |
| 1059 | } else { // Not an LLVM intrinsic. |
| 1060 | const std::string &Name = F->getName(); |
| 1061 | if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1062 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1063 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1064 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1065 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1066 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 1067 | return; |
| 1068 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1069 | } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1070 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1071 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1072 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1073 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1074 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 1075 | return; |
| 1076 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1077 | } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1078 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1079 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1080 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1081 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1082 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 1083 | return; |
| 1084 | } |
| 1085 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 1086 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1087 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1089 | SDOperand Callee; |
| 1090 | if (!RenameFn) |
| 1091 | Callee = getValue(I.getOperand(0)); |
| 1092 | else |
| 1093 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1094 | std::vector<std::pair<SDOperand, const Type*> > Args; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1095 | Args.reserve(I.getNumOperands()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1096 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1097 | Value *Arg = I.getOperand(i); |
| 1098 | SDOperand ArgNode = getValue(Arg); |
| 1099 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 1100 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1101 | |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1102 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 1103 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1105 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 9092fa3 | 2005-05-12 19:56:57 +0000 | [diff] [blame] | 1106 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(), |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1107 | I.isTailCall(), Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1108 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1109 | setValue(&I, Result.first); |
| 1110 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 1114 | SDOperand Src = getValue(I.getOperand(0)); |
| 1115 | |
| 1116 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (IntPtr < Src.getValueType()) |
| 1119 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 1120 | else if (IntPtr > Src.getValueType()) |
| 1121 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Scale the source by the type size. |
| 1124 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 1125 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 1126 | Src, getIntPtrConstant(ElementSize)); |
| 1127 | |
| 1128 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1129 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1130 | |
| 1131 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1132 | TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1133 | DAG.getExternalSymbol("malloc", IntPtr), |
| 1134 | Args, DAG); |
| 1135 | setValue(&I, Result.first); // Pointers always fit in registers |
| 1136 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 1140 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1141 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 1142 | TLI.getTargetData().getIntPtrType())); |
| 1143 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1144 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1145 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1146 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 1147 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 1150 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 1151 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 1152 | // instructions are special in various ways, which require special support to |
| 1153 | // insert. The specified MachineInstr is created but not inserted into any |
| 1154 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 1155 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1156 | MachineBasicBlock *MBB) { |
| 1157 | std::cerr << "If a target marks an instruction with " |
| 1158 | "'usesCustomDAGSchedInserter', it must implement " |
| 1159 | "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
| 1160 | abort(); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Nate Begeman | 4a95945 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 1164 | SDOperand TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op, |
| 1165 | SelectionDAG &DAG) { |
| 1166 | return DAG.getNode(ISD::RET, MVT::Other, Chain, Op); |
| 1167 | } |
| 1168 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1169 | SDOperand TargetLowering::LowerVAStart(SDOperand Chain, |
| 1170 | SDOperand VAListP, Value *VAListV, |
| 1171 | SelectionDAG &DAG) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1172 | // We have no sane default behavior, just emit a useful error message and bail |
| 1173 | // out. |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1174 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1175 | abort(); |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1176 | return SDOperand(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1179 | SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV, |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1180 | SelectionDAG &DAG) { |
| 1181 | // Default to a noop. |
| 1182 | return Chain; |
| 1183 | } |
| 1184 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1185 | SDOperand TargetLowering::LowerVACopy(SDOperand Chain, |
| 1186 | SDOperand SrcP, Value *SrcV, |
| 1187 | SDOperand DestP, Value *DestV, |
| 1188 | SelectionDAG &DAG) { |
| 1189 | // Default to copying the input list. |
| 1190 | SDOperand Val = DAG.getLoad(getPointerTy(), Chain, |
| 1191 | SrcP, DAG.getSrcValue(SrcV)); |
Andrew Lenharth | 213e557 | 2005-06-22 21:04:42 +0000 | [diff] [blame] | 1192 | SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1193 | Val, DestP, DAG.getSrcValue(DestV)); |
| 1194 | return Result; |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | std::pair<SDOperand,SDOperand> |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1198 | TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 1199 | const Type *ArgTy, SelectionDAG &DAG) { |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1200 | // We have no sane default behavior, just emit a useful error message and bail |
| 1201 | // out. |
| 1202 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
| 1203 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1204 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1209 | DAG.setRoot(TLI.LowerVAStart(getRoot(), getValue(I.getOperand(1)), |
| 1210 | I.getOperand(1), DAG)); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 1214 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1215 | TLI.LowerVAArg(getRoot(), getValue(I.getOperand(0)), I.getOperand(0), |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1216 | I.getType(), DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1217 | setValue(&I, Result.first); |
| 1218 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1222 | DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1223 | I.getOperand(1), DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1227 | SDOperand Result = |
| 1228 | TLI.LowerVACopy(getRoot(), getValue(I.getOperand(2)), I.getOperand(2), |
| 1229 | getValue(I.getOperand(1)), I.getOperand(1), DAG); |
| 1230 | DAG.setRoot(Result); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1233 | |
| 1234 | // It is always conservatively correct for llvm.returnaddress and |
| 1235 | // llvm.frameaddress to return 0. |
| 1236 | std::pair<SDOperand, SDOperand> |
| 1237 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 1238 | unsigned Depth, SelectionDAG &DAG) { |
| 1239 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 1242 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1243 | assert(0 && "LowerOperation not implemented for this target!"); |
| 1244 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1245 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1248 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 1249 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 1250 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1251 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1252 | setValue(&I, Result.first); |
| 1253 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1256 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 1257 | #if 0 |
| 1258 | // If the size of the cpy/move/set is constant (known) |
| 1259 | if (ConstantUInt* op3 = dyn_cast<ConstantUInt>(I.getOperand(3))) { |
| 1260 | uint64_t size = op3->getValue(); |
| 1261 | switch (Op) { |
| 1262 | case ISD::MEMSET: |
| 1263 | if (size <= TLI.getMaxStoresPerMemSet()) { |
| 1264 | if (ConstantUInt* op4 = dyn_cast<ConstantUInt>(I.getOperand(4))) { |
| 1265 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 1266 | uint64_t align = op4.getValue(); |
| 1267 | while (size > align) { |
| 1268 | size -=align; |
| 1269 | } |
| 1270 | Value *SrcV = I.getOperand(0); |
| 1271 | SDOperand Src = getValue(SrcV); |
| 1272 | SDOperand Ptr = getValue(I.getOperand(1)); |
| 1273 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
| 1274 | DAG.getSrcValue(I.getOperand(1)))); |
| 1275 | } |
| 1276 | break; |
| 1277 | } |
| 1278 | break; // don't do this optimization, use a normal memset |
| 1279 | case ISD::MEMMOVE: |
| 1280 | case ISD::MEMCPY: |
| 1281 | break; // FIXME: not implemented yet |
| 1282 | } |
| 1283 | } |
| 1284 | #endif |
| 1285 | |
| 1286 | // Non-optimized version |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1287 | std::vector<SDOperand> Ops; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1288 | Ops.push_back(getRoot()); |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1289 | Ops.push_back(getValue(I.getOperand(1))); |
| 1290 | Ops.push_back(getValue(I.getOperand(2))); |
| 1291 | Ops.push_back(getValue(I.getOperand(3))); |
| 1292 | Ops.push_back(getValue(I.getOperand(4))); |
| 1293 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1296 | //===----------------------------------------------------------------------===// |
| 1297 | // SelectionDAGISel code |
| 1298 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1299 | |
| 1300 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 1301 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 1302 | } |
| 1303 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1304 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1305 | // FIXME: we only modify the CFG to split critical edges. This |
| 1306 | // updates dom and loop info. |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1307 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1308 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1309 | |
| 1310 | /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset, |
| 1311 | /// casting to the type of GEPI. |
| 1312 | static Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI, |
| 1313 | Value *Ptr, Value *PtrOffset) { |
| 1314 | if (V) return V; // Already computed. |
| 1315 | |
| 1316 | BasicBlock::iterator InsertPt; |
| 1317 | if (BB == GEPI->getParent()) { |
| 1318 | // If insert into the GEP's block, insert right after the GEP. |
| 1319 | InsertPt = GEPI; |
| 1320 | ++InsertPt; |
| 1321 | } else { |
| 1322 | // Otherwise, insert at the top of BB, after any PHI nodes |
| 1323 | InsertPt = BB->begin(); |
| 1324 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 1325 | } |
| 1326 | |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1327 | // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into |
| 1328 | // BB so that there is only one value live across basic blocks (the cast |
| 1329 | // operand). |
| 1330 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 1331 | if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType())) |
| 1332 | Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt); |
| 1333 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1334 | // Add the offset, cast it to the right type. |
| 1335 | Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt); |
| 1336 | Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt); |
| 1337 | return V = Ptr; |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction |
| 1342 | /// selection, we want to be a bit careful about some things. In particular, if |
| 1343 | /// we have a GEP instruction that is used in a different block than it is |
| 1344 | /// defined, the addressing expression of the GEP cannot be folded into loads or |
| 1345 | /// stores that use it. In this case, decompose the GEP and move constant |
| 1346 | /// indices into blocks that use it. |
| 1347 | static void OptimizeGEPExpression(GetElementPtrInst *GEPI, |
| 1348 | const TargetData &TD) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1349 | // If this GEP is only used inside the block it is defined in, there is no |
| 1350 | // need to rewrite it. |
| 1351 | bool isUsedOutsideDefBB = false; |
| 1352 | BasicBlock *DefBB = GEPI->getParent(); |
| 1353 | for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); |
| 1354 | UI != E; ++UI) { |
| 1355 | if (cast<Instruction>(*UI)->getParent() != DefBB) { |
| 1356 | isUsedOutsideDefBB = true; |
| 1357 | break; |
| 1358 | } |
| 1359 | } |
| 1360 | if (!isUsedOutsideDefBB) return; |
| 1361 | |
| 1362 | // If this GEP has no non-zero constant indices, there is nothing we can do, |
| 1363 | // ignore it. |
| 1364 | bool hasConstantIndex = false; |
| 1365 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 1366 | E = GEPI->op_end(); OI != E; ++OI) { |
| 1367 | if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) |
| 1368 | if (CI->getRawValue()) { |
| 1369 | hasConstantIndex = true; |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
Chris Lattner | 3802c25 | 2005-12-11 09:05:13 +0000 | [diff] [blame] | 1373 | // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses. |
| 1374 | if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the |
| 1377 | // constant offset (which we now know is non-zero) and deal with it later. |
| 1378 | uint64_t ConstantOffset = 0; |
| 1379 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 1380 | Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI); |
| 1381 | const Type *Ty = GEPI->getOperand(0)->getType(); |
| 1382 | |
| 1383 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 1384 | E = GEPI->op_end(); OI != E; ++OI) { |
| 1385 | Value *Idx = *OI; |
| 1386 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 1387 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 1388 | if (Field) |
| 1389 | ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 1390 | Ty = StTy->getElementType(Field); |
| 1391 | } else { |
| 1392 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 1393 | |
| 1394 | // Handle constant subscripts. |
| 1395 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 1396 | if (CI->getRawValue() == 0) continue; |
| 1397 | |
| 1398 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 1399 | ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 1400 | else |
| 1401 | ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 1402 | continue; |
| 1403 | } |
| 1404 | |
| 1405 | // Ptr = Ptr + Idx * ElementSize; |
| 1406 | |
| 1407 | // Cast Idx to UIntPtrTy if needed. |
| 1408 | Idx = new CastInst(Idx, UIntPtrTy, "", GEPI); |
| 1409 | |
| 1410 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 1411 | // Mask off bits that should not be set. |
| 1412 | ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 1413 | Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize); |
| 1414 | |
| 1415 | // Multiply by the element size and add to the base. |
| 1416 | Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI); |
| 1417 | Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | // Make sure that the offset fits in uintptr_t. |
| 1422 | ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 1423 | Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset); |
| 1424 | |
| 1425 | // Okay, we have now emitted all of the variable index parts to the BB that |
| 1426 | // the GEP is defined in. Loop over all of the using instructions, inserting |
| 1427 | // 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] | 1428 | // instruction to use the newly computed value, making GEPI dead. When the |
| 1429 | // user is a load or store instruction address, we emit the add into the user |
| 1430 | // block, otherwise we use a canonical version right next to the gep (these |
| 1431 | // won't be foldable as addresses, so we might as well share the computation). |
| 1432 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1433 | std::map<BasicBlock*,Value*> InsertedExprs; |
| 1434 | while (!GEPI->use_empty()) { |
| 1435 | Instruction *User = cast<Instruction>(GEPI->use_back()); |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1436 | |
| 1437 | // If this use is not foldable into the addressing mode, use a version |
| 1438 | // emitted in the GEP block. |
| 1439 | Value *NewVal; |
| 1440 | if (!isa<LoadInst>(User) && |
| 1441 | (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) { |
| 1442 | NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, |
| 1443 | Ptr, PtrOffset); |
| 1444 | } else { |
| 1445 | // Otherwise, insert the code in the User's block so it can be folded into |
| 1446 | // any users in that block. |
| 1447 | NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1448 | User->getParent(), GEPI, |
| 1449 | Ptr, PtrOffset); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1450 | } |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1451 | User->replaceUsesOfWith(GEPI, NewVal); |
| 1452 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Finally, the GEP is dead, remove it. |
| 1455 | GEPI->eraseFromParent(); |
| 1456 | } |
| 1457 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1458 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 1459 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 1460 | RegMap = MF.getSSARegMap(); |
| 1461 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 1462 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1463 | // First, split all critical edges for PHI nodes with incoming values that are |
| 1464 | // constants, this way the load of the constant into a vreg will not be placed |
| 1465 | // into MBBs that are used some other way. |
| 1466 | // |
| 1467 | // In this pass we also look for GEP instructions that are used across basic |
| 1468 | // blocks and rewrites them to improve basic-block-at-a-time selection. |
| 1469 | // |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1470 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 1471 | PHINode *PN; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1472 | BasicBlock::iterator BBI; |
| 1473 | for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI) |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1474 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 1475 | if (isa<Constant>(PN->getIncomingValue(i))) |
| 1476 | SplitCriticalEdge(PN->getIncomingBlock(i), BB); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1477 | |
| 1478 | for (BasicBlock::iterator E = BB->end(); BBI != E; ) |
| 1479 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++)) |
| 1480 | OptimizeGEPExpression(GEPI, TLI.getTargetData()); |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1481 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1483 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 1484 | |
| 1485 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 1486 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1488 | return true; |
| 1489 | } |
| 1490 | |
| 1491 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1492 | SDOperand SelectionDAGISel:: |
| 1493 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1494 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1495 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1496 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1497 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1498 | |
| 1499 | // If this type is not legal, we must make sure to not create an invalid |
| 1500 | // register use. |
| 1501 | MVT::ValueType SrcVT = Op.getValueType(); |
| 1502 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
| 1503 | SelectionDAG &DAG = SDL.DAG; |
| 1504 | if (SrcVT == DestVT) { |
| 1505 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1506 | } else if (SrcVT < DestVT) { |
| 1507 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 1508 | if (MVT::isFloatingPoint(SrcVT)) |
| 1509 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 1510 | else |
Chris Lattner | fab0887 | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 1511 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1512 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1513 | } else { |
| 1514 | // The src value is expanded into multiple registers. |
| 1515 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1516 | Op, DAG.getConstant(0, MVT::i32)); |
| 1517 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1518 | Op, DAG.getConstant(1, MVT::i32)); |
| 1519 | Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo); |
| 1520 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 1521 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1524 | void SelectionDAGISel:: |
| 1525 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 1526 | std::vector<SDOperand> &UnorderedChains) { |
| 1527 | // If this is the entry block, emit arguments. |
| 1528 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1529 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1530 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 1531 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1533 | unsigned a = 0; |
| 1534 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1535 | AI != E; ++AI, ++a) |
| 1536 | if (!AI->use_empty()) { |
| 1537 | SDL.setValue(AI, Args[a]); |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1539 | // If this argument is live outside of the entry block, insert a copy from |
| 1540 | // whereever we got it to the vreg that other BB's will reference it as. |
| 1541 | if (FuncInfo.ValueMap.count(AI)) { |
| 1542 | SDOperand Copy = |
| 1543 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 1544 | UnorderedChains.push_back(Copy); |
| 1545 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1546 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Next, if the function has live ins that need to be copied into vregs, |
| 1549 | // emit the copies now, into the top of the block. |
| 1550 | MachineFunction &MF = SDL.DAG.getMachineFunction(); |
| 1551 | if (MF.livein_begin() != MF.livein_end()) { |
| 1552 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 1553 | const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo(); |
| 1554 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 1555 | E = MF.livein_end(); LI != E; ++LI) |
| 1556 | if (LI->second) |
| 1557 | MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 1558 | LI->first, RegMap->getRegClass(LI->second)); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1559 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Finally, if the target has anything special to do, allow it to do so. |
| 1562 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1566 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 1567 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 1568 | FunctionLoweringInfo &FuncInfo) { |
| 1569 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1570 | |
| 1571 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1573 | // Lower any arguments needed in this block if this is the entry block. |
| 1574 | if (LLVMBB == &LLVMBB->getParent()->front()) |
| 1575 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1576 | |
| 1577 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 1578 | SDL.setCurrentBasicBlock(BB); |
| 1579 | |
| 1580 | // Lower all of the non-terminator instructions. |
| 1581 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 1582 | I != E; ++I) |
| 1583 | SDL.visit(*I); |
| 1584 | |
| 1585 | // Ensure that all instructions which are used outside of their defining |
| 1586 | // blocks are available as virtual registers. |
| 1587 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1588 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1589 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1590 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1591 | UnorderedChains.push_back( |
| 1592 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 1596 | // ensure constants are generated when needed. Remember the virtual registers |
| 1597 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 1598 | // directly add them, because expansion might result in multiple MBB's for one |
| 1599 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 1600 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1601 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1602 | |
| 1603 | // Emit constants only once even if used by multiple PHI nodes. |
| 1604 | std::map<Constant*, unsigned> ConstantsOut; |
| 1605 | |
| 1606 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 1607 | // this block. |
| 1608 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1609 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1610 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1611 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 1612 | PHINode *PN; |
| 1613 | |
| 1614 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1615 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1616 | // emitted yet. |
| 1617 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1618 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 1619 | if (!PN->use_empty()) { |
| 1620 | unsigned Reg; |
| 1621 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1622 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 1623 | unsigned &RegOut = ConstantsOut[C]; |
| 1624 | if (RegOut == 0) { |
| 1625 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1626 | UnorderedChains.push_back( |
| 1627 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1628 | } |
| 1629 | Reg = RegOut; |
| 1630 | } else { |
| 1631 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1632 | if (Reg == 0) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1633 | assert(isa<AllocaInst>(PHIOp) && |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1634 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 1635 | "Didn't codegen value into a register!??"); |
| 1636 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1637 | UnorderedChains.push_back( |
| 1638 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1639 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1640 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1642 | // Remember that this register needs to added to the machine PHI node as |
| 1643 | // the input for this MBB. |
| 1644 | unsigned NumElements = |
| 1645 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 1646 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 1647 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1648 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1649 | } |
| 1650 | ConstantsOut.clear(); |
| 1651 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1652 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 1653 | if (!UnorderedChains.empty()) { |
Chris Lattner | 7436b57 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 1654 | SDOperand Root = SDL.getRoot(); |
| 1655 | if (Root.getOpcode() != ISD::EntryToken) { |
| 1656 | unsigned i = 0, e = UnorderedChains.size(); |
| 1657 | for (; i != e; ++i) { |
| 1658 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 1659 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 1660 | break; // Don't add the root if we already indirectly depend on it. |
| 1661 | } |
| 1662 | |
| 1663 | if (i == e) |
| 1664 | UnorderedChains.push_back(Root); |
| 1665 | } |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1666 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 1667 | } |
| 1668 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1669 | // Lower the terminator after the copies are emitted. |
| 1670 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1671 | |
| 1672 | // Make sure the root of the DAG is up-to-date. |
| 1673 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 1677 | FunctionLoweringInfo &FuncInfo) { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 1678 | SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1679 | CurDAG = &DAG; |
| 1680 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 1681 | |
| 1682 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 1683 | // types that are not supported by the target. |
| 1684 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 1685 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1686 | // Run the DAG combiner in pre-legalize mode. |
| 1687 | DAG.Combine(false); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1689 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 1690 | DEBUG(DAG.dump()); |
| 1691 | |
| 1692 | // Second step, hack on the DAG until it only uses operations and types that |
| 1693 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1694 | DAG.Legalize(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1695 | |
| 1696 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 1697 | DEBUG(DAG.dump()); |
| 1698 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1699 | // Run the DAG combiner in post-legalize mode. |
| 1700 | DAG.Combine(true); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | d48050a | 2005-10-05 06:09:10 +0000 | [diff] [blame] | 1702 | if (ViewDAGs) DAG.viewGraph(); |
| 1703 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1704 | // Third, instruction select all of the operations to machine code, adding the |
| 1705 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1706 | InstructionSelectBasicBlock(DAG); |
| 1707 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1708 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 1709 | DEBUG(BB->dump()); |
| 1710 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1711 | // 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] | 1712 | // PHI nodes in successors. |
| 1713 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 1714 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 1715 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1716 | "This is not a machine PHI node that we are updating!"); |
| 1717 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 1718 | PHI->addMachineBasicBlockOperand(BB); |
| 1719 | } |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Finally, add the CFG edges from the last selected MBB to the successor |
| 1722 | // MBBs. |
| 1723 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1724 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 1725 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)]; |
| 1726 | BB->addSuccessor(Succ0MBB); |
| 1727 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1728 | } |