Introduce a new ReplaceCallWith method, which simplifies a lot of code.
[oota-llvm.git] / lib / Target / X86 / X86TargetAsmInfo.cpp
1 //===-- X86TargetAsmInfo.cpp - X86 asm properties ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations of the X86TargetAsmInfo properties.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86Subtarget.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Module.h"
22 #include "llvm/ADT/StringExtras.h"
23 using namespace llvm;
24
25 static const char* x86_asm_table[] = {"{si}", "S",
26                                       "{di}", "D",
27                                       "{ax}", "a",
28                                       "{cx}", "c",
29                                       "{memory}", "memory",
30                                       "{flags}", "",
31                                       "{dirflag}", "",
32                                       "{fpsr}", "",
33                                       "{cc}", "cc",
34                                       0,0};
35
36 X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) {
37   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
38   
39   // FIXME - Should be simplified.
40
41   AsmTransCBE = x86_asm_table;
42   
43   switch (Subtarget->TargetType) {
44   case X86Subtarget::isDarwin:
45     AlignmentIsInBytes = false;
46     GlobalPrefix = "_";
47     if (!Subtarget->is64Bit())
48       Data64bitsDirective = 0;       // we can't emit a 64-bit unit
49     ZeroDirective = "\t.space\t";  // ".space N" emits N zeros.
50     PrivateGlobalPrefix = "L";     // Marker for constant pool idxs
51     BSSSection = 0;                       // no BSS section.
52     ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
53     ConstantPoolSection = "\t.const\n";
54     JumpTableDataSection = "\t.const\n";
55     CStringSection = "\t.cstring";
56     FourByteConstantSection = "\t.literal4\n";
57     EightByteConstantSection = "\t.literal8\n";
58     if (Subtarget->is64Bit())
59       SixteenByteConstantSection = "\t.literal16\n";
60     ReadOnlySection = "\t.const\n";
61     LCOMMDirective = "\t.lcomm\t";
62     COMMDirectiveTakesAlignment = false;
63     HasDotTypeDotSizeDirective = false;
64     if (TM.getRelocationModel() == Reloc::Static) {
65       StaticCtorsSection = ".constructor";
66       StaticDtorsSection = ".destructor";
67     } else {
68       StaticCtorsSection = ".mod_init_func";
69       StaticDtorsSection = ".mod_term_func";
70     }
71     InlineAsmStart = "# InlineAsm Start";
72     InlineAsmEnd = "# InlineAsm End";
73     SetDirective = "\t.set";
74     UsedDirective = "\t.no_dead_strip\t";
75     WeakRefDirective = "\t.weak_reference\t";
76     HiddenDirective = "\t.private_extern\t";
77     
78     // In non-PIC modes, emit a special label before jump tables so that the
79     // linker can perform more accurate dead code stripping.
80     if (TM.getRelocationModel() != Reloc::PIC_) {
81       // Emit a local label that is preserved until the linker runs.
82       JumpTableSpecialLabelPrefix = "l";
83     }
84     
85     NeedsSet = true;
86     DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
87     DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
88     DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
89     DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
90     DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
91     DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
92     DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
93     DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
94     DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
95     DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
96     DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
97     break;
98
99   case X86Subtarget::isELF:
100     // Set up DWARF directives
101     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
102     AbsoluteSectionOffsets = true;
103     // bool HasLEB128; // Defaults to false.
104     // hasDotLoc - True if target asm supports .loc directives.
105     // bool HasDotLoc; // Defaults to false.
106     // HasDotFile - True if target asm supports .file directives.
107     // bool HasDotFile; // Defaults to false.
108     ReadOnlySection = "\t.section\t.rodata\n";
109     PrivateGlobalPrefix = ".L";
110     WeakRefDirective = "\t.weak\t";
111     DwarfRequiresFrameSection = false;
112     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",@progbits";
113     DwarfInfoSection =    "\t.section\t.debug_info,\"\",@progbits";
114     DwarfLineSection =    "\t.section\t.debug_line,\"\",@progbits";
115     DwarfFrameSection =   "\t.section\t.debug_frame,\"\",@progbits";
116     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",@progbits";
117     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",@progbits";
118     DwarfStrSection =     "\t.section\t.debug_str,\"\",@progbits";
119     DwarfLocSection =     "\t.section\t.debug_loc,\"\",@progbits";
120     DwarfARangesSection = "\t.section\t.debug_aranges,\"\",@progbits";
121     DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",@progbits";
122     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",@progbits";
123     break;
124
125   case X86Subtarget::isCygwin:
126   case X86Subtarget::isMingw:
127     GlobalPrefix = "_";
128     LCOMMDirective = "\t.lcomm\t";
129     COMMDirectiveTakesAlignment = false;
130     HasDotTypeDotSizeDirective = false;
131     StaticCtorsSection = "\t.section .ctors,\"aw\"";
132     StaticDtorsSection = "\t.section .dtors,\"aw\"";
133     HiddenDirective = NULL;
134
135     // Set up DWARF directives
136     HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
137     AbsoluteSectionOffsets = true;
138     PrivateGlobalPrefix = "L";  // Prefix for private global symbols
139     WeakRefDirective = "\t.weak\t";
140     DwarfRequiresFrameSection = false;
141     DwarfSectionOffsetDirective = "\t.secrel32\t";
142     DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"dr\"";
143     DwarfInfoSection =    "\t.section\t.debug_info,\"dr\"";
144     DwarfLineSection =    "\t.section\t.debug_line,\"dr\"";
145     DwarfFrameSection =   "\t.section\t.debug_frame,\"dr\"";
146     DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"dr\"";
147     DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"dr\"";
148     DwarfStrSection =     "\t.section\t.debug_str,\"dr\"";
149     DwarfLocSection =     "\t.section\t.debug_loc,\"dr\"";
150     DwarfARangesSection = "\t.section\t.debug_aranges,\"dr\"";
151     DwarfRangesSection =  "\t.section\t.debug_ranges,\"dr\"";
152     DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"dr\"";
153     break;
154
155   case X86Subtarget::isWindows:
156     GlobalPrefix = "_";
157     HasDotTypeDotSizeDirective = false;
158     break;
159
160   default: break;
161   }
162   
163   if (Subtarget->isFlavorIntel()) {
164     GlobalPrefix = "_";
165     CommentString = ";";
166   
167     PrivateGlobalPrefix = "$";
168     AlignDirective = "\talign\t";
169     ZeroDirective = "\tdb\t";
170     ZeroDirectiveSuffix = " dup(0)";
171     AsciiDirective = "\tdb\t";
172     AscizDirective = 0;
173     Data8bitsDirective = "\tdb\t";
174     Data16bitsDirective = "\tdw\t";
175     Data32bitsDirective = "\tdd\t";
176     Data64bitsDirective = "\tdq\t";
177     HasDotTypeDotSizeDirective = false;
178     
179     TextSection = "_text";
180     DataSection = "_data";
181     JumpTableDataSection = NULL;
182     SwitchToSectionDirective = "";
183     TextSectionStartSuffix = "\tsegment 'CODE'";
184     DataSectionStartSuffix = "\tsegment 'DATA'";
185     SectionEndDirectiveSuffix = "\tends\n";
186   }
187
188   AssemblerDialect = Subtarget->getAsmFlavor();
189 }
190
191 bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const {
192   // FIXME: this should verify that we are targetting a 486 or better.  If not,
193   // we will turn this bswap into something that will be lowered to logical ops
194   // instead of emitting the bswap asm.  For now, we don't support 486 or lower
195   // so don't worry about this.
196   
197   // Verify this is a simple bswap.
198   if (CI->getNumOperands() != 2 ||
199       CI->getType() != CI->getOperand(1)->getType() ||
200       !CI->getType()->isInteger())
201     return false;
202   
203   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
204   if (!Ty || Ty->getBitWidth() % 16 != 0)
205     return false;
206   
207   // Okay, we can do this xform, do so now.
208   const Type *Tys[] = { Ty, Ty };
209   Module *M = CI->getParent()->getParent()->getParent();
210   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
211   
212   Value *Op = CI->getOperand(1);
213   Op = new CallInst(Int, Op, CI->getName(), CI);
214   
215   CI->replaceAllUsesWith(Op);
216   CI->eraseFromParent();
217   return true;
218 }
219
220
221 bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const {
222   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
223   std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
224   
225   std::string AsmStr = IA->getAsmString();
226   
227   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
228   std::vector<std::string> AsmPieces;
229   SplitString(AsmStr, AsmPieces, "\n");  // ; as separator?
230   
231   switch (AsmPieces.size()) {
232   default: return false;    
233   case 1:
234     AsmStr = AsmPieces[0];
235     AsmPieces.clear();
236     SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
237     
238     // bswap $0
239     if (AsmPieces.size() == 2 && 
240         AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") {
241       // No need to check constraints, nothing other than the equivalent of
242       // "=r,0" would be valid here.
243       return LowerToBSwap(CI);
244     }
245     break;
246   case 3:
247     if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
248         Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
249         Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
250       // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
251       std::vector<std::string> Words;
252       SplitString(AsmPieces[0], Words, " \t");
253       if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
254         Words.clear();
255         SplitString(AsmPieces[1], Words, " \t");
256         if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
257           Words.clear();
258           SplitString(AsmPieces[2], Words, " \t,");
259           if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
260               Words[2] == "%edx") {
261             return LowerToBSwap(CI);
262           }
263         }
264       }
265     }
266     break;
267   }
268   return false;
269 }