InstructionsTest.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/AsmParser/Parser.h"
  9. #include "llvm/IR/Instructions.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/Analysis/ValueTracking.h"
  12. #include "llvm/IR/BasicBlock.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/IR/DataLayout.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/MDBuilder.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/NoFolder.h"
  22. #include "llvm/IR/Operator.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include "gmock/gmock-matchers.h"
  25. #include "gtest/gtest.h"
  26. #include <memory>
  27. namespace llvm {
  28. namespace {
  29. static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
  30. SMDiagnostic Err;
  31. std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
  32. if (!Mod)
  33. Err.print("InstructionsTests", errs());
  34. return Mod;
  35. }
  36. TEST(InstructionsTest, ReturnInst) {
  37. LLVMContext C;
  38. // test for PR6589
  39. const ReturnInst* r0 = ReturnInst::Create(C);
  40. EXPECT_EQ(r0->getNumOperands(), 0U);
  41. EXPECT_EQ(r0->op_begin(), r0->op_end());
  42. IntegerType* Int1 = IntegerType::get(C, 1);
  43. Constant* One = ConstantInt::get(Int1, 1, true);
  44. const ReturnInst* r1 = ReturnInst::Create(C, One);
  45. EXPECT_EQ(1U, r1->getNumOperands());
  46. User::const_op_iterator b(r1->op_begin());
  47. EXPECT_NE(r1->op_end(), b);
  48. EXPECT_EQ(One, *b);
  49. EXPECT_EQ(One, r1->getOperand(0));
  50. ++b;
  51. EXPECT_EQ(r1->op_end(), b);
  52. // clean up
  53. delete r0;
  54. delete r1;
  55. }
  56. // Test fixture that provides a module and a single function within it. Useful
  57. // for tests that need to refer to the function in some way.
  58. class ModuleWithFunctionTest : public testing::Test {
  59. protected:
  60. ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
  61. FArgTypes.push_back(Type::getInt8Ty(Ctx));
  62. FArgTypes.push_back(Type::getInt32Ty(Ctx));
  63. FArgTypes.push_back(Type::getInt64Ty(Ctx));
  64. FunctionType *FTy =
  65. FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
  66. F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
  67. }
  68. LLVMContext Ctx;
  69. std::unique_ptr<Module> M;
  70. SmallVector<Type *, 3> FArgTypes;
  71. Function *F;
  72. };
  73. TEST_F(ModuleWithFunctionTest, CallInst) {
  74. Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
  75. ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
  76. ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
  77. std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
  78. // Make sure iteration over a call's arguments works as expected.
  79. unsigned Idx = 0;
  80. for (Value *Arg : Call->arg_operands()) {
  81. EXPECT_EQ(FArgTypes[Idx], Arg->getType());
  82. EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
  83. Idx++;
  84. }
  85. }
  86. TEST_F(ModuleWithFunctionTest, InvokeInst) {
  87. BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
  88. BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
  89. Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
  90. ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
  91. ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
  92. std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
  93. // Make sure iteration over invoke's arguments works as expected.
  94. unsigned Idx = 0;
  95. for (Value *Arg : Invoke->arg_operands()) {
  96. EXPECT_EQ(FArgTypes[Idx], Arg->getType());
  97. EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
  98. Idx++;
  99. }
  100. }
  101. TEST(InstructionsTest, BranchInst) {
  102. LLVMContext C;
  103. // Make a BasicBlocks
  104. BasicBlock* bb0 = BasicBlock::Create(C);
  105. BasicBlock* bb1 = BasicBlock::Create(C);
  106. // Mandatory BranchInst
  107. const BranchInst* b0 = BranchInst::Create(bb0);
  108. EXPECT_TRUE(b0->isUnconditional());
  109. EXPECT_FALSE(b0->isConditional());
  110. EXPECT_EQ(1U, b0->getNumSuccessors());
  111. // check num operands
  112. EXPECT_EQ(1U, b0->getNumOperands());
  113. EXPECT_NE(b0->op_begin(), b0->op_end());
  114. EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
  115. EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
  116. IntegerType* Int1 = IntegerType::get(C, 1);
  117. Constant* One = ConstantInt::get(Int1, 1, true);
  118. // Conditional BranchInst
  119. BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
  120. EXPECT_FALSE(b1->isUnconditional());
  121. EXPECT_TRUE(b1->isConditional());
  122. EXPECT_EQ(2U, b1->getNumSuccessors());
  123. // check num operands
  124. EXPECT_EQ(3U, b1->getNumOperands());
  125. User::const_op_iterator b(b1->op_begin());
  126. // check COND
  127. EXPECT_NE(b, b1->op_end());
  128. EXPECT_EQ(One, *b);
  129. EXPECT_EQ(One, b1->getOperand(0));
  130. EXPECT_EQ(One, b1->getCondition());
  131. ++b;
  132. // check ELSE
  133. EXPECT_EQ(bb1, *b);
  134. EXPECT_EQ(bb1, b1->getOperand(1));
  135. EXPECT_EQ(bb1, b1->getSuccessor(1));
  136. ++b;
  137. // check THEN
  138. EXPECT_EQ(bb0, *b);
  139. EXPECT_EQ(bb0, b1->getOperand(2));
  140. EXPECT_EQ(bb0, b1->getSuccessor(0));
  141. ++b;
  142. EXPECT_EQ(b1->op_end(), b);
  143. // clean up
  144. delete b0;
  145. delete b1;
  146. delete bb0;
  147. delete bb1;
  148. }
  149. TEST(InstructionsTest, CastInst) {
  150. LLVMContext C;
  151. Type *Int8Ty = Type::getInt8Ty(C);
  152. Type *Int16Ty = Type::getInt16Ty(C);
  153. Type *Int32Ty = Type::getInt32Ty(C);
  154. Type *Int64Ty = Type::getInt64Ty(C);
  155. Type *V8x8Ty = VectorType::get(Int8Ty, 8);
  156. Type *V8x64Ty = VectorType::get(Int64Ty, 8);
  157. Type *X86MMXTy = Type::getX86_MMXTy(C);
  158. Type *HalfTy = Type::getHalfTy(C);
  159. Type *FloatTy = Type::getFloatTy(C);
  160. Type *DoubleTy = Type::getDoubleTy(C);
  161. Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
  162. Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
  163. Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
  164. Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
  165. Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
  166. Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
  167. Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
  168. Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
  169. Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
  170. Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
  171. Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
  172. Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
  173. Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
  174. Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
  175. const Constant* c8 = Constant::getNullValue(V8x8Ty);
  176. const Constant* c64 = Constant::getNullValue(V8x64Ty);
  177. const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
  178. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
  179. EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
  180. EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
  181. EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
  182. EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
  183. EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
  184. EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
  185. EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
  186. EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
  187. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
  188. EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
  189. EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
  190. // Check address space casts are rejected since we don't know the sizes here
  191. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
  192. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
  193. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
  194. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
  195. EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
  196. EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
  197. EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
  198. V2Int32PtrAS1Ty,
  199. true));
  200. // Test mismatched number of elements for pointers
  201. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
  202. EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
  203. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
  204. EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
  205. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
  206. EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
  207. EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
  208. EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
  209. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
  210. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
  211. EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
  212. EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
  213. EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
  214. EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
  215. EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
  216. EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
  217. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
  218. EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
  219. EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
  220. EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
  221. EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
  222. EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
  223. EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
  224. EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
  225. Constant::getNullValue(V4Int32PtrTy),
  226. V2Int32PtrTy));
  227. EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
  228. Constant::getNullValue(V2Int32PtrTy),
  229. V4Int32PtrTy));
  230. EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
  231. Constant::getNullValue(V4Int32PtrAS1Ty),
  232. V2Int32PtrTy));
  233. EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
  234. Constant::getNullValue(V2Int32PtrTy),
  235. V4Int32PtrAS1Ty));
  236. // Check that assertion is not hit when creating a cast with a vector of
  237. // pointers
  238. // First form
  239. BasicBlock *BB = BasicBlock::Create(C);
  240. Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
  241. auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
  242. // Second form
  243. auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
  244. delete Inst2;
  245. Inst1->eraseFromParent();
  246. delete BB;
  247. }
  248. TEST(InstructionsTest, VectorGep) {
  249. LLVMContext C;
  250. // Type Definitions
  251. Type *I8Ty = IntegerType::get(C, 8);
  252. Type *I32Ty = IntegerType::get(C, 32);
  253. PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
  254. PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
  255. VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
  256. VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
  257. // Test different aspects of the vector-of-pointers type
  258. // and GEPs which use this type.
  259. ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
  260. ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
  261. std::vector<Constant*> ConstVa(2, Ci32a);
  262. std::vector<Constant*> ConstVb(2, Ci32b);
  263. Constant *C2xi32a = ConstantVector::get(ConstVa);
  264. Constant *C2xi32b = ConstantVector::get(ConstVb);
  265. CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
  266. CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
  267. ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
  268. ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
  269. EXPECT_NE(ICmp0, ICmp1); // suppress warning.
  270. BasicBlock* BB0 = BasicBlock::Create(C);
  271. // Test InsertAtEnd ICmpInst constructor.
  272. ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
  273. EXPECT_NE(ICmp0, ICmp2); // suppress warning.
  274. GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
  275. GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
  276. GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
  277. GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
  278. CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
  279. CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
  280. CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
  281. CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
  282. Value *S0 = BTC0->stripPointerCasts();
  283. Value *S1 = BTC1->stripPointerCasts();
  284. Value *S2 = BTC2->stripPointerCasts();
  285. Value *S3 = BTC3->stripPointerCasts();
  286. EXPECT_NE(S0, Gep0);
  287. EXPECT_NE(S1, Gep1);
  288. EXPECT_NE(S2, Gep2);
  289. EXPECT_NE(S3, Gep3);
  290. int64_t Offset;
  291. DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
  292. "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
  293. ":128:128-n8:16:32:64-S128");
  294. // Make sure we don't crash
  295. GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
  296. GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
  297. GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
  298. GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
  299. // Gep of Geps
  300. GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
  301. GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
  302. GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
  303. GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
  304. EXPECT_EQ(GepII0->getNumIndices(), 1u);
  305. EXPECT_EQ(GepII1->getNumIndices(), 1u);
  306. EXPECT_EQ(GepII2->getNumIndices(), 1u);
  307. EXPECT_EQ(GepII3->getNumIndices(), 1u);
  308. EXPECT_FALSE(GepII0->hasAllZeroIndices());
  309. EXPECT_FALSE(GepII1->hasAllZeroIndices());
  310. EXPECT_FALSE(GepII2->hasAllZeroIndices());
  311. EXPECT_FALSE(GepII3->hasAllZeroIndices());
  312. delete GepII0;
  313. delete GepII1;
  314. delete GepII2;
  315. delete GepII3;
  316. delete BTC0;
  317. delete BTC1;
  318. delete BTC2;
  319. delete BTC3;
  320. delete Gep0;
  321. delete Gep1;
  322. delete Gep2;
  323. delete Gep3;
  324. ICmp2->eraseFromParent();
  325. delete BB0;
  326. delete ICmp0;
  327. delete ICmp1;
  328. delete PtrVecA;
  329. delete PtrVecB;
  330. }
  331. TEST(InstructionsTest, FPMathOperator) {
  332. LLVMContext Context;
  333. IRBuilder<> Builder(Context);
  334. MDBuilder MDHelper(Context);
  335. Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
  336. MDNode *MD1 = MDHelper.createFPMath(1.0);
  337. Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
  338. EXPECT_TRUE(isa<FPMathOperator>(V1));
  339. FPMathOperator *O1 = cast<FPMathOperator>(V1);
  340. EXPECT_EQ(O1->getFPAccuracy(), 1.0);
  341. V1->deleteValue();
  342. I->deleteValue();
  343. }
  344. TEST(InstructionsTest, isEliminableCastPair) {
  345. LLVMContext C;
  346. Type* Int16Ty = Type::getInt16Ty(C);
  347. Type* Int32Ty = Type::getInt32Ty(C);
  348. Type* Int64Ty = Type::getInt64Ty(C);
  349. Type* Int64PtrTy = Type::getInt64PtrTy(C);
  350. // Source and destination pointers have same size -> bitcast.
  351. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  352. CastInst::IntToPtr,
  353. Int64PtrTy, Int64Ty, Int64PtrTy,
  354. Int32Ty, nullptr, Int32Ty),
  355. CastInst::BitCast);
  356. // Source and destination have unknown sizes, but the same address space and
  357. // the intermediate int is the maximum pointer size -> bitcast
  358. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  359. CastInst::IntToPtr,
  360. Int64PtrTy, Int64Ty, Int64PtrTy,
  361. nullptr, nullptr, nullptr),
  362. CastInst::BitCast);
  363. // Source and destination have unknown sizes, but the same address space and
  364. // the intermediate int is not the maximum pointer size -> nothing
  365. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
  366. CastInst::IntToPtr,
  367. Int64PtrTy, Int32Ty, Int64PtrTy,
  368. nullptr, nullptr, nullptr),
  369. 0U);
  370. // Middle pointer big enough -> bitcast.
  371. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  372. CastInst::PtrToInt,
  373. Int64Ty, Int64PtrTy, Int64Ty,
  374. nullptr, Int64Ty, nullptr),
  375. CastInst::BitCast);
  376. // Middle pointer too small -> fail.
  377. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  378. CastInst::PtrToInt,
  379. Int64Ty, Int64PtrTy, Int64Ty,
  380. nullptr, Int32Ty, nullptr),
  381. 0U);
  382. // Test that we don't eliminate bitcasts between different address spaces,
  383. // or if we don't have available pointer size information.
  384. DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
  385. "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
  386. "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
  387. Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
  388. Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
  389. IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
  390. IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
  391. // Cannot simplify inttoptr, addrspacecast
  392. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  393. CastInst::AddrSpaceCast,
  394. Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
  395. nullptr, Int16SizePtr, Int64SizePtr),
  396. 0U);
  397. // Cannot simplify addrspacecast, ptrtoint
  398. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
  399. CastInst::PtrToInt,
  400. Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
  401. Int64SizePtr, Int16SizePtr, nullptr),
  402. 0U);
  403. // Pass since the bitcast address spaces are the same
  404. EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
  405. CastInst::BitCast,
  406. Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
  407. nullptr, nullptr, nullptr),
  408. CastInst::IntToPtr);
  409. }
  410. TEST(InstructionsTest, CloneCall) {
  411. LLVMContext C;
  412. Type *Int32Ty = Type::getInt32Ty(C);
  413. Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
  414. FunctionType *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
  415. Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
  416. Value *Args[] = {
  417. ConstantInt::get(Int32Ty, 1),
  418. ConstantInt::get(Int32Ty, 2),
  419. ConstantInt::get(Int32Ty, 3)
  420. };
  421. std::unique_ptr<CallInst> Call(
  422. CallInst::Create(FnTy, Callee, Args, "result"));
  423. // Test cloning the tail call kind.
  424. CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
  425. CallInst::TCK_MustTail};
  426. for (CallInst::TailCallKind TCK : Kinds) {
  427. Call->setTailCallKind(TCK);
  428. std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
  429. EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
  430. }
  431. Call->setTailCallKind(CallInst::TCK_None);
  432. // Test cloning an attribute.
  433. {
  434. AttrBuilder AB;
  435. AB.addAttribute(Attribute::ReadOnly);
  436. Call->setAttributes(
  437. AttributeList::get(C, AttributeList::FunctionIndex, AB));
  438. std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
  439. EXPECT_TRUE(Clone->onlyReadsMemory());
  440. }
  441. }
  442. TEST(InstructionsTest, AlterCallBundles) {
  443. LLVMContext C;
  444. Type *Int32Ty = Type::getInt32Ty(C);
  445. FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
  446. Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
  447. Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
  448. OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
  449. std::unique_ptr<CallInst> Call(
  450. CallInst::Create(FnTy, Callee, Args, OldBundle, "result"));
  451. Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
  452. AttrBuilder AB;
  453. AB.addAttribute(Attribute::Cold);
  454. Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
  455. Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
  456. OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
  457. std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
  458. EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
  459. EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
  460. EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
  461. EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
  462. EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
  463. EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
  464. EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
  465. EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
  466. }
  467. TEST(InstructionsTest, AlterInvokeBundles) {
  468. LLVMContext C;
  469. Type *Int32Ty = Type::getInt32Ty(C);
  470. FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
  471. Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
  472. Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
  473. std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
  474. std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
  475. OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
  476. std::unique_ptr<InvokeInst> Invoke(
  477. InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
  478. OldBundle, "result"));
  479. AttrBuilder AB;
  480. AB.addAttribute(Attribute::Cold);
  481. Invoke->setAttributes(
  482. AttributeList::get(C, AttributeList::FunctionIndex, AB));
  483. Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
  484. OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
  485. std::unique_ptr<InvokeInst> Clone(
  486. InvokeInst::Create(Invoke.get(), NewBundle));
  487. EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
  488. EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
  489. EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
  490. EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
  491. EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
  492. EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
  493. EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
  494. EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
  495. EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
  496. }
  497. TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
  498. auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
  499. auto *Arg0 = &*F->arg_begin();
  500. IRBuilder<NoFolder> B(Ctx);
  501. B.SetInsertPoint(OnlyBB);
  502. {
  503. auto *UI =
  504. cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
  505. ASSERT_TRUE(UI->isExact());
  506. UI->dropPoisonGeneratingFlags();
  507. ASSERT_FALSE(UI->isExact());
  508. }
  509. {
  510. auto *ShrI =
  511. cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
  512. ASSERT_TRUE(ShrI->isExact());
  513. ShrI->dropPoisonGeneratingFlags();
  514. ASSERT_FALSE(ShrI->isExact());
  515. }
  516. {
  517. auto *AI = cast<Instruction>(
  518. B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
  519. ASSERT_TRUE(AI->hasNoUnsignedWrap());
  520. AI->dropPoisonGeneratingFlags();
  521. ASSERT_FALSE(AI->hasNoUnsignedWrap());
  522. ASSERT_FALSE(AI->hasNoSignedWrap());
  523. }
  524. {
  525. auto *SI = cast<Instruction>(
  526. B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
  527. ASSERT_TRUE(SI->hasNoSignedWrap());
  528. SI->dropPoisonGeneratingFlags();
  529. ASSERT_FALSE(SI->hasNoUnsignedWrap());
  530. ASSERT_FALSE(SI->hasNoSignedWrap());
  531. }
  532. {
  533. auto *ShlI = cast<Instruction>(
  534. B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
  535. ASSERT_TRUE(ShlI->hasNoSignedWrap());
  536. ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
  537. ShlI->dropPoisonGeneratingFlags();
  538. ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
  539. ASSERT_FALSE(ShlI->hasNoSignedWrap());
  540. }
  541. {
  542. Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
  543. auto *GI = cast<GetElementPtrInst>(
  544. B.CreateInBoundsGEP(B.getInt8Ty(), GEPBase, Arg0));
  545. ASSERT_TRUE(GI->isInBounds());
  546. GI->dropPoisonGeneratingFlags();
  547. ASSERT_FALSE(GI->isInBounds());
  548. }
  549. }
  550. TEST(InstructionsTest, GEPIndices) {
  551. LLVMContext Context;
  552. IRBuilder<NoFolder> Builder(Context);
  553. Type *ElementTy = Builder.getInt8Ty();
  554. Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
  555. Value *Indices[] = {
  556. Builder.getInt32(0),
  557. Builder.getInt32(13),
  558. Builder.getInt32(42) };
  559. Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
  560. Indices);
  561. ASSERT_TRUE(isa<GetElementPtrInst>(V));
  562. auto *GEPI = cast<GetElementPtrInst>(V);
  563. ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
  564. ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
  565. EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
  566. EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
  567. EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
  568. EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
  569. EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
  570. const auto *CGEPI = GEPI;
  571. ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
  572. ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
  573. EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
  574. EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
  575. EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
  576. EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
  577. EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
  578. delete GEPI;
  579. }
  580. TEST(InstructionsTest, SwitchInst) {
  581. LLVMContext C;
  582. std::unique_ptr<BasicBlock> BB1, BB2, BB3;
  583. BB1.reset(BasicBlock::Create(C));
  584. BB2.reset(BasicBlock::Create(C));
  585. BB3.reset(BasicBlock::Create(C));
  586. // We create block 0 after the others so that it gets destroyed first and
  587. // clears the uses of the other basic blocks.
  588. std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
  589. auto *Int32Ty = Type::getInt32Ty(C);
  590. SwitchInst *SI =
  591. SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
  592. SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
  593. SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
  594. SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
  595. auto CI = SI->case_begin();
  596. ASSERT_NE(CI, SI->case_end());
  597. EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
  598. EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
  599. EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
  600. EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
  601. EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
  602. EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
  603. EXPECT_EQ(CI + 1, std::next(CI));
  604. EXPECT_EQ(CI + 2, std::next(CI, 2));
  605. EXPECT_EQ(CI + 3, std::next(CI, 3));
  606. EXPECT_EQ(SI->case_end(), CI + 3);
  607. EXPECT_EQ(0, CI - CI);
  608. EXPECT_EQ(1, (CI + 1) - CI);
  609. EXPECT_EQ(2, (CI + 2) - CI);
  610. EXPECT_EQ(3, SI->case_end() - CI);
  611. EXPECT_EQ(3, std::distance(CI, SI->case_end()));
  612. auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
  613. SwitchInst::ConstCaseIt CCE = SI->case_end();
  614. ASSERT_NE(CCI, SI->case_end());
  615. EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
  616. EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
  617. EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
  618. EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
  619. EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
  620. EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
  621. EXPECT_EQ(CCI + 1, std::next(CCI));
  622. EXPECT_EQ(CCI + 2, std::next(CCI, 2));
  623. EXPECT_EQ(CCI + 3, std::next(CCI, 3));
  624. EXPECT_EQ(CCE, CCI + 3);
  625. EXPECT_EQ(0, CCI - CCI);
  626. EXPECT_EQ(1, (CCI + 1) - CCI);
  627. EXPECT_EQ(2, (CCI + 2) - CCI);
  628. EXPECT_EQ(3, CCE - CCI);
  629. EXPECT_EQ(3, std::distance(CCI, CCE));
  630. // Make sure that the const iterator is compatible with a const auto ref.
  631. const auto &Handle = *CCI;
  632. EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
  633. EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
  634. }
  635. TEST(InstructionsTest, SwitchInstProfUpdateWrapper) {
  636. LLVMContext C;
  637. std::unique_ptr<BasicBlock> BB1, BB2, BB3;
  638. BB1.reset(BasicBlock::Create(C));
  639. BB2.reset(BasicBlock::Create(C));
  640. BB3.reset(BasicBlock::Create(C));
  641. // We create block 0 after the others so that it gets destroyed first and
  642. // clears the uses of the other basic blocks.
  643. std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
  644. auto *Int32Ty = Type::getInt32Ty(C);
  645. SwitchInst *SI =
  646. SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 4, BB0.get());
  647. SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
  648. SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
  649. SI->setMetadata(LLVMContext::MD_prof,
  650. MDBuilder(C).createBranchWeights({ 9, 1, 22 }));
  651. {
  652. SwitchInstProfUpdateWrapper SIW(*SI);
  653. EXPECT_EQ(*SIW.getSuccessorWeight(0), 9u);
  654. EXPECT_EQ(*SIW.getSuccessorWeight(1), 1u);
  655. EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
  656. SIW.setSuccessorWeight(0, 99u);
  657. SIW.setSuccessorWeight(1, 11u);
  658. EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
  659. EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
  660. EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
  661. }
  662. { // Create another wrapper and check that the data persist.
  663. SwitchInstProfUpdateWrapper SIW(*SI);
  664. EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
  665. EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
  666. EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
  667. }
  668. }
  669. TEST(InstructionsTest, CommuteShuffleMask) {
  670. SmallVector<int, 16> Indices({-1, 0, 7});
  671. ShuffleVectorInst::commuteShuffleMask(Indices, 4);
  672. EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
  673. }
  674. TEST(InstructionsTest, ShuffleMaskQueries) {
  675. // Create the elements for various constant vectors.
  676. LLVMContext Ctx;
  677. Type *Int32Ty = Type::getInt32Ty(Ctx);
  678. Constant *CU = UndefValue::get(Int32Ty);
  679. Constant *C0 = ConstantInt::get(Int32Ty, 0);
  680. Constant *C1 = ConstantInt::get(Int32Ty, 1);
  681. Constant *C2 = ConstantInt::get(Int32Ty, 2);
  682. Constant *C3 = ConstantInt::get(Int32Ty, 3);
  683. Constant *C4 = ConstantInt::get(Int32Ty, 4);
  684. Constant *C5 = ConstantInt::get(Int32Ty, 5);
  685. Constant *C6 = ConstantInt::get(Int32Ty, 6);
  686. Constant *C7 = ConstantInt::get(Int32Ty, 7);
  687. Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
  688. EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity));
  689. EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select
  690. EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity));
  691. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source
  692. EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity));
  693. EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity));
  694. Constant *Select = ConstantVector::get({CU, C1, C5});
  695. EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select));
  696. EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select));
  697. EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select));
  698. EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select));
  699. EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select));
  700. EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select));
  701. Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
  702. EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse));
  703. EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse));
  704. EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse));
  705. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source
  706. EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse));
  707. EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse));
  708. Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
  709. EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource));
  710. EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource));
  711. EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource));
  712. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource));
  713. EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource));
  714. EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource));
  715. Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
  716. EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat));
  717. EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat));
  718. EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat));
  719. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source
  720. EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat));
  721. EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat));
  722. Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
  723. EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose));
  724. EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose));
  725. EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose));
  726. EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose));
  727. EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose));
  728. EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose));
  729. // More tests to make sure the logic is/stays correct...
  730. EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3})));
  731. EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU})));
  732. EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU})));
  733. EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3})));
  734. EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4})));
  735. EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU})));
  736. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7})));
  737. EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3})));
  738. EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4})));
  739. EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0})));
  740. EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7})));
  741. EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
  742. // Nothing special about the values here - just re-using inputs to reduce code.
  743. Constant *V0 = ConstantVector::get({C0, C1, C2, C3});
  744. Constant *V1 = ConstantVector::get({C3, C2, C1, C0});
  745. // Identity with undef elts.
  746. ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1,
  747. ConstantVector::get({C0, C1, CU, CU}));
  748. EXPECT_TRUE(Id1->isIdentity());
  749. EXPECT_FALSE(Id1->isIdentityWithPadding());
  750. EXPECT_FALSE(Id1->isIdentityWithExtract());
  751. EXPECT_FALSE(Id1->isConcat());
  752. delete Id1;
  753. // Result has less elements than operands.
  754. ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1,
  755. ConstantVector::get({C0, C1, C2}));
  756. EXPECT_FALSE(Id2->isIdentity());
  757. EXPECT_FALSE(Id2->isIdentityWithPadding());
  758. EXPECT_TRUE(Id2->isIdentityWithExtract());
  759. EXPECT_FALSE(Id2->isConcat());
  760. delete Id2;
  761. // Result has less elements than operands; choose from Op1.
  762. ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1,
  763. ConstantVector::get({C4, CU, C6}));
  764. EXPECT_FALSE(Id3->isIdentity());
  765. EXPECT_FALSE(Id3->isIdentityWithPadding());
  766. EXPECT_TRUE(Id3->isIdentityWithExtract());
  767. EXPECT_FALSE(Id3->isConcat());
  768. delete Id3;
  769. // Result has less elements than operands; choose from Op0 and Op1 is not identity.
  770. ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1,
  771. ConstantVector::get({C4, C1, C6}));
  772. EXPECT_FALSE(Id4->isIdentity());
  773. EXPECT_FALSE(Id4->isIdentityWithPadding());
  774. EXPECT_FALSE(Id4->isIdentityWithExtract());
  775. EXPECT_FALSE(Id4->isConcat());
  776. delete Id4;
  777. // Result has more elements than operands, and extra elements are undef.
  778. ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1,
  779. ConstantVector::get({CU, C1, C2, C3, CU, CU}));
  780. EXPECT_FALSE(Id5->isIdentity());
  781. EXPECT_TRUE(Id5->isIdentityWithPadding());
  782. EXPECT_FALSE(Id5->isIdentityWithExtract());
  783. EXPECT_FALSE(Id5->isConcat());
  784. delete Id5;
  785. // Result has more elements than operands, and extra elements are undef; choose from Op1.
  786. ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1,
  787. ConstantVector::get({C4, C5, C6, CU, CU, CU}));
  788. EXPECT_FALSE(Id6->isIdentity());
  789. EXPECT_TRUE(Id6->isIdentityWithPadding());
  790. EXPECT_FALSE(Id6->isIdentityWithExtract());
  791. EXPECT_FALSE(Id6->isConcat());
  792. delete Id6;
  793. // Result has more elements than operands, but extra elements are not undef.
  794. ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
  795. ConstantVector::get({C0, C1, C2, C3, CU, C1}));
  796. EXPECT_FALSE(Id7->isIdentity());
  797. EXPECT_FALSE(Id7->isIdentityWithPadding());
  798. EXPECT_FALSE(Id7->isIdentityWithExtract());
  799. EXPECT_FALSE(Id7->isConcat());
  800. delete Id7;
  801. // Result has more elements than operands; choose from Op0 and Op1 is not identity.
  802. ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
  803. ConstantVector::get({C4, CU, C2, C3, CU, CU}));
  804. EXPECT_FALSE(Id8->isIdentity());
  805. EXPECT_FALSE(Id8->isIdentityWithPadding());
  806. EXPECT_FALSE(Id8->isIdentityWithExtract());
  807. EXPECT_FALSE(Id8->isConcat());
  808. delete Id8;
  809. // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat.
  810. ShuffleVectorInst *Id9 = new ShuffleVectorInst(V0, V1,
  811. ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
  812. EXPECT_FALSE(Id9->isIdentity());
  813. EXPECT_FALSE(Id9->isIdentityWithPadding());
  814. EXPECT_FALSE(Id9->isIdentityWithExtract());
  815. EXPECT_TRUE(Id9->isConcat());
  816. delete Id9;
  817. // Result has less than twice as many elements as operands, so not a concat.
  818. ShuffleVectorInst *Id10 = new ShuffleVectorInst(V0, V1,
  819. ConstantVector::get({C0, CU, C2, C3, CU, CU, C6}));
  820. EXPECT_FALSE(Id10->isIdentity());
  821. EXPECT_FALSE(Id10->isIdentityWithPadding());
  822. EXPECT_FALSE(Id10->isIdentityWithExtract());
  823. EXPECT_FALSE(Id10->isConcat());
  824. delete Id10;
  825. // Result has more than twice as many elements as operands, so not a concat.
  826. ShuffleVectorInst *Id11 = new ShuffleVectorInst(V0, V1,
  827. ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7, CU}));
  828. EXPECT_FALSE(Id11->isIdentity());
  829. EXPECT_FALSE(Id11->isIdentityWithPadding());
  830. EXPECT_FALSE(Id11->isIdentityWithExtract());
  831. EXPECT_FALSE(Id11->isConcat());
  832. delete Id11;
  833. // If an input is undef, it's not a concat.
  834. // TODO: IdentityWithPadding should be true here even though the high mask values are not undef.
  835. ShuffleVectorInst *Id12 = new ShuffleVectorInst(V0, ConstantVector::get({CU, CU, CU, CU}),
  836. ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
  837. EXPECT_FALSE(Id12->isIdentity());
  838. EXPECT_FALSE(Id12->isIdentityWithPadding());
  839. EXPECT_FALSE(Id12->isIdentityWithExtract());
  840. EXPECT_FALSE(Id12->isConcat());
  841. delete Id12;
  842. }
  843. TEST(InstructionsTest, SkipDebug) {
  844. LLVMContext C;
  845. std::unique_ptr<Module> M = parseIR(C,
  846. R"(
  847. declare void @llvm.dbg.value(metadata, metadata, metadata)
  848. define void @f() {
  849. entry:
  850. call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
  851. ret void
  852. }
  853. !llvm.dbg.cu = !{!0}
  854. !llvm.module.flags = !{!3, !4}
  855. !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
  856. !1 = !DIFile(filename: "t2.c", directory: "foo")
  857. !2 = !{}
  858. !3 = !{i32 2, !"Dwarf Version", i32 4}
  859. !4 = !{i32 2, !"Debug Info Version", i32 3}
  860. !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
  861. !9 = !DISubroutineType(types: !10)
  862. !10 = !{null}
  863. !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
  864. !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  865. !13 = !DILocation(line: 2, column: 7, scope: !8)
  866. )");
  867. ASSERT_TRUE(M);
  868. Function *F = cast<Function>(M->getNamedValue("f"));
  869. BasicBlock &BB = F->front();
  870. // The first non-debug instruction is the terminator.
  871. auto *Term = BB.getTerminator();
  872. EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
  873. EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
  874. // After the terminator, there are no non-debug instructions.
  875. EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
  876. }
  877. TEST(InstructionsTest, PhiMightNotBeFPMathOperator) {
  878. LLVMContext Context;
  879. IRBuilder<> Builder(Context);
  880. MDBuilder MDHelper(Context);
  881. Instruction *I = Builder.CreatePHI(Builder.getInt32Ty(), 0);
  882. EXPECT_FALSE(isa<FPMathOperator>(I));
  883. I->deleteValue();
  884. Instruction *FP = Builder.CreatePHI(Builder.getDoubleTy(), 0);
  885. EXPECT_TRUE(isa<FPMathOperator>(FP));
  886. FP->deleteValue();
  887. }
  888. TEST(InstructionsTest, FNegInstruction) {
  889. LLVMContext Context;
  890. Type *FltTy = Type::getFloatTy(Context);
  891. Constant *One = ConstantFP::get(FltTy, 1.0);
  892. BinaryOperator *FAdd = BinaryOperator::CreateFAdd(One, One);
  893. FAdd->setHasNoNaNs(true);
  894. UnaryOperator *FNeg = UnaryOperator::CreateFNegFMF(One, FAdd);
  895. EXPECT_TRUE(FNeg->hasNoNaNs());
  896. EXPECT_FALSE(FNeg->hasNoInfs());
  897. EXPECT_FALSE(FNeg->hasNoSignedZeros());
  898. EXPECT_FALSE(FNeg->hasAllowReciprocal());
  899. EXPECT_FALSE(FNeg->hasAllowContract());
  900. EXPECT_FALSE(FNeg->hasAllowReassoc());
  901. EXPECT_FALSE(FNeg->hasApproxFunc());
  902. FAdd->deleteValue();
  903. FNeg->deleteValue();
  904. }
  905. TEST(InstructionsTest, CallBrInstruction) {
  906. LLVMContext Context;
  907. std::unique_ptr<Module> M = parseIR(Context, R"(
  908. define void @foo() {
  909. entry:
  910. callbr void asm sideeffect "// XXX: ${0:l}", "X"(i8* blockaddress(@foo, %branch_test.exit))
  911. to label %land.rhs.i [label %branch_test.exit]
  912. land.rhs.i:
  913. br label %branch_test.exit
  914. branch_test.exit:
  915. %0 = phi i1 [ true, %entry ], [ false, %land.rhs.i ]
  916. br i1 %0, label %if.end, label %if.then
  917. if.then:
  918. ret void
  919. if.end:
  920. ret void
  921. }
  922. )");
  923. Function *Foo = M->getFunction("foo");
  924. auto BBs = Foo->getBasicBlockList().begin();
  925. CallBrInst &CBI = cast<CallBrInst>(BBs->front());
  926. ++BBs;
  927. ++BBs;
  928. BasicBlock &BranchTestExit = *BBs;
  929. ++BBs;
  930. BasicBlock &IfThen = *BBs;
  931. // Test that setting the first indirect destination of callbr updates the dest
  932. EXPECT_EQ(&BranchTestExit, CBI.getIndirectDest(0));
  933. CBI.setIndirectDest(0, &IfThen);
  934. EXPECT_EQ(&IfThen, CBI.getIndirectDest(0));
  935. // Further, test that changing the indirect destination updates the arg
  936. // operand to use the block address of the new indirect destination basic
  937. // block. This is a critical invariant of CallBrInst.
  938. BlockAddress *IndirectBA = BlockAddress::get(CBI.getIndirectDest(0));
  939. BlockAddress *ArgBA = cast<BlockAddress>(CBI.getArgOperand(0));
  940. EXPECT_EQ(IndirectBA, ArgBA)
  941. << "After setting the indirect destination, callbr had an indirect "
  942. "destination of '"
  943. << CBI.getIndirectDest(0)->getName() << "', but a argument of '"
  944. << ArgBA->getBasicBlock()->getName() << "'. These should always match:\n"
  945. << CBI;
  946. EXPECT_EQ(IndirectBA->getBasicBlock(), &IfThen);
  947. EXPECT_EQ(ArgBA->getBasicBlock(), &IfThen);
  948. }
  949. TEST(InstructionsTest, UnaryOperator) {
  950. LLVMContext Context;
  951. IRBuilder<> Builder(Context);
  952. Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
  953. Value *F = Builder.CreateFNeg(I);
  954. EXPECT_TRUE(isa<Value>(F));
  955. EXPECT_TRUE(isa<Instruction>(F));
  956. EXPECT_TRUE(isa<UnaryInstruction>(F));
  957. EXPECT_TRUE(isa<UnaryOperator>(F));
  958. EXPECT_FALSE(isa<BinaryOperator>(F));
  959. F->deleteValue();
  960. I->deleteValue();
  961. }
  962. } // end anonymous namespace
  963. } // end namespace llvm