llvm.org GIT mirror llvm / 37efe67
JumpTable support! What this represents is working asm and jit support for x86 and ppc for 100% dense switch statements when relocations are non-PIC. This support will be extended and enhanced in the coming days to support PIC, and less dense forms of jump tables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27947 91177308-0d34-0410-b5e6-96231b3b80d8 Nate Begeman 14 years ago
40 changed file(s) with 721 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
139139 /// before emitting the constant pool for a function.
140140 const char *ConstantPoolSection; // Defaults to "\t.section .rodata\n"
141141
142 /// JumpTableSection - This is the section that we SwitchToSection right
143 /// before emitting the jump tables for a function.
144 const char *JumpTableSection; // Defaults to "\t.section .rodata\n"
145
142146 /// StaticCtorsSection - This is the directive that is emitted to switch to
143147 /// a section to emit the static constructor list.
144148 /// Defaults to "\t.section .ctors,\"aw\",@progbits".
230234 ///
231235 void EmitConstantPool(MachineConstantPool *MCP);
232236
237 /// EmitJumpTableInfo - Print assembly representations of the jump tables
238 /// used by the current function to the current output stream.
239 ///
240 void EmitJumpTableInfo(MachineJumpTableInfo *MJTI);
241
233242 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
234243 /// special global used by LLVM. If so, emit it and return true, otherwise
235244 /// do nothing and return false.
256265 /// printInlineAsm - This method formats and prints the specified machine
257266 /// instruction that is an inline asm.
258267 void printInlineAsm(const MachineInstr *MI) const;
268
269 /// printBasicBlockLabel - This method prints the label for the specified
270 /// MachineBasicBlock
271 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB) const;
272
259273 private:
260274 void EmitXXStructorList(Constant *List);
261275
1717 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
1818
1919 #include "llvm/Support/DataTypes.h"
20 #include
2021
2122 namespace llvm {
2223
2324 class MachineBasicBlock;
2425 class MachineConstantPool;
26 class MachineJumpTableInfo;
2527 class MachineFunction;
2628 class MachineRelocation;
2729 class Value;
4648 /// for the function.
4749 virtual void emitConstantPool(MachineConstantPool *MCP) {}
4850
51 /// initJumpTableInfo - This callback is invoked by the JIT to allocate the
52 /// necessary memory to hold the jump tables.
53 virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI) {}
54
55 /// emitJumpTableInfo - This callback is invoked to output the jump tables
56 /// for the function. In addition to a pointer to the MachineJumpTableInfo,
57 /// this function also takes a map of MBBs to addresses, so that the final
58 /// addresses of the MBBs can be written to the jump tables.
59 virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
60 std::map &MBBM) {}
61
4962 /// startFunctionStub - This callback is invoked when the JIT needs the
5063 /// address of a function that has not been code generated yet. The StubSize
5164 /// specifies the total size required by the stub. Stubs are not allowed to
93106 //
94107 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) = 0;
95108
109 // getJumpTablelEntryAddress - Return the address of the jump table with index
110 // 'Index' in the function that last called initJumpTableInfo.
111 //
112 virtual uint64_t getJumpTableEntryAddress(unsigned Index) = 0;
113
96114 // allocateGlobal - Allocate some space for a global variable.
97115 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) = 0;
98116
2828 class SSARegMap;
2929 class MachineFrameInfo;
3030 class MachineConstantPool;
31 class MachineJumpTableInfo;
3132
3233 // ilist_traits
3334 template <>
9293
9394 // Keep track of constants which are spilled to memory
9495 MachineConstantPool *ConstantPool;
96
97 // Keep track of jump tables for switch instructions
98 MachineJumpTableInfo *JumpTableInfo;
9599
96100 // Function-level unique numbering for MachineBasicBlocks. When a
97101 // MachineBasicBlock is inserted into a MachineFunction is it automatically
137141 ///
138142 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
139143
144 /// getJumpTableInfo - Return the jump table info object for the current
145 /// function. This object contains information about jump tables for switch
146 /// instructions in the current function.
147 ///
148 MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
149
140150 /// getConstantPool - Return the constant pool object for the current
141151 /// function.
142152 ///
104104 MO_MachineBasicBlock, // MachineBasicBlock reference
105105 MO_FrameIndex, // Abstract Stack Frame Index
106106 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
107 MO_JumpTableIndex, // Address of indexed Jump Table for switch
107108 MO_ExternalSymbol, // Name of external global symbol
108109 MO_GlobalAddress // Address of a global value
109110 };
241242 }
242243 bool isFrameIndex() const { return opType == MO_FrameIndex; }
243244 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
245 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
244246 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
245247 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
246248
282284 }
283285 unsigned getConstantPoolIndex() const {
284286 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
287 return (unsigned)contents.immedVal;
288 }
289 unsigned getJumpTableIndex() const {
290 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
285291 return (unsigned)contents.immedVal;
286292 }
287293 GlobalValue *getGlobal() const {
347353 }
348354
349355 void setOffset(int Offset) {
350 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
356 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
357 isJumpTableIndex()) &&
351358 "Wrong MachineOperand accessor");
352359 extra.offset = Offset;
353360 }
610617 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
611618 }
612619
620 /// addJumpTableIndexOperand - Add a jump table object index to the
621 /// instruction.
622 ///
623 void addJumpTableIndexOperand(unsigned I) {
624 assert(!OperandsComplete() &&
625 "Trying to add an operand to a machine instr that is already done!");
626 operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
627 }
628
613629 void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative, int Offset) {
614630 assert(!OperandsComplete() &&
615631 "Trying to add an operand to a machine instr that is already done!");
0 //===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables --*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Nate Begeman and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The MachineJumpTableInfo class keeps track of jump tables referenced by
10 // lowered switch instructions in the MachineFunction.
11 //
12 // Instructions reference the address of these jump tables through the use of
13 // MO_JumpTableIndex values. When emitting assembly or machine code, these
14 // virtual address references are converted to refer to the address of the
15 // function jump tables.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
20 #define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
21
22 #include "llvm/Target/TargetData.h"
23 #include
24 #include
25
26 namespace llvm {
27
28 class MachineBasicBlock;
29
30 /// MachineJumpTableEntry - One jump table in the jump table info.
31 ///
32 struct MachineJumpTableEntry {
33 /// MBBs - The vector of basic blocks from which to create the jump table.
34 std::vector MBBs;
35
36 MachineJumpTableEntry(std::vector &M) : MBBs(M) {}
37 };
38
39 class MachineJumpTableInfo {
40 const TargetData &TD;
41 std::vector JumpTables;
42 public:
43 MachineJumpTableInfo(const TargetData &td) : TD(td) {}
44
45 /// getJumpTableIndex - Create a new jump table or return an existing one.
46 ///
47 unsigned getJumpTableIndex(std::vector &DestBBs);
48
49 /// isEmpty - Return true if there are no jump tables.
50 ///
51 bool isEmpty() const { return JumpTables.empty(); }
52
53 const std::vector &getJumpTables() const {
54 return JumpTables;
55 }
56
57 unsigned getEntrySize() const { return TD.getPointerSize(); }
58 unsigned getAlignment() const { return TD.getPointerAlignment(); }
59
60 /// print - Used by the MachineFunction printer to print information about
61 /// jump tables. Implemented in MachineFunction.cpp
62 ///
63 void print(std::ostream &OS) const;
64
65 /// dump - Call print(std::cerr) to be called from the debugger.
66 ///
67 void dump() const;
68 };
69
70 } // End llvm namespace
71
72 #endif
9999 if (isa(Node)) return true;
100100 if (isa(Node)) return true;
101101 if (isa(Node)) return true;
102 if (isa(Node)) return true;
102103 if (isa(Node)) return true;
103104 return false;
104105 }
119119 int offset = 0);
120120 SDOperand getFrameIndex(int FI, MVT::ValueType VT);
121121 SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
122 SDOperand getJumpTable(int JTI, MVT::ValueType VT);
123 SDOperand getTargetJumpTable(int JTI, MVT::ValueType VT);
122124 SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
123125 unsigned Alignment=0, int offset = 0);
124126 SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
467469 std::map, SDNode*> TargetConstants;
468470 std::map, SDNode*> ConstantFPs;
469471 std::map, SDNode*> TargetConstantFPs;
470 std::map FrameIndices, TargetFrameIndices;
472 std::map FrameIndices, TargetFrameIndices, JumpTableIndices,
473 TargetJumpTableIndices;
471474 std::map
472475 std::pair >, SDNode*> ConstantPoolIndices;
473476 std::map
1717 #include "llvm/Pass.h"
1818 #include "llvm/Constant.h"
1919 #include "llvm/CodeGen/SelectionDAGNodes.h"
20 #include
2021
2122 namespace llvm {
2223 class SelectionDAG;
3940 SelectionDAG *CurDAG;
4041 MachineBasicBlock *BB;
4142
42 SelectionDAGISel(TargetLowering &tli) : TLI(tli) {}
43 SelectionDAGISel(TargetLowering &tli) : TLI(tli), JT(0,0,0) {}
4344
4445 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
4546
8687 // ThisBB - the blcok into which to emit the code for the setcc and branches
8788 MachineBasicBlock *ThisBB;
8889 };
90 struct JumpTable {
91 JumpTable(unsigned R, unsigned J, MachineBasicBlock *me) : Reg(R), JTI(J),
92 MBB(me) {}
93 // Reg - the virtual register containing the index of the jump table entry
94 // to jump to.
95 unsigned Reg;
96 // JTI - the JumpTableIndex for this jump table in the function.
97 unsigned JTI;
98 // MBB - the MBB into which to emit the code for the indirect jump.
99 MachineBasicBlock *MBB;
100 // SuccMBBs - a vector of unique successor MBBs used for updating CFG info
101 // and PHI nodes.
102 std::set SuccMBBs;
103 };
89104
90105 protected:
91106 /// Pick a safe ordering and emit instructions for each target node in the
113128 /// SwitchCases - Vector of CaseBlock structures used to communicate
114129 /// SwitchInst code generation information.
115130 std::vector SwitchCases;
131
132 /// JT - Record which holds necessary information for emitting a jump table
133 JumpTable JT;
116134 };
117135
118136 }
6464 // Various leaf nodes.
6565 STRING, BasicBlock, VALUETYPE, CONDCODE, Register,
6666 Constant, ConstantFP,
67 GlobalAddress, FrameIndex, ConstantPool, ExternalSymbol,
67 GlobalAddress, FrameIndex, JumpTable, ConstantPool, ExternalSymbol,
6868
6969 // TargetConstant* - Like Constant*, but the DAG does not do any folding or
7070 // simplification of the constant.
7676 // dag, turning into a GlobalAddress operand.
7777 TargetGlobalAddress,
7878 TargetFrameIndex,
79 TargetJumpTable,
7980 TargetConstantPool,
8081 TargetExternalSymbol,
8182
387388 // operand, the second is the MBB to branch to.
388389 BR,
389390
391 // BRIND - Indirect branch. The first operand is the chain, the second
392 // is the value to branch to, which must be of the same type as the target's
393 // pointer type.
394 BRIND,
395
390396 // BRCOND - Conditional branch. The first operand is the chain,
391397 // the second is the condition, the third is the block to branch
392398 // to if the condition is true.
11641170 }
11651171 };
11661172
1173 class JumpTableSDNode : public SDNode {
1174 int JTI;
1175 protected:
1176 friend class SelectionDAG;
1177 JumpTableSDNode(int jti, MVT::ValueType VT, bool isTarg)
1178 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable, VT),
1179 JTI(jti) {}
1180 public:
1181
1182 int getIndex() const { return JTI; }
1183
1184 static bool classof(const JumpTableSDNode *) { return true; }
1185 static bool classof(const SDNode *N) {
1186 return N->getOpcode() == ISD::JumpTable ||
1187 N->getOpcode() == ISD::TargetJumpTable;
1188 }
1189 };
1190
11671191 class ConstantPoolSDNode : public SDNode {
11681192 Constant *C;
11691193 int Offset;
1616 #include "llvm/Constants.h"
1717 #include "llvm/Module.h"
1818 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineJumpTableInfo.h"
1920 #include "llvm/Support/Mangler.h"
2021 #include "llvm/Support/MathExtras.h"
2122 #include "llvm/Target/TargetMachine.h"
4546 AlignmentIsInBytes(true),
4647 SwitchToSectionDirective("\t.section\t"),
4748 ConstantPoolSection("\t.section .rodata\n"),
49 JumpTableSection("\t.section .rodata\n"),
4850 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
4951 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
5052 LCOMMDirective(0),
122124 unsigned ValEnd = CP[i].Offset + EntSize;
123125 // Emit inter-object padding for alignment.
124126 EmitZeros(CP[i+1].Offset-ValEnd);
127 }
128 }
129 }
130
131 /// EmitJumpTableInfo - Print assembly representations of the jump tables used
132 /// by the current function to the current output stream.
133 ///
134 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
135 const std::vector &JT = MJTI->getJumpTables();
136 if (JT.empty()) return;
137 const TargetData &TD = TM.getTargetData();
138
139 // FIXME: someday we need to handle PIC jump tables
140 assert((TM.getRelocationModel() == Reloc::Static ||
141 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
142 "Unhandled relocation model emitting jump table information!");
143
144 SwitchSection(JumpTableSection, 0);
145 EmitAlignment(Log2_32(TD.getPointerAlignment()));
146 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
147 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
148 << ":\n";
149 const std::vector &JTBBs = JT[i].MBBs;
150 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
151 O << Data32bitsDirective << ' ';
152 printBasicBlockLabel(JTBBs[ii]);
153 O << '\n';
125154 }
126155 }
127156 }
653682 // Target doesn't support this yet!
654683 return true;
655684 }
685
686 /// printBasicBlockLabel - This method prints the label for the specified
687 /// MachineBasicBlock
688 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB) const {
689 O << PrivateGlobalPrefix << "LBB"
690 << Mang->getValueName(MBB->getParent()->getFunction())
691 << "_" << MBB->getNumber() << '\t' << CommentString
692 << MBB->getBasicBlock()->getName();
693 }
8383 assert(0 && "CP not implementated yet!");
8484 return 0;
8585 }
86
86 virtual uint64_t getJumpTableEntryAddress(unsigned Index) {
87 assert(0 && "JT not implementated yet!");
88 return 0;
89 }
8790 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) {
8891 assert(0 && "Globals not implemented yet!");
8992 return 0;
5555 { return 0; }
5656
5757 uint64_t getConstantPoolEntryAddress(unsigned Num) { return 0; }
58 uint64_t getJumpTableEntryAddress(unsigned Num) { return 0; }
5859 uint64_t getCurrentPCValue() { return 0; }
5960 uint64_t getCurrentPCOffset() { return 0; }
6061 };
9697 void emitConstantPool(MachineConstantPool *MCP) {
9798 MCE.emitConstantPool(MCP);
9899 }
99
100 void initJumpTableInfo(MachineJumpTableInfo *MJTI) {
101 MCE.initJumpTableInfo(MJTI);
102 }
103 void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
104 std::map &MBBM) {
105 MCE.emitJumpTableInfo(MJTI, MBBM);
106 }
107
100108 void startFunctionStub(unsigned StubSize) {
101109 MCE.startFunctionStub(StubSize);
102110 }
145153 uint64_t getConstantPoolEntryAddress(unsigned Num) {
146154 return MCE.getConstantPoolEntryAddress(Num);
147155 }
148
156 uint64_t getJumpTableEntryAddress(unsigned Num) {
157 return MCE.getJumpTableEntryAddress(Num);
158 }
149159 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment)
150160 { return MCE.allocateGlobal(size, alignment); }
151161
1717 #include "llvm/CodeGen/SSARegMap.h"
1818 #include "llvm/CodeGen/MachineFrameInfo.h"
1919 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
2021 #include "llvm/CodeGen/Passes.h"
2122 #include "llvm/Target/TargetMachine.h"
2223 #include "llvm/Target/TargetFrameInfo.h"
112113 MFInfo = 0;
113114 FrameInfo = new MachineFrameInfo();
114115 ConstantPool = new MachineConstantPool(TM.getTargetData());
116 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
115117 BasicBlocks.Parent = this;
116118 }
117119
121123 delete MFInfo;
122124 delete FrameInfo;
123125 delete ConstantPool;
126 delete JumpTableInfo;
124127 delete[] UsedPhysRegs;
125128 }
126129
131134
132135 // Print Frame Information
133136 getFrameInfo()->print(*this, OS);
137
138 // Print JumpTable Information
139 getJumpTableInfo()->print(OS);
134140
135141 // Print Constant Pool
136142 getConstantPool()->print(OS);
333339
334340
335341 //===----------------------------------------------------------------------===//
342 // MachineJumpTableInfo implementation
343 //===----------------------------------------------------------------------===//
344
345 /// getJumpTableIndex - Create a new jump table entry in the jump table info
346 /// or return an existing one.
347 ///
348 unsigned MachineJumpTableInfo::getJumpTableIndex(
349 std::vector &DestBBs) {
350 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
351 if (JumpTables[i].MBBs == DestBBs)
352 return i;
353
354 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
355 return JumpTables.size()-1;
356 }
357
358
359 void MachineJumpTableInfo::print(std::ostream &OS) const {
360 // FIXME: this is lame, maybe we could print out the MBB numbers or something
361 // like {1, 2, 4, 5, 3, 0}
362 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
363 OS << " has " << JumpTables[i].MBBs.size()
364 << " entries\n";
365 }
366 }
367
368 void MachineJumpTableInfo::dump() const { print(std::cerr); }
369
370
371 //===----------------------------------------------------------------------===//
336372 // MachineConstantPool implementation
337373 //===----------------------------------------------------------------------===//
338374
237237 case MachineOperand::MO_ConstantPoolIndex:
238238 OS << "";
239239 break;
240 case MachineOperand::MO_JumpTableIndex:
241 OS << "";
242 break;
240243 case MachineOperand::MO_GlobalAddress:
241244 OS << "getName();
242245 if (MO.getOffset()) OS << "+" << MO.getOffset();
376379 case MachineOperand::MO_ConstantPoolIndex:
377380 OS << "";
378381 break;
382 case MachineOperand::MO_JumpTableIndex:
383 OS << "";
384 break;
379385 case MachineOperand::MO_GlobalAddress:
380386 OS << "getName() << ">";
381387 break;
509509 case ISD::Register:
510510 case ISD::BasicBlock:
511511 case ISD::TargetFrameIndex:
512 case ISD::TargetJumpTable:
512513 case ISD::TargetConstant:
513514 case ISD::TargetConstantFP:
514515 case ISD::TargetConstantPool:
551552 abort();
552553 case ISD::GlobalAddress:
553554 case ISD::ExternalSymbol:
554 case ISD::ConstantPool: // Nothing to do.
555 case ISD::ConstantPool:
556 case ISD::JumpTable: // Nothing to do.
555557 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
556558 default: assert(0 && "This action is not supported yet!");
557559 case TargetLowering::Custom:
11821184
11831185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
11841186 break;
1185
1187 case ISD::BRIND:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1189 // Ensure that libcalls are emitted before a branch.
1190 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1191 Tmp1 = LegalizeOp(Tmp1);
1192 LastCALLSEQ_END = DAG.getEntryNode();
1193
1194 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1195 default: assert(0 && "Indirect target must be legal type (pointer)!");
1196 case Legal:
1197 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1198 break;
1199 }
1200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1201 break;
11861202 case ISD::BRCOND:
11871203 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
11881204 // Ensure that libcalls are emitted before a return.
116116 } else if (FrameIndexSDNode *FI =
117117 dyn_cast(Op)) {
118118 MI->addFrameIndexOperand(FI->getIndex());
119 } else if (JumpTableSDNode *JT =
120 dyn_cast(Op)) {
121 MI->addJumpTableIndexOperand(JT->getIndex());
119122 } else if (ConstantPoolSDNode *CP =
120123 dyn_cast(Op)) {
121124 int Offset = CP->getOffset();
385385 break;
386386 case ISD::TargetFrameIndex:
387387 Erased = TargetFrameIndices.erase(cast(N)->getIndex());
388 break;
389 case ISD::JumpTable:
390 Erased = JumpTableIndices.erase(cast(N)->getIndex());
391 break;
392 case ISD::TargetJumpTable:
393 Erased =
394 TargetJumpTableIndices.erase(cast(N)->getIndex());
388395 break;
389396 case ISD::ConstantPool:
390397 Erased = ConstantPoolIndices.
736743 SDNode *&N = TargetFrameIndices[FI];
737744 if (N) return SDOperand(N, 0);
738745 N = new FrameIndexSDNode(FI, VT, true);
746 AllNodes.push_back(N);
747 return SDOperand(N, 0);
748 }
749
750 SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
751 SDNode *&N = JumpTableIndices[JTI];
752 if (N) return SDOperand(N, 0);
753 N = new JumpTableSDNode(JTI, VT, false);
754 AllNodes.push_back(N);
755 return SDOperand(N, 0);
756 }
757
758 SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
759 SDNode *&N = TargetJumpTableIndices[JTI];
760 if (N) return SDOperand(N, 0);
761 N = new JumpTableSDNode(JTI, VT, true);
739762 AllNodes.push_back(N);
740763 return SDOperand(N, 0);
741764 }
27412764 case ISD::ConstantFP: return "ConstantFP";
27422765 case ISD::GlobalAddress: return "GlobalAddress";
27432766 case ISD::FrameIndex: return "FrameIndex";
2767 case ISD::JumpTable: return "JumpTable";
27442768 case ISD::ConstantPool: return "ConstantPool";
27452769 case ISD::ExternalSymbol: return "ExternalSymbol";
27462770 case ISD::INTRINSIC_WO_CHAIN: {
27582782 case ISD::TargetConstantFP:return "TargetConstantFP";
27592783 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
27602784 case ISD::TargetFrameIndex: return "TargetFrameIndex";
2785 case ISD::TargetJumpTable: return "TargetJumpTable";
27612786 case ISD::TargetConstantPool: return "TargetConstantPool";
27622787 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
27632788
28482873
28492874 // Control flow instructions
28502875 case ISD::BR: return "br";
2876 case ISD::BRIND: return "brind";
28512877 case ISD::BRCOND: return "brcond";
28522878 case ISD::BR_CC: return "br_cc";
28532879 case ISD::RET: return "ret";
2626 #include "llvm/CodeGen/MachineDebugInfo.h"
2727 #include "llvm/CodeGen/MachineFunction.h"
2828 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
2930 #include "llvm/CodeGen/MachineInstrBuilder.h"
3031 #include "llvm/CodeGen/SelectionDAG.h"
3132 #include "llvm/CodeGen/SSARegMap.h"
397398 /// SwitchCases - Vector of CaseBlock structures used to communicate
398399 /// SwitchInst code generation information.
399400 std::vector SwitchCases;
401 SelectionDAGISel::JumpTable JT;
400402
401403 /// FuncInfo - Information about the function as a whole.
402404 ///
405407 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
406408 FunctionLoweringInfo &funcinfo)
407409 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
408 FuncInfo(funcinfo) {
410 JT(0,0,0), FuncInfo(funcinfo) {
409411 }
410412
411413 /// getRoot - Return the current virtual root of the Selection DAG.
473475
474476 // Helper for visitSwitch
475477 void visitSwitchCase(SelectionDAGISel::CaseBlock &CB);
478 void visitJumpTable(SelectionDAGISel::JumpTable &JT);
476479
477480 // These all get lowered before this pass.
478481 void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
815818 CurMBB->addSuccessor(CB.RHSBB);
816819 }
817820
821 /// visitSwitchCase - Emits the necessary code to represent a single node in
822 /// the binary search tree resulting from lowering a switch instruction.
823 void SelectionDAGLowering::visitJumpTable(SelectionDAGISel::JumpTable &JT) {
824 // FIXME: Need to emit different code for PIC vs. Non-PIC, specifically,
825 // we need to add the address of the jump table to the value loaded, since
826 // the entries in the jump table will be differences rather than absolute
827 // addresses.
828
829 // Emit the code for the jump table
830 MVT::ValueType PTy = TLI.getPointerTy();
831 unsigned PTyBytes = MVT::getSizeInBits(PTy)/8;
832 SDOperand Copy = DAG.getCopyFromReg(getRoot(), JT.Reg, PTy);
833 SDOperand IDX = DAG.getNode(ISD::MUL, PTy, Copy,
834 DAG.getConstant(PTyBytes, PTy));
835 SDOperand ADD = DAG.getNode(ISD::ADD, PTy, IDX, DAG.getJumpTable(JT.JTI,PTy));
836 SDOperand LD = DAG.getLoad(PTy, Copy.getValue(1), ADD, DAG.getSrcValue(0));
837 DAG.setRoot(DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD));
838
839 // Update successor info
840 for (std::set::iterator ii = JT.SuccMBBs.begin(),
841 ee = JT.SuccMBBs.end(); ii != ee; ++ii)
842 JT.MBB->addSuccessor(*ii);
843 }
844
818845 void SelectionDAGLowering::visitSwitch(SwitchInst &I) {
819846 // Figure out which block is immediately after the current one.
820847 MachineBasicBlock *NextBlock = 0;
849876 // search tree.
850877 Value *SV = I.getOperand(0);
851878 MachineBasicBlock *Default = FuncInfo.MBBMap[I.getDefaultDest()];
852
853 // Get the current MachineFunction and LLVM basic block, for use in creating
854 // and inserting new MBBs during the creation of the binary search tree.
879
880 // Get the MachineFunction which holds the current MBB. This is used during
881 // emission of jump tables, and when inserting any additional MBBs necessary
882 // to represent the switch.
855883 MachineFunction *CurMF = CurMBB->getParent();
856884 const BasicBlock *LLVMBB = CurMBB->getBasicBlock();
885 Reloc::Model Relocs = TLI.getTargetMachine().getRelocationModel();
886
887 // If the switch has more than 3 blocks, and is 100% dense, then emit a jump
888 // table rather than lowering the switch to a binary tree of conditional
889 // branches.
890 // FIXME: Make this work with 64 bit targets someday, possibly by always
891 // doing differences there so that entries stay 32 bits.
892 // FIXME: Make this work with PIC code
893 if (TLI.isOperationLegal(ISD::BRIND, TLI.getPointerTy()) &&
894 TLI.getPointerTy() == MVT::i32 &&
895 (Relocs == Reloc::Static || Relocs == Reloc::DynamicNoPIC) &&
896 Cases.size() > 3) {
897 uint64_t First = cast(Cases.front().first)->getRawValue();
898 uint64_t Last = cast(Cases.back().first)->getRawValue();
899
900 // Determine density
901 // FIXME: support sub-100% density
902 if (((Last - First) + 1ULL) == (uint64_t)Cases.size()) {
903 // Create a new basic block to hold the code for loading the address
904 // of the jump table, and jumping to it. Update successor information;
905 // we will either branch to the default case for the switch, or the jump
906 // table.
907 MachineBasicBlock *JumpTableBB = new MachineBasicBlock(LLVMBB);
908 CurMF->getBasicBlockList().insert(BBI, JumpTableBB);
909 CurMBB->addSuccessor(Default);
910 CurMBB->addSuccessor(JumpTableBB);
911
912 // Subtract the lowest switch case value from the value being switched on
913 // and conditional branch to default mbb if the result is greater than the
914 // difference between smallest and largest cases.
915 SDOperand SwitchOp = getValue(SV);
916 MVT::ValueType VT = SwitchOp.getValueType();
917 SDOperand SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
918 DAG.getConstant(First, VT));
919
920 // The SDNode we just created, which holds the value being switched on
921 // minus the the smallest case value, needs to be copied to a virtual
922 // register so it can be used as an index into the jump table in a
923 // subsequent basic block. This value may be smaller or larger than the
924 // target's pointer type, and therefore require extension or truncating.
925 if (VT > TLI.getPointerTy())
926 SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
927 else
928 SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
929 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
930 SDOperand CopyTo = DAG.getCopyToReg(getRoot(), JumpTableReg, SwitchOp);
931
932 // Emit the range check for the jump table, and branch to the default
933 // block for the switch statement if the value being switched on exceeds
934 // the largest case in the switch.
935 SDOperand CMP = DAG.getSetCC(TLI.getSetCCResultTy(), SUB,
936 DAG.getConstant(Last-First,VT), ISD::SETUGT);
937 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP,
938 DAG.getBasicBlock(Default)));
939
940 // Build a sorted vector of destination BBs, corresponding to each target
941 // of the switch.
942 // FIXME: need to insert DefaultMBB for each "hole" in the jump table,
943 // when we support jump tables with < 100% density.
944 std::set UniqueBBs;
945 std::vector DestBBs;
946 for (CaseItr ii = Cases.begin(), ee = Cases.end(); ii != ee; ++ii) {
947 DestBBs.push_back(ii->second);
948 UniqueBBs.insert(ii->second);
949 }
950 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
951
952 // Set the jump table information so that we can codegen it as a second
953 // MachineBasicBlock
954 JT.Reg = JumpTableReg;
955 JT.JTI = JTI;
956 JT.MBB = JumpTableBB;
957 JT.SuccMBBs = UniqueBBs;
958 return;
959 }
960 }
857961
858962 // Push the initial CaseRec onto the worklist
859963 std::vector CaseVec;
30213125 SDL.visit(*LLVMBB->getTerminator());
30223126
30233127 // Copy over any CaseBlock records that may now exist due to SwitchInst
3024 // lowering.
3128 // lowering, as well as any jump table information.
30253129 SwitchCases.clear();
30263130 SwitchCases = SDL.SwitchCases;
3131 JT = SDL.JT;
30273132
30283133 // Make sure the root of the DAG is up-to-date.
30293134 DAG.setRoot(SDL.getRoot());
30733178
30743179 // Next, now that we know what the last MBB the LLVM BB expanded is, update
30753180 // PHI nodes in successors.
3076 if (SwitchCases.empty()) {
3181 if (SwitchCases.empty() && JT.Reg == 0) {
30773182 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
30783183 MachineInstr *PHI = PHINodesToUpdate[i].first;
30793184 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
30803185 "This is not a machine PHI node that we are updating!");
30813186 PHI->addRegOperand(PHINodesToUpdate[i].second);
30823187 PHI->addMachineBasicBlockOperand(BB);
3188 }
3189 return;
3190 }
3191
3192 // If we need to emit a jump table,
3193 if (JT.Reg) {
3194 assert(SwitchCases.empty() && "Cannot have jump table and lowered switch");
3195 SelectionDAG SDAG(TLI, MF, getAnalysisToUpdate());
3196 CurDAG = &SDAG;
3197 SelectionDAGLowering SDL(SDAG, TLI, FuncInfo);
3198 // Set the current basic block to the mbb we wish to insert the code into
3199 BB = JT.MBB;
3200 SDL.setCurrentBasicBlock(BB);
3201 // Emit the code
3202 SDL.visitJumpTable(JT);
3203 SDAG.setRoot(SDL.getRoot());
3204 CodeGenAndEmitDAG(SDAG);
3205 // Update PHI Nodes
3206 for (unsigned pi = 0, pe = PHINodesToUpdate.size(); pi != pe; ++pi) {
3207 MachineInstr *PHI = PHINodesToUpdate[pi].first;
3208 MachineBasicBlock *PHIBB = PHI->getParent();
3209 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
3210 "This is not a machine PHI node that we are updating!");
3211 if (JT.SuccMBBs.find(PHIBB) != JT.SuccMBBs.end()) {
3212 PHI->addRegOperand(PHINodesToUpdate[pi].second);
3213 PHI->addMachineBasicBlockOperand(BB);
3214 }
30833215 }
30843216 return;
30853217 }
327327 return Result;
328328 }
329329
330 /// FIXME: document
330 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
331 /// is the address of the memory at which to store Val, cast to GenericValue *.
332 /// It is not a pointer to a GenericValue containing the address at which to
333 /// store Val.
331334 ///
332335 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
333336 const Type *Ty) {
1919 #include "llvm/CodeGen/MachineCodeEmitter.h"
2020 #include "llvm/CodeGen/MachineFunction.h"
2121 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
2223 #include "llvm/CodeGen/MachineRelocation.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
2325 #include "llvm/Target/TargetData.h"
2426 #include "llvm/Target/TargetJITInfo.h"
2527 #include "llvm/Support/Debug.h"
429431 /// ConstantPoolBase - A pointer to the first entry in the constant pool.
430432 ///
431433 void *ConstantPoolBase;
432 public:
434
435 /// ConstantPool - The constant pool for the current function.
436 ///
437 MachineJumpTableInfo *JumpTable;
438
439 /// JumpTableBase - A pointer to the first entry in the jump table.
440 ///
441 void *JumpTableBase;
442 public:
433443 JITEmitter(JIT &jit) : MemMgr(jit.getJITInfo().needsGOT()) {
434444 TheJIT = &jit;
435445 DEBUG(std::cerr <<
440450 virtual void startFunction(MachineFunction &F);
441451 virtual void finishFunction(MachineFunction &F);
442452 virtual void emitConstantPool(MachineConstantPool *MCP);
453 virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI);
454 virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
455 std::map &MBBM);
443456 virtual void startFunctionStub(unsigned StubSize);
444457 virtual void* finishFunctionStub(const Function *F);
445458 virtual void emitByte(unsigned char B);
453466 virtual uint64_t getCurrentPCValue();
454467 virtual uint64_t getCurrentPCOffset();
455468 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
469 virtual uint64_t getJumpTableEntryAddress(unsigned Entry);
456470 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment);
457471
458472 private:
582596 }
583597 }
584598
599 void JITEmitter::initJumpTableInfo(MachineJumpTableInfo *MJTI) {
600 const std::vector &JT = MJTI->getJumpTables();
601 if (JT.empty()) return;
602
603 unsigned Size = 0;
604 unsigned EntrySize = MJTI->getEntrySize();
605 for (unsigned i = 0, e = JT.size(); i != e; ++i)
606 Size += JT[i].MBBs.size() * EntrySize;
607
608 // Just allocate space for all the jump tables now. We will fix up the actual
609 // MBB entries in the tables after we emit the code for each block, since then
610 // we will know the final locations of the MBBs in memory.
611 JumpTable = MJTI;
612 JumpTableBase = MemMgr.allocateConstant(Size, MJTI->getAlignment());
613 }
614
615 void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI,
616 std::map &MBBM){
617 const std::vector &JT = MJTI->getJumpTables();
618 if (JT.empty()) return;
619
620 unsigned Offset = 0;
621 unsigned EntrySize = MJTI->getEntrySize();
622
623 // For each jump table, map each target in the jump table to the address of
624 // an emitted MachineBasicBlock.
625 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
626 const std::vector &MBBs = JT[i].MBBs;
627 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
628 uint64_t addr = MBBM[MBBs[mi]];
629 GenericValue addrgv;
630 const Type *Ty;
631 if (EntrySize == 4) {
632 addrgv.UIntVal = addr;
633 Ty = Type::UIntTy;
634 } else if (EntrySize == 8) {
635 addrgv.ULongVal = addr;
636 Ty = Type::ULongTy;
637 } else {
638 assert(0 && "Unhandled jump table entry size!");
639 abort();
640 }
641 // Store the address of the basic block for this jump table slot in the
642 // memory we allocated for the jump table in 'initJumpTableInfo'
643 void *ptr = (void *)((char *)JumpTableBase + Offset);
644 TheJIT->StoreValueToMemory(addrgv, (GenericValue *)ptr, Ty);
645 Offset += EntrySize;
646 }
647 }
648 }
649
585650 void JITEmitter::startFunctionStub(unsigned StubSize) {
586651 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
587652 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
618683 "Invalid ConstantPoolIndex!");
619684 return (intptr_t)ConstantPoolBase +
620685 ConstantPool->getConstants()[ConstantNum].Offset;
686 }
687
688 // getJumpTableEntryAddress - Return the address of the JumpTable with index
689 // 'Index' in the jumpp table that was last initialized with 'initJumpTableInfo'
690 //
691 uint64_t JITEmitter::getJumpTableEntryAddress(unsigned Index) {
692 const std::vector &JT = JumpTable->getJumpTables();
693 assert(Index < JT.size() && "Invalid jump table index!");
694
695 unsigned Offset = 0;
696 unsigned EntrySize = JumpTable->getEntrySize();
697
698 for (unsigned i = 0; i < Index; ++i)
699 Offset += JT[i].MBBs.size() * EntrySize;
700
701 return (uint64_t)((char *)JumpTableBase + Offset);
621702 }
622703
623704 unsigned char* JITEmitter::allocateGlobal(unsigned size, unsigned alignment)
114114 abort();
115115 return;
116116
117 case MachineOperand::MO_MachineBasicBlock: {
118 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
119 O << PrivateGlobalPrefix << "LBB"
120 << Mang->getValueName(MBBOp->getParent()->getFunction())
121 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
122 << MBBOp->getBasicBlock()->getName();
123 return;
124 }
117 case MachineOperand::MO_MachineBasicBlock:
118 printBasicBlockLabel(MO.getMachineBasicBlock());
119 return;
125120
126121 case MachineOperand::MO_ConstantPoolIndex:
127122 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
192192 addRegisterClass(MVT::f64, Alpha::F8RCRegisterClass);
193193 addRegisterClass(MVT::f32, Alpha::F4RCRegisterClass);
194194
195 setOperationAction(ISD::BRIND, MVT::i64, Expand);
195196 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
196197 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
197198
188188 case MachineOperand::MO_UnextendedImmed:
189189 O << /*(unsigned int)*/MO.getImmedValue();
190190 return;
191 case MachineOperand::MO_MachineBasicBlock: {
192 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
193 O << PrivateGlobalPrefix << "LBB"
194 << Mang->getValueName(MBBOp->getParent()->getFunction())
195 << "_" << MBBOp->getNumber () << "\t// "
196 << MBBOp->getBasicBlock ()->getName ();
197 return;
198 }
191 case MachineOperand::MO_MachineBasicBlock:
192 printBasicBlockLabel(MO.getMachineBasicBlock());
193 return;
199194 case MachineOperand::MO_PCRelativeDisp:
200195 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
201196 abort ();
3434 // register class for predicate registers
3535 addRegisterClass(MVT::i1, IA64::PRRegisterClass);
3636
37 setOperationAction(ISD::BRIND , MVT::i64, Expand);
3738 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
3839 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
3940
232232 printOperand(MI, OpNo+1);
233233 }
234234
235 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB) const;
236
235237 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
236238 virtual bool doFinalization(Module &M) = 0;
237239
276278 Data64bitsDirective = 0; // we can't emit a 64-bit unit
277279 AlignmentIsInBytes = false; // Alignment is by power of 2.
278280 ConstantPoolSection = "\t.const\t";
281 // FIXME: Conditionalize jump table section based on PIC
282 JumpTableSection = ".const";
279283 LCOMMDirective = "\t.lcomm\t";
280284 StaticCtorsSection = ".mod_init_func";
281285 StaticDtorsSection = ".mod_term_func";
372376 abort();
373377 return;
374378
375 case MachineOperand::MO_MachineBasicBlock: {
376 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
377 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
378 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
379 case MachineOperand::MO_MachineBasicBlock:
380 printBasicBlockLabel(MO.getMachineBasicBlock());
379381 return;
380 }
381
382 case MachineOperand::MO_JumpTableIndex:
383 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber()
384 << '_' << MO.getJumpTableIndex();
385 // FIXME: PIC relocation model
386 return;
382387 case MachineOperand::MO_ConstantPoolIndex:
383388 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
384389 << '_' << MO.getConstantPoolIndex();
499504 return;
500505 }
501506
507 void PPCAsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB) const {
508 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
509 << MBB->getNumber() << '\t' << CommentString
510 << MBB->getBasicBlock()->getName();
511 }
502512
503513 /// runOnMachineFunction - This uses the printMachineInstruction()
504514 /// method to print assembly for each instruction.
512522
513523 // Print out constants referenced by the function
514524 EmitConstantPool(MF.getConstantPool());
525
526 // Print out jump tables referenced by the function
527 EmitJumpTableInfo(MF.getJumpTableInfo());
515528
516529 // Print out labels for the function.
517530 const Function *F = MF.getFunction();
3333 // Tracks which instruction references which BasicBlock
3434 std::vector > BBRefs;
3535 // Tracks where each BasicBlock starts
36 std::maplong> BBLocations;
36 std::mapuint64_t> BBLocations;
3737
3838 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
3939 ///
9090 "JIT relocation model must be set to static or default!");
9191 MCE.startFunction(MF);
9292 MCE.emitConstantPool(MF.getConstantPool());
93 MCE.initJumpTableInfo(MF.getJumpTableInfo());
9394 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
9495 emitBasicBlock(*BB);
96 MCE.emitJumpTableInfo(MF.getJumpTableInfo(), BBLocations);
9597 MCE.finishFunction(MF);
9698
9799 // Resolve branches to BasicBlocks for the entire function
191193 } else if (MO.isMachineBasicBlock()) {
192194 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
193195 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
194 } else if (MO.isConstantPoolIndex()) {
195 unsigned index = MO.getConstantPoolIndex();
196 } else if (MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
197 if (MO.isConstantPoolIndex())
198 rv = MCE.getConstantPoolEntryAddress(MO.getConstantPoolIndex());
199 else
200 rv = MCE.getJumpTableEntryAddress(MO.getJumpTableIndex());
201
196202 unsigned Opcode = MI.getOpcode();
197 rv = MCE.getConstantPoolEntryAddress(index);
198203 if (Opcode == PPC::LIS || Opcode == PPC::ADDIS) {
199204 // lis wants hi16(addr)
200205 if ((short)rv < 0) rv += 1 << 16;
205210 // These load opcodes want lo16(addr)
206211 rv &= 0xffff;
207212 } else {
208 assert(0 && "Unknown constant pool using instruction!");
213 assert(0 && "Unknown constant pool or jump table using instruction!");
209214 }
210215 } else {
211216 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
519519 && "Cannot handle constant offsets yet!");
520520 Disp = N.getOperand(1).getOperand(0); // The global address.
521521 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
522 Disp.getOpcode() == ISD::TargetConstantPool);
522 Disp.getOpcode() == ISD::TargetConstantPool ||
523 Disp.getOpcode() == ISD::TargetJumpTable);
523524 Base = N.getOperand(0);
524525 return true; // [&g+r]
525526 }
660661 && "Cannot handle constant offsets yet!");
661662 Disp = N.getOperand(1).getOperand(0); // The global address.
662663 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
663 Disp.getOpcode() == ISD::TargetConstantPool);
664 Disp.getOpcode() == ISD::TargetConstantPool ||
665 Disp.getOpcode() == ISD::TargetJumpTable);
664666 Base = N.getOperand(0);
665667 return true; // [&g+r]
666668 }
12401242 N->getOperand(4), Chain);
12411243 return;
12421244 }
1245 case ISD::BRIND: {
1246 SDOperand Chain, Target;
1247 Select(Chain, N->getOperand(0));
1248 Select(Target,N->getOperand(1));
1249 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1250 Chain), 0);
1251 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1252 return;
1253 }
12431254 }
12441255
12451256 SelectCode(Result, Op);
122122 // appropriate instructions to materialize the address.
123123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
124124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
125 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
125126
126127 // RET must be custom lowered, to meet ABI requirements
127128 setOperationAction(ISD::RET , MVT::Other, Custom);
600601 }
601602
602603 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
604 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
605 return Lo;
606 }
607
608 static SDOperand LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
609 JumpTableSDNode *JT = cast(Op);
610 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
611 SDOperand Zero = DAG.getConstant(0, MVT::i32);
612
613 const TargetMachine &TM = DAG.getTarget();
614
615 // If this is a non-darwin platform, we don't support non-static relo models
616 // yet.
617 if (TM.getRelocationModel() == Reloc::Static ||
618 !TM.getSubtarget().isDarwin()) {
619 // Generate non-pic code that has direct accesses to the constant pool.
620 // The address of the global is just (hi(&g)+lo(&g)).
621 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, JTI, Zero);
622 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, JTI, Zero);
623 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
624 }
625
626 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, JTI, Zero);
627 if (TM.getRelocationModel() == Reloc::PIC) {
628 // With PIC, the first instruction is actually "GR+hi(&G)".
629 Hi = DAG.getNode(ISD::ADD, MVT::i32,
630 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
631 }
632
633 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, JTI, Zero);
603634 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
604635 return Lo;
605636 }
16511682 default: assert(0 && "Wasn't expecting to be able to lower this!");
16521683 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
16531684 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1685 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
16541686 case ISD::SETCC: return LowerSETCC(Op, DAG);
16551687 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
16561688 case ISD::RET: return LowerRET(Op, DAG);
701701
702702 def MTLR : XFXForm_7_ext<31, 467, 8, (ops GPRC:$rS), "mtlr $rS", SprMTSPR>,
703703 PPC970_DGroup_First, PPC970_Unit_FXU;
704 def MFLR : XFXForm_1_ext<31, 339, 8, (ops GPRC:$rT), "mflr $rT", SprMFSPR>,
704 def MFLR : XFXForm_1_ext<31, 339, 8, (ops GPRC:$rT), "mflr $rT", SprMFSPR>,
705705 PPC970_DGroup_First, PPC970_Unit_FXU;
706706
707707 // Move to/from VRSAVE: despite being a SPR, the VRSAVE register is renamed like
10141014 def : Pat<(PPClo tglobaladdr:$in, 0), (LI tglobaladdr:$in)>;
10151015 def : Pat<(PPChi tconstpool:$in, 0), (LIS tconstpool:$in)>;
10161016 def : Pat<(PPClo tconstpool:$in, 0), (LI tconstpool:$in)>;
1017 def : Pat<(PPChi tjumptable:$in, 0), (LIS tjumptable:$in)>;
1018 def : Pat<(PPClo tjumptable:$in, 0), (LI tjumptable:$in)>;
10171019 def : Pat<(add GPRC:$in, (PPChi tglobaladdr:$g, 0)),
10181020 (ADDIS GPRC:$in, tglobaladdr:$g)>;
10191021 def : Pat<(add GPRC:$in, (PPChi tconstpool:$g, 0)),
10201022 (ADDIS GPRC:$in, tconstpool:$g)>;
1023 def : Pat<(add GPRC:$in, (PPChi tjumptable:$g, 0)),
1024 (ADDIS GPRC:$in, tjumptable:$g)>;
10211025
10221026 // Fused negative multiply subtract, alternate pattern
10231027 def : Pat<(fsub F8RC:$B, (fmul F8RC:$A, F8RC:$C)),
162162 case MachineOperand::MO_UnextendedImmed:
163163 O << (int)MO.getImmedValue();
164164 break;
165 case MachineOperand::MO_MachineBasicBlock: {
166 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
167 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
168 << "_" << MBBOp->getNumber () << "\t! "
169 << MBBOp->getBasicBlock ()->getName ();
165 case MachineOperand::MO_MachineBasicBlock:
166 printBasicBlockLabel(MO.getMachineBasicBlock());
170167 return;
171 }
172168 case MachineOperand::MO_PCRelativeDisp:
173169 std::cerr << "Shouldn't use addPCDisp() when building Sparc MachineInstrs";
174170 abort ();
166166
167167 // Sparc doesn't have BRCOND either, it has BR_CC.
168168 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
169 setOperationAction(ISD::BRIND, MVT::i32, Expand);
169170 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
170171 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
171172 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
147147
148148 def SDTBrcond : SDTypeProfile<0, 2, [ // brcond
149149 SDTCisInt<0>, SDTCisVT<1, OtherVT>
150 ]>;
151
152 def SDTBrind : SDTypeProfile<0, 1, [ // brind
153 SDTCisPtrTy<0>
150154 ]>;
151155
152156 def SDTRet : SDTypeProfile<0, 0, []>; // ret
216220 "ConstantPoolSDNode">;
217221 def tconstpool : SDNode<"ISD::TargetConstantPool", SDTPtrLeaf, [],
218222 "ConstantPoolSDNode">;
223 def jumptable : SDNode<"ISD::JumpTable", SDTPtrLeaf, [],
224 "JumpTableSDNode">;
225 def tjumptable : SDNode<"ISD::TargetJumpTable", SDTPtrLeaf, [],
226 "JumpTableSDNode">;
219227 def frameindex : SDNode<"ISD::FrameIndex", SDTPtrLeaf, [],
220228 "FrameIndexSDNode">;
221229 def tframeindex : SDNode<"ISD::TargetFrameIndex", SDTPtrLeaf, [],
292300 def selectcc : SDNode<"ISD::SELECT_CC" , SDTSelectCC>;
293301
294302 def brcond : SDNode<"ISD::BRCOND" , SDTBrcond, [SDNPHasChain]>;
303 def brind : SDNode<"ISD::BRIND" , SDTBrind, [SDNPHasChain]>;
295304 def br : SDNode<"ISD::BR" , SDTBr, [SDNPHasChain]>;
296305 def ret : SDNode<"ISD::RET" , SDTRet, [SDNPHasChain]>;
297306
3737 // Print out constants referenced by the function
3838 EmitConstantPool(MF.getConstantPool());
3939
40 // Print out jump tables referenced by the function
41 EmitJumpTableInfo(MF.getJumpTableInfo());
42
4043 // Print out labels for the function.
4144 const Function *F = MF.getFunction();
4245 switch (F->getLinkage()) {
119122 O << '$';
120123 O << (int)MO.getImmedValue();
121124 return;
122 case MachineOperand::MO_MachineBasicBlock: {
123 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
124 O << PrivateGlobalPrefix << "BB"
125 << Mang->getValueName(MBBOp->getParent()->getFunction())
126 << "_" << MBBOp->getNumber () << "\t# "
127 << MBBOp->getBasicBlock ()->getName ();
128 return;
129 }
125 case MachineOperand::MO_MachineBasicBlock:
126 printBasicBlockLabel(MO.getMachineBasicBlock());
127 return;
130128 case MachineOperand::MO_PCRelativeDisp:
131129 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
132130 abort ();
133131 return;
132 case MachineOperand::MO_JumpTableIndex: {
133 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
134 if (!isMemOp) O << '$';
135 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
136 << MO.getJumpTableIndex();
137 // FIXME: PIC relocation model
138 return;
139 }
134140 case MachineOperand::MO_ConstantPoolIndex: {
135141 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
136142 if (!isMemOp) O << '$';
5353 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
5454 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
5555 ConstantPoolSection = "\t.const\n";
56 JumpTableSection = "\t.const\n"; // FIXME: depends on PIC mode
5657 LCOMMDirective = "\t.lcomm\t";
5758 COMMDirectiveTakesAlignment = false;
5859 HasDotTypeDotSizeDirective = false;
204205 return false; // success
205206 }
206207
208 void X86SharedAsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB)
209 const {
210 O << PrivateGlobalPrefix << "BB"
211 << Mang->getValueName(MBB->getParent()->getFunction())
212 << "_" << MBB->getNumber() << '\t' << CommentString
213 << MBB->getBasicBlock()->getName();
214 }
215
207216 /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code
208217 /// for a MachineFunction to the given output stream, using the given target
209218 /// machine description.
8787 MI->getOperand(Op+3).isGlobalAddress() ||
8888 MI->getOperand(Op+3).isConstantPoolIndex());
8989 }
90
91 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB) const;
9092 };
9193
9294 } // end namespace llvm
3434 class Emitter : public MachineFunctionPass {
3535 const X86InstrInfo *II;
3636 MachineCodeEmitter &MCE;
37 std::map BasicBlockAddrs;
38 std::vector > BBRefs;
37 std::map BasicBlockAddrs;
38 std::vector > BBRefs;
3939 public:
4040 explicit Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
4141 Emitter(MachineCodeEmitter &mce, const X86InstrInfo& ii)
5050 void emitInstruction(const MachineInstr &MI);
5151
5252 private:
53 void emitBasicBlock(const MachineBasicBlock &MBB);
54
55 void emitPCRelativeBlockAddress(const MachineBasicBlock *BB);
53 void emitBasicBlock(MachineBasicBlock &MBB);
54 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
5655 void emitPCRelativeValue(unsigned Address);
5756 void emitGlobalAddressForCall(GlobalValue *GV, bool isTailCall);
5857 void emitGlobalAddressForPtr(GlobalValue *GV, int Disp = 0);
8382
8483 MCE.startFunction(MF);
8584 MCE.emitConstantPool(MF.getConstantPool());
85 MCE.initJumpTableInfo(MF.getJumpTableInfo());
8686 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
8787 emitBasicBlock(*I);
88 MCE.emitJumpTableInfo(MF.getJumpTableInfo(), BasicBlockAddrs);
8889 MCE.finishFunction(MF);
8990
9091 // Resolve all forward branches now...
9899 return false;
99100 }
100101
101 void Emitter::emitBasicBlock(const MachineBasicBlock &MBB) {
102 void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
102103 if (uint64_t Addr = MCE.getCurrentPCValue())
103104 BasicBlockAddrs[&MBB] = Addr;
104105
118119 /// (because this is a forward branch), it keeps track of the information
119120 /// necessary to resolve this address later (and emits a dummy value).
120121 ///
121 void Emitter::emitPCRelativeBlockAddress(const MachineBasicBlock *MBB) {
122 void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
122123 // If this is a backwards branch, we already know the address of the target,
123124 // so just emit the value.
124 std::map::iterator I =
125 BasicBlockAddrs.find(MBB);
125 std::map::iterator I = BasicBlockAddrs.find(MBB);
126126 if (I != BasicBlockAddrs.end()) {
127127 emitPCRelativeValue(I->second);
128128 } else {
241241 } else if (Op3.isConstantPoolIndex()) {
242242 DispVal += MCE.getConstantPoolEntryAddress(Op3.getConstantPoolIndex());
243243 DispVal += Op3.getOffset();
244 } else if (Op3.isJumpTableIndex()) {
245 DispVal += MCE.getJumpTableEntryAddress(Op3.getJumpTableIndex());
244246 } else {
245247 DispVal = Op3.getImmedValue();
246248 }
444446 assert(sizeOfImm(Desc) == 4 &&
445447 "Don't know how to emit non-pointer values!");
446448 emitExternalSymbolAddress(MO1.getSymbolName(), false, false);
449 } else if (MO1.isJumpTableIndex()) {
450 assert(sizeOfImm(Desc) == 4 &&
451 "Don't know how to emit non-pointer values!");
452 emitConstant(MCE.getJumpTableEntryAddress(MO1.getJumpTableIndex()), 4);
447453 } else {
448454 emitConstant(MO1.getImmedValue(), sizeOfImm(Desc));
449455 }
168168 setOperationAction(ISD::RET , MVT::Other, Custom);
169169 // Darwin ABI issue.
170170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
171 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
171172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
172173 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
173174 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
27912792 return Chain;
27922793 }
27932794
2794 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2795 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2795 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2796 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
27962797 // one of the above mentioned nodes. It has to be wrapped because otherwise
27972798 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
27982799 // be used to form addressing mode. These wrapped nodes will be selected
28022803 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
28032804 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
28042805 CP->getAlignment()));
2806 if (Subtarget->isTargetDarwin()) {
2807 // With PIC, the address is actually $g + Offset.
2808 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2809 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2810 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2811 }
2812
2813 return Result;
2814 }
2815 case ISD::JumpTable: {
2816 JumpTableSDNode *JT = cast(Op);
2817 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2818 DAG.getTargetJumpTable(JT->getIndex(),
2819 getPointerTy()));
28052820 if (Subtarget->isTargetDarwin()) {
28062821 // With PIC, the address is actually $g + Offset.
28072822 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
364364 class IBr opcode, dag ops, string asm, list pattern> :
365365 I;
366366
367 // Conditional branches
367 // Indirect branches
368368 let isBarrier = 1 in
369369 def JMP : IBr<0xE9, (ops brtarget:$dst), "jmp $dst", [(br bb:$dst)]>;
370370
371 let isBranch = 1, isTerminator = 1, noResults = 1, isBarrier = 1 in {
372 def JMP32r : I<0xFF, MRM4r, (ops R32:$dst), "jmp{l} {*}$dst",
373 [(brind R32:$dst)]>;
374 def JMP32m : I<0xFF, MRM4m, (ops i32mem:$dst), "jmp{l} {*}$dst",
375 [(brind (loadi32 addr:$dst))]>;
376 }
377
378 // Conditional branches
371379 def JE : IBr<0x84, (ops brtarget:$dst), "je $dst",
372380 [(X86brcond bb:$dst, X86_COND_E)]>, TB;
373381 def JNE : IBr<0x85, (ops brtarget:$dst), "jne $dst",
23012309
23022310 // ConstantPool GlobalAddress, ExternalSymbol
23032311 def : Pat<(i32 (X86Wrapper tconstpool :$dst)), (MOV32ri tconstpool :$dst)>;
2312 def : Pat<(i32 (X86Wrapper tjumptable :$dst)), (MOV32ri tjumptable :$dst)>;
23042313 def : Pat<(i32 (X86Wrapper tglobaladdr :$dst)), (MOV32ri tglobaladdr :$dst)>;
23052314 def : Pat<(i32 (X86Wrapper texternalsym:$dst)), (MOV32ri texternalsym:$dst)>;
23062315
23072316 def : Pat<(add R32:$src1, (X86Wrapper tconstpool:$src2)),
23082317 (ADD32ri R32:$src1, tconstpool:$src2)>;
2318 def : Pat<(add R32:$src1, (X86Wrapper tjumptable:$src2)),
2319 (ADD32ri R32:$src1, tjumptable:$src2)>;
23092320 def : Pat<(add R32:$src1, (X86Wrapper tglobaladdr :$src2)),
23102321 (ADD32ri R32:$src1, tglobaladdr:$src2)>;
23112322 def : Pat<(add R32:$src1, (X86Wrapper texternalsym:$src2)),
23122323 (ADD32ri R32:$src1, texternalsym:$src2)>;
23132324
2325 // FIXME: can you really ever store to a constant pool?
23142326 def : Pat<(store (X86Wrapper tconstpool:$src), addr:$dst),
23152327 (MOV32mi addr:$dst, tconstpool:$src)>;
23162328 def : Pat<(store (X86Wrapper tglobaladdr:$src), addr:$dst),
111111 case MachineOperand::MO_UnextendedImmed:
112112 O << (int)MO.getImmedValue();
113113 return;
114 case MachineOperand::MO_MachineBasicBlock: {
115 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
116 O << PrivateGlobalPrefix << "BB"
117 << Mang->getValueName(MBBOp->getParent()->getFunction())
118 << "_" << MBBOp->getNumber () << "\t# "
119 << MBBOp->getBasicBlock ()->getName ();
120 return;
121 }
114 case MachineOperand::MO_MachineBasicBlock:
115 printBasicBlockLabel(MO.getMachineBasicBlock());
116 return;
122117 case MachineOperand::MO_PCRelativeDisp:
123118 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
124119 abort ();
32243224 << " case ISD::TargetConstant:\n"
32253225 << " case ISD::TargetConstantPool:\n"
32263226 << " case ISD::TargetFrameIndex:\n"
3227 << " case ISD::TargetJumpTable:\n"
32273228 << " case ISD::TargetGlobalAddress: {\n"
32283229 << " Result = N;\n"
32293230 << " return;\n"