OLD | NEW |
1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// | 1 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file defines a DAG pattern matching instruction selector for X86, | 10 // This file defines a DAG pattern matching instruction selector for X86, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0), | 75 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0), |
76 Segment(), GV(nullptr), CP(nullptr), BlockAddr(nullptr), ES(nullptr), | 76 Segment(), GV(nullptr), CP(nullptr), BlockAddr(nullptr), ES(nullptr), |
77 JT(-1), Align(0), SymbolFlags(X86II::MO_NO_FLAG) { | 77 JT(-1), Align(0), SymbolFlags(X86II::MO_NO_FLAG) { |
78 } | 78 } |
79 | 79 |
80 bool hasSymbolicDisplacement() const { | 80 bool hasSymbolicDisplacement() const { |
81 return GV != nullptr || CP != nullptr || ES != nullptr || | 81 return GV != nullptr || CP != nullptr || ES != nullptr || |
82 JT != -1 || BlockAddr != nullptr; | 82 JT != -1 || BlockAddr != nullptr; |
83 } | 83 } |
84 | 84 |
| 85 // @LOCALMOD-BEGIN |
| 86 /// clearSymbolicDisplacement - Remove all sources of symbolic |
| 87 /// constant displacement from the addressing mode. This is |
| 88 /// needed when the symbolic constant is pulled out of the address |
| 89 /// computation, e.g. when |
| 90 /// ... DISP(%r15,%rax,1) ... |
| 91 /// is replaced with |
| 92 /// lea DISP(%rax),%tmp |
| 93 /// ... (%r15,%tmp,1) ... |
| 94 void clearSymbolicDisplacement() { |
| 95 GV = 0; |
| 96 CP = 0; |
| 97 BlockAddr = 0; |
| 98 ES = 0; |
| 99 JT = -1; |
| 100 } |
| 101 // @LOCALMOD-END |
| 102 |
85 bool hasBaseOrIndexReg() const { | 103 bool hasBaseOrIndexReg() const { |
86 return BaseType == FrameIndexBase || | 104 return BaseType == FrameIndexBase || |
87 IndexReg.getNode() != nullptr || Base_Reg.getNode() != nullptr; | 105 IndexReg.getNode() != nullptr || Base_Reg.getNode() != nullptr; |
88 } | 106 } |
89 | 107 |
90 /// isRIPRelative - Return true if this addressing mode is already RIP | 108 /// isRIPRelative - Return true if this addressing mode is already RIP |
91 /// relative. | 109 /// relative. |
92 bool isRIPRelative() const { | 110 bool isRIPRelative() const { |
93 if (BaseType != RegBase) return false; | 111 if (BaseType != RegBase) return false; |
94 if (RegisterSDNode *RegNode = | 112 if (RegisterSDNode *RegNode = |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 SDValue &Scale, SDValue &Index, SDValue &Disp, | 232 SDValue &Scale, SDValue &Index, SDValue &Disp, |
215 SDValue &Segment); | 233 SDValue &Segment); |
216 bool SelectTLSADDRAddr(SDValue N, SDValue &Base, | 234 bool SelectTLSADDRAddr(SDValue N, SDValue &Base, |
217 SDValue &Scale, SDValue &Index, SDValue &Disp, | 235 SDValue &Scale, SDValue &Index, SDValue &Disp, |
218 SDValue &Segment); | 236 SDValue &Segment); |
219 bool SelectScalarSSELoad(SDNode *Root, SDValue N, | 237 bool SelectScalarSSELoad(SDNode *Root, SDValue N, |
220 SDValue &Base, SDValue &Scale, | 238 SDValue &Base, SDValue &Scale, |
221 SDValue &Index, SDValue &Disp, | 239 SDValue &Index, SDValue &Disp, |
222 SDValue &Segment, | 240 SDValue &Segment, |
223 SDValue &NodeWithChain); | 241 SDValue &NodeWithChain); |
| 242 // @LOCALMOD-BEGIN |
| 243 void LegalizeAddressingModeForNaCl(SDValue N, X86ISelAddressMode &AM); |
| 244 // @LOCALMOD-END |
| 245 |
224 | 246 |
225 bool TryFoldLoad(SDNode *P, SDValue N, | 247 bool TryFoldLoad(SDNode *P, SDValue N, |
226 SDValue &Base, SDValue &Scale, | 248 SDValue &Base, SDValue &Scale, |
227 SDValue &Index, SDValue &Disp, | 249 SDValue &Index, SDValue &Disp, |
228 SDValue &Segment); | 250 SDValue &Segment); |
229 | 251 |
230 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for | 252 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
231 /// inline asm expressions. | 253 /// inline asm expressions. |
232 bool SelectInlineAsmMemoryOperand(const SDValue &Op, | 254 bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
233 char ConstraintCode, | 255 char ConstraintCode, |
234 std::vector<SDValue> &OutOps) override; | 256 std::vector<SDValue> &OutOps) override; |
235 | 257 |
236 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); | 258 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
237 | 259 |
238 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, | 260 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, |
239 SDValue &Scale, SDValue &Index, | 261 SDValue &Scale, SDValue &Index, |
240 SDValue &Disp, SDValue &Segment) { | 262 SDValue &Disp, SDValue &Segment) { |
| 263 |
| 264 // @LOCALMOD-BEGIN: NaCl64 pointers are 32-bit, but memory operands |
| 265 // are 64-bit (adds r15). |
| 266 EVT MemOpVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; |
241 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) | 267 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
242 ? CurDAG->getTargetFrameIndex(AM.Base_FrameIndex, | 268 ? CurDAG->getTargetFrameIndex(AM.Base_FrameIndex, |
243 TLI->getPointerTy()) | 269 MemOpVT) |
244 : AM.Base_Reg; | 270 : AM.Base_Reg; |
| 271 // @LOCALMOD-END |
245 Scale = getI8Imm(AM.Scale); | 272 Scale = getI8Imm(AM.Scale); |
246 Index = AM.IndexReg; | 273 Index = AM.IndexReg; |
247 // These are 32-bit even in 64-bit mode since RIP relative offset | 274 // These are 32-bit even in 64-bit mode since RIP relative offset |
248 // is 32-bit. | 275 // is 32-bit. |
249 if (AM.GV) | 276 if (AM.GV) |
250 Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(), | 277 Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(), |
251 MVT::i32, AM.Disp, | 278 MVT::i32, AM.Disp, |
252 AM.SymbolFlags); | 279 AM.SymbolFlags); |
253 else if (AM.CP) | 280 else if (AM.CP) |
254 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, | 281 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 const X86TargetMachine &getTargetMachine() const { | 321 const X86TargetMachine &getTargetMachine() const { |
295 return static_cast<const X86TargetMachine &>(TM); | 322 return static_cast<const X86TargetMachine &>(TM); |
296 } | 323 } |
297 | 324 |
298 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted | 325 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted |
299 /// to the target-specific type. | 326 /// to the target-specific type. |
300 const X86InstrInfo *getInstrInfo() const { | 327 const X86InstrInfo *getInstrInfo() const { |
301 return getTargetMachine().getSubtargetImpl()->getInstrInfo(); | 328 return getTargetMachine().getSubtargetImpl()->getInstrInfo(); |
302 } | 329 } |
303 | 330 |
| 331 // @LOCALMOD-START |
| 332 // Determine when an addressing mode with a base register and an index |
| 333 // register can be selected (or not). The NaCl 64-bit sandbox could end |
| 334 // up adding an r15 base register for memory accesses late (after register |
| 335 // allocation), so having both a base and index register selected |
| 336 // now will make it hard to modify the address and base off of r15 later. |
| 337 // This only applies to memory accesses and not lea (distinguished by |
| 338 // selectingMemOp). |
| 339 bool selectingMemOp; |
| 340 bool RestrictUseOfBaseReg() { |
| 341 return selectingMemOp && Subtarget->isTargetNaCl64(); |
| 342 } |
| 343 // @LOCALMOD-END |
| 344 |
304 /// \brief Address-mode matching performs shift-of-and to and-of-shift | 345 /// \brief Address-mode matching performs shift-of-and to and-of-shift |
305 /// reassociation in order to expose more scaled addressing | 346 /// reassociation in order to expose more scaled addressing |
306 /// opportunities. | 347 /// opportunities. |
307 bool ComplexPatternFuncMutatesDAG() const override { | 348 bool ComplexPatternFuncMutatesDAG() const override { |
308 return true; | 349 return true; |
309 } | 350 } |
310 }; | 351 }; |
311 } | 352 } |
312 | 353 |
313 | 354 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 void X86DAGToDAGISel::PreprocessISelDAG() { | 495 void X86DAGToDAGISel::PreprocessISelDAG() { |
455 // OptForSize is used in pattern predicates that isel is matching. | 496 // OptForSize is used in pattern predicates that isel is matching. |
456 OptForSize = MF->getFunction()->getAttributes(). | 497 OptForSize = MF->getFunction()->getAttributes(). |
457 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); | 498 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); |
458 | 499 |
459 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), | 500 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
460 E = CurDAG->allnodes_end(); I != E; ) { | 501 E = CurDAG->allnodes_end(); I != E; ) { |
461 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. | 502 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
462 | 503 |
463 if (OptLevel != CodeGenOpt::None && | 504 if (OptLevel != CodeGenOpt::None && |
464 // Only does this when target favors doesn't favor register indirect | 505 !Subtarget->isTargetNaCl() && // @LOCALMOD: We can't fold load/call |
465 // call. | |
466 ((N->getOpcode() == X86ISD::CALL && !Subtarget->callRegIndirect()) || | 506 ((N->getOpcode() == X86ISD::CALL && !Subtarget->callRegIndirect()) || |
467 (N->getOpcode() == X86ISD::TC_RETURN && | 507 (N->getOpcode() == X86ISD::TC_RETURN && |
468 // Only does this if load can be folded into TC_RETURN. | 508 // Only does this if load can be folded into TC_RETURN. |
469 (Subtarget->is64Bit() || | 509 (Subtarget->is64Bit() || |
470 getTargetMachine().getRelocationModel() != Reloc::PIC_)))) { | 510 getTargetMachine().getRelocationModel() != Reloc::PIC_)))) { |
471 /// Also try moving call address load from outside callseq_start to just | 511 /// Also try moving call address load from outside callseq_start to just |
472 /// before the call to allow it to be folded. | 512 /// before the call to allow it to be folded. |
473 /// | 513 /// |
474 /// [Load chain] | 514 /// [Load chain] |
475 /// ^ | 515 /// ^ |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 CodeModel::Model M = TM.getCodeModel(); | 645 CodeModel::Model M = TM.getCodeModel(); |
606 if (Subtarget->is64Bit()) { | 646 if (Subtarget->is64Bit()) { |
607 if (!X86::isOffsetSuitableForCodeModel(Val, M, | 647 if (!X86::isOffsetSuitableForCodeModel(Val, M, |
608 AM.hasSymbolicDisplacement())) | 648 AM.hasSymbolicDisplacement())) |
609 return true; | 649 return true; |
610 // In addition to the checks required for a register base, check that | 650 // In addition to the checks required for a register base, check that |
611 // we do not try to use an unsafe Disp with a frame index. | 651 // we do not try to use an unsafe Disp with a frame index. |
612 if (AM.BaseType == X86ISelAddressMode::FrameIndexBase && | 652 if (AM.BaseType == X86ISelAddressMode::FrameIndexBase && |
613 !isDispSafeForFrameIndex(Val)) | 653 !isDispSafeForFrameIndex(Val)) |
614 return true; | 654 return true; |
| 655 // LOCALMOD-BEGIN |
| 656 // Do not fold large offsets into displacements. |
| 657 // Various constant folding and address-mode selections can result in |
| 658 // 32-bit operations (e.g. from GEP) getting folded into the displacement |
| 659 // and often results in a negative value in the index register |
| 660 // (see also LegalizeAddressModeForNaCl) |
| 661 else if (Subtarget->isTargetNaCl64() && |
| 662 (AM.BaseType == X86ISelAddressMode::RegBase || |
| 663 AM.BaseType == X86ISelAddressMode::FrameIndexBase) && |
| 664 (Val > 65535 || Val < -65536) && selectingMemOp) |
| 665 return true; |
| 666 // LOCALMOD-END |
615 } | 667 } |
616 AM.Disp = Val; | 668 AM.Disp = Val; |
617 return false; | 669 return false; |
618 | 670 |
619 } | 671 } |
620 | 672 |
621 bool X86DAGToDAGISel::MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){ | 673 bool X86DAGToDAGISel::MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){ |
622 SDValue Address = N->getOperand(1); | 674 SDValue Address = N->getOperand(1); |
623 | 675 |
| 676 // @LOCALMOD-START |
| 677 // Disable this tls access optimization in Native Client, since |
| 678 // gs:0 (or fs:0 on X86-64) does not exactly contain its own address. |
| 679 if (Subtarget->isTargetNaCl()) { |
| 680 return true; |
| 681 } |
| 682 // @LOCALMOD-END |
| 683 |
624 // load gs:0 -> GS segment register. | 684 // load gs:0 -> GS segment register. |
625 // load fs:0 -> FS segment register. | 685 // load fs:0 -> FS segment register. |
626 // | 686 // |
627 // This optimization is valid because the GNU TLS model defines that | 687 // This optimization is valid because the GNU TLS model defines that |
628 // gs:0 (or fs:0 on X86-64) contains its own address. | 688 // gs:0 (or fs:0 on X86-64) contains its own address. |
629 // For more information see http://people.redhat.com/drepper/tls.pdf | 689 // For more information see http://people.redhat.com/drepper/tls.pdf |
630 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address)) | 690 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address)) |
631 if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr && | 691 if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr && |
632 Subtarget->isTargetLinux()) | 692 Subtarget->isTargetLinux()) |
633 switch (N->getPointerInfo().getAddrSpace()) { | 693 switch (N->getPointerInfo().getAddrSpace()) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 return true; | 798 return true; |
739 } | 799 } |
740 | 800 |
741 /// MatchAddress - Add the specified node to the specified addressing mode, | 801 /// MatchAddress - Add the specified node to the specified addressing mode, |
742 /// returning true if it cannot be done. This just pattern matches for the | 802 /// returning true if it cannot be done. This just pattern matches for the |
743 /// addressing mode. | 803 /// addressing mode. |
744 bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) { | 804 bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) { |
745 if (MatchAddressRecursively(N, AM, 0)) | 805 if (MatchAddressRecursively(N, AM, 0)) |
746 return true; | 806 return true; |
747 | 807 |
| 808 |
| 809 if (!RestrictUseOfBaseReg()) { // @LOCALMOD |
748 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has | 810 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has |
749 // a smaller encoding and avoids a scaled-index. | 811 // a smaller encoding and avoids a scaled-index. |
750 if (AM.Scale == 2 && | 812 if (AM.Scale == 2 && |
751 AM.BaseType == X86ISelAddressMode::RegBase && | 813 AM.BaseType == X86ISelAddressMode::RegBase && |
752 AM.Base_Reg.getNode() == nullptr) { | 814 AM.Base_Reg.getNode() == nullptr) { |
753 AM.Base_Reg = AM.IndexReg; | 815 AM.Base_Reg = AM.IndexReg; |
754 AM.Scale = 1; | 816 AM.Scale = 1; |
755 } | 817 } |
756 | 818 } // @LOCALMOD |
| 819 |
757 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode, | 820 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode, |
758 // because it has a smaller encoding. | 821 // because it has a smaller encoding. |
759 // TODO: Which other code models can use this? | 822 // TODO: Which other code models can use this? |
760 if (TM.getCodeModel() == CodeModel::Small && | 823 if (TM.getCodeModel() == CodeModel::Small && |
761 Subtarget->is64Bit() && | 824 Subtarget->is64Bit() && |
762 AM.Scale == 1 && | 825 AM.Scale == 1 && |
763 AM.BaseType == X86ISelAddressMode::RegBase && | 826 AM.BaseType == X86ISelAddressMode::RegBase && |
764 AM.Base_Reg.getNode() == nullptr && | 827 AM.Base_Reg.getNode() == nullptr && |
765 AM.IndexReg.getNode() == nullptr && | 828 AM.IndexReg.getNode() == nullptr && |
766 AM.SymbolFlags == X86II::MO_NO_FLAG && | 829 AM.SymbolFlags == X86II::MO_NO_FLAG && |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 break; | 1157 break; |
1095 } | 1158 } |
1096 | 1159 |
1097 case ISD::SMUL_LOHI: | 1160 case ISD::SMUL_LOHI: |
1098 case ISD::UMUL_LOHI: | 1161 case ISD::UMUL_LOHI: |
1099 // A mul_lohi where we need the low part can be folded as a plain multiply. | 1162 // A mul_lohi where we need the low part can be folded as a plain multiply. |
1100 if (N.getResNo() != 0) break; | 1163 if (N.getResNo() != 0) break; |
1101 // FALL THROUGH | 1164 // FALL THROUGH |
1102 case ISD::MUL: | 1165 case ISD::MUL: |
1103 case X86ISD::MUL_IMM: | 1166 case X86ISD::MUL_IMM: |
| 1167 // @LOCALMOD |
| 1168 if (!RestrictUseOfBaseReg()) { |
1104 // X*[3,5,9] -> X+X*[2,4,8] | 1169 // X*[3,5,9] -> X+X*[2,4,8] |
1105 if (AM.BaseType == X86ISelAddressMode::RegBase && | 1170 if (AM.BaseType == X86ISelAddressMode::RegBase && |
1106 AM.Base_Reg.getNode() == nullptr && | 1171 AM.Base_Reg.getNode() == nullptr && |
1107 AM.IndexReg.getNode() == nullptr) { | 1172 AM.IndexReg.getNode() == nullptr) { |
1108 if (ConstantSDNode | 1173 if (ConstantSDNode |
1109 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) | 1174 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) |
1110 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || | 1175 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || |
1111 CN->getZExtValue() == 9) { | 1176 CN->getZExtValue() == 9) { |
1112 AM.Scale = unsigned(CN->getZExtValue())-1; | 1177 AM.Scale = unsigned(CN->getZExtValue())-1; |
1113 | 1178 |
(...skipping 12 matching lines...) Expand all Loading... |
1126 if (FoldOffsetIntoAddress(Disp, AM)) | 1191 if (FoldOffsetIntoAddress(Disp, AM)) |
1127 Reg = N.getNode()->getOperand(0); | 1192 Reg = N.getNode()->getOperand(0); |
1128 } else { | 1193 } else { |
1129 Reg = N.getNode()->getOperand(0); | 1194 Reg = N.getNode()->getOperand(0); |
1130 } | 1195 } |
1131 | 1196 |
1132 AM.IndexReg = AM.Base_Reg = Reg; | 1197 AM.IndexReg = AM.Base_Reg = Reg; |
1133 return false; | 1198 return false; |
1134 } | 1199 } |
1135 } | 1200 } |
| 1201 } // @LOCALMOD |
1136 break; | 1202 break; |
1137 | 1203 |
1138 case ISD::SUB: { | 1204 case ISD::SUB: { |
1139 // Given A-B, if A can be completely folded into the address and | 1205 // Given A-B, if A can be completely folded into the address and |
1140 // the index field with the index field unused, use -B as the index. | 1206 // the index field with the index field unused, use -B as the index. |
1141 // This is a win if a has multiple parts that can be folded into | 1207 // This is a win if a has multiple parts that can be folded into |
1142 // the address. Also, this saves a mov if the base register has | 1208 // the address. Also, this saves a mov if the base register has |
1143 // other uses, since it avoids a two-address sub instruction, however | 1209 // other uses, since it avoids a two-address sub instruction, however |
1144 // it costs an additional mov if the index register has other uses. | 1210 // it costs an additional mov if the index register has other uses. |
1145 | 1211 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1212 !MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)) | 1278 !MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)) |
1213 return false; | 1279 return false; |
1214 AM = Backup; | 1280 AM = Backup; |
1215 | 1281 |
1216 // Try again after commuting the operands. | 1282 // Try again after commuting the operands. |
1217 if (!MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&& | 1283 if (!MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&& |
1218 !MatchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1)) | 1284 !MatchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1)) |
1219 return false; | 1285 return false; |
1220 AM = Backup; | 1286 AM = Backup; |
1221 | 1287 |
| 1288 if (!RestrictUseOfBaseReg()) { // @LOCALMOD |
1222 // If we couldn't fold both operands into the address at the same time, | 1289 // If we couldn't fold both operands into the address at the same time, |
1223 // see if we can just put each operand into a register and fold at least | 1290 // see if we can just put each operand into a register and fold at least |
1224 // the add. | 1291 // the add. |
1225 if (AM.BaseType == X86ISelAddressMode::RegBase && | 1292 if (AM.BaseType == X86ISelAddressMode::RegBase && |
1226 !AM.Base_Reg.getNode() && | 1293 !AM.Base_Reg.getNode() && |
1227 !AM.IndexReg.getNode()) { | 1294 !AM.IndexReg.getNode()) { |
1228 N = Handle.getValue(); | 1295 N = Handle.getValue(); |
1229 AM.Base_Reg = N.getOperand(0); | 1296 AM.Base_Reg = N.getOperand(0); |
1230 AM.IndexReg = N.getOperand(1); | 1297 AM.IndexReg = N.getOperand(1); |
1231 AM.Scale = 1; | 1298 AM.Scale = 1; |
1232 return false; | 1299 return false; |
1233 } | 1300 } |
| 1301 } // @LOCALMOD |
1234 N = Handle.getValue(); | 1302 N = Handle.getValue(); |
1235 break; | 1303 break; |
1236 } | 1304 } |
1237 | 1305 |
1238 case ISD::OR: | 1306 case ISD::OR: |
1239 // Handle "X | C" as "X + C" iff X is known to have C bits clear. | 1307 // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
1240 if (CurDAG->isBaseWithConstantOffset(N)) { | 1308 if (CurDAG->isBaseWithConstantOffset(N)) { |
1241 X86ISelAddressMode Backup = AM; | 1309 X86ISelAddressMode Backup = AM; |
1242 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1)); | 1310 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1)); |
1243 | 1311 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 break; | 1351 break; |
1284 } | 1352 } |
1285 } | 1353 } |
1286 | 1354 |
1287 return MatchAddressBase(N, AM); | 1355 return MatchAddressBase(N, AM); |
1288 } | 1356 } |
1289 | 1357 |
1290 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the | 1358 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
1291 /// specified addressing mode without any further recursion. | 1359 /// specified addressing mode without any further recursion. |
1292 bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) { | 1360 bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) { |
1293 // Is the base register already occupied? | 1361 if (RestrictUseOfBaseReg()) { // @LOCALMOD |
| 1362 if (AM.IndexReg.getNode() == 0) { |
| 1363 AM.IndexReg = N; |
| 1364 AM.Scale = 1; |
| 1365 return false; |
| 1366 } |
| 1367 return true; |
| 1368 } // @LOCALMOD |
| 1369 // Is the base register already occupied? |
1294 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) { | 1370 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) { |
1295 // If so, check to see if the scale index register is set. | 1371 // If so, check to see if the scale index register is set. |
1296 if (!AM.IndexReg.getNode()) { | 1372 if (!AM.IndexReg.getNode()) { |
1297 AM.IndexReg = N; | 1373 AM.IndexReg = N; |
1298 AM.Scale = 1; | 1374 AM.Scale = 1; |
1299 return false; | 1375 return false; |
1300 } | 1376 } |
1301 | 1377 |
1302 // Otherwise, we cannot select it. | 1378 // Otherwise, we cannot select it. |
1303 return true; | 1379 return true; |
1304 } | 1380 } |
1305 | 1381 |
1306 // Default, generate it as a register. | 1382 // Default, generate it as a register. |
1307 AM.BaseType = X86ISelAddressMode::RegBase; | 1383 AM.BaseType = X86ISelAddressMode::RegBase; |
1308 AM.Base_Reg = N; | 1384 AM.Base_Reg = N; |
1309 return false; | 1385 return false; |
1310 } | 1386 } |
1311 | 1387 |
1312 /// SelectAddr - returns true if it is able pattern match an addressing mode. | 1388 /// SelectAddr - returns true if it is able pattern match an addressing mode. |
1313 /// It returns the operands which make up the maximal addressing mode it can | 1389 /// It returns the operands which make up the maximal addressing mode it can |
1314 /// match by reference. | 1390 /// match by reference. |
1315 /// | 1391 /// |
1316 /// Parent is the parent node of the addr operand that is being matched. It | 1392 /// Parent is the parent node of the addr operand that is being matched. It |
1317 /// is always a load, store, atomic node, or null. It is only null when | 1393 /// is always a load, store, atomic node, or null. It is only null when |
1318 /// checking memory operands for inline asm nodes. | 1394 /// checking memory operands for inline asm nodes. |
1319 bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, | 1395 bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, |
1320 SDValue &Scale, SDValue &Index, | 1396 SDValue &Scale, SDValue &Index, |
1321 SDValue &Disp, SDValue &Segment) { | 1397 SDValue &Disp, SDValue &Segment) { |
1322 X86ISelAddressMode AM; | 1398 X86ISelAddressMode AM; |
| 1399 // @LOCALMOD |
| 1400 selectingMemOp = true; |
1323 | 1401 |
1324 if (Parent && | 1402 if (Parent && |
1325 // This list of opcodes are all the nodes that have an "addr:$ptr" operand | 1403 // This list of opcodes are all the nodes that have an "addr:$ptr" operand |
1326 // that are not a MemSDNode, and thus don't have proper addrspace info. | 1404 // that are not a MemSDNode, and thus don't have proper addrspace info. |
1327 Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme | 1405 Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme |
1328 Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores | 1406 Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores |
1329 Parent->getOpcode() != X86ISD::TLSCALL && // Fixme | 1407 Parent->getOpcode() != X86ISD::TLSCALL && // Fixme |
1330 Parent->getOpcode() != X86ISD::EH_SJLJ_SETJMP && // setjmp | 1408 Parent->getOpcode() != X86ISD::EH_SJLJ_SETJMP && // setjmp |
1331 Parent->getOpcode() != X86ISD::EH_SJLJ_LONGJMP) { // longjmp | 1409 Parent->getOpcode() != X86ISD::EH_SJLJ_LONGJMP) { // longjmp |
1332 unsigned AddrSpace = | 1410 unsigned AddrSpace = |
1333 cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace(); | 1411 cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace(); |
1334 // AddrSpace 256 -> GS, 257 -> FS. | 1412 // AddrSpace 256 -> GS, 257 -> FS. |
1335 if (AddrSpace == 256) | 1413 if (AddrSpace == 256) |
1336 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16); | 1414 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16); |
1337 if (AddrSpace == 257) | 1415 if (AddrSpace == 257) |
1338 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16); | 1416 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16); |
1339 } | 1417 } |
1340 | 1418 |
1341 if (MatchAddress(N, AM)) | 1419 if (MatchAddress(N, AM)) |
1342 return false; | 1420 return false; |
1343 | 1421 |
1344 MVT VT = N.getSimpleValueType(); | 1422 // @LOCALMOD-START |
| 1423 if (Subtarget->isTargetNaCl64()) { |
| 1424 LegalizeAddressingModeForNaCl(N, AM); |
| 1425 } |
| 1426 // @LOCALMOD-END |
| 1427 |
| 1428 MVT VT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; // @LOCALMOD |
| 1429 |
1345 if (AM.BaseType == X86ISelAddressMode::RegBase) { | 1430 if (AM.BaseType == X86ISelAddressMode::RegBase) { |
1346 if (!AM.Base_Reg.getNode()) | 1431 if (!AM.Base_Reg.getNode()) |
1347 AM.Base_Reg = CurDAG->getRegister(0, VT); | 1432 AM.Base_Reg = CurDAG->getRegister(0, VT); |
1348 } | 1433 } |
1349 | 1434 |
1350 if (!AM.IndexReg.getNode()) | 1435 if (!AM.IndexReg.getNode()) |
1351 AM.IndexReg = CurDAG->getRegister(0, VT); | 1436 AM.IndexReg = CurDAG->getRegister(0, VT); |
1352 | 1437 |
1353 getAddressOperands(AM, Base, Scale, Index, Disp, Segment); | 1438 getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
| 1439 |
| 1440 // @LOCALMOD-BEGIN |
| 1441 // For Native Client 64-bit, zero-extend 32-bit pointers |
| 1442 // to 64-bits for memory operations. Most of the time, this |
| 1443 // won't generate any additional instructions because the backend |
| 1444 // knows that operations on 32-bit registers implicitly zero-extends. |
| 1445 // If we don't do this, there are a few corner cases where LLVM might |
| 1446 // assume the upper bits won't be modified or used, but since we |
| 1447 // always clear the upper bits, this is not a good assumption. |
| 1448 // http://code.google.com/p/nativeclient/issues/detail?id=1564 |
| 1449 if (Subtarget->isTargetNaCl64()) { |
| 1450 assert(Base.getValueType() == MVT::i64 && "Unexpected base operand size"); |
| 1451 |
| 1452 if (Index.getValueType() != MVT::i64) { |
| 1453 Index = CurDAG->getZExtOrTrunc(Index, SDLoc(Index), MVT::i64); |
| 1454 // Insert the new node into the topological ordering. |
| 1455 InsertDAGNode(*CurDAG, Parent ? SDValue(Parent, 0) : N, Index); |
| 1456 } |
| 1457 } |
| 1458 // @LOCALMOD-END |
| 1459 |
1354 return true; | 1460 return true; |
1355 } | 1461 } |
1356 | 1462 |
1357 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to | 1463 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
1358 /// match a load whose top elements are either undef or zeros. The load flavor | 1464 /// match a load whose top elements are either undef or zeros. The load flavor |
1359 /// is derived from the type of N, which is either v4f32 or v2f64. | 1465 /// is derived from the type of N, which is either v4f32 or v2f64. |
1360 /// | 1466 /// |
1361 /// We also return: | 1467 /// We also return: |
1362 /// PatternChainNode: this is the matched node that has a chain input and | 1468 /// PatternChainNode: this is the matched node that has a chain input and |
1363 /// output. | 1469 /// output. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1431 bool X86DAGToDAGISel::SelectLEA64_32Addr(SDValue N, SDValue &Base, | 1537 bool X86DAGToDAGISel::SelectLEA64_32Addr(SDValue N, SDValue &Base, |
1432 SDValue &Scale, SDValue &Index, | 1538 SDValue &Scale, SDValue &Index, |
1433 SDValue &Disp, SDValue &Segment) { | 1539 SDValue &Disp, SDValue &Segment) { |
1434 if (!SelectLEAAddr(N, Base, Scale, Index, Disp, Segment)) | 1540 if (!SelectLEAAddr(N, Base, Scale, Index, Disp, Segment)) |
1435 return false; | 1541 return false; |
1436 | 1542 |
1437 SDLoc DL(N); | 1543 SDLoc DL(N); |
1438 RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Base); | 1544 RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Base); |
1439 if (RN && RN->getReg() == 0) | 1545 if (RN && RN->getReg() == 0) |
1440 Base = CurDAG->getRegister(0, MVT::i64); | 1546 Base = CurDAG->getRegister(0, MVT::i64); |
1441 else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(Base))
{ | 1547 // @LOCALMOD-BEGIN: -- the commit in http://reviews.llvm.org/D4929 |
| 1548 // to address: http://llvm.org/bugs/show_bug.cgi?id=20016 |
| 1549 // break's NaCl's test/NaCl/X86/nacl64-addrmodes.ll |
| 1550 // revert to checking N. |
| 1551 // else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(Ba
se)) { |
| 1552 else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(N)) { |
| 1553 // @LOCALMOD-END |
1442 // Base could already be %rip, particularly in the x32 ABI. | 1554 // Base could already be %rip, particularly in the x32 ABI. |
1443 Base = SDValue(CurDAG->getMachineNode( | 1555 Base = SDValue(CurDAG->getMachineNode( |
1444 TargetOpcode::SUBREG_TO_REG, DL, MVT::i64, | 1556 TargetOpcode::SUBREG_TO_REG, DL, MVT::i64, |
1445 CurDAG->getTargetConstant(0, MVT::i64), | 1557 CurDAG->getTargetConstant(0, MVT::i64), |
1446 Base, | 1558 Base, |
1447 CurDAG->getTargetConstant(X86::sub_32bit, MVT::i32)), | 1559 CurDAG->getTargetConstant(X86::sub_32bit, MVT::i32)), |
1448 0); | 1560 0); |
1449 } | 1561 } |
1450 | 1562 |
1451 RN = dyn_cast<RegisterSDNode>(Index); | 1563 RN = dyn_cast<RegisterSDNode>(Index); |
(...skipping 19 matching lines...) Expand all Loading... |
1471 SDValue &Base, SDValue &Scale, | 1583 SDValue &Base, SDValue &Scale, |
1472 SDValue &Index, SDValue &Disp, | 1584 SDValue &Index, SDValue &Disp, |
1473 SDValue &Segment) { | 1585 SDValue &Segment) { |
1474 X86ISelAddressMode AM; | 1586 X86ISelAddressMode AM; |
1475 | 1587 |
1476 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support | 1588 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support |
1477 // segments. | 1589 // segments. |
1478 SDValue Copy = AM.Segment; | 1590 SDValue Copy = AM.Segment; |
1479 SDValue T = CurDAG->getRegister(0, MVT::i32); | 1591 SDValue T = CurDAG->getRegister(0, MVT::i32); |
1480 AM.Segment = T; | 1592 AM.Segment = T; |
| 1593 // @LOCALMOD |
| 1594 selectingMemOp = false; |
1481 if (MatchAddress(N, AM)) | 1595 if (MatchAddress(N, AM)) |
1482 return false; | 1596 return false; |
1483 assert (T == AM.Segment); | 1597 assert (T == AM.Segment); |
1484 AM.Segment = Copy; | 1598 AM.Segment = Copy; |
1485 | 1599 |
1486 MVT VT = N.getSimpleValueType(); | 1600 MVT VT = N.getSimpleValueType(); |
1487 unsigned Complexity = 0; | 1601 unsigned Complexity = 0; |
1488 if (AM.BaseType == X86ISelAddressMode::RegBase) | 1602 if (AM.BaseType == X86ISelAddressMode::RegBase) |
1489 if (AM.Base_Reg.getNode()) | 1603 if (AM.Base_Reg.getNode()) |
1490 Complexity = 1; | 1604 Complexity = 1; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1534 SDValue &Disp, SDValue &Segment) { | 1648 SDValue &Disp, SDValue &Segment) { |
1535 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress); | 1649 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress); |
1536 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); | 1650 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); |
1537 | 1651 |
1538 X86ISelAddressMode AM; | 1652 X86ISelAddressMode AM; |
1539 AM.GV = GA->getGlobal(); | 1653 AM.GV = GA->getGlobal(); |
1540 AM.Disp += GA->getOffset(); | 1654 AM.Disp += GA->getOffset(); |
1541 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType()); | 1655 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType()); |
1542 AM.SymbolFlags = GA->getTargetFlags(); | 1656 AM.SymbolFlags = GA->getTargetFlags(); |
1543 | 1657 |
1544 if (N.getValueType() == MVT::i32) { | 1658 if (N.getValueType() == MVT::i32 && |
| 1659 !Subtarget->isTargetNaCl64()) { // @LOCALMOD |
1545 AM.Scale = 1; | 1660 AM.Scale = 1; |
1546 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32); | 1661 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32); |
1547 } else { | 1662 } else { |
1548 AM.IndexReg = CurDAG->getRegister(0, MVT::i64); | 1663 AM.IndexReg = CurDAG->getRegister(0, MVT::i64); |
1549 } | 1664 } |
1550 | 1665 |
1551 getAddressOperands(AM, Base, Scale, Index, Disp, Segment); | 1666 getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
1552 return true; | 1667 return true; |
1553 } | 1668 } |
1554 | 1669 |
1555 | 1670 |
1556 bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N, | 1671 bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N, |
1557 SDValue &Base, SDValue &Scale, | 1672 SDValue &Base, SDValue &Scale, |
1558 SDValue &Index, SDValue &Disp, | 1673 SDValue &Index, SDValue &Disp, |
1559 SDValue &Segment) { | 1674 SDValue &Segment) { |
1560 if (!ISD::isNON_EXTLoad(N.getNode()) || | 1675 if (!ISD::isNON_EXTLoad(N.getNode()) || |
1561 !IsProfitableToFold(N, P, P) || | 1676 !IsProfitableToFold(N, P, P) || |
1562 !IsLegalToFold(N, P, P, OptLevel)) | 1677 !IsLegalToFold(N, P, P, OptLevel)) |
1563 return false; | 1678 return false; |
1564 | 1679 |
1565 return SelectAddr(N.getNode(), | 1680 return SelectAddr(N.getNode(), |
1566 N.getOperand(1), Base, Scale, Index, Disp, Segment); | 1681 N.getOperand(1), Base, Scale, Index, Disp, Segment); |
1567 } | 1682 } |
1568 | 1683 |
| 1684 // @LOCALMOD-BEGIN |
| 1685 // LegalizeAddressingModeForNaCl - NaCl specific addressing fixes. This ensures |
| 1686 // two addressing mode invariants. |
| 1687 // |
| 1688 // case 1. Addressing using only a displacement (constant address references) |
| 1689 // is only legal when the displacement is positive. This is because, when |
| 1690 // later we replace |
| 1691 // movl 0xffffffff, %eax |
| 1692 // by |
| 1693 // movl 0xffffffff(%r15), %eax |
| 1694 // the displacement becomes a negative offset from %r15, making this a |
| 1695 // reference to the guard region below %r15 rather than to %r15 + 4GB - 1, |
| 1696 // as the programmer expected. To handle these cases we pull negative |
| 1697 // displacements out whenever there is no base or index register in the |
| 1698 // addressing mode. I.e., the above becomes |
| 1699 // movl $0xffffffff, %ebx |
| 1700 // movl %rbx, %rbx |
| 1701 // movl (%r15, %rbx, 1), %eax |
| 1702 // |
| 1703 // case 2. Because NaCl needs to zero the top 32-bits of the index, we can't |
| 1704 // allow the index register to be negative. However, if we are using a base |
| 1705 // frame index, global address or the constant pool, and AM.Disp > 0, then |
| 1706 // negative values of "index" may be expected to legally occur. |
| 1707 // To avoid this, we fold the displacement (and scale) back into the |
| 1708 // index. This results in a LEA before the current instruction. |
| 1709 // Unfortunately, this may add a requirement for an additional register. |
| 1710 // |
| 1711 // For example, this sandboxed code is broken if %eax is negative: |
| 1712 // |
| 1713 // movl %eax,%eax |
| 1714 // incl -30(%rbp,%rax,4) |
| 1715 // |
| 1716 // Instead, we now generate: |
| 1717 // leal -30(%rbp,%rax,4), %tmp |
| 1718 // movl %tmp,%tmp |
| 1719 // incl (%r15,%tmp,1) |
| 1720 // |
| 1721 // TODO(espindola): This might not be complete since the matcher can select |
| 1722 // any dag node to go in the index. This is also not how the rest of the |
| 1723 // matcher logic works, if the matcher selects something, it must be |
| 1724 // valid and not depend on further patching. A more desirable fix is |
| 1725 // probably to update the matching code to avoid assigning a register |
| 1726 // to a value that we cannot prove is positive. |
| 1727 // |
| 1728 // Note: Any changes to the testing logic need to be synchronized |
| 1729 // with the implementation of isLegalAddressingModeForNaCl() in |
| 1730 // X86FastISel.cpp. |
| 1731 void X86DAGToDAGISel::LegalizeAddressingModeForNaCl(SDValue N, |
| 1732 X86ISelAddressMode &AM) { |
| 1733 |
| 1734 |
| 1735 // RIP-relative addressing is always fine. |
| 1736 if (AM.isRIPRelative()) |
| 1737 return; |
| 1738 |
| 1739 SDLoc dl(N); |
| 1740 // Case 1 above: |
| 1741 if (!AM.hasBaseOrIndexReg() && !AM.hasSymbolicDisplacement() && AM.Disp < 0) { |
| 1742 SDValue Imm = CurDAG->getTargetConstant(AM.Disp, MVT::i32); |
| 1743 SDValue MovNode = |
| 1744 SDValue(CurDAG->getMachineNode(X86::MOV32ri, dl, MVT::i32, Imm), 0); |
| 1745 AM.IndexReg = MovNode; |
| 1746 AM.Disp = 0; |
| 1747 InsertDAGNode(*CurDAG, N, MovNode); |
| 1748 return; |
| 1749 } |
| 1750 |
| 1751 // MatchAddress wants to use the base register when there's only |
| 1752 // one register and no scale. We need to use the index register instead. |
| 1753 if (AM.BaseType == X86ISelAddressMode::RegBase && |
| 1754 AM.Base_Reg.getNode() && |
| 1755 !AM.IndexReg.getNode()) { |
| 1756 AM.IndexReg = AM.Base_Reg; |
| 1757 AM.setBaseReg(SDValue()); |
| 1758 } |
| 1759 |
| 1760 // Case 2 above comprises two sub-cases: |
| 1761 // sub-case 1: Prevent negative indexes |
| 1762 |
| 1763 // This is relevant only if there is an index register involved. |
| 1764 if (!AM.IndexReg.getNode()) |
| 1765 return; |
| 1766 |
| 1767 // There are two situations to deal with. The first is as described |
| 1768 // above, which is essentially a potentially negative index into an |
| 1769 // interior pointer to a stack-allocated structure. The second is a |
| 1770 // potentially negative index into an interior pointer to a global |
| 1771 // array. In this case, the global array will be a symbolic |
| 1772 // displacement. In theory, we could recognize that it is an |
| 1773 // interior pointer only when the concrete displacement AM.Disp is |
| 1774 // nonzero, but there is at least one test case (aha.c in the LLVM |
| 1775 // test suite) that incorrectly uses a negative index to dereference |
| 1776 // a global array, so we conservatively apply the translation to all |
| 1777 // global dereferences. |
| 1778 bool HasSymbolic = AM.hasSymbolicDisplacement(); |
| 1779 bool NeedsFixing1 = HasSymbolic || |
| 1780 (AM.BaseType == X86ISelAddressMode::FrameIndexBase && AM.Disp > 0); |
| 1781 |
| 1782 // sub-case 2: Both index and base registers are being used. The |
| 1783 // test for index register being used is done above, so we only need |
| 1784 // to test for a base register being used. |
| 1785 bool NeedsFixing2 = |
| 1786 (AM.BaseType == X86ISelAddressMode::RegBase) && AM.Base_Reg.getNode(); |
| 1787 |
| 1788 if (!NeedsFixing1 && !NeedsFixing2) |
| 1789 return; |
| 1790 |
| 1791 static const unsigned LogTable[] = { ~0u, 0, 1, ~0u, 2, ~0u, ~0u, ~0u, 3 }; |
| 1792 assert(AM.Scale < sizeof(LogTable)/sizeof(LogTable[0])); |
| 1793 unsigned ScaleLog = LogTable[AM.Scale]; |
| 1794 assert(ScaleLog <= 3); |
| 1795 SmallVector<SDNode*, 8> NewNodes; |
| 1796 |
| 1797 SDValue NewIndex = AM.IndexReg; |
| 1798 if (ScaleLog > 0) { |
| 1799 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8); |
| 1800 NewNodes.push_back(ShlCount.getNode()); |
| 1801 SDValue ShlNode = CurDAG->getNode(ISD::SHL, dl, N.getValueType(), |
| 1802 NewIndex, ShlCount); |
| 1803 NewNodes.push_back(ShlNode.getNode()); |
| 1804 NewIndex = ShlNode; |
| 1805 } |
| 1806 if (AM.Disp > 0 || HasSymbolic) { |
| 1807 // If the addressing mode has a potentially problematic |
| 1808 // displacement, we pull it out into a new instruction node. If |
| 1809 // it contains a symbolic displacement, we need to express it as a |
| 1810 // Wrapper node to make LLVM work. |
| 1811 SDValue Base, Scale, Index, Disp, Segment; |
| 1812 getAddressOperands(AM, Base, Scale, Index, Disp, Segment); |
| 1813 SDValue DispNode; |
| 1814 if (HasSymbolic) |
| 1815 DispNode = CurDAG->getNode(X86ISD::Wrapper, dl, N.getValueType(), Disp); |
| 1816 else |
| 1817 DispNode = CurDAG->getConstant(AM.Disp, N.getValueType()); |
| 1818 NewNodes.push_back(DispNode.getNode()); |
| 1819 |
| 1820 SDValue AddNode = CurDAG->getNode(ISD::ADD, dl, N.getValueType(), |
| 1821 NewIndex, DispNode); |
| 1822 NewNodes.push_back(AddNode.getNode()); |
| 1823 NewIndex = AddNode; |
| 1824 } |
| 1825 |
| 1826 if (NeedsFixing2) { |
| 1827 SDValue AddBase = CurDAG->getNode(ISD::ADD, dl, N.getValueType(), |
| 1828 NewIndex, AM.Base_Reg); |
| 1829 NewNodes.push_back(AddBase.getNode()); |
| 1830 NewIndex = AddBase; |
| 1831 AM.setBaseReg(SDValue()); |
| 1832 } |
| 1833 AM.Disp = 0; |
| 1834 AM.clearSymbolicDisplacement(); |
| 1835 AM.Scale = 1; |
| 1836 AM.IndexReg = NewIndex; |
| 1837 |
| 1838 // Insert the new nodes into the topological ordering. |
| 1839 for (unsigned i=0; i < NewNodes.size(); i++) { |
| 1840 if (NewNodes[i]->getNodeId() == -1 || |
| 1841 NewNodes[i]->getNodeId() > N.getNode()->getNodeId()) { |
| 1842 CurDAG->RepositionNode(N.getNode(), NewNodes[i]); |
| 1843 NewNodes[i]->setNodeId(N.getNode()->getNodeId()); |
| 1844 } |
| 1845 } |
| 1846 } |
| 1847 // @LOCALMOD-END |
| 1848 |
1569 /// getGlobalBaseReg - Return an SDNode that returns the value of | 1849 /// getGlobalBaseReg - Return an SDNode that returns the value of |
1570 /// the global base register. Output instructions required to | 1850 /// the global base register. Output instructions required to |
1571 /// initialize the global base register, if necessary. | 1851 /// initialize the global base register, if necessary. |
1572 /// | 1852 /// |
1573 SDNode *X86DAGToDAGISel::getGlobalBaseReg() { | 1853 SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
1574 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); | 1854 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
1575 return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy()).getNode(); | 1855 return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy()).getNode(); |
1576 } | 1856 } |
1577 | 1857 |
1578 /// Atomic opcode table | 1858 /// Atomic opcode table |
(...skipping 1266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2845 return false; | 3125 return false; |
2846 } | 3126 } |
2847 | 3127 |
2848 /// createX86ISelDag - This pass converts a legalized DAG into a | 3128 /// createX86ISelDag - This pass converts a legalized DAG into a |
2849 /// X86-specific DAG, ready for instruction scheduling. | 3129 /// X86-specific DAG, ready for instruction scheduling. |
2850 /// | 3130 /// |
2851 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, | 3131 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, |
2852 CodeGenOpt::Level OptLevel) { | 3132 CodeGenOpt::Level OptLevel) { |
2853 return new X86DAGToDAGISel(TM, OptLevel); | 3133 return new X86DAGToDAGISel(TM, OptLevel); |
2854 } | 3134 } |
OLD | NEW |