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