Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAGISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 19 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 24 | #include "llvm/InlineAsm.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Intrinsics.h" |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 27 | #include "llvm/IntrinsicInst.h" |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/SelectionDAG.h" |
| 35 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 36 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetAsmInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
| 39 | #include "llvm/Target/TargetFrameInfo.h" |
| 40 | #include "llvm/Target/TargetInstrInfo.h" |
| 41 | #include "llvm/Target/TargetLowering.h" |
| 42 | #include "llvm/Target/TargetMachine.h" |
Vladimir Prus | 1247291 | 2006-05-23 13:43:15 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Compiler.h" |
Jeff Cohen | 7e88103 | 2006-02-24 02:52:40 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 51 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 53 | ViewISelDAGs("view-isel-dags", cl::Hidden, |
| 54 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 55 | static cl::opt<bool> |
| 56 | ViewSchedDAGs("view-sched-dags", cl::Hidden, |
| 57 | cl::desc("Pop up a window to show sched dags as they are processed")); |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 58 | #else |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 59 | static const bool ViewISelDAGs = 0, ViewSchedDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 62 | //===---------------------------------------------------------------------===// |
| 63 | /// |
| 64 | /// RegisterScheduler class - Track the registration of instruction schedulers. |
| 65 | /// |
| 66 | //===---------------------------------------------------------------------===// |
| 67 | MachinePassRegistry RegisterScheduler::Registry; |
| 68 | |
| 69 | //===---------------------------------------------------------------------===// |
| 70 | /// |
| 71 | /// ISHeuristic command line option for instruction schedulers. |
| 72 | /// |
| 73 | //===---------------------------------------------------------------------===// |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 74 | namespace { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 75 | cl::opt<RegisterScheduler::FunctionPassCtor, false, |
| 76 | RegisterPassParser<RegisterScheduler> > |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 77 | ISHeuristic("sched", |
Chris Lattner | 3700f90 | 2006-08-03 00:18:59 +0000 | [diff] [blame] | 78 | cl::init(&createDefaultScheduler), |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 79 | cl::desc("Instruction schedulers available:")); |
| 80 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 81 | static RegisterScheduler |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 82 | defaultListDAGScheduler("default", " Best scheduler for the target", |
| 83 | createDefaultScheduler); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 86 | namespace { |
| 87 | /// RegsForValue - This struct represents the physical registers that a |
| 88 | /// particular value is assigned and the type information about the value. |
| 89 | /// This is needed because values can be promoted into larger registers and |
| 90 | /// expanded into multiple smaller registers than the value. |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 91 | struct VISIBILITY_HIDDEN RegsForValue { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 92 | /// Regs - This list hold the register (for legal and promoted values) |
| 93 | /// or register set (for expanded values) that the value should be assigned |
| 94 | /// to. |
| 95 | std::vector<unsigned> Regs; |
| 96 | |
| 97 | /// RegVT - The value type of each register. |
| 98 | /// |
| 99 | MVT::ValueType RegVT; |
| 100 | |
| 101 | /// ValueVT - The value type of the LLVM value, which may be promoted from |
| 102 | /// RegVT or made from merging the two expanded parts. |
| 103 | MVT::ValueType ValueVT; |
| 104 | |
| 105 | RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {} |
| 106 | |
| 107 | RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt) |
| 108 | : RegVT(regvt), ValueVT(valuevt) { |
| 109 | Regs.push_back(Reg); |
| 110 | } |
| 111 | RegsForValue(const std::vector<unsigned> ®s, |
| 112 | MVT::ValueType regvt, MVT::ValueType valuevt) |
| 113 | : Regs(regs), RegVT(regvt), ValueVT(valuevt) { |
| 114 | } |
| 115 | |
| 116 | /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from |
| 117 | /// this value and returns the result as a ValueVT value. This uses |
| 118 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 119 | SDOperand getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 120 | SDOperand &Chain, SDOperand &Flag) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 121 | |
| 122 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 123 | /// specified value into the registers specified by this object. This uses |
| 124 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 125 | void getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 126 | SDOperand &Chain, SDOperand &Flag, |
| 127 | MVT::ValueType PtrVT) const; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 128 | |
| 129 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 130 | /// operand list. This adds the code marker and includes the number of |
| 131 | /// values added into it. |
| 132 | void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 133 | std::vector<SDOperand> &Ops) const; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 134 | }; |
| 135 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 137 | namespace llvm { |
| 138 | //===--------------------------------------------------------------------===// |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 139 | /// createDefaultScheduler - This creates an instruction scheduler appropriate |
| 140 | /// for the target. |
| 141 | ScheduleDAG* createDefaultScheduler(SelectionDAGISel *IS, |
| 142 | SelectionDAG *DAG, |
| 143 | MachineBasicBlock *BB) { |
| 144 | TargetLowering &TLI = IS->getTargetLowering(); |
| 145 | |
| 146 | if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) { |
| 147 | return createTDListDAGScheduler(IS, DAG, BB); |
| 148 | } else { |
| 149 | assert(TLI.getSchedulingPreference() == |
| 150 | TargetLowering::SchedulingForRegPressure && "Unknown sched type!"); |
| 151 | return createBURRListDAGScheduler(IS, DAG, BB); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | //===--------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 157 | /// FunctionLoweringInfo - This contains information that is global to a |
| 158 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 159 | class FunctionLoweringInfo { |
| 160 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 161 | TargetLowering &TLI; |
| 162 | Function &Fn; |
| 163 | MachineFunction &MF; |
| 164 | SSARegMap *RegMap; |
| 165 | |
| 166 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 167 | |
| 168 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 169 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 170 | |
| 171 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 172 | /// we must remember which virtual registers hold the values for |
| 173 | /// cross-basic-block values. |
Chris Lattner | 9f24ad7 | 2007-02-04 01:35:11 +0000 | [diff] [blame] | 174 | DenseMap<const Value*, unsigned> ValueMap; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 175 | |
| 176 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 177 | /// the entry block. This allows the allocas to be efficiently referenced |
| 178 | /// anywhere in the function. |
| 179 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 180 | |
| 181 | unsigned MakeReg(MVT::ValueType VT) { |
| 182 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 183 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 184 | |
| 185 | /// isExportedInst - Return true if the specified value is an instruction |
| 186 | /// exported from its block. |
| 187 | bool isExportedInst(const Value *V) { |
| 188 | return ValueMap.count(V); |
| 189 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 191 | unsigned CreateRegForValue(const Value *V); |
| 192 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 193 | unsigned InitializeRegForValue(const Value *V) { |
| 194 | unsigned &R = ValueMap[V]; |
| 195 | assert(R == 0 && "Already initialized this value register!"); |
| 196 | return R = CreateRegForValue(V); |
| 197 | } |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 202 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 203 | /// switch instruction, which may expand to multiple basic blocks. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 204 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 205 | if (isa<PHINode>(I)) return true; |
| 206 | BasicBlock *BB = I->getParent(); |
| 207 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 208 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 209 | // FIXME: Remove switchinst special case. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 210 | isa<SwitchInst>(*UI)) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 211 | return true; |
| 212 | return false; |
| 213 | } |
| 214 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 215 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 216 | /// entry block, return true. This includes arguments used by switches, since |
| 217 | /// the switch may expand into multiple basic blocks. |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 218 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 219 | BasicBlock *Entry = A->getParent()->begin(); |
| 220 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 221 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 222 | return false; // Use not in entry block. |
| 223 | return true; |
| 224 | } |
| 225 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 226 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 227 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 228 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 229 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 230 | // Create a vreg for each argument register that is not dead and is used |
| 231 | // outside of the entry block for the function. |
| 232 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 233 | AI != E; ++AI) |
| 234 | if (!isOnlyUsedInEntryBlock(AI)) |
| 235 | InitializeRegForValue(AI); |
| 236 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 237 | // Initialize the mapping of values to registers. This is only set up for |
| 238 | // instruction values that are used outside of the block that defines |
| 239 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 240 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 241 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 242 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 243 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 244 | const Type *Ty = AI->getAllocatedType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 245 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 246 | unsigned Align = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 247 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 248 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 249 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 250 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 251 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 252 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 253 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 256 | for (; BB != EB; ++BB) |
| 257 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 258 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 259 | if (!isa<AllocaInst>(I) || |
| 260 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 261 | InitializeRegForValue(I); |
| 262 | |
| 263 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 264 | // also creates the initial PHI MachineInstrs, though none of the input |
| 265 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 266 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 267 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 268 | MBBMap[BB] = MBB; |
| 269 | MF.getBasicBlockList().push_back(MBB); |
| 270 | |
| 271 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 272 | // appropriate. |
| 273 | PHINode *PN; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 274 | for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){ |
| 275 | if (PN->use_empty()) continue; |
| 276 | |
| 277 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
| 278 | unsigned NumElements; |
| 279 | if (VT != MVT::Vector) |
| 280 | NumElements = TLI.getNumElements(VT); |
| 281 | else { |
| 282 | MVT::ValueType VT1,VT2; |
| 283 | NumElements = |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 284 | TLI.getVectorTypeBreakdown(cast<VectorType>(PN->getType()), |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 285 | VT1, VT2); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 287 | unsigned PHIReg = ValueMap[PN]; |
| 288 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 289 | const TargetInstrInfo *TII = TLI.getTargetMachine().getInstrInfo(); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 290 | for (unsigned i = 0; i != NumElements; ++i) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 291 | BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 296 | /// CreateRegForValue - Allocate the appropriate number of virtual registers of |
| 297 | /// the correctly promoted or expanded types. Assign these registers |
| 298 | /// consecutive vreg numbers and return the first assigned number. |
| 299 | unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { |
| 300 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 301 | |
| 302 | // The number of multiples of registers that we need, to, e.g., split up |
| 303 | // a <2 x int64> -> 4 x i32 registers. |
| 304 | unsigned NumVectorRegs = 1; |
| 305 | |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 306 | // If this is a vector type, figure out what type it will decompose into |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 307 | // and how many of the elements it will use. |
| 308 | if (VT == MVT::Vector) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 309 | const VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 310 | unsigned NumElts = PTy->getNumElements(); |
| 311 | MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType()); |
| 312 | |
| 313 | // Divide the input until we get to a supported size. This will always |
| 314 | // end with a scalar if the target doesn't support vectors. |
| 315 | while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) { |
| 316 | NumElts >>= 1; |
| 317 | NumVectorRegs <<= 1; |
| 318 | } |
Chris Lattner | 6cb7004 | 2006-03-16 23:05:19 +0000 | [diff] [blame] | 319 | if (NumElts == 1) |
| 320 | VT = EltTy; |
| 321 | else |
| 322 | VT = getVectorType(EltTy, NumElts); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // The common case is that we will only create one register for this |
| 326 | // value. If we have that case, create and return the virtual register. |
| 327 | unsigned NV = TLI.getNumElements(VT); |
| 328 | if (NV == 1) { |
| 329 | // If we are promoting this value, pick the next largest supported type. |
| 330 | MVT::ValueType PromotedType = TLI.getTypeToTransformTo(VT); |
| 331 | unsigned Reg = MakeReg(PromotedType); |
| 332 | // If this is a vector of supported or promoted types (e.g. 4 x i16), |
| 333 | // create all of the registers. |
| 334 | for (unsigned i = 1; i != NumVectorRegs; ++i) |
| 335 | MakeReg(PromotedType); |
| 336 | return Reg; |
| 337 | } |
| 338 | |
| 339 | // If this value is represented with multiple target registers, make sure |
| 340 | // to create enough consecutive registers of the right (smaller) type. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 341 | VT = TLI.getTypeToExpandTo(VT); |
| 342 | unsigned R = MakeReg(VT); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 343 | for (unsigned i = 1; i != NV*NumVectorRegs; ++i) |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 344 | MakeReg(VT); |
Chris Lattner | 3c38449 | 2006-03-16 19:51:18 +0000 | [diff] [blame] | 345 | return R; |
| 346 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 347 | |
| 348 | //===----------------------------------------------------------------------===// |
| 349 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 350 | /// implementation that is parameterized by a TargetLowering object. |
| 351 | /// Also, targets can overload any lowering method. |
| 352 | /// |
| 353 | namespace llvm { |
| 354 | class SelectionDAGLowering { |
| 355 | MachineBasicBlock *CurMBB; |
| 356 | |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 357 | DenseMap<const Value*, SDOperand> NodeMap; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 358 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 359 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 360 | /// them up and then emit token factor nodes when possible. This allows us to |
| 361 | /// get simple disambiguation between loads without worrying about alias |
| 362 | /// analysis. |
| 363 | std::vector<SDOperand> PendingLoads; |
| 364 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 365 | /// Case - A pair of values to record the Value for a switch case, and the |
| 366 | /// case's target basic block. |
| 367 | typedef std::pair<Constant*, MachineBasicBlock*> Case; |
| 368 | typedef std::vector<Case>::iterator CaseItr; |
| 369 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
| 370 | |
| 371 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 372 | /// of conditional branches. |
| 373 | struct CaseRec { |
| 374 | CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) : |
| 375 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 376 | |
| 377 | /// CaseBB - The MBB in which to emit the compare and branch |
| 378 | MachineBasicBlock *CaseBB; |
| 379 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 380 | /// greater-than-or-equal-to these Constants. |
| 381 | Constant *LT; |
| 382 | Constant *GE; |
| 383 | /// Range - A pair of iterators representing the range of case values to be |
| 384 | /// processed at this point in the binary search tree. |
| 385 | CaseRange Range; |
| 386 | }; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 387 | |
| 388 | typedef std::vector<CaseRec> CaseRecVector; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 389 | |
| 390 | /// The comparison function for sorting Case values. |
| 391 | struct CaseCmp { |
| 392 | bool operator () (const Case& C1, const Case& C2) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 393 | assert(isa<ConstantInt>(C1.first) && isa<ConstantInt>(C2.first)); |
Chris Lattner | ae4f99d | 2007-02-13 20:09:07 +0000 | [diff] [blame] | 394 | return cast<const ConstantInt>(C1.first)->getSExtValue() < |
| 395 | cast<const ConstantInt>(C2.first)->getSExtValue(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 396 | } |
| 397 | }; |
| 398 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 399 | public: |
| 400 | // TLI - This is information that describes the available target features we |
| 401 | // need for lowering. This indicates when operations are unavailable, |
| 402 | // implemented with a libcall, etc. |
| 403 | TargetLowering &TLI; |
| 404 | SelectionDAG &DAG; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 405 | const TargetData *TD; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 406 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 407 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 408 | /// SwitchInst code generation information. |
| 409 | std::vector<SelectionDAGISel::CaseBlock> SwitchCases; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 410 | /// JTCases - Vector of JumpTable structures used to communicate |
| 411 | /// SwitchInst code generation information. |
| 412 | std::vector<SelectionDAGISel::JumpTableBlock> JTCases; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 414 | /// FuncInfo - Information about the function as a whole. |
| 415 | /// |
| 416 | FunctionLoweringInfo &FuncInfo; |
| 417 | |
| 418 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 419 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 420 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 421 | FuncInfo(funcinfo) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 424 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 425 | /// |
| 426 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 427 | if (PendingLoads.empty()) |
| 428 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 429 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 430 | if (PendingLoads.size() == 1) { |
| 431 | SDOperand Root = PendingLoads[0]; |
| 432 | DAG.setRoot(Root); |
| 433 | PendingLoads.clear(); |
| 434 | return Root; |
| 435 | } |
| 436 | |
| 437 | // Otherwise, we have to make a token factor node. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 438 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 439 | &PendingLoads[0], PendingLoads.size()); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 440 | PendingLoads.clear(); |
| 441 | DAG.setRoot(Root); |
| 442 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 445 | SDOperand CopyValueToVirtualRegister(Value *V, unsigned Reg); |
| 446 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 447 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 448 | |
| 449 | void visit(unsigned Opcode, User &I) { |
Chris Lattner | 1e7aa5c | 2006-11-10 04:41:34 +0000 | [diff] [blame] | 450 | // Note: this doesn't use InstVisitor, because it has to work with |
| 451 | // ConstantExpr's in addition to instructions. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 452 | switch (Opcode) { |
| 453 | default: assert(0 && "Unknown instruction type encountered!"); |
| 454 | abort(); |
| 455 | // Build the switch statement using the Instruction.def file. |
| 456 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 457 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 458 | #include "llvm/Instruction.def" |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 463 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 464 | SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 465 | const Value *SV, SDOperand Root, |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 466 | bool isVolatile); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 467 | |
| 468 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 469 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 470 | } |
| 471 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 472 | SDOperand getValue(const Value *V); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 474 | void setValue(const Value *V, SDOperand NewN) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 475 | SDOperand &N = NodeMap[V]; |
| 476 | assert(N.Val == 0 && "Already set a value for this node!"); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 477 | N = NewN; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 478 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 480 | RegsForValue GetRegistersForValue(const std::string &ConstrCode, |
| 481 | MVT::ValueType VT, |
| 482 | bool OutReg, bool InReg, |
| 483 | std::set<unsigned> &OutputRegs, |
| 484 | std::set<unsigned> &InputRegs); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 486 | void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB, |
| 487 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
| 488 | unsigned Opc); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 489 | bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 490 | void ExportFromCurrentBlock(Value *V); |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 491 | void LowerCallTo(Instruction &I, |
| 492 | const Type *CalledValueTy, unsigned CallingConv, |
| 493 | bool IsTailCall, SDOperand Callee, unsigned OpIdx); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 495 | // Terminator instructions. |
| 496 | void visitRet(ReturnInst &I); |
| 497 | void visitBr(BranchInst &I); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 498 | void visitSwitch(SwitchInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 499 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 500 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 501 | // Helpers for visitSwitch |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 502 | bool handleSmallSwitchRange(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 503 | CaseRecVector& WorkList, |
| 504 | Value* SV, |
| 505 | MachineBasicBlock* Default); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 506 | bool handleJTSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 507 | CaseRecVector& WorkList, |
| 508 | Value* SV, |
| 509 | MachineBasicBlock* Default); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 510 | bool handleBTSplitSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 511 | CaseRecVector& WorkList, |
| 512 | Value* SV, |
| 513 | MachineBasicBlock* Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 514 | void visitSwitchCase(SelectionDAGISel::CaseBlock &CB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 515 | void visitJumpTable(SelectionDAGISel::JumpTable &JT); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 516 | void visitJumpTableHeader(SelectionDAGISel::JumpTable &JT, |
| 517 | SelectionDAGISel::JumpTableHeader &JTH); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 519 | // These all get lowered before this pass. |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 520 | void visitInvoke(InvokeInst &I); |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 521 | void visitInvoke(InvokeInst &I, bool AsTerminator); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 522 | void visitUnwind(UnwindInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 523 | |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 524 | void visitScalarBinary(User &I, unsigned OpCode); |
| 525 | void visitVectorBinary(User &I, unsigned OpCode); |
| 526 | void visitEitherBinary(User &I, unsigned ScalarOp, unsigned VectorOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 527 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 528 | void visitAdd(User &I) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 529 | if (isa<VectorType>(I.getType())) |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 530 | visitVectorBinary(I, ISD::VADD); |
| 531 | else if (I.getType()->isFloatingPoint()) |
| 532 | visitScalarBinary(I, ISD::FADD); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 533 | else |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 534 | visitScalarBinary(I, ISD::ADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 536 | void visitSub(User &I); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 537 | void visitMul(User &I) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 538 | if (isa<VectorType>(I.getType())) |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 539 | visitVectorBinary(I, ISD::VMUL); |
| 540 | else if (I.getType()->isFloatingPoint()) |
| 541 | visitScalarBinary(I, ISD::FMUL); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 542 | else |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 543 | visitScalarBinary(I, ISD::MUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 544 | } |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 545 | void visitURem(User &I) { visitScalarBinary(I, ISD::UREM); } |
| 546 | void visitSRem(User &I) { visitScalarBinary(I, ISD::SREM); } |
| 547 | void visitFRem(User &I) { visitScalarBinary(I, ISD::FREM); } |
| 548 | void visitUDiv(User &I) { visitEitherBinary(I, ISD::UDIV, ISD::VUDIV); } |
| 549 | void visitSDiv(User &I) { visitEitherBinary(I, ISD::SDIV, ISD::VSDIV); } |
| 550 | void visitFDiv(User &I) { visitEitherBinary(I, ISD::FDIV, ISD::VSDIV); } |
| 551 | void visitAnd (User &I) { visitEitherBinary(I, ISD::AND, ISD::VAND ); } |
| 552 | void visitOr (User &I) { visitEitherBinary(I, ISD::OR, ISD::VOR ); } |
| 553 | void visitXor (User &I) { visitEitherBinary(I, ISD::XOR, ISD::VXOR ); } |
| 554 | void visitShl (User &I) { visitShift(I, ISD::SHL); } |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 555 | void visitLShr(User &I) { visitShift(I, ISD::SRL); } |
| 556 | void visitAShr(User &I) { visitShift(I, ISD::SRA); } |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 557 | void visitICmp(User &I); |
| 558 | void visitFCmp(User &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 559 | // Visit the conversion instructions |
| 560 | void visitTrunc(User &I); |
| 561 | void visitZExt(User &I); |
| 562 | void visitSExt(User &I); |
| 563 | void visitFPTrunc(User &I); |
| 564 | void visitFPExt(User &I); |
| 565 | void visitFPToUI(User &I); |
| 566 | void visitFPToSI(User &I); |
| 567 | void visitUIToFP(User &I); |
| 568 | void visitSIToFP(User &I); |
| 569 | void visitPtrToInt(User &I); |
| 570 | void visitIntToPtr(User &I); |
| 571 | void visitBitCast(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 573 | void visitExtractElement(User &I); |
| 574 | void visitInsertElement(User &I); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 575 | void visitShuffleVector(User &I); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 577 | void visitGetElementPtr(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 578 | void visitSelect(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 579 | |
| 580 | void visitMalloc(MallocInst &I); |
| 581 | void visitFree(FreeInst &I); |
| 582 | void visitAlloca(AllocaInst &I); |
| 583 | void visitLoad(LoadInst &I); |
| 584 | void visitStore(StoreInst &I); |
| 585 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 586 | void visitCall(CallInst &I); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 587 | void visitInlineAsm(CallInst &I); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 588 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 589 | void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 591 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 592 | void visitVAArg(VAArgInst &I); |
| 593 | void visitVAEnd(CallInst &I); |
| 594 | void visitVACopy(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 596 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 597 | |
| 598 | void visitUserOp1(Instruction &I) { |
| 599 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 600 | abort(); |
| 601 | } |
| 602 | void visitUserOp2(Instruction &I) { |
| 603 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 604 | abort(); |
| 605 | } |
| 606 | }; |
| 607 | } // end namespace llvm |
| 608 | |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 609 | SDOperand SelectionDAGLowering::getValue(const Value *V) { |
| 610 | SDOperand &N = NodeMap[V]; |
| 611 | if (N.Val) return N; |
| 612 | |
| 613 | const Type *VTy = V->getType(); |
| 614 | MVT::ValueType VT = TLI.getValueType(VTy); |
| 615 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 616 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 617 | visit(CE->getOpcode(), *CE); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 618 | SDOperand N1 = NodeMap[V]; |
| 619 | assert(N1.Val && "visit didn't populate the ValueMap!"); |
| 620 | return N1; |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 621 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 622 | return N = DAG.getGlobalAddress(GV, VT); |
| 623 | } else if (isa<ConstantPointerNull>(C)) { |
| 624 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 625 | } else if (isa<UndefValue>(C)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 626 | if (!isa<VectorType>(VTy)) |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 627 | return N = DAG.getNode(ISD::UNDEF, VT); |
| 628 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 629 | // Create a VBUILD_VECTOR of undef nodes. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 630 | const VectorType *PTy = cast<VectorType>(VTy); |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 631 | unsigned NumElements = PTy->getNumElements(); |
| 632 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 633 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 634 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 635 | Ops.assign(NumElements, DAG.getNode(ISD::UNDEF, PVT)); |
| 636 | |
| 637 | // Create a VConstant node with generic Vector type. |
| 638 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 639 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 640 | return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 641 | &Ops[0], Ops.size()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 642 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 643 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 644 | } else if (const VectorType *PTy = dyn_cast<VectorType>(VTy)) { |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 645 | unsigned NumElements = PTy->getNumElements(); |
| 646 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 647 | |
| 648 | // Now that we know the number and type of the elements, push a |
| 649 | // Constant or ConstantFP node onto the ops list for each element of |
| 650 | // the packed constant. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 651 | SmallVector<SDOperand, 8> Ops; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 652 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 653 | for (unsigned i = 0; i != NumElements; ++i) |
| 654 | Ops.push_back(getValue(CP->getOperand(i))); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 655 | } else { |
| 656 | assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!"); |
| 657 | SDOperand Op; |
| 658 | if (MVT::isFloatingPoint(PVT)) |
| 659 | Op = DAG.getConstantFP(0, PVT); |
| 660 | else |
| 661 | Op = DAG.getConstant(0, PVT); |
| 662 | Ops.assign(NumElements, Op); |
| 663 | } |
| 664 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 665 | // Create a VBUILD_VECTOR node with generic Vector type. |
Chris Lattner | 23d564c | 2006-03-19 00:20:20 +0000 | [diff] [blame] | 666 | Ops.push_back(DAG.getConstant(NumElements, MVT::i32)); |
| 667 | Ops.push_back(DAG.getValueType(PVT)); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 668 | return NodeMap[V] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], |
| 669 | Ops.size()); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 670 | } else { |
| 671 | // Canonicalize all constant ints to be unsigned. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 672 | return N = DAG.getConstant(cast<ConstantInt>(C)->getZExtValue(),VT); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 677 | std::map<const AllocaInst*, int>::iterator SI = |
| 678 | FuncInfo.StaticAllocaMap.find(AI); |
| 679 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 680 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 681 | } |
| 682 | |
Chris Lattner | 251db18 | 2007-02-25 18:40:32 +0000 | [diff] [blame] | 683 | unsigned InReg = FuncInfo.ValueMap[V]; |
| 684 | assert(InReg && "Value not in map!"); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 685 | |
| 686 | // If this type is not legal, make it so now. |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 687 | if (VT != MVT::Vector) { |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 688 | if (TLI.getTypeAction(VT) == TargetLowering::Expand) { |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 689 | // Source must be expanded. This input value is actually coming from the |
Chris Lattner | 251db18 | 2007-02-25 18:40:32 +0000 | [diff] [blame] | 690 | // register pair InReg and InReg+1. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 691 | MVT::ValueType DestVT = TLI.getTypeToExpandTo(VT); |
| 692 | unsigned NumVals = TLI.getNumElements(VT); |
| 693 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 694 | if (NumVals == 1) |
| 695 | N = DAG.getNode(ISD::BIT_CONVERT, VT, N); |
| 696 | else { |
| 697 | assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!"); |
| 698 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 699 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 700 | } |
| 701 | } else { |
| 702 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 703 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 704 | if (TLI.getTypeAction(VT) == TargetLowering::Promote) // Promotion case |
| 705 | N = MVT::isFloatingPoint(VT) |
| 706 | ? DAG.getNode(ISD::FP_ROUND, VT, N) |
| 707 | : DAG.getNode(ISD::TRUNCATE, VT, N); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 708 | } |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 709 | } else { |
| 710 | // Otherwise, if this is a vector, make it available as a generic vector |
| 711 | // here. |
| 712 | MVT::ValueType PTyElementVT, PTyLegalElementVT; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 713 | const VectorType *PTy = cast<VectorType>(VTy); |
| 714 | unsigned NE = TLI.getVectorTypeBreakdown(PTy, PTyElementVT, |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 715 | PTyLegalElementVT); |
| 716 | |
| 717 | // Build a VBUILD_VECTOR with the input registers. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 718 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 719 | if (PTyElementVT == PTyLegalElementVT) { |
| 720 | // If the value types are legal, just VBUILD the CopyFromReg nodes. |
| 721 | for (unsigned i = 0; i != NE; ++i) |
| 722 | Ops.push_back(DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 723 | PTyElementVT)); |
| 724 | } else if (PTyElementVT < PTyLegalElementVT) { |
| 725 | // If the register was promoted, use TRUNCATE of FP_ROUND as appropriate. |
| 726 | for (unsigned i = 0; i != NE; ++i) { |
| 727 | SDOperand Op = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 728 | PTyElementVT); |
| 729 | if (MVT::isFloatingPoint(PTyElementVT)) |
| 730 | Op = DAG.getNode(ISD::FP_ROUND, PTyElementVT, Op); |
| 731 | else |
| 732 | Op = DAG.getNode(ISD::TRUNCATE, PTyElementVT, Op); |
| 733 | Ops.push_back(Op); |
| 734 | } |
| 735 | } else { |
| 736 | // If the register was expanded, use BUILD_PAIR. |
| 737 | assert((NE & 1) == 0 && "Must expand into a multiple of 2 elements!"); |
| 738 | for (unsigned i = 0; i != NE/2; ++i) { |
| 739 | SDOperand Op0 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 740 | PTyElementVT); |
| 741 | SDOperand Op1 = DAG.getCopyFromReg(DAG.getEntryNode(), InReg++, |
| 742 | PTyElementVT); |
| 743 | Ops.push_back(DAG.getNode(ISD::BUILD_PAIR, VT, Op0, Op1)); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | Ops.push_back(DAG.getConstant(NE, MVT::i32)); |
| 748 | Ops.push_back(DAG.getValueType(PTyLegalElementVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 749 | N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 2e2ef95 | 2006-04-05 06:54:42 +0000 | [diff] [blame] | 750 | |
| 751 | // Finally, use a VBIT_CONVERT to make this available as the appropriate |
| 752 | // vector type. |
| 753 | N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N, |
| 754 | DAG.getConstant(PTy->getNumElements(), |
| 755 | MVT::i32), |
| 756 | DAG.getValueType(TLI.getValueType(PTy->getElementType()))); |
Chris Lattner | 199862b | 2006-03-16 19:57:50 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | return N; |
| 760 | } |
| 761 | |
| 762 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 763 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 764 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 765 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 766 | return; |
| 767 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 768 | SmallVector<SDOperand, 8> NewValues; |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 769 | NewValues.push_back(getRoot()); |
| 770 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 771 | SDOperand RetOp = getValue(I.getOperand(i)); |
| 772 | |
| 773 | // If this is an integer return value, we need to promote it ourselves to |
| 774 | // the full width of a register, since LegalizeOp will use ANY_EXTEND rather |
| 775 | // than sign/zero. |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 776 | // FIXME: C calling convention requires the return type to be promoted to |
| 777 | // at least 32-bit. But this is not necessary for non-C calling conventions. |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 778 | if (MVT::isInteger(RetOp.getValueType()) && |
| 779 | RetOp.getValueType() < MVT::i64) { |
| 780 | MVT::ValueType TmpVT; |
| 781 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 782 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
| 783 | else |
| 784 | TmpVT = MVT::i32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 785 | const FunctionType *FTy = I.getParent()->getParent()->getFunctionType(); |
Reid Spencer | bcca340 | 2007-01-03 16:49:33 +0000 | [diff] [blame] | 786 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
Reid Spencer | 8c57dfb | 2007-01-03 04:25:33 +0000 | [diff] [blame] | 787 | if (FTy->paramHasAttr(0, FunctionType::SExtAttribute)) |
| 788 | ExtendKind = ISD::SIGN_EXTEND; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 789 | if (FTy->paramHasAttr(0, FunctionType::ZExtAttribute)) |
| 790 | ExtendKind = ISD::ZERO_EXTEND; |
Reid Spencer | 376dd21 | 2007-01-03 05:03:05 +0000 | [diff] [blame] | 791 | RetOp = DAG.getNode(ExtendKind, TmpVT, RetOp); |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 792 | } |
| 793 | NewValues.push_back(RetOp); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 794 | NewValues.push_back(DAG.getConstant(false, MVT::i32)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 796 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, |
| 797 | &NewValues[0], NewValues.size())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 800 | /// ExportFromCurrentBlock - If this condition isn't known to be exported from |
| 801 | /// the current basic block, add it to ValueMap now so that we'll get a |
| 802 | /// CopyTo/FromReg. |
| 803 | void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) { |
| 804 | // No need to export constants. |
| 805 | if (!isa<Instruction>(V) && !isa<Argument>(V)) return; |
| 806 | |
| 807 | // Already exported? |
| 808 | if (FuncInfo.isExportedInst(V)) return; |
| 809 | |
| 810 | unsigned Reg = FuncInfo.InitializeRegForValue(V); |
| 811 | PendingLoads.push_back(CopyValueToVirtualRegister(V, Reg)); |
| 812 | } |
| 813 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 814 | bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V, |
| 815 | const BasicBlock *FromBB) { |
| 816 | // The operands of the setcc have to be in this block. We don't know |
| 817 | // how to export them from some other block. |
| 818 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 819 | // Can export from current BB. |
| 820 | if (VI->getParent() == FromBB) |
| 821 | return true; |
| 822 | |
| 823 | // Is already exported, noop. |
| 824 | return FuncInfo.isExportedInst(V); |
| 825 | } |
| 826 | |
| 827 | // If this is an argument, we can export it if the BB is the entry block or |
| 828 | // if it is already exported. |
| 829 | if (isa<Argument>(V)) { |
| 830 | if (FromBB == &FromBB->getParent()->getEntryBlock()) |
| 831 | return true; |
| 832 | |
| 833 | // Otherwise, can only export this if it is already exported. |
| 834 | return FuncInfo.isExportedInst(V); |
| 835 | } |
| 836 | |
| 837 | // Otherwise, constants can always be exported. |
| 838 | return true; |
| 839 | } |
| 840 | |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 841 | static bool InBlock(const Value *V, const BasicBlock *BB) { |
| 842 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 843 | return I->getParent() == BB; |
| 844 | return true; |
| 845 | } |
| 846 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 847 | /// FindMergedConditions - If Cond is an expression like |
| 848 | void SelectionDAGLowering::FindMergedConditions(Value *Cond, |
| 849 | MachineBasicBlock *TBB, |
| 850 | MachineBasicBlock *FBB, |
| 851 | MachineBasicBlock *CurBB, |
| 852 | unsigned Opc) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 853 | // If this node is not part of the or/and tree, emit it as a branch. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 854 | Instruction *BOp = dyn_cast<Instruction>(Cond); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 855 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 856 | if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) || |
| 857 | (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() || |
Chris Lattner | 6a586c8 | 2006-10-29 21:01:20 +0000 | [diff] [blame] | 858 | BOp->getParent() != CurBB->getBasicBlock() || |
| 859 | !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) || |
| 860 | !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 861 | const BasicBlock *BB = CurBB->getBasicBlock(); |
| 862 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 863 | // If the leaf of the tree is a comparison, merge the condition into |
| 864 | // the caseblock. |
| 865 | if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) && |
| 866 | // The operands of the cmp have to be in this block. We don't know |
Chris Lattner | 5a145f0 | 2006-10-29 18:23:37 +0000 | [diff] [blame] | 867 | // how to export them from some other block. If this is the first block |
| 868 | // of the sequence, no exporting is needed. |
| 869 | (CurBB == CurMBB || |
| 870 | (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && |
| 871 | isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 872 | BOp = cast<Instruction>(Cond); |
| 873 | ISD::CondCode Condition; |
| 874 | if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { |
| 875 | switch (IC->getPredicate()) { |
| 876 | default: assert(0 && "Unknown icmp predicate opcode!"); |
| 877 | case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break; |
| 878 | case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break; |
| 879 | case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break; |
| 880 | case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break; |
| 881 | case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break; |
| 882 | case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break; |
| 883 | case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break; |
| 884 | case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break; |
| 885 | case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break; |
| 886 | case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break; |
| 887 | } |
| 888 | } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) { |
| 889 | ISD::CondCode FPC, FOC; |
| 890 | switch (FC->getPredicate()) { |
| 891 | default: assert(0 && "Unknown fcmp predicate opcode!"); |
| 892 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 893 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 894 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 895 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 896 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 897 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 898 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 899 | case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break; |
| 900 | case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break; |
| 901 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 902 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 903 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 904 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 905 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 906 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 907 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 908 | } |
| 909 | if (FiniteOnlyFPMath()) |
| 910 | Condition = FOC; |
| 911 | else |
| 912 | Condition = FPC; |
| 913 | } else { |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 914 | Condition = ISD::SETEQ; // silence warning. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 915 | assert(0 && "Unknown compare instruction"); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 918 | SelectionDAGISel::CaseBlock CB(Condition, BOp->getOperand(0), |
| 919 | BOp->getOperand(1), TBB, FBB, CurBB); |
| 920 | SwitchCases.push_back(CB); |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | // Create a CaseBlock record representing this branch. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 925 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(), |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 926 | TBB, FBB, CurBB); |
| 927 | SwitchCases.push_back(CB); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 931 | |
| 932 | // Create TmpBB after CurBB. |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 933 | MachineFunction::iterator BBI = CurBB; |
| 934 | MachineBasicBlock *TmpBB = new MachineBasicBlock(CurBB->getBasicBlock()); |
| 935 | CurBB->getParent()->getBasicBlockList().insert(++BBI, TmpBB); |
| 936 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 937 | if (Opc == Instruction::Or) { |
| 938 | // Codegen X | Y as: |
| 939 | // jmp_if_X TBB |
| 940 | // jmp TmpBB |
| 941 | // TmpBB: |
| 942 | // jmp_if_Y TBB |
| 943 | // jmp FBB |
| 944 | // |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 945 | |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 946 | // Emit the LHS condition. |
| 947 | FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc); |
| 948 | |
| 949 | // Emit the RHS condition into TmpBB. |
| 950 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 951 | } else { |
| 952 | assert(Opc == Instruction::And && "Unknown merge op!"); |
| 953 | // Codegen X & Y as: |
| 954 | // jmp_if_X TmpBB |
| 955 | // jmp FBB |
| 956 | // TmpBB: |
| 957 | // jmp_if_Y TBB |
| 958 | // jmp FBB |
| 959 | // |
| 960 | // This requires creation of TmpBB after CurBB. |
| 961 | |
| 962 | // Emit the LHS condition. |
| 963 | FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc); |
| 964 | |
| 965 | // Emit the RHS condition into TmpBB. |
| 966 | FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc); |
| 967 | } |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 970 | /// If the set of cases should be emitted as a series of branches, return true. |
| 971 | /// If we should emit this as a bunch of and/or'd together conditions, return |
| 972 | /// false. |
| 973 | static bool |
| 974 | ShouldEmitAsBranches(const std::vector<SelectionDAGISel::CaseBlock> &Cases) { |
| 975 | if (Cases.size() != 2) return true; |
| 976 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 977 | // If this is two comparisons of the same values or'd or and'd together, they |
| 978 | // will get folded into a single comparison, so don't emit two blocks. |
| 979 | if ((Cases[0].CmpLHS == Cases[1].CmpLHS && |
| 980 | Cases[0].CmpRHS == Cases[1].CmpRHS) || |
| 981 | (Cases[0].CmpRHS == Cases[1].CmpLHS && |
| 982 | Cases[0].CmpLHS == Cases[1].CmpRHS)) { |
| 983 | return false; |
| 984 | } |
| 985 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 986 | return true; |
| 987 | } |
| 988 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 989 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 990 | // Update machine-CFG edges. |
| 991 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 992 | |
| 993 | // Figure out which block is immediately after the current one. |
| 994 | MachineBasicBlock *NextBlock = 0; |
| 995 | MachineFunction::iterator BBI = CurMBB; |
| 996 | if (++BBI != CurMBB->getParent()->end()) |
| 997 | NextBlock = BBI; |
| 998 | |
| 999 | if (I.isUnconditional()) { |
| 1000 | // If this is not a fall-through branch, emit the branch. |
| 1001 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1002 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 1003 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1005 | // Update machine-CFG edges. |
| 1006 | CurMBB->addSuccessor(Succ0MBB); |
| 1007 | |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | // If this condition is one of the special cases we handle, do special stuff |
| 1012 | // now. |
| 1013 | Value *CondVal = I.getCondition(); |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1014 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1015 | |
| 1016 | // If this is a series of conditions that are or'd or and'd together, emit |
| 1017 | // this as a sequence of branches instead of setcc's with and/or operations. |
| 1018 | // For example, instead of something like: |
| 1019 | // cmp A, B |
| 1020 | // C = seteq |
| 1021 | // cmp D, E |
| 1022 | // F = setle |
| 1023 | // or C, F |
| 1024 | // jnz foo |
| 1025 | // Emit: |
| 1026 | // cmp A, B |
| 1027 | // je foo |
| 1028 | // cmp D, E |
| 1029 | // jle foo |
| 1030 | // |
| 1031 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) { |
| 1032 | if (BOp->hasOneUse() && |
Chris Lattner | d2f9ee9 | 2006-10-27 21:54:23 +0000 | [diff] [blame] | 1033 | (BOp->getOpcode() == Instruction::And || |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1034 | BOp->getOpcode() == Instruction::Or)) { |
| 1035 | FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode()); |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1036 | // If the compares in later blocks need to use values not currently |
| 1037 | // exported from this block, export them now. This block should always |
| 1038 | // be the first entry. |
| 1039 | assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!"); |
| 1040 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1041 | // Allow some cases to be rejected. |
| 1042 | if (ShouldEmitAsBranches(SwitchCases)) { |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1043 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) { |
| 1044 | ExportFromCurrentBlock(SwitchCases[i].CmpLHS); |
| 1045 | ExportFromCurrentBlock(SwitchCases[i].CmpRHS); |
| 1046 | } |
| 1047 | |
| 1048 | // Emit the branch for this block. |
| 1049 | visitSwitchCase(SwitchCases[0]); |
| 1050 | SwitchCases.erase(SwitchCases.begin()); |
| 1051 | return; |
Chris Lattner | 5a145f0 | 2006-10-29 18:23:37 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Chris Lattner | 0ccb500 | 2006-10-31 23:06:00 +0000 | [diff] [blame] | 1054 | // Okay, we decided not to do this, remove any inserted MBB's and clear |
| 1055 | // SwitchCases. |
| 1056 | for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) |
| 1057 | CurMBB->getParent()->getBasicBlockList().erase(SwitchCases[i].ThisBB); |
| 1058 | |
Chris Lattner | df19f27 | 2006-10-31 22:37:42 +0000 | [diff] [blame] | 1059 | SwitchCases.clear(); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Create a CaseBlock record representing this branch. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1064 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(), |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 1065 | Succ0MBB, Succ1MBB, CurMBB); |
| 1066 | // Use visitSwitchCase to actually insert the fast branch sequence for this |
| 1067 | // cond branch. |
| 1068 | visitSwitchCase(CB); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1071 | /// visitSwitchCase - Emits the necessary code to represent a single node in |
| 1072 | /// the binary search tree resulting from lowering a switch instruction. |
| 1073 | void SelectionDAGLowering::visitSwitchCase(SelectionDAGISel::CaseBlock &CB) { |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1074 | SDOperand Cond; |
| 1075 | SDOperand CondLHS = getValue(CB.CmpLHS); |
| 1076 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1077 | // Build the setcc now, fold "(X == true)" to X and "(X == false)" to !X to |
| 1078 | // handle common cases produced by branch lowering. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1079 | if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ) |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1080 | Cond = CondLHS; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1081 | else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 1082 | SDOperand True = DAG.getConstant(1, CondLHS.getValueType()); |
| 1083 | Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True); |
| 1084 | } else |
| 1085 | Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1086 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1087 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1088 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1089 | MachineBasicBlock *NextBlock = 0; |
| 1090 | MachineFunction::iterator BBI = CurMBB; |
| 1091 | if (++BBI != CurMBB->getParent()->end()) |
| 1092 | NextBlock = BBI; |
| 1093 | |
| 1094 | // If the lhs block is the next block, invert the condition so that we can |
| 1095 | // fall through to the lhs instead of the rhs block. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1096 | if (CB.TrueBB == NextBlock) { |
| 1097 | std::swap(CB.TrueBB, CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1098 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 1099 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
| 1100 | } |
| 1101 | SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), Cond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1102 | DAG.getBasicBlock(CB.TrueBB)); |
| 1103 | if (CB.FalseBB == NextBlock) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1104 | DAG.setRoot(BrCond); |
| 1105 | else |
| 1106 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1107 | DAG.getBasicBlock(CB.FalseBB))); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1108 | // Update successor info |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 1109 | CurMBB->addSuccessor(CB.TrueBB); |
| 1110 | CurMBB->addSuccessor(CB.FalseBB); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1113 | /// visitJumpTable - Emit JumpTable node in the current MBB |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1114 | void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1115 | // Emit the code for the jump table |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1116 | assert(JT.Reg != -1UL && "Should lower JT Header first!"); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1117 | MVT::ValueType PTy = TLI.getPointerTy(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1118 | SDOperand Index = DAG.getCopyFromReg(getRoot(), JT.Reg, PTy); |
| 1119 | SDOperand Table = DAG.getJumpTable(JT.JTI, PTy); |
| 1120 | DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1), |
| 1121 | Table, Index)); |
| 1122 | return; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1125 | /// visitJumpTableHeader - This function emits necessary code to produce index |
| 1126 | /// in the JumpTable from switch case. |
| 1127 | void SelectionDAGLowering::visitJumpTableHeader(SelectionDAGISel::JumpTable &JT, |
| 1128 | SelectionDAGISel::JumpTableHeader &JTH) { |
| 1129 | // Subtract the lowest switch case value from the value being switched on |
| 1130 | // and conditional branch to default mbb if the result is greater than the |
| 1131 | // difference between smallest and largest cases. |
| 1132 | SDOperand SwitchOp = getValue(JTH.SValue); |
| 1133 | MVT::ValueType VT = SwitchOp.getValueType(); |
| 1134 | SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp, |
| 1135 | DAG.getConstant(JTH.First, VT)); |
| 1136 | |
| 1137 | // The SDNode we just created, which holds the value being switched on |
| 1138 | // minus the the smallest case value, needs to be copied to a virtual |
| 1139 | // register so it can be used as an index into the jump table in a |
| 1140 | // subsequent basic block. This value may be smaller or larger than the |
| 1141 | // target's pointer type, and therefore require extension or truncating. |
| 1142 | if (VT > TLI.getPointerTy()) |
| 1143 | SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB); |
| 1144 | else |
| 1145 | SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB); |
| 1146 | |
| 1147 | unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy()); |
| 1148 | SDOperand CopyTo = DAG.getCopyToReg(getRoot(), JumpTableReg, SwitchOp); |
| 1149 | JT.Reg = JumpTableReg; |
| 1150 | |
| 1151 | // Emit the range check for the jump table, and branch to the default |
| 1152 | // block for the switch statement if the value being switched on exceeds |
| 1153 | // the largest case in the switch. |
| 1154 | SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultTy(), SUB, |
| 1155 | DAG.getConstant(JTH.Last-JTH.First,VT), |
| 1156 | ISD::SETUGT); |
| 1157 | |
| 1158 | // Set NextBlock to be the MBB immediately after the current one, if any. |
| 1159 | // This is used to avoid emitting unnecessary branches to the next block. |
| 1160 | MachineBasicBlock *NextBlock = 0; |
| 1161 | MachineFunction::iterator BBI = CurMBB; |
| 1162 | if (++BBI != CurMBB->getParent()->end()) |
| 1163 | NextBlock = BBI; |
| 1164 | |
| 1165 | SDOperand BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP, |
| 1166 | DAG.getBasicBlock(JT.Default)); |
| 1167 | |
| 1168 | if (JT.MBB == NextBlock) |
| 1169 | DAG.setRoot(BrCond); |
| 1170 | else |
| 1171 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, |
| 1172 | DAG.getBasicBlock(JT.MBB))); |
| 1173 | } |
| 1174 | |
| 1175 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1176 | void SelectionDAGLowering::visitInvoke(InvokeInst &I) { |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 1177 | assert(0 && "Should never be visited directly"); |
| 1178 | } |
| 1179 | void SelectionDAGLowering::visitInvoke(InvokeInst &I, bool AsTerminator) { |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1180 | // Retrieve successors. |
| 1181 | MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 1182 | MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 1183 | |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 1184 | if (!AsTerminator) { |
| 1185 | // Mark landing pad so that it doesn't get deleted in branch folding. |
| 1186 | LandingPad->setIsLandingPad(); |
| 1187 | |
| 1188 | // Insert a label before the invoke call to mark the try range. |
| 1189 | // This can be used to detect deletion of the invoke via the |
| 1190 | // MachineModuleInfo. |
| 1191 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 1192 | unsigned BeginLabel = MMI->NextLabelID(); |
| 1193 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
| 1194 | DAG.getConstant(BeginLabel, MVT::i32))); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1195 | |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 1196 | LowerCallTo(I, I.getCalledValue()->getType(), |
| 1197 | I.getCallingConv(), |
| 1198 | false, |
| 1199 | getValue(I.getOperand(0)), |
| 1200 | 3); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1201 | |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 1202 | // Insert a label before the invoke call to mark the try range. |
| 1203 | // This can be used to detect deletion of the invoke via the |
| 1204 | // MachineModuleInfo. |
| 1205 | unsigned EndLabel = MMI->NextLabelID(); |
| 1206 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
| 1207 | DAG.getConstant(EndLabel, MVT::i32))); |
| 1208 | |
| 1209 | // Inform MachineModuleInfo of range. |
| 1210 | MMI->addInvoke(LandingPad, BeginLabel, EndLabel); |
| 1211 | |
| 1212 | // Update successor info |
| 1213 | CurMBB->addSuccessor(Return); |
| 1214 | CurMBB->addSuccessor(LandingPad); |
| 1215 | } else { |
| 1216 | // Drop into normal successor. |
| 1217 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
| 1218 | DAG.getBasicBlock(Return))); |
| 1219 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | void SelectionDAGLowering::visitUnwind(UnwindInst &I) { |
| 1223 | } |
| 1224 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1225 | /// handleSmaaSwitchCaseRange - Emit a series of specific tests (suitable for |
| 1226 | /// small case ranges). |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1227 | bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1228 | CaseRecVector& WorkList, |
| 1229 | Value* SV, |
| 1230 | MachineBasicBlock* Default) { |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1231 | Case& BackCase = *(CR.Range.second-1); |
| 1232 | |
| 1233 | // Size is the number of Cases represented by this range. |
| 1234 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1235 | if (Size >=3) |
| 1236 | return false; |
| 1237 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1238 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1239 | // inserting any additional MBBs necessary to represent the switch. |
| 1240 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1241 | |
| 1242 | // Figure out which block is immediately after the current one. |
| 1243 | MachineBasicBlock *NextBlock = 0; |
| 1244 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1245 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1246 | if (++BBI != CurMBB->getParent()->end()) |
| 1247 | NextBlock = BBI; |
| 1248 | |
| 1249 | // TODO: If any two of the cases has the same destination, and if one value |
| 1250 | // is the same as the other, but has one bit unset that the other has set, |
| 1251 | // use bit manipulation to do two compares at once. For example: |
| 1252 | // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" |
| 1253 | |
| 1254 | // Rearrange the case blocks so that the last one falls through if possible. |
| 1255 | if (NextBlock && Default != NextBlock && BackCase.second != NextBlock) { |
| 1256 | // The last case block won't fall through into 'NextBlock' if we emit the |
| 1257 | // branches in this order. See if rearranging a case value would help. |
| 1258 | for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) { |
| 1259 | if (I->second == NextBlock) { |
| 1260 | std::swap(*I, BackCase); |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // Create a CaseBlock record representing a conditional branch to |
| 1267 | // the Case's target mbb if the value being switched on SV is equal |
| 1268 | // to C. |
| 1269 | MachineBasicBlock *CurBlock = CR.CaseBB; |
| 1270 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) { |
| 1271 | MachineBasicBlock *FallThrough; |
| 1272 | if (I != E-1) { |
| 1273 | FallThrough = new MachineBasicBlock(CurBlock->getBasicBlock()); |
| 1274 | CurMF->getBasicBlockList().insert(BBI, FallThrough); |
| 1275 | } else { |
| 1276 | // If the last case doesn't match, go to the default block. |
| 1277 | FallThrough = Default; |
| 1278 | } |
| 1279 | |
| 1280 | SelectionDAGISel::CaseBlock CB(ISD::SETEQ, SV, I->first, |
| 1281 | I->second, FallThrough, CurBlock); |
| 1282 | |
| 1283 | // If emitting the first comparison, just call visitSwitchCase to emit the |
| 1284 | // code into the current block. Otherwise, push the CaseBlock onto the |
| 1285 | // vector to be later processed by SDISel, and insert the node's MBB |
| 1286 | // before the next MBB. |
| 1287 | if (CurBlock == CurMBB) |
| 1288 | visitSwitchCase(CB); |
| 1289 | else |
| 1290 | SwitchCases.push_back(CB); |
| 1291 | |
| 1292 | CurBlock = FallThrough; |
| 1293 | } |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1294 | |
| 1295 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /// handleJTSwitchCase - Emit jumptable for current switch case range |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1299 | bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1300 | CaseRecVector& WorkList, |
| 1301 | Value* SV, |
| 1302 | MachineBasicBlock* Default) { |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1303 | Case& FrontCase = *CR.Range.first; |
| 1304 | Case& BackCase = *(CR.Range.second-1); |
| 1305 | |
| 1306 | // Size is the number of Cases represented by this range. |
| 1307 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1308 | |
| 1309 | uint64_t First = cast<ConstantInt>(FrontCase.first)->getSExtValue(); |
| 1310 | uint64_t Last = cast<ConstantInt>(BackCase.first)->getSExtValue(); |
| 1311 | |
| 1312 | if ((!TLI.isOperationLegal(ISD::BR_JT, MVT::Other) && |
| 1313 | !TLI.isOperationLegal(ISD::BRIND, MVT::Other)) || |
| 1314 | Size <= 5) |
| 1315 | return false; |
| 1316 | |
| 1317 | double Density = (double)Size / (double)((Last - First) + 1ULL); |
| 1318 | if (Density < 0.3125) |
| 1319 | return false; |
| 1320 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1321 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1322 | // inserting any additional MBBs necessary to represent the switch. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1323 | MachineFunction *CurMF = CurMBB->getParent(); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1324 | |
| 1325 | // Figure out which block is immediately after the current one. |
| 1326 | MachineBasicBlock *NextBlock = 0; |
| 1327 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1328 | |
| 1329 | if (++BBI != CurMBB->getParent()->end()) |
| 1330 | NextBlock = BBI; |
| 1331 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1332 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1333 | |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1334 | // Create a new basic block to hold the code for loading the address |
| 1335 | // of the jump table, and jumping to it. Update successor information; |
| 1336 | // we will either branch to the default case for the switch, or the jump |
| 1337 | // table. |
| 1338 | MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB); |
| 1339 | CurMF->getBasicBlockList().insert(BBI, JumpTableBB); |
| 1340 | CR.CaseBB->addSuccessor(Default); |
| 1341 | CR.CaseBB->addSuccessor(JumpTableBB); |
| 1342 | |
| 1343 | // Build a vector of destination BBs, corresponding to each target |
| 1344 | // of the jump table. If the value of the jump table slot corresponds to |
| 1345 | // a case statement, push the case's BB onto the vector, otherwise, push |
| 1346 | // the default BB. |
| 1347 | std::vector<MachineBasicBlock*> DestBBs; |
| 1348 | int64_t TEI = First; |
| 1349 | for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) |
| 1350 | if (cast<ConstantInt>(I->first)->getSExtValue() == TEI) { |
| 1351 | DestBBs.push_back(I->second); |
| 1352 | ++I; |
| 1353 | } else { |
| 1354 | DestBBs.push_back(Default); |
| 1355 | } |
| 1356 | |
| 1357 | // Update successor info. Add one edge to each unique successor. |
| 1358 | // Vector bool would be better, but vector<bool> is really slow. |
| 1359 | std::vector<unsigned char> SuccsHandled; |
| 1360 | SuccsHandled.resize(CR.CaseBB->getParent()->getNumBlockIDs()); |
| 1361 | |
| 1362 | for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(), |
| 1363 | E = DestBBs.end(); I != E; ++I) { |
| 1364 | if (!SuccsHandled[(*I)->getNumber()]) { |
| 1365 | SuccsHandled[(*I)->getNumber()] = true; |
| 1366 | JumpTableBB->addSuccessor(*I); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // Create a jump table index for this jump table, or return an existing |
| 1371 | // one. |
| 1372 | unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs); |
| 1373 | |
| 1374 | // Set the jump table information so that we can codegen it as a second |
| 1375 | // MachineBasicBlock |
| 1376 | SelectionDAGISel::JumpTable JT(-1UL, JTI, JumpTableBB, Default); |
| 1377 | SelectionDAGISel::JumpTableHeader JTH(First, Last, SV, CR.CaseBB, |
| 1378 | (CR.CaseBB == CurMBB)); |
| 1379 | if (CR.CaseBB == CurMBB) |
| 1380 | visitJumpTableHeader(JT, JTH); |
| 1381 | |
| 1382 | JTCases.push_back(SelectionDAGISel::JumpTableBlock(JTH, JT)); |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1383 | |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1384 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /// handleBTSplitSwitchCase - emit comparison and split binary search tree into |
| 1388 | /// 2 subtrees. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1389 | bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR, |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1390 | CaseRecVector& WorkList, |
| 1391 | Value* SV, |
| 1392 | MachineBasicBlock* Default) { |
| 1393 | // Get the MachineFunction which holds the current MBB. This is used when |
| 1394 | // inserting any additional MBBs necessary to represent the switch. |
| 1395 | MachineFunction *CurMF = CurMBB->getParent(); |
| 1396 | |
| 1397 | // Figure out which block is immediately after the current one. |
| 1398 | MachineBasicBlock *NextBlock = 0; |
| 1399 | MachineFunction::iterator BBI = CR.CaseBB; |
| 1400 | |
| 1401 | if (++BBI != CurMBB->getParent()->end()) |
| 1402 | NextBlock = BBI; |
| 1403 | |
| 1404 | Case& FrontCase = *CR.Range.first; |
| 1405 | Case& BackCase = *(CR.Range.second-1); |
| 1406 | const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock(); |
| 1407 | |
| 1408 | // Size is the number of Cases represented by this range. |
| 1409 | unsigned Size = CR.Range.second - CR.Range.first; |
| 1410 | |
| 1411 | uint64_t First = cast<ConstantInt>(FrontCase.first)->getSExtValue(); |
| 1412 | uint64_t Last = cast<ConstantInt>(BackCase.first)->getSExtValue(); |
| 1413 | double Density = 0; |
| 1414 | CaseItr Pivot; |
| 1415 | |
| 1416 | // Select optimal pivot, maximizing sum density of LHS and RHS. This will |
| 1417 | // (heuristically) allow us to emit JumpTable's later. |
| 1418 | unsigned LSize = 1; |
| 1419 | unsigned RSize = Size-1; |
| 1420 | for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second; |
| 1421 | J!=E; ++I, ++J, ++LSize, --RSize) { |
| 1422 | uint64_t LEnd = cast<ConstantInt>(I->first)->getSExtValue(); |
| 1423 | uint64_t RBegin = cast<ConstantInt>(J->first)->getSExtValue(); |
| 1424 | double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL); |
| 1425 | double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL); |
| 1426 | if (Density < (LDensity + RDensity)) { |
| 1427 | Pivot = J; |
| 1428 | Density = LDensity + RDensity; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | CaseRange LHSR(CR.Range.first, Pivot); |
| 1433 | CaseRange RHSR(Pivot, CR.Range.second); |
| 1434 | Constant *C = Pivot->first; |
| 1435 | MachineBasicBlock *FalseBB = 0, *TrueBB = 0; |
| 1436 | |
| 1437 | // We know that we branch to the LHS if the Value being switched on is |
| 1438 | // less than the Pivot value, C. We use this to optimize our binary |
| 1439 | // tree a bit, by recognizing that if SV is greater than or equal to the |
| 1440 | // LHS's Case Value, and that Case Value is exactly one less than the |
| 1441 | // Pivot's Value, then we can branch directly to the LHS's Target, |
| 1442 | // rather than creating a leaf node for it. |
| 1443 | if ((LHSR.second - LHSR.first) == 1 && |
| 1444 | LHSR.first->first == CR.GE && |
| 1445 | cast<ConstantInt>(C)->getZExtValue() == |
| 1446 | (cast<ConstantInt>(CR.GE)->getZExtValue() + 1ULL)) { |
| 1447 | TrueBB = LHSR.first->second; |
| 1448 | } else { |
| 1449 | TrueBB = new MachineBasicBlock(LLVMBB); |
| 1450 | CurMF->getBasicBlockList().insert(BBI, TrueBB); |
| 1451 | WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR)); |
| 1452 | } |
| 1453 | |
| 1454 | // Similar to the optimization above, if the Value being switched on is |
| 1455 | // known to be less than the Constant CR.LT, and the current Case Value |
| 1456 | // is CR.LT - 1, then we can branch directly to the target block for |
| 1457 | // the current Case Value, rather than emitting a RHS leaf node for it. |
| 1458 | if ((RHSR.second - RHSR.first) == 1 && CR.LT && |
| 1459 | cast<ConstantInt>(RHSR.first->first)->getZExtValue() == |
| 1460 | (cast<ConstantInt>(CR.LT)->getZExtValue() - 1ULL)) { |
| 1461 | FalseBB = RHSR.first->second; |
| 1462 | } else { |
| 1463 | FalseBB = new MachineBasicBlock(LLVMBB); |
| 1464 | CurMF->getBasicBlockList().insert(BBI, FalseBB); |
| 1465 | WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR)); |
| 1466 | } |
| 1467 | |
| 1468 | // Create a CaseBlock record representing a conditional branch to |
| 1469 | // the LHS node if the value being switched on SV is less than C. |
| 1470 | // Otherwise, branch to LHS. |
| 1471 | SelectionDAGISel::CaseBlock CB(ISD::SETLT, SV, C, TrueBB, FalseBB, |
| 1472 | CR.CaseBB); |
| 1473 | |
| 1474 | if (CR.CaseBB == CurMBB) |
| 1475 | visitSwitchCase(CB); |
| 1476 | else |
| 1477 | SwitchCases.push_back(CB); |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1478 | |
| 1479 | return true; |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1482 | void SelectionDAGLowering::visitSwitch(SwitchInst &I) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1483 | // Figure out which block is immediately after the current one. |
| 1484 | MachineBasicBlock *NextBlock = 0; |
| 1485 | MachineFunction::iterator BBI = CurMBB; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1487 | MachineBasicBlock *Default = FuncInfo.MBBMap[I.getDefaultDest()]; |
| 1488 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1489 | // If there is only the default destination, branch to it if it is not the |
| 1490 | // next basic block. Otherwise, just fall through. |
| 1491 | if (I.getNumOperands() == 2) { |
| 1492 | // Update machine-CFG edges. |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1493 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1494 | // If this is not a fall-through branch, emit the branch. |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1495 | if (Default != NextBlock) |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1496 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1497 | DAG.getBasicBlock(Default))); |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | d2c1d22 | 2006-10-22 21:36:53 +0000 | [diff] [blame] | 1499 | CurMBB->addSuccessor(Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1500 | return; |
| 1501 | } |
| 1502 | |
| 1503 | // If there are any non-default case statements, create a vector of Cases |
| 1504 | // representing each one, and sort the vector so that we can efficiently |
| 1505 | // create a binary search tree from them. |
| 1506 | std::vector<Case> Cases; |
Bill Wendling | c70ddad | 2006-10-19 21:46:38 +0000 | [diff] [blame] | 1507 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1508 | for (unsigned i = 1; i < I.getNumSuccessors(); ++i) { |
| 1509 | MachineBasicBlock *SMBB = FuncInfo.MBBMap[I.getSuccessor(i)]; |
| 1510 | Cases.push_back(Case(I.getSuccessorValue(i), SMBB)); |
| 1511 | } |
| 1512 | std::sort(Cases.begin(), Cases.end(), CaseCmp()); |
| 1513 | |
| 1514 | // Get the Value to be switched on and default basic blocks, which will be |
| 1515 | // inserted into CaseBlock records, representing basic blocks in the binary |
| 1516 | // search tree. |
| 1517 | Value *SV = I.getOperand(0); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1518 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1519 | // Push the initial CaseRec onto the worklist |
Anton Korobeynikov | b17b08d | 2007-03-27 11:29:11 +0000 | [diff] [blame] | 1520 | CaseRecVector WorkList; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1521 | WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end()))); |
| 1522 | |
| 1523 | while (!WorkList.empty()) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1524 | // Grab a record representing a case range to process off the worklist |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1525 | CaseRec CR = WorkList.back(); |
| 1526 | WorkList.pop_back(); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1527 | |
| 1528 | // If the range has few cases (two or less) emit a series of specific |
| 1529 | // tests. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1530 | if (handleSmallSwitchRange(CR, WorkList, SV, Default)) |
| 1531 | continue; |
| 1532 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1533 | // If the switch has more than 5 blocks, and at least 31.25% dense, and the |
| 1534 | // target supports indirect branches, then emit a jump table rather than |
| 1535 | // lowering the switch to a binary tree of conditional branches. |
Anton Korobeynikov | dd43321 | 2007-03-27 12:05:48 +0000 | [diff] [blame^] | 1536 | if (handleJTSwitchCase(CR, WorkList, SV, Default)) |
| 1537 | continue; |
| 1538 | |
| 1539 | // Emit binary tree. We need to pick a pivot, and push left and right ranges |
| 1540 | // onto the worklist. Leafs are handled via handleSmallSwitchRange() call. |
| 1541 | handleBTSplitSwitchCase(CR, WorkList, SV, Default); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 1546 | void SelectionDAGLowering::visitSub(User &I) { |
| 1547 | // -0.0 - X --> fneg |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1548 | const Type *Ty = I.getType(); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1549 | if (isa<VectorType>(Ty)) { |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1550 | visitVectorBinary(I, ISD::VSUB); |
| 1551 | } else if (Ty->isFloatingPoint()) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1552 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 1553 | if (CFP->isExactlyValue(-0.0)) { |
| 1554 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 1555 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 1556 | return; |
| 1557 | } |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1558 | visitScalarBinary(I, ISD::FSUB); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1559 | } else |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1560 | visitScalarBinary(I, ISD::SUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1563 | void SelectionDAGLowering::visitScalarBinary(User &I, unsigned OpCode) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1564 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1565 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1566 | |
| 1567 | setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2)); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1570 | void |
| 1571 | SelectionDAGLowering::visitVectorBinary(User &I, unsigned OpCode) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1572 | assert(isa<VectorType>(I.getType())); |
| 1573 | const VectorType *Ty = cast<VectorType>(I.getType()); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1574 | SDOperand Typ = DAG.getValueType(TLI.getValueType(Ty->getElementType())); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1575 | |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1576 | setValue(&I, DAG.getNode(OpCode, MVT::Vector, |
| 1577 | getValue(I.getOperand(0)), |
| 1578 | getValue(I.getOperand(1)), |
| 1579 | DAG.getConstant(Ty->getNumElements(), MVT::i32), |
| 1580 | Typ)); |
| 1581 | } |
| 1582 | |
| 1583 | void SelectionDAGLowering::visitEitherBinary(User &I, unsigned ScalarOp, |
| 1584 | unsigned VectorOp) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1585 | if (isa<VectorType>(I.getType())) |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1586 | visitVectorBinary(I, VectorOp); |
| 1587 | else |
| 1588 | visitScalarBinary(I, ScalarOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 1589 | } |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 1590 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 1591 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 1592 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1593 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 1594 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1595 | if (TLI.getShiftAmountTy() < Op2.getValueType()) |
| 1596 | Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2); |
| 1597 | else if (TLI.getShiftAmountTy() > Op2.getValueType()) |
| 1598 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1600 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 1601 | } |
| 1602 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1603 | void SelectionDAGLowering::visitICmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1604 | ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; |
| 1605 | if (ICmpInst *IC = dyn_cast<ICmpInst>(&I)) |
| 1606 | predicate = IC->getPredicate(); |
| 1607 | else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) |
| 1608 | predicate = ICmpInst::Predicate(IC->getPredicate()); |
| 1609 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1610 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1611 | ISD::CondCode Opcode; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1612 | switch (predicate) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1613 | case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break; |
| 1614 | case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break; |
| 1615 | case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break; |
| 1616 | case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break; |
| 1617 | case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break; |
| 1618 | case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break; |
| 1619 | case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break; |
| 1620 | case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break; |
| 1621 | case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break; |
| 1622 | case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break; |
| 1623 | default: |
| 1624 | assert(!"Invalid ICmp predicate value"); |
| 1625 | Opcode = ISD::SETEQ; |
| 1626 | break; |
| 1627 | } |
| 1628 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
| 1629 | } |
| 1630 | |
| 1631 | void SelectionDAGLowering::visitFCmp(User &I) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1632 | FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; |
| 1633 | if (FCmpInst *FC = dyn_cast<FCmpInst>(&I)) |
| 1634 | predicate = FC->getPredicate(); |
| 1635 | else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) |
| 1636 | predicate = FCmpInst::Predicate(FC->getPredicate()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1637 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 1638 | SDOperand Op2 = getValue(I.getOperand(1)); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1639 | ISD::CondCode Condition, FOC, FPC; |
| 1640 | switch (predicate) { |
| 1641 | case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; |
| 1642 | case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; |
| 1643 | case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; |
| 1644 | case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; |
| 1645 | case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; |
| 1646 | case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; |
| 1647 | case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; |
| 1648 | case FCmpInst::FCMP_ORD: FOC = ISD::SETEQ; FPC = ISD::SETO; break; |
| 1649 | case FCmpInst::FCMP_UNO: FOC = ISD::SETNE; FPC = ISD::SETUO; break; |
| 1650 | case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; |
| 1651 | case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; |
| 1652 | case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; |
| 1653 | case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; |
| 1654 | case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; |
| 1655 | case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; |
| 1656 | case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; |
| 1657 | default: |
| 1658 | assert(!"Invalid FCmp predicate value"); |
| 1659 | FOC = FPC = ISD::SETFALSE; |
| 1660 | break; |
| 1661 | } |
| 1662 | if (FiniteOnlyFPMath()) |
| 1663 | Condition = FOC; |
| 1664 | else |
| 1665 | Condition = FPC; |
| 1666 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | void SelectionDAGLowering::visitSelect(User &I) { |
| 1670 | SDOperand Cond = getValue(I.getOperand(0)); |
| 1671 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 1672 | SDOperand FalseVal = getValue(I.getOperand(2)); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1673 | if (!isa<VectorType>(I.getType())) { |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 1674 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 1675 | TrueVal, FalseVal)); |
| 1676 | } else { |
| 1677 | setValue(&I, DAG.getNode(ISD::VSELECT, MVT::Vector, Cond, TrueVal, FalseVal, |
| 1678 | *(TrueVal.Val->op_end()-2), |
| 1679 | *(TrueVal.Val->op_end()-1))); |
| 1680 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1683 | |
| 1684 | void SelectionDAGLowering::visitTrunc(User &I) { |
| 1685 | // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). |
| 1686 | SDOperand N = getValue(I.getOperand(0)); |
| 1687 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1688 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 1689 | } |
| 1690 | |
| 1691 | void SelectionDAGLowering::visitZExt(User &I) { |
| 1692 | // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 1693 | // ZExt also can't be a cast to bool for same reason. So, nothing much to do |
| 1694 | SDOperand N = getValue(I.getOperand(0)); |
| 1695 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1696 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 1697 | } |
| 1698 | |
| 1699 | void SelectionDAGLowering::visitSExt(User &I) { |
| 1700 | // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). |
| 1701 | // SExt also can't be a cast to bool for same reason. So, nothing much to do |
| 1702 | SDOperand N = getValue(I.getOperand(0)); |
| 1703 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1704 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N)); |
| 1705 | } |
| 1706 | |
| 1707 | void SelectionDAGLowering::visitFPTrunc(User &I) { |
| 1708 | // FPTrunc is never a no-op cast, no need to check |
| 1709 | SDOperand N = getValue(I.getOperand(0)); |
| 1710 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1711 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N)); |
| 1712 | } |
| 1713 | |
| 1714 | void SelectionDAGLowering::visitFPExt(User &I){ |
| 1715 | // FPTrunc is never a no-op cast, no need to check |
| 1716 | SDOperand N = getValue(I.getOperand(0)); |
| 1717 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1718 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N)); |
| 1719 | } |
| 1720 | |
| 1721 | void SelectionDAGLowering::visitFPToUI(User &I) { |
| 1722 | // FPToUI is never a no-op cast, no need to check |
| 1723 | SDOperand N = getValue(I.getOperand(0)); |
| 1724 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1725 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N)); |
| 1726 | } |
| 1727 | |
| 1728 | void SelectionDAGLowering::visitFPToSI(User &I) { |
| 1729 | // FPToSI is never a no-op cast, no need to check |
| 1730 | SDOperand N = getValue(I.getOperand(0)); |
| 1731 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1732 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N)); |
| 1733 | } |
| 1734 | |
| 1735 | void SelectionDAGLowering::visitUIToFP(User &I) { |
| 1736 | // UIToFP is never a no-op cast, no need to check |
| 1737 | SDOperand N = getValue(I.getOperand(0)); |
| 1738 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1739 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N)); |
| 1740 | } |
| 1741 | |
| 1742 | void SelectionDAGLowering::visitSIToFP(User &I){ |
| 1743 | // UIToFP is never a no-op cast, no need to check |
| 1744 | SDOperand N = getValue(I.getOperand(0)); |
| 1745 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1746 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N)); |
| 1747 | } |
| 1748 | |
| 1749 | void SelectionDAGLowering::visitPtrToInt(User &I) { |
| 1750 | // What to do depends on the size of the integer and the size of the pointer. |
| 1751 | // We can either truncate, zero extend, or no-op, accordingly. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1752 | SDOperand N = getValue(I.getOperand(0)); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1753 | MVT::ValueType SrcVT = N.getValueType(); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1754 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1755 | SDOperand Result; |
| 1756 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 1757 | Result = DAG.getNode(ISD::TRUNCATE, DestVT, N); |
| 1758 | else |
| 1759 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 1760 | Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N); |
| 1761 | setValue(&I, Result); |
| 1762 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1763 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1764 | void SelectionDAGLowering::visitIntToPtr(User &I) { |
| 1765 | // What to do depends on the size of the integer and the size of the pointer. |
| 1766 | // We can either truncate, zero extend, or no-op, accordingly. |
| 1767 | SDOperand N = getValue(I.getOperand(0)); |
| 1768 | MVT::ValueType SrcVT = N.getValueType(); |
| 1769 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
| 1770 | if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(SrcVT)) |
| 1771 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N)); |
| 1772 | else |
| 1773 | // Note: ZERO_EXTEND can handle cases where the sizes are equal too |
| 1774 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N)); |
| 1775 | } |
| 1776 | |
| 1777 | void SelectionDAGLowering::visitBitCast(User &I) { |
| 1778 | SDOperand N = getValue(I.getOperand(0)); |
| 1779 | MVT::ValueType DestVT = TLI.getValueType(I.getType()); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1780 | if (DestVT == MVT::Vector) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1781 | // This is a cast to a vector from something else. |
| 1782 | // Get information about the output vector. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1783 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1784 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 1785 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N, |
| 1786 | DAG.getConstant(DestTy->getNumElements(),MVT::i32), |
| 1787 | DAG.getValueType(EltVT))); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1788 | return; |
| 1789 | } |
| 1790 | MVT::ValueType SrcVT = N.getValueType(); |
| 1791 | if (SrcVT == MVT::Vector) { |
| 1792 | // This is a cast from a vctor to something else. |
| 1793 | // Get information about the input vector. |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 1794 | setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1795 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1796 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1797 | |
| 1798 | // BitCast assures us that source and destination are the same size so this |
| 1799 | // is either a BIT_CONVERT or a no-op. |
| 1800 | if (DestVT != N.getValueType()) |
| 1801 | setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types |
| 1802 | else |
| 1803 | setValue(&I, N); // noop cast. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 1806 | void SelectionDAGLowering::visitInsertElement(User &I) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1807 | SDOperand InVec = getValue(I.getOperand(0)); |
| 1808 | SDOperand InVal = getValue(I.getOperand(1)); |
| 1809 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 1810 | getValue(I.getOperand(2))); |
| 1811 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1812 | SDOperand Num = *(InVec.Val->op_end()-2); |
| 1813 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 1814 | setValue(&I, DAG.getNode(ISD::VINSERT_VECTOR_ELT, MVT::Vector, |
| 1815 | InVec, InVal, InIdx, Num, Typ)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Chris Lattner | 2bbd810 | 2006-03-29 00:11:43 +0000 | [diff] [blame] | 1818 | void SelectionDAGLowering::visitExtractElement(User &I) { |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1819 | SDOperand InVec = getValue(I.getOperand(0)); |
| 1820 | SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), |
| 1821 | getValue(I.getOperand(1))); |
| 1822 | SDOperand Typ = *(InVec.Val->op_end()-1); |
| 1823 | setValue(&I, DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, |
| 1824 | TLI.getValueType(I.getType()), InVec, InIdx)); |
| 1825 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1827 | void SelectionDAGLowering::visitShuffleVector(User &I) { |
| 1828 | SDOperand V1 = getValue(I.getOperand(0)); |
| 1829 | SDOperand V2 = getValue(I.getOperand(1)); |
| 1830 | SDOperand Mask = getValue(I.getOperand(2)); |
| 1831 | |
| 1832 | SDOperand Num = *(V1.Val->op_end()-2); |
| 1833 | SDOperand Typ = *(V2.Val->op_end()-1); |
| 1834 | setValue(&I, DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
| 1835 | V1, V2, Mask, Num, Typ)); |
| 1836 | } |
| 1837 | |
| 1838 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1839 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 1840 | SDOperand N = getValue(I.getOperand(0)); |
| 1841 | const Type *Ty = I.getOperand(0)->getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1842 | |
| 1843 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 1844 | OI != E; ++OI) { |
| 1845 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1846 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1847 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1848 | if (Field) { |
| 1849 | // N = N + Offset |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 1850 | uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1851 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 1852 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1853 | } |
| 1854 | Ty = StTy->getElementType(Field); |
| 1855 | } else { |
| 1856 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1858 | // If this is a constant subscript, handle it quickly. |
| 1859 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1860 | if (CI->getZExtValue() == 0) continue; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 1861 | uint64_t Offs = |
Evan Cheng | 0d630d2 | 2007-01-05 01:46:20 +0000 | [diff] [blame] | 1862 | TD->getTypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1863 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 1864 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1865 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1866 | |
| 1867 | // N = N + Idx * ElementSize; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1868 | uint64_t ElementSize = TD->getTypeSize(Ty); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1869 | SDOperand IdxN = getValue(Idx); |
| 1870 | |
| 1871 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 1872 | // it. |
| 1873 | if (IdxN.getValueType() < N.getValueType()) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 1874 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1875 | } else if (IdxN.getValueType() > N.getValueType()) |
| 1876 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 1877 | |
| 1878 | // If this is a multiply by a power of two, turn it into a shl |
| 1879 | // immediately. This is a very common case. |
| 1880 | if (isPowerOf2_64(ElementSize)) { |
| 1881 | unsigned Amt = Log2_64(ElementSize); |
| 1882 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 1883 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 1884 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 1885 | continue; |
| 1886 | } |
| 1887 | |
| 1888 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 1889 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 1890 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | setValue(&I, N); |
| 1894 | } |
| 1895 | |
| 1896 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 1897 | // If this is a fixed sized alloca in the entry block of the function, |
| 1898 | // allocate it statically on the stack. |
| 1899 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 1900 | return; // getValue will auto-populate this. |
| 1901 | |
| 1902 | const Type *Ty = I.getAllocatedType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1903 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 1904 | unsigned Align = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 1905 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 1906 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1907 | |
| 1908 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1909 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1910 | if (IntPtr < AllocSize.getValueType()) |
| 1911 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 1912 | else if (IntPtr > AllocSize.getValueType()) |
| 1913 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1914 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1915 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1916 | getIntPtrConstant(TySize)); |
| 1917 | |
| 1918 | // Handle alignment. If the requested alignment is less than or equal to the |
| 1919 | // stack alignment, ignore it and round the size of the allocation up to the |
| 1920 | // stack alignment size. If the size is greater than the stack alignment, we |
| 1921 | // note this in the DYNAMIC_STACKALLOC node. |
| 1922 | unsigned StackAlign = |
| 1923 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1924 | if (Align <= StackAlign) { |
| 1925 | Align = 0; |
| 1926 | // Add SA-1 to the size. |
| 1927 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 1928 | getIntPtrConstant(StackAlign-1)); |
| 1929 | // Mask out the low bits for alignment purposes. |
| 1930 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 1931 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 1932 | } |
| 1933 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1934 | SDOperand Ops[] = { getRoot(), AllocSize, getIntPtrConstant(Align) }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 1935 | const MVT::ValueType *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(), |
| 1936 | MVT::Other); |
| 1937 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3); |
Chris Lattner | 0da331f | 2007-02-04 01:31:47 +0000 | [diff] [blame] | 1938 | setValue(&I, DSA); |
| 1939 | DAG.setRoot(DSA.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1940 | |
| 1941 | // Inform the Frame Information that we have just allocated a variable-sized |
| 1942 | // object. |
| 1943 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 1944 | } |
| 1945 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1946 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 1947 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1949 | SDOperand Root; |
| 1950 | if (I.isVolatile()) |
| 1951 | Root = getRoot(); |
| 1952 | else { |
| 1953 | // Do not serialize non-volatile loads against each other. |
| 1954 | Root = DAG.getRoot(); |
| 1955 | } |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1956 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1957 | setValue(&I, getLoadFrom(I.getType(), Ptr, I.getOperand(0), |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1958 | Root, I.isVolatile())); |
| 1959 | } |
| 1960 | |
| 1961 | SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1962 | const Value *SV, SDOperand Root, |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1963 | bool isVolatile) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1964 | SDOperand L; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1965 | if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 1966 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1967 | L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr, |
| 1968 | DAG.getSrcValue(SV)); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1969 | } else { |
Evan Cheng | 0b4f80e | 2006-12-20 01:27:29 +0000 | [diff] [blame] | 1970 | L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SV, 0, isVolatile); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1971 | } |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1972 | |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1973 | if (isVolatile) |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1974 | DAG.setRoot(L.getValue(1)); |
| 1975 | else |
| 1976 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1977 | |
| 1978 | return L; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 1983 | Value *SrcV = I.getOperand(0); |
| 1984 | SDOperand Src = getValue(SrcV); |
| 1985 | SDOperand Ptr = getValue(I.getOperand(1)); |
Evan Cheng | 0b4f80e | 2006-12-20 01:27:29 +0000 | [diff] [blame] | 1986 | DAG.setRoot(DAG.getStore(getRoot(), Src, Ptr, I.getOperand(1), 0, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1987 | I.isVolatile())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 1990 | /// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot |
| 1991 | /// access memory and has no other side effects at all. |
| 1992 | static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) { |
| 1993 | #define GET_NO_MEMORY_INTRINSICS |
| 1994 | #include "llvm/Intrinsics.gen" |
| 1995 | #undef GET_NO_MEMORY_INTRINSICS |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 1999 | // IntrinsicOnlyReadsMemory - Return true if the specified intrinsic doesn't |
| 2000 | // have any side-effects or if it only reads memory. |
| 2001 | static bool IntrinsicOnlyReadsMemory(unsigned IntrinsicID) { |
| 2002 | #define GET_SIDE_EFFECT_INFO |
| 2003 | #include "llvm/Intrinsics.gen" |
| 2004 | #undef GET_SIDE_EFFECT_INFO |
| 2005 | return false; |
| 2006 | } |
| 2007 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2008 | /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC |
| 2009 | /// node. |
| 2010 | void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, |
| 2011 | unsigned Intrinsic) { |
Chris Lattner | 7255a54 | 2006-03-24 22:49:42 +0000 | [diff] [blame] | 2012 | bool HasChain = !IntrinsicCannotAccessMemory(Intrinsic); |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 2013 | bool OnlyLoad = HasChain && IntrinsicOnlyReadsMemory(Intrinsic); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2014 | |
| 2015 | // Build the operand list. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2016 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 2017 | if (HasChain) { // If this intrinsic has side-effects, chainify it. |
| 2018 | if (OnlyLoad) { |
| 2019 | // We don't need to serialize loads against other loads. |
| 2020 | Ops.push_back(DAG.getRoot()); |
| 2021 | } else { |
| 2022 | Ops.push_back(getRoot()); |
| 2023 | } |
| 2024 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2025 | |
| 2026 | // Add the intrinsic ID as an integer operand. |
| 2027 | Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy())); |
| 2028 | |
| 2029 | // Add all operands of the call to the operand list. |
| 2030 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 2031 | SDOperand Op = getValue(I.getOperand(i)); |
| 2032 | |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2033 | // If this is a vector type, force it to the right vector type. |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2034 | if (Op.getValueType() == MVT::Vector) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2035 | const VectorType *OpTy = cast<VectorType>(I.getOperand(i)->getType()); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2036 | MVT::ValueType EltVT = TLI.getValueType(OpTy->getElementType()); |
| 2037 | |
| 2038 | MVT::ValueType VVT = MVT::getVectorType(EltVT, OpTy->getNumElements()); |
| 2039 | assert(VVT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2040 | Op = DAG.getNode(ISD::VBIT_CONVERT, VVT, Op); |
| 2041 | } |
| 2042 | |
| 2043 | assert(TLI.isTypeLegal(Op.getValueType()) && |
| 2044 | "Intrinsic uses a non-legal type?"); |
| 2045 | Ops.push_back(Op); |
| 2046 | } |
| 2047 | |
| 2048 | std::vector<MVT::ValueType> VTs; |
| 2049 | if (I.getType() != Type::VoidTy) { |
| 2050 | MVT::ValueType VT = TLI.getValueType(I.getType()); |
| 2051 | if (VT == MVT::Vector) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2052 | const VectorType *DestTy = cast<VectorType>(I.getType()); |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2053 | MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType()); |
| 2054 | |
| 2055 | VT = MVT::getVectorType(EltVT, DestTy->getNumElements()); |
| 2056 | assert(VT != MVT::Other && "Intrinsic uses a non-legal type?"); |
| 2057 | } |
| 2058 | |
| 2059 | assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?"); |
| 2060 | VTs.push_back(VT); |
| 2061 | } |
| 2062 | if (HasChain) |
| 2063 | VTs.push_back(MVT::Other); |
| 2064 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2065 | const MVT::ValueType *VTList = DAG.getNodeValueTypes(VTs); |
| 2066 | |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2067 | // Create the node. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2068 | SDOperand Result; |
| 2069 | if (!HasChain) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2070 | Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(), |
| 2071 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2072 | else if (I.getType() != Type::VoidTy) |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2073 | Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(), |
| 2074 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2075 | else |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2076 | Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(), |
| 2077 | &Ops[0], Ops.size()); |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2078 | |
Chris Lattner | e58a780 | 2006-04-02 03:41:14 +0000 | [diff] [blame] | 2079 | if (HasChain) { |
| 2080 | SDOperand Chain = Result.getValue(Result.Val->getNumValues()-1); |
| 2081 | if (OnlyLoad) |
| 2082 | PendingLoads.push_back(Chain); |
| 2083 | else |
| 2084 | DAG.setRoot(Chain); |
| 2085 | } |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2086 | if (I.getType() != Type::VoidTy) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2087 | if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) { |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2088 | MVT::ValueType EVT = TLI.getValueType(PTy->getElementType()); |
| 2089 | Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result, |
| 2090 | DAG.getConstant(PTy->getNumElements(), MVT::i32), |
| 2091 | DAG.getValueType(EVT)); |
| 2092 | } |
| 2093 | setValue(&I, Result); |
| 2094 | } |
| 2095 | } |
| 2096 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2097 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 2098 | /// we want to emit this as a call to a named external function, return the name |
| 2099 | /// otherwise lower it and return null. |
| 2100 | const char * |
| 2101 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 2102 | switch (Intrinsic) { |
Chris Lattner | 0eade31 | 2006-03-24 02:22:33 +0000 | [diff] [blame] | 2103 | default: |
| 2104 | // By default, turn this into a target intrinsic node. |
| 2105 | visitTargetIntrinsic(I, Intrinsic); |
| 2106 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2107 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 2108 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 2109 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 2110 | case Intrinsic::returnaddress: |
| 2111 | setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(), |
| 2112 | getValue(I.getOperand(1)))); |
| 2113 | return 0; |
| 2114 | case Intrinsic::frameaddress: |
| 2115 | setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(), |
| 2116 | getValue(I.getOperand(1)))); |
| 2117 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2118 | case Intrinsic::setjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 2119 | return "_setjmp"+!TLI.usesUnderscoreSetJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2120 | break; |
| 2121 | case Intrinsic::longjmp: |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 2122 | return "_longjmp"+!TLI.usesUnderscoreLongJmp(); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2123 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 2124 | case Intrinsic::memcpy_i32: |
| 2125 | case Intrinsic::memcpy_i64: |
| 2126 | visitMemIntrinsic(I, ISD::MEMCPY); |
| 2127 | return 0; |
| 2128 | case Intrinsic::memset_i32: |
| 2129 | case Intrinsic::memset_i64: |
| 2130 | visitMemIntrinsic(I, ISD::MEMSET); |
| 2131 | return 0; |
| 2132 | case Intrinsic::memmove_i32: |
| 2133 | case Intrinsic::memmove_i64: |
| 2134 | visitMemIntrinsic(I, ISD::MEMMOVE); |
| 2135 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 2137 | case Intrinsic::dbg_stoppoint: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2138 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2139 | DbgStopPointInst &SPI = cast<DbgStopPointInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2140 | if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2141 | SDOperand Ops[5]; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2142 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2143 | Ops[0] = getRoot(); |
| 2144 | Ops[1] = getValue(SPI.getLineValue()); |
| 2145 | Ops[2] = getValue(SPI.getColumnValue()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2146 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2147 | DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 2148 | assert(DD && "Not a debug information descriptor"); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2149 | CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD); |
| 2150 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2151 | Ops[3] = DAG.getString(CompileUnit->getFileName()); |
| 2152 | Ops[4] = DAG.getString(CompileUnit->getDirectory()); |
Jim Laskey | ce72b17 | 2006-02-11 01:01:30 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2154 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops, 5)); |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 2155 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2156 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2157 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2158 | } |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2159 | case Intrinsic::dbg_region_start: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2160 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2161 | DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2162 | if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) { |
| 2163 | unsigned LabelID = MMI->RecordRegionStart(RSI.getContext()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 2164 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, getRoot(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2165 | DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2168 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2169 | } |
| 2170 | case Intrinsic::dbg_region_end: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2171 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2172 | DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2173 | if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) { |
| 2174 | unsigned LabelID = MMI->RecordRegionEnd(REI.getContext()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 2175 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2176 | getRoot(), DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2179 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2180 | } |
| 2181 | case Intrinsic::dbg_func_start: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2182 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2183 | DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2184 | if (MMI && FSI.getSubprogram() && |
| 2185 | MMI->Verify(FSI.getSubprogram())) { |
| 2186 | unsigned LabelID = MMI->RecordRegionStart(FSI.getSubprogram()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 2187 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2188 | getRoot(), DAG.getConstant(LabelID, MVT::i32))); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 2191 | return 0; |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2192 | } |
| 2193 | case Intrinsic::dbg_declare: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2194 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2195 | DbgDeclareInst &DI = cast<DbgDeclareInst>(I); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2196 | if (MMI && DI.getVariable() && MMI->Verify(DI.getVariable())) { |
Jim Laskey | 0892cee | 2006-03-24 09:50:27 +0000 | [diff] [blame] | 2197 | SDOperand AddressOp = getValue(DI.getAddress()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2198 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AddressOp)) |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 2199 | MMI->RecordVariable(DI.getVariable(), FI->getIndex()); |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2204 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2205 | case Intrinsic::eh_exception: { |
| 2206 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 2207 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2208 | if (MMI) { |
| 2209 | // Add a label to mark the beginning of the landing pad. Deletion of the |
| 2210 | // landing pad can thus be detected via the MachineModuleInfo. |
| 2211 | unsigned LabelID = MMI->addLandingPad(CurMBB); |
| 2212 | DAG.setRoot(DAG.getNode(ISD::LABEL, MVT::Other, DAG.getEntryNode(), |
| 2213 | DAG.getConstant(LabelID, MVT::i32))); |
| 2214 | |
| 2215 | // Mark exception register as live in. |
| 2216 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 2217 | if (Reg) CurMBB->addLiveIn(Reg); |
| 2218 | |
| 2219 | // Insert the EXCEPTIONADDR instruction. |
| 2220 | SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other); |
| 2221 | SDOperand Ops[1]; |
| 2222 | Ops[0] = DAG.getRoot(); |
| 2223 | SDOperand Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1); |
| 2224 | setValue(&I, Op); |
| 2225 | DAG.setRoot(Op.getValue(1)); |
Jim Laskey | 7a1de98 | 2007-02-24 09:45:44 +0000 | [diff] [blame] | 2226 | } else { |
Jim Laskey | 64ce0ca | 2007-02-28 18:37:04 +0000 | [diff] [blame] | 2227 | setValue(&I, DAG.getConstant(0, TLI.getPointerTy())); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2228 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2229 | return 0; |
| 2230 | } |
| 2231 | |
Jim Laskey | 0b4711b | 2007-03-01 20:24:30 +0000 | [diff] [blame] | 2232 | case Intrinsic::eh_selector: |
| 2233 | case Intrinsic::eh_filter:{ |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2234 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
| 2235 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2236 | if (MMI) { |
| 2237 | // Inform the MachineModuleInfo of the personality for this landing pad. |
Jim Laskey | cbfdb7b | 2007-02-22 16:10:05 +0000 | [diff] [blame] | 2238 | ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(2)); |
| 2239 | assert(CE && CE->getOpcode() == Instruction::BitCast && |
| 2240 | isa<Function>(CE->getOperand(0)) && |
| 2241 | "Personality should be a function"); |
| 2242 | MMI->addPersonality(CurMBB, cast<Function>(CE->getOperand(0))); |
Jim Laskey | 0b4711b | 2007-03-01 20:24:30 +0000 | [diff] [blame] | 2243 | if (Intrinsic == Intrinsic::eh_filter) |
| 2244 | MMI->setIsFilterLandingPad(CurMBB); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2245 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2246 | // Gather all the type infos for this landing pad and pass them along to |
| 2247 | // MachineModuleInfo. |
| 2248 | std::vector<GlobalVariable *> TyInfo; |
| 2249 | for (unsigned i = 3, N = I.getNumOperands(); i < N; ++i) { |
Jim Laskey | cbfdb7b | 2007-02-22 16:10:05 +0000 | [diff] [blame] | 2250 | ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i)); |
| 2251 | if (CE && CE->getOpcode() == Instruction::BitCast && |
| 2252 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 2253 | TyInfo.push_back(cast<GlobalVariable>(CE->getOperand(0))); |
| 2254 | } else { |
| 2255 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i)); |
| 2256 | assert(CI && CI->getZExtValue() == 0 && |
| 2257 | "TypeInfo must be a global variable typeinfo or NULL"); |
| 2258 | TyInfo.push_back(NULL); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2259 | } |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2260 | } |
| 2261 | MMI->addCatchTypeInfo(CurMBB, TyInfo); |
| 2262 | |
| 2263 | // Mark exception selector register as live in. |
| 2264 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 2265 | if (Reg) CurMBB->addLiveIn(Reg); |
| 2266 | |
| 2267 | // Insert the EHSELECTION instruction. |
Jim Laskey | 64ce0ca | 2007-02-28 18:37:04 +0000 | [diff] [blame] | 2268 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2269 | SDOperand Ops[2]; |
| 2270 | Ops[0] = getValue(I.getOperand(1)); |
| 2271 | Ops[1] = getRoot(); |
| 2272 | SDOperand Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2); |
| 2273 | setValue(&I, Op); |
| 2274 | DAG.setRoot(Op.getValue(1)); |
Jim Laskey | 7a1de98 | 2007-02-24 09:45:44 +0000 | [diff] [blame] | 2275 | } else { |
Jim Laskey | 64ce0ca | 2007-02-28 18:37:04 +0000 | [diff] [blame] | 2276 | setValue(&I, DAG.getConstant(0, MVT::i32)); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2277 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | case Intrinsic::eh_typeid_for: { |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2283 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2284 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2285 | if (MMI) { |
| 2286 | // Find the type id for the given typeinfo. |
| 2287 | GlobalVariable *GV = NULL; |
Jim Laskey | cbfdb7b | 2007-02-22 16:10:05 +0000 | [diff] [blame] | 2288 | ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(1)); |
| 2289 | if (CE && CE->getOpcode() == Instruction::BitCast && |
| 2290 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 2291 | GV = cast<GlobalVariable>(CE->getOperand(0)); |
| 2292 | } else { |
| 2293 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 2294 | assert(CI && CI->getZExtValue() == 0 && |
| 2295 | "TypeInfo must be a global variable typeinfo or NULL"); |
| 2296 | GV = NULL; |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | unsigned TypeID = MMI->getTypeIDFor(GV); |
| 2300 | setValue(&I, DAG.getConstant(TypeID, MVT::i32)); |
Jim Laskey | 7a1de98 | 2007-02-24 09:45:44 +0000 | [diff] [blame] | 2301 | } else { |
| 2302 | setValue(&I, DAG.getConstant(0, MVT::i32)); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2303 | } |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 2304 | |
| 2305 | return 0; |
| 2306 | } |
| 2307 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2308 | case Intrinsic::sqrt_f32: |
| 2309 | case Intrinsic::sqrt_f64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2310 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 2311 | getValue(I.getOperand(1)).getValueType(), |
| 2312 | getValue(I.getOperand(1)))); |
| 2313 | return 0; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2314 | case Intrinsic::powi_f32: |
| 2315 | case Intrinsic::powi_f64: |
| 2316 | setValue(&I, DAG.getNode(ISD::FPOWI, |
| 2317 | getValue(I.getOperand(1)).getValueType(), |
| 2318 | getValue(I.getOperand(1)), |
| 2319 | getValue(I.getOperand(2)))); |
| 2320 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2321 | case Intrinsic::pcmarker: { |
| 2322 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 2323 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 2324 | return 0; |
| 2325 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2326 | case Intrinsic::readcyclecounter: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2327 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2328 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, |
| 2329 | DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2, |
| 2330 | &Op, 1); |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2331 | setValue(&I, Tmp); |
| 2332 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2333 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 2334 | } |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2335 | case Intrinsic::bswap_i16: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2336 | case Intrinsic::bswap_i32: |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2337 | case Intrinsic::bswap_i64: |
| 2338 | setValue(&I, DAG.getNode(ISD::BSWAP, |
| 2339 | getValue(I.getOperand(1)).getValueType(), |
| 2340 | getValue(I.getOperand(1)))); |
| 2341 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2342 | case Intrinsic::cttz_i8: |
| 2343 | case Intrinsic::cttz_i16: |
| 2344 | case Intrinsic::cttz_i32: |
| 2345 | case Intrinsic::cttz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2346 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 2347 | getValue(I.getOperand(1)).getValueType(), |
| 2348 | getValue(I.getOperand(1)))); |
| 2349 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2350 | case Intrinsic::ctlz_i8: |
| 2351 | case Intrinsic::ctlz_i16: |
| 2352 | case Intrinsic::ctlz_i32: |
| 2353 | case Intrinsic::ctlz_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2354 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 2355 | getValue(I.getOperand(1)).getValueType(), |
| 2356 | getValue(I.getOperand(1)))); |
| 2357 | return 0; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 2358 | case Intrinsic::ctpop_i8: |
| 2359 | case Intrinsic::ctpop_i16: |
| 2360 | case Intrinsic::ctpop_i32: |
| 2361 | case Intrinsic::ctpop_i64: |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2362 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 2363 | getValue(I.getOperand(1)).getValueType(), |
| 2364 | getValue(I.getOperand(1)))); |
| 2365 | return 0; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2366 | case Intrinsic::stacksave: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2367 | SDOperand Op = getRoot(); |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 2368 | SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, |
| 2369 | DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2370 | setValue(&I, Tmp); |
| 2371 | DAG.setRoot(Tmp.getValue(1)); |
| 2372 | return 0; |
| 2373 | } |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 2374 | case Intrinsic::stackrestore: { |
| 2375 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 2376 | DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2377 | return 0; |
Chris Lattner | 39a17dd | 2006-01-23 05:22:07 +0000 | [diff] [blame] | 2378 | } |
Chris Lattner | ac22c83 | 2005-12-12 22:51:16 +0000 | [diff] [blame] | 2379 | case Intrinsic::prefetch: |
| 2380 | // FIXME: Currently discarding prefetches. |
| 2381 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 2386 | void SelectionDAGLowering::LowerCallTo(Instruction &I, |
| 2387 | const Type *CalledValueTy, |
| 2388 | unsigned CallingConv, |
| 2389 | bool IsTailCall, |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2390 | SDOperand Callee, unsigned OpIdx) { |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 2391 | const PointerType *PT = cast<PointerType>(CalledValueTy); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2392 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 2393 | |
| 2394 | TargetLowering::ArgListTy Args; |
| 2395 | TargetLowering::ArgListEntry Entry; |
| 2396 | Args.reserve(I.getNumOperands()); |
| 2397 | for (unsigned i = OpIdx, e = I.getNumOperands(); i != e; ++i) { |
| 2398 | Value *Arg = I.getOperand(i); |
| 2399 | SDOperand ArgNode = getValue(Arg); |
| 2400 | Entry.Node = ArgNode; Entry.Ty = Arg->getType(); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2401 | Entry.isSExt = FTy->paramHasAttr(i, FunctionType::SExtAttribute); |
| 2402 | Entry.isZExt = FTy->paramHasAttr(i, FunctionType::ZExtAttribute); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2403 | Entry.isInReg = FTy->paramHasAttr(i, FunctionType::InRegAttribute); |
| 2404 | Entry.isSRet = FTy->paramHasAttr(i, FunctionType::StructRetAttribute); |
| 2405 | Args.push_back(Entry); |
| 2406 | } |
| 2407 | |
| 2408 | std::pair<SDOperand,SDOperand> Result = |
| 2409 | TLI.LowerCallTo(getRoot(), I.getType(), |
| 2410 | FTy->paramHasAttr(0,FunctionType::SExtAttribute), |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 2411 | FTy->isVarArg(), CallingConv, IsTailCall, |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2412 | Callee, Args, DAG); |
| 2413 | if (I.getType() != Type::VoidTy) |
| 2414 | setValue(&I, Result.first); |
| 2415 | DAG.setRoot(Result.second); |
| 2416 | } |
| 2417 | |
| 2418 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2419 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 2420 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2421 | if (Function *F = I.getCalledFunction()) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 2422 | if (F->isDeclaration()) |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2423 | if (unsigned IID = F->getIntrinsicID()) { |
| 2424 | RenameFn = visitIntrinsicCall(I, IID); |
| 2425 | if (!RenameFn) |
| 2426 | return; |
| 2427 | } else { // Not an LLVM intrinsic. |
| 2428 | const std::string &Name = F->getName(); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2429 | if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) { |
| 2430 | if (I.getNumOperands() == 3 && // Basic sanity checks. |
| 2431 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 2432 | I.getType() == I.getOperand(1)->getType() && |
| 2433 | I.getType() == I.getOperand(2)->getType()) { |
| 2434 | SDOperand LHS = getValue(I.getOperand(1)); |
| 2435 | SDOperand RHS = getValue(I.getOperand(2)); |
| 2436 | setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(), |
| 2437 | LHS, RHS)); |
| 2438 | return; |
| 2439 | } |
| 2440 | } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 2441 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2442 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 2443 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2444 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 2445 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 2446 | return; |
| 2447 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2448 | } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2449 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2450 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 2451 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2452 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2453 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 2454 | return; |
| 2455 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2456 | } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2457 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 2458 | I.getOperand(1)->getType()->isFloatingPoint() && |
Chris Lattner | 06a248c9 | 2006-02-14 05:39:35 +0000 | [diff] [blame] | 2459 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2460 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2461 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 2462 | return; |
| 2463 | } |
| 2464 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 2465 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2466 | } else if (isa<InlineAsm>(I.getOperand(0))) { |
| 2467 | visitInlineAsm(I); |
| 2468 | return; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 2469 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 2471 | SDOperand Callee; |
| 2472 | if (!RenameFn) |
| 2473 | Callee = getValue(I.getOperand(0)); |
| 2474 | else |
| 2475 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2476 | |
Jim Laskey | 1da20a7 | 2007-02-23 21:45:01 +0000 | [diff] [blame] | 2477 | LowerCallTo(I, I.getCalledValue()->getType(), |
| 2478 | I.getCallingConv(), |
| 2479 | I.isTailCall(), |
| 2480 | Callee, |
| 2481 | 1); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Jim Laskey | 735b6f8 | 2007-02-22 15:38:06 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2485 | SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2486 | SDOperand &Chain, SDOperand &Flag)const{ |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2487 | SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag); |
| 2488 | Chain = Val.getValue(1); |
| 2489 | Flag = Val.getValue(2); |
| 2490 | |
| 2491 | // If the result was expanded, copy from the top part. |
| 2492 | if (Regs.size() > 1) { |
| 2493 | assert(Regs.size() == 2 && |
| 2494 | "Cannot expand to more than 2 elts yet!"); |
| 2495 | SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag); |
Evan Cheng | 693163e | 2006-10-04 22:23:53 +0000 | [diff] [blame] | 2496 | Chain = Hi.getValue(1); |
| 2497 | Flag = Hi.getValue(2); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2498 | if (DAG.getTargetLoweringInfo().isLittleEndian()) |
| 2499 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi); |
| 2500 | else |
| 2501 | return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2502 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2504 | // Otherwise, if the return value was promoted or extended, truncate it to the |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2505 | // appropriate type. |
| 2506 | if (RegVT == ValueVT) |
| 2507 | return Val; |
| 2508 | |
Chris Lattner | 5df99b3 | 2007-03-25 05:00:54 +0000 | [diff] [blame] | 2509 | if (MVT::isVector(RegVT)) { |
| 2510 | assert(ValueVT == MVT::Vector && "Unknown vector conversion!"); |
| 2511 | return DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Val, |
| 2512 | DAG.getConstant(MVT::getVectorNumElements(RegVT), |
| 2513 | MVT::i32), |
| 2514 | DAG.getValueType(MVT::getVectorBaseType(RegVT))); |
| 2515 | } |
| 2516 | |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2517 | if (MVT::isInteger(RegVT)) { |
| 2518 | if (ValueVT < RegVT) |
| 2519 | return DAG.getNode(ISD::TRUNCATE, ValueVT, Val); |
| 2520 | else |
| 2521 | return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val); |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2522 | } |
Chris Lattner | 5df99b3 | 2007-03-25 05:00:54 +0000 | [diff] [blame] | 2523 | |
| 2524 | assert(MVT::isFloatingPoint(RegVT) && MVT::isFloatingPoint(ValueVT)); |
| 2525 | return DAG.getNode(ISD::FP_ROUND, ValueVT, Val); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2528 | /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the |
| 2529 | /// specified value into the registers specified by this object. This uses |
| 2530 | /// Chain/Flag as the input and updates them for the output Chain/Flag. |
| 2531 | void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2532 | SDOperand &Chain, SDOperand &Flag, |
| 2533 | MVT::ValueType PtrVT) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2534 | if (Regs.size() == 1) { |
| 2535 | // If there is a single register and the types differ, this must be |
| 2536 | // a promotion. |
| 2537 | if (RegVT != ValueVT) { |
Chris Lattner | 5df99b3 | 2007-03-25 05:00:54 +0000 | [diff] [blame] | 2538 | if (MVT::isVector(RegVT)) { |
| 2539 | assert(Val.getValueType() == MVT::Vector &&"Not a vector-vector cast?"); |
| 2540 | Val = DAG.getNode(ISD::VBIT_CONVERT, RegVT, Val); |
| 2541 | } else if (MVT::isInteger(RegVT)) { |
Chris Lattner | 0c48fd4 | 2006-06-08 18:27:11 +0000 | [diff] [blame] | 2542 | if (RegVT < ValueVT) |
| 2543 | Val = DAG.getNode(ISD::TRUNCATE, RegVT, Val); |
| 2544 | else |
| 2545 | Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val); |
| 2546 | } else |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2547 | Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val); |
| 2548 | } |
| 2549 | Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag); |
| 2550 | Flag = Chain.getValue(1); |
| 2551 | } else { |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2552 | std::vector<unsigned> R(Regs); |
| 2553 | if (!DAG.getTargetLoweringInfo().isLittleEndian()) |
| 2554 | std::reverse(R.begin(), R.end()); |
| 2555 | |
| 2556 | for (unsigned i = 0, e = R.size(); i != e; ++i) { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2557 | SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 2558 | DAG.getConstant(i, PtrVT)); |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2559 | Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2560 | Flag = Chain.getValue(1); |
| 2561 | } |
| 2562 | } |
| 2563 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2565 | /// AddInlineAsmOperands - Add this value to the specified inlineasm node |
| 2566 | /// operand list. This adds the code marker and includes the number of |
| 2567 | /// values added into it. |
| 2568 | void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG, |
Chris Lattner | 9f6637d | 2006-02-23 20:06:57 +0000 | [diff] [blame] | 2569 | std::vector<SDOperand> &Ops) const { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2570 | Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32)); |
| 2571 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 2572 | Ops.push_back(DAG.getRegister(Regs[i], RegVT)); |
| 2573 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2574 | |
| 2575 | /// isAllocatableRegister - If the specified register is safe to allocate, |
| 2576 | /// i.e. it isn't a stack pointer or some other special register, return the |
| 2577 | /// register class for the register. Otherwise, return null. |
| 2578 | static const TargetRegisterClass * |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2579 | isAllocatableRegister(unsigned Reg, MachineFunction &MF, |
| 2580 | const TargetLowering &TLI, const MRegisterInfo *MRI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2581 | MVT::ValueType FoundVT = MVT::Other; |
| 2582 | const TargetRegisterClass *FoundRC = 0; |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2583 | for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(), |
| 2584 | E = MRI->regclass_end(); RCI != E; ++RCI) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2585 | MVT::ValueType ThisVT = MVT::Other; |
| 2586 | |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2587 | const TargetRegisterClass *RC = *RCI; |
| 2588 | // If none of the the value types for this register class are valid, we |
| 2589 | // can't use it. For example, 64-bit reg classes on 32-bit targets. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2590 | for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end(); |
| 2591 | I != E; ++I) { |
| 2592 | if (TLI.isTypeLegal(*I)) { |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2593 | // If we have already found this register in a different register class, |
| 2594 | // choose the one with the largest VT specified. For example, on |
| 2595 | // PowerPC, we favor f64 register classes over f32. |
| 2596 | if (FoundVT == MVT::Other || |
| 2597 | MVT::getSizeInBits(FoundVT) < MVT::getSizeInBits(*I)) { |
| 2598 | ThisVT = *I; |
| 2599 | break; |
| 2600 | } |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2604 | if (ThisVT == MVT::Other) continue; |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2606 | // NOTE: This isn't ideal. In particular, this might allocate the |
| 2607 | // frame pointer in functions that need it (due to them not being taken |
| 2608 | // out of allocation, because a variable sized allocation hasn't been seen |
| 2609 | // yet). This is a slight code pessimization, but should still work. |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2610 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 2611 | E = RC->allocation_order_end(MF); I != E; ++I) |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2612 | if (*I == Reg) { |
| 2613 | // We found a matching register class. Keep looking at others in case |
| 2614 | // we find one with larger registers that this physreg is also in. |
| 2615 | FoundRC = RC; |
| 2616 | FoundVT = ThisVT; |
| 2617 | break; |
| 2618 | } |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2619 | } |
Chris Lattner | f8814cf | 2006-04-02 00:24:45 +0000 | [diff] [blame] | 2620 | return FoundRC; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | RegsForValue SelectionDAGLowering:: |
| 2624 | GetRegistersForValue(const std::string &ConstrCode, |
| 2625 | MVT::ValueType VT, bool isOutReg, bool isInReg, |
| 2626 | std::set<unsigned> &OutputRegs, |
| 2627 | std::set<unsigned> &InputRegs) { |
| 2628 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 2629 | TLI.getRegForInlineAsmConstraint(ConstrCode, VT); |
| 2630 | std::vector<unsigned> Regs; |
| 2631 | |
| 2632 | unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1; |
| 2633 | MVT::ValueType RegVT; |
| 2634 | MVT::ValueType ValueVT = VT; |
| 2635 | |
Chris Lattner | 2a82160 | 2006-11-02 01:41:49 +0000 | [diff] [blame] | 2636 | // If this is a constraint for a specific physical register, like {r17}, |
| 2637 | // assign it now. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2638 | if (PhysReg.first) { |
| 2639 | if (VT == MVT::Other) |
| 2640 | ValueVT = *PhysReg.second->vt_begin(); |
Chris Lattner | cf752aa | 2006-06-08 18:22:48 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Get the actual register value type. This is important, because the user |
| 2643 | // may have asked for (e.g.) the AX register in i32 type. We need to |
| 2644 | // remember that AX is actually i16 to get the right extension. |
| 2645 | RegVT = *PhysReg.second->vt_begin(); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2646 | |
| 2647 | // This is a explicit reference to a physical register. |
| 2648 | Regs.push_back(PhysReg.first); |
| 2649 | |
| 2650 | // If this is an expanded reference, add the rest of the regs to Regs. |
| 2651 | if (NumRegs != 1) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2652 | TargetRegisterClass::iterator I = PhysReg.second->begin(); |
| 2653 | TargetRegisterClass::iterator E = PhysReg.second->end(); |
| 2654 | for (; *I != PhysReg.first; ++I) |
| 2655 | assert(I != E && "Didn't find reg!"); |
| 2656 | |
| 2657 | // Already added the first reg. |
| 2658 | --NumRegs; ++I; |
| 2659 | for (; NumRegs; --NumRegs, ++I) { |
| 2660 | assert(I != E && "Ran out of registers to allocate!"); |
| 2661 | Regs.push_back(*I); |
| 2662 | } |
| 2663 | } |
| 2664 | return RegsForValue(Regs, RegVT, ValueVT); |
| 2665 | } |
| 2666 | |
Chris Lattner | 2a82160 | 2006-11-02 01:41:49 +0000 | [diff] [blame] | 2667 | // Otherwise, if this was a reference to an LLVM register class, create vregs |
| 2668 | // for this reference. |
| 2669 | std::vector<unsigned> RegClassRegs; |
| 2670 | if (PhysReg.second) { |
| 2671 | // If this is an early clobber or tied register, our regalloc doesn't know |
| 2672 | // how to maintain the constraint. If it isn't, go ahead and create vreg |
| 2673 | // and let the regalloc do the right thing. |
| 2674 | if (!isOutReg || !isInReg) { |
| 2675 | if (VT == MVT::Other) |
| 2676 | ValueVT = *PhysReg.second->vt_begin(); |
| 2677 | RegVT = *PhysReg.second->vt_begin(); |
| 2678 | |
| 2679 | // Create the appropriate number of virtual registers. |
| 2680 | SSARegMap *RegMap = DAG.getMachineFunction().getSSARegMap(); |
| 2681 | for (; NumRegs; --NumRegs) |
| 2682 | Regs.push_back(RegMap->createVirtualRegister(PhysReg.second)); |
| 2683 | |
| 2684 | return RegsForValue(Regs, RegVT, ValueVT); |
| 2685 | } |
| 2686 | |
| 2687 | // Otherwise, we can't allocate it. Let the code below figure out how to |
| 2688 | // maintain these constraints. |
| 2689 | RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end()); |
| 2690 | |
| 2691 | } else { |
| 2692 | // This is a reference to a register class that doesn't directly correspond |
| 2693 | // to an LLVM register class. Allocate NumRegs consecutive, available, |
| 2694 | // registers from the class. |
| 2695 | RegClassRegs = TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT); |
| 2696 | } |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2697 | |
| 2698 | const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo(); |
| 2699 | MachineFunction &MF = *CurMBB->getParent(); |
| 2700 | unsigned NumAllocated = 0; |
| 2701 | for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) { |
| 2702 | unsigned Reg = RegClassRegs[i]; |
| 2703 | // See if this register is available. |
| 2704 | if ((isOutReg && OutputRegs.count(Reg)) || // Already used. |
| 2705 | (isInReg && InputRegs.count(Reg))) { // Already used. |
| 2706 | // Make sure we find consecutive registers. |
| 2707 | NumAllocated = 0; |
| 2708 | continue; |
| 2709 | } |
| 2710 | |
| 2711 | // Check to see if this register is allocatable (i.e. don't give out the |
| 2712 | // stack pointer). |
Chris Lattner | 9b6fb5d | 2006-02-22 23:09:03 +0000 | [diff] [blame] | 2713 | const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2714 | if (!RC) { |
| 2715 | // Make sure we find consecutive registers. |
| 2716 | NumAllocated = 0; |
| 2717 | continue; |
| 2718 | } |
| 2719 | |
| 2720 | // Okay, this register is good, we can use it. |
| 2721 | ++NumAllocated; |
| 2722 | |
| 2723 | // If we allocated enough consecutive |
| 2724 | if (NumAllocated == NumRegs) { |
| 2725 | unsigned RegStart = (i-NumAllocated)+1; |
| 2726 | unsigned RegEnd = i+1; |
| 2727 | // Mark all of the allocated registers used. |
| 2728 | for (unsigned i = RegStart; i != RegEnd; ++i) { |
| 2729 | unsigned Reg = RegClassRegs[i]; |
| 2730 | Regs.push_back(Reg); |
| 2731 | if (isOutReg) OutputRegs.insert(Reg); // Mark reg used. |
| 2732 | if (isInReg) InputRegs.insert(Reg); // Mark reg used. |
| 2733 | } |
| 2734 | |
| 2735 | return RegsForValue(Regs, *RC->vt_begin(), VT); |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | // Otherwise, we couldn't allocate enough registers for this. |
| 2740 | return RegsForValue(); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Chris Lattner | 367f109 | 2007-01-29 23:45:14 +0000 | [diff] [blame] | 2743 | /// getConstraintGenerality - Return an integer indicating how general CT is. |
| 2744 | static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) { |
| 2745 | switch (CT) { |
| 2746 | default: assert(0 && "Unknown constraint type!"); |
| 2747 | case TargetLowering::C_Other: |
| 2748 | case TargetLowering::C_Unknown: |
| 2749 | return 0; |
| 2750 | case TargetLowering::C_Register: |
| 2751 | return 1; |
| 2752 | case TargetLowering::C_RegisterClass: |
| 2753 | return 2; |
| 2754 | case TargetLowering::C_Memory: |
| 2755 | return 3; |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | static std::string GetMostGeneralConstraint(std::vector<std::string> &C, |
| 2760 | const TargetLowering &TLI) { |
| 2761 | assert(!C.empty() && "Must have at least one constraint"); |
| 2762 | if (C.size() == 1) return C[0]; |
| 2763 | |
| 2764 | std::string *Current = &C[0]; |
| 2765 | // If we have multiple constraints, try to pick the most general one ahead |
| 2766 | // of time. This isn't a wonderful solution, but handles common cases. |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 2767 | TargetLowering::ConstraintType Flavor = TLI.getConstraintType(Current[0]); |
Chris Lattner | 367f109 | 2007-01-29 23:45:14 +0000 | [diff] [blame] | 2768 | for (unsigned j = 1, e = C.size(); j != e; ++j) { |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 2769 | TargetLowering::ConstraintType ThisFlavor = TLI.getConstraintType(C[j]); |
Chris Lattner | 367f109 | 2007-01-29 23:45:14 +0000 | [diff] [blame] | 2770 | if (getConstraintGenerality(ThisFlavor) > |
| 2771 | getConstraintGenerality(Flavor)) { |
| 2772 | // This constraint letter is more general than the previous one, |
| 2773 | // use it. |
| 2774 | Flavor = ThisFlavor; |
| 2775 | Current = &C[j]; |
| 2776 | } |
| 2777 | } |
| 2778 | return *Current; |
| 2779 | } |
| 2780 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2782 | /// visitInlineAsm - Handle a call to an InlineAsm object. |
| 2783 | /// |
| 2784 | void SelectionDAGLowering::visitInlineAsm(CallInst &I) { |
| 2785 | InlineAsm *IA = cast<InlineAsm>(I.getOperand(0)); |
| 2786 | |
| 2787 | SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), |
| 2788 | MVT::Other); |
| 2789 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2790 | std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2791 | std::vector<MVT::ValueType> ConstraintVTs; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2792 | |
| 2793 | /// AsmNodeOperands - A list of pairs. The first element is a register, the |
| 2794 | /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set |
| 2795 | /// if it is a def of that register. |
| 2796 | std::vector<SDOperand> AsmNodeOperands; |
| 2797 | AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain |
| 2798 | AsmNodeOperands.push_back(AsmStr); |
| 2799 | |
| 2800 | SDOperand Chain = getRoot(); |
| 2801 | SDOperand Flag; |
| 2802 | |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2803 | // We fully assign registers here at isel time. This is not optimal, but |
| 2804 | // should work. For register classes that correspond to LLVM classes, we |
| 2805 | // could let the LLVM RA do its thing, but we currently don't. Do a prepass |
| 2806 | // over the constraints, collecting fixed registers that we know we can't use. |
| 2807 | std::set<unsigned> OutputRegs, InputRegs; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2808 | unsigned OpNum = 1; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2809 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 367f109 | 2007-01-29 23:45:14 +0000 | [diff] [blame] | 2810 | std::string ConstraintCode = |
| 2811 | GetMostGeneralConstraint(Constraints[i].Codes, TLI); |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2812 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2813 | MVT::ValueType OpVT; |
| 2814 | |
| 2815 | // Compute the value type for each operand and add it to ConstraintVTs. |
| 2816 | switch (Constraints[i].Type) { |
| 2817 | case InlineAsm::isOutput: |
| 2818 | if (!Constraints[i].isIndirectOutput) { |
| 2819 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
| 2820 | OpVT = TLI.getValueType(I.getType()); |
| 2821 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2822 | const Type *OpTy = I.getOperand(OpNum)->getType(); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2823 | OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType()); |
| 2824 | OpNum++; // Consumes a call operand. |
| 2825 | } |
| 2826 | break; |
| 2827 | case InlineAsm::isInput: |
| 2828 | OpVT = TLI.getValueType(I.getOperand(OpNum)->getType()); |
| 2829 | OpNum++; // Consumes a call operand. |
| 2830 | break; |
| 2831 | case InlineAsm::isClobber: |
| 2832 | OpVT = MVT::Other; |
| 2833 | break; |
| 2834 | } |
| 2835 | |
| 2836 | ConstraintVTs.push_back(OpVT); |
| 2837 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2838 | if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0) |
| 2839 | continue; // Not assigned a fixed reg. |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2841 | // Build a list of regs that this operand uses. This always has a single |
| 2842 | // element for promoted/expanded operands. |
| 2843 | RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT, |
| 2844 | false, false, |
| 2845 | OutputRegs, InputRegs); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2846 | |
| 2847 | switch (Constraints[i].Type) { |
| 2848 | case InlineAsm::isOutput: |
| 2849 | // We can't assign any other output to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2850 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2851 | // If this is an early-clobber output, it cannot be assigned to the same |
| 2852 | // value as the input reg. |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2853 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2854 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2855 | break; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2856 | case InlineAsm::isInput: |
| 2857 | // We can't assign any other input to this register. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2858 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2859 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2860 | case InlineAsm::isClobber: |
| 2861 | // Clobbered regs cannot be used as inputs or outputs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2862 | InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
| 2863 | OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end()); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2864 | break; |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2865 | } |
| 2866 | } |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2867 | |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 2868 | // Loop over all of the inputs, copying the operand values into the |
| 2869 | // appropriate registers and processing the output regs. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2870 | RegsForValue RetValRegs; |
| 2871 | std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit; |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2872 | OpNum = 1; |
Chris Lattner | 0f0b7d4 | 2006-02-21 23:12:12 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2874 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 367f109 | 2007-01-29 23:45:14 +0000 | [diff] [blame] | 2875 | std::string ConstraintCode = |
| 2876 | GetMostGeneralConstraint(Constraints[i].Codes, TLI); |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2877 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2878 | switch (Constraints[i].Type) { |
| 2879 | case InlineAsm::isOutput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2880 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 2881 | if (ConstraintCode.size() == 1) // not a physreg name. |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 2882 | CTy = TLI.getConstraintType(ConstraintCode); |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2883 | |
| 2884 | if (CTy == TargetLowering::C_Memory) { |
| 2885 | // Memory output. |
| 2886 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
| 2887 | |
| 2888 | // Check that the operand (the address to store to) isn't a float. |
| 2889 | if (!MVT::isInteger(InOperandVal.getValueType())) |
| 2890 | assert(0 && "MATCH FAIL!"); |
| 2891 | |
| 2892 | if (!Constraints[i].isIndirectOutput) |
| 2893 | assert(0 && "MATCH FAIL!"); |
| 2894 | |
| 2895 | OpNum++; // Consumes a call operand. |
| 2896 | |
| 2897 | // Extend/truncate to the right pointer type if needed. |
| 2898 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 2899 | if (InOperandVal.getValueType() < PtrType) |
| 2900 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 2901 | else if (InOperandVal.getValueType() > PtrType) |
| 2902 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 2903 | |
| 2904 | // Add information to the INLINEASM node to know about this output. |
| 2905 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 2906 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 2907 | AsmNodeOperands.push_back(InOperandVal); |
| 2908 | break; |
| 2909 | } |
| 2910 | |
| 2911 | // Otherwise, this is a register output. |
| 2912 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 2913 | |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2914 | // If this is an early-clobber output, or if there is an input |
| 2915 | // constraint that matches this, we need to reserve the input register |
| 2916 | // so no other inputs allocate to it. |
| 2917 | bool UsesInputRegister = false; |
| 2918 | if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput) |
| 2919 | UsesInputRegister = true; |
| 2920 | |
| 2921 | // Copy the output from the appropriate register. Find a register that |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2922 | // we can use. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2923 | RegsForValue Regs = |
| 2924 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 2925 | true, UsesInputRegister, |
| 2926 | OutputRegs, InputRegs); |
Chris Lattner | d03f158 | 2006-10-31 07:33:13 +0000 | [diff] [blame] | 2927 | if (Regs.Regs.empty()) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2928 | cerr << "Couldn't allocate output reg for contraint '" |
| 2929 | << ConstraintCode << "'!\n"; |
Chris Lattner | d03f158 | 2006-10-31 07:33:13 +0000 | [diff] [blame] | 2930 | exit(1); |
| 2931 | } |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 2932 | |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2933 | if (!Constraints[i].isIndirectOutput) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2934 | assert(RetValRegs.Regs.empty() && |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2935 | "Cannot have multiple output constraints yet!"); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2936 | assert(I.getType() != Type::VoidTy && "Bad inline asm!"); |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 2937 | RetValRegs = Regs; |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2938 | } else { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2939 | IndirectStoresToEmit.push_back(std::make_pair(Regs, |
| 2940 | I.getOperand(OpNum))); |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 2941 | OpNum++; // Consumes a call operand. |
| 2942 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2943 | |
| 2944 | // Add information to the INLINEASM node to know that this register is |
| 2945 | // set. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2946 | Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | } |
| 2949 | case InlineAsm::isInput: { |
Chris Lattner | 2287346 | 2006-02-27 23:45:39 +0000 | [diff] [blame] | 2950 | SDOperand InOperandVal = getValue(I.getOperand(OpNum)); |
Chris Lattner | 4e4b576 | 2006-02-01 18:59:47 +0000 | [diff] [blame] | 2951 | OpNum++; // Consumes a call operand. |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2953 | if (isdigit(ConstraintCode[0])) { // Matching constraint? |
| 2954 | // If this is required to match an output register we have already set, |
| 2955 | // just use its register. |
| 2956 | unsigned OperandNo = atoi(ConstraintCode.c_str()); |
Chris Lattner | 3d81fee | 2006-02-04 02:16:44 +0000 | [diff] [blame] | 2957 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2958 | // Scan until we find the definition we already emitted of this operand. |
| 2959 | // When we find it, create a RegsForValue operand. |
| 2960 | unsigned CurOp = 2; // The first operand. |
| 2961 | for (; OperandNo; --OperandNo) { |
| 2962 | // Advance to the next operand. |
| 2963 | unsigned NumOps = |
| 2964 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
Chris Lattner | a15cf70 | 2006-07-20 19:02:21 +0000 | [diff] [blame] | 2965 | assert(((NumOps & 7) == 2 /*REGDEF*/ || |
| 2966 | (NumOps & 7) == 4 /*MEM*/) && |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2967 | "Skipped past definitions?"); |
| 2968 | CurOp += (NumOps>>3)+1; |
| 2969 | } |
| 2970 | |
| 2971 | unsigned NumOps = |
| 2972 | cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue(); |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 2973 | if ((NumOps & 7) == 2 /*REGDEF*/) { |
| 2974 | // Add NumOps>>3 registers to MatchedRegs. |
| 2975 | RegsForValue MatchedRegs; |
| 2976 | MatchedRegs.ValueVT = InOperandVal.getValueType(); |
| 2977 | MatchedRegs.RegVT = AsmNodeOperands[CurOp+1].getValueType(); |
| 2978 | for (unsigned i = 0, e = NumOps>>3; i != e; ++i) { |
| 2979 | unsigned Reg = |
| 2980 | cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg(); |
| 2981 | MatchedRegs.Regs.push_back(Reg); |
| 2982 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2983 | |
Chris Lattner | 527fae1 | 2007-02-01 01:21:12 +0000 | [diff] [blame] | 2984 | // Use the produced MatchedRegs object to |
| 2985 | MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag, |
| 2986 | TLI.getPointerTy()); |
| 2987 | MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands); |
| 2988 | break; |
| 2989 | } else { |
| 2990 | assert((NumOps & 7) == 4/*MEM*/ && "Unknown matching constraint!"); |
| 2991 | assert(0 && "matching constraints for memory operands unimp"); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 2992 | } |
Chris Lattner | 2223aea | 2006-02-02 00:25:23 +0000 | [diff] [blame] | 2993 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 2994 | |
| 2995 | TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass; |
| 2996 | if (ConstraintCode.size() == 1) // not a physreg name. |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 2997 | CTy = TLI.getConstraintType(ConstraintCode); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 2998 | |
| 2999 | if (CTy == TargetLowering::C_Other) { |
Chris Lattner | 53069fb | 2006-10-31 19:41:18 +0000 | [diff] [blame] | 3000 | InOperandVal = TLI.isOperandValidForConstraint(InOperandVal, |
| 3001 | ConstraintCode[0], DAG); |
| 3002 | if (!InOperandVal.Val) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3003 | cerr << "Invalid operand for inline asm constraint '" |
| 3004 | << ConstraintCode << "'!\n"; |
Chris Lattner | 53069fb | 2006-10-31 19:41:18 +0000 | [diff] [blame] | 3005 | exit(1); |
| 3006 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 3007 | |
| 3008 | // Add information to the INLINEASM node to know about this input. |
| 3009 | unsigned ResOpType = 3 /*IMM*/ | (1 << 3); |
| 3010 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 3011 | AsmNodeOperands.push_back(InOperandVal); |
| 3012 | break; |
| 3013 | } else if (CTy == TargetLowering::C_Memory) { |
| 3014 | // Memory input. |
| 3015 | |
Chris Lattner | 6dfc680 | 2007-03-08 22:29:47 +0000 | [diff] [blame] | 3016 | // If the operand is a float, spill to a constant pool entry to get its |
| 3017 | // address. |
| 3018 | if (ConstantFP *Val = dyn_cast<ConstantFP>(I.getOperand(OpNum-1))) |
| 3019 | InOperandVal = DAG.getConstantPool(Val, TLI.getPointerTy()); |
| 3020 | |
Chris Lattner | b4ddac9 | 2007-03-08 07:07:03 +0000 | [diff] [blame] | 3021 | if (!MVT::isInteger(InOperandVal.getValueType())) { |
Chris Lattner | 6dfc680 | 2007-03-08 22:29:47 +0000 | [diff] [blame] | 3022 | cerr << "Match failed, cannot handle this yet!\n"; |
| 3023 | InOperandVal.Val->dump(); |
Chris Lattner | b4ddac9 | 2007-03-08 07:07:03 +0000 | [diff] [blame] | 3024 | exit(1); |
| 3025 | } |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Extend/truncate to the right pointer type if needed. |
| 3028 | MVT::ValueType PtrType = TLI.getPointerTy(); |
| 3029 | if (InOperandVal.getValueType() < PtrType) |
| 3030 | InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal); |
| 3031 | else if (InOperandVal.getValueType() > PtrType) |
| 3032 | InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal); |
| 3033 | |
| 3034 | // Add information to the INLINEASM node to know about this input. |
| 3035 | unsigned ResOpType = 4/*MEM*/ | (1 << 3); |
| 3036 | AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32)); |
| 3037 | AsmNodeOperands.push_back(InOperandVal); |
| 3038 | break; |
| 3039 | } |
| 3040 | |
| 3041 | assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!"); |
| 3042 | |
| 3043 | // Copy the input into the appropriate registers. |
| 3044 | RegsForValue InRegs = |
| 3045 | GetRegistersForValue(ConstraintCode, ConstraintVTs[i], |
| 3046 | false, true, OutputRegs, InputRegs); |
| 3047 | // FIXME: should be match fail. |
| 3048 | assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!"); |
| 3049 | |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 3050 | InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag, TLI.getPointerTy()); |
Chris Lattner | 87bc3bd | 2006-02-24 01:11:24 +0000 | [diff] [blame] | 3051 | |
| 3052 | InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3053 | break; |
| 3054 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3055 | case InlineAsm::isClobber: { |
| 3056 | RegsForValue ClobberedRegs = |
| 3057 | GetRegistersForValue(ConstraintCode, MVT::Other, false, false, |
| 3058 | OutputRegs, InputRegs); |
| 3059 | // Add the clobbered value to the operand list, so that the register |
| 3060 | // allocator is aware that the physreg got clobbered. |
| 3061 | if (!ClobberedRegs.Regs.empty()) |
| 3062 | ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3063 | break; |
| 3064 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 3065 | } |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3066 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3067 | |
| 3068 | // Finish up input operands. |
| 3069 | AsmNodeOperands[0] = Chain; |
| 3070 | if (Flag.Val) AsmNodeOperands.push_back(Flag); |
| 3071 | |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3072 | Chain = DAG.getNode(ISD::INLINEASM, |
| 3073 | DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3074 | &AsmNodeOperands[0], AsmNodeOperands.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3075 | Flag = Chain.getValue(1); |
| 3076 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3077 | // If this asm returns a register value, copy the result from that register |
| 3078 | // and set it as the value of the call. |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3079 | if (!RetValRegs.Regs.empty()) |
| 3080 | setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3081 | |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3082 | std::vector<std::pair<SDOperand, Value*> > StoresToEmit; |
| 3083 | |
| 3084 | // Process indirect outputs, first output all of the flagged copies out of |
| 3085 | // physregs. |
| 3086 | for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) { |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3087 | RegsForValue &OutRegs = IndirectStoresToEmit[i].first; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3088 | Value *Ptr = IndirectStoresToEmit[i].second; |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3089 | SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag); |
| 3090 | StoresToEmit.push_back(std::make_pair(OutVal, Ptr)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | // Emit the non-flagged stores from the physregs. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3094 | SmallVector<SDOperand, 8> OutChains; |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3095 | for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3096 | OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first, |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3097 | getValue(StoresToEmit[i].second), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3098 | StoresToEmit[i].second, 0)); |
Chris Lattner | 6656dd1 | 2006-01-31 02:03:41 +0000 | [diff] [blame] | 3099 | if (!OutChains.empty()) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3100 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3101 | &OutChains[0], OutChains.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 3102 | DAG.setRoot(Chain); |
| 3103 | } |
| 3104 | |
| 3105 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3106 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 3107 | SDOperand Src = getValue(I.getOperand(0)); |
| 3108 | |
| 3109 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 3110 | |
| 3111 | if (IntPtr < Src.getValueType()) |
| 3112 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 3113 | else if (IntPtr > Src.getValueType()) |
| 3114 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3115 | |
| 3116 | // Scale the source by the type size. |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3117 | uint64_t ElementSize = TD->getTypeSize(I.getType()->getElementType()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3118 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 3119 | Src, getIntPtrConstant(ElementSize)); |
| 3120 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3121 | TargetLowering::ArgListTy Args; |
| 3122 | TargetLowering::ArgListEntry Entry; |
| 3123 | Entry.Node = Src; |
| 3124 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3125 | Args.push_back(Entry); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 3126 | |
| 3127 | std::pair<SDOperand,SDOperand> Result = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3128 | TLI.LowerCallTo(getRoot(), I.getType(), false, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 3129 | DAG.getExternalSymbol("malloc", IntPtr), |
| 3130 | Args, DAG); |
| 3131 | setValue(&I, Result.first); // Pointers always fit in registers |
| 3132 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3136 | TargetLowering::ArgListTy Args; |
| 3137 | TargetLowering::ArgListEntry Entry; |
| 3138 | Entry.Node = getValue(I.getOperand(0)); |
| 3139 | Entry.Ty = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3140 | Args.push_back(Entry); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3141 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 3142 | std::pair<SDOperand,SDOperand> Result = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3143 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 3144 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 3145 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 3148 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 3149 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 3150 | // instructions are special in various ways, which require special support to |
| 3151 | // insert. The specified MachineInstr is created but not inserted into any |
| 3152 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 3153 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 3154 | MachineBasicBlock *MBB) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3155 | cerr << "If a target marks an instruction with " |
| 3156 | << "'usesCustomDAGSchedInserter', it must implement " |
| 3157 | << "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 3158 | abort(); |
| 3159 | return 0; |
| 3160 | } |
| 3161 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3162 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3163 | DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(), |
| 3164 | getValue(I.getOperand(1)), |
| 3165 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3169 | SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(), |
| 3170 | getValue(I.getOperand(0)), |
| 3171 | DAG.getSrcValue(I.getOperand(0))); |
| 3172 | setValue(&I, V); |
| 3173 | DAG.setRoot(V.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3177 | DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(), |
| 3178 | getValue(I.getOperand(1)), |
| 3179 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3183 | DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(), |
| 3184 | getValue(I.getOperand(1)), |
| 3185 | getValue(I.getOperand(2)), |
| 3186 | DAG.getSrcValue(I.getOperand(1)), |
| 3187 | DAG.getSrcValue(I.getOperand(2)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3190 | /// ExpandScalarFormalArgs - Recursively expand the formal_argument node, either |
| 3191 | /// bit_convert it or join a pair of them with a BUILD_PAIR when appropriate. |
| 3192 | static SDOperand ExpandScalarFormalArgs(MVT::ValueType VT, SDNode *Arg, |
| 3193 | unsigned &i, SelectionDAG &DAG, |
| 3194 | TargetLowering &TLI) { |
| 3195 | if (TLI.getTypeAction(VT) != TargetLowering::Expand) |
| 3196 | return SDOperand(Arg, i++); |
| 3197 | |
| 3198 | MVT::ValueType EVT = TLI.getTypeToTransformTo(VT); |
| 3199 | unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT); |
| 3200 | if (NumVals == 1) { |
| 3201 | return DAG.getNode(ISD::BIT_CONVERT, VT, |
| 3202 | ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI)); |
| 3203 | } else if (NumVals == 2) { |
| 3204 | SDOperand Lo = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI); |
| 3205 | SDOperand Hi = ExpandScalarFormalArgs(EVT, Arg, i, DAG, TLI); |
| 3206 | if (!TLI.isLittleEndian()) |
| 3207 | std::swap(Lo, Hi); |
| 3208 | return DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
| 3209 | } else { |
| 3210 | // Value scalarized into many values. Unimp for now. |
| 3211 | assert(0 && "Cannot expand i64 -> i16 yet!"); |
| 3212 | } |
| 3213 | return SDOperand(); |
| 3214 | } |
| 3215 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3216 | /// TargetLowering::LowerArguments - This is the default LowerArguments |
| 3217 | /// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3218 | /// targets are migrated to using FORMAL_ARGUMENTS, this hook should be |
| 3219 | /// integrated into SDISel. |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3220 | std::vector<SDOperand> |
| 3221 | TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3222 | const FunctionType *FTy = F.getFunctionType(); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3223 | // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node. |
| 3224 | std::vector<SDOperand> Ops; |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3225 | Ops.push_back(DAG.getRoot()); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3226 | Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy())); |
| 3227 | Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy())); |
| 3228 | |
| 3229 | // Add one result value for each formal argument. |
| 3230 | std::vector<MVT::ValueType> RetVals; |
Anton Korobeynikov | 6aa279d | 2007-01-28 18:01:49 +0000 | [diff] [blame] | 3231 | unsigned j = 1; |
Anton Korobeynikov | ac2b2cf | 2007-01-28 16:04:40 +0000 | [diff] [blame] | 3232 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 3233 | I != E; ++I, ++j) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3234 | MVT::ValueType VT = getValueType(I->getType()); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3235 | unsigned Flags = ISD::ParamFlags::NoFlagSet; |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3236 | unsigned OriginalAlignment = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 3237 | getTargetData()->getABITypeAlignment(I->getType()); |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3238 | |
Chris Lattner | ddf53e4 | 2007-02-26 02:56:58 +0000 | [diff] [blame] | 3239 | // FIXME: Distinguish between a formal with no [sz]ext attribute from one |
| 3240 | // that is zero extended! |
| 3241 | if (FTy->paramHasAttr(j, FunctionType::ZExtAttribute)) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3242 | Flags &= ~(ISD::ParamFlags::SExt); |
Chris Lattner | ddf53e4 | 2007-02-26 02:56:58 +0000 | [diff] [blame] | 3243 | if (FTy->paramHasAttr(j, FunctionType::SExtAttribute)) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3244 | Flags |= ISD::ParamFlags::SExt; |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3245 | if (FTy->paramHasAttr(j, FunctionType::InRegAttribute)) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3246 | Flags |= ISD::ParamFlags::InReg; |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3247 | if (FTy->paramHasAttr(j, FunctionType::StructRetAttribute)) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3248 | Flags |= ISD::ParamFlags::StructReturn; |
| 3249 | Flags |= (OriginalAlignment << ISD::ParamFlags::OrigAlignmentOffs); |
Chris Lattner | ddf53e4 | 2007-02-26 02:56:58 +0000 | [diff] [blame] | 3250 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3251 | switch (getTypeAction(VT)) { |
| 3252 | default: assert(0 && "Unknown type action!"); |
| 3253 | case Legal: |
| 3254 | RetVals.push_back(VT); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3255 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3256 | break; |
| 3257 | case Promote: |
| 3258 | RetVals.push_back(getTypeToTransformTo(VT)); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3259 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3260 | break; |
| 3261 | case Expand: |
| 3262 | if (VT != MVT::Vector) { |
| 3263 | // If this is a large integer, it needs to be broken up into small |
| 3264 | // integers. Figure out what the destination type is and how many small |
| 3265 | // integers it turns into. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 3266 | MVT::ValueType NVT = getTypeToExpandTo(VT); |
| 3267 | unsigned NumVals = getNumElements(VT); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3268 | for (unsigned i = 0; i != NumVals; ++i) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3269 | RetVals.push_back(NVT); |
Lauro Ramos Venancio | cf8270a | 2007-02-13 18:10:13 +0000 | [diff] [blame] | 3270 | // if it isn't first piece, alignment must be 1 |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3271 | if (i > 0) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3272 | Flags = (Flags & (~ISD::ParamFlags::OrigAlignment)) | |
| 3273 | (1 << ISD::ParamFlags::OrigAlignmentOffs); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3274 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
| 3275 | } |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3276 | } else { |
| 3277 | // Otherwise, this is a vector type. We only support legal vectors |
| 3278 | // right now. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3279 | unsigned NumElems = cast<VectorType>(I->getType())->getNumElements(); |
| 3280 | const Type *EltTy = cast<VectorType>(I->getType())->getElementType(); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 3281 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3282 | // Figure out if there is a Packed type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3283 | // type. If so, convert to the vector type. |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3284 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
| 3285 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
| 3286 | RetVals.push_back(TVT); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3287 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3288 | } else { |
| 3289 | assert(0 && "Don't support illegal by-val vector arguments yet!"); |
| 3290 | } |
| 3291 | } |
| 3292 | break; |
| 3293 | } |
| 3294 | } |
Evan Cheng | 3b0d286 | 2006-04-25 23:03:35 +0000 | [diff] [blame] | 3295 | |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3296 | RetVals.push_back(MVT::Other); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Create the node. |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3299 | SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, |
| 3300 | DAG.getNodeValueTypes(RetVals), RetVals.size(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3301 | &Ops[0], Ops.size()).Val; |
Chris Lattner | 8c0c10c | 2006-05-16 06:45:34 +0000 | [diff] [blame] | 3302 | |
| 3303 | DAG.setRoot(SDOperand(Result, Result->getNumValues()-1)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3304 | |
| 3305 | // Set up the return result vector. |
| 3306 | Ops.clear(); |
| 3307 | unsigned i = 0; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3308 | unsigned Idx = 1; |
| 3309 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; |
| 3310 | ++I, ++Idx) { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3311 | MVT::ValueType VT = getValueType(I->getType()); |
| 3312 | |
| 3313 | switch (getTypeAction(VT)) { |
| 3314 | default: assert(0 && "Unknown type action!"); |
| 3315 | case Legal: |
| 3316 | Ops.push_back(SDOperand(Result, i++)); |
| 3317 | break; |
| 3318 | case Promote: { |
| 3319 | SDOperand Op(Result, i++); |
| 3320 | if (MVT::isInteger(VT)) { |
Chris Lattner | f8e7a21 | 2007-01-04 22:22:37 +0000 | [diff] [blame] | 3321 | if (FTy->paramHasAttr(Idx, FunctionType::SExtAttribute)) |
| 3322 | Op = DAG.getNode(ISD::AssertSext, Op.getValueType(), Op, |
| 3323 | DAG.getValueType(VT)); |
| 3324 | else if (FTy->paramHasAttr(Idx, FunctionType::ZExtAttribute)) |
| 3325 | Op = DAG.getNode(ISD::AssertZext, Op.getValueType(), Op, |
| 3326 | DAG.getValueType(VT)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3327 | Op = DAG.getNode(ISD::TRUNCATE, VT, Op); |
| 3328 | } else { |
| 3329 | assert(MVT::isFloatingPoint(VT) && "Not int or FP?"); |
| 3330 | Op = DAG.getNode(ISD::FP_ROUND, VT, Op); |
| 3331 | } |
| 3332 | Ops.push_back(Op); |
| 3333 | break; |
| 3334 | } |
| 3335 | case Expand: |
| 3336 | if (VT != MVT::Vector) { |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3337 | // If this is a large integer or a floating point node that needs to be |
| 3338 | // expanded, it needs to be reassembled from small integers. Figure out |
| 3339 | // what the source elt type is and how many small integers it is. |
| 3340 | Ops.push_back(ExpandScalarFormalArgs(VT, Result, i, DAG, *this)); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3341 | } else { |
| 3342 | // Otherwise, this is a vector type. We only support legal vectors |
| 3343 | // right now. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3344 | const VectorType *PTy = cast<VectorType>(I->getType()); |
Evan Cheng | 020c41f | 2006-04-28 05:25:15 +0000 | [diff] [blame] | 3345 | unsigned NumElems = PTy->getNumElements(); |
| 3346 | const Type *EltTy = PTy->getElementType(); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 3347 | |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3348 | // Figure out if there is a Packed type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3349 | // type. If so, convert to the vector type. |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3350 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 3351 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
Evan Cheng | 020c41f | 2006-04-28 05:25:15 +0000 | [diff] [blame] | 3352 | SDOperand N = SDOperand(Result, i++); |
| 3353 | // Handle copies from generic vectors to registers. |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 3354 | N = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, N, |
| 3355 | DAG.getConstant(NumElems, MVT::i32), |
| 3356 | DAG.getValueType(getValueType(EltTy))); |
| 3357 | Ops.push_back(N); |
| 3358 | } else { |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3359 | assert(0 && "Don't support illegal by-val vector arguments yet!"); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3360 | abort(); |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | break; |
| 3364 | } |
| 3365 | } |
| 3366 | return Ops; |
| 3367 | } |
| 3368 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3369 | |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3370 | /// ExpandScalarCallArgs - Recursively expand call argument node by |
| 3371 | /// bit_converting it or extract a pair of elements from the larger node. |
| 3372 | static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg, |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3373 | unsigned Flags, |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3374 | SmallVector<SDOperand, 32> &Ops, |
| 3375 | SelectionDAG &DAG, |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3376 | TargetLowering &TLI, |
| 3377 | bool isFirst = true) { |
| 3378 | |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3379 | if (TLI.getTypeAction(VT) != TargetLowering::Expand) { |
Lauro Ramos Venancio | cf8270a | 2007-02-13 18:10:13 +0000 | [diff] [blame] | 3380 | // if it isn't first piece, alignment must be 1 |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3381 | if (!isFirst) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3382 | Flags = (Flags & (~ISD::ParamFlags::OrigAlignment)) | |
| 3383 | (1 << ISD::ParamFlags::OrigAlignmentOffs); |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3384 | Ops.push_back(Arg); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3385 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3386 | return; |
| 3387 | } |
| 3388 | |
| 3389 | MVT::ValueType EVT = TLI.getTypeToTransformTo(VT); |
| 3390 | unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT); |
| 3391 | if (NumVals == 1) { |
| 3392 | Arg = DAG.getNode(ISD::BIT_CONVERT, EVT, Arg); |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3393 | ExpandScalarCallArgs(EVT, Arg, Flags, Ops, DAG, TLI, isFirst); |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3394 | } else if (NumVals == 2) { |
| 3395 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg, |
| 3396 | DAG.getConstant(0, TLI.getPointerTy())); |
| 3397 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg, |
| 3398 | DAG.getConstant(1, TLI.getPointerTy())); |
| 3399 | if (!TLI.isLittleEndian()) |
| 3400 | std::swap(Lo, Hi); |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3401 | ExpandScalarCallArgs(EVT, Lo, Flags, Ops, DAG, TLI, isFirst); |
| 3402 | ExpandScalarCallArgs(EVT, Hi, Flags, Ops, DAG, TLI, false); |
Evan Cheng | b15974a | 2006-12-12 07:27:38 +0000 | [diff] [blame] | 3403 | } else { |
| 3404 | // Value scalarized into many values. Unimp for now. |
| 3405 | assert(0 && "Cannot expand i64 -> i16 yet!"); |
| 3406 | } |
| 3407 | } |
| 3408 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3409 | /// TargetLowering::LowerCallTo - This is the default LowerCallTo |
| 3410 | /// implementation, which just inserts an ISD::CALL node, which is later custom |
| 3411 | /// lowered by the target to something concrete. FIXME: When all targets are |
| 3412 | /// migrated to using ISD::CALL, this hook should be integrated into SDISel. |
| 3413 | std::pair<SDOperand, SDOperand> |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3414 | TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 3415 | bool RetTyIsSigned, bool isVarArg, |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3416 | unsigned CallingConv, bool isTailCall, |
| 3417 | SDOperand Callee, |
| 3418 | ArgListTy &Args, SelectionDAG &DAG) { |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3419 | SmallVector<SDOperand, 32> Ops; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3420 | Ops.push_back(Chain); // Op#0 - Chain |
| 3421 | Ops.push_back(DAG.getConstant(CallingConv, getPointerTy())); // Op#1 - CC |
| 3422 | Ops.push_back(DAG.getConstant(isVarArg, getPointerTy())); // Op#2 - VarArg |
| 3423 | Ops.push_back(DAG.getConstant(isTailCall, getPointerTy())); // Op#3 - Tail |
| 3424 | Ops.push_back(Callee); |
| 3425 | |
| 3426 | // Handle all of the outgoing arguments. |
| 3427 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3428 | MVT::ValueType VT = getValueType(Args[i].Ty); |
| 3429 | SDOperand Op = Args[i].Node; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3430 | unsigned Flags = ISD::ParamFlags::NoFlagSet; |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3431 | unsigned OriginalAlignment = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 3432 | getTargetData()->getABITypeAlignment(Args[i].Ty); |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3433 | |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3434 | if (Args[i].isSExt) |
| 3435 | Flags |= ISD::ParamFlags::SExt; |
| 3436 | if (Args[i].isZExt) |
| 3437 | Flags |= ISD::ParamFlags::ZExt; |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3438 | if (Args[i].isInReg) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3439 | Flags |= ISD::ParamFlags::InReg; |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3440 | if (Args[i].isSRet) |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3441 | Flags |= ISD::ParamFlags::StructReturn; |
| 3442 | Flags |= OriginalAlignment << ISD::ParamFlags::OrigAlignmentOffs; |
Anton Korobeynikov | 0db79d8 | 2007-03-06 06:10:33 +0000 | [diff] [blame] | 3443 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3444 | switch (getTypeAction(VT)) { |
| 3445 | default: assert(0 && "Unknown type action!"); |
Lauro Ramos Venancio | 7aa47b6 | 2007-02-13 13:50:08 +0000 | [diff] [blame] | 3446 | case Legal: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3447 | Ops.push_back(Op); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3448 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | case Promote: |
| 3451 | if (MVT::isInteger(VT)) { |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3452 | unsigned ExtOp; |
| 3453 | if (Args[i].isSExt) |
| 3454 | ExtOp = ISD::SIGN_EXTEND; |
| 3455 | else if (Args[i].isZExt) |
| 3456 | ExtOp = ISD::ZERO_EXTEND; |
| 3457 | else |
| 3458 | ExtOp = ISD::ANY_EXTEND; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3459 | Op = DAG.getNode(ExtOp, getTypeToTransformTo(VT), Op); |
| 3460 | } else { |
| 3461 | assert(MVT::isFloatingPoint(VT) && "Not int or FP?"); |
| 3462 | Op = DAG.getNode(ISD::FP_EXTEND, getTypeToTransformTo(VT), Op); |
| 3463 | } |
| 3464 | Ops.push_back(Op); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3465 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3466 | break; |
| 3467 | case Expand: |
| 3468 | if (VT != MVT::Vector) { |
| 3469 | // If this is a large integer, it needs to be broken down into small |
| 3470 | // integers. Figure out what the source elt type is and how many small |
| 3471 | // integers it is. |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3472 | ExpandScalarCallArgs(VT, Op, Flags, Ops, DAG, *this); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3473 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3474 | // Otherwise, this is a vector type. We only support legal vectors |
| 3475 | // right now. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3476 | const VectorType *PTy = cast<VectorType>(Args[i].Ty); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3477 | unsigned NumElems = PTy->getNumElements(); |
| 3478 | const Type *EltTy = PTy->getElementType(); |
| 3479 | |
| 3480 | // Figure out if there is a Packed type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3481 | // type. If so, convert to the vector type. |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3482 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
Chris Lattner | 1b8daae | 2006-05-17 20:43:21 +0000 | [diff] [blame] | 3483 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3484 | // Insert a VBIT_CONVERT of the MVT::Vector type to the vector type. |
Chris Lattner | 1b8daae | 2006-05-17 20:43:21 +0000 | [diff] [blame] | 3485 | Op = DAG.getNode(ISD::VBIT_CONVERT, TVT, Op); |
| 3486 | Ops.push_back(Op); |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 3487 | Ops.push_back(DAG.getConstant(Flags, MVT::i32)); |
Chris Lattner | 1b8daae | 2006-05-17 20:43:21 +0000 | [diff] [blame] | 3488 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3489 | assert(0 && "Don't support illegal by-val vector call args yet!"); |
| 3490 | abort(); |
| 3491 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3492 | } |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | // Figure out the result value types. |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3498 | SmallVector<MVT::ValueType, 4> RetTys; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3499 | |
| 3500 | if (RetTy != Type::VoidTy) { |
| 3501 | MVT::ValueType VT = getValueType(RetTy); |
| 3502 | switch (getTypeAction(VT)) { |
| 3503 | default: assert(0 && "Unknown type action!"); |
| 3504 | case Legal: |
| 3505 | RetTys.push_back(VT); |
| 3506 | break; |
| 3507 | case Promote: |
| 3508 | RetTys.push_back(getTypeToTransformTo(VT)); |
| 3509 | break; |
| 3510 | case Expand: |
| 3511 | if (VT != MVT::Vector) { |
| 3512 | // If this is a large integer, it needs to be reassembled from small |
| 3513 | // integers. Figure out what the source elt type is and how many small |
| 3514 | // integers it is. |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 3515 | MVT::ValueType NVT = getTypeToExpandTo(VT); |
| 3516 | unsigned NumVals = getNumElements(VT); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3517 | for (unsigned i = 0; i != NumVals; ++i) |
| 3518 | RetTys.push_back(NVT); |
| 3519 | } else { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3520 | // Otherwise, this is a vector type. We only support legal vectors |
| 3521 | // right now. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3522 | const VectorType *PTy = cast<VectorType>(RetTy); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3523 | unsigned NumElems = PTy->getNumElements(); |
| 3524 | const Type *EltTy = PTy->getElementType(); |
| 3525 | |
| 3526 | // Figure out if there is a Packed type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3527 | // type. If so, convert to the vector type. |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3528 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems); |
| 3529 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
| 3530 | RetTys.push_back(TVT); |
| 3531 | } else { |
| 3532 | assert(0 && "Don't support illegal by-val vector call results yet!"); |
| 3533 | abort(); |
| 3534 | } |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3535 | } |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | RetTys.push_back(MVT::Other); // Always has a chain. |
| 3540 | |
| 3541 | // Finally, create the CALL node. |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 3542 | SDOperand Res = DAG.getNode(ISD::CALL, |
| 3543 | DAG.getVTList(&RetTys[0], RetTys.size()), |
| 3544 | &Ops[0], Ops.size()); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3545 | |
| 3546 | // This returns a pair of operands. The first element is the |
| 3547 | // return value for the function (if RetTy is not VoidTy). The second |
| 3548 | // element is the outgoing token chain. |
| 3549 | SDOperand ResVal; |
| 3550 | if (RetTys.size() != 1) { |
| 3551 | MVT::ValueType VT = getValueType(RetTy); |
| 3552 | if (RetTys.size() == 2) { |
| 3553 | ResVal = Res; |
| 3554 | |
| 3555 | // If this value was promoted, truncate it down. |
| 3556 | if (ResVal.getValueType() != VT) { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3557 | if (VT == MVT::Vector) { |
Chris Lattner | 5df99b3 | 2007-03-25 05:00:54 +0000 | [diff] [blame] | 3558 | // Insert a VBIT_CONVERT to convert from the packed result type to the |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3559 | // MVT::Vector type. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3560 | unsigned NumElems = cast<VectorType>(RetTy)->getNumElements(); |
| 3561 | const Type *EltTy = cast<VectorType>(RetTy)->getElementType(); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3562 | |
| 3563 | // Figure out if there is a Packed type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 3564 | // type. If so, convert to the vector type. |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 3565 | MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy),NumElems); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3566 | if (TVT != MVT::Other && isTypeLegal(TVT)) { |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3567 | // Insert a VBIT_CONVERT of the FORMAL_ARGUMENTS to a |
| 3568 | // "N x PTyElementVT" MVT::Vector type. |
| 3569 | ResVal = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, ResVal, |
Chris Lattner | d202ca4 | 2006-05-17 20:49:36 +0000 | [diff] [blame] | 3570 | DAG.getConstant(NumElems, MVT::i32), |
| 3571 | DAG.getValueType(getValueType(EltTy))); |
Chris Lattner | da098e7 | 2006-05-16 23:39:44 +0000 | [diff] [blame] | 3572 | } else { |
| 3573 | abort(); |
| 3574 | } |
| 3575 | } else if (MVT::isInteger(VT)) { |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3576 | unsigned AssertOp = ISD::AssertSext; |
| 3577 | if (!RetTyIsSigned) |
| 3578 | AssertOp = ISD::AssertZext; |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3579 | ResVal = DAG.getNode(AssertOp, ResVal.getValueType(), ResVal, |
| 3580 | DAG.getValueType(VT)); |
| 3581 | ResVal = DAG.getNode(ISD::TRUNCATE, VT, ResVal); |
| 3582 | } else { |
| 3583 | assert(MVT::isFloatingPoint(VT)); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 3584 | if (getTypeAction(VT) == Expand) |
| 3585 | ResVal = DAG.getNode(ISD::BIT_CONVERT, VT, ResVal); |
| 3586 | else |
| 3587 | ResVal = DAG.getNode(ISD::FP_ROUND, VT, ResVal); |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | } else if (RetTys.size() == 3) { |
| 3591 | ResVal = DAG.getNode(ISD::BUILD_PAIR, VT, |
| 3592 | Res.getValue(0), Res.getValue(1)); |
| 3593 | |
| 3594 | } else { |
| 3595 | assert(0 && "Case not handled yet!"); |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | return std::make_pair(ResVal, Res.getValue(Res.Val->getNumValues()-1)); |
| 3600 | } |
| 3601 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 3602 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 3603 | assert(0 && "LowerOperation not implemented for this target!"); |
| 3604 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 3605 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3608 | SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op, |
| 3609 | SelectionDAG &DAG) { |
| 3610 | assert(0 && "CustomPromoteOperation not implemented for this target!"); |
| 3611 | abort(); |
| 3612 | return SDOperand(); |
| 3613 | } |
| 3614 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3615 | /// getMemsetValue - Vectorized representation of the memset value |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3616 | /// operand. |
| 3617 | static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT, |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 3618 | SelectionDAG &DAG) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3619 | MVT::ValueType CurVT = VT; |
| 3620 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { |
| 3621 | uint64_t Val = C->getValue() & 255; |
| 3622 | unsigned Shift = 8; |
| 3623 | while (CurVT != MVT::i8) { |
| 3624 | Val = (Val << Shift) | Val; |
| 3625 | Shift <<= 1; |
| 3626 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3627 | } |
| 3628 | return DAG.getConstant(Val, VT); |
| 3629 | } else { |
| 3630 | Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value); |
| 3631 | unsigned Shift = 8; |
| 3632 | while (CurVT != MVT::i8) { |
| 3633 | Value = |
| 3634 | DAG.getNode(ISD::OR, VT, |
| 3635 | DAG.getNode(ISD::SHL, VT, Value, |
| 3636 | DAG.getConstant(Shift, MVT::i8)), Value); |
| 3637 | Shift <<= 1; |
| 3638 | CurVT = (MVT::ValueType)((unsigned)CurVT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | return Value; |
| 3642 | } |
| 3643 | } |
| 3644 | |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3645 | /// getMemsetStringVal - Similar to getMemsetValue. Except this is only |
| 3646 | /// used when a memcpy is turned into a memset when the source is a constant |
| 3647 | /// string ptr. |
| 3648 | static SDOperand getMemsetStringVal(MVT::ValueType VT, |
| 3649 | SelectionDAG &DAG, TargetLowering &TLI, |
| 3650 | std::string &Str, unsigned Offset) { |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3651 | uint64_t Val = 0; |
| 3652 | unsigned MSB = getSizeInBits(VT) / 8; |
| 3653 | if (TLI.isLittleEndian()) |
| 3654 | Offset = Offset + MSB - 1; |
| 3655 | for (unsigned i = 0; i != MSB; ++i) { |
Evan Cheng | a5a57d6 | 2006-11-29 01:38:07 +0000 | [diff] [blame] | 3656 | Val = (Val << 8) | (unsigned char)Str[Offset]; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3657 | Offset += TLI.isLittleEndian() ? -1 : 1; |
| 3658 | } |
| 3659 | return DAG.getConstant(Val, VT); |
| 3660 | } |
| 3661 | |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3662 | /// getMemBasePlusOffset - Returns base and offset node for the |
| 3663 | static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset, |
| 3664 | SelectionDAG &DAG, TargetLowering &TLI) { |
| 3665 | MVT::ValueType VT = Base.getValueType(); |
| 3666 | return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT)); |
| 3667 | } |
| 3668 | |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3669 | /// MeetsMaxMemopRequirement - Determines if the number of memory ops required |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3670 | /// to replace the memset / memcpy is below the threshold. It also returns the |
| 3671 | /// types of the sequence of memory ops to perform memset / memcpy. |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3672 | static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps, |
| 3673 | unsigned Limit, uint64_t Size, |
| 3674 | unsigned Align, TargetLowering &TLI) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3675 | MVT::ValueType VT; |
| 3676 | |
| 3677 | if (TLI.allowsUnalignedMemoryAccesses()) { |
| 3678 | VT = MVT::i64; |
| 3679 | } else { |
| 3680 | switch (Align & 7) { |
| 3681 | case 0: |
| 3682 | VT = MVT::i64; |
| 3683 | break; |
| 3684 | case 4: |
| 3685 | VT = MVT::i32; |
| 3686 | break; |
| 3687 | case 2: |
| 3688 | VT = MVT::i16; |
| 3689 | break; |
| 3690 | default: |
| 3691 | VT = MVT::i8; |
| 3692 | break; |
| 3693 | } |
| 3694 | } |
| 3695 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3696 | MVT::ValueType LVT = MVT::i64; |
| 3697 | while (!TLI.isTypeLegal(LVT)) |
| 3698 | LVT = (MVT::ValueType)((unsigned)LVT - 1); |
| 3699 | assert(MVT::isInteger(LVT)); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3700 | |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3701 | if (VT > LVT) |
| 3702 | VT = LVT; |
| 3703 | |
Evan Cheng | dea7245 | 2006-02-14 23:05:54 +0000 | [diff] [blame] | 3704 | unsigned NumMemOps = 0; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3705 | while (Size != 0) { |
| 3706 | unsigned VTSize = getSizeInBits(VT) / 8; |
| 3707 | while (VTSize > Size) { |
| 3708 | VT = (MVT::ValueType)((unsigned)VT - 1); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3709 | VTSize >>= 1; |
| 3710 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3711 | assert(MVT::isInteger(VT)); |
| 3712 | |
| 3713 | if (++NumMemOps > Limit) |
| 3714 | return false; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3715 | MemOps.push_back(VT); |
| 3716 | Size -= VTSize; |
| 3717 | } |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3718 | |
| 3719 | return true; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 3722 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3723 | SDOperand Op1 = getValue(I.getOperand(1)); |
| 3724 | SDOperand Op2 = getValue(I.getOperand(2)); |
| 3725 | SDOperand Op3 = getValue(I.getOperand(3)); |
| 3726 | SDOperand Op4 = getValue(I.getOperand(4)); |
| 3727 | unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue(); |
| 3728 | if (Align == 0) Align = 1; |
| 3729 | |
| 3730 | if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) { |
| 3731 | std::vector<MVT::ValueType> MemOps; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Expand memset / memcpy to a series of load / store ops |
| 3734 | // if the size operand falls below a certain threshold. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3735 | SmallVector<SDOperand, 8> OutChains; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3736 | switch (Op) { |
Evan Cheng | ac940ab | 2006-02-14 19:45:56 +0000 | [diff] [blame] | 3737 | default: break; // Do nothing for now. |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3738 | case ISD::MEMSET: { |
Evan Cheng | c4f8eee | 2006-02-14 20:12:38 +0000 | [diff] [blame] | 3739 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(), |
| 3740 | Size->getValue(), Align, TLI)) { |
Evan Cheng | 80e89d7 | 2006-02-14 09:11:59 +0000 | [diff] [blame] | 3741 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3742 | unsigned Offset = 0; |
| 3743 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 3744 | MVT::ValueType VT = MemOps[i]; |
| 3745 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | a47876d | 2006-02-15 22:12:35 +0000 | [diff] [blame] | 3746 | SDOperand Value = getMemsetValue(Op2, VT, DAG); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3747 | SDOperand Store = DAG.getStore(getRoot(), Value, |
Chris Lattner | 864635a | 2006-02-22 22:37:12 +0000 | [diff] [blame] | 3748 | getMemBasePlusOffset(Op1, Offset, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3749 | I.getOperand(1), Offset); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3750 | OutChains.push_back(Store); |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3751 | Offset += VTSize; |
| 3752 | } |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3753 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3754 | break; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3755 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3756 | case ISD::MEMCPY: { |
| 3757 | if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(), |
| 3758 | Size->getValue(), Align, TLI)) { |
| 3759 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3760 | unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3761 | GlobalAddressSDNode *G = NULL; |
| 3762 | std::string Str; |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3763 | bool CopyFromStr = false; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3764 | |
| 3765 | if (Op2.getOpcode() == ISD::GlobalAddress) |
| 3766 | G = cast<GlobalAddressSDNode>(Op2); |
| 3767 | else if (Op2.getOpcode() == ISD::ADD && |
| 3768 | Op2.getOperand(0).getOpcode() == ISD::GlobalAddress && |
| 3769 | Op2.getOperand(1).getOpcode() == ISD::Constant) { |
| 3770 | G = cast<GlobalAddressSDNode>(Op2.getOperand(0)); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3771 | SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue(); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3772 | } |
| 3773 | if (G) { |
| 3774 | GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal()); |
Evan Cheng | f3e486e | 2006-11-29 01:58:12 +0000 | [diff] [blame] | 3775 | if (GV && GV->isConstant()) { |
Evan Cheng | 0937103 | 2006-03-10 23:52:03 +0000 | [diff] [blame] | 3776 | Str = GV->getStringValue(false); |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3777 | if (!Str.empty()) { |
| 3778 | CopyFromStr = true; |
| 3779 | SrcOff += SrcDelta; |
| 3780 | } |
| 3781 | } |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3784 | for (unsigned i = 0; i < NumMemOps; i++) { |
| 3785 | MVT::ValueType VT = MemOps[i]; |
| 3786 | unsigned VTSize = getSizeInBits(VT) / 8; |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3787 | SDOperand Value, Chain, Store; |
| 3788 | |
Evan Cheng | cffbb51 | 2006-02-16 23:11:42 +0000 | [diff] [blame] | 3789 | if (CopyFromStr) { |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3790 | Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff); |
| 3791 | Chain = getRoot(); |
| 3792 | Store = |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3793 | DAG.getStore(Chain, Value, |
| 3794 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3795 | I.getOperand(1), DstOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3796 | } else { |
| 3797 | Value = DAG.getLoad(VT, getRoot(), |
| 3798 | getMemBasePlusOffset(Op2, SrcOff, DAG, TLI), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3799 | I.getOperand(2), SrcOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3800 | Chain = Value.getValue(1); |
| 3801 | Store = |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3802 | DAG.getStore(Chain, Value, |
| 3803 | getMemBasePlusOffset(Op1, DstOff, DAG, TLI), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3804 | I.getOperand(1), DstOff); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3805 | } |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3806 | OutChains.push_back(Store); |
Evan Cheng | 74d0aa9 | 2006-02-15 21:59:04 +0000 | [diff] [blame] | 3807 | SrcOff += VTSize; |
| 3808 | DstOff += VTSize; |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | break; |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | if (!OutChains.empty()) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3816 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3817 | &OutChains[0], OutChains.size())); |
Evan Cheng | c080d6f | 2006-02-15 01:54:51 +0000 | [diff] [blame] | 3818 | return; |
Evan Cheng | 1db92f9 | 2006-02-14 08:22:34 +0000 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3822 | DAG.setRoot(DAG.getNode(Op, MVT::Other, getRoot(), Op1, Op2, Op3, Op4)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 3825 | //===----------------------------------------------------------------------===// |
| 3826 | // SelectionDAGISel code |
| 3827 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3828 | |
| 3829 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 3830 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 3831 | } |
| 3832 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 3833 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 3834 | // FIXME: we only modify the CFG to split critical edges. This |
| 3835 | // updates dom and loop info. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 3836 | AU.addRequired<AliasAnalysis>(); |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 3837 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 3838 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3839 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3840 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3841 | /// OptimizeNoopCopyExpression - We have determined that the specified cast |
| 3842 | /// instruction is a noop copy (e.g. it's casting from one pointer type to |
| 3843 | /// another, int->uint, or int->sbyte on PPC. |
| 3844 | /// |
| 3845 | /// Return true if any changes are made. |
| 3846 | static bool OptimizeNoopCopyExpression(CastInst *CI) { |
| 3847 | BasicBlock *DefBB = CI->getParent(); |
| 3848 | |
| 3849 | /// InsertedCasts - Only insert a cast in each block once. |
| 3850 | std::map<BasicBlock*, CastInst*> InsertedCasts; |
| 3851 | |
| 3852 | bool MadeChange = false; |
| 3853 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
| 3854 | UI != E; ) { |
| 3855 | Use &TheUse = UI.getUse(); |
| 3856 | Instruction *User = cast<Instruction>(*UI); |
| 3857 | |
| 3858 | // Figure out which BB this cast is used in. For PHI's this is the |
| 3859 | // appropriate predecessor block. |
| 3860 | BasicBlock *UserBB = User->getParent(); |
| 3861 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 3862 | unsigned OpVal = UI.getOperandNo()/2; |
| 3863 | UserBB = PN->getIncomingBlock(OpVal); |
| 3864 | } |
| 3865 | |
| 3866 | // Preincrement use iterator so we don't invalidate it. |
| 3867 | ++UI; |
| 3868 | |
| 3869 | // If this user is in the same block as the cast, don't change the cast. |
| 3870 | if (UserBB == DefBB) continue; |
| 3871 | |
| 3872 | // If we have already inserted a cast into this block, use it. |
| 3873 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 3874 | |
| 3875 | if (!InsertedCast) { |
| 3876 | BasicBlock::iterator InsertPt = UserBB->begin(); |
| 3877 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 3878 | |
| 3879 | InsertedCast = |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 3880 | CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
| 3881 | InsertPt); |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3882 | MadeChange = true; |
| 3883 | } |
| 3884 | |
| 3885 | // Replace a use of the cast with a use of the new casat. |
| 3886 | TheUse = InsertedCast; |
| 3887 | } |
| 3888 | |
| 3889 | // If we removed all uses, nuke the cast. |
| 3890 | if (CI->use_empty()) |
| 3891 | CI->eraseFromParent(); |
| 3892 | |
| 3893 | return MadeChange; |
| 3894 | } |
| 3895 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3896 | /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset, |
| 3897 | /// casting to the type of GEPI. |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3898 | static Instruction *InsertGEPComputeCode(Instruction *&V, BasicBlock *BB, |
| 3899 | Instruction *GEPI, Value *Ptr, |
| 3900 | Value *PtrOffset) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3901 | if (V) return V; // Already computed. |
| 3902 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3903 | // Figure out the insertion point |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3904 | BasicBlock::iterator InsertPt; |
| 3905 | if (BB == GEPI->getParent()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3906 | // If GEP is already inserted into BB, insert right after the GEP. |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3907 | InsertPt = GEPI; |
| 3908 | ++InsertPt; |
| 3909 | } else { |
| 3910 | // Otherwise, insert at the top of BB, after any PHI nodes |
| 3911 | InsertPt = BB->begin(); |
| 3912 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 3913 | } |
| 3914 | |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 3915 | // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into |
| 3916 | // BB so that there is only one value live across basic blocks (the cast |
| 3917 | // operand). |
| 3918 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 3919 | if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType())) |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 3920 | Ptr = CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), |
| 3921 | "", InsertPt); |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 3922 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3923 | // Add the offset, cast it to the right type. |
| 3924 | Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3925 | // Ptr is an integer type, GEPI is pointer type ==> IntToPtr |
| 3926 | return V = CastInst::create(Instruction::IntToPtr, Ptr, GEPI->getType(), |
| 3927 | "", InsertPt); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3930 | /// ReplaceUsesOfGEPInst - Replace all uses of RepPtr with inserted code to |
| 3931 | /// compute its value. The RepPtr value can be computed with Ptr+PtrOffset. One |
| 3932 | /// trivial way of doing this would be to evaluate Ptr+PtrOffset in RepPtr's |
| 3933 | /// block, then ReplaceAllUsesWith'ing everything. However, we would prefer to |
| 3934 | /// sink PtrOffset into user blocks where doing so will likely allow us to fold |
| 3935 | /// the constant add into a load or store instruction. Additionally, if a user |
| 3936 | /// is a pointer-pointer cast, we look through it to find its users. |
| 3937 | static void ReplaceUsesOfGEPInst(Instruction *RepPtr, Value *Ptr, |
| 3938 | Constant *PtrOffset, BasicBlock *DefBB, |
| 3939 | GetElementPtrInst *GEPI, |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3940 | std::map<BasicBlock*,Instruction*> &InsertedExprs) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3941 | while (!RepPtr->use_empty()) { |
| 3942 | Instruction *User = cast<Instruction>(RepPtr->use_back()); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3943 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3944 | // If the user is a Pointer-Pointer cast, recurse. Only BitCast can be |
| 3945 | // used for a Pointer-Pointer cast. |
| 3946 | if (isa<BitCastInst>(User)) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3947 | ReplaceUsesOfGEPInst(User, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3948 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3949 | // Drop the use of RepPtr. The cast is dead. Don't delete it now, else we |
| 3950 | // could invalidate an iterator. |
| 3951 | User->setOperand(0, UndefValue::get(RepPtr->getType())); |
| 3952 | continue; |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3955 | // If this is a load of the pointer, or a store through the pointer, emit |
| 3956 | // the increment into the load/store block. |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3957 | Instruction *NewVal; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3958 | if (isa<LoadInst>(User) || |
| 3959 | (isa<StoreInst>(User) && User->getOperand(0) != RepPtr)) { |
| 3960 | NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], |
| 3961 | User->getParent(), GEPI, |
| 3962 | Ptr, PtrOffset); |
| 3963 | } else { |
| 3964 | // If this use is not foldable into the addressing mode, use a version |
| 3965 | // emitted in the GEP block. |
| 3966 | NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, |
| 3967 | Ptr, PtrOffset); |
| 3968 | } |
| 3969 | |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3970 | if (GEPI->getType() != RepPtr->getType()) { |
| 3971 | BasicBlock::iterator IP = NewVal; |
| 3972 | ++IP; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3973 | // NewVal must be a GEP which must be pointer type, so BitCast |
| 3974 | NewVal = new BitCastInst(NewVal, RepPtr->getType(), "", IP); |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 3975 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3976 | User->replaceUsesOfWith(RepPtr, NewVal); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 3977 | } |
| 3978 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3979 | |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3980 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3981 | /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction |
| 3982 | /// selection, we want to be a bit careful about some things. In particular, if |
| 3983 | /// we have a GEP instruction that is used in a different block than it is |
| 3984 | /// defined, the addressing expression of the GEP cannot be folded into loads or |
| 3985 | /// stores that use it. In this case, decompose the GEP and move constant |
| 3986 | /// indices into blocks that use it. |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 3987 | static bool OptimizeGEPExpression(GetElementPtrInst *GEPI, |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3988 | const TargetData *TD) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 3989 | // If this GEP is only used inside the block it is defined in, there is no |
| 3990 | // need to rewrite it. |
| 3991 | bool isUsedOutsideDefBB = false; |
| 3992 | BasicBlock *DefBB = GEPI->getParent(); |
| 3993 | for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); |
| 3994 | UI != E; ++UI) { |
| 3995 | if (cast<Instruction>(*UI)->getParent() != DefBB) { |
| 3996 | isUsedOutsideDefBB = true; |
| 3997 | break; |
| 3998 | } |
| 3999 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4000 | if (!isUsedOutsideDefBB) return false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4001 | |
| 4002 | // If this GEP has no non-zero constant indices, there is nothing we can do, |
| 4003 | // ignore it. |
| 4004 | bool hasConstantIndex = false; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4005 | bool hasVariableIndex = false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4006 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 4007 | E = GEPI->op_end(); OI != E; ++OI) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4008 | if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4009 | if (CI->getZExtValue()) { |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4010 | hasConstantIndex = true; |
| 4011 | break; |
| 4012 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4013 | } else { |
| 4014 | hasVariableIndex = true; |
| 4015 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4016 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4017 | |
| 4018 | // If this is a "GEP X, 0, 0, 0", turn this into a cast. |
| 4019 | if (!hasConstantIndex && !hasVariableIndex) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4020 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 4021 | Value *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4022 | GEPI->getName(), GEPI); |
| 4023 | GEPI->replaceAllUsesWith(NC); |
| 4024 | GEPI->eraseFromParent(); |
| 4025 | return true; |
| 4026 | } |
| 4027 | |
Chris Lattner | 3802c25 | 2005-12-11 09:05:13 +0000 | [diff] [blame] | 4028 | // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses. |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4029 | if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) |
| 4030 | return false; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4031 | |
| 4032 | // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the |
| 4033 | // constant offset (which we now know is non-zero) and deal with it later. |
| 4034 | uint64_t ConstantOffset = 0; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 4035 | const Type *UIntPtrTy = TD->getIntPtrType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 4036 | Value *Ptr = new PtrToIntInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4037 | const Type *Ty = GEPI->getOperand(0)->getType(); |
| 4038 | |
| 4039 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 4040 | E = GEPI->op_end(); OI != E; ++OI) { |
| 4041 | Value *Idx = *OI; |
| 4042 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4043 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4044 | if (Field) |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 4045 | ConstantOffset += TD->getStructLayout(StTy)->getElementOffset(Field); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4046 | Ty = StTy->getElementType(Field); |
| 4047 | } else { |
| 4048 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 4049 | |
| 4050 | // Handle constant subscripts. |
| 4051 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4052 | if (CI->getZExtValue() == 0) continue; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4053 | ConstantOffset += (int64_t)TD->getTypeSize(Ty)*CI->getSExtValue(); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4054 | continue; |
| 4055 | } |
| 4056 | |
| 4057 | // Ptr = Ptr + Idx * ElementSize; |
| 4058 | |
| 4059 | // Cast Idx to UIntPtrTy if needed. |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 4060 | Idx = CastInst::createIntegerCast(Idx, UIntPtrTy, true/*SExt*/, "", GEPI); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4061 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 4062 | uint64_t ElementSize = TD->getTypeSize(Ty); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4063 | // Mask off bits that should not be set. |
| 4064 | ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4065 | Constant *SizeCst = ConstantInt::get(UIntPtrTy, ElementSize); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4066 | |
| 4067 | // Multiply by the element size and add to the base. |
| 4068 | Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI); |
| 4069 | Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI); |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | // Make sure that the offset fits in uintptr_t. |
| 4074 | ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4075 | Constant *PtrOffset = ConstantInt::get(UIntPtrTy, ConstantOffset); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4076 | |
| 4077 | // Okay, we have now emitted all of the variable index parts to the BB that |
| 4078 | // the GEP is defined in. Loop over all of the using instructions, inserting |
| 4079 | // an "add Ptr, ConstantOffset" into each block that uses it and update the |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 4080 | // instruction to use the newly computed value, making GEPI dead. When the |
| 4081 | // user is a load or store instruction address, we emit the add into the user |
| 4082 | // block, otherwise we use a canonical version right next to the gep (these |
| 4083 | // won't be foldable as addresses, so we might as well share the computation). |
| 4084 | |
Chris Lattner | f0df882 | 2006-05-06 09:10:37 +0000 | [diff] [blame] | 4085 | std::map<BasicBlock*,Instruction*> InsertedExprs; |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4086 | ReplaceUsesOfGEPInst(GEPI, Ptr, PtrOffset, DefBB, GEPI, InsertedExprs); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Finally, the GEP is dead, remove it. |
| 4089 | GEPI->eraseFromParent(); |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4090 | |
| 4091 | return true; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4094 | /// isLoopInvariantInst - Returns true if all operands of the instruction are |
| 4095 | /// loop invariants in the specified loop. |
| 4096 | static bool isLoopInvariantInst(Instruction *I, Loop *L) { |
| 4097 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 4098 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 4099 | if (!L->isLoopInvariant(I->getOperand(i))) |
| 4100 | return false; |
| 4101 | return true; |
| 4102 | } |
| 4103 | |
| 4104 | /// SinkInvariantGEPIndex - If a GEP instruction has a variable index that has |
| 4105 | /// been hoisted out of the loop by LICM pass, sink it back into the use BB |
| 4106 | /// if it can be determined that the index computation can be folded into the |
| 4107 | /// addressing mode of the load / store uses. |
| 4108 | static bool SinkInvariantGEPIndex(BinaryOperator *BinOp, LoopInfo *loopInfo, |
| 4109 | const TargetLowering &TLI) { |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4110 | // Only look at Add / Sub for now. |
| 4111 | if (BinOp->getOpcode() != Instruction::Add && |
| 4112 | BinOp->getOpcode() != Instruction::Sub) |
| 4113 | return false; |
| 4114 | |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4115 | // DestBBs - These are the blocks where a copy of BinOp will be inserted. |
Evan Cheng | db264ae | 2007-03-17 08:53:30 +0000 | [diff] [blame] | 4116 | SmallSet<BasicBlock*, 8> DestBBs; |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4117 | BasicBlock *DefBB = BinOp->getParent(); |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4118 | bool MadeChange = false; |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4119 | for (Value::use_iterator UI = BinOp->use_begin(), E = BinOp->use_end(); |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4120 | UI != E; ++UI) { |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4121 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4122 | // Only look for GEP use in another block. |
| 4123 | if (User->getParent() == DefBB) continue; |
| 4124 | |
| 4125 | if (isa<GetElementPtrInst>(User)) { |
| 4126 | BasicBlock *UserBB = User->getParent(); |
| 4127 | Loop *L = loopInfo->getLoopFor(UserBB); |
| 4128 | |
| 4129 | // Only sink if expression is a loop invariant in the use BB. |
Evan Cheng | 9f5ead9 | 2007-03-16 17:50:20 +0000 | [diff] [blame] | 4130 | if (L && isLoopInvariantInst(BinOp, L) && !User->use_empty()) { |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4131 | const Type *UseTy = NULL; |
| 4132 | // FIXME: We are assuming all the uses of the GEP will have the |
| 4133 | // same type. |
| 4134 | Instruction *GEPUser = cast<Instruction>(*User->use_begin()); |
| 4135 | if (LoadInst *Load = dyn_cast<LoadInst>(GEPUser)) |
| 4136 | UseTy = Load->getType(); |
| 4137 | else if (StoreInst *Store = dyn_cast<StoreInst>(GEPUser)) |
| 4138 | UseTy = Store->getOperand(0)->getType(); |
| 4139 | |
| 4140 | // Check if it is possible to fold the expression to address mode. |
| 4141 | if (UseTy && |
Evan Cheng | 29a68fb | 2007-03-20 19:32:11 +0000 | [diff] [blame] | 4142 | TLI.isLegalAddressExpression(BinOp->getOpcode(), |
| 4143 | BinOp->getOperand(0), |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4144 | BinOp->getOperand(1), UseTy)) { |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4145 | DestBBs.insert(UserBB); |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4146 | MadeChange = true; |
| 4147 | } |
| 4148 | } |
| 4149 | } |
| 4150 | } |
| 4151 | |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4152 | // Nothing to do. |
| 4153 | if (!MadeChange) |
| 4154 | return false; |
| 4155 | |
| 4156 | /// InsertedOps - Only insert a duplicate in each block once. |
| 4157 | std::map<BasicBlock*, BinaryOperator*> InsertedOps; |
| 4158 | for (Value::use_iterator UI = BinOp->use_begin(), E = BinOp->use_end(); |
| 4159 | UI != E; ) { |
| 4160 | Instruction *User = cast<Instruction>(*UI); |
| 4161 | BasicBlock *UserBB = User->getParent(); |
| 4162 | |
| 4163 | // Preincrement use iterator so we don't invalidate it. |
| 4164 | ++UI; |
| 4165 | |
| 4166 | // If any user in this BB wants it, replace all the uses in the BB. |
| 4167 | if (DestBBs.count(UserBB)) { |
| 4168 | // Sink it into user block. |
| 4169 | BinaryOperator *&InsertedOp = InsertedOps[UserBB]; |
| 4170 | if (!InsertedOp) { |
| 4171 | BasicBlock::iterator InsertPt = UserBB->begin(); |
| 4172 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 4173 | |
| 4174 | InsertedOp = |
| 4175 | BinaryOperator::create(BinOp->getOpcode(), BinOp->getOperand(0), |
| 4176 | BinOp->getOperand(1), "", InsertPt); |
| 4177 | } |
| 4178 | |
| 4179 | User->replaceUsesOfWith(BinOp, InsertedOp); |
| 4180 | } |
| 4181 | } |
| 4182 | |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4183 | if (BinOp->use_empty()) |
| 4184 | BinOp->eraseFromParent(); |
| 4185 | |
Evan Cheng | 2edd563 | 2007-03-17 08:22:49 +0000 | [diff] [blame] | 4186 | return true; |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 4189 | |
| 4190 | /// SplitEdgeNicely - Split the critical edge from TI to it's specified |
| 4191 | /// successor if it will improve codegen. We only do this if the successor has |
| 4192 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 4193 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 4194 | /// instead of introducing a new block. |
| 4195 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) { |
| 4196 | BasicBlock *TIBB = TI->getParent(); |
| 4197 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 4198 | assert(isa<PHINode>(Dest->begin()) && |
| 4199 | "This should only be called if Dest has a PHI!"); |
| 4200 | |
| 4201 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 4202 | /// PHIs in Dest that TI would provide. |
| 4203 | std::vector<Value*> TIPHIValues; |
| 4204 | |
| 4205 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 4206 | // this terminator. |
| 4207 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 4208 | BasicBlock *Pred = *PI; |
| 4209 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 4210 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 4211 | if (!PredBr || !PredBr->isUnconditional() || |
| 4212 | // Must be empty other than the branch. |
| 4213 | &Pred->front() != PredBr) |
| 4214 | continue; |
| 4215 | |
| 4216 | // Finally, since we know that Dest has phi nodes in it, we have to make |
| 4217 | // sure that jumping to Pred will have the same affect as going to Dest in |
| 4218 | // terms of PHI values. |
| 4219 | PHINode *PN; |
| 4220 | unsigned PHINo = 0; |
| 4221 | bool FoundMatch = true; |
| 4222 | for (BasicBlock::iterator I = Dest->begin(); |
| 4223 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 4224 | if (PHINo == TIPHIValues.size()) |
| 4225 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
| 4226 | |
| 4227 | // If the PHI entry doesn't work, we can't use this pred. |
| 4228 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 4229 | FoundMatch = false; |
| 4230 | break; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | // If we found a workable predecessor, change TI to branch to Succ. |
| 4235 | if (FoundMatch) { |
| 4236 | Dest->removePredecessor(TIBB); |
| 4237 | TI->setSuccessor(SuccNum, Pred); |
| 4238 | return; |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | SplitCriticalEdge(TI, SuccNum, P, true); |
| 4243 | } |
| 4244 | |
| 4245 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4246 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 4247 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 4248 | RegMap = MF.getSSARegMap(); |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4249 | DOUT << "\n\n\n=== " << Fn.getName() << "\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4250 | |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4251 | LoopInfo *loopInfo = &getAnalysis<LoopInfo>(); |
| 4252 | |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 4253 | // First, split all critical edges. |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4254 | // |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 4255 | // In this pass we also look for GEP and cast instructions that are used |
| 4256 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 4257 | // selection. |
| 4258 | // |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4259 | bool MadeChange = true; |
| 4260 | while (MadeChange) { |
| 4261 | MadeChange = false; |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4262 | for (Function::iterator FNI = Fn.begin(), E = Fn.end(); FNI != E; ++FNI) { |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 4263 | // Split all critical edges where the dest block has a PHI. |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4264 | TerminatorInst *BBTI = FNI->getTerminator(); |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 4265 | if (BBTI->getNumSuccessors() > 1) { |
| 4266 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | bad7f48 | 2006-10-28 19:22:10 +0000 | [diff] [blame] | 4267 | if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) && |
| 4268 | isCriticalEdge(BBTI, i, true)) |
| 4269 | SplitEdgeNicely(BBTI, i, this); |
Chris Lattner | 47e32e6 | 2006-10-28 17:04:37 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 4272 | |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4273 | for (BasicBlock::iterator BBI = FNI->begin(), E = FNI->end(); BBI != E; ) { |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 4274 | Instruction *I = BBI++; |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 4275 | |
| 4276 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 4277 | // If we found an inline asm expession, and if the target knows how to |
| 4278 | // lower it to normal LLVM code, do so now. |
| 4279 | if (isa<InlineAsm>(CI->getCalledValue())) |
| 4280 | if (const TargetAsmInfo *TAI = |
| 4281 | TLI.getTargetMachine().getTargetAsmInfo()) { |
| 4282 | if (TAI->ExpandInlineAsm(CI)) |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4283 | BBI = FNI->begin(); |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4286 | MadeChange |= OptimizeGEPExpression(GEPI, TLI.getTargetData()); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 4287 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | c970f06 | 2006-09-13 06:02:42 +0000 | [diff] [blame] | 4288 | // If the source of the cast is a constant, then this should have |
| 4289 | // already been constant folded. The only reason NOT to constant fold |
| 4290 | // it is if something (e.g. LSR) was careful to place the constant |
| 4291 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 4292 | // the address of globals out of a loop). If this is the case, we don't |
| 4293 | // want to forward-subst the cast. |
| 4294 | if (isa<Constant>(CI->getOperand(0))) |
| 4295 | continue; |
| 4296 | |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 4297 | // If this is a noop copy, sink it into user blocks to reduce the number |
| 4298 | // of virtual registers that must be created and coallesced. |
| 4299 | MVT::ValueType SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 4300 | MVT::ValueType DstVT = TLI.getValueType(CI->getType()); |
| 4301 | |
| 4302 | // This is an fp<->int conversion? |
| 4303 | if (MVT::isInteger(SrcVT) != MVT::isInteger(DstVT)) |
| 4304 | continue; |
| 4305 | |
| 4306 | // If this is an extension, it will be a zero or sign extension, which |
| 4307 | // isn't a noop. |
| 4308 | if (SrcVT < DstVT) continue; |
| 4309 | |
| 4310 | // If these values will be promoted, find out what they will be promoted |
| 4311 | // to. This helps us consider truncates on PPC as noop copies when they |
| 4312 | // are. |
| 4313 | if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) |
| 4314 | SrcVT = TLI.getTypeToTransformTo(SrcVT); |
| 4315 | if (TLI.getTypeAction(DstVT) == TargetLowering::Promote) |
| 4316 | DstVT = TLI.getTypeToTransformTo(DstVT); |
| 4317 | |
| 4318 | // If, after promotion, these are the same types, this is a noop copy. |
| 4319 | if (SrcVT == DstVT) |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4320 | MadeChange |= OptimizeNoopCopyExpression(CI); |
Evan Cheng | 3cd4e50 | 2007-03-16 08:46:27 +0000 | [diff] [blame] | 4321 | } else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I)) { |
| 4322 | MadeChange |= SinkInvariantGEPIndex(BinOp, loopInfo, TLI); |
Chris Lattner | 7e59809 | 2006-05-05 01:04:50 +0000 | [diff] [blame] | 4323 | } |
| 4324 | } |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 4325 | } |
Chris Lattner | 9032364 | 2006-05-05 21:17:49 +0000 | [diff] [blame] | 4326 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 4327 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4328 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 4329 | |
| 4330 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 4331 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4332 | |
Evan Cheng | ad2070c | 2007-02-10 02:43:39 +0000 | [diff] [blame] | 4333 | // Add function live-ins to entry block live-in set. |
| 4334 | BasicBlock *EntryBB = &Fn.getEntryBlock(); |
| 4335 | BB = FuncInfo.MBBMap[EntryBB]; |
| 4336 | if (!MF.livein_empty()) |
| 4337 | for (MachineFunction::livein_iterator I = MF.livein_begin(), |
| 4338 | E = MF.livein_end(); I != E; ++I) |
| 4339 | BB->addLiveIn(I->first); |
| 4340 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4341 | return true; |
| 4342 | } |
| 4343 | |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4344 | SDOperand SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, |
| 4345 | unsigned Reg) { |
| 4346 | SDOperand Op = getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 4347 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4348 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 4349 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4350 | |
| 4351 | // If this type is not legal, we must make sure to not create an invalid |
| 4352 | // register use. |
| 4353 | MVT::ValueType SrcVT = Op.getValueType(); |
| 4354 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4355 | if (SrcVT == DestVT) { |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4356 | return DAG.getCopyToReg(getRoot(), Reg, Op); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 4357 | } else if (SrcVT == MVT::Vector) { |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4358 | // Handle copies from generic vectors to registers. |
| 4359 | MVT::ValueType PTyElementVT, PTyLegalElementVT; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 4360 | unsigned NE = TLI.getVectorTypeBreakdown(cast<VectorType>(V->getType()), |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4361 | PTyElementVT, PTyLegalElementVT); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 4362 | |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4363 | // Insert a VBIT_CONVERT of the input vector to a "N x PTyElementVT" |
| 4364 | // MVT::Vector type. |
| 4365 | Op = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Op, |
| 4366 | DAG.getConstant(NE, MVT::i32), |
| 4367 | DAG.getValueType(PTyElementVT)); |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 4368 | |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4369 | // Loop over all of the elements of the resultant vector, |
| 4370 | // VEXTRACT_VECTOR_ELT'ing them, converting them to PTyLegalElementVT, then |
| 4371 | // copying them into output registers. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4372 | SmallVector<SDOperand, 8> OutChains; |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4373 | SDOperand Root = getRoot(); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4374 | for (unsigned i = 0; i != NE; ++i) { |
| 4375 | SDOperand Elt = DAG.getNode(ISD::VEXTRACT_VECTOR_ELT, PTyElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 4376 | Op, DAG.getConstant(i, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4377 | if (PTyElementVT == PTyLegalElementVT) { |
| 4378 | // Elements are legal. |
| 4379 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt)); |
| 4380 | } else if (PTyLegalElementVT > PTyElementVT) { |
| 4381 | // Elements are promoted. |
| 4382 | if (MVT::isFloatingPoint(PTyLegalElementVT)) |
| 4383 | Elt = DAG.getNode(ISD::FP_EXTEND, PTyLegalElementVT, Elt); |
| 4384 | else |
| 4385 | Elt = DAG.getNode(ISD::ANY_EXTEND, PTyLegalElementVT, Elt); |
| 4386 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Elt)); |
| 4387 | } else { |
| 4388 | // Elements are expanded. |
| 4389 | // The src value is expanded into multiple registers. |
| 4390 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 4391 | Elt, DAG.getConstant(0, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4392 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, PTyLegalElementVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 4393 | Elt, DAG.getConstant(1, TLI.getPointerTy())); |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4394 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Lo)); |
| 4395 | OutChains.push_back(DAG.getCopyToReg(Root, Reg++, Hi)); |
| 4396 | } |
Chris Lattner | 1c6191f | 2006-03-21 19:20:37 +0000 | [diff] [blame] | 4397 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4398 | return DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4399 | &OutChains[0], OutChains.size()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4400 | } else if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4401 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 4402 | if (MVT::isFloatingPoint(SrcVT)) |
| 4403 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 4404 | else |
Chris Lattner | fab0887 | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 4405 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4406 | return DAG.getCopyToReg(getRoot(), Reg, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4407 | } else { |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4408 | DestVT = TLI.getTypeToExpandTo(SrcVT); |
| 4409 | unsigned NumVals = TLI.getNumElements(SrcVT); |
| 4410 | if (NumVals == 1) |
| 4411 | return DAG.getCopyToReg(getRoot(), Reg, |
| 4412 | DAG.getNode(ISD::BIT_CONVERT, DestVT, Op)); |
| 4413 | assert(NumVals == 2 && "1 to 4 (and more) expansion not implemented!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4414 | // The src value is expanded into multiple registers. |
| 4415 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 4416 | Op, DAG.getConstant(0, TLI.getPointerTy())); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4417 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
Evan Cheng | a844126 | 2006-06-15 08:11:54 +0000 | [diff] [blame] | 4418 | Op, DAG.getConstant(1, TLI.getPointerTy())); |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4419 | Op = DAG.getCopyToReg(getRoot(), Reg, Lo); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4420 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 4421 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4424 | void SelectionDAGISel:: |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4425 | LowerArguments(BasicBlock *LLVMBB, SelectionDAGLowering &SDL, |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4426 | std::vector<SDOperand> &UnorderedChains) { |
| 4427 | // If this is the entry block, emit arguments. |
Evan Cheng | 15699fc | 2007-02-10 01:08:18 +0000 | [diff] [blame] | 4428 | Function &F = *LLVMBB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 4429 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4430 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 4431 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4432 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4433 | unsigned a = 0; |
| 4434 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 4435 | AI != E; ++AI, ++a) |
| 4436 | if (!AI->use_empty()) { |
| 4437 | SDL.setValue(AI, Args[a]); |
Evan Cheng | f7179bb | 2006-04-27 08:29:42 +0000 | [diff] [blame] | 4438 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4439 | // If this argument is live outside of the entry block, insert a copy from |
| 4440 | // whereever we got it to the vreg that other BB's will reference it as. |
Chris Lattner | 251db18 | 2007-02-25 18:40:32 +0000 | [diff] [blame] | 4441 | DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo.ValueMap.find(AI); |
| 4442 | if (VMI != FuncInfo.ValueMap.end()) { |
| 4443 | SDOperand Copy = SDL.CopyValueToVirtualRegister(AI, VMI->second); |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4444 | UnorderedChains.push_back(Copy); |
| 4445 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 4446 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4447 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4448 | // Finally, if the target has anything special to do, allow it to do so. |
Chris Lattner | 9664541 | 2006-05-16 06:10:58 +0000 | [diff] [blame] | 4449 | // FIXME: this should insert code into the DAG! |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4450 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4453 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 4454 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4455 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4456 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4457 | |
| 4458 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4459 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4460 | // Lower any arguments needed in this block if this is the entry block. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 4461 | if (LLVMBB == &LLVMBB->getParent()->getEntryBlock()) |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 4462 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4463 | |
| 4464 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 4465 | SDL.setCurrentBasicBlock(BB); |
| 4466 | |
| 4467 | // Lower all of the non-terminator instructions. |
| 4468 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 4469 | I != E; ++I) |
| 4470 | SDL.visit(*I); |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 4471 | |
| 4472 | // Lower call part of invoke. |
| 4473 | InvokeInst *Invoke = dyn_cast<InvokeInst>(LLVMBB->getTerminator()); |
| 4474 | if (Invoke) SDL.visitInvoke(*Invoke, false); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4475 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4476 | // Ensure that all instructions which are used outside of their defining |
| 4477 | // blocks are available as virtual registers. |
| 4478 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 4479 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | 9f24ad7 | 2007-02-04 01:35:11 +0000 | [diff] [blame] | 4480 | DenseMap<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4481 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4482 | UnorderedChains.push_back( |
Chris Lattner | 571e434 | 2006-10-27 21:36:01 +0000 | [diff] [blame] | 4483 | SDL.CopyValueToVirtualRegister(I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 4487 | // ensure constants are generated when needed. Remember the virtual registers |
| 4488 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 4489 | // directly add them, because expansion might result in multiple MBB's for one |
| 4490 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 4491 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4492 | // |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4493 | TerminatorInst *TI = LLVMBB->getTerminator(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4494 | |
| 4495 | // Emit constants only once even if used by multiple PHI nodes. |
| 4496 | std::map<Constant*, unsigned> ConstantsOut; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4497 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4498 | // Vector bool would be better, but vector<bool> is really slow. |
| 4499 | std::vector<unsigned char> SuccsHandled; |
| 4500 | if (TI->getNumSuccessors()) |
| 4501 | SuccsHandled.resize(BB->getParent()->getNumBlockIDs()); |
| 4502 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4503 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 4504 | // this block. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4505 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 4506 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4507 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4508 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4509 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4510 | // If this terminator has multiple identical successors (common for |
| 4511 | // switches), only handle each succ once. |
| 4512 | unsigned SuccMBBNo = SuccMBB->getNumber(); |
| 4513 | if (SuccsHandled[SuccMBBNo]) continue; |
| 4514 | SuccsHandled[SuccMBBNo] = true; |
| 4515 | |
| 4516 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4517 | PHINode *PN; |
| 4518 | |
| 4519 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 4520 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 4521 | // emitted yet. |
| 4522 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4523 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 4524 | // Ignore dead phi's. |
| 4525 | if (PN->use_empty()) continue; |
| 4526 | |
| 4527 | unsigned Reg; |
| 4528 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
Chris Lattner | 3f7927c | 2006-11-29 01:12:32 +0000 | [diff] [blame] | 4529 | |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4530 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 4531 | unsigned &RegOut = ConstantsOut[C]; |
| 4532 | if (RegOut == 0) { |
| 4533 | RegOut = FuncInfo.CreateRegForValue(C); |
| 4534 | UnorderedChains.push_back( |
| 4535 | SDL.CopyValueToVirtualRegister(C, RegOut)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4536 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4537 | Reg = RegOut; |
| 4538 | } else { |
| 4539 | Reg = FuncInfo.ValueMap[PHIOp]; |
| 4540 | if (Reg == 0) { |
| 4541 | assert(isa<AllocaInst>(PHIOp) && |
| 4542 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 4543 | "Didn't codegen value into a register!??"); |
| 4544 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
| 4545 | UnorderedChains.push_back( |
| 4546 | SDL.CopyValueToVirtualRegister(PHIOp, Reg)); |
Chris Lattner | 7e02151 | 2006-03-31 02:12:18 +0000 | [diff] [blame] | 4547 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4548 | } |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4549 | |
| 4550 | // Remember that this register needs to added to the machine PHI node as |
| 4551 | // the input for this MBB. |
| 4552 | MVT::ValueType VT = TLI.getValueType(PN->getType()); |
| 4553 | unsigned NumElements; |
| 4554 | if (VT != MVT::Vector) |
| 4555 | NumElements = TLI.getNumElements(VT); |
| 4556 | else { |
| 4557 | MVT::ValueType VT1,VT2; |
| 4558 | NumElements = |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 4559 | TLI.getVectorTypeBreakdown(cast<VectorType>(PN->getType()), |
Chris Lattner | 8c494ab | 2006-10-27 23:50:33 +0000 | [diff] [blame] | 4560 | VT1, VT2); |
| 4561 | } |
| 4562 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 4563 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
| 4564 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4565 | } |
| 4566 | ConstantsOut.clear(); |
| 4567 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4568 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 4569 | if (!UnorderedChains.empty()) { |
Chris Lattner | 7436b57 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 4570 | SDOperand Root = SDL.getRoot(); |
| 4571 | if (Root.getOpcode() != ISD::EntryToken) { |
| 4572 | unsigned i = 0, e = UnorderedChains.size(); |
| 4573 | for (; i != e; ++i) { |
| 4574 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 4575 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 4576 | break; // Don't add the root if we already indirectly depend on it. |
| 4577 | } |
| 4578 | |
| 4579 | if (i == e) |
| 4580 | UnorderedChains.push_back(Root); |
| 4581 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4582 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4583 | &UnorderedChains[0], UnorderedChains.size())); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4586 | // Lower the terminator after the copies are emitted. |
Jim Laskey | 183f47f | 2007-02-25 21:43:59 +0000 | [diff] [blame] | 4587 | if (Invoke) { |
| 4588 | // Just the branch part of invoke. |
| 4589 | SDL.visitInvoke(*Invoke, true); |
| 4590 | } else { |
| 4591 | SDL.visit(*LLVMBB->getTerminator()); |
| 4592 | } |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4593 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4594 | // Copy over any CaseBlock records that may now exist due to SwitchInst |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4595 | // lowering, as well as any jump table information. |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4596 | SwitchCases.clear(); |
| 4597 | SwitchCases = SDL.SwitchCases; |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4598 | JTCases.clear(); |
| 4599 | JTCases = SDL.JTCases; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4600 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 4601 | // Make sure the root of the DAG is up-to-date. |
| 4602 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4605 | void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) { |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4606 | // Get alias analysis for load/store combining. |
| 4607 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 4608 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4609 | // Run the DAG combiner in pre-legalize mode. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4610 | DAG.Combine(false, AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4611 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4612 | DOUT << "Lowered selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4613 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4614 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4615 | // Second step, hack on the DAG until it only uses operations and types that |
| 4616 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 4617 | DAG.Legalize(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4618 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4619 | DOUT << "Legalized selection DAG:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4620 | DEBUG(DAG.dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4621 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 4622 | // Run the DAG combiner in post-legalize mode. |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 4623 | DAG.Combine(true, AA); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 4624 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4625 | if (ViewISelDAGs) DAG.viewGraph(); |
Evan Cheng | 552c4a8 | 2006-04-28 02:09:19 +0000 | [diff] [blame] | 4626 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4627 | // Third, instruction select all of the operations to machine code, adding the |
| 4628 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4629 | InstructionSelectBasicBlock(DAG); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4630 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4631 | DOUT << "Selected machine code:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4632 | DEBUG(BB->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4633 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4634 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4635 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 4636 | FunctionLoweringInfo &FuncInfo) { |
| 4637 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 4638 | { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 4639 | SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4640 | CurDAG = &DAG; |
| 4641 | |
| 4642 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 4643 | // types that are not supported by the target. |
| 4644 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 4645 | |
| 4646 | // Second step, emit the lowered DAG as machine code. |
| 4647 | CodeGenAndEmitDAG(DAG); |
| 4648 | } |
| 4649 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4650 | // Next, now that we know what the last MBB the LLVM BB expanded is, update |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4651 | // PHI nodes in successors. |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4652 | if (SwitchCases.empty() && JTCases.empty()) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4653 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 4654 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 4655 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4656 | "This is not a machine PHI node that we are updating!"); |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4657 | PHI->addRegOperand(PHINodesToUpdate[i].second, false); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4658 | PHI->addMachineBasicBlockOperand(BB); |
| 4659 | } |
| 4660 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4661 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4662 | |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 4663 | // If the JumpTable record is filled in, then we need to emit a jump table. |
| 4664 | // Updating the PHI nodes is tricky in this case, since we need to determine |
| 4665 | // whether the PHI is a successor of the range check MBB or the jump table MBB |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4666 | for (unsigned i = 0, e = JTCases.size(); i != e; ++i) { |
| 4667 | // Lower header first, if it wasn't already lowered |
| 4668 | if (!JTCases[i].first.Emitted) { |
| 4669 | SelectionDAG HSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 4670 | CurDAG = &HSDAG; |
| 4671 | SelectionDAGLowering HSDL(HSDAG, TLI, FuncInfo); |
| 4672 | // Set the current basic block to the mbb we wish to insert the code into |
| 4673 | BB = JTCases[i].first.HeaderBB; |
| 4674 | HSDL.setCurrentBasicBlock(BB); |
| 4675 | // Emit the code |
| 4676 | HSDL.visitJumpTableHeader(JTCases[i].second, JTCases[i].first); |
| 4677 | HSDAG.setRoot(HSDL.getRoot()); |
| 4678 | CodeGenAndEmitDAG(HSDAG); |
| 4679 | } |
| 4680 | |
| 4681 | SelectionDAG JSDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
| 4682 | CurDAG = &JSDAG; |
| 4683 | SelectionDAGLowering JSDL(JSDAG, TLI, FuncInfo); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4684 | // Set the current basic block to the mbb we wish to insert the code into |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4685 | BB = JTCases[i].second.MBB; |
| 4686 | JSDL.setCurrentBasicBlock(BB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4687 | // Emit the code |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4688 | JSDL.visitJumpTable(JTCases[i].second); |
| 4689 | JSDAG.setRoot(JSDL.getRoot()); |
| 4690 | CodeGenAndEmitDAG(JSDAG); |
| 4691 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4692 | // Update PHI Nodes |
| 4693 | for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 4694 | MachineInstr *PHI = PHINodesToUpdate[pi].first; |
| 4695 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 4696 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4697 | "This is not a machine PHI node that we are updating!"); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4698 | if (PHIBB == JTCases[i].second.Default) { |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4699 | PHI->addRegOperand(PHINodesToUpdate[pi].second, false); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 4700 | PHI->addMachineBasicBlockOperand(JTCases[i].first.HeaderBB); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 4701 | } |
| 4702 | if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) { |
Chris Lattner | 09e4606 | 2006-09-05 02:31:13 +0000 | [diff] [blame] | 4703 | PHI->addRegOperand(PHINodesToUpdate[pi].second, false); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 4704 | PHI->addMachineBasicBlockOperand(BB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 4709 | // If the switch block involved a branch to one of the actual successors, we |
| 4710 | // need to update PHI nodes in that block. |
| 4711 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 4712 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 4713 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 4714 | "This is not a machine PHI node that we are updating!"); |
| 4715 | if (BB->isSuccessor(PHI->getParent())) { |
| 4716 | PHI->addRegOperand(PHINodesToUpdate[i].second, false); |
| 4717 | PHI->addMachineBasicBlockOperand(BB); |
| 4718 | } |
| 4719 | } |
| 4720 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4721 | // If we generated any switch lowering information, build and codegen any |
| 4722 | // additional DAGs necessary. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4723 | for (unsigned i = 0, e = SwitchCases.size(); i != e; ++i) { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 4724 | SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate<MachineModuleInfo>()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4725 | CurDAG = &SDAG; |
| 4726 | SelectionDAGLowering SDL(SDAG, TLI, FuncInfo); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4727 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4728 | // Set the current basic block to the mbb we wish to insert the code into |
| 4729 | BB = SwitchCases[i].ThisBB; |
| 4730 | SDL.setCurrentBasicBlock(BB); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4731 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4732 | // Emit the code |
| 4733 | SDL.visitSwitchCase(SwitchCases[i]); |
| 4734 | SDAG.setRoot(SDL.getRoot()); |
| 4735 | CodeGenAndEmitDAG(SDAG); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4736 | |
| 4737 | // Handle any PHI nodes in successors of this chunk, as if we were coming |
| 4738 | // from the original BB before switch expansion. Note that PHI nodes can |
| 4739 | // occur multiple times in PHINodesToUpdate. We have to be very careful to |
| 4740 | // handle them the right number of times. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4741 | while ((BB = SwitchCases[i].TrueBB)) { // Handle LHS and RHS. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4742 | for (MachineBasicBlock::iterator Phi = BB->begin(); |
| 4743 | Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){ |
| 4744 | // This value for this PHI node is recorded in PHINodesToUpdate, get it. |
| 4745 | for (unsigned pn = 0; ; ++pn) { |
| 4746 | assert(pn != PHINodesToUpdate.size() && "Didn't find PHI entry!"); |
| 4747 | if (PHINodesToUpdate[pn].first == Phi) { |
| 4748 | Phi->addRegOperand(PHINodesToUpdate[pn].second, false); |
| 4749 | Phi->addMachineBasicBlockOperand(SwitchCases[i].ThisBB); |
| 4750 | break; |
| 4751 | } |
| 4752 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4753 | } |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4754 | |
| 4755 | // Don't process RHS if same block as LHS. |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4756 | if (BB == SwitchCases[i].FalseBB) |
| 4757 | SwitchCases[i].FalseBB = 0; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 4758 | |
| 4759 | // If we haven't handled the RHS, do so now. Otherwise, we're done. |
Chris Lattner | 2452595 | 2006-10-24 18:07:37 +0000 | [diff] [blame] | 4760 | SwitchCases[i].TrueBB = SwitchCases[i].FalseBB; |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4761 | SwitchCases[i].FalseBB = 0; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 4762 | } |
Chris Lattner | 57ab659 | 2006-10-24 17:57:59 +0000 | [diff] [blame] | 4763 | assert(SwitchCases[i].TrueBB == 0 && SwitchCases[i].FalseBB == 0); |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 4764 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 4765 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4766 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4767 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4768 | //===----------------------------------------------------------------------===// |
| 4769 | /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each |
| 4770 | /// target node in the graph. |
| 4771 | void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) { |
| 4772 | if (ViewSchedDAGs) DAG.viewGraph(); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 4773 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 4774 | RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault(); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4775 | |
| 4776 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 4777 | Ctor = ISHeuristic; |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 4778 | RegisterScheduler::setDefault(Ctor); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 4779 | } |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 4780 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4781 | ScheduleDAG *SL = Ctor(this, &DAG, BB); |
Chris Lattner | a3818e6 | 2006-01-21 19:12:11 +0000 | [diff] [blame] | 4782 | BB = SL->Run(); |
Evan Cheng | cccf123 | 2006-02-04 06:49:00 +0000 | [diff] [blame] | 4783 | delete SL; |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 4784 | } |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4785 | |
Chris Lattner | 03fc53c | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 4786 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4787 | HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() { |
| 4788 | return new HazardRecognizer(); |
| 4789 | } |
| 4790 | |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 4791 | //===----------------------------------------------------------------------===// |
| 4792 | // Helper functions used by the generated instruction selector. |
| 4793 | //===----------------------------------------------------------------------===// |
| 4794 | // Calls to these methods are generated by tblgen. |
| 4795 | |
| 4796 | /// CheckAndMask - The isel is trying to match something like (and X, 255). If |
| 4797 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 4798 | /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value |
| 4799 | /// specified in the .td file (e.g. 255). |
| 4800 | bool SelectionDAGISel::CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, |
| 4801 | int64_t DesiredMaskS) { |
| 4802 | uint64_t ActualMask = RHS->getValue(); |
| 4803 | uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType()); |
| 4804 | |
| 4805 | // If the actual mask exactly matches, success! |
| 4806 | if (ActualMask == DesiredMask) |
| 4807 | return true; |
| 4808 | |
| 4809 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
| 4810 | if (ActualMask & ~DesiredMask) |
| 4811 | return false; |
| 4812 | |
| 4813 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 4814 | // either already zero or is not demanded. Check for known zero input bits. |
| 4815 | uint64_t NeededMask = DesiredMask & ~ActualMask; |
| 4816 | if (getTargetLowering().MaskedValueIsZero(LHS, NeededMask)) |
| 4817 | return true; |
| 4818 | |
| 4819 | // TODO: check to see if missing bits are just not demanded. |
| 4820 | |
| 4821 | // Otherwise, this pattern doesn't match. |
| 4822 | return false; |
| 4823 | } |
| 4824 | |
| 4825 | /// CheckOrMask - The isel is trying to match something like (or X, 255). If |
| 4826 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 4827 | /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value |
| 4828 | /// specified in the .td file (e.g. 255). |
| 4829 | bool SelectionDAGISel::CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, |
| 4830 | int64_t DesiredMaskS) { |
| 4831 | uint64_t ActualMask = RHS->getValue(); |
| 4832 | uint64_t DesiredMask =DesiredMaskS & MVT::getIntVTBitMask(LHS.getValueType()); |
| 4833 | |
| 4834 | // If the actual mask exactly matches, success! |
| 4835 | if (ActualMask == DesiredMask) |
| 4836 | return true; |
| 4837 | |
| 4838 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
| 4839 | if (ActualMask & ~DesiredMask) |
| 4840 | return false; |
| 4841 | |
| 4842 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 4843 | // either already zero or is not demanded. Check for known zero input bits. |
| 4844 | uint64_t NeededMask = DesiredMask & ~ActualMask; |
| 4845 | |
| 4846 | uint64_t KnownZero, KnownOne; |
| 4847 | getTargetLowering().ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne); |
| 4848 | |
| 4849 | // If all the missing bits in the or are already known to be set, match! |
| 4850 | if ((NeededMask & KnownOne) == NeededMask) |
| 4851 | return true; |
| 4852 | |
| 4853 | // TODO: check to see if missing bits are just not demanded. |
| 4854 | |
| 4855 | // Otherwise, this pattern doesn't match. |
| 4856 | return false; |
| 4857 | } |
| 4858 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 4859 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4860 | /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated |
| 4861 | /// by tblgen. Others should not call it. |
| 4862 | void SelectionDAGISel:: |
| 4863 | SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) { |
| 4864 | std::vector<SDOperand> InOps; |
| 4865 | std::swap(InOps, Ops); |
| 4866 | |
| 4867 | Ops.push_back(InOps[0]); // input chain. |
| 4868 | Ops.push_back(InOps[1]); // input asm string. |
| 4869 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4870 | unsigned i = 2, e = InOps.size(); |
| 4871 | if (InOps[e-1].getValueType() == MVT::Flag) |
| 4872 | --e; // Don't process a flag operand if it is here. |
| 4873 | |
| 4874 | while (i != e) { |
| 4875 | unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue(); |
| 4876 | if ((Flags & 7) != 4 /*MEM*/) { |
| 4877 | // Just skip over this operand, copying the operands verbatim. |
| 4878 | Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1); |
| 4879 | i += (Flags >> 3) + 1; |
| 4880 | } else { |
| 4881 | assert((Flags >> 3) == 1 && "Memory operand with multiple values?"); |
| 4882 | // Otherwise, this is a memory operand. Ask the target to select it. |
| 4883 | std::vector<SDOperand> SelOps; |
| 4884 | if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4885 | cerr << "Could not match memory address. Inline asm failure!\n"; |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4886 | exit(1); |
| 4887 | } |
| 4888 | |
| 4889 | // Add this to the output node. |
Chris Lattner | 36d4396 | 2006-12-16 21:14:48 +0000 | [diff] [blame] | 4890 | Ops.push_back(DAG.getTargetConstant(4/*MEM*/ | (SelOps.size() << 3), |
| 4891 | MVT::i32)); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 4892 | Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); |
| 4893 | i += 2; |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | // Add the flag input back if present. |
| 4898 | if (e != InOps.size()) |
| 4899 | Ops.push_back(InOps.back()); |
| 4900 | } |