blob: 4c1cf95b37f8c356e5e3f3081c342fe5fff68c7a [file] [log] [blame]
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +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
Misha Brukman91fb9ab2005-04-21 22:43:08 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Misha Brukman91fb9ab2005-04-21 22:43:08 +00008//
Chris Lattnerd7ff5782001-08-23 17:05:04 +00009// This file contains the actual instruction interpreter.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Interpreter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/Statistic.h"
16#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000019#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
Chris Lattner2528d632008-07-08 17:25:49 +000021#include "llvm/Support/CommandLine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000023#include "llvm/Support/ErrorHandling.h"
Reid Spencer7a9c62b2007-01-12 07:05:14 +000024#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000025#include "llvm/Support/raw_ostream.h"
Gabor Greifcb6832e2007-10-11 19:40:35 +000026#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000027#include <cmath>
Chris Lattnerf0788082003-11-25 20:44:56 +000028using namespace llvm;
Chris Lattnera0d7b082002-12-23 23:59:41 +000029
Chandler Carruthf58e3762014-04-22 03:04:17 +000030#define DEBUG_TYPE "interpreter"
31
Chris Lattnere712a5a2006-12-19 22:56:53 +000032STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner2528d632008-07-08 17:25:49 +000034static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35 cl::desc("make the interpreter print every volatile load and store"));
36
Chris Lattnerc62e2e52001-10-15 05:51:48 +000037//===----------------------------------------------------------------------===//
Reid Spencer54c6e842007-03-03 06:22:22 +000038// Various Helper Functions
Chris Lattner78244c42001-10-15 13:25:40 +000039//===----------------------------------------------------------------------===//
Chris Lattnerc8c6c032003-12-28 09:44:37 +000040
Chris Lattner78244c42001-10-15 13:25:40 +000041static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaekee278c222003-10-24 19:59:01 +000042 SF.Values[V] = Val;
Chris Lattner78244c42001-10-15 13:25:40 +000043}
44
Chris Lattner79baf912002-05-03 19:52:30 +000045//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +000046// Binary Instruction Implementations
47//===----------------------------------------------------------------------===//
48
49#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencer40015aa2007-03-06 03:09:31 +000050 case Type::TY##TyID: \
51 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
52 break
Chris Lattnerd7ff5782001-08-23 17:05:04 +000053
Dan Gohmana5b96452009-06-04 22:49:04 +000054static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +000055 GenericValue Src2, Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000056 switch (Ty->getTypeID()) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +000057 IMPLEMENT_BINARY_OPERATOR(+, Float);
58 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000059 default:
David Greene8121a812010-01-05 01:27:23 +000060 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +000061 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000062 }
Chris Lattnerd7ff5782001-08-23 17:05:04 +000063}
64
Dan Gohmana5b96452009-06-04 22:49:04 +000065static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +000066 GenericValue Src2, Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000067 switch (Ty->getTypeID()) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +000068 IMPLEMENT_BINARY_OPERATOR(-, Float);
69 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000070 default:
David Greene8121a812010-01-05 01:27:23 +000071 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +000072 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +000073 }
Chris Lattnerd7ff5782001-08-23 17:05:04 +000074}
75
Dan Gohmana5b96452009-06-04 22:49:04 +000076static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +000077 GenericValue Src2, Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000078 switch (Ty->getTypeID()) {
Chris Lattner0b00b312001-10-27 08:28:11 +000079 IMPLEMENT_BINARY_OPERATOR(*, Float);
80 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattner0b00b312001-10-27 08:28:11 +000081 default:
David Greene8121a812010-01-05 01:27:23 +000082 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +000083 llvm_unreachable(nullptr);
Chris Lattner0b00b312001-10-27 08:28:11 +000084 }
Chris Lattner0b00b312001-10-27 08:28:11 +000085}
86
Fangrui Songf78650a2018-07-30 19:41:25 +000087static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +000088 GenericValue Src2, Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000089 switch (Ty->getTypeID()) {
Chris Lattner0b00b312001-10-27 08:28:11 +000090 IMPLEMENT_BINARY_OPERATOR(/, Float);
91 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattner0b00b312001-10-27 08:28:11 +000092 default:
David Greene8121a812010-01-05 01:27:23 +000093 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +000094 llvm_unreachable(nullptr);
Chris Lattner5946b112001-10-30 20:27:31 +000095 }
Chris Lattner5946b112001-10-30 20:27:31 +000096}
97
Fangrui Songf78650a2018-07-30 19:41:25 +000098static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +000099 GenericValue Src2, Type *Ty) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000100 switch (Ty->getTypeID()) {
Chris Lattner5946b112001-10-30 20:27:31 +0000101 case Type::FloatTyID:
102 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103 break;
104 case Type::DoubleTyID:
105 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
106 break;
107 default:
David Greene8121a812010-01-05 01:27:23 +0000108 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000109 llvm_unreachable(nullptr);
Chris Lattner0b00b312001-10-27 08:28:11 +0000110 }
Chris Lattner0b00b312001-10-27 08:28:11 +0000111}
112
Reid Spencer40015aa2007-03-06 03:09:31 +0000113#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
114 case Type::IntegerTyID: \
115 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
116 break;
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000117
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000118#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
119 case Type::VectorTyID: { \
120 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
121 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
122 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
123 Dest.AggregateVal[_i].IntVal = APInt(1, \
124 Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
125 } break;
126
Chris Lattnered27f842003-04-23 19:55:35 +0000127// Handle pointers specially because they must be compared with only as much
128// width as the host has. We _do not_ want to be comparing 64 bit values when
129// running on a 32-bit target, otherwise the upper 32 bits might mess up
130// comparisons if they contain garbage.
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000131#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnered27f842003-04-23 19:55:35 +0000132 case Type::PointerTyID: \
Reid Spencer40015aa2007-03-06 03:09:31 +0000133 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
134 (void*)(intptr_t)Src2.PointerVal); \
135 break;
Chris Lattnered27f842003-04-23 19:55:35 +0000136
Reid Spencer266e42b2006-12-23 06:05:41 +0000137static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000138 Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000139 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000140 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000141 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000142 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000143 IMPLEMENT_POINTER_ICMP(==);
Reid Spencer266e42b2006-12-23 06:05:41 +0000144 default:
David Greene8121a812010-01-05 01:27:23 +0000145 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000146 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000147 }
148 return Dest;
149}
150
151static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000152 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000153 GenericValue Dest;
154 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000155 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000156 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000157 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000158 default:
David Greene8121a812010-01-05 01:27:23 +0000159 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000160 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000161 }
162 return Dest;
163}
164
165static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000166 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000167 GenericValue Dest;
168 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000169 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000170 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000171 IMPLEMENT_POINTER_ICMP(<);
Reid Spencer266e42b2006-12-23 06:05:41 +0000172 default:
David Greene8121a812010-01-05 01:27:23 +0000173 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000174 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000175 }
176 return Dest;
177}
178
179static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000180 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000181 GenericValue Dest;
182 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000183 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000184 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000185 IMPLEMENT_POINTER_ICMP(<);
Reid Spencer266e42b2006-12-23 06:05:41 +0000186 default:
David Greene8121a812010-01-05 01:27:23 +0000187 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000188 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000189 }
190 return Dest;
191}
192
193static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000194 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000195 GenericValue Dest;
196 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000197 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000198 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000199 IMPLEMENT_POINTER_ICMP(>);
Reid Spencer266e42b2006-12-23 06:05:41 +0000200 default:
David Greene8121a812010-01-05 01:27:23 +0000201 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000202 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000203 }
204 return Dest;
205}
206
207static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000208 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000209 GenericValue Dest;
210 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000211 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000212 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000213 IMPLEMENT_POINTER_ICMP(>);
Reid Spencer266e42b2006-12-23 06:05:41 +0000214 default:
David Greene8121a812010-01-05 01:27:23 +0000215 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000216 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000217 }
218 return Dest;
219}
220
221static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000222 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000223 GenericValue Dest;
224 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000225 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000226 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000227 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000228 default:
David Greene8121a812010-01-05 01:27:23 +0000229 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000230 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000231 }
232 return Dest;
233}
234
235static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000236 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000237 GenericValue Dest;
238 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000239 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000240 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000241 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000242 default:
David Greene8121a812010-01-05 01:27:23 +0000243 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000244 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000245 }
246 return Dest;
247}
248
249static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000250 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000251 GenericValue Dest;
252 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000253 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000254 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000255 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000256 default:
David Greene8121a812010-01-05 01:27:23 +0000257 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000258 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000259 }
260 return Dest;
261}
262
263static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000264 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000265 GenericValue Dest;
266 switch (Ty->getTypeID()) {
Reid Spencer40015aa2007-03-06 03:09:31 +0000267 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000268 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000269 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencer266e42b2006-12-23 06:05:41 +0000270 default:
David Greene8121a812010-01-05 01:27:23 +0000271 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000272 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000273 }
274 return Dest;
275}
276
Reid Spencer0d54e782006-12-31 05:51:36 +0000277void Interpreter::visitICmpInst(ICmpInst &I) {
278 ExecutionContext &SF = ECStack.back();
Chris Lattner229907c2011-07-18 04:54:35 +0000279 Type *Ty = I.getOperand(0)->getType();
Reid Spencer0d54e782006-12-31 05:51:36 +0000280 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
281 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
282 GenericValue R; // Result
Fangrui Songf78650a2018-07-30 19:41:25 +0000283
Reid Spencer0d54e782006-12-31 05:51:36 +0000284 switch (I.getPredicate()) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000285 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
286 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencer0d54e782006-12-31 05:51:36 +0000287 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
294 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
295 default:
David Greene8121a812010-01-05 01:27:23 +0000296 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
Craig Toppere73658d2014-04-28 04:05:08 +0000297 llvm_unreachable(nullptr);
Reid Spencer0d54e782006-12-31 05:51:36 +0000298 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000299
Reid Spencer0d54e782006-12-31 05:51:36 +0000300 SetValue(&I, R, SF);
301}
302
303#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencer40015aa2007-03-06 03:09:31 +0000304 case Type::TY##TyID: \
305 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
306 break
Reid Spencer266e42b2006-12-23 06:05:41 +0000307
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000308#define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
309 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
310 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
311 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
312 Dest.AggregateVal[_i].IntVal = APInt(1, \
313 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
314 break;
315
316#define IMPLEMENT_VECTOR_FCMP(OP) \
317 case Type::VectorTyID: \
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000318 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000319 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
320 } else { \
321 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
322 }
323
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000324static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000325 Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000326 GenericValue Dest;
327 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000328 IMPLEMENT_FCMP(==, Float);
329 IMPLEMENT_FCMP(==, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000330 IMPLEMENT_VECTOR_FCMP(==);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000331 default:
David Greene8121a812010-01-05 01:27:23 +0000332 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000333 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000334 }
335 return Dest;
336}
337
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000338#define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
339 if (TY->isFloatTy()) { \
340 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
341 Dest.IntVal = APInt(1,false); \
342 return Dest; \
343 } \
344 } else { \
345 if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
346 Dest.IntVal = APInt(1,false); \
347 return Dest; \
348 } \
349 }
350
351#define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
352 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
353 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
354 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
355 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
356 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
357 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
358 else { \
359 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
360 } \
361 }
362
363#define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
364 if (TY->isVectorTy()) { \
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000365 if (cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000366 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
367 } else { \
368 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
369 } \
370 } \
371
372
373
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000374static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000375 Type *Ty)
376{
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000377 GenericValue Dest;
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000378 // if input is scalar value and Src1 or Src2 is NaN return false
379 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
380 // if vector input detect NaNs and fill mask
381 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
382 GenericValue DestMask = Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000383 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000384 IMPLEMENT_FCMP(!=, Float);
385 IMPLEMENT_FCMP(!=, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000386 IMPLEMENT_VECTOR_FCMP(!=);
387 default:
388 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000389 llvm_unreachable(nullptr);
Nadav Rotem4e4d45e2013-04-12 22:02:26 +0000390 }
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000391 // in vector case mask out NaN elements
392 if (Ty->isVectorTy())
393 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
394 if (DestMask.AggregateVal[_i].IntVal == false)
395 Dest.AggregateVal[_i].IntVal = APInt(1,false);
396
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000397 return Dest;
398}
399
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000400static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000401 Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000402 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000403 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000404 IMPLEMENT_FCMP(<=, Float);
405 IMPLEMENT_FCMP(<=, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000406 IMPLEMENT_VECTOR_FCMP(<=);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000407 default:
David Greene8121a812010-01-05 01:27:23 +0000408 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000409 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000410 }
411 return Dest;
412}
413
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000414static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000415 Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000416 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000417 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000418 IMPLEMENT_FCMP(>=, Float);
419 IMPLEMENT_FCMP(>=, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000420 IMPLEMENT_VECTOR_FCMP(>=);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000421 default:
David Greene8121a812010-01-05 01:27:23 +0000422 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000423 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000424 }
425 return Dest;
426}
427
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000428static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000429 Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000430 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000431 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000432 IMPLEMENT_FCMP(<, Float);
433 IMPLEMENT_FCMP(<, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000434 IMPLEMENT_VECTOR_FCMP(<);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000435 default:
David Greene8121a812010-01-05 01:27:23 +0000436 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000437 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000438 }
439 return Dest;
440}
441
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000442static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000443 Type *Ty) {
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000444 GenericValue Dest;
Chris Lattner6b727592004-06-17 18:19:28 +0000445 switch (Ty->getTypeID()) {
Reid Spencer0d54e782006-12-31 05:51:36 +0000446 IMPLEMENT_FCMP(>, Float);
447 IMPLEMENT_FCMP(>, Double);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000448 IMPLEMENT_VECTOR_FCMP(>);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000449 default:
David Greene8121a812010-01-05 01:27:23 +0000450 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000451 llvm_unreachable(nullptr);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000452 }
453 return Dest;
454}
455
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000456#define IMPLEMENT_UNORDERED(TY, X,Y) \
Chris Lattnerfdd87902009-10-05 05:54:46 +0000457 if (TY->isFloatTy()) { \
Anton Korobeynikov035eaac2008-02-20 11:10:28 +0000458 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
459 Dest.IntVal = APInt(1,true); \
460 return Dest; \
461 } \
462 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
463 Dest.IntVal = APInt(1,true); \
464 return Dest; \
465 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000466
David Blaikie9f380a32015-03-16 18:06:57 +0000467#define IMPLEMENT_VECTOR_UNORDERED(TY, X, Y, FUNC) \
468 if (TY->isVectorTy()) { \
469 GenericValue DestMask = Dest; \
470 Dest = FUNC(Src1, Src2, Ty); \
471 for (size_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \
472 if (DestMask.AggregateVal[_i].IntVal == true) \
473 Dest.AggregateVal[_i].IntVal = APInt(1, true); \
474 return Dest; \
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000475 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000476
477static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000478 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000479 GenericValue Dest;
480 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000481 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
482 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000483 return executeFCMP_OEQ(Src1, Src2, Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000484
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000485}
486
487static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000488 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000489 GenericValue Dest;
490 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000491 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
492 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000493 return executeFCMP_ONE(Src1, Src2, Ty);
494}
495
496static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000497 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000498 GenericValue Dest;
499 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000500 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
501 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000502 return executeFCMP_OLE(Src1, Src2, Ty);
503}
504
505static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000506 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000507 GenericValue Dest;
508 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000509 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
510 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000511 return executeFCMP_OGE(Src1, Src2, Ty);
512}
513
514static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000515 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000516 GenericValue Dest;
517 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000518 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
519 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000520 return executeFCMP_OLT(Src1, Src2, Ty);
521}
522
523static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000524 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000525 GenericValue Dest;
526 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000527 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
528 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000529 return executeFCMP_OGT(Src1, Src2, Ty);
530}
531
532static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000533 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000534 GenericValue Dest;
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000535 if(Ty->isVectorTy()) {
536 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
537 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000538 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000539 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
540 Dest.AggregateVal[_i].IntVal = APInt(1,
541 ( (Src1.AggregateVal[_i].FloatVal ==
542 Src1.AggregateVal[_i].FloatVal) &&
543 (Src2.AggregateVal[_i].FloatVal ==
544 Src2.AggregateVal[_i].FloatVal)));
545 } else {
546 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
547 Dest.AggregateVal[_i].IntVal = APInt(1,
548 ( (Src1.AggregateVal[_i].DoubleVal ==
549 Src1.AggregateVal[_i].DoubleVal) &&
550 (Src2.AggregateVal[_i].DoubleVal ==
551 Src2.AggregateVal[_i].DoubleVal)));
552 }
553 } else if (Ty->isFloatTy())
Fangrui Songf78650a2018-07-30 19:41:25 +0000554 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
Reid Spencer40015aa2007-03-06 03:09:31 +0000555 Src2.FloatVal == Src2.FloatVal));
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000556 else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000557 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
Reid Spencer40015aa2007-03-06 03:09:31 +0000558 Src2.DoubleVal == Src2.DoubleVal));
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000559 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000560 return Dest;
561}
562
563static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
Chris Lattner229907c2011-07-18 04:54:35 +0000564 Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000565 GenericValue Dest;
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000566 if(Ty->isVectorTy()) {
567 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
568 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000569 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000570 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
571 Dest.AggregateVal[_i].IntVal = APInt(1,
572 ( (Src1.AggregateVal[_i].FloatVal !=
573 Src1.AggregateVal[_i].FloatVal) ||
574 (Src2.AggregateVal[_i].FloatVal !=
575 Src2.AggregateVal[_i].FloatVal)));
576 } else {
577 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
578 Dest.AggregateVal[_i].IntVal = APInt(1,
579 ( (Src1.AggregateVal[_i].DoubleVal !=
580 Src1.AggregateVal[_i].DoubleVal) ||
581 (Src2.AggregateVal[_i].DoubleVal !=
582 Src2.AggregateVal[_i].DoubleVal)));
583 }
584 } else if (Ty->isFloatTy())
Fangrui Songf78650a2018-07-30 19:41:25 +0000585 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
Reid Spencer40015aa2007-03-06 03:09:31 +0000586 Src2.FloatVal != Src2.FloatVal));
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000587 else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000588 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
Reid Spencer40015aa2007-03-06 03:09:31 +0000589 Src2.DoubleVal != Src2.DoubleVal));
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000590 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000591 return Dest;
592}
593
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000594static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
Craig Toppere3dcce92015-08-01 22:20:21 +0000595 Type *Ty, const bool val) {
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000596 GenericValue Dest;
597 if(Ty->isVectorTy()) {
598 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
599 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
600 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
601 Dest.AggregateVal[_i].IntVal = APInt(1,val);
602 } else {
603 Dest.IntVal = APInt(1, val);
604 }
605
606 return Dest;
607}
608
Reid Spencer266e42b2006-12-23 06:05:41 +0000609void Interpreter::visitFCmpInst(FCmpInst &I) {
610 ExecutionContext &SF = ECStack.back();
Chris Lattner229907c2011-07-18 04:54:35 +0000611 Type *Ty = I.getOperand(0)->getType();
Reid Spencer266e42b2006-12-23 06:05:41 +0000612 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
613 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
614 GenericValue R; // Result
Fangrui Songf78650a2018-07-30 19:41:25 +0000615
Reid Spencer266e42b2006-12-23 06:05:41 +0000616 switch (I.getPredicate()) {
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000617 default:
618 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
Craig Toppere73658d2014-04-28 04:05:08 +0000619 llvm_unreachable(nullptr);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000620 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000621 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000622 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000623 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000624 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000625 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
626 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
638 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000639 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000640
Reid Spencer266e42b2006-12-23 06:05:41 +0000641 SetValue(&I, R, SF);
642}
643
Fangrui Songf78650a2018-07-30 19:41:25 +0000644static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
Chris Lattner229907c2011-07-18 04:54:35 +0000645 GenericValue Src2, Type *Ty) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000646 GenericValue Result;
647 switch (predicate) {
648 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
649 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
650 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
651 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
652 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
653 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
654 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
655 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
656 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
657 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000658 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
659 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
660 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
661 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
662 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
663 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
664 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
665 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
666 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
667 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
668 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
669 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
670 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
671 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000672 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
673 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
Reid Spencer266e42b2006-12-23 06:05:41 +0000674 default:
David Greene8121a812010-01-05 01:27:23 +0000675 dbgs() << "Unhandled Cmp predicate\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000676 llvm_unreachable(nullptr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000677 }
678}
679
Chris Lattner185045c2003-05-10 21:22:39 +0000680void Interpreter::visitBinaryOperator(BinaryOperator &I) {
681 ExecutionContext &SF = ECStack.back();
Chris Lattner229907c2011-07-18 04:54:35 +0000682 Type *Ty = I.getOperand(0)->getType();
Chris Lattner7076ff22002-06-25 16:13:21 +0000683 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
684 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000685 GenericValue R; // Result
686
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000687 // First process vector operation
688 if (Ty->isVectorTy()) {
689 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
690 R.AggregateVal.resize(Src1.AggregateVal.size());
Nadav Rotem4e4d45e2013-04-12 22:02:26 +0000691
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000692 // Macros to execute binary operation 'OP' over integer vectors
693#define INTEGER_VECTOR_OPERATION(OP) \
694 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
695 R.AggregateVal[i].IntVal = \
696 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
697
698 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
699 // they have different notation.
700#define INTEGER_VECTOR_FUNCTION(OP) \
701 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
702 R.AggregateVal[i].IntVal = \
703 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
704
705 // Macros to execute binary operation 'OP' over floating point type TY
706 // (float or double) vectors
707#define FLOAT_VECTOR_FUNCTION(OP, TY) \
708 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
709 R.AggregateVal[i].TY = \
710 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
711
712 // Macros to choose appropriate TY: float or double and run operation
713 // execution
714#define FLOAT_VECTOR_OP(OP) { \
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000715 if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000716 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
717 else { \
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000718 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000719 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
720 else { \
721 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
722 llvm_unreachable(0); \
723 } \
724 } \
725}
726
727 switch(I.getOpcode()){
728 default:
729 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
Craig Toppere73658d2014-04-28 04:05:08 +0000730 llvm_unreachable(nullptr);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000731 break;
732 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
733 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
734 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
735 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
736 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
737 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
738 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
739 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
740 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
741 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
742 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
743 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
744 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
745 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
746 case Instruction::FRem:
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000747 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000748 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000749 R.AggregateVal[i].FloatVal =
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000750 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
751 else {
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000752 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000753 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
Fangrui Songf78650a2018-07-30 19:41:25 +0000754 R.AggregateVal[i].DoubleVal =
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000755 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
756 else {
757 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +0000758 llvm_unreachable(nullptr);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000759 }
760 }
761 break;
762 }
763 } else {
764 switch (I.getOpcode()) {
765 default:
766 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
Craig Toppere73658d2014-04-28 04:05:08 +0000767 llvm_unreachable(nullptr);
Nadav Rotembe0e89d2013-04-26 20:19:41 +0000768 break;
769 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
770 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
771 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
772 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
773 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
774 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
775 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
776 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
777 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
778 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
779 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
780 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
781 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
782 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
783 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
784 }
785 }
Chris Lattner7076ff22002-06-25 16:13:21 +0000786 SetValue(&I, R, SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000787}
788
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000789static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Craig Toppere3dcce92015-08-01 22:20:21 +0000790 GenericValue Src3, Type *Ty) {
Elena Demikhovsky843657c2013-09-02 06:40:09 +0000791 GenericValue Dest;
792 if(Ty->isVectorTy()) {
793 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
794 assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
795 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
796 for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
797 Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
798 Src3.AggregateVal[i] : Src2.AggregateVal[i];
799 } else {
800 Dest = (Src1.IntVal == 0) ? Src3 : Src2;
801 }
802 return Dest;
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000803}
804
805void Interpreter::visitSelectInst(SelectInst &I) {
806 ExecutionContext &SF = ECStack.back();
Craig Toppere3dcce92015-08-01 22:20:21 +0000807 Type * Ty = I.getOperand(0)->getType();
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000808 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
809 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
810 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Elena Demikhovsky843657c2013-09-02 06:40:09 +0000811 GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
Chris Lattner2b2d7a92004-04-20 16:43:21 +0000812 SetValue(&I, R, SF);
813}
814
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000815//===----------------------------------------------------------------------===//
816// Terminator Instruction Implementations
817//===----------------------------------------------------------------------===//
818
Chris Lattner15157b82001-10-27 04:15:57 +0000819void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeke51f10072004-02-13 05:48:00 +0000820 // runAtExitHandlers() assumes there are no stack frames, but
821 // if exit() was called, then it had a stack frame. Blow away
822 // the stack before interpreting atexit handlers.
Chris Lattner0c778f72009-10-29 05:26:09 +0000823 ECStack.clear();
824 runAtExitHandlers();
825 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattner15157b82001-10-27 04:15:57 +0000826}
827
Brian Gaeke65cac902003-11-07 05:22:49 +0000828/// Pop the last stack frame off of ECStack and then copy the result
829/// back into the result variable if we are not returning void. The
Jeff Cohen24396692006-02-07 05:29:44 +0000830/// result variable may be the ExitValue, or the Value of the calling
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000831/// CallInst if there was a previous stack frame. This method may
832/// invalidate any ECStack iterators you have. This method also takes
833/// care of switching to the normal destination BB, if we are returning
834/// from an invoke.
Brian Gaeke65cac902003-11-07 05:22:49 +0000835///
Chris Lattner229907c2011-07-18 04:54:35 +0000836void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
Chris Lattner0c778f72009-10-29 05:26:09 +0000837 GenericValue Result) {
Brian Gaeke65cac902003-11-07 05:22:49 +0000838 // Pop the current stack frame.
839 ECStack.pop_back();
840
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000841 if (ECStack.empty()) { // Finished main. Put result into exit code...
Dan Gohmanc61056a2010-06-18 02:01:10 +0000842 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
Jeff Cohen24396692006-02-07 05:29:44 +0000843 ExitValue = Result; // Capture the exit value of the program
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000844 } else {
Reid Spencerff38cf82007-06-01 22:23:29 +0000845 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000846 }
847 } else {
848 // If we have a previous stack frame, and we have a previous call,
849 // fill in the return value...
Brian Gaeke65cac902003-11-07 05:22:49 +0000850 ExecutionContext &CallingSF = ECStack.back();
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000851 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000852 // Save result...
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000853 if (!CallingSF.Caller.getType()->isVoidTy())
Brian Gaeke85baf8c2003-11-07 20:44:58 +0000854 SetValue(I, Result, CallingSF);
855 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
856 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke18b59572003-11-07 19:26:23 +0000857 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaeke65cac902003-11-07 05:22:49 +0000858 }
859 }
860}
861
Chris Lattner185045c2003-05-10 21:22:39 +0000862void Interpreter::visitReturnInst(ReturnInst &I) {
863 ExecutionContext &SF = ECStack.back();
Chris Lattner229907c2011-07-18 04:54:35 +0000864 Type *RetTy = Type::getVoidTy(I.getContext());
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000865 GenericValue Result;
866
867 // Save away the return value... (if we are not 'ret void')
Chris Lattner7076ff22002-06-25 16:13:21 +0000868 if (I.getNumOperands()) {
869 RetTy = I.getReturnValue()->getType();
870 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000871 }
872
Brian Gaeke65cac902003-11-07 05:22:49 +0000873 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000874}
875
Chris Lattner98e54142004-10-16 18:21:33 +0000876void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner2104b8d2010-04-07 22:58:41 +0000877 report_fatal_error("Program executed an 'unreachable' instruction!");
Chris Lattner98e54142004-10-16 18:21:33 +0000878}
879
Chris Lattner185045c2003-05-10 21:22:39 +0000880void Interpreter::visitBranchInst(BranchInst &I) {
881 ExecutionContext &SF = ECStack.back();
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000882 BasicBlock *Dest;
883
Chris Lattner7076ff22002-06-25 16:13:21 +0000884 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
885 if (!I.isUnconditional()) {
886 Value *Cond = I.getCondition();
Reid Spencer40015aa2007-03-06 03:09:31 +0000887 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000888 Dest = I.getSuccessor(1);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000889 }
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000890 SwitchToNewBasicBlock(Dest, SF);
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000891}
892
Chris Lattner185045c2003-05-10 21:22:39 +0000893void Interpreter::visitSwitchInst(SwitchInst &I) {
894 ExecutionContext &SF = ECStack.back();
Eli Friedman95031ed2011-09-29 20:21:17 +0000895 Value* Cond = I.getCondition();
896 Type *ElTy = Cond->getType();
897 GenericValue CondVal = getOperandValue(Cond, SF);
Chris Lattnerd215af02003-04-22 20:34:47 +0000898
899 // Check to see if any of the cases match...
Craig Topper353eda42014-04-24 06:44:33 +0000900 BasicBlock *Dest = nullptr;
Chandler Carruth927d8e62017-04-12 07:27:28 +0000901 for (auto Case : I.cases()) {
902 GenericValue CaseVal = getOperandValue(Case.getCaseValue(), SF);
Bob Wilsone4077362013-09-09 19:14:35 +0000903 if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
Chandler Carruth927d8e62017-04-12 07:27:28 +0000904 Dest = cast<BasicBlock>(Case.getCaseSuccessor());
Bob Wilsone4077362013-09-09 19:14:35 +0000905 break;
Chris Lattnerd215af02003-04-22 20:34:47 +0000906 }
Eli Friedman95031ed2011-09-29 20:21:17 +0000907 }
Chris Lattnerd215af02003-04-22 20:34:47 +0000908 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000909 SwitchToNewBasicBlock(Dest, SF);
910}
911
Chris Lattner0c778f72009-10-29 05:26:09 +0000912void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
913 ExecutionContext &SF = ECStack.back();
914 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
915 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
916}
917
918
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000919// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
920// This function handles the actual updating of block and instruction iterators
921// as well as execution of all of the PHI nodes in the destination block.
922//
923// This method does this because all of the PHI nodes must be executed
924// atomically, reading their inputs before any of the results are updated. Not
925// doing this can cause problems if the PHI nodes depend on other PHI nodes for
926// their inputs. If the input PHI node is updated before it is read, incorrect
927// results can happen. Thus we use a two phase approach.
928//
929void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
930 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
931 SF.CurBB = Dest; // Update CurBB to branch destination
932 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
933
934 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
935
936 // Loop over all of the PHI nodes in the current block, reading their inputs.
937 std::vector<GenericValue> ResultValues;
938
939 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
940 // Search for the value corresponding to this previous bb...
941 int i = PN->getBasicBlockIndex(PrevBB);
942 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
943 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000944
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000945 // Save the incoming value for this PHI node...
946 ResultValues.push_back(getOperandValue(IncomingValue, SF));
947 }
948
949 // Now loop over all of the PHI nodes setting their values...
950 SF.CurInst = SF.CurBB->begin();
Reid Spencer66149462004-09-15 17:06:42 +0000951 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
952 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattnerbd6771c2003-05-10 20:21:16 +0000953 SetValue(PN, ResultValues[i], SF);
Reid Spencer66149462004-09-15 17:06:42 +0000954 }
Chris Lattnerd215af02003-04-22 20:34:47 +0000955}
956
Chris Lattnerd7ff5782001-08-23 17:05:04 +0000957//===----------------------------------------------------------------------===//
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000958// Memory Instruction Implementations
959//===----------------------------------------------------------------------===//
960
Victor Hernandez8acf2952009-10-23 21:09:37 +0000961void Interpreter::visitAllocaInst(AllocaInst &I) {
Chris Lattner185045c2003-05-10 21:22:39 +0000962 ExecutionContext &SF = ECStack.back();
963
Chris Lattner229907c2011-07-18 04:54:35 +0000964 Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000965
Chris Lattner0ebb7932002-04-28 21:57:33 +0000966 // Get the number of elements being allocated by the array...
Fangrui Songf78650a2018-07-30 19:41:25 +0000967 unsigned NumElements =
Reid Spencer40015aa2007-03-06 03:09:31 +0000968 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
969
Mehdi Aminia3fcefb2015-07-16 16:34:23 +0000970 unsigned TypeSize = (size_t)getDataLayout().getTypeAllocSize(Ty);
Reid Spencer40015aa2007-03-06 03:09:31 +0000971
Gabor Greifcb6832e2007-10-11 19:40:35 +0000972 // Avoid malloc-ing zero bytes, use max()...
973 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000974
975 // Allocate enough memory to hold the type...
Serge Pavlov76d8cce2018-02-20 05:41:26 +0000976 void *Memory = safe_malloc(MemToAlloc);
Reid Spencer40015aa2007-03-06 03:09:31 +0000977
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000978 LLVM_DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize
979 << " bytes) x " << NumElements << " (Total: " << MemToAlloc
980 << ") at " << uintptr_t(Memory) << '\n');
Chris Lattnerfb55ba02002-02-19 18:50:09 +0000981
Chris Lattnera0d7b082002-12-23 23:59:41 +0000982 GenericValue Result = PTOGV(Memory);
Craig Toppere73658d2014-04-28 04:05:08 +0000983 assert(Result.PointerVal && "Null pointer returned by malloc!");
Chris Lattner7076ff22002-06-25 16:13:21 +0000984 SetValue(&I, Result, SF);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000985
Chris Lattner7076ff22002-06-25 16:13:21 +0000986 if (I.getOpcode() == Instruction::Alloca)
Chris Lattnerfb55ba02002-02-19 18:50:09 +0000987 ECStack.back().Allocas.add(Memory);
Chris Lattner2c1a98e2001-08-27 05:16:50 +0000988}
989
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000990// getElementOffset - The workhorse for getelementptr.
Chris Lattner05fbeed2001-10-29 19:32:19 +0000991//
Chris Lattnerf0788082003-11-25 20:44:56 +0000992GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukman91fb9ab2005-04-21 22:43:08 +0000993 gep_type_iterator E,
994 ExecutionContext &SF) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000995 assert(Ptr->getType()->isPointerTy() &&
Chris Lattner05fbeed2001-10-29 19:32:19 +0000996 "Cannot getElementOffset of a nonpointer type!");
997
Reid Spencer40015aa2007-03-06 03:09:31 +0000998 uint64_t Total = 0;
Chris Lattnerc837dbc2002-08-27 22:33:45 +0000999
1000 for (; I != E; ++I) {
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001001 if (StructType *STy = I.getStructTypeOrNull()) {
Mehdi Aminia3fcefb2015-07-16 16:34:23 +00001002 const StructLayout *SLO = getDataLayout().getStructLayout(STy);
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001003
Reid Spencere0fc4df2006-10-20 07:07:24 +00001004 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1005 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001006
Reid Spencer40015aa2007-03-06 03:09:31 +00001007 Total += SLO->getElementOffset(Index);
Chris Lattnerf0788082003-11-25 20:44:56 +00001008 } else {
Chris Lattner13b3e5b2003-02-25 21:14:59 +00001009 // Get the index number for the array... which must be long type...
Chris Lattnerf0788082003-11-25 20:44:56 +00001010 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1011
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001012 int64_t Idx;
Fangrui Songf78650a2018-07-30 19:41:25 +00001013 unsigned BitWidth =
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001014 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencerfab44b62007-01-18 01:25:42 +00001015 if (BitWidth == 32)
Reid Spencer40015aa2007-03-06 03:09:31 +00001016 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattner982e8502008-04-06 21:50:58 +00001017 else {
1018 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencer40015aa2007-03-06 03:09:31 +00001019 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattner982e8502008-04-06 21:50:58 +00001020 }
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001021 Total += getDataLayout().getTypeAllocSize(I.getIndexedType()) * Idx;
Brian Gaekee278c222003-10-24 19:59:01 +00001022 }
Chris Lattner05fbeed2001-10-29 19:32:19 +00001023 }
1024
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001025 GenericValue Result;
Reid Spencer40015aa2007-03-06 03:09:31 +00001026 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001027 LLVM_DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001028 return Result;
Chris Lattner05fbeed2001-10-29 19:32:19 +00001029}
1030
Chris Lattner185045c2003-05-10 21:22:39 +00001031void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1032 ExecutionContext &SF = ECStack.back();
Owen Anderson455df542009-06-26 16:46:15 +00001033 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattnerf0788082003-11-25 20:44:56 +00001034 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner05fbeed2001-10-29 19:32:19 +00001035}
1036
Chris Lattner185045c2003-05-10 21:22:39 +00001037void Interpreter::visitLoadInst(LoadInst &I) {
1038 ExecutionContext &SF = ECStack.back();
Chris Lattner7076ff22002-06-25 16:13:21 +00001039 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnera0d7b082002-12-23 23:59:41 +00001040 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001041 GenericValue Result;
Reid Spencerec8c51c2007-03-03 08:38:04 +00001042 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner7076ff22002-06-25 16:13:21 +00001043 SetValue(&I, Result, SF);
Chris Lattner2528d632008-07-08 17:25:49 +00001044 if (I.isVolatile() && PrintVolatile)
David Greene8121a812010-01-05 01:27:23 +00001045 dbgs() << "Volatile load " << I;
Chris Lattner2c1a98e2001-08-27 05:16:50 +00001046}
1047
Chris Lattner185045c2003-05-10 21:22:39 +00001048void Interpreter::visitStoreInst(StoreInst &I) {
1049 ExecutionContext &SF = ECStack.back();
Chris Lattner6a1a65f2002-10-15 20:34:05 +00001050 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1051 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnera0d7b082002-12-23 23:59:41 +00001052 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner7fe1f7c2002-10-26 01:57:15 +00001053 I.getOperand(0)->getType());
Chris Lattner2528d632008-07-08 17:25:49 +00001054 if (I.isVolatile() && PrintVolatile)
David Greene8121a812010-01-05 01:27:23 +00001055 dbgs() << "Volatile store: " << I;
Chris Lattner6a1a65f2002-10-15 20:34:05 +00001056}
1057
Chris Lattner2c1a98e2001-08-27 05:16:50 +00001058//===----------------------------------------------------------------------===//
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001059// Miscellaneous Instruction Implementations
1060//===----------------------------------------------------------------------===//
1061
Brian Gaekea6454d352003-11-07 20:04:22 +00001062void Interpreter::visitCallSite(CallSite CS) {
Chris Lattner185045c2003-05-10 21:22:39 +00001063 ExecutionContext &SF = ECStack.back();
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001064
1065 // Check to see if this is an intrinsic function call...
Reid Spencerc57be6c2007-04-16 21:50:40 +00001066 Function *F = CS.getCalledFunction();
Chris Lattner0c778f72009-10-29 05:26:09 +00001067 if (F && F->isDeclaration())
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001068 switch (F->getIntrinsicID()) {
Brian Gaeke1c0133f2004-01-14 06:02:53 +00001069 case Intrinsic::not_intrinsic:
1070 break;
Chris Lattner071a5e52004-03-13 00:24:00 +00001071 case Intrinsic::vastart: { // va_start
Brian Gaeke7b4be132004-02-25 23:01:48 +00001072 GenericValue ArgIndex;
1073 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1074 ArgIndex.UIntPairVal.second = 0;
1075 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001076 return;
Brian Gaeke7b4be132004-02-25 23:01:48 +00001077 }
Chris Lattner071a5e52004-03-13 00:24:00 +00001078 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001079 return;
Chris Lattner071a5e52004-03-13 00:24:00 +00001080 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001081 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1082 return;
1083 default:
Brian Gaeke1c0133f2004-01-14 06:02:53 +00001084 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001085 // class to transform it into hopefully tasty LLVM code.
1086 //
Chris Lattnerd9b75112007-04-17 17:38:28 +00001087 BasicBlock::iterator me(CS.getInstruction());
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001088 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerd9b75112007-04-17 17:38:28 +00001089 bool atBegin(Parent->begin() == me);
1090 if (!atBegin)
1091 --me;
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001092 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1093
1094 // Restore the CurInst pointer to the first instruction newly inserted, if
1095 // any.
Chris Lattnerd9b75112007-04-17 17:38:28 +00001096 if (atBegin) {
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001097 SF.CurInst = Parent->begin();
1098 } else {
Chris Lattnerd9b75112007-04-17 17:38:28 +00001099 SF.CurInst = me;
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001100 ++SF.CurInst;
1101 }
Brian Gaekead373c82004-04-23 18:05:28 +00001102 return;
Chris Lattnerc8c6c032003-12-28 09:44:37 +00001103 }
1104
Reid Spencerc57be6c2007-04-16 21:50:40 +00001105
Brian Gaekea6454d352003-11-07 20:04:22 +00001106 SF.Caller = CS;
Chris Lattneraee56b82003-04-22 21:22:33 +00001107 std::vector<GenericValue> ArgVals;
Brian Gaekea6d48e42003-11-07 19:59:08 +00001108 const unsigned NumArgs = SF.Caller.arg_size();
1109 ArgVals.reserve(NumArgs);
Reid Spencerc57be6c2007-04-16 21:50:40 +00001110 uint16_t pNum = 1;
Brian Gaekea6d48e42003-11-07 19:59:08 +00001111 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencerc57be6c2007-04-16 21:50:40 +00001112 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekea6d48e42003-11-07 19:59:08 +00001113 Value *V = *i;
1114 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner4e7aa442003-01-13 00:58:52 +00001115 }
Chris Lattner676d4112001-09-10 04:49:44 +00001116
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001117 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner62b7fd12002-04-07 20:49:59 +00001118 // and treat it as a function pointer.
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001119 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattner470754e2003-05-08 16:18:31 +00001120 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001121}
1122
Alp Tokercb402912014-01-24 17:20:08 +00001123// auxiliary function for shift operations
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001124static unsigned getShiftAmount(uint64_t orgShiftAmount,
1125 llvm::APInt valueToShift) {
1126 unsigned valueWidth = valueToShift.getBitWidth();
1127 if (orgShiftAmount < (uint64_t)valueWidth)
1128 return orgShiftAmount;
1129 // according to the llvm documentation, if orgShiftAmount > valueWidth,
1130 // the result is undfeined. but we do shift by this rule:
1131 return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;
1132}
1133
1134
Reid Spencer2341c222007-02-02 02:16:23 +00001135void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +00001136 ExecutionContext &SF = ECStack.back();
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +00001137 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1138 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1139 GenericValue Dest;
Craig Toppere3dcce92015-08-01 22:20:21 +00001140 Type *Ty = I.getType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001141
1142 if (Ty->isVectorTy()) {
1143 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1144 assert(src1Size == Src2.AggregateVal.size());
1145 for (unsigned i = 0; i < src1Size; i++) {
1146 GenericValue Result;
1147 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1148 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1149 Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1150 Dest.AggregateVal.push_back(Result);
1151 }
1152 } else {
1153 // scalar
1154 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1155 llvm::APInt valueToShift = Src1.IntVal;
1156 Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1157 }
1158
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +00001159 SetValue(&I, Dest, SF);
1160}
1161
Reid Spencer2341c222007-02-02 02:16:23 +00001162void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +00001163 ExecutionContext &SF = ECStack.back();
Brian Gaeke5a8ec7d2003-12-11 00:22:59 +00001164 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1165 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1166 GenericValue Dest;
Craig Toppere3dcce92015-08-01 22:20:21 +00001167 Type *Ty = I.getType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001168
1169 if (Ty->isVectorTy()) {
1170 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1171 assert(src1Size == Src2.AggregateVal.size());
1172 for (unsigned i = 0; i < src1Size; i++) {
1173 GenericValue Result;
1174 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1175 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1176 Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1177 Dest.AggregateVal.push_back(Result);
1178 }
1179 } else {
1180 // scalar
1181 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1182 llvm::APInt valueToShift = Src1.IntVal;
1183 Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1184 }
1185
Reid Spencerfdff9382006-11-08 06:47:33 +00001186 SetValue(&I, Dest, SF);
1187}
1188
Reid Spencer2341c222007-02-02 02:16:23 +00001189void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencerfdff9382006-11-08 06:47:33 +00001190 ExecutionContext &SF = ECStack.back();
Reid Spencerfdff9382006-11-08 06:47:33 +00001191 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1192 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner762edbc2009-01-16 20:17:02 +00001193 GenericValue Dest;
Craig Toppere3dcce92015-08-01 22:20:21 +00001194 Type *Ty = I.getType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001195
1196 if (Ty->isVectorTy()) {
1197 size_t src1Size = Src1.AggregateVal.size();
1198 assert(src1Size == Src2.AggregateVal.size());
1199 for (unsigned i = 0; i < src1Size; i++) {
1200 GenericValue Result;
1201 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1202 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1203 Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1204 Dest.AggregateVal.push_back(Result);
1205 }
1206 } else {
1207 // scalar
1208 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1209 llvm::APInt valueToShift = Src1.IntVal;
1210 Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1211 }
1212
Chris Lattner7076ff22002-06-25 16:13:21 +00001213 SetValue(&I, Dest, SF);
Chris Lattner2c1a98e2001-08-27 05:16:50 +00001214}
1215
Chris Lattner229907c2011-07-18 04:54:35 +00001216GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001217 ExecutionContext &SF) {
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001218 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001219 Type *SrcTy = SrcVal->getType();
1220 if (SrcTy->isVectorTy()) {
1221 Type *DstVecTy = DstTy->getScalarType();
1222 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1223 unsigned NumElts = Src.AggregateVal.size();
1224 // the sizes of src and dst vectors must be equal
1225 Dest.AggregateVal.resize(NumElts);
1226 for (unsigned i = 0; i < NumElts; i++)
1227 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);
1228 } else {
1229 IntegerType *DITy = cast<IntegerType>(DstTy);
1230 unsigned DBitWidth = DITy->getBitWidth();
1231 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1232 }
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001233 return Dest;
Chris Lattner2c1a98e2001-08-27 05:16:50 +00001234}
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001235
Chris Lattner229907c2011-07-18 04:54:35 +00001236GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001237 ExecutionContext &SF) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001238 Type *SrcTy = SrcVal->getType();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001239 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001240 if (SrcTy->isVectorTy()) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001241 Type *DstVecTy = DstTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001242 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1243 unsigned size = Src.AggregateVal.size();
1244 // the sizes of src and dst vectors must be equal.
1245 Dest.AggregateVal.resize(size);
1246 for (unsigned i = 0; i < size; i++)
1247 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);
1248 } else {
Craig Toppere3dcce92015-08-01 22:20:21 +00001249 auto *DITy = cast<IntegerType>(DstTy);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001250 unsigned DBitWidth = DITy->getBitWidth();
1251 Dest.IntVal = Src.IntVal.sext(DBitWidth);
1252 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001253 return Dest;
1254}
1255
Chris Lattner229907c2011-07-18 04:54:35 +00001256GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001257 ExecutionContext &SF) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001258 Type *SrcTy = SrcVal->getType();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001259 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001260 if (SrcTy->isVectorTy()) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001261 Type *DstVecTy = DstTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001262 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1263
1264 unsigned size = Src.AggregateVal.size();
1265 // the sizes of src and dst vectors must be equal.
1266 Dest.AggregateVal.resize(size);
1267 for (unsigned i = 0; i < size; i++)
1268 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);
1269 } else {
Craig Toppere3dcce92015-08-01 22:20:21 +00001270 auto *DITy = cast<IntegerType>(DstTy);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001271 unsigned DBitWidth = DITy->getBitWidth();
1272 Dest.IntVal = Src.IntVal.zext(DBitWidth);
1273 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001274 return Dest;
1275}
1276
Chris Lattner229907c2011-07-18 04:54:35 +00001277GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001278 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001279 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001280
1281 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1282 assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1283 DstTy->getScalarType()->isFloatTy() &&
1284 "Invalid FPTrunc instruction");
1285
1286 unsigned size = Src.AggregateVal.size();
1287 // the sizes of src and dst vectors must be equal.
1288 Dest.AggregateVal.resize(size);
1289 for (unsigned i = 0; i < size; i++)
1290 Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1291 } else {
1292 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1293 "Invalid FPTrunc instruction");
1294 Dest.FloatVal = (float)Src.DoubleVal;
1295 }
1296
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001297 return Dest;
1298}
1299
Chris Lattner229907c2011-07-18 04:54:35 +00001300GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001301 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001302 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001303
1304 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1305 assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1306 DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1307
1308 unsigned size = Src.AggregateVal.size();
1309 // the sizes of src and dst vectors must be equal.
1310 Dest.AggregateVal.resize(size);
1311 for (unsigned i = 0; i < size; i++)
1312 Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1313 } else {
1314 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1315 "Invalid FPExt instruction");
1316 Dest.DoubleVal = (double)Src.FloatVal;
1317 }
1318
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001319 return Dest;
1320}
1321
Chris Lattner229907c2011-07-18 04:54:35 +00001322GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001323 ExecutionContext &SF) {
Chris Lattner229907c2011-07-18 04:54:35 +00001324 Type *SrcTy = SrcVal->getType();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001325 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001326
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001327 if (SrcTy->getTypeID() == Type::VectorTyID) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001328 Type *DstVecTy = DstTy->getScalarType();
1329 Type *SrcVecTy = SrcTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001330 uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1331 unsigned size = Src.AggregateVal.size();
1332 // the sizes of src and dst vectors must be equal.
1333 Dest.AggregateVal.resize(size);
1334
1335 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1336 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1337 for (unsigned i = 0; i < size; i++)
1338 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1339 Src.AggregateVal[i].FloatVal, DBitWidth);
1340 } else {
1341 for (unsigned i = 0; i < size; i++)
1342 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1343 Src.AggregateVal[i].DoubleVal, DBitWidth);
1344 }
1345 } else {
1346 // scalar
1347 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1348 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1349
1350 if (SrcTy->getTypeID() == Type::FloatTyID)
1351 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1352 else {
1353 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1354 }
1355 }
1356
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001357 return Dest;
1358}
1359
Chris Lattner229907c2011-07-18 04:54:35 +00001360GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001361 ExecutionContext &SF) {
Chris Lattner229907c2011-07-18 04:54:35 +00001362 Type *SrcTy = SrcVal->getType();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001363 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001364
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001365 if (SrcTy->getTypeID() == Type::VectorTyID) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001366 Type *DstVecTy = DstTy->getScalarType();
1367 Type *SrcVecTy = SrcTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001368 uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1369 unsigned size = Src.AggregateVal.size();
1370 // the sizes of src and dst vectors must be equal
1371 Dest.AggregateVal.resize(size);
1372
1373 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1374 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1375 for (unsigned i = 0; i < size; i++)
1376 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1377 Src.AggregateVal[i].FloatVal, DBitWidth);
1378 } else {
1379 for (unsigned i = 0; i < size; i++)
1380 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1381 Src.AggregateVal[i].DoubleVal, DBitWidth);
1382 }
1383 } else {
1384 // scalar
1385 unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1386 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1387
1388 if (SrcTy->getTypeID() == Type::FloatTyID)
1389 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1390 else {
1391 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1392 }
1393 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001394 return Dest;
1395}
1396
Chris Lattner229907c2011-07-18 04:54:35 +00001397GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001398 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001399 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001400
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001401 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001402 Type *DstVecTy = DstTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001403 unsigned size = Src.AggregateVal.size();
1404 // the sizes of src and dst vectors must be equal
1405 Dest.AggregateVal.resize(size);
1406
1407 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1408 assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1409 for (unsigned i = 0; i < size; i++)
1410 Dest.AggregateVal[i].FloatVal =
1411 APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal);
1412 } else {
1413 for (unsigned i = 0; i < size; i++)
1414 Dest.AggregateVal[i].DoubleVal =
1415 APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal);
1416 }
1417 } else {
1418 // scalar
1419 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1420 if (DstTy->getTypeID() == Type::FloatTyID)
1421 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1422 else {
1423 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1424 }
1425 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001426 return Dest;
1427}
1428
Chris Lattner229907c2011-07-18 04:54:35 +00001429GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001430 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001431 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001432
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001433 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
Craig Toppere3dcce92015-08-01 22:20:21 +00001434 Type *DstVecTy = DstTy->getScalarType();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001435 unsigned size = Src.AggregateVal.size();
1436 // the sizes of src and dst vectors must be equal
1437 Dest.AggregateVal.resize(size);
1438
1439 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1440 assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1441 for (unsigned i = 0; i < size; i++)
1442 Dest.AggregateVal[i].FloatVal =
1443 APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal);
1444 } else {
1445 for (unsigned i = 0; i < size; i++)
1446 Dest.AggregateVal[i].DoubleVal =
1447 APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal);
1448 }
1449 } else {
1450 // scalar
1451 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1452
1453 if (DstTy->getTypeID() == Type::FloatTyID)
1454 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1455 else {
1456 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1457 }
1458 }
1459
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001460 return Dest;
1461}
1462
Chris Lattner229907c2011-07-18 04:54:35 +00001463GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001464 ExecutionContext &SF) {
Reid Spencer40015aa2007-03-06 03:09:31 +00001465 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001466 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands19d0b472010-02-16 11:11:14 +00001467 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001468
Reid Spencer40015aa2007-03-06 03:09:31 +00001469 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001470 return Dest;
1471}
1472
Chris Lattner229907c2011-07-18 04:54:35 +00001473GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001474 ExecutionContext &SF) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001475 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands19d0b472010-02-16 11:11:14 +00001476 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencerec8c51c2007-03-03 08:38:04 +00001477
Mehdi Aminia3fcefb2015-07-16 16:34:23 +00001478 uint32_t PtrSize = getDataLayout().getPointerSizeInBits();
Reid Spencer10dbc1e2007-03-06 03:41:50 +00001479 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer1efc4aa2007-03-06 03:46:41 +00001480 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer10dbc1e2007-03-06 03:41:50 +00001481
Reid Spencer1d482072007-04-26 18:19:35 +00001482 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001483 return Dest;
1484}
1485
Chris Lattner229907c2011-07-18 04:54:35 +00001486GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001487 ExecutionContext &SF) {
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001488
1489 // This instruction supports bitwise conversion of vectors to integers and
1490 // to vectors of other types (as long as they have the same size)
Chris Lattner229907c2011-07-18 04:54:35 +00001491 Type *SrcTy = SrcVal->getType();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001492 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001493
1494 if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1495 (DstTy->getTypeID() == Type::VectorTyID)) {
1496 // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1497 // scalar src bitcast to vector dst
Mehdi Aminia3fcefb2015-07-16 16:34:23 +00001498 bool isLittleEndian = getDataLayout().isLittleEndian();
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001499 GenericValue TempDst, TempSrc, SrcVec;
Craig Toppere3dcce92015-08-01 22:20:21 +00001500 Type *SrcElemTy;
1501 Type *DstElemTy;
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001502 unsigned SrcBitSize;
1503 unsigned DstBitSize;
1504 unsigned SrcNum;
1505 unsigned DstNum;
1506
1507 if (SrcTy->getTypeID() == Type::VectorTyID) {
1508 SrcElemTy = SrcTy->getScalarType();
1509 SrcBitSize = SrcTy->getScalarSizeInBits();
1510 SrcNum = Src.AggregateVal.size();
1511 SrcVec = Src;
1512 } else {
1513 // if src is scalar value, make it vector <1 x type>
1514 SrcElemTy = SrcTy;
1515 SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1516 SrcNum = 1;
1517 SrcVec.AggregateVal.push_back(Src);
1518 }
1519
1520 if (DstTy->getTypeID() == Type::VectorTyID) {
1521 DstElemTy = DstTy->getScalarType();
1522 DstBitSize = DstTy->getScalarSizeInBits();
1523 DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1524 } else {
1525 DstElemTy = DstTy;
1526 DstBitSize = DstTy->getPrimitiveSizeInBits();
1527 DstNum = 1;
1528 }
1529
1530 if (SrcNum * SrcBitSize != DstNum * DstBitSize)
Torok Edwinfbcc6632009-07-14 16:55:14 +00001531 llvm_unreachable("Invalid BitCast");
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001532
1533 // If src is floating point, cast to integer first.
1534 TempSrc.AggregateVal.resize(SrcNum);
1535 if (SrcElemTy->isFloatTy()) {
1536 for (unsigned i = 0; i < SrcNum; i++)
1537 TempSrc.AggregateVal[i].IntVal =
1538 APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);
1539
1540 } else if (SrcElemTy->isDoubleTy()) {
1541 for (unsigned i = 0; i < SrcNum; i++)
1542 TempSrc.AggregateVal[i].IntVal =
1543 APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);
1544 } else if (SrcElemTy->isIntegerTy()) {
1545 for (unsigned i = 0; i < SrcNum; i++)
1546 TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1547 } else {
1548 // Pointers are not allowed as the element type of vector.
1549 llvm_unreachable("Invalid Bitcast");
1550 }
1551
1552 // now TempSrc is integer type vector
1553 if (DstNum < SrcNum) {
1554 // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1555 unsigned Ratio = SrcNum / DstNum;
1556 unsigned SrcElt = 0;
1557 for (unsigned i = 0; i < DstNum; i++) {
1558 GenericValue Elt;
1559 Elt.IntVal = 0;
1560 Elt.IntVal = Elt.IntVal.zext(DstBitSize);
1561 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1562 for (unsigned j = 0; j < Ratio; j++) {
1563 APInt Tmp;
1564 Tmp = Tmp.zext(SrcBitSize);
1565 Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1566 Tmp = Tmp.zext(DstBitSize);
Craig Topper24e71012017-04-28 03:36:24 +00001567 Tmp <<= ShiftAmt;
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001568 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1569 Elt.IntVal |= Tmp;
1570 }
1571 TempDst.AggregateVal.push_back(Elt);
1572 }
1573 } else {
1574 // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1575 unsigned Ratio = DstNum / SrcNum;
1576 for (unsigned i = 0; i < SrcNum; i++) {
1577 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1578 for (unsigned j = 0; j < Ratio; j++) {
1579 GenericValue Elt;
1580 Elt.IntVal = Elt.IntVal.zext(SrcBitSize);
1581 Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
Craig Topperfc947bc2017-04-18 17:14:21 +00001582 Elt.IntVal.lshrInPlace(ShiftAmt);
Elena Demikhovsky62d19c82013-08-05 12:17:06 +00001583 // it could be DstBitSize == SrcBitSize, so check it
1584 if (DstBitSize < SrcBitSize)
1585 Elt.IntVal = Elt.IntVal.trunc(DstBitSize);
1586 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1587 TempDst.AggregateVal.push_back(Elt);
1588 }
1589 }
1590 }
1591
1592 // convert result from integer to specified type
1593 if (DstTy->getTypeID() == Type::VectorTyID) {
1594 if (DstElemTy->isDoubleTy()) {
1595 Dest.AggregateVal.resize(DstNum);
1596 for (unsigned i = 0; i < DstNum; i++)
1597 Dest.AggregateVal[i].DoubleVal =
1598 TempDst.AggregateVal[i].IntVal.bitsToDouble();
1599 } else if (DstElemTy->isFloatTy()) {
1600 Dest.AggregateVal.resize(DstNum);
1601 for (unsigned i = 0; i < DstNum; i++)
1602 Dest.AggregateVal[i].FloatVal =
1603 TempDst.AggregateVal[i].IntVal.bitsToFloat();
1604 } else {
1605 Dest = TempDst;
1606 }
1607 } else {
1608 if (DstElemTy->isDoubleTy())
1609 Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1610 else if (DstElemTy->isFloatTy()) {
1611 Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1612 } else {
1613 Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1614 }
1615 }
1616 } else { // if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1617 // (DstTy->getTypeID() == Type::VectorTyID))
1618
1619 // scalar src bitcast to scalar dst
1620 if (DstTy->isPointerTy()) {
1621 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1622 Dest.PointerVal = Src.PointerVal;
1623 } else if (DstTy->isIntegerTy()) {
1624 if (SrcTy->isFloatTy())
1625 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1626 else if (SrcTy->isDoubleTy()) {
1627 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1628 } else if (SrcTy->isIntegerTy()) {
1629 Dest.IntVal = Src.IntVal;
1630 } else {
1631 llvm_unreachable("Invalid BitCast");
1632 }
1633 } else if (DstTy->isFloatTy()) {
1634 if (SrcTy->isIntegerTy())
1635 Dest.FloatVal = Src.IntVal.bitsToFloat();
1636 else {
1637 Dest.FloatVal = Src.FloatVal;
1638 }
1639 } else if (DstTy->isDoubleTy()) {
1640 if (SrcTy->isIntegerTy())
1641 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1642 else {
1643 Dest.DoubleVal = Src.DoubleVal;
1644 }
1645 } else {
1646 llvm_unreachable("Invalid Bitcast");
1647 }
1648 }
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001649
1650 return Dest;
1651}
1652
1653void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattner185045c2003-05-10 21:22:39 +00001654 ExecutionContext &SF = ECStack.back();
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001655 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1656}
1657
1658void Interpreter::visitSExtInst(SExtInst &I) {
1659 ExecutionContext &SF = ECStack.back();
1660 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1661}
1662
1663void Interpreter::visitZExtInst(ZExtInst &I) {
1664 ExecutionContext &SF = ECStack.back();
1665 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1666}
1667
1668void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1669 ExecutionContext &SF = ECStack.back();
1670 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1671}
1672
1673void Interpreter::visitFPExtInst(FPExtInst &I) {
1674 ExecutionContext &SF = ECStack.back();
1675 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1676}
1677
1678void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1679 ExecutionContext &SF = ECStack.back();
1680 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1681}
1682
1683void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1684 ExecutionContext &SF = ECStack.back();
1685 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1686}
1687
1688void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1689 ExecutionContext &SF = ECStack.back();
1690 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1691}
1692
1693void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1694 ExecutionContext &SF = ECStack.back();
1695 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1696}
1697
1698void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1699 ExecutionContext &SF = ECStack.back();
1700 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1701}
1702
1703void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1704 ExecutionContext &SF = ECStack.back();
1705 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1706}
1707
1708void Interpreter::visitBitCastInst(BitCastInst &I) {
1709 ExecutionContext &SF = ECStack.back();
1710 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnerc837dbc2002-08-27 22:33:45 +00001711}
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001712
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001713#define IMPLEMENT_VAARG(TY) \
1714 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1715
1716void Interpreter::visitVAArgInst(VAArgInst &I) {
1717 ExecutionContext &SF = ECStack.back();
1718
Brian Gaeke7b4be132004-02-25 23:01:48 +00001719 // Get the incoming valist parameter. LLI treats the valist as a
1720 // (ec-stack-depth var-arg-index) pair.
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001721 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke7b4be132004-02-25 23:01:48 +00001722 GenericValue Dest;
1723 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencer40015aa2007-03-06 03:09:31 +00001724 .VarArgs[VAList.UIntPairVal.second];
Chris Lattner229907c2011-07-18 04:54:35 +00001725 Type *Ty = I.getType();
Chris Lattner6b727592004-06-17 18:19:28 +00001726 switch (Ty->getTypeID()) {
Jim Grosbachb9baa442013-01-31 19:46:59 +00001727 case Type::IntegerTyID:
1728 Dest.IntVal = Src.IntVal;
Jim Grosbachb9baa442013-01-31 19:46:59 +00001729 break;
Jim Grosbachd0ef7282013-02-01 18:57:06 +00001730 IMPLEMENT_VAARG(Pointer);
1731 IMPLEMENT_VAARG(Float);
1732 IMPLEMENT_VAARG(Double);
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001733 default:
David Greene8121a812010-01-05 01:27:23 +00001734 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +00001735 llvm_unreachable(nullptr);
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001736 }
Misha Brukman91fb9ab2005-04-21 22:43:08 +00001737
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001738 // Set the Value of this Instruction.
1739 SetValue(&I, Dest, SF);
Andrew Lenharth9144ec42005-06-18 18:34:52 +00001740
1741 // Move the pointer to the next vararg.
1742 ++VAList.UIntPairVal.second;
Brian Gaeke9ef636c2003-11-07 21:20:47 +00001743}
1744
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001745void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1746 ExecutionContext &SF = ECStack.back();
1747 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1748 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1749 GenericValue Dest;
1750
1751 Type *Ty = I.getType();
1752 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1753
1754 if(Src1.AggregateVal.size() > indx) {
1755 switch (Ty->getTypeID()) {
1756 default:
1757 dbgs() << "Unhandled destination type for extractelement instruction: "
1758 << *Ty << "\n";
Craig Toppere73658d2014-04-28 04:05:08 +00001759 llvm_unreachable(nullptr);
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001760 break;
1761 case Type::IntegerTyID:
1762 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1763 break;
1764 case Type::FloatTyID:
1765 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1766 break;
1767 case Type::DoubleTyID:
1768 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1769 break;
1770 }
1771 } else {
1772 dbgs() << "Invalid index in extractelement instruction\n";
1773 }
1774
1775 SetValue(&I, Dest, SF);
1776}
1777
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001778void Interpreter::visitInsertElementInst(InsertElementInst &I) {
1779 ExecutionContext &SF = ECStack.back();
James Y Knight62df5ee2019-01-10 16:07:20 +00001780 VectorType *Ty = cast<VectorType>(I.getType());
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001781
1782 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1783 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1784 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1785 GenericValue Dest;
1786
James Y Knight62df5ee2019-01-10 16:07:20 +00001787 Type *TyContained = Ty->getElementType();
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001788
1789 const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
1790 Dest.AggregateVal = Src1.AggregateVal;
1791
1792 if(Src1.AggregateVal.size() <= indx)
1793 llvm_unreachable("Invalid index in insertelement instruction");
1794 switch (TyContained->getTypeID()) {
1795 default:
1796 llvm_unreachable("Unhandled dest type for insertelement instruction");
1797 case Type::IntegerTyID:
1798 Dest.AggregateVal[indx].IntVal = Src2.IntVal;
1799 break;
1800 case Type::FloatTyID:
1801 Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
1802 break;
1803 case Type::DoubleTyID:
1804 Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
1805 break;
1806 }
1807 SetValue(&I, Dest, SF);
1808}
1809
1810void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){
1811 ExecutionContext &SF = ECStack.back();
1812
James Y Knight62df5ee2019-01-10 16:07:20 +00001813 VectorType *Ty = cast<VectorType>(I.getType());
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001814
1815 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1816 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1817 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1818 GenericValue Dest;
1819
1820 // There is no need to check types of src1 and src2, because the compiled
1821 // bytecode can't contain different types for src1 and src2 for a
1822 // shufflevector instruction.
1823
James Y Knight62df5ee2019-01-10 16:07:20 +00001824 Type *TyContained = Ty->getElementType();
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001825 unsigned src1Size = (unsigned)Src1.AggregateVal.size();
1826 unsigned src2Size = (unsigned)Src2.AggregateVal.size();
1827 unsigned src3Size = (unsigned)Src3.AggregateVal.size();
1828
1829 Dest.AggregateVal.resize(src3Size);
1830
1831 switch (TyContained->getTypeID()) {
1832 default:
1833 llvm_unreachable("Unhandled dest type for insertelement instruction");
1834 break;
1835 case Type::IntegerTyID:
1836 for( unsigned i=0; i<src3Size; i++) {
1837 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1838 if(j < src1Size)
1839 Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
1840 else if(j < src1Size + src2Size)
1841 Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
1842 else
1843 // The selector may not be greater than sum of lengths of first and
1844 // second operands and llasm should not allow situation like
1845 // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
1846 // <2 x i32> < i32 0, i32 5 >,
1847 // where i32 5 is invalid, but let it be additional check here:
1848 llvm_unreachable("Invalid mask in shufflevector instruction");
1849 }
1850 break;
1851 case Type::FloatTyID:
1852 for( unsigned i=0; i<src3Size; i++) {
1853 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1854 if(j < src1Size)
1855 Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
1856 else if(j < src1Size + src2Size)
1857 Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
1858 else
1859 llvm_unreachable("Invalid mask in shufflevector instruction");
1860 }
1861 break;
1862 case Type::DoubleTyID:
1863 for( unsigned i=0; i<src3Size; i++) {
1864 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1865 if(j < src1Size)
1866 Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
1867 else if(j < src1Size + src2Size)
1868 Dest.AggregateVal[i].DoubleVal =
1869 Src2.AggregateVal[j-src1Size].DoubleVal;
1870 else
1871 llvm_unreachable("Invalid mask in shufflevector instruction");
1872 }
1873 break;
1874 }
1875 SetValue(&I, Dest, SF);
1876}
1877
Elena Demikhovsky8e97f012013-09-12 10:48:23 +00001878void Interpreter::visitExtractValueInst(ExtractValueInst &I) {
1879 ExecutionContext &SF = ECStack.back();
1880 Value *Agg = I.getAggregateOperand();
1881 GenericValue Dest;
1882 GenericValue Src = getOperandValue(Agg, SF);
1883
1884 ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1885 unsigned Num = I.getNumIndices();
1886 GenericValue *pSrc = &Src;
1887
1888 for (unsigned i = 0 ; i < Num; ++i) {
1889 pSrc = &pSrc->AggregateVal[*IdxBegin];
1890 ++IdxBegin;
1891 }
1892
1893 Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1894 switch (IndexedType->getTypeID()) {
1895 default:
1896 llvm_unreachable("Unhandled dest type for extractelement instruction");
1897 break;
1898 case Type::IntegerTyID:
1899 Dest.IntVal = pSrc->IntVal;
1900 break;
1901 case Type::FloatTyID:
1902 Dest.FloatVal = pSrc->FloatVal;
1903 break;
1904 case Type::DoubleTyID:
1905 Dest.DoubleVal = pSrc->DoubleVal;
1906 break;
1907 case Type::ArrayTyID:
1908 case Type::StructTyID:
1909 case Type::VectorTyID:
1910 Dest.AggregateVal = pSrc->AggregateVal;
1911 break;
1912 case Type::PointerTyID:
1913 Dest.PointerVal = pSrc->PointerVal;
1914 break;
1915 }
1916
1917 SetValue(&I, Dest, SF);
1918}
1919
1920void Interpreter::visitInsertValueInst(InsertValueInst &I) {
1921
1922 ExecutionContext &SF = ECStack.back();
1923 Value *Agg = I.getAggregateOperand();
1924
1925 GenericValue Src1 = getOperandValue(Agg, SF);
1926 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1927 GenericValue Dest = Src1; // Dest is a slightly changed Src1
1928
1929 ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
1930 unsigned Num = I.getNumIndices();
1931
1932 GenericValue *pDest = &Dest;
1933 for (unsigned i = 0 ; i < Num; ++i) {
1934 pDest = &pDest->AggregateVal[*IdxBegin];
1935 ++IdxBegin;
1936 }
1937 // pDest points to the target value in the Dest now
1938
1939 Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1940
1941 switch (IndexedType->getTypeID()) {
1942 default:
1943 llvm_unreachable("Unhandled dest type for insertelement instruction");
1944 break;
1945 case Type::IntegerTyID:
1946 pDest->IntVal = Src2.IntVal;
1947 break;
1948 case Type::FloatTyID:
1949 pDest->FloatVal = Src2.FloatVal;
1950 break;
1951 case Type::DoubleTyID:
1952 pDest->DoubleVal = Src2.DoubleVal;
1953 break;
1954 case Type::ArrayTyID:
1955 case Type::StructTyID:
1956 case Type::VectorTyID:
1957 pDest->AggregateVal = Src2.AggregateVal;
1958 break;
1959 case Type::PointerTyID:
1960 pDest->PointerVal = Src2.PointerVal;
1961 break;
1962 }
1963
1964 SetValue(&I, Dest, SF);
1965}
1966
Reid Spencer54c6e842007-03-03 06:22:22 +00001967GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1968 ExecutionContext &SF) {
1969 switch (CE->getOpcode()) {
Elena Demikhovsky843657c2013-09-02 06:40:09 +00001970 case Instruction::Trunc:
Reid Spencer54c6e842007-03-03 06:22:22 +00001971 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1972 case Instruction::ZExt:
1973 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1974 case Instruction::SExt:
1975 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1976 case Instruction::FPTrunc:
1977 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1978 case Instruction::FPExt:
1979 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1980 case Instruction::UIToFP:
1981 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1982 case Instruction::SIToFP:
1983 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1984 case Instruction::FPToUI:
1985 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1986 case Instruction::FPToSI:
1987 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1988 case Instruction::PtrToInt:
1989 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1990 case Instruction::IntToPtr:
1991 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1992 case Instruction::BitCast:
1993 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1994 case Instruction::GetElementPtr:
1995 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1996 gep_type_end(CE), SF);
Reid Spencerec8c51c2007-03-03 08:38:04 +00001997 case Instruction::FCmp:
1998 case Instruction::ICmp:
1999 return executeCmpInst(CE->getPredicate(),
2000 getOperandValue(CE->getOperand(0), SF),
2001 getOperandValue(CE->getOperand(1), SF),
2002 CE->getOperand(0)->getType());
2003 case Instruction::Select:
2004 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
2005 getOperandValue(CE->getOperand(1), SF),
Elena Demikhovsky843657c2013-09-02 06:40:09 +00002006 getOperandValue(CE->getOperand(2), SF),
2007 CE->getOperand(0)->getType());
Reid Spencer54c6e842007-03-03 06:22:22 +00002008 default :
2009 break;
2010 }
Reid Spencerec8c51c2007-03-03 08:38:04 +00002011
2012 // The cases below here require a GenericValue parameter for the result
2013 // so we initialize one, compute it and then return it.
Reid Spencer40015aa2007-03-06 03:09:31 +00002014 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
2015 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencer54c6e842007-03-03 06:22:22 +00002016 GenericValue Dest;
Chris Lattner229907c2011-07-18 04:54:35 +00002017 Type * Ty = CE->getOperand(0)->getType();
Reid Spencer54c6e842007-03-03 06:22:22 +00002018 switch (CE->getOpcode()) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002019 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
2020 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
2021 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
2022 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
2023 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
2024 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencer40015aa2007-03-06 03:09:31 +00002025 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
2026 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
2027 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
2028 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
2029 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
2030 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmana5b96452009-06-04 22:49:04 +00002031 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
2032 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
2033 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002034 case Instruction::Shl:
Reid Spencer40015aa2007-03-06 03:09:31 +00002035 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
2036 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002037 case Instruction::LShr:
Reid Spencer40015aa2007-03-06 03:09:31 +00002038 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
2039 break;
Fangrui Songf78650a2018-07-30 19:41:25 +00002040 case Instruction::AShr:
Reid Spencer40015aa2007-03-06 03:09:31 +00002041 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
2042 break;
Reid Spencer54c6e842007-03-03 06:22:22 +00002043 default:
David Greene8121a812010-01-05 01:27:23 +00002044 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
Ahmed Charles636a3d62012-02-19 11:37:01 +00002045 llvm_unreachable("Unhandled ConstantExpr");
Reid Spencer54c6e842007-03-03 06:22:22 +00002046 }
Reid Spencerec8c51c2007-03-03 08:38:04 +00002047 return Dest;
Reid Spencer54c6e842007-03-03 06:22:22 +00002048}
2049
2050GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
2051 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
2052 return getConstantExprValue(CE, SF);
2053 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
2054 return getConstantValue(CPV);
2055 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2056 return PTOGV(getPointerToGlobal(GV));
2057 } else {
2058 return SF.Values[V];
2059 }
2060}
2061
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002062//===----------------------------------------------------------------------===//
2063// Dispatch and Execution Code
2064//===----------------------------------------------------------------------===//
2065
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002066//===----------------------------------------------------------------------===//
Chris Lattner470754e2003-05-08 16:18:31 +00002067// callFunction - Execute the specified function...
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002068//
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +00002069void Interpreter::callFunction(Function *F, ArrayRef<GenericValue> ArgVals) {
Craig Toppere73658d2014-04-28 04:05:08 +00002070 assert((ECStack.empty() || !ECStack.back().Caller.getInstruction() ||
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002071 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
2072 "Incorrect number of arguments passed into function call!");
Chris Lattnera28e1f22003-09-17 17:26:22 +00002073 // Make a new stack frame... and fill it in.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002074 ECStack.emplace_back();
Chris Lattnera28e1f22003-09-17 17:26:22 +00002075 ExecutionContext &StackFrame = ECStack.back();
Chris Lattner470754e2003-05-08 16:18:31 +00002076 StackFrame.CurFunction = F;
Brian Gaeke65cac902003-11-07 05:22:49 +00002077
2078 // Special handling for external functions.
Reid Spencer5301e7c2007-01-30 20:08:39 +00002079 if (F->isDeclaration()) {
Brian Gaeke65cac902003-11-07 05:22:49 +00002080 GenericValue Result = callExternalFunction (F, ArgVals);
2081 // Simulate a 'ret' instruction of the appropriate type.
2082 popStackAndReturnValueToCaller (F->getReturnType (), Result);
2083 return;
2084 }
2085
2086 // Get pointers to first LLVM BB & Instruction in function.
Duncan P. N. Exon Smithc8f02e72015-10-13 17:33:41 +00002087 StackFrame.CurBB = &F->front();
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002088 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002089
Chris Lattner62b7fd12002-04-07 20:49:59 +00002090 // Run through the function arguments and initialize their values...
Chris Lattner531f9e92005-03-15 04:54:21 +00002091 assert((ArgVals.size() == F->arg_size() ||
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002092 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner62b7fd12002-04-07 20:49:59 +00002093 "Invalid number of values passed to function invocation!");
Chris Lattner22e90432003-05-08 16:06:52 +00002094
2095 // Handle non-varargs arguments...
Chris Lattner676d4112001-09-10 04:49:44 +00002096 unsigned i = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +00002097 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
Reid Spencerc57be6c2007-04-16 21:50:40 +00002098 AI != E; ++AI, ++i)
Duncan P. N. Exon Smithc8f02e72015-10-13 17:33:41 +00002099 SetValue(&*AI, ArgVals[i], StackFrame);
Chris Lattner22e90432003-05-08 16:06:52 +00002100
2101 // Handle varargs arguments...
2102 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002103}
2104
Reid Spencer65ba7502007-05-16 02:05:13 +00002105
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002106void Interpreter::run() {
Brian Gaeke411281c2003-09-04 23:15:40 +00002107 while (!ECStack.empty()) {
Brian Gaekee278c222003-10-24 19:59:01 +00002108 // Interpret a single instruction & increment the "PC".
2109 ExecutionContext &SF = ECStack.back(); // Current stack frame
2110 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukman91fb9ab2005-04-21 22:43:08 +00002111
Brian Gaekee278c222003-10-24 19:59:01 +00002112 // Track the number of dynamic instructions executed.
2113 ++NumDynamicInsts;
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002114
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002115 LLVM_DEBUG(dbgs() << "About to interpret: " << I);
Brian Gaekee278c222003-10-24 19:59:01 +00002116 visit(I); // Dispatch to one of the visit* methods...
Chris Lattnerd7ff5782001-08-23 17:05:04 +00002117 }
2118}