blob: 3b2bd6fa81b0f398ecbcb5bd16464562ed9b8bcc [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===//
Gabor Greif15580382010-03-16 09:55:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Gabor Greif15580382010-03-16 09:55:46 +00006//
7//===----------------------------------------------------------------------===//
8
Vedant Kumarf01827f2018-06-19 23:42:17 +00009#include "llvm/AsmParser/Parser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000010#include "llvm/IR/Instructions.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/DataLayout.h"
16#include "llvm/IR/DerivedTypes.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000017#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/MDBuilder.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000021#include "llvm/IR/Module.h"
Sanjoy Dasaa722ae2017-02-23 22:50:52 +000022#include "llvm/IR/NoFolder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Operator.h"
Vedant Kumarf01827f2018-06-19 23:42:17 +000024#include "llvm/Support/SourceMgr.h"
Zvi Rackoverdfbd3d72017-05-08 12:40:18 +000025#include "gmock/gmock-matchers.h"
Gabor Greif15580382010-03-16 09:55:46 +000026#include "gtest/gtest.h"
Eli Bendersky84aa5e52014-03-26 20:41:15 +000027#include <memory>
Gabor Greif15580382010-03-16 09:55:46 +000028
29namespace llvm {
30namespace {
31
Vedant Kumarf01827f2018-06-19 23:42:17 +000032static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
33 SMDiagnostic Err;
34 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
35 if (!Mod)
36 Err.print("InstructionsTests", errs());
37 return Mod;
38}
39
Gabor Greif35a9b8b2010-03-16 10:59:48 +000040TEST(InstructionsTest, ReturnInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000041 LLVMContext C;
Gabor Greif15580382010-03-16 09:55:46 +000042
Gabor Greif35a9b8b2010-03-16 10:59:48 +000043 // test for PR6589
Gabor Greif15580382010-03-16 09:55:46 +000044 const ReturnInst* r0 = ReturnInst::Create(C);
Gabor Greifc377afc2010-03-16 15:26:09 +000045 EXPECT_EQ(r0->getNumOperands(), 0U);
Gabor Greif35a9b8b2010-03-16 10:59:48 +000046 EXPECT_EQ(r0->op_begin(), r0->op_end());
Gabor Greif86ca5492010-03-16 11:24:53 +000047
Chris Lattner229907c2011-07-18 04:54:35 +000048 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greif86ca5492010-03-16 11:24:53 +000049 Constant* One = ConstantInt::get(Int1, 1, true);
50 const ReturnInst* r1 = ReturnInst::Create(C, One);
John McCalle83797c2011-08-27 19:23:22 +000051 EXPECT_EQ(1U, r1->getNumOperands());
Gabor Greif86ca5492010-03-16 11:24:53 +000052 User::const_op_iterator b(r1->op_begin());
John McCalle83797c2011-08-27 19:23:22 +000053 EXPECT_NE(r1->op_end(), b);
54 EXPECT_EQ(One, *b);
55 EXPECT_EQ(One, r1->getOperand(0));
Gabor Greif86ca5492010-03-16 11:24:53 +000056 ++b;
John McCalle83797c2011-08-27 19:23:22 +000057 EXPECT_EQ(r1->op_end(), b);
Gabor Greif421dd122010-03-16 12:32:03 +000058
59 // clean up
60 delete r0;
61 delete r1;
Gabor Greif15580382010-03-16 09:55:46 +000062}
63
Eli Bendersky84741622014-03-26 21:46:24 +000064// Test fixture that provides a module and a single function within it. Useful
65// for tests that need to refer to the function in some way.
66class ModuleWithFunctionTest : public testing::Test {
67protected:
NAKAMURA Takumibe8556d2014-03-27 11:32:41 +000068 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
NAKAMURA Takumicb5ebf62014-03-27 11:38:28 +000069 FArgTypes.push_back(Type::getInt8Ty(Ctx));
70 FArgTypes.push_back(Type::getInt32Ty(Ctx));
71 FArgTypes.push_back(Type::getInt64Ty(Ctx));
Eli Bendersky84741622014-03-26 21:46:24 +000072 FunctionType *FTy =
73 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
74 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
75 }
Eli Bendersky84aa5e52014-03-26 20:41:15 +000076
Eli Bendersky84741622014-03-26 21:46:24 +000077 LLVMContext Ctx;
78 std::unique_ptr<Module> M;
NAKAMURA Takumicce8a582014-03-27 11:33:11 +000079 SmallVector<Type *, 3> FArgTypes;
Eli Bendersky84741622014-03-26 21:46:24 +000080 Function *F;
81};
Eli Bendersky84aa5e52014-03-26 20:41:15 +000082
Eli Bendersky84741622014-03-26 21:46:24 +000083TEST_F(ModuleWithFunctionTest, CallInst) {
84 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
85 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
86 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
Eli Benderskyc35c4b32014-03-26 21:11:34 +000087 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
Eli Bendersky84aa5e52014-03-26 20:41:15 +000088
89 // Make sure iteration over a call's arguments works as expected.
90 unsigned Idx = 0;
91 for (Value *Arg : Call->arg_operands()) {
Eli Bendersky84741622014-03-26 21:46:24 +000092 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
Eli Bendersky84aa5e52014-03-26 20:41:15 +000093 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
94 Idx++;
95 }
96}
97
Eli Bendersky84741622014-03-26 21:46:24 +000098TEST_F(ModuleWithFunctionTest, InvokeInst) {
99 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
100 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
101
102 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
103 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
104 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
105 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
106
107 // Make sure iteration over invoke's arguments works as expected.
108 unsigned Idx = 0;
109 for (Value *Arg : Invoke->arg_operands()) {
110 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
111 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
112 Idx++;
113 }
114}
115
Gabor Greifc377afc2010-03-16 15:26:09 +0000116TEST(InstructionsTest, BranchInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000117 LLVMContext C;
Gabor Greifc377afc2010-03-16 15:26:09 +0000118
119 // Make a BasicBlocks
120 BasicBlock* bb0 = BasicBlock::Create(C);
121 BasicBlock* bb1 = BasicBlock::Create(C);
122
123 // Mandatory BranchInst
124 const BranchInst* b0 = BranchInst::Create(bb0);
125
Gabor Greife52f3982010-03-16 15:53:58 +0000126 EXPECT_TRUE(b0->isUnconditional());
127 EXPECT_FALSE(b0->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000128 EXPECT_EQ(1U, b0->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000129
Gabor Greifc377afc2010-03-16 15:26:09 +0000130 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000131 EXPECT_EQ(1U, b0->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000132
133 EXPECT_NE(b0->op_begin(), b0->op_end());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000134 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greife52f3982010-03-16 15:53:58 +0000135
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000136 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
Gabor Greifc377afc2010-03-16 15:26:09 +0000137
Chris Lattner229907c2011-07-18 04:54:35 +0000138 IntegerType* Int1 = IntegerType::get(C, 1);
Gabor Greifc377afc2010-03-16 15:26:09 +0000139 Constant* One = ConstantInt::get(Int1, 1, true);
140
141 // Conditional BranchInst
142 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
143
Gabor Greife52f3982010-03-16 15:53:58 +0000144 EXPECT_FALSE(b1->isUnconditional());
145 EXPECT_TRUE(b1->isConditional());
John McCalle83797c2011-08-27 19:23:22 +0000146 EXPECT_EQ(2U, b1->getNumSuccessors());
Gabor Greife52f3982010-03-16 15:53:58 +0000147
Gabor Greifc377afc2010-03-16 15:26:09 +0000148 // check num operands
John McCalle83797c2011-08-27 19:23:22 +0000149 EXPECT_EQ(3U, b1->getNumOperands());
Gabor Greifc377afc2010-03-16 15:26:09 +0000150
151 User::const_op_iterator b(b1->op_begin());
152
153 // check COND
154 EXPECT_NE(b, b1->op_end());
John McCalle83797c2011-08-27 19:23:22 +0000155 EXPECT_EQ(One, *b);
156 EXPECT_EQ(One, b1->getOperand(0));
157 EXPECT_EQ(One, b1->getCondition());
Gabor Greifc377afc2010-03-16 15:26:09 +0000158 ++b;
159
160 // check ELSE
John McCalle83797c2011-08-27 19:23:22 +0000161 EXPECT_EQ(bb1, *b);
162 EXPECT_EQ(bb1, b1->getOperand(1));
163 EXPECT_EQ(bb1, b1->getSuccessor(1));
Gabor Greifc377afc2010-03-16 15:26:09 +0000164 ++b;
165
166 // check THEN
John McCalle83797c2011-08-27 19:23:22 +0000167 EXPECT_EQ(bb0, *b);
168 EXPECT_EQ(bb0, b1->getOperand(2));
169 EXPECT_EQ(bb0, b1->getSuccessor(0));
Gabor Greifc377afc2010-03-16 15:26:09 +0000170 ++b;
171
John McCalle83797c2011-08-27 19:23:22 +0000172 EXPECT_EQ(b1->op_end(), b);
Gabor Greifc377afc2010-03-16 15:26:09 +0000173
Gabor Greifc377afc2010-03-16 15:26:09 +0000174 // clean up
175 delete b0;
176 delete b1;
177
178 delete bb0;
179 delete bb1;
180}
181
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000182TEST(InstructionsTest, CastInst) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000183 LLVMContext C;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000184
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000185 Type *Int8Ty = Type::getInt8Ty(C);
186 Type *Int16Ty = Type::getInt16Ty(C);
187 Type *Int32Ty = Type::getInt32Ty(C);
188 Type *Int64Ty = Type::getInt64Ty(C);
189 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
190 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
191 Type *X86MMXTy = Type::getX86_MMXTy(C);
192
193 Type *HalfTy = Type::getHalfTy(C);
194 Type *FloatTy = Type::getFloatTy(C);
195 Type *DoubleTy = Type::getDoubleTy(C);
196
197 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
198 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
199 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
200
201 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
202 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
203
204 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
205 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
206
207 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
208 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
209 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
210 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
211
212 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
213 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000214 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000215
Duncan Sandsa8514532011-05-18 07:13:41 +0000216 const Constant* c8 = Constant::getNullValue(V8x8Ty);
217 const Constant* c64 = Constant::getNullValue(V8x64Ty);
218
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000219 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
220
Matt Arsenaultb4019ae2013-07-30 22:02:14 +0000221 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
222 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
223 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
224 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
225 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
John McCalle83797c2011-08-27 19:23:22 +0000226 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
227 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000228
229 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
230 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
231 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
232 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
233 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
234
235 // Check address space casts are rejected since we don't know the sizes here
236 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
237 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
238 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
240 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000241 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
242 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
243 V2Int32PtrAS1Ty,
244 true));
Matt Arsenaultcacbb232013-07-30 20:45:05 +0000245
246 // Test mismatched number of elements for pointers
247 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
248 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
249 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
250 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
251 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
252
253 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
254 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
255 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
256 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
257 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
258 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
259 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
260 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
261 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
262
263 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
264 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
265 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
266
267 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
268 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
269 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
270 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
271 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
272 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
Matt Arsenault065ced92013-07-31 00:17:33 +0000273
274
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +0000275 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
276 Constant::getNullValue(V4Int32PtrTy),
277 V2Int32PtrTy));
278 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
279 Constant::getNullValue(V2Int32PtrTy),
280 V4Int32PtrTy));
281
282 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
283 Constant::getNullValue(V4Int32PtrAS1Ty),
284 V2Int32PtrTy));
285 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
286 Constant::getNullValue(V2Int32PtrTy),
287 V4Int32PtrAS1Ty));
288
289
Matt Arsenault065ced92013-07-31 00:17:33 +0000290 // Check that assertion is not hit when creating a cast with a vector of
291 // pointers
292 // First form
293 BasicBlock *BB = BasicBlock::Create(C);
294 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000295 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
Matt Arsenault065ced92013-07-31 00:17:33 +0000296
297 // Second form
Mehdi Amini03b42e42016-04-14 21:59:01 +0000298 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
299
300 delete Inst2;
301 Inst1->eraseFromParent();
302 delete BB;
Duncan Sands2d3cdd62011-04-01 03:34:54 +0000303}
304
Nadav Rotem3924cb02011-12-05 06:29:09 +0000305TEST(InstructionsTest, VectorGep) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000306 LLVMContext C;
Nadav Rotem3924cb02011-12-05 06:29:09 +0000307
308 // Type Definitions
David Blaikieb3a39062015-03-14 21:40:10 +0000309 Type *I8Ty = IntegerType::get(C, 8);
310 Type *I32Ty = IntegerType::get(C, 32);
311 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
312 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000313
314 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
315 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
316
317 // Test different aspects of the vector-of-pointers type
318 // and GEPs which use this type.
319 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
320 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
321 std::vector<Constant*> ConstVa(2, Ci32a);
322 std::vector<Constant*> ConstVb(2, Ci32b);
323 Constant *C2xi32a = ConstantVector::get(ConstVa);
324 Constant *C2xi32b = ConstantVector::get(ConstVb);
325
326 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
327 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
328
329 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
330 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
331 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
332
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000333 BasicBlock* BB0 = BasicBlock::Create(C);
334 // Test InsertAtEnd ICmpInst constructor.
335 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
336 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
337
David Blaikieb3a39062015-03-14 21:40:10 +0000338 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
339 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
340 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
341 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000342
343 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
344 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
345 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
346 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
347
348 Value *S0 = BTC0->stripPointerCasts();
349 Value *S1 = BTC1->stripPointerCasts();
350 Value *S2 = BTC2->stripPointerCasts();
351 Value *S3 = BTC3->stripPointerCasts();
352
353 EXPECT_NE(S0, Gep0);
354 EXPECT_NE(S1, Gep1);
355 EXPECT_NE(S2, Gep2);
356 EXPECT_NE(S3, Gep3);
357
358 int64_t Offset;
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000359 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
Rafael Espindolac6751622013-12-13 18:56:34 +0000360 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
Nadav Rotem3924cb02011-12-05 06:29:09 +0000361 ":128:128-n8:16:32:64-S128");
362 // Make sure we don't crash
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000363 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
364 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
365 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
366 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000367
368 // Gep of Geps
David Blaikieb3a39062015-03-14 21:40:10 +0000369 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
370 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
371 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
372 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
Nadav Rotem3924cb02011-12-05 06:29:09 +0000373
374 EXPECT_EQ(GepII0->getNumIndices(), 1u);
375 EXPECT_EQ(GepII1->getNumIndices(), 1u);
376 EXPECT_EQ(GepII2->getNumIndices(), 1u);
377 EXPECT_EQ(GepII3->getNumIndices(), 1u);
378
379 EXPECT_FALSE(GepII0->hasAllZeroIndices());
380 EXPECT_FALSE(GepII1->hasAllZeroIndices());
381 EXPECT_FALSE(GepII2->hasAllZeroIndices());
382 EXPECT_FALSE(GepII3->hasAllZeroIndices());
383
384 delete GepII0;
385 delete GepII1;
386 delete GepII2;
387 delete GepII3;
388
389 delete BTC0;
390 delete BTC1;
391 delete BTC2;
392 delete BTC3;
393
394 delete Gep0;
395 delete Gep1;
396 delete Gep2;
397 delete Gep3;
398
Evgeniy Stepanova259b262013-01-16 14:38:50 +0000399 ICmp2->eraseFromParent();
400 delete BB0;
401
Nadav Rotem3924cb02011-12-05 06:29:09 +0000402 delete ICmp0;
403 delete ICmp1;
404 delete PtrVecA;
405 delete PtrVecB;
406}
407
Duncan Sands05f4df82012-04-16 16:28:59 +0000408TEST(InstructionsTest, FPMathOperator) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000409 LLVMContext Context;
Duncan Sands05f4df82012-04-16 16:28:59 +0000410 IRBuilder<> Builder(Context);
411 MDBuilder MDHelper(Context);
412 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
413 MDNode *MD1 = MDHelper.createFPMath(1.0);
Duncan Sands05f4df82012-04-16 16:28:59 +0000414 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000415 EXPECT_TRUE(isa<FPMathOperator>(V1));
Duncan Sands05f4df82012-04-16 16:28:59 +0000416 FPMathOperator *O1 = cast<FPMathOperator>(V1);
Duncan Sands05f4df82012-04-16 16:28:59 +0000417 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
Reid Kleckner96ab8722017-05-18 17:24:10 +0000418 V1->deleteValue();
419 I->deleteValue();
Duncan Sands05f4df82012-04-16 16:28:59 +0000420}
421
Duncan Sandse2395dc2012-10-30 16:03:32 +0000422
423TEST(InstructionsTest, isEliminableCastPair) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000424 LLVMContext C;
Duncan Sandse2395dc2012-10-30 16:03:32 +0000425
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000426 Type* Int16Ty = Type::getInt16Ty(C);
Duncan Sandse2395dc2012-10-30 16:03:32 +0000427 Type* Int32Ty = Type::getInt32Ty(C);
428 Type* Int64Ty = Type::getInt64Ty(C);
429 Type* Int64PtrTy = Type::getInt64PtrTy(C);
430
431 // Source and destination pointers have same size -> bitcast.
432 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
433 CastInst::IntToPtr,
434 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000435 Int32Ty, nullptr, Int32Ty),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000436 CastInst::BitCast);
437
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000438 // Source and destination have unknown sizes, but the same address space and
439 // the intermediate int is the maximum pointer size -> bitcast
Duncan Sandse2395dc2012-10-30 16:03:32 +0000440 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
441 CastInst::IntToPtr,
442 Int64PtrTy, Int64Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000443 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000444 CastInst::BitCast);
445
446 // Source and destination have unknown sizes, but the same address space and
447 // the intermediate int is not the maximum pointer size -> nothing
448 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
449 CastInst::IntToPtr,
450 Int64PtrTy, Int32Ty, Int64PtrTy,
Craig Topper66f09ad2014-06-08 22:29:17 +0000451 nullptr, nullptr, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000452 0U);
453
454 // Middle pointer big enough -> bitcast.
455 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
456 CastInst::PtrToInt,
457 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000458 nullptr, Int64Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000459 CastInst::BitCast);
460
461 // Middle pointer too small -> fail.
462 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
463 CastInst::PtrToInt,
464 Int64Ty, Int64PtrTy, Int64Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000465 nullptr, Int32Ty, nullptr),
Duncan Sandse2395dc2012-10-30 16:03:32 +0000466 0U);
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000467
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000468 // Test that we don't eliminate bitcasts between different address spaces,
469 // or if we don't have available pointer size information.
470 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
471 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
Rafael Espindolac6751622013-12-13 18:56:34 +0000472 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000473
474 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
475 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
476
477 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
478 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
479
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000480 // Cannot simplify inttoptr, addrspacecast
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000481 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000482 CastInst::AddrSpaceCast,
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000483 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
Craig Topper66f09ad2014-06-08 22:29:17 +0000484 nullptr, Int16SizePtr, Int64SizePtr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000485 0U);
486
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000487 // Cannot simplify addrspacecast, ptrtoint
488 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
489 CastInst::PtrToInt,
490 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
Craig Topper66f09ad2014-06-08 22:29:17 +0000491 Int64SizePtr, Int16SizePtr, nullptr),
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +0000492 0U);
493
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000494 // Pass since the bitcast address spaces are the same
495 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
496 CastInst::BitCast,
497 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
Craig Topper66f09ad2014-06-08 22:29:17 +0000498 nullptr, nullptr, nullptr),
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000499 CastInst::IntToPtr);
500
Duncan Sandse2395dc2012-10-30 16:03:32 +0000501}
502
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000503TEST(InstructionsTest, CloneCall) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000504 LLVMContext C;
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000505 Type *Int32Ty = Type::getInt32Ty(C);
506 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
James Y Knight7976eb52019-02-01 20:43:25 +0000507 FunctionType *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000508 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
509 Value *Args[] = {
510 ConstantInt::get(Int32Ty, 1),
511 ConstantInt::get(Int32Ty, 2),
512 ConstantInt::get(Int32Ty, 3)
513 };
James Y Knight7976eb52019-02-01 20:43:25 +0000514 std::unique_ptr<CallInst> Call(
515 CallInst::Create(FnTy, Callee, Args, "result"));
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000516
517 // Test cloning the tail call kind.
518 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
519 CallInst::TCK_MustTail};
520 for (CallInst::TailCallKind TCK : Kinds) {
521 Call->setTailCallKind(TCK);
522 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
523 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
524 }
525 Call->setTailCallKind(CallInst::TCK_None);
526
527 // Test cloning an attribute.
528 {
529 AttrBuilder AB;
530 AB.addAttribute(Attribute::ReadOnly);
Reid Klecknerb5180542017-03-21 16:57:19 +0000531 Call->setAttributes(
532 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000533 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
534 EXPECT_TRUE(Clone->onlyReadsMemory());
535 }
536}
537
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000538TEST(InstructionsTest, AlterCallBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000539 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000540 Type *Int32Ty = Type::getInt32Ty(C);
James Y Knight7976eb52019-02-01 20:43:25 +0000541 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000542 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
543 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
544 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
545 std::unique_ptr<CallInst> Call(
James Y Knight7976eb52019-02-01 20:43:25 +0000546 CallInst::Create(FnTy, Callee, Args, OldBundle, "result"));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000547 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
548 AttrBuilder AB;
549 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000550 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000551 Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000552
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000553 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
554 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
555 EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
556 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
557 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
558 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
559 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
560 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
Joseph Tremoulet56c99582016-01-14 06:30:19 +0000561 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000562 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
563}
Matt Arsenault130e0ef2013-07-30 22:27:10 +0000564
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000565TEST(InstructionsTest, AlterInvokeBundles) {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000566 LLVMContext C;
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000567 Type *Int32Ty = Type::getInt32Ty(C);
James Y Knightd9e85a02019-02-01 20:43:34 +0000568 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000569 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
570 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000571 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
572 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000573 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
James Y Knightd9e85a02019-02-01 20:43:34 +0000574 std::unique_ptr<InvokeInst> Invoke(
575 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
576 OldBundle, "result"));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000577 AttrBuilder AB;
578 AB.addAttribute(Attribute::Cold);
Reid Klecknerb5180542017-03-21 16:57:19 +0000579 Invoke->setAttributes(
580 AttributeList::get(C, AttributeList::FunctionIndex, AB));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000581 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
582
583 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
Joseph Tremouletf6cc7e62016-01-15 15:08:36 +0000584 std::unique_ptr<InvokeInst> Clone(
585 InvokeInst::Create(Invoke.get(), NewBundle));
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000586 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
587 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
588 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
589 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
590 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
591 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
592 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
NAKAMURA Takumi3557b882016-01-14 09:21:49 +0000593 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000594 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000595}
596
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000597TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
598 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
599 auto *Arg0 = &*F->arg_begin();
600
601 IRBuilder<NoFolder> B(Ctx);
602 B.SetInsertPoint(OnlyBB);
603
604 {
605 auto *UI =
606 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
607 ASSERT_TRUE(UI->isExact());
608 UI->dropPoisonGeneratingFlags();
609 ASSERT_FALSE(UI->isExact());
610 }
611
612 {
613 auto *ShrI =
614 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
615 ASSERT_TRUE(ShrI->isExact());
616 ShrI->dropPoisonGeneratingFlags();
617 ASSERT_FALSE(ShrI->isExact());
618 }
619
620 {
621 auto *AI = cast<Instruction>(
622 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
623 ASSERT_TRUE(AI->hasNoUnsignedWrap());
624 AI->dropPoisonGeneratingFlags();
625 ASSERT_FALSE(AI->hasNoUnsignedWrap());
626 ASSERT_FALSE(AI->hasNoSignedWrap());
627 }
628
629 {
630 auto *SI = cast<Instruction>(
631 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
632 ASSERT_TRUE(SI->hasNoSignedWrap());
633 SI->dropPoisonGeneratingFlags();
634 ASSERT_FALSE(SI->hasNoUnsignedWrap());
635 ASSERT_FALSE(SI->hasNoSignedWrap());
636 }
637
638 {
639 auto *ShlI = cast<Instruction>(
640 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
641 ASSERT_TRUE(ShlI->hasNoSignedWrap());
642 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
643 ShlI->dropPoisonGeneratingFlags();
644 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
645 ASSERT_FALSE(ShlI->hasNoSignedWrap());
646 }
647
648 {
649 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
James Y Knight77160752019-02-01 20:44:47 +0000650 auto *GI = cast<GetElementPtrInst>(
651 B.CreateInBoundsGEP(B.getInt8Ty(), GEPBase, Arg0));
Sanjoy Dasaa722ae2017-02-23 22:50:52 +0000652 ASSERT_TRUE(GI->isInBounds());
653 GI->dropPoisonGeneratingFlags();
654 ASSERT_FALSE(GI->isInBounds());
655 }
656}
657
Chandler Carruthd1c95b62017-02-28 08:04:20 +0000658TEST(InstructionsTest, GEPIndices) {
659 LLVMContext Context;
660 IRBuilder<NoFolder> Builder(Context);
661 Type *ElementTy = Builder.getInt8Ty();
662 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
663 Value *Indices[] = {
664 Builder.getInt32(0),
665 Builder.getInt32(13),
666 Builder.getInt32(42) };
667
668 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
669 Indices);
670 ASSERT_TRUE(isa<GetElementPtrInst>(V));
671
672 auto *GEPI = cast<GetElementPtrInst>(V);
673 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
674 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
675 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
676 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
677 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
678 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
679 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
680
681 const auto *CGEPI = GEPI;
682 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
683 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
684 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
685 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
686 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
687 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
688 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
689
690 delete GEPI;
691}
692
Chandler Carruth927d8e62017-04-12 07:27:28 +0000693TEST(InstructionsTest, SwitchInst) {
694 LLVMContext C;
695
696 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
697 BB1.reset(BasicBlock::Create(C));
698 BB2.reset(BasicBlock::Create(C));
699 BB3.reset(BasicBlock::Create(C));
700
701 // We create block 0 after the others so that it gets destroyed first and
702 // clears the uses of the other basic blocks.
703 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
704
705 auto *Int32Ty = Type::getInt32Ty(C);
706
707 SwitchInst *SI =
708 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
709 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
710 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
711 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
712
713 auto CI = SI->case_begin();
714 ASSERT_NE(CI, SI->case_end());
715 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
716 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
717 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
718 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
719 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
720 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
721 EXPECT_EQ(CI + 1, std::next(CI));
722 EXPECT_EQ(CI + 2, std::next(CI, 2));
723 EXPECT_EQ(CI + 3, std::next(CI, 3));
724 EXPECT_EQ(SI->case_end(), CI + 3);
725 EXPECT_EQ(0, CI - CI);
726 EXPECT_EQ(1, (CI + 1) - CI);
727 EXPECT_EQ(2, (CI + 2) - CI);
728 EXPECT_EQ(3, SI->case_end() - CI);
729 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
730
731 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
732 SwitchInst::ConstCaseIt CCE = SI->case_end();
733 ASSERT_NE(CCI, SI->case_end());
734 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
735 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
736 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
737 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
738 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
739 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
740 EXPECT_EQ(CCI + 1, std::next(CCI));
741 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
742 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
743 EXPECT_EQ(CCE, CCI + 3);
744 EXPECT_EQ(0, CCI - CCI);
745 EXPECT_EQ(1, (CCI + 1) - CCI);
746 EXPECT_EQ(2, (CCI + 2) - CCI);
747 EXPECT_EQ(3, CCE - CCI);
748 EXPECT_EQ(3, std::distance(CCI, CCE));
749
750 // Make sure that the const iterator is compatible with a const auto ref.
751 const auto &Handle = *CCI;
752 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
753 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
754}
755
Zvi Rackoverdfbd3d72017-05-08 12:40:18 +0000756TEST(InstructionsTest, CommuteShuffleMask) {
757 SmallVector<int, 16> Indices({-1, 0, 7});
758 ShuffleVectorInst::commuteShuffleMask(Indices, 4);
759 EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
760}
761
Sanjay Patel2ca33602018-06-19 18:44:00 +0000762TEST(InstructionsTest, ShuffleMaskQueries) {
763 // Create the elements for various constant vectors.
764 LLVMContext Ctx;
765 Type *Int32Ty = Type::getInt32Ty(Ctx);
766 Constant *CU = UndefValue::get(Int32Ty);
767 Constant *C0 = ConstantInt::get(Int32Ty, 0);
768 Constant *C1 = ConstantInt::get(Int32Ty, 1);
769 Constant *C2 = ConstantInt::get(Int32Ty, 2);
770 Constant *C3 = ConstantInt::get(Int32Ty, 3);
771 Constant *C4 = ConstantInt::get(Int32Ty, 4);
772 Constant *C5 = ConstantInt::get(Int32Ty, 5);
773 Constant *C6 = ConstantInt::get(Int32Ty, 6);
774 Constant *C7 = ConstantInt::get(Int32Ty, 7);
775
776 Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
777 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity));
778 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select
779 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity));
780 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source
781 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity));
782 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity));
783
784 Constant *Select = ConstantVector::get({CU, C1, C5});
785 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select));
786 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select));
787 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select));
788 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select));
789 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select));
790 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select));
791
792 Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
793 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse));
794 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse));
795 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse));
796 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source
797 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse));
798 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse));
799
800 Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
801 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource));
802 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource));
803 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource));
804 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource));
805 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource));
806 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource));
807
808 Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
809 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat));
810 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat));
811 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat));
812 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source
813 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat));
814 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat));
815
816 Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
817 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose));
818 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose));
819 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose));
820 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose));
821 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose));
822 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose));
823
824 // More tests to make sure the logic is/stays correct...
825 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3})));
826 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU})));
827
828 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU})));
829 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3})));
830
831 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4})));
832 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU})));
833
834 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7})));
835 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3})));
836
837 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4})));
838 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0})));
839
840 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7})));
841 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
Sanjay Patelac619a02018-08-30 15:05:38 +0000842
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000843 // Nothing special about the values here - just re-using inputs to reduce code.
844 Constant *V0 = ConstantVector::get({C0, C1, C2, C3});
845 Constant *V1 = ConstantVector::get({C3, C2, C1, C0});
846
Sanjay Patelac619a02018-08-30 15:05:38 +0000847 // Identity with undef elts.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000848 ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000849 ConstantVector::get({C0, C1, CU, CU}));
850 EXPECT_TRUE(Id1->isIdentity());
851 EXPECT_FALSE(Id1->isIdentityWithPadding());
852 EXPECT_FALSE(Id1->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000853 EXPECT_FALSE(Id1->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000854 delete Id1;
855
856 // Result has less elements than operands.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000857 ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000858 ConstantVector::get({C0, C1, C2}));
859 EXPECT_FALSE(Id2->isIdentity());
860 EXPECT_FALSE(Id2->isIdentityWithPadding());
861 EXPECT_TRUE(Id2->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000862 EXPECT_FALSE(Id2->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000863 delete Id2;
864
865 // Result has less elements than operands; choose from Op1.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000866 ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000867 ConstantVector::get({C4, CU, C6}));
868 EXPECT_FALSE(Id3->isIdentity());
869 EXPECT_FALSE(Id3->isIdentityWithPadding());
870 EXPECT_TRUE(Id3->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000871 EXPECT_FALSE(Id3->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000872 delete Id3;
873
874 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000875 ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000876 ConstantVector::get({C4, C1, C6}));
877 EXPECT_FALSE(Id4->isIdentity());
878 EXPECT_FALSE(Id4->isIdentityWithPadding());
879 EXPECT_FALSE(Id4->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000880 EXPECT_FALSE(Id4->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000881 delete Id4;
882
883 // Result has more elements than operands, and extra elements are undef.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000884 ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000885 ConstantVector::get({CU, C1, C2, C3, CU, CU}));
886 EXPECT_FALSE(Id5->isIdentity());
887 EXPECT_TRUE(Id5->isIdentityWithPadding());
888 EXPECT_FALSE(Id5->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000889 EXPECT_FALSE(Id5->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000890 delete Id5;
891
892 // Result has more elements than operands, and extra elements are undef; choose from Op1.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000893 ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000894 ConstantVector::get({C4, C5, C6, CU, CU, CU}));
895 EXPECT_FALSE(Id6->isIdentity());
896 EXPECT_TRUE(Id6->isIdentityWithPadding());
897 EXPECT_FALSE(Id6->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000898 EXPECT_FALSE(Id6->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000899 delete Id6;
900
901 // Result has more elements than operands, but extra elements are not undef.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000902 ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000903 ConstantVector::get({C0, C1, C2, C3, CU, C1}));
904 EXPECT_FALSE(Id7->isIdentity());
905 EXPECT_FALSE(Id7->isIdentityWithPadding());
906 EXPECT_FALSE(Id7->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000907 EXPECT_FALSE(Id7->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000908 delete Id7;
909
910 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
Sanjay Patel0ff51d82018-09-20 14:36:09 +0000911 ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
Sanjay Patelac619a02018-08-30 15:05:38 +0000912 ConstantVector::get({C4, CU, C2, C3, CU, CU}));
913 EXPECT_FALSE(Id8->isIdentity());
914 EXPECT_FALSE(Id8->isIdentityWithPadding());
915 EXPECT_FALSE(Id8->isIdentityWithExtract());
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000916 EXPECT_FALSE(Id8->isConcat());
Sanjay Patelac619a02018-08-30 15:05:38 +0000917 delete Id8;
Sanjay Patelfd4976b2018-09-20 15:21:52 +0000918
919 // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat.
920 ShuffleVectorInst *Id9 = new ShuffleVectorInst(V0, V1,
921 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
922 EXPECT_FALSE(Id9->isIdentity());
923 EXPECT_FALSE(Id9->isIdentityWithPadding());
924 EXPECT_FALSE(Id9->isIdentityWithExtract());
925 EXPECT_TRUE(Id9->isConcat());
926 delete Id9;
927
928 // Result has less than twice as many elements as operands, so not a concat.
929 ShuffleVectorInst *Id10 = new ShuffleVectorInst(V0, V1,
930 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6}));
931 EXPECT_FALSE(Id10->isIdentity());
932 EXPECT_FALSE(Id10->isIdentityWithPadding());
933 EXPECT_FALSE(Id10->isIdentityWithExtract());
934 EXPECT_FALSE(Id10->isConcat());
935 delete Id10;
936
937 // Result has more than twice as many elements as operands, so not a concat.
938 ShuffleVectorInst *Id11 = new ShuffleVectorInst(V0, V1,
939 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7, CU}));
940 EXPECT_FALSE(Id11->isIdentity());
941 EXPECT_FALSE(Id11->isIdentityWithPadding());
942 EXPECT_FALSE(Id11->isIdentityWithExtract());
943 EXPECT_FALSE(Id11->isConcat());
944 delete Id11;
945
946 // If an input is undef, it's not a concat.
947 // TODO: IdentityWithPadding should be true here even though the high mask values are not undef.
948 ShuffleVectorInst *Id12 = new ShuffleVectorInst(V0, ConstantVector::get({CU, CU, CU, CU}),
949 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
950 EXPECT_FALSE(Id12->isIdentity());
951 EXPECT_FALSE(Id12->isIdentityWithPadding());
952 EXPECT_FALSE(Id12->isIdentityWithExtract());
953 EXPECT_FALSE(Id12->isConcat());
954 delete Id12;
Sanjay Patel2ca33602018-06-19 18:44:00 +0000955}
956
Vedant Kumarf01827f2018-06-19 23:42:17 +0000957TEST(InstructionsTest, SkipDebug) {
958 LLVMContext C;
959 std::unique_ptr<Module> M = parseIR(C,
960 R"(
961 declare void @llvm.dbg.value(metadata, metadata, metadata)
962
963 define void @f() {
964 entry:
965 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
966 ret void
967 }
968
969 !llvm.dbg.cu = !{!0}
970 !llvm.module.flags = !{!3, !4}
971 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
972 !1 = !DIFile(filename: "t2.c", directory: "foo")
973 !2 = !{}
974 !3 = !{i32 2, !"Dwarf Version", i32 4}
975 !4 = !{i32 2, !"Debug Info Version", i32 3}
976 !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)
977 !9 = !DISubroutineType(types: !10)
978 !10 = !{null}
979 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
980 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
981 !13 = !DILocation(line: 2, column: 7, scope: !8)
982 )");
983 ASSERT_TRUE(M);
984 Function *F = cast<Function>(M->getNamedValue("f"));
985 BasicBlock &BB = F->front();
986
987 // The first non-debug instruction is the terminator.
988 auto *Term = BB.getTerminator();
989 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
Vedant Kumar1cb63dc2018-06-26 21:16:59 +0000990 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
Vedant Kumarf01827f2018-06-19 23:42:17 +0000991
992 // After the terminator, there are no non-debug instructions.
993 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
994}
995
Sanjoy Das719e7862019-03-05 01:15:08 +0000996TEST(InstructionsTest, PhiIsNotFPMathOperator) {
997 LLVMContext Context;
998 IRBuilder<> Builder(Context);
999 MDBuilder MDHelper(Context);
1000 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1001 EXPECT_FALSE(isa<FPMathOperator>(I));
1002 I->deleteValue();
1003}
1004
Joseph Tremouletbba70e42016-01-14 06:21:42 +00001005} // end anonymous namespace
1006} // end namespace llvm