llvm.org GIT mirror llvm / 6994040
This is the patch to provide clean intrinsic function overloading support in LLVM. It cleans up the intrinsic definitions and generally smooths the process for more complicated intrinsic writing. It will be used by the upcoming atomic intrinsics as well as vector and float intrinsics in the future. This also changes the syntax for llvm.bswap, llvm.part.set, llvm.part.select, and llvm.ct* intrinsics. They are automatically upgraded by both the LLVM ASM reader and the bitcode reader. The test cases have been updated, with special tests added to ensure the automatic upgrading is supported. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40807 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 12 years ago
27 changed file(s) with 622 addition(s) and 338 deletion(s). Raw diff Collapse all Expand all
37133713 of the LLVM language, it is required if any are added that they be documented
37143714 here.

37153715
3716

Some intrinsic functions can be overloaded, i.e., the intrinsic represents

3717 a family of functions that perform the same operation but on different data
3718 types. This is most frequent with the integer types. Since LLVM can represent
3719 over 8 million different integer types, there is a way to declare an intrinsic
3720 that can be overloaded based on its arguments. Such an intrinsic will have the
3721 names of its argument types encoded into its function name, each
3722 preceded by a period. For example, the llvm.ctpop function can take an
3723 integer of any width. This leads to a family of functions such as
3724 i32 @llvm.ctpop.i8(i8 %val) and i32 @llvm.ctpop.i29(i29 %val).
3725

3726
3716

Some intrinsic functions can be overloaded, i.e., the intrinsic represents

3717 a family of functions that perform the same operation but on different data
3718 types. Because LLVM can represent over 8 million different integer types,
3719 overloading is used commonly to allow an intrinsic function to operate on any
3720 integer type. One or more of the argument types or the result type can be
3721 overloaded to accept any integer type. Argument types may also be defined as
3722 exactly matching a previous argument's type or the result type. This allows an
3723 intrinsic function which accepts multiple arguments, but needs all of them to
3724 be of the same type, to only be overloaded with respect to a single argument or
3725 the result.

3726
3727

Overloaded intrinsics will have the names of its overloaded argument types

3728 encoded into its function name, each preceded by a period. Only those types
3729 which are overloaded result in a name suffix. Arguments whose type is matched
3730 against another type do not. For example, the llvm.ctpop function can
3731 take an integer of any width and returns an integer of exactly the same integer
3732 width. This leads to a family of functions such as
3733 i8 @llvm.ctpop.i8(i8 %val) and i29 @llvm.ctpop.i29(i29 %val).
3734 Only one type, the return type, is overloaded, and only one type suffix is
3735 required. Because the argument's type is matched against the return type, it
3736 does not require its own name suffix.

37273737
37283738

To learn how to add an intrinsic function, please see the

37293739 Extending LLVM Guide.
45574567
45584568
Syntax:
45594569

This is an overloaded intrinsic function. You can use bswap on any integer

4560 type that is an even number of bytes (i.e. BitWidth % 16 == 0). Note the suffix
4561 that includes the type for the result and the operand.
4562

                  
                
4563 declare i16 @llvm.bswap.i16.i16(i16 <id>)
4564 declare i32 @llvm.bswap.i32.i32(i32 <id>)
4565 declare i64 @llvm.bswap.i64.i64(i64 <id>)
4570 type that is an even number of bytes (i.e. BitWidth % 16 == 0).
4571

                  
                
4572 declare i16 @llvm.bswap.i16(i16 <id>)
4573 declare i32 @llvm.bswap.i32(i32 <id>)
4574 declare i64 @llvm.bswap.i64(i64 <id>)
45664575
45674576
45684577
Overview:
45774586
Semantics:
45784587
45794588

4580 The llvm.bswap.16.i16 intrinsic returns an i16 value that has the high
4589 The llvm.bswap.i16 intrinsic returns an i16 value that has the high
45814590 and low byte of the input i16 swapped. Similarly, the llvm.bswap.i32
45824591 intrinsic returns an i32 value that has the four bytes of the input i32
45834592 swapped, so that if the input bytes are numbered 0, 1, 2, 3 then the returned
4584 i32 will have its bytes in 3, 2, 1, 0 order. The llvm.bswap.i48.i48,
4585 llvm.bswap.i64.i64 and other intrinsics extend this concept to
4593 i32 will have its bytes in 3, 2, 1, 0 order. The llvm.bswap.i48,
4594 llvm.bswap.i64 and other intrinsics extend this concept to
45864595 additional even-byte lengths (6 bytes, 8 bytes and more, respectively).
45874596

45884597
45994608

This is an overloaded intrinsic. You can use llvm.ctpop on any integer bit

46004609 width. Not all targets support all bit widths however.
46014610

                  
                
4602 declare i32 @llvm.ctpop.i8 (i8 <src>)
4603 declare i32 @llvm.ctpop.i16(i16 <src>)
4611 declare i8 @llvm.ctpop.i8 (i8 <src>)
4612 declare i16 @llvm.ctpop.i16(i16 <src>)
46044613 declare i32 @llvm.ctpop.i32(i32 <src>)
4605 declare i32 @llvm.ctpop.i64(i64 <src>)
4606 declare i32 @llvm.ctpop.i256(i256 <src>)
4614 declare i64 @llvm.ctpop.i64(i64 <src>)
4615 declare i256 @llvm.ctpop.i256(i256 <src>)
46074616
46084617
46094618
Overview:
46384647

This is an overloaded intrinsic. You can use llvm.ctlz on any

46394648 integer bit width. Not all targets support all bit widths however.
46404649

                  
                
4641 declare i32 @llvm.ctlz.i8 (i8 <src>)
4642 declare i32 @llvm.ctlz.i16(i16 <src>)
4650 declare i8 @llvm.ctlz.i8 (i8 <src>)
4651 declare i16 @llvm.ctlz.i16(i16 <src>)
46434652 declare i32 @llvm.ctlz.i32(i32 <src>)
4644 declare i32 @llvm.ctlz.i64(i64 <src>)
4645 declare i32 @llvm.ctlz.i256(i256 <src>)
4653 declare i64 @llvm.ctlz.i64(i64 <src>)
4654 declare i256 @llvm.ctlz.i256(i256 <src>)
46464655
46474656
46484657
Overview:
46814690

This is an overloaded intrinsic. You can use llvm.cttz on any

46824691 integer bit width. Not all targets support all bit widths however.
46834692

                  
                
4684 declare i32 @llvm.cttz.i8 (i8 <src>)
4685 declare i32 @llvm.cttz.i16(i16 <src>)
4693 declare i8 @llvm.cttz.i8 (i8 <src>)
4694 declare i16 @llvm.cttz.i16(i16 <src>)
46864695 declare i32 @llvm.cttz.i32(i32 <src>)
4687 declare i32 @llvm.cttz.i64(i64 <src>)
4688 declare i32 @llvm.cttz.i256(i256 <src>)
4696 declare i64 @llvm.cttz.i64(i64 <src>)
4697 declare i256 @llvm.cttz.i256(i256 <src>)
46894698
46904699
46914700
Overview:
47224731

This is an overloaded intrinsic. You can use llvm.part.select

47234732 on any integer bit width.
47244733

                  
                
4725 declare i17 @llvm.part.select.i17.i17 (i17 %val, i32 %loBit, i32 %hiBit)
4726 declare i29 @llvm.part.select.i29.i29 (i29 %val, i32 %loBit, i32 %hiBit)
4734 declare i17 @llvm.part.select.i17 (i17 %val, i32 %loBit, i32 %hiBit)
4735 declare i29 @llvm.part.select.i29 (i29 %val, i32 %loBit, i32 %hiBit)
47274736
47284737
47294738
Overview:
47694778

This is an overloaded intrinsic. You can use llvm.part.set

47704779 on any integer bit width.
47714780

                  
                
4772 declare i17 @llvm.part.set.i17.i17.i9 (i17 %val, i9 %repl, i32 %lo, i32 %hi)
4773 declare i29 @llvm.part.set.i29.i29.i9 (i29 %val, i9 %repl, i32 %lo, i32 %hi)
4781 declare i17 @llvm.part.set.i17.i9 (i17 %val, i9 %repl, i32 %lo, i32 %hi)
4782 declare i29 @llvm.part.set.i29.i9 (i29 %val, i9 %repl, i32 %lo, i32 %hi)
47744783
47754784
47764785
Overview:
0 //===-- llvm/AutoUpgrade.h - AutoUpgrade Helpers ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chandler Carruth is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These functions are implemented by lib/VMCore/AutoUpgrade.cpp.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_AUTOUPGRADE_H
14 #define LLVM_AUTOUPGRADE_H
15
16 namespace llvm {
17 class Function;
18 class CallInst;
19 class BasicBlock;
20
21 /// This is a more granular function that simply checks an intrinsic function
22 /// for upgrading, and if it requires upgrading provides the new function.
23 Function* UpgradeIntrinsicFunction(Function *F);
24
25 /// This is the complement to the above, replacing a specific call to an
26 /// intrinsic function with a call to the specified new function.
27 void UpgradeIntrinsicCall(CallInst *CI, Function *NewFn);
28
29 /// This is an auto-upgrade hook for any old intrinsic function syntaxes
30 /// which need to have both the function updated as well as all calls updated
31 /// to the new function. This should only be run in a post-processing fashion
32 /// so that it can update all calls to the old function.
33 void UpgradeCallsToIntrinsic(Function* F);
34
35 } // End llvm namespace
36
37 #endif
6666
6767 LAST_VALUETYPE = 27, // This always remains at the end of the list.
6868
69 // iAny - An integer value of any bit width. This is used for intrinsics
70 // that have overloadings based on integer bit widths. This is only for
71 // tblgen's consumption!
72 iAny = 254,
69 // iAny - An integer or vector integer value of any bit width. This is
70 // used for intrinsics that have overloadings based on integer bit widths.
71 // This is only for tblgen's consumption!
72 iAny = 254,
7373
7474 // iPTR - An int value the size of the pointer of the current
7575 // target. This should only be used internal to tblgen!
76 iPTR = 255
76 iPTR = 255
7777 };
7878
7979 /// MVT::ValueType - This type holds low-level value types. Valid values
4949 def v2f64 : ValueType<128, 26>; // 2 x f64 vector value
5050
5151 // Pseudo valuetype to represent "integer of any bit width"
52 def iAny : ValueType<0 , 254>; // integer value of any bit width
52 def iAny : ValueType<0 , 254>;
5353
5454 // Pseudo valuetype mapped to the current pointer size.
5555 def iPTR : ValueType<0 , 255>;
5151 // Types used by intrinsics.
5252 //===----------------------------------------------------------------------===//
5353
54 class LLVMType, string typeval> {
54 class LLVMType> {
5555 ValueType VT = vt;
56 string TypeVal = typeval;
57 }
58
59 class LLVMIntegerType
60 : LLVMType {
61 int Width = width;
62 }
63
64 class LLVMVectorType
65 : LLVMType{
66 int NumElts = numelts;
56 }
57
58 class LLVMPointerType
59 : LLVMType{
6760 LLVMType ElTy = elty;
6861 }
6962
70 class LLVMPointerType
71 : LLVMType{
72 LLVMType ElTy = elty;
63 class LLVMMatchType
64 : LLVMType{
65 int Number = num;
7366 }
7467
75 class LLVMEmptyStructType
76 : LLVMType{
77 }
78
79 def llvm_void_ty : LLVMType;
80 def llvm_int_ty : LLVMIntegerType;
81 def llvm_i1_ty : LLVMIntegerType;
82 def llvm_i8_ty : LLVMIntegerType;
83 def llvm_i16_ty : LLVMIntegerType;
84 def llvm_i32_ty : LLVMIntegerType;
85 def llvm_i64_ty : LLVMIntegerType;
86 def llvm_float_ty : LLVMType;
87 def llvm_double_ty : LLVMType>;
68 def llvm_void_ty : LLVMType>;
69 def llvm_anyint_ty : LLVMType;
70 def llvm_i1_ty : LLVMType;
71 def llvm_i8_ty : LLVMType;
72 def llvm_i16_ty : LLVMType;
73 def llvm_i32_ty : LLVMType;
74 def llvm_i64_ty : LLVMType;
75 def llvm_float_ty : LLVMType;
76 def llvm_double_ty : LLVMType;
8877 def llvm_ptr_ty : LLVMPointerType; // i8*
8978 def llvm_ptrptr_ty : LLVMPointerType; // i8**
90 def llvm_empty_ty : LLVMEmptyStructType; // { }
79 def llvm_empty_ty : LLVMType; // { }
9180 def llvm_descriptor_ty : LLVMPointerType; // { }*
9281
93 def llvm_v16i8_ty : LLVMVectorType; // 16 x i8
94 def llvm_v8i16_ty : LLVMVectorType; // 8 x i16
95 def llvm_v2i64_ty : LLVMVectorType; // 2 x i64
96 def llvm_v2i32_ty : LLVMVectorType; // 2 x i32
97 def llvm_v1i64_ty : LLVMVectorType; // 1 x i64
98 def llvm_v4i32_ty : LLVMVectorType; // 4 x i32
99 def llvm_v4f32_ty : LLVMVectorType; // 4 x float
100 def llvm_v2f64_ty : LLVMVectorType;// 2 x double
82 def llvm_v16i8_ty : LLVMType; // 16 x i8
83 def llvm_v8i16_ty : LLVMType; // 8 x i16
84 def llvm_v2i64_ty : LLVMType; // 2 x i64
85 def llvm_v2i32_ty : LLVMType; // 2 x i32
86 def llvm_v1i64_ty : LLVMType; // 1 x i64
87 def llvm_v4i32_ty : LLVMType; // 4 x i32
88 def llvm_v4f32_ty : LLVMType; // 4 x float
89 def llvm_v2f64_ty : LLVMType; // 2 x double
10190
10291 // MMX Vector Types
103 def llvm_v8i8_ty : LLVMVectorType; // 8 x i8
104 def llvm_v4i16_ty : LLVMVectorType; // 4 x i16
105
106 def llvm_vararg_ty : LLVMType; // vararg
92 def llvm_v8i8_ty : LLVMType; // 8 x i8
93 def llvm_v4i16_ty : LLVMType; // 4 x i16
94
95 def llvm_vararg_ty : LLVMType; // this means vararg here
10796
10897 //===----------------------------------------------------------------------===//
10998 // Intrinsic Definitions.
184173 }
185174
186175 let Properties = [IntrNoMem] in {
187 def int_sqrt_f32 : Intrinsic<[llvm_float_ty , llvm_float_ty]>;
176 def int_sqrt_f32 : Intrinsic<[llvm_float_ty, llvm_float_ty]>;
188177 def int_sqrt_f64 : Intrinsic<[llvm_double_ty, llvm_double_ty]>;
189178
190 def int_powi_f32 : Intrinsic<[llvm_float_ty , llvm_float_ty, llvm_i32_ty]>;
179 def int_powi_f32 : Intrinsic<[llvm_float_ty, llvm_float_ty, llvm_i32_ty]>;
191180 def int_powi_f64 : Intrinsic<[llvm_double_ty, llvm_double_ty, llvm_i32_ty]>;
192181 }
193182
202191
203192 // None of these intrinsics accesses memory at all.
204193 let Properties = [IntrNoMem] in {
205 def int_bswap: Intrinsic<[llvm_int_ty, llvm_int_ty]>;
206 def int_ctpop: Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
207 def int_ctlz : Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
208 def int_cttz : Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
194 def int_bswap: Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>]>;
195 def int_ctpop: Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>]>;
196 def int_ctlz : Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>]>;
197 def int_cttz : Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>]>;
209198 def int_part_select :
210 Intrinsic<[llvm_int_ty, llvm_int_ty, llvm_i32_ty, llvm_i32_ty]>;
199 Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>, llvm_i32_ty, llvm_i32_ty]>;
211200 def int_part_set :
212 Intrinsic<[llvm_int_ty, llvm_int_ty, llvm_int_ty, llvm_i32_ty,
201 Intrinsic<[llvm_anyint_ty, LLVMMatchType<0>, llvm_anyint_ty, llvm_i32_ty,
213202 llvm_i32_ty]>;
214203 }
215204
447447 return ConstantInt::get(Op->getValue().byteSwap());
448448 } else if (Name.size() > 11 && !memcmp(&Name[0],"llvm.ctpop",10)) {
449449 uint64_t ctpop = Op->getValue().countPopulation();
450 return ConstantInt::get(Type::Int32Ty, ctpop);
450 return ConstantInt::get(Ty, ctpop);
451451 } else if (Name.size() > 10 && !memcmp(&Name[0], "llvm.cttz", 9)) {
452452 uint64_t cttz = Op->getValue().countTrailingZeros();
453 return ConstantInt::get(Type::Int32Ty, cttz);
453 return ConstantInt::get(Ty, cttz);
454454 } else if (Name.size() > 10 && !memcmp(&Name[0], "llvm.ctlz", 9)) {
455455 uint64_t ctlz = Op->getValue().countLeadingZeros();
456 return ConstantInt::get(Type::Int32Ty, ctlz);
456 return ConstantInt::get(Ty, ctlz);
457457 }
458458 }
459459 } else if (NumOperands == 2) {
1717 #include "llvm/Instructions.h"
1818 #include "llvm/Module.h"
1919 #include "llvm/ValueSymbolTable.h"
20 #include "llvm/AutoUpgrade.h"
2021 #include "llvm/Support/GetElementPtrTypeIterator.h"
2122 #include "llvm/Support/CommandLine.h"
2223 #include "llvm/ADT/SmallVector.h"
129130 GenerateError(UndefinedReferences);
130131 return;
131132 }
133
134 // Look for intrinsic functions and CallInst that need to be upgraded
135 for (Module::iterator FI = CurrentModule->begin(),
136 FE = CurrentModule->end(); FI != FE; )
137 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
132138
133139 Values.clear(); // Clear out function local definitions
134140 Types.clear();
1818 #include "llvm/Instructions.h"
1919 #include "llvm/Module.h"
2020 #include "llvm/ParameterAttributes.h"
21 #include "llvm/AutoUpgrade.h"
2122 #include "llvm/ADT/SmallString.h"
2223 #include "llvm/Support/MathExtras.h"
2324 #include "llvm/Support/MemoryBuffer.h"
855856 return Error("Malformed global initializer set");
856857 if (!FunctionsWithBodies.empty())
857858 return Error("Too few function bodies found");
859
860 // Look for intrinsic functions which need to be upgraded at some point
861 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
862 FI != FE; ++FI) {
863 if (Function* NewFn = UpgradeIntrinsicFunction(FI))
864 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
865 }
858866
859867 // Force deallocation of memory for these vectors to favor the client that
860868 // want lazy deserialization.
15871595 if (ErrInfo) *ErrInfo = ErrorString;
15881596 return true;
15891597 }
1598
1599 // Upgrade any old intrinsic calls in the function.
1600 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
1601 E = UpgradedIntrinsics.end(); I != E; ++I) {
1602 if (I->first != I->second) {
1603 for (Value::use_iterator UI = I->first->use_begin(),
1604 UE = I->first->use_end(); UI != UE; ) {
1605 if (CallInst* CI = dyn_cast(*UI++))
1606 UpgradeIntrinsicCall(CI, I->second);
1607 }
1608 }
1609 }
15901610
15911611 return false;
15921612 }
16131633 materializeFunction(F, ErrInfo))
16141634 return 0;
16151635 }
1636
1637 // Upgrade any intrinsic calls that slipped through (should not happen!) and
1638 // delete the old functions to clean up. We can't do this unless the entire
1639 // module is materialized because there could always be another function body
1640 // with calls to the old function.
1641 for (std::vector >::iterator I =
1642 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
1643 if (I->first != I->second) {
1644 for (Value::use_iterator UI = I->first->use_begin(),
1645 UE = I->first->use_end(); UI != UE; ) {
1646 if (CallInst* CI = dyn_cast(*UI++))
1647 UpgradeIntrinsicCall(CI, I->second);
1648 }
1649 ValueList.replaceUsesOfWith(I->first, I->second);
1650 I->first->eraseFromParent();
1651 }
1652 }
1653 std::vector >().swap(UpgradedIntrinsics);
1654
16161655 return TheModule;
16171656 }
16181657
101101 // When reading the module header, this list is populated with functions that
102102 // have bodies later in the file.
103103 std::vector FunctionsWithBodies;
104
105 // When intrinsic functions are encountered which require upgrading they are
106 // stored here with their replacement function.
107 typedef std::vector > UpgradedIntrinsicMap;
108 UpgradedIntrinsicMap UpgradedIntrinsics;
104109
105110 // After the module header has been read, the FunctionsWithBodies list is
106111 // reversed. This keeps track of whether we've done this yet.
28132813 SDOperand Arg = getValue(I.getOperand(1));
28142814 MVT::ValueType Ty = Arg.getValueType();
28152815 SDOperand result = DAG.getNode(ISD::CTTZ, Ty, Arg);
2816 if (Ty < MVT::i32)
2817 result = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, result);
2818 else if (Ty > MVT::i32)
2819 result = DAG.getNode(ISD::TRUNCATE, MVT::i32, result);
28202816 setValue(&I, result);
28212817 return 0;
28222818 }
28242820 SDOperand Arg = getValue(I.getOperand(1));
28252821 MVT::ValueType Ty = Arg.getValueType();
28262822 SDOperand result = DAG.getNode(ISD::CTLZ, Ty, Arg);
2827 if (Ty < MVT::i32)
2828 result = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, result);
2829 else if (Ty > MVT::i32)
2830 result = DAG.getNode(ISD::TRUNCATE, MVT::i32, result);
28312823 setValue(&I, result);
28322824 return 0;
28332825 }
28352827 SDOperand Arg = getValue(I.getOperand(1));
28362828 MVT::ValueType Ty = Arg.getValueType();
28372829 SDOperand result = DAG.getNode(ISD::CTPOP, Ty, Arg);
2838 if (Ty < MVT::i32)
2839 result = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, result);
2840 else if (Ty > MVT::i32)
2841 result = DAG.getNode(ISD::TRUNCATE, MVT::i32, result);
28422830 setValue(&I, result);
28432831 return 0;
28442832 }
218218 return false;
219219
220220 // Okay, we can do this xform, do so now.
221 const Type *Tys[] = { Ty, Ty };
221 const Type *Tys[] = { Ty };
222222 Module *M = CI->getParent()->getParent()->getParent();
223 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
223 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
224224
225225 Value *Op = CI->getOperand(1);
226226 Op = new CallInst(Int, Op, CI->getName(), CI);
37163716 for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
37173717 if (ByteValues[i] != V)
37183718 return 0;
3719 const Type *Tys[] = { ITy, ITy };
3719 const Type *Tys[] = { ITy };
37203720 Module *M = I.getParent()->getParent()->getParent();
3721 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2);
3721 Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
37223722 return new CallInst(F, V);
37233723 }
37243724
0 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Chandler Carruth and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the auto-upgrade helper functions
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/AutoUpgrade.h"
14 #include "llvm/Function.h"
15 #include "llvm/Module.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/ParameterAttributes.h"
18 #include "llvm/Intrinsics.h"
19 using namespace llvm;
20
21
22 Function* llvm::UpgradeIntrinsicFunction(Function *F) {
23 assert(F && "Illegal to upgrade a non-existent Function.");
24
25 // Get the Function's name.
26 const std::string& Name = F->getName();
27
28 // Convenience
29 const FunctionType *FTy = F->getFunctionType();
30
31 // Quickly eliminate it, if it's not a candidate.
32 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
33 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
34 return 0;
35
36 Module *M = F->getParent();
37 switch (Name[5]) {
38 default: break;
39 case 'b':
40 // This upgrades the name of the llvm.bswap intrinsic function to only use
41 // a single type name for overloading. We only care about the old format
42 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
43 // a '.' after 'bswap.'
44 if (Name.compare(5,6,"bswap.",6) == 0) {
45 std::string::size_type delim = Name.find('.',11);
46
47 if (delim != std::string::npos) {
48 // Construct the new name as 'llvm.bswap' + '.i*'
49 F->setName(Name.substr(0,10)+Name.substr(delim));
50 return F;
51 }
52 }
53 break;
54
55 case 'c':
56 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
57 // correct return type, so we check for the name, and then check if the
58 // return type does not match the parameter type.
59 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
60 Name.compare(5,4,"ctlz",4) == 0 ||
61 Name.compare(5,4,"cttz",4) == 0) &&
62 FTy->getReturnType() != FTy->getParamType(0)) {
63 // We first need to change the name of the old (bad) intrinsic, because
64 // its type is incorrect, but we cannot overload that name. We
65 // arbitrarily unique it here allowing us to construct a correctly named
66 // and typed function below.
67 F->setName("");
68
69 // Now construct the new intrinsic with the correct name and type. We
70 // leave the old function around in order to query its type, whatever it
71 // may be, and correctly convert up to the new type.
72 return cast(M->getOrInsertFunction(Name,
73 FTy->getParamType(0),
74 FTy->getParamType(0),
75 (Type *)0));
76 }
77 break;
78
79 case 'p':
80 // This upgrades the llvm.part.select overloaded intrinsic names to only
81 // use one type specifier in the name. We only care about the old format
82 // 'llvm.part.select.i*.i*', and solve as above with bswap.
83 if (Name.compare(5,12,"part.select.",12) == 0) {
84 std::string::size_type delim = Name.find('.',17);
85
86 if (delim != std::string::npos) {
87 // Construct a new name as 'llvm.part.select' + '.i*'
88 F->setName(Name.substr(0,16)+Name.substr(delim));
89 return F;
90 }
91 break;
92 }
93
94 // This upgrades the llvm.part.set intrinsics similarly as above, however
95 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
96 // must match. There is an additional type specifier after these two
97 // matching types that we must retain when upgrading. Thus, we require
98 // finding 2 periods, not just one, after the intrinsic name.
99 if (Name.compare(5,9,"part.set.",9) == 0) {
100 std::string::size_type delim = Name.find('.',14);
101
102 if (delim != std::string::npos &&
103 Name.find('.',delim+1) != std::string::npos) {
104 // Construct a new name as 'llvm.part.select' + '.i*.i*'
105 F->setName(Name.substr(0,13)+Name.substr(delim));
106 return F;
107 }
108 break;
109 }
110
111 break;
112 }
113
114 // This may not belong here. This function is effectively being overloaded
115 // to both detect an intrinsic which needs upgrading, and to provide the
116 // upgraded form of the intrinsic. We should perhaps have two separate
117 // functions for this.
118 return 0;
119 }
120
121 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
122 // upgraded intrinsic. All argument and return casting must be provided in
123 // order to seamlessly integrate with existing context.
124 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
125 assert(NewFn && "Cannot upgrade an intrinsic call without a new function.");
126
127 Function *F = CI->getCalledFunction();
128 assert(F && "CallInst has no function associated with it.");
129
130 const FunctionType *FTy = F->getFunctionType();
131 const FunctionType *NewFnTy = NewFn->getFunctionType();
132
133 switch(NewFn->getIntrinsicID()) {
134 default: assert(0 && "Unknown function for CallInst upgrade.");
135 case Intrinsic::ctlz:
136 case Intrinsic::ctpop:
137 case Intrinsic::cttz:
138 // Build a small vector of the 1..(N-1) operands, which are the
139 // parameters.
140 SmallVector Operands(CI->op_begin()+1, CI->op_end());
141
142 // Construct a new CallInst
143 CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(),
144 "upgraded."+CI->getName(), CI);
145 NewCI->setTailCall(CI->isTailCall());
146 NewCI->setCallingConv(CI->getCallingConv());
147
148 // Handle any uses of the old CallInst.
149 if (!CI->use_empty()) {
150 // Check for sign extend parameter attributes on the return values.
151 bool SrcSExt = NewFnTy->getParamAttrs() &&
152 NewFnTy->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
153 bool DestSExt = FTy->getParamAttrs() &&
154 FTy->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt);
155
156 // Construct an appropriate cast from the new return type to the old.
157 CastInst *RetCast = CastInst::create(
158 CastInst::getCastOpcode(NewCI, SrcSExt,
159 F->getReturnType(),
160 DestSExt),
161 NewCI, F->getReturnType(),
162 NewCI->getName(), CI);
163 NewCI->moveBefore(RetCast);
164
165 // Replace all uses of the old call with the new cast which has the
166 // correct type.
167 CI->replaceAllUsesWith(RetCast);
168 }
169
170 // Clean up the old call now that it has been completely upgraded.
171 CI->eraseFromParent();
172 break;
173 }
174 }
175
176 // This tests each Function to determine if it needs upgrading. When we find
177 // one we are interested in, we then upgrade all calls to reflect the new
178 // function.
179 void llvm::UpgradeCallsToIntrinsic(Function* F) {
180 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
181
182 // Upgrade the function and check if it is a totaly new function.
183 if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
184 if (NewFn != F) {
185 // Replace all uses to the old function with the new one if necessary.
186 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
187 UI != UE; ) {
188 if (CallInst* CI = dyn_cast(*UI++))
189 UpgradeIntrinsicCall(CI, NewFn);
190 }
191 // Remove old function, no longer used, from the module.
192 F->eraseFromParent();
193 }
194 }
195 }
196
5252 #include "llvm/Intrinsics.h"
5353 #include "llvm/PassManager.h"
5454 #include "llvm/Analysis/Dominators.h"
55 #include "llvm/CodeGen/ValueTypes.h"
5556 #include "llvm/Support/CFG.h"
5657 #include "llvm/Support/InstVisitor.h"
5758 #include "llvm/Support/Streams.h"
224225 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
225226 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
226227
227 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...);
228 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F,
229 unsigned Count, ...);
228230
229231 void WriteValue(const Value *V) {
230232 if (!V) return;
10741076 /// VerifyIntrinsicPrototype - TableGen emits calls to this function into
10751077 /// Intrinsics.gen. This implements a little state machine that verifies the
10761078 /// prototype of intrinsics.
1077 void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...) {
1079 void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID,
1080 Function *F,
1081 unsigned Count, ...) {
10781082 va_list VA;
1079 va_start(VA, F);
1083 va_start(VA, Count);
10801084
10811085 const FunctionType *FTy = F->getFunctionType();
10821086
10851089 // suffix, to be checked at the end.
10861090 std::string Suffix;
10871091
1092 if (FTy->getNumParams() + FTy->isVarArg() != Count - 1) {
1093 CheckFailed("Intrinsic prototype has incorrect number of arguments!", F);
1094 return;
1095 }
1096
10881097 // Note that "arg#0" is the return type.
1089 for (unsigned ArgNo = 0; 1; ++ArgNo) {
1090 int TypeID = va_arg(VA, int);
1091
1092 if (TypeID == -2) {
1098 for (unsigned ArgNo = 0; ArgNo < Count; ++ArgNo) {
1099 MVT::ValueType VT = va_arg(VA, MVT::ValueType);
1100
1101 if (VT == MVT::isVoid && ArgNo > 0) {
1102 if (!FTy->isVarArg())
1103 CheckFailed("Intrinsic prototype has no '...'!", F);
10931104 break;
10941105 }
10951106
1096 if (TypeID == -1) {
1097 if (ArgNo != FTy->getNumParams()+1)
1098 CheckFailed("Intrinsic prototype has too many arguments!", F);
1099 break;
1100 }
1101
1102 if (ArgNo == FTy->getNumParams()+1) {
1103 CheckFailed("Intrinsic prototype has too few arguments!", F);
1104 break;
1105 }
1106
11071107 const Type *Ty;
11081108 if (ArgNo == 0)
11091109 Ty = FTy->getReturnType();
11101110 else
11111111 Ty = FTy->getParamType(ArgNo-1);
1112
1113 unsigned NumElts = 0;
1114 const Type *EltTy = Ty;
1115 if (const VectorType *VTy = dyn_cast(Ty)) {
1116 EltTy = VTy->getElementType();
1117 NumElts = VTy->getNumElements();
1118 }
11121119
1113 if (TypeID != Ty->getTypeID()) {
1114 if (ArgNo == 0)
1115 CheckFailed("Intrinsic prototype has incorrect result type!", F);
1116 else
1117 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F);
1118 break;
1119 }
1120
1121 if (TypeID == Type::IntegerTyID) {
1122 unsigned ExpectedBits = (unsigned) va_arg(VA, int);
1123 unsigned GotBits = cast(Ty)->getBitWidth();
1124 if (ExpectedBits == 0) {
1125 Suffix += ".i" + utostr(GotBits);
1126 } else if (GotBits != ExpectedBits) {
1127 std::string bitmsg = " Expected " + utostr(ExpectedBits) + " but got "+
1128 utostr(GotBits) + " bits.";
1129 if (ArgNo == 0)
1130 CheckFailed("Intrinsic prototype has incorrect integer result width!"
1131 + bitmsg, F);
1132 else
1133 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " has "
1134 "incorrect integer width!" + bitmsg, F);
1135 break;
1120 if ((int)VT < 0) {
1121 int Match = ~VT;
1122 if (Match == 0) {
1123 if (Ty != FTy->getReturnType()) {
1124 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not "
1125 "match return type.", F);
1126 break;
1127 }
1128 } else {
1129 if (Ty != FTy->getParamType(Match-1)) {
1130 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not "
1131 "match parameter %" + utostr(Match-1) + ".", F);
1132 break;
1133 }
11361134 }
1135 } else if (VT == MVT::iAny) {
1136 unsigned GotBits = cast(EltTy)->getBitWidth();
1137 Suffix += ".";
1138 if (EltTy != Ty)
1139 Suffix += "v" + utostr(NumElts);
1140 Suffix += "i" + utostr(GotBits);;
11371141 // Check some constraints on various intrinsics.
11381142 switch (ID) {
11391143 default: break; // Not everything needs to be checked.
11401144 case Intrinsic::bswap:
11411145 if (GotBits < 16 || GotBits % 16 != 0)
11421146 CheckFailed("Intrinsic requires even byte width argument", F);
1143 /* FALL THROUGH */
1144 case Intrinsic::part_set:
1145 case Intrinsic::part_select:
1146 if (ArgNo == 1) {
1147 unsigned ResultBits =
1148 cast(FTy->getReturnType())->getBitWidth();
1149 if (GotBits != ResultBits)
1150 CheckFailed("Intrinsic requires the bit widths of the first "
1151 "parameter and the result to match", F);
1152 }
11531147 break;
11541148 }
1155 } else if (TypeID == Type::VectorTyID) {
1149 } else if (VT == MVT::iPTR) {
1150 if (!isa(Ty)) {
1151 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not a "
1152 "pointer and a pointer is required.", F);
1153 break;
1154 }
1155 } else if (MVT::isVector(VT)) {
11561156 // If this is a vector argument, verify the number and type of elements.
1157 const VectorType *PTy = cast(Ty);
1158 int ElemTy = va_arg(VA, int);
1159 if (ElemTy != PTy->getElementType()->getTypeID()) {
1157 if (MVT::getVectorElementType(VT) != MVT::getValueType(EltTy)) {
11601158 CheckFailed("Intrinsic prototype has incorrect vector element type!",
11611159 F);
11621160 break;
11631161 }
1164 if (ElemTy == Type::IntegerTyID) {
1165 unsigned NumBits = (unsigned)va_arg(VA, int);
1166 unsigned ExpectedBits =
1167 cast(PTy->getElementType())->getBitWidth();
1168 if (NumBits != ExpectedBits) {
1169 CheckFailed("Intrinsic prototype has incorrect vector element type!",
1170 F);
1171 break;
1172 }
1173 }
1174 if ((unsigned)va_arg(VA, int) != PTy->getNumElements()) {
1162 if (MVT::getVectorNumElements(VT) != NumElts) {
11751163 CheckFailed("Intrinsic prototype has incorrect number of "
11761164 "vector elements!",F);
1177 break;
1165 break;
11781166 }
1167 } else if (MVT::getTypeForValueType(VT) != EltTy) {
1168 if (ArgNo == 0)
1169 CheckFailed("Intrinsic prototype has incorrect result type!", F);
1170 else
1171 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F);
1172 break;
1173 } else if (EltTy != Ty) {
1174 if (ArgNo == 0)
1175 CheckFailed("Intrinsic result type is vector "
1176 "and a scalar is required.", F);
1177 else
1178 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is vector "
1179 "and a scalar is required.", F);
11791180 }
11801181 }
11811182
0 ; Tests to make sure intrinsics are automatically upgraded.
1 ; RUN: llvm-as < %s | llvm-dis | not grep {i32 @llvm\\.ct}
2 ; RUN: llvm-as < %s | llvm-dis | \
3 ; RUN: not grep {llvm\\.part\\.set\\.i\[0-9\]*\\.i\[0-9\]*\\.i\[0-9\]*}
4 ; RUN: llvm-as < %s | llvm-dis | \
5 ; RUN: not grep {llvm\\.part\\.select\\.i\[0-9\]*\\.i\[0-9\]*}
6 ; RUN: llvm-as < %s | llvm-dis | \
7 ; RUN: not grep {llvm\\.bswap\\.i\[0-9\]*\\.i\[0-9\]*}
8
9 declare i32 @llvm.ctpop.i28(i28 %val)
10 declare i32 @llvm.cttz.i29(i29 %val)
11 declare i32 @llvm.ctlz.i30(i30 %val)
12
13 define i32 @test_ct(i32 %A) {
14 %c1 = call i32 @llvm.ctpop.i28(i28 1234)
15 %c2 = call i32 @llvm.cttz.i29(i29 2345)
16 %c3 = call i32 @llvm.ctlz.i30(i30 3456)
17 %r1 = add i32 %c1, %c2
18 %r2 = add i32 %r1, %c3
19 ret i32 %r2
20 }
21
22 declare i32 @llvm.part.set.i32.i32.i32(i32 %x, i32 %rep, i32 %hi, i32 %lo)
23 declare i16 @llvm.part.set.i16.i16.i16(i16 %x, i16 %rep, i32 %hi, i32 %lo)
24 define i32 @test_part_set(i32 %A, i16 %B) {
25 %a = call i32 @llvm.part.set.i32.i32.i32(i32 %A, i32 27, i32 8, i32 0)
26 %b = call i16 @llvm.part.set.i16.i16.i16(i16 %B, i16 27, i32 8, i32 0)
27 %c = zext i16 %b to i32
28 %d = add i32 %a, %c
29 ret i32 %d
30 }
31
32 declare i32 @llvm.part.select.i32.i32(i32 %x, i32 %hi, i32 %lo)
33 declare i16 @llvm.part.select.i16.i16(i16 %x, i32 %hi, i32 %lo)
34 define i32 @test_part_select(i32 %A, i16 %B) {
35 %a = call i32 @llvm.part.select.i32.i32(i32 %A, i32 8, i32 0)
36 %b = call i16 @llvm.part.select.i16.i16(i16 %B, i32 8, i32 0)
37 %c = zext i16 %b to i32
38 %d = add i32 %a, %c
39 ret i32 %d
40 }
41
42 declare i32 @llvm.bswap.i32.i32(i32 %x)
43 declare i16 @llvm.bswap.i16.i16(i16 %x)
44 define i32 @test_bswap(i32 %A, i16 %B) {
45 %a = call i32 @llvm.bswap.i32.i32(i32 %A)
46 %b = call i16 @llvm.bswap.i16.i16(i16 %B)
47 %c = zext i16 %b to i32
48 %d = add i32 %a, %c
49 ret i32 %d
50 }
51
0 ; This isn't really an assembly file. It just runs test on bitcode to ensure
1 ; it is auto-upgraded.
2 ; RUN: llvm-dis < %s.bc | not grep {i32 @llvm\\.ct}
3 ; RUN: llvm-dis < %s.bc | \
4 ; RUN: not grep {llvm\\.part\\.set\\.i\[0-9\]*\\.i\[0-9\]*\\.i\[0-9\]*}
5 ; RUN: llvm-dis < %s.bc | \
6 ; RUN: not grep {llvm\\.part\\.select\\.i\[0-9\]*\\.i\[0-9\]*}
7 ; RUN: llvm-dis < %s.bc | \
8 ; RUN: not grep {llvm\\.bswap\\.i\[0-9\]*\\.i\[0-9\]*}
9
44 ; RUN: llvm-as < %s | llc -march=alpha -mcpu=ev56 | not grep -i ctlz
55 ; RUN: llvm-as < %s | llc -march=alpha -mattr=-CIX | not grep -i ctlz
66
7 declare i32 @llvm.ctlz.i8(i8)
7 declare i8 @llvm.ctlz.i8(i8)
88
99 define i32 @bar(i8 %x) {
1010 entry:
11 %tmp.1 = call i32 @llvm.ctlz.i8( i8 %x )
12 ret i32 %tmp.1
11 %tmp.1 = call i8 @llvm.ctlz.i8( i8 %x )
12 %tmp.2 = sext i8 %tmp.1 to i32
13 ret i32 %tmp.2
1314 }
22 ; RUN: llvm-as < %s > %t.bc
33 ; RUN: lli --force-interpreter=true %t.bc
44
5 declare i32 @llvm.part.set.i32.i32.i32(i32 %x, i32 %rep, i32 %hi, i32 %lo)
6 declare i16 @llvm.part.set.i16.i16.i16(i16 %x, i16 %rep, i32 %hi, i32 %lo)
5 declare i32 @llvm.part.set.i32.i32(i32 %x, i32 %rep, i32 %hi, i32 %lo)
6 declare i16 @llvm.part.set.i16.i16(i16 %x, i16 %rep, i32 %hi, i32 %lo)
77 define i32 @test_part_set(i32 %A, i16 %B) {
8 %a = call i32 @llvm.part.set.i32.i32.i32(i32 %A, i32 27, i32 8, i32 0)
9 %b = call i16 @llvm.part.set.i16.i16.i16(i16 %B, i16 27, i32 8, i32 0)
8 %a = call i32 @llvm.part.set.i32.i32(i32 %A, i32 27, i32 8, i32 0)
9 %b = call i16 @llvm.part.set.i16.i16(i16 %B, i16 27, i32 8, i32 0)
1010 %c = zext i16 %b to i32
1111 %d = add i32 %a, %c
1212 ret i32 %d
1313 }
1414
15 declare i32 @llvm.part.select.i32.i32(i32 %x, i32 %hi, i32 %lo)
16 declare i16 @llvm.part.select.i16.i16(i16 %x, i32 %hi, i32 %lo)
15 declare i32 @llvm.part.select.i32(i32 %x, i32 %hi, i32 %lo)
16 declare i16 @llvm.part.select.i16(i16 %x, i32 %hi, i32 %lo)
1717 define i32 @test_part_select(i32 %A, i16 %B) {
18 %a = call i32 @llvm.part.select.i32.i32(i32 %A, i32 8, i32 0)
19 %b = call i16 @llvm.part.select.i16.i16(i16 %B, i32 8, i32 0)
18 %a = call i32 @llvm.part.select.i32(i32 %A, i32 8, i32 0)
19 %b = call i16 @llvm.part.select.i16(i16 %B, i32 8, i32 0)
2020 %c = zext i16 %b to i32
2121 %d = add i32 %a, %c
2222 ret i32 %d
11
22 define i32 @_ZNK4llvm5APInt17countLeadingZerosEv(i64 *%t) {
33 %tmp19 = load i64* %t
4 %tmp23 = tail call i32 @llvm.ctlz.i64( i64 %tmp19 ) ; [#uses=1]
4 %tmp22 = tail call i64 @llvm.ctlz.i64( i64 %tmp19 ) ; [#uses=1]
5 %tmp23 = trunc i64 %tmp22 to i32
56 %tmp89 = add i32 %tmp23, -64 ; [#uses=1]
67 %tmp90 = add i32 %tmp89, 0 ; [#uses=1]
78 ret i32 %tmp90
89 }
910
10 declare i32 @llvm.ctlz.i64(i64)
11 declare i64 @llvm.ctlz.i64(i64)
402402
403403 declare void @llvm.prefetch(i8*, i32, i32)
404404
405 declare i32 @upgrd.rm.llvm.ctpop.i8(i8)
406
407 declare i32 @upgrd.rm.llvm.ctpop.i16(i16)
408
409 declare i32 @upgrd.rm.llvm.ctpop.i32(i32)
410
411 declare i32 @upgrd.rm.llvm.ctpop.i64(i64)
412
413 declare i32 @upgrd.rm.llvm.cttz.i8(i8)
414
415 declare i32 @upgrd.rm.llvm.cttz.i16(i16)
416
417 declare i32 @upgrd.rm.llvm.cttz.i32(i32)
418
419 declare i32 @upgrd.rm.llvm.cttz.i64(i64)
420
421 declare i32 @upgrd.rm.llvm.ctlz.i8(i8)
422
423 declare i32 @upgrd.rm.llvm.ctlz.i16(i16)
424
425 declare i32 @upgrd.rm.llvm.ctlz.i32(i32)
426
427 declare i32 @upgrd.rm.llvm.ctlz.i64(i64)
428
429405 declare float @llvm.sqrt.f32(float)
430406
431407 declare double @llvm.sqrt.f64(double)
436412 call void @llvm.prefetch( i8* null, i32 1, i32 3 )
437413 call float @llvm.sqrt.f32( float 5.000000e+00 ) ; :3 [#uses=0]
438414 call double @llvm.sqrt.f64( double 6.000000e+00 ) ; :4 [#uses=0]
439 call i32 @llvm.ctpop.i8( i8 10 ) ; :5 [#uses=1]
440 bitcast i32 %5 to i32 ; :6 [#uses=0]
441 call i32 @llvm.ctpop.i16( i16 11 ) ; :7 [#uses=1]
442 bitcast i32 %7 to i32 ; :8 [#uses=0]
415 call i8 @llvm.ctpop.i8( i8 10 ) ; :5 [#uses=1]
416 call i16 @llvm.ctpop.i16( i16 11 ) ; :7 [#uses=1]
443417 call i32 @llvm.ctpop.i32( i32 12 ) ; :9 [#uses=1]
444 bitcast i32 %9 to i32 ; :10 [#uses=0]
445 call i32 @llvm.ctpop.i64( i64 13 ) ; :11 [#uses=1]
446 bitcast i32 %11 to i32 ; :12 [#uses=0]
447 call i32 @llvm.ctlz.i8( i8 14 ) ; :13 [#uses=1]
448 bitcast i32 %13 to i32 ; :14 [#uses=0]
449 call i32 @llvm.ctlz.i16( i16 15 ) ; :15 [#uses=1]
450 bitcast i32 %15 to i32 ; :16 [#uses=0]
418 call i64 @llvm.ctpop.i64( i64 13 ) ; :11 [#uses=1]
419 call i8 @llvm.ctlz.i8( i8 14 ) ; :13 [#uses=1]
420 call i16 @llvm.ctlz.i16( i16 15 ) ; :15 [#uses=1]
451421 call i32 @llvm.ctlz.i32( i32 16 ) ; :17 [#uses=1]
452 bitcast i32 %17 to i32 ; :18 [#uses=0]
453 call i32 @llvm.ctlz.i64( i64 17 ) ; :19 [#uses=1]
454 bitcast i32 %19 to i32 ; :20 [#uses=0]
455 call i32 @llvm.cttz.i8( i8 18 ) ; :21 [#uses=1]
456 bitcast i32 %21 to i32 ; :22 [#uses=0]
457 call i32 @llvm.cttz.i16( i16 19 ) ; :23 [#uses=1]
458 bitcast i32 %23 to i32 ; :24 [#uses=0]
422 call i64 @llvm.ctlz.i64( i64 17 ) ; :19 [#uses=1]
423 call i8 @llvm.cttz.i8( i8 18 ) ; :21 [#uses=1]
424 call i16 @llvm.cttz.i16( i16 19 ) ; :23 [#uses=1]
459425 call i32 @llvm.cttz.i32( i32 20 ) ; :25 [#uses=1]
460 bitcast i32 %25 to i32 ; :26 [#uses=0]
461 call i32 @llvm.cttz.i64( i64 21 ) ; :27 [#uses=1]
462 bitcast i32 %27 to i32 ; :28 [#uses=0]
463 ret void
464 }
465
466 declare i32 @llvm.ctpop.i8(i8)
467
468 declare i32 @llvm.ctpop.i16(i16)
426 call i64 @llvm.cttz.i64( i64 21 ) ; :27 [#uses=1]
427 ret void
428 }
429
430 declare i8 @llvm.ctpop.i8(i8)
431
432 declare i16 @llvm.ctpop.i16(i16)
469433
470434 declare i32 @llvm.ctpop.i32(i32)
471435
472 declare i32 @llvm.ctpop.i64(i64)
473
474 declare i32 @llvm.ctlz.i8(i8)
475
476 declare i32 @llvm.ctlz.i16(i16)
436 declare i64 @llvm.ctpop.i64(i64)
437
438 declare i8 @llvm.ctlz.i8(i8)
439
440 declare i16 @llvm.ctlz.i16(i16)
477441
478442 declare i32 @llvm.ctlz.i32(i32)
479443
480 declare i32 @llvm.ctlz.i64(i64)
481
482 declare i32 @llvm.cttz.i8(i8)
483
484 declare i32 @llvm.cttz.i16(i16)
444 declare i64 @llvm.ctlz.i64(i64)
445
446 declare i8 @llvm.cttz.i8(i8)
447
448 declare i16 @llvm.cttz.i16(i16)
485449
486450 declare i32 @llvm.cttz.i32(i32)
487451
488 declare i32 @llvm.cttz.i64(i64)
452 declare i64 @llvm.cttz.i64(i64)
489453
490454 ; ModuleID = 'packed.ll'
491455 @foo1 = external global <4 x float> ; <<4 x float>*> [#uses=2]
22 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | \
33 ; RUN: grep -v declare | not grep llvm.ct
44
5 declare i32 @llvm.ctpop.i31(i31 %val)
5 declare i31 @llvm.ctpop.i31(i31 %val)
66 declare i32 @llvm.cttz.i32(i32 %val)
7 declare i32 @llvm.ctlz.i33(i33 %val)
7 declare i33 @llvm.ctlz.i33(i33 %val)
88
99 define i32 @test(i32 %A) {
10 %c1 = call i32 @llvm.ctpop.i31(i31 12415124)
10 %c1 = call i31 @llvm.ctpop.i31(i31 12415124)
1111 %c2 = call i32 @llvm.cttz.i32(i32 87359874)
12 %c3 = call i32 @llvm.ctlz.i33(i33 87359874)
13 %r1 = add i32 %c1, %c2
14 %r2 = add i32 %r1, %c3
12 %c3 = call i33 @llvm.ctlz.i33(i33 87359874)
13 %t1 = zext i31 %c1 to i32
14 %t3 = trunc i33 %c3 to i32
15 %r1 = add i32 %t1, %c2
16 %r2 = add i32 %r1, %t3
1517 ret i32 %r2
1618 }
14691469 if (Args.size() != 2)
14701470 error("Invalid prototype for " + Name);
14711471 return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]);
1472 }
1473 break;
1474 case 'b':
1475 if (Name.length() == 14 && !memcmp(&Name[5], "bswap.i", 7)) {
1476 const Type* ArgTy = Args[0]->getType();
1477 Name += ".i" + utostr(cast(ArgTy)->getBitWidth());
1478 Function *F = cast(
1479 CurModule.CurrentModule->getOrInsertFunction(Name, RetTy, ArgTy,
1480 (void*)0));
1481 return new CallInst(F, Args[0]);
1482 }
1483 break;
1484 case 'c':
1485 if ((Name.length() <= 14 && !memcmp(&Name[5], "ctpop.i", 7)) ||
1486 (Name.length() <= 13 && !memcmp(&Name[5], "ctlz.i", 6)) ||
1487 (Name.length() <= 13 && !memcmp(&Name[5], "cttz.i", 6))) {
1488 // These intrinsics changed their result type.
1489 const Type* ArgTy = Args[0]->getType();
1490 Function *OldF = CurModule.CurrentModule->getFunction(Name);
1491 if (OldF)
1492 OldF->setName("upgrd.rm." + Name);
1493
1494 Function *NewF = cast(
1495 CurModule.CurrentModule->getOrInsertFunction(Name, Type::Int32Ty,
1496 ArgTy, (void*)0));
1497
1498 Instruction *Call = new CallInst(NewF, Args[0], "", CurBB);
1499 return CastInst::createIntegerCast(Call, RetTy, false);
15001472 }
15011473 break;
15021474
2929 std::string GCCBuiltinName;// Name of the corresponding GCC builtin, or "".
3030 std::string TargetPrefix; // Target prefix, e.g. "ppc" for t-s intrinsics.
3131
32 /// ArgTypes - The type primitive enum value for the return value and all
33 /// of the arguments. These are things like Type::IntegerTyID.
34 std::vector ArgTypes;
35
3632 /// ArgVTs - The MVT::ValueType for each argument type. Note that this list
3733 /// is only populated when in the context of a target .td file. When
3834 /// building Intrinsics.td, this isn't available, because we don't know the
9696 case MVT::v2f64: return "MVT::v2f64";
9797 case MVT::v3i32: return "MVT::v3i32";
9898 case MVT::v3f32: return "MVT::v3f32";
99 case MVT::iPTR: return "TLI.getPointerTy()";
99 case MVT::iPTR: return "MVT::iPTR";
100100 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
101101 }
102102 }
650650 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
651651 Record *TyEl = TypeList->getElementAsRecord(i);
652652 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
653 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
654653 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
655654 isOverloaded |= VT == MVT::iAny;
656655 ArgVTs.push_back(VT);
657656 ArgTypeDefs.push_back(TyEl);
658657 }
659 if (ArgTypes.size() == 0)
658 if (ArgVTs.size() == 0)
660659 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
661660
662661
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "CodeGenTarget.h"
1314 #include "IntrinsicEmitter.h"
1415 #include "Record.h"
1516 #include "llvm/ADT/StringExtras.h"
9394
9495 // For overloaded intrinsics, only the prefix needs to match
9596 if (Ints[I->second].isOverloaded)
96 OS << " if (Len >= " << I->first.size()
97 << " && !memcmp(Name, \"" << I->first << "\", " << I->first.size()
98 << ")) return Intrinsic::" << Ints[I->second].EnumName << ";\n";
97 OS << " if (Len > " << I->first.size()
98 << " && !memcmp(Name, \"" << I->first << ".\", "
99 << (I->first.size() + 1) << ")) return Intrinsic::"
100 << Ints[I->second].EnumName << ";\n";
99101 else
100102 OS << " if (Len == " << I->first.size()
101 << " && !memcmp(Name, \"" << I->first << "\", Len)) return Intrinsic::"
103 << " && !memcmp(Name, \"" << I->first << "\", "
104 << I->first.size() << ")) return Intrinsic::"
102105 << Ints[I->second].EnumName << ";\n";
103106 }
104107 OS << " }\n";
116119 OS << "#endif\n\n";
117120 }
118121
119 static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
120 if (ArgType->getValueAsString("TypeVal") == "...") return true;
121
122 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
123 // If this is an integer type, check the width is correct.
124 if (ArgType->isSubClassOf("LLVMIntegerType"))
125 OS << ArgType->getValueAsInt("Width") << ", ";
126
127 // If this is a vector type, check that the subtype and size are correct.
128 else if (ArgType->isSubClassOf("LLVMVectorType")) {
129 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
130 OS << ArgType->getValueAsInt("NumElts") << ", ";
131 }
132
133 return false;
122 static void EmitTypeForValueType(std::ostream &OS, MVT::ValueType VT) {
123 if (MVT::isInteger(VT)) {
124 unsigned BitWidth = MVT::getSizeInBits(VT);
125 OS << "IntegerType::get(" << BitWidth << ")";
126 } else if (VT == MVT::Other) {
127 // MVT::OtherVT is used to mean the empty struct type here.
128 OS << "StructType::get(std::vector())";
129 } else if (VT == MVT::f32) {
130 OS << "Type::FloatTy";
131 } else if (VT == MVT::f64) {
132 OS << "Type::DoubleTy";
133 } else if (VT == MVT::isVoid) {
134 OS << "Type::VoidTy";
135 } else {
136 assert(false && "Unsupported ValueType!");
137 }
134138 }
135139
136140 static void EmitTypeGenerate(std::ostream &OS, Record *ArgType,
137141 unsigned &ArgNo) {
138 if (ArgType->isSubClassOf("LLVMIntegerType")) {
139 unsigned BitWidth = ArgType->getValueAsInt("Width");
142 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
143
144 if (ArgType->isSubClassOf("LLVMMatchType")) {
145 unsigned Number = ArgType->getValueAsInt("Number");
146 assert(Number < ArgNo && "Invalid matching number!");
147 OS << "Tys[" << Number << "]";
148 } else if (VT == MVT::iAny) {
140149 // NOTE: The ArgNo variable here is not the absolute argument number, it is
141150 // the index of the "arbitrary" type in the Tys array passed to the
142151 // Intrinsic::getDeclaration function. Consequently, we only want to
143 // increment it when we actually hit an arbitrary integer type which is
144 // identified by BitWidth == 0. Getting this wrong leads to very subtle
145 // bugs!
146 if (BitWidth == 0)
147 OS << "Tys[" << ArgNo++ << "]";
148 else
149 OS << "IntegerType::get(" << BitWidth << ")";
150 } else if (ArgType->isSubClassOf("LLVMVectorType")) {
152 // increment it when we actually hit an overloaded type. Getting this wrong
153 // leads to very subtle bugs!
154 OS << "Tys[" << ArgNo++ << "]";
155 } else if (MVT::isVector(VT)) {
151156 OS << "VectorType::get(";
152 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
153 OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
154 } else if (ArgType->isSubClassOf("LLVMPointerType")) {
157 EmitTypeForValueType(OS, MVT::getVectorElementType(VT));
158 OS << ", " << MVT::getVectorNumElements(VT) << ")";
159 } else if (VT == MVT::iPTR) {
155160 OS << "PointerType::get(";
156161 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
157162 OS << ")";
158 } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) {
159 OS << "StructType::get(std::vector())";
163 } else if (VT == MVT::isVoid) {
164 if (ArgNo == 0)
165 OS << "Type::VoidTy";
166 else
167 // MVT::isVoid is used to mean varargs here.
168 OS << "...";
160169 } else {
161 OS << "Type::getPrimitiveType(";
162 OS << ArgType->getValueAsString("TypeVal") << ")";
170 EmitTypeForValueType(OS, VT);
163171 }
164172 }
165173
208216 }
209217
210218 const std::vector &ArgTypes = I->first;
211 OS << " VerifyIntrinsicPrototype(ID, IF, ";
212 bool VarArg = false;
219 OS << " VerifyIntrinsicPrototype(ID, IF, " << ArgTypes.size() << ", ";
213220 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
214 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
215 if (VarArg) {
216 if ((j+1) != ArgTypes.size())
221 Record *ArgType = ArgTypes[j];
222 if (ArgType->isSubClassOf("LLVMMatchType")) {
223 unsigned Number = ArgType->getValueAsInt("Number");
224 assert(Number < j && "Invalid matching number!");
225 OS << "~" << Number;
226 } else {
227 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
228 OS << getEnumName(VT);
229 if (VT == MVT::isVoid && j != 0 && j != ArgTypes.size()-1)
217230 throw "Var arg type not last argument";
218 break;
219231 }
232 if (j != ArgTypes.size()-1)
233 OS << ", ";
220234 }
221235
222 OS << (VarArg ? "-2);\n" : "-1);\n");
236 OS << ");\n";
223237 OS << " break;\n";
224238 }
225239 OS << " }\n";
254268 const std::vector &ArgTypes = I->first;
255269 unsigned N = ArgTypes.size();
256270
257 if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") {
271 if (N > 1 &&
272 getValueType(ArgTypes[N-1]->getValueAsDef("VT")) == MVT::isVoid) {
258273 OS << " IsVarArg = true;\n";
259274 --N;
260275 }
208208 << " static const MVT::ValueType " << Name
209209 << "[] = {\n ";
210210 for (unsigned i = 0, e = RC.VTs.size(); i != e; ++i)
211 OS << getName(RC.VTs[i]) << ", ";
211 OS << getEnumName(RC.VTs[i]) << ", ";
212212 OS << "MVT::Other\n };\n\n";
213213 }
214214 OS << "} // end anonymous namespace\n\n";