blob: e02ba15ab1035a3f3b2f824206fa1c8b87f5223a [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- Execution.cpp - Implement code to simulate the program ------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmand1c881a2005-04-21 22:43:08 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmand1c881a2005-04-21 22:43:08 +00009//
Chris Lattner92101ac2001-08-23 17:05:04 +000010// This file contains the actual instruction interpreter.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaeke63438cc2003-12-11 00:22:59 +000014#define DEBUG_TYPE "interpreter"
Chris Lattner92101ac2001-08-23 17:05:04 +000015#include "Interpreter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Instructions.h"
Chris Lattner663ceeb2008-07-08 17:25:49 +000022#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000026#include "llvm/Support/MathExtras.h"
Gabor Greif724441e2007-10-11 19:40:35 +000027#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000028#include <cmath>
Chris Lattner4af6de82003-11-25 20:44:56 +000029using namespace llvm;
Chris Lattnerfe11a972002-12-23 23:59:41 +000030
Chris Lattnercecf56b2006-12-19 22:56:53 +000031STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner663ceeb2008-07-08 17:25:49 +000033static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
34 cl::desc("make the interpreter print every volatile load and store"));
35
Chris Lattner2e42d3a2001-10-15 05:51:48 +000036//===----------------------------------------------------------------------===//
Reid Spencere1aa0662007-03-03 06:22:22 +000037// Various Helper Functions
Chris Lattner39bb5b42001-10-15 13:25:40 +000038//===----------------------------------------------------------------------===//
Chris Lattner73011782003-12-28 09:44:37 +000039
Chris Lattner39bb5b42001-10-15 13:25:40 +000040static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +000041 SF.Values[V] = Val;
Chris Lattner39bb5b42001-10-15 13:25:40 +000042}
43
Chris Lattner2adcd832002-05-03 19:52:30 +000044//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +000045// Binary Instruction Implementations
46//===----------------------------------------------------------------------===//
47
48#define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +000049 case Type::TY##TyID: \
50 Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
51 break
Chris Lattner92101ac2001-08-23 17:05:04 +000052
Dan Gohmanae3a0be2009-06-04 22:49:04 +000053static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000054 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000055 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000056 IMPLEMENT_BINARY_OPERATOR(+, Float);
57 IMPLEMENT_BINARY_OPERATOR(+, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000058 default:
David Greeneef573a32010-01-05 01:27:23 +000059 dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000060 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +000061 }
Chris Lattner92101ac2001-08-23 17:05:04 +000062}
63
Dan Gohmanae3a0be2009-06-04 22:49:04 +000064static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000065 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000066 switch (Ty->getTypeID()) {
Chris Lattner92101ac2001-08-23 17:05:04 +000067 IMPLEMENT_BINARY_OPERATOR(-, Float);
68 IMPLEMENT_BINARY_OPERATOR(-, Double);
Chris Lattner92101ac2001-08-23 17:05:04 +000069 default:
David Greeneef573a32010-01-05 01:27:23 +000070 dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000071 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +000072 }
Chris Lattner92101ac2001-08-23 17:05:04 +000073}
74
Dan Gohmanae3a0be2009-06-04 22:49:04 +000075static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000076 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000077 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000078 IMPLEMENT_BINARY_OPERATOR(*, Float);
79 IMPLEMENT_BINARY_OPERATOR(*, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000080 default:
David Greeneef573a32010-01-05 01:27:23 +000081 dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000082 llvm_unreachable(0);
Chris Lattnerc2593162001-10-27 08:28:11 +000083 }
Chris Lattnerc2593162001-10-27 08:28:11 +000084}
85
Reid Spencere1aa0662007-03-03 06:22:22 +000086static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000087 GenericValue Src2, Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000088 switch (Ty->getTypeID()) {
Chris Lattnerc2593162001-10-27 08:28:11 +000089 IMPLEMENT_BINARY_OPERATOR(/, Float);
90 IMPLEMENT_BINARY_OPERATOR(/, Double);
Chris Lattnerc2593162001-10-27 08:28:11 +000091 default:
David Greeneef573a32010-01-05 01:27:23 +000092 dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +000093 llvm_unreachable(0);
Chris Lattnerbb76f022001-10-30 20:27:31 +000094 }
Chris Lattnerbb76f022001-10-30 20:27:31 +000095}
96
Reid Spencere1aa0662007-03-03 06:22:22 +000097static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000098 GenericValue Src2, Type *Ty) {
Reid Spencer0a783f72006-11-02 01:53:59 +000099 switch (Ty->getTypeID()) {
Chris Lattnerbb76f022001-10-30 20:27:31 +0000100 case Type::FloatTyID:
101 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
102 break;
103 case Type::DoubleTyID:
104 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
105 break;
106 default:
David Greeneef573a32010-01-05 01:27:23 +0000107 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Chris Lattnerc2593162001-10-27 08:28:11 +0000109 }
Chris Lattnerc2593162001-10-27 08:28:11 +0000110}
111
Reid Spencerf9536332007-03-06 03:09:31 +0000112#define IMPLEMENT_INTEGER_ICMP(OP, TY) \
113 case Type::IntegerTyID: \
114 Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
115 break;
Chris Lattner92101ac2001-08-23 17:05:04 +0000116
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000117#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
118 case Type::VectorTyID: { \
119 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
120 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
121 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
122 Dest.AggregateVal[_i].IntVal = APInt(1, \
123 Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
124 } break;
125
Chris Lattnerfd506f52003-04-23 19:55:35 +0000126// Handle pointers specially because they must be compared with only as much
127// width as the host has. We _do not_ want to be comparing 64 bit values when
128// running on a 32-bit target, otherwise the upper 32 bits might mess up
129// comparisons if they contain garbage.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000130#define IMPLEMENT_POINTER_ICMP(OP) \
Chris Lattnerfd506f52003-04-23 19:55:35 +0000131 case Type::PointerTyID: \
Reid Spencerf9536332007-03-06 03:09:31 +0000132 Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
133 (void*)(intptr_t)Src2.PointerVal); \
134 break;
Chris Lattnerfd506f52003-04-23 19:55:35 +0000135
Reid Spencere4d87aa2006-12-23 06:05:41 +0000136static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000137 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000138 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000139 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000140 IMPLEMENT_INTEGER_ICMP(eq,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000141 IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000142 IMPLEMENT_POINTER_ICMP(==);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000143 default:
David Greeneef573a32010-01-05 01:27:23 +0000144 dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000145 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000146 }
147 return Dest;
148}
149
150static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000151 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000152 GenericValue Dest;
153 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000154 IMPLEMENT_INTEGER_ICMP(ne,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000155 IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000156 IMPLEMENT_POINTER_ICMP(!=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000157 default:
David Greeneef573a32010-01-05 01:27:23 +0000158 dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000159 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000160 }
161 return Dest;
162}
163
164static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000165 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000166 GenericValue Dest;
167 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000168 IMPLEMENT_INTEGER_ICMP(ult,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000169 IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000170 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000171 default:
David Greeneef573a32010-01-05 01:27:23 +0000172 dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000173 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000174 }
175 return Dest;
176}
177
178static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000179 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000180 GenericValue Dest;
181 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000182 IMPLEMENT_INTEGER_ICMP(slt,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000183 IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000184 IMPLEMENT_POINTER_ICMP(<);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000185 default:
David Greeneef573a32010-01-05 01:27:23 +0000186 dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000187 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000188 }
189 return Dest;
190}
191
192static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000193 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000194 GenericValue Dest;
195 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000196 IMPLEMENT_INTEGER_ICMP(ugt,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000197 IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000198 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000199 default:
David Greeneef573a32010-01-05 01:27:23 +0000200 dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000201 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000202 }
203 return Dest;
204}
205
206static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000207 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000208 GenericValue Dest;
209 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000210 IMPLEMENT_INTEGER_ICMP(sgt,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000211 IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000212 IMPLEMENT_POINTER_ICMP(>);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000213 default:
David Greeneef573a32010-01-05 01:27:23 +0000214 dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000215 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000216 }
217 return Dest;
218}
219
220static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000221 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000222 GenericValue Dest;
223 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000224 IMPLEMENT_INTEGER_ICMP(ule,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000225 IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000226 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000227 default:
David Greeneef573a32010-01-05 01:27:23 +0000228 dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000229 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000230 }
231 return Dest;
232}
233
234static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000235 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000236 GenericValue Dest;
237 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000238 IMPLEMENT_INTEGER_ICMP(sle,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000239 IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000240 IMPLEMENT_POINTER_ICMP(<=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000241 default:
David Greeneef573a32010-01-05 01:27:23 +0000242 dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000243 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000244 }
245 return Dest;
246}
247
248static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000249 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000250 GenericValue Dest;
251 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000252 IMPLEMENT_INTEGER_ICMP(uge,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000253 IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000254 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000255 default:
David Greeneef573a32010-01-05 01:27:23 +0000256 dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000257 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000258 }
259 return Dest;
260}
261
262static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000263 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000264 GenericValue Dest;
265 switch (Ty->getTypeID()) {
Reid Spencerf9536332007-03-06 03:09:31 +0000266 IMPLEMENT_INTEGER_ICMP(sge,Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000267 IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000268 IMPLEMENT_POINTER_ICMP(>=);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000269 default:
David Greeneef573a32010-01-05 01:27:23 +0000270 dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000271 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000272 }
273 return Dest;
274}
275
Reid Spencere49661b2006-12-31 05:51:36 +0000276void Interpreter::visitICmpInst(ICmpInst &I) {
277 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000278 Type *Ty = I.getOperand(0)->getType();
Reid Spencere49661b2006-12-31 05:51:36 +0000279 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
280 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
281 GenericValue R; // Result
282
283 switch (I.getPredicate()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000284 case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
285 case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
Reid Spencere49661b2006-12-31 05:51:36 +0000286 case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
287 case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
288 case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
289 case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
290 case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
291 case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
292 case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
293 case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
294 default:
David Greeneef573a32010-01-05 01:27:23 +0000295 dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
Torok Edwinc23197a2009-07-14 16:55:14 +0000296 llvm_unreachable(0);
Reid Spencere49661b2006-12-31 05:51:36 +0000297 }
298
299 SetValue(&I, R, SF);
300}
301
302#define IMPLEMENT_FCMP(OP, TY) \
Reid Spencerf9536332007-03-06 03:09:31 +0000303 case Type::TY##TyID: \
304 Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
305 break
Reid Spencere4d87aa2006-12-23 06:05:41 +0000306
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000307#define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
308 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
309 Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
310 for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
311 Dest.AggregateVal[_i].IntVal = APInt(1, \
312 Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
313 break;
314
315#define IMPLEMENT_VECTOR_FCMP(OP) \
316 case Type::VectorTyID: \
317 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
318 IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
319 } else { \
320 IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
321 }
322
Reid Spencera54b7cb2007-01-12 07:05:14 +0000323static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000324 Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000325 GenericValue Dest;
326 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000327 IMPLEMENT_FCMP(==, Float);
328 IMPLEMENT_FCMP(==, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000329 IMPLEMENT_VECTOR_FCMP(==);
Chris Lattner92101ac2001-08-23 17:05:04 +0000330 default:
David Greeneef573a32010-01-05 01:27:23 +0000331 dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000332 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000333 }
334 return Dest;
335}
336
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000337#define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
338 if (TY->isFloatTy()) { \
339 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
340 Dest.IntVal = APInt(1,false); \
341 return Dest; \
342 } \
343 } else { \
344 if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
345 Dest.IntVal = APInt(1,false); \
346 return Dest; \
347 } \
348 }
349
350#define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
351 assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
352 Dest.AggregateVal.resize( X.AggregateVal.size() ); \
353 for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
354 if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
355 Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
356 Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
357 else { \
358 Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
359 } \
360 }
361
362#define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
363 if (TY->isVectorTy()) { \
364 if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
365 MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
366 } else { \
367 MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
368 } \
369 } \
370
371
372
Reid Spencera54b7cb2007-01-12 07:05:14 +0000373static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000374 Type *Ty)
375{
Chris Lattner92101ac2001-08-23 17:05:04 +0000376 GenericValue Dest;
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000377 // if input is scalar value and Src1 or Src2 is NaN return false
378 IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
379 // if vector input detect NaNs and fill mask
380 MASK_VECTOR_NANS(Ty, Src1, Src2, false)
381 GenericValue DestMask = Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000382 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000383 IMPLEMENT_FCMP(!=, Float);
384 IMPLEMENT_FCMP(!=, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000385 IMPLEMENT_VECTOR_FCMP(!=);
386 default:
387 dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
388 llvm_unreachable(0);
Nadav Rotem0fda0f32013-04-12 22:02:26 +0000389 }
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000390 // in vector case mask out NaN elements
391 if (Ty->isVectorTy())
392 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
393 if (DestMask.AggregateVal[_i].IntVal == false)
394 Dest.AggregateVal[_i].IntVal = APInt(1,false);
395
Chris Lattner92101ac2001-08-23 17:05:04 +0000396 return Dest;
397}
398
Reid Spencera54b7cb2007-01-12 07:05:14 +0000399static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000400 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000401 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000402 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000403 IMPLEMENT_FCMP(<=, Float);
404 IMPLEMENT_FCMP(<=, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000405 IMPLEMENT_VECTOR_FCMP(<=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000406 default:
David Greeneef573a32010-01-05 01:27:23 +0000407 dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000408 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000409 }
410 return Dest;
411}
412
Reid Spencera54b7cb2007-01-12 07:05:14 +0000413static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000414 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000415 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000416 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000417 IMPLEMENT_FCMP(>=, Float);
418 IMPLEMENT_FCMP(>=, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000419 IMPLEMENT_VECTOR_FCMP(>=);
Chris Lattner92101ac2001-08-23 17:05:04 +0000420 default:
David Greeneef573a32010-01-05 01:27:23 +0000421 dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000422 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000423 }
424 return Dest;
425}
426
Reid Spencera54b7cb2007-01-12 07:05:14 +0000427static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000428 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000429 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000430 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000431 IMPLEMENT_FCMP(<, Float);
432 IMPLEMENT_FCMP(<, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000433 IMPLEMENT_VECTOR_FCMP(<);
Chris Lattner92101ac2001-08-23 17:05:04 +0000434 default:
David Greeneef573a32010-01-05 01:27:23 +0000435 dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000436 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000437 }
438 return Dest;
439}
440
Reid Spencera54b7cb2007-01-12 07:05:14 +0000441static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000442 Type *Ty) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000443 GenericValue Dest;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000444 switch (Ty->getTypeID()) {
Reid Spencere49661b2006-12-31 05:51:36 +0000445 IMPLEMENT_FCMP(>, Float);
446 IMPLEMENT_FCMP(>, Double);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000447 IMPLEMENT_VECTOR_FCMP(>);
Chris Lattner92101ac2001-08-23 17:05:04 +0000448 default:
David Greeneef573a32010-01-05 01:27:23 +0000449 dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000450 llvm_unreachable(0);
Chris Lattner92101ac2001-08-23 17:05:04 +0000451 }
452 return Dest;
453}
454
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000455#define IMPLEMENT_UNORDERED(TY, X,Y) \
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000456 if (TY->isFloatTy()) { \
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000457 if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
458 Dest.IntVal = APInt(1,true); \
459 return Dest; \
460 } \
461 } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
462 Dest.IntVal = APInt(1,true); \
463 return Dest; \
464 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000465
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000466#define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \
467 if (TY->isVectorTy()) { \
468 GenericValue DestMask = Dest; \
469 Dest = _FUNC(Src1, Src2, Ty); \
470 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \
471 if (DestMask.AggregateVal[_i].IntVal == true) \
472 Dest.AggregateVal[_i].IntVal = APInt(1,true); \
473 return Dest; \
474 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000475
476static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000477 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000478 GenericValue Dest;
479 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000480 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
481 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000482 return executeFCMP_OEQ(Src1, Src2, Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000483
Reid Spencera54b7cb2007-01-12 07:05:14 +0000484}
485
486static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000487 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000488 GenericValue Dest;
489 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000490 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
491 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000492 return executeFCMP_ONE(Src1, Src2, Ty);
493}
494
495static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000496 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000497 GenericValue Dest;
498 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000499 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
500 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000501 return executeFCMP_OLE(Src1, Src2, Ty);
502}
503
504static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000505 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000506 GenericValue Dest;
507 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000508 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
509 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000510 return executeFCMP_OGE(Src1, Src2, Ty);
511}
512
513static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000514 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000515 GenericValue Dest;
516 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000517 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
518 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000519 return executeFCMP_OLT(Src1, Src2, Ty);
520}
521
522static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000523 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000524 GenericValue Dest;
525 IMPLEMENT_UNORDERED(Ty, Src1, Src2)
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000526 MASK_VECTOR_NANS(Ty, Src1, Src2, true)
527 IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000528 return executeFCMP_OGT(Src1, Src2, Ty);
529}
530
531static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000532 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000533 GenericValue Dest;
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000534 if(Ty->isVectorTy()) {
535 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
536 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
537 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
538 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
539 Dest.AggregateVal[_i].IntVal = APInt(1,
540 ( (Src1.AggregateVal[_i].FloatVal ==
541 Src1.AggregateVal[_i].FloatVal) &&
542 (Src2.AggregateVal[_i].FloatVal ==
543 Src2.AggregateVal[_i].FloatVal)));
544 } else {
545 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
546 Dest.AggregateVal[_i].IntVal = APInt(1,
547 ( (Src1.AggregateVal[_i].DoubleVal ==
548 Src1.AggregateVal[_i].DoubleVal) &&
549 (Src2.AggregateVal[_i].DoubleVal ==
550 Src2.AggregateVal[_i].DoubleVal)));
551 }
552 } else if (Ty->isFloatTy())
Reid Spencerf9536332007-03-06 03:09:31 +0000553 Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
554 Src2.FloatVal == Src2.FloatVal));
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000555 else {
Reid Spencerf9536332007-03-06 03:09:31 +0000556 Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
557 Src2.DoubleVal == Src2.DoubleVal));
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000558 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000559 return Dest;
560}
561
562static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000563 Type *Ty) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000564 GenericValue Dest;
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000565 if(Ty->isVectorTy()) {
566 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
567 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
568 if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
569 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
570 Dest.AggregateVal[_i].IntVal = APInt(1,
571 ( (Src1.AggregateVal[_i].FloatVal !=
572 Src1.AggregateVal[_i].FloatVal) ||
573 (Src2.AggregateVal[_i].FloatVal !=
574 Src2.AggregateVal[_i].FloatVal)));
575 } else {
576 for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
577 Dest.AggregateVal[_i].IntVal = APInt(1,
578 ( (Src1.AggregateVal[_i].DoubleVal !=
579 Src1.AggregateVal[_i].DoubleVal) ||
580 (Src2.AggregateVal[_i].DoubleVal !=
581 Src2.AggregateVal[_i].DoubleVal)));
582 }
583 } else if (Ty->isFloatTy())
Reid Spencerf9536332007-03-06 03:09:31 +0000584 Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
585 Src2.FloatVal != Src2.FloatVal));
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000586 else {
Reid Spencerf9536332007-03-06 03:09:31 +0000587 Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
588 Src2.DoubleVal != Src2.DoubleVal));
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000589 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000590 return Dest;
591}
592
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000593static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
594 const Type *Ty, const bool val) {
595 GenericValue Dest;
596 if(Ty->isVectorTy()) {
597 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
598 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
599 for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
600 Dest.AggregateVal[_i].IntVal = APInt(1,val);
601 } else {
602 Dest.IntVal = APInt(1, val);
603 }
604
605 return Dest;
606}
607
Reid Spencere4d87aa2006-12-23 06:05:41 +0000608void Interpreter::visitFCmpInst(FCmpInst &I) {
609 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000610 Type *Ty = I.getOperand(0)->getType();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000611 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
612 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
613 GenericValue R; // Result
614
615 switch (I.getPredicate()) {
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000616 default:
617 dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
618 llvm_unreachable(0);
619 break;
620 case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
621 break;
622 case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
623 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000624 case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
625 case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
626 case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
627 case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
628 case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
629 case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
630 case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
631 case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
632 case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
633 case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
634 case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
635 case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
636 case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
637 case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000638 }
639
640 SetValue(&I, R, SF);
641}
642
Reid Spencere4d87aa2006-12-23 06:05:41 +0000643static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000644 GenericValue Src2, Type *Ty) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000645 GenericValue Result;
646 switch (predicate) {
647 case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
648 case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
649 case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
650 case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
651 case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
652 case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
653 case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
654 case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
655 case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
656 case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000657 case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
658 case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
659 case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
660 case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
661 case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
662 case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
663 case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
664 case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
665 case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
666 case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
667 case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
668 case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
669 case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
670 case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000671 case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
672 case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000673 default:
David Greeneef573a32010-01-05 01:27:23 +0000674 dbgs() << "Unhandled Cmp predicate\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000675 llvm_unreachable(0);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000676 }
677}
678
Chris Lattnerd7916e92003-05-10 21:22:39 +0000679void Interpreter::visitBinaryOperator(BinaryOperator &I) {
680 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000681 Type *Ty = I.getOperand(0)->getType();
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000682 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
683 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000684 GenericValue R; // Result
685
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000686 // First process vector operation
687 if (Ty->isVectorTy()) {
688 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
689 R.AggregateVal.resize(Src1.AggregateVal.size());
Nadav Rotem0fda0f32013-04-12 22:02:26 +0000690
Nadav Rotem0d840bb2013-04-26 20:19:41 +0000691 // Macros to execute binary operation 'OP' over integer vectors
692#define INTEGER_VECTOR_OPERATION(OP) \
693 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
694 R.AggregateVal[i].IntVal = \
695 Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
696
697 // Additional macros to execute binary operations udiv/sdiv/urem/srem since
698 // they have different notation.
699#define INTEGER_VECTOR_FUNCTION(OP) \
700 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
701 R.AggregateVal[i].IntVal = \
702 Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
703
704 // Macros to execute binary operation 'OP' over floating point type TY
705 // (float or double) vectors
706#define FLOAT_VECTOR_FUNCTION(OP, TY) \
707 for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
708 R.AggregateVal[i].TY = \
709 Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
710
711 // Macros to choose appropriate TY: float or double and run operation
712 // execution
713#define FLOAT_VECTOR_OP(OP) { \
714 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
715 FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
716 else { \
717 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
718 FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
719 else { \
720 dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
721 llvm_unreachable(0); \
722 } \
723 } \
724}
725
726 switch(I.getOpcode()){
727 default:
728 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
729 llvm_unreachable(0);
730 break;
731 case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break;
732 case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
733 case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
734 case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
735 case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
736 case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
737 case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
738 case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
739 case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
740 case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
741 case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
742 case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
743 case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
744 case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
745 case Instruction::FRem:
746 if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
747 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
748 R.AggregateVal[i].FloatVal =
749 fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
750 else {
751 if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
752 for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
753 R.AggregateVal[i].DoubleVal =
754 fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
755 else {
756 dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
757 llvm_unreachable(0);
758 }
759 }
760 break;
761 }
762 } else {
763 switch (I.getOpcode()) {
764 default:
765 dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
766 llvm_unreachable(0);
767 break;
768 case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
769 case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
770 case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
771 case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
772 case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
773 case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
774 case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
775 case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
776 case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
777 case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
778 case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
779 case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
780 case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
781 case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
782 case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
783 }
784 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000785 SetValue(&I, R, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000786}
787
Misha Brukmand1c881a2005-04-21 22:43:08 +0000788static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +0000789 GenericValue Src3, const Type *Ty) {
790 GenericValue Dest;
791 if(Ty->isVectorTy()) {
792 assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
793 assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
794 Dest.AggregateVal.resize( Src1.AggregateVal.size() );
795 for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
796 Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
797 Src3.AggregateVal[i] : Src2.AggregateVal[i];
798 } else {
799 Dest = (Src1.IntVal == 0) ? Src3 : Src2;
800 }
801 return Dest;
Chris Lattner759d34f2004-04-20 16:43:21 +0000802}
803
804void Interpreter::visitSelectInst(SelectInst &I) {
805 ExecutionContext &SF = ECStack.back();
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +0000806 const Type * Ty = I.getOperand(0)->getType();
Chris Lattner759d34f2004-04-20 16:43:21 +0000807 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
808 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
809 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +0000810 GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
Chris Lattner759d34f2004-04-20 16:43:21 +0000811 SetValue(&I, R, SF);
812}
813
Chris Lattner92101ac2001-08-23 17:05:04 +0000814//===----------------------------------------------------------------------===//
815// Terminator Instruction Implementations
816//===----------------------------------------------------------------------===//
817
Chris Lattnere43db882001-10-27 04:15:57 +0000818void Interpreter::exitCalled(GenericValue GV) {
Brian Gaeked8400d82004-02-13 05:48:00 +0000819 // runAtExitHandlers() assumes there are no stack frames, but
820 // if exit() was called, then it had a stack frame. Blow away
821 // the stack before interpreting atexit handlers.
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000822 ECStack.clear();
823 runAtExitHandlers();
824 exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
Chris Lattnere43db882001-10-27 04:15:57 +0000825}
826
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000827/// Pop the last stack frame off of ECStack and then copy the result
828/// back into the result variable if we are not returning void. The
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000829/// result variable may be the ExitValue, or the Value of the calling
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000830/// CallInst if there was a previous stack frame. This method may
831/// invalidate any ECStack iterators you have. This method also takes
832/// care of switching to the normal destination BB, if we are returning
833/// from an invoke.
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000834///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000835void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000836 GenericValue Result) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000837 // Pop the current stack frame.
838 ECStack.pop_back();
839
Misha Brukmand1c881a2005-04-21 22:43:08 +0000840 if (ECStack.empty()) { // Finished main. Put result into exit code...
Dan Gohman96343312010-06-18 02:01:10 +0000841 if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
Jeff Cohen8c9191c2006-02-07 05:29:44 +0000842 ExitValue = Result; // Capture the exit value of the program
Misha Brukmand1c881a2005-04-21 22:43:08 +0000843 } else {
Reid Spencere7707872007-06-01 22:23:29 +0000844 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
Misha Brukmand1c881a2005-04-21 22:43:08 +0000845 }
846 } else {
847 // If we have a previous stack frame, and we have a previous call,
848 // fill in the return value...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000849 ExecutionContext &CallingSF = ECStack.back();
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000850 if (Instruction *I = CallingSF.Caller.getInstruction()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000851 // Save result...
Benjamin Kramerf0127052010-01-05 13:12:22 +0000852 if (!CallingSF.Caller.getType()->isVoidTy())
Brian Gaekedbde1ae2003-11-07 20:44:58 +0000853 SetValue(I, Result, CallingSF);
854 if (InvokeInst *II = dyn_cast<InvokeInst> (I))
855 SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
Brian Gaeke2cb474c2003-11-07 19:26:23 +0000856 CallingSF.Caller = CallSite(); // We returned from the call...
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000857 }
858 }
859}
860
Chris Lattnerd7916e92003-05-10 21:22:39 +0000861void Interpreter::visitReturnInst(ReturnInst &I) {
862 ExecutionContext &SF = ECStack.back();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000863 Type *RetTy = Type::getVoidTy(I.getContext());
Chris Lattner92101ac2001-08-23 17:05:04 +0000864 GenericValue Result;
865
866 // Save away the return value... (if we are not 'ret void')
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000867 if (I.getNumOperands()) {
868 RetTy = I.getReturnValue()->getType();
869 Result = getOperandValue(I.getReturnValue(), SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000870 }
871
Brian Gaekeaf955ba2003-11-07 05:22:49 +0000872 popStackAndReturnValueToCaller(RetTy, Result);
Chris Lattner92101ac2001-08-23 17:05:04 +0000873}
874
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000875void Interpreter::visitUnreachableInst(UnreachableInst &I) {
Chris Lattner75361b62010-04-07 22:58:41 +0000876 report_fatal_error("Program executed an 'unreachable' instruction!");
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000877}
878
Chris Lattnerd7916e92003-05-10 21:22:39 +0000879void Interpreter::visitBranchInst(BranchInst &I) {
880 ExecutionContext &SF = ECStack.back();
Chris Lattner92101ac2001-08-23 17:05:04 +0000881 BasicBlock *Dest;
882
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000883 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
884 if (!I.isUnconditional()) {
885 Value *Cond = I.getCondition();
Reid Spencerf9536332007-03-06 03:09:31 +0000886 if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
Misha Brukmand1c881a2005-04-21 22:43:08 +0000887 Dest = I.getSuccessor(1);
Chris Lattner92101ac2001-08-23 17:05:04 +0000888 }
Chris Lattner77113b62003-05-10 20:21:16 +0000889 SwitchToNewBasicBlock(Dest, SF);
Chris Lattner92101ac2001-08-23 17:05:04 +0000890}
891
Chris Lattnerd7916e92003-05-10 21:22:39 +0000892void Interpreter::visitSwitchInst(SwitchInst &I) {
893 ExecutionContext &SF = ECStack.back();
Eli Friedmanbb5a7442011-09-29 20:21:17 +0000894 Value* Cond = I.getCondition();
895 Type *ElTy = Cond->getType();
896 GenericValue CondVal = getOperandValue(Cond, SF);
Chris Lattner09e93922003-04-22 20:34:47 +0000897
898 // Check to see if any of the cases match...
Chris Lattner77113b62003-05-10 20:21:16 +0000899 BasicBlock *Dest = 0;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +0000900 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000901 IntegersSubset& Case = i.getCaseValueEx();
902 if (Case.isSingleNumber()) {
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +0000903 // FIXME: Currently work with ConstantInt based numbers.
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000904 const ConstantInt *CI = Case.getSingleNumber(0).toConstantInt();
905 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
906 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000907 Dest = cast<BasicBlock>(i.getCaseSuccessor());
908 break;
909 }
Chris Lattner09e93922003-04-22 20:34:47 +0000910 }
Stepan Dyatkovskiy47cbc4e2012-06-23 10:58:58 +0000911 if (Case.isSingleNumbersOnly()) {
912 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
913 // FIXME: Currently work with ConstantInt based numbers.
914 const ConstantInt *CI = Case.getSingleNumber(n).toConstantInt();
915 GenericValue Val = getOperandValue(const_cast<ConstantInt*>(CI), SF);
916 if (executeICMP_EQ(Val, CondVal, ElTy).IntVal != 0) {
917 Dest = cast<BasicBlock>(i.getCaseSuccessor());
918 break;
919 }
920 }
921 } else
922 for (unsigned n = 0, en = Case.getNumItems(); n != en; ++n) {
923 IntegersSubset::Range r = Case.getItem(n);
924 // FIXME: Currently work with ConstantInt based numbers.
925 const ConstantInt *LowCI = r.getLow().toConstantInt();
926 const ConstantInt *HighCI = r.getHigh().toConstantInt();
927 GenericValue Low = getOperandValue(const_cast<ConstantInt*>(LowCI), SF);
928 GenericValue High = getOperandValue(const_cast<ConstantInt*>(HighCI), SF);
929 if (executeICMP_ULE(Low, CondVal, ElTy).IntVal != 0 &&
930 executeICMP_ULE(CondVal, High, ElTy).IntVal != 0) {
931 Dest = cast<BasicBlock>(i.getCaseSuccessor());
932 break;
933 }
934 }
Eli Friedmanbb5a7442011-09-29 20:21:17 +0000935 }
Chris Lattner09e93922003-04-22 20:34:47 +0000936 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
Chris Lattner77113b62003-05-10 20:21:16 +0000937 SwitchToNewBasicBlock(Dest, SF);
938}
939
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000940void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
941 ExecutionContext &SF = ECStack.back();
942 void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
943 SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
944}
945
946
Chris Lattner77113b62003-05-10 20:21:16 +0000947// SwitchToNewBasicBlock - This method is used to jump to a new basic block.
948// This function handles the actual updating of block and instruction iterators
949// as well as execution of all of the PHI nodes in the destination block.
950//
951// This method does this because all of the PHI nodes must be executed
952// atomically, reading their inputs before any of the results are updated. Not
953// doing this can cause problems if the PHI nodes depend on other PHI nodes for
954// their inputs. If the input PHI node is updated before it is read, incorrect
955// results can happen. Thus we use a two phase approach.
956//
957void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
958 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
959 SF.CurBB = Dest; // Update CurBB to branch destination
960 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
961
962 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
963
964 // Loop over all of the PHI nodes in the current block, reading their inputs.
965 std::vector<GenericValue> ResultValues;
966
967 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
968 // Search for the value corresponding to this previous bb...
969 int i = PN->getBasicBlockIndex(PrevBB);
970 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
971 Value *IncomingValue = PN->getIncomingValue(i);
Misha Brukmand1c881a2005-04-21 22:43:08 +0000972
Chris Lattner77113b62003-05-10 20:21:16 +0000973 // Save the incoming value for this PHI node...
974 ResultValues.push_back(getOperandValue(IncomingValue, SF));
975 }
976
977 // Now loop over all of the PHI nodes setting their values...
978 SF.CurInst = SF.CurBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000979 for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
980 PHINode *PN = cast<PHINode>(SF.CurInst);
Chris Lattner77113b62003-05-10 20:21:16 +0000981 SetValue(PN, ResultValues[i], SF);
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000982 }
Chris Lattner09e93922003-04-22 20:34:47 +0000983}
984
Chris Lattner92101ac2001-08-23 17:05:04 +0000985//===----------------------------------------------------------------------===//
Chris Lattner86660982001-08-27 05:16:50 +0000986// Memory Instruction Implementations
987//===----------------------------------------------------------------------===//
988
Victor Hernandez7b929da2009-10-23 21:09:37 +0000989void Interpreter::visitAllocaInst(AllocaInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +0000990 ExecutionContext &SF = ECStack.back();
991
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000992 Type *Ty = I.getType()->getElementType(); // Type to be allocated
Chris Lattner86660982001-08-27 05:16:50 +0000993
Chris Lattnercc82cc12002-04-28 21:57:33 +0000994 // Get the number of elements being allocated by the array...
Reid Spencerf9536332007-03-06 03:09:31 +0000995 unsigned NumElements =
996 getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
997
Duncan Sands777d2302009-05-09 07:06:46 +0000998 unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
Reid Spencerf9536332007-03-06 03:09:31 +0000999
Gabor Greif724441e2007-10-11 19:40:35 +00001000 // Avoid malloc-ing zero bytes, use max()...
1001 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
Chris Lattner86660982001-08-27 05:16:50 +00001002
1003 // Allocate enough memory to hold the type...
Reid Spencerf9536332007-03-06 03:09:31 +00001004 void *Memory = malloc(MemToAlloc);
1005
David Greeneef573a32010-01-05 01:27:23 +00001006 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
Chris Lattner79c5d3f2009-08-23 04:52:46 +00001007 << NumElements << " (Total: " << MemToAlloc << ") at "
1008 << uintptr_t(Memory) << '\n');
Chris Lattner9bffa732002-02-19 18:50:09 +00001009
Chris Lattnerfe11a972002-12-23 23:59:41 +00001010 GenericValue Result = PTOGV(Memory);
Chris Lattnerea38c0e2001-11-07 19:46:27 +00001011 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001012 SetValue(&I, Result, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001013
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001014 if (I.getOpcode() == Instruction::Alloca)
Chris Lattner9bffa732002-02-19 18:50:09 +00001015 ECStack.back().Allocas.add(Memory);
Chris Lattner86660982001-08-27 05:16:50 +00001016}
1017
Chris Lattnera34c5682002-08-27 22:33:45 +00001018// getElementOffset - The workhorse for getelementptr.
Chris Lattner95c3af52001-10-29 19:32:19 +00001019//
Chris Lattner4af6de82003-11-25 20:44:56 +00001020GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
Misha Brukmand1c881a2005-04-21 22:43:08 +00001021 gep_type_iterator E,
1022 ExecutionContext &SF) {
Duncan Sands1df98592010-02-16 11:11:14 +00001023 assert(Ptr->getType()->isPointerTy() &&
Chris Lattner95c3af52001-10-29 19:32:19 +00001024 "Cannot getElementOffset of a nonpointer type!");
1025
Reid Spencerf9536332007-03-06 03:09:31 +00001026 uint64_t Total = 0;
Chris Lattnera34c5682002-08-27 22:33:45 +00001027
1028 for (; I != E; ++I) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001029 if (StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner782b9392001-11-26 18:18:18 +00001030 const StructLayout *SLO = TD.getStructLayout(STy);
Misha Brukmand1c881a2005-04-21 22:43:08 +00001031
Reid Spencerb83eb642006-10-20 07:07:24 +00001032 const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1033 unsigned Index = unsigned(CPU->getZExtValue());
Misha Brukmand1c881a2005-04-21 22:43:08 +00001034
Reid Spencerf9536332007-03-06 03:09:31 +00001035 Total += SLO->getElementOffset(Index);
Chris Lattner4af6de82003-11-25 20:44:56 +00001036 } else {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001037 SequentialType *ST = cast<SequentialType>(*I);
Chris Lattner006a4a52003-02-25 21:14:59 +00001038 // Get the index number for the array... which must be long type...
Chris Lattner4af6de82003-11-25 20:44:56 +00001039 GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1040
Reid Spencera54b7cb2007-01-12 07:05:14 +00001041 int64_t Idx;
1042 unsigned BitWidth =
1043 cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
Reid Spencer23e28832007-01-18 01:25:42 +00001044 if (BitWidth == 32)
Reid Spencerf9536332007-03-06 03:09:31 +00001045 Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +00001046 else {
1047 assert(BitWidth == 64 && "Invalid index type for getelementptr");
Reid Spencerf9536332007-03-06 03:09:31 +00001048 Idx = (int64_t)IdxGV.IntVal.getZExtValue();
Chris Lattneraee7f212008-04-06 21:50:58 +00001049 }
Duncan Sands777d2302009-05-09 07:06:46 +00001050 Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
Brian Gaeke03e43dc2003-10-24 19:59:01 +00001051 }
Chris Lattner95c3af52001-10-29 19:32:19 +00001052 }
1053
Chris Lattnera34c5682002-08-27 22:33:45 +00001054 GenericValue Result;
Reid Spencerf9536332007-03-06 03:09:31 +00001055 Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
David Greeneef573a32010-01-05 01:27:23 +00001056 DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
Chris Lattnera34c5682002-08-27 22:33:45 +00001057 return Result;
Chris Lattner95c3af52001-10-29 19:32:19 +00001058}
1059
Chris Lattnerd7916e92003-05-10 21:22:39 +00001060void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
1061 ExecutionContext &SF = ECStack.back();
Owen Andersonfdca74c2009-06-26 16:46:15 +00001062 SetValue(&I, executeGEPOperation(I.getPointerOperand(),
Chris Lattner4af6de82003-11-25 20:44:56 +00001063 gep_type_begin(I), gep_type_end(I), SF), SF);
Chris Lattner95c3af52001-10-29 19:32:19 +00001064}
1065
Chris Lattnerd7916e92003-05-10 21:22:39 +00001066void Interpreter::visitLoadInst(LoadInst &I) {
1067 ExecutionContext &SF = ECStack.back();
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001068 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001069 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
Reid Spencere0929362007-03-03 08:38:04 +00001070 GenericValue Result;
Reid Spencere0929362007-03-03 08:38:04 +00001071 LoadValueFromMemory(Result, Ptr, I.getType());
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001072 SetValue(&I, Result, SF);
Chris Lattner663ceeb2008-07-08 17:25:49 +00001073 if (I.isVolatile() && PrintVolatile)
David Greeneef573a32010-01-05 01:27:23 +00001074 dbgs() << "Volatile load " << I;
Chris Lattner86660982001-08-27 05:16:50 +00001075}
1076
Chris Lattnerd7916e92003-05-10 21:22:39 +00001077void Interpreter::visitStoreInst(StoreInst &I) {
1078 ExecutionContext &SF = ECStack.back();
Chris Lattnerfddc7552002-10-15 20:34:05 +00001079 GenericValue Val = getOperandValue(I.getOperand(0), SF);
1080 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
Chris Lattnerfe11a972002-12-23 23:59:41 +00001081 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
Chris Lattner683d5da92002-10-26 01:57:15 +00001082 I.getOperand(0)->getType());
Chris Lattner663ceeb2008-07-08 17:25:49 +00001083 if (I.isVolatile() && PrintVolatile)
David Greeneef573a32010-01-05 01:27:23 +00001084 dbgs() << "Volatile store: " << I;
Chris Lattnerfddc7552002-10-15 20:34:05 +00001085}
1086
Chris Lattner86660982001-08-27 05:16:50 +00001087//===----------------------------------------------------------------------===//
Chris Lattner92101ac2001-08-23 17:05:04 +00001088// Miscellaneous Instruction Implementations
1089//===----------------------------------------------------------------------===//
1090
Brian Gaekefea483d2003-11-07 20:04:22 +00001091void Interpreter::visitCallSite(CallSite CS) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001092 ExecutionContext &SF = ECStack.back();
Chris Lattner73011782003-12-28 09:44:37 +00001093
1094 // Check to see if this is an intrinsic function call...
Reid Spencer4ccf4622007-04-16 21:50:40 +00001095 Function *F = CS.getCalledFunction();
Chris Lattnerf32a6a32009-10-29 05:26:09 +00001096 if (F && F->isDeclaration())
Chris Lattner73011782003-12-28 09:44:37 +00001097 switch (F->getIntrinsicID()) {
Brian Gaeke34562ba2004-01-14 06:02:53 +00001098 case Intrinsic::not_intrinsic:
1099 break;
Chris Lattner317201d2004-03-13 00:24:00 +00001100 case Intrinsic::vastart: { // va_start
Brian Gaeke9d20b712004-02-25 23:01:48 +00001101 GenericValue ArgIndex;
1102 ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1103 ArgIndex.UIntPairVal.second = 0;
1104 SetValue(CS.getInstruction(), ArgIndex, SF);
Chris Lattner73011782003-12-28 09:44:37 +00001105 return;
Brian Gaeke9d20b712004-02-25 23:01:48 +00001106 }
Chris Lattner317201d2004-03-13 00:24:00 +00001107 case Intrinsic::vaend: // va_end is a noop for the interpreter
Chris Lattner73011782003-12-28 09:44:37 +00001108 return;
Chris Lattner317201d2004-03-13 00:24:00 +00001109 case Intrinsic::vacopy: // va_copy: dest = src
Chris Lattner73011782003-12-28 09:44:37 +00001110 SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1111 return;
1112 default:
Brian Gaeke34562ba2004-01-14 06:02:53 +00001113 // If it is an unknown intrinsic function, use the intrinsic lowering
Chris Lattner73011782003-12-28 09:44:37 +00001114 // class to transform it into hopefully tasty LLVM code.
1115 //
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001116 BasicBlock::iterator me(CS.getInstruction());
Chris Lattner73011782003-12-28 09:44:37 +00001117 BasicBlock *Parent = CS.getInstruction()->getParent();
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001118 bool atBegin(Parent->begin() == me);
1119 if (!atBegin)
1120 --me;
Chris Lattner73011782003-12-28 09:44:37 +00001121 IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1122
1123 // Restore the CurInst pointer to the first instruction newly inserted, if
1124 // any.
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001125 if (atBegin) {
Chris Lattner73011782003-12-28 09:44:37 +00001126 SF.CurInst = Parent->begin();
1127 } else {
Chris Lattnerb8e237b2007-04-17 17:38:28 +00001128 SF.CurInst = me;
Chris Lattner73011782003-12-28 09:44:37 +00001129 ++SF.CurInst;
1130 }
Brian Gaekeb440dea2004-04-23 18:05:28 +00001131 return;
Chris Lattner73011782003-12-28 09:44:37 +00001132 }
1133
Reid Spencer4ccf4622007-04-16 21:50:40 +00001134
Brian Gaekefea483d2003-11-07 20:04:22 +00001135 SF.Caller = CS;
Chris Lattner02868352003-04-22 21:22:33 +00001136 std::vector<GenericValue> ArgVals;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001137 const unsigned NumArgs = SF.Caller.arg_size();
1138 ArgVals.reserve(NumArgs);
Reid Spencer4ccf4622007-04-16 21:50:40 +00001139 uint16_t pNum = 1;
Brian Gaekeae2495a2003-11-07 19:59:08 +00001140 for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
Reid Spencer4ccf4622007-04-16 21:50:40 +00001141 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
Brian Gaekeae2495a2003-11-07 19:59:08 +00001142 Value *V = *i;
1143 ArgVals.push_back(getOperandValue(V, SF));
Chris Lattner93780132003-01-13 00:58:52 +00001144 }
Chris Lattner365a76e2001-09-10 04:49:44 +00001145
Misha Brukmand1c881a2005-04-21 22:43:08 +00001146 // To handle indirect calls, we must get the pointer value from the argument
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001147 // and treat it as a function pointer.
Misha Brukmand1c881a2005-04-21 22:43:08 +00001148 GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
Chris Lattnerda82ed52003-05-08 16:18:31 +00001149 callFunction((Function*)GVTOP(SRC), ArgVals);
Chris Lattner92101ac2001-08-23 17:05:04 +00001150}
1151
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001152// auxilary function for shift operations
1153static unsigned getShiftAmount(uint64_t orgShiftAmount,
1154 llvm::APInt valueToShift) {
1155 unsigned valueWidth = valueToShift.getBitWidth();
1156 if (orgShiftAmount < (uint64_t)valueWidth)
1157 return orgShiftAmount;
1158 // according to the llvm documentation, if orgShiftAmount > valueWidth,
1159 // the result is undfeined. but we do shift by this rule:
1160 return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;
1161}
1162
1163
Reid Spencer832254e2007-02-02 02:16:23 +00001164void Interpreter::visitShl(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001165 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +00001166 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1167 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1168 GenericValue Dest;
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001169 const Type *Ty = I.getType();
1170
1171 if (Ty->isVectorTy()) {
1172 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1173 assert(src1Size == Src2.AggregateVal.size());
1174 for (unsigned i = 0; i < src1Size; i++) {
1175 GenericValue Result;
1176 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1177 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1178 Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1179 Dest.AggregateVal.push_back(Result);
1180 }
1181 } else {
1182 // scalar
1183 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1184 llvm::APInt valueToShift = Src1.IntVal;
1185 Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1186 }
1187
Brian Gaeke63438cc2003-12-11 00:22:59 +00001188 SetValue(&I, Dest, SF);
1189}
1190
Reid Spencer832254e2007-02-02 02:16:23 +00001191void Interpreter::visitLShr(BinaryOperator &I) {
Brian Gaeke63438cc2003-12-11 00:22:59 +00001192 ExecutionContext &SF = ECStack.back();
Brian Gaeke63438cc2003-12-11 00:22:59 +00001193 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1194 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1195 GenericValue Dest;
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001196 const Type *Ty = I.getType();
1197
1198 if (Ty->isVectorTy()) {
1199 uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1200 assert(src1Size == Src2.AggregateVal.size());
1201 for (unsigned i = 0; i < src1Size; i++) {
1202 GenericValue Result;
1203 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1204 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1205 Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1206 Dest.AggregateVal.push_back(Result);
1207 }
1208 } else {
1209 // scalar
1210 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1211 llvm::APInt valueToShift = Src1.IntVal;
1212 Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1213 }
1214
Reid Spencer3822ff52006-11-08 06:47:33 +00001215 SetValue(&I, Dest, SF);
1216}
1217
Reid Spencer832254e2007-02-02 02:16:23 +00001218void Interpreter::visitAShr(BinaryOperator &I) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001219 ExecutionContext &SF = ECStack.back();
Reid Spencer3822ff52006-11-08 06:47:33 +00001220 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1221 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
Chris Lattner9029cb82009-01-16 20:17:02 +00001222 GenericValue Dest;
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001223 const Type *Ty = I.getType();
1224
1225 if (Ty->isVectorTy()) {
1226 size_t src1Size = Src1.AggregateVal.size();
1227 assert(src1Size == Src2.AggregateVal.size());
1228 for (unsigned i = 0; i < src1Size; i++) {
1229 GenericValue Result;
1230 uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1231 llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1232 Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1233 Dest.AggregateVal.push_back(Result);
1234 }
1235 } else {
1236 // scalar
1237 uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1238 llvm::APInt valueToShift = Src1.IntVal;
1239 Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1240 }
1241
Chris Lattner0b12b5f2002-06-25 16:13:21 +00001242 SetValue(&I, Dest, SF);
Chris Lattner86660982001-08-27 05:16:50 +00001243}
1244
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001245GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001246 ExecutionContext &SF) {
Chris Lattnera34c5682002-08-27 22:33:45 +00001247 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001248 Type *SrcTy = SrcVal->getType();
1249 if (SrcTy->isVectorTy()) {
1250 Type *DstVecTy = DstTy->getScalarType();
1251 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1252 unsigned NumElts = Src.AggregateVal.size();
1253 // the sizes of src and dst vectors must be equal
1254 Dest.AggregateVal.resize(NumElts);
1255 for (unsigned i = 0; i < NumElts; i++)
1256 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);
1257 } else {
1258 IntegerType *DITy = cast<IntegerType>(DstTy);
1259 unsigned DBitWidth = DITy->getBitWidth();
1260 Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1261 }
Chris Lattnera34c5682002-08-27 22:33:45 +00001262 return Dest;
Chris Lattner86660982001-08-27 05:16:50 +00001263}
Chris Lattner92101ac2001-08-23 17:05:04 +00001264
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001265GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001266 ExecutionContext &SF) {
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001267 const Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001268 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001269 if (SrcTy->isVectorTy()) {
1270 const Type *DstVecTy = DstTy->getScalarType();
1271 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1272 unsigned size = Src.AggregateVal.size();
1273 // the sizes of src and dst vectors must be equal.
1274 Dest.AggregateVal.resize(size);
1275 for (unsigned i = 0; i < size; i++)
1276 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);
1277 } else {
1278 const IntegerType *DITy = cast<IntegerType>(DstTy);
1279 unsigned DBitWidth = DITy->getBitWidth();
1280 Dest.IntVal = Src.IntVal.sext(DBitWidth);
1281 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001282 return Dest;
1283}
1284
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001285GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001286 ExecutionContext &SF) {
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001287 const Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001288 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001289 if (SrcTy->isVectorTy()) {
1290 const Type *DstVecTy = DstTy->getScalarType();
1291 unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1292
1293 unsigned size = Src.AggregateVal.size();
1294 // the sizes of src and dst vectors must be equal.
1295 Dest.AggregateVal.resize(size);
1296 for (unsigned i = 0; i < size; i++)
1297 Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);
1298 } else {
1299 const IntegerType *DITy = cast<IntegerType>(DstTy);
1300 unsigned DBitWidth = DITy->getBitWidth();
1301 Dest.IntVal = Src.IntVal.zext(DBitWidth);
1302 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001303 return Dest;
1304}
1305
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001306GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001307 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001308 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001309
1310 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1311 assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1312 DstTy->getScalarType()->isFloatTy() &&
1313 "Invalid FPTrunc instruction");
1314
1315 unsigned size = Src.AggregateVal.size();
1316 // the sizes of src and dst vectors must be equal.
1317 Dest.AggregateVal.resize(size);
1318 for (unsigned i = 0; i < size; i++)
1319 Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1320 } else {
1321 assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1322 "Invalid FPTrunc instruction");
1323 Dest.FloatVal = (float)Src.DoubleVal;
1324 }
1325
Reid Spencera54b7cb2007-01-12 07:05:14 +00001326 return Dest;
1327}
1328
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001329GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001330 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001331 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001332
1333 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1334 assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1335 DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1336
1337 unsigned size = Src.AggregateVal.size();
1338 // the sizes of src and dst vectors must be equal.
1339 Dest.AggregateVal.resize(size);
1340 for (unsigned i = 0; i < size; i++)
1341 Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1342 } else {
1343 assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1344 "Invalid FPExt instruction");
1345 Dest.DoubleVal = (double)Src.FloatVal;
1346 }
1347
Reid Spencera54b7cb2007-01-12 07:05:14 +00001348 return Dest;
1349}
1350
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001351GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001352 ExecutionContext &SF) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001353 Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001354 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencere0929362007-03-03 08:38:04 +00001355
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001356 if (SrcTy->getTypeID() == Type::VectorTyID) {
1357 const Type *DstVecTy = DstTy->getScalarType();
1358 const Type *SrcVecTy = SrcTy->getScalarType();
1359 uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1360 unsigned size = Src.AggregateVal.size();
1361 // the sizes of src and dst vectors must be equal.
1362 Dest.AggregateVal.resize(size);
1363
1364 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1365 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1366 for (unsigned i = 0; i < size; i++)
1367 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1368 Src.AggregateVal[i].FloatVal, DBitWidth);
1369 } else {
1370 for (unsigned i = 0; i < size; i++)
1371 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1372 Src.AggregateVal[i].DoubleVal, DBitWidth);
1373 }
1374 } else {
1375 // scalar
1376 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1377 assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1378
1379 if (SrcTy->getTypeID() == Type::FloatTyID)
1380 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1381 else {
1382 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1383 }
1384 }
1385
Reid Spencera54b7cb2007-01-12 07:05:14 +00001386 return Dest;
1387}
1388
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001389GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001390 ExecutionContext &SF) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001391 Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001392 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencere0929362007-03-03 08:38:04 +00001393
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001394 if (SrcTy->getTypeID() == Type::VectorTyID) {
1395 const Type *DstVecTy = DstTy->getScalarType();
1396 const Type *SrcVecTy = SrcTy->getScalarType();
1397 uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1398 unsigned size = Src.AggregateVal.size();
1399 // the sizes of src and dst vectors must be equal
1400 Dest.AggregateVal.resize(size);
1401
1402 if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1403 assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1404 for (unsigned i = 0; i < size; i++)
1405 Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1406 Src.AggregateVal[i].FloatVal, DBitWidth);
1407 } else {
1408 for (unsigned i = 0; i < size; i++)
1409 Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1410 Src.AggregateVal[i].DoubleVal, DBitWidth);
1411 }
1412 } else {
1413 // scalar
1414 unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1415 assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1416
1417 if (SrcTy->getTypeID() == Type::FloatTyID)
1418 Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1419 else {
1420 Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1421 }
1422 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001423 return Dest;
1424}
1425
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001426GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001427 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001428 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencere0929362007-03-03 08:38:04 +00001429
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001430 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1431 const Type *DstVecTy = DstTy->getScalarType();
1432 unsigned size = Src.AggregateVal.size();
1433 // the sizes of src and dst vectors must be equal
1434 Dest.AggregateVal.resize(size);
1435
1436 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1437 assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1438 for (unsigned i = 0; i < size; i++)
1439 Dest.AggregateVal[i].FloatVal =
1440 APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal);
1441 } else {
1442 for (unsigned i = 0; i < size; i++)
1443 Dest.AggregateVal[i].DoubleVal =
1444 APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal);
1445 }
1446 } else {
1447 // scalar
1448 assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1449 if (DstTy->getTypeID() == Type::FloatTyID)
1450 Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
1451 else {
1452 Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
1453 }
1454 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001455 return Dest;
1456}
1457
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001458GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001459 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001460 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Reid Spencere0929362007-03-03 08:38:04 +00001461
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001462 if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1463 const Type *DstVecTy = DstTy->getScalarType();
1464 unsigned size = Src.AggregateVal.size();
1465 // the sizes of src and dst vectors must be equal
1466 Dest.AggregateVal.resize(size);
1467
1468 if (DstVecTy->getTypeID() == Type::FloatTyID) {
1469 assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1470 for (unsigned i = 0; i < size; i++)
1471 Dest.AggregateVal[i].FloatVal =
1472 APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal);
1473 } else {
1474 for (unsigned i = 0; i < size; i++)
1475 Dest.AggregateVal[i].DoubleVal =
1476 APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal);
1477 }
1478 } else {
1479 // scalar
1480 assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1481
1482 if (DstTy->getTypeID() == Type::FloatTyID)
1483 Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
1484 else {
1485 Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
1486 }
1487 }
1488
Reid Spencera54b7cb2007-01-12 07:05:14 +00001489 return Dest;
1490}
1491
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001492GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001493 ExecutionContext &SF) {
Reid Spencerf9536332007-03-06 03:09:31 +00001494 uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001495 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands1df98592010-02-16 11:11:14 +00001496 assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001497
Reid Spencerf9536332007-03-06 03:09:31 +00001498 Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001499 return Dest;
1500}
1501
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001502GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001503 ExecutionContext &SF) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001504 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Duncan Sands1df98592010-02-16 11:11:14 +00001505 assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
Reid Spencere0929362007-03-03 08:38:04 +00001506
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001507 uint32_t PtrSize = TD.getPointerSizeInBits();
Reid Spencer7553c342007-03-06 03:41:50 +00001508 if (PtrSize != Src.IntVal.getBitWidth())
Reid Spencer57631052007-03-06 03:46:41 +00001509 Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
Reid Spencer7553c342007-03-06 03:41:50 +00001510
Reid Spencer6bc63332007-04-26 18:19:35 +00001511 Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
Reid Spencera54b7cb2007-01-12 07:05:14 +00001512 return Dest;
1513}
1514
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001515GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
Reid Spencera54b7cb2007-01-12 07:05:14 +00001516 ExecutionContext &SF) {
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001517
1518 // This instruction supports bitwise conversion of vectors to integers and
1519 // to vectors of other types (as long as they have the same size)
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001520 Type *SrcTy = SrcVal->getType();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001521 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001522
1523 if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1524 (DstTy->getTypeID() == Type::VectorTyID)) {
1525 // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1526 // scalar src bitcast to vector dst
1527 bool isLittleEndian = TD.isLittleEndian();
1528 GenericValue TempDst, TempSrc, SrcVec;
1529 const Type *SrcElemTy;
1530 const Type *DstElemTy;
1531 unsigned SrcBitSize;
1532 unsigned DstBitSize;
1533 unsigned SrcNum;
1534 unsigned DstNum;
1535
1536 if (SrcTy->getTypeID() == Type::VectorTyID) {
1537 SrcElemTy = SrcTy->getScalarType();
1538 SrcBitSize = SrcTy->getScalarSizeInBits();
1539 SrcNum = Src.AggregateVal.size();
1540 SrcVec = Src;
1541 } else {
1542 // if src is scalar value, make it vector <1 x type>
1543 SrcElemTy = SrcTy;
1544 SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1545 SrcNum = 1;
1546 SrcVec.AggregateVal.push_back(Src);
1547 }
1548
1549 if (DstTy->getTypeID() == Type::VectorTyID) {
1550 DstElemTy = DstTy->getScalarType();
1551 DstBitSize = DstTy->getScalarSizeInBits();
1552 DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1553 } else {
1554 DstElemTy = DstTy;
1555 DstBitSize = DstTy->getPrimitiveSizeInBits();
1556 DstNum = 1;
1557 }
1558
1559 if (SrcNum * SrcBitSize != DstNum * DstBitSize)
Torok Edwinc23197a2009-07-14 16:55:14 +00001560 llvm_unreachable("Invalid BitCast");
Elena Demikhovskycf21d152013-08-05 12:17:06 +00001561
1562 // If src is floating point, cast to integer first.
1563 TempSrc.AggregateVal.resize(SrcNum);
1564 if (SrcElemTy->isFloatTy()) {
1565 for (unsigned i = 0; i < SrcNum; i++)
1566 TempSrc.AggregateVal[i].IntVal =
1567 APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);
1568
1569 } else if (SrcElemTy->isDoubleTy()) {
1570 for (unsigned i = 0; i < SrcNum; i++)
1571 TempSrc.AggregateVal[i].IntVal =
1572 APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);
1573 } else if (SrcElemTy->isIntegerTy()) {
1574 for (unsigned i = 0; i < SrcNum; i++)
1575 TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1576 } else {
1577 // Pointers are not allowed as the element type of vector.
1578 llvm_unreachable("Invalid Bitcast");
1579 }
1580
1581 // now TempSrc is integer type vector
1582 if (DstNum < SrcNum) {
1583 // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1584 unsigned Ratio = SrcNum / DstNum;
1585 unsigned SrcElt = 0;
1586 for (unsigned i = 0; i < DstNum; i++) {
1587 GenericValue Elt;
1588 Elt.IntVal = 0;
1589 Elt.IntVal = Elt.IntVal.zext(DstBitSize);
1590 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1591 for (unsigned j = 0; j < Ratio; j++) {
1592 APInt Tmp;
1593 Tmp = Tmp.zext(SrcBitSize);
1594 Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1595 Tmp = Tmp.zext(DstBitSize);
1596 Tmp = Tmp.shl(ShiftAmt);
1597 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1598 Elt.IntVal |= Tmp;
1599 }
1600 TempDst.AggregateVal.push_back(Elt);
1601 }
1602 } else {
1603 // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1604 unsigned Ratio = DstNum / SrcNum;
1605 for (unsigned i = 0; i < SrcNum; i++) {
1606 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1607 for (unsigned j = 0; j < Ratio; j++) {
1608 GenericValue Elt;
1609 Elt.IntVal = Elt.IntVal.zext(SrcBitSize);
1610 Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
1611 Elt.IntVal = Elt.IntVal.lshr(ShiftAmt);
1612 // it could be DstBitSize == SrcBitSize, so check it
1613 if (DstBitSize < SrcBitSize)
1614 Elt.IntVal = Elt.IntVal.trunc(DstBitSize);
1615 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1616 TempDst.AggregateVal.push_back(Elt);
1617 }
1618 }
1619 }
1620
1621 // convert result from integer to specified type
1622 if (DstTy->getTypeID() == Type::VectorTyID) {
1623 if (DstElemTy->isDoubleTy()) {
1624 Dest.AggregateVal.resize(DstNum);
1625 for (unsigned i = 0; i < DstNum; i++)
1626 Dest.AggregateVal[i].DoubleVal =
1627 TempDst.AggregateVal[i].IntVal.bitsToDouble();
1628 } else if (DstElemTy->isFloatTy()) {
1629 Dest.AggregateVal.resize(DstNum);
1630 for (unsigned i = 0; i < DstNum; i++)
1631 Dest.AggregateVal[i].FloatVal =
1632 TempDst.AggregateVal[i].IntVal.bitsToFloat();
1633 } else {
1634 Dest = TempDst;
1635 }
1636 } else {
1637 if (DstElemTy->isDoubleTy())
1638 Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1639 else if (DstElemTy->isFloatTy()) {
1640 Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1641 } else {
1642 Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1643 }
1644 }
1645 } else { // if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1646 // (DstTy->getTypeID() == Type::VectorTyID))
1647
1648 // scalar src bitcast to scalar dst
1649 if (DstTy->isPointerTy()) {
1650 assert(SrcTy->isPointerTy() && "Invalid BitCast");
1651 Dest.PointerVal = Src.PointerVal;
1652 } else if (DstTy->isIntegerTy()) {
1653 if (SrcTy->isFloatTy())
1654 Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1655 else if (SrcTy->isDoubleTy()) {
1656 Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1657 } else if (SrcTy->isIntegerTy()) {
1658 Dest.IntVal = Src.IntVal;
1659 } else {
1660 llvm_unreachable("Invalid BitCast");
1661 }
1662 } else if (DstTy->isFloatTy()) {
1663 if (SrcTy->isIntegerTy())
1664 Dest.FloatVal = Src.IntVal.bitsToFloat();
1665 else {
1666 Dest.FloatVal = Src.FloatVal;
1667 }
1668 } else if (DstTy->isDoubleTy()) {
1669 if (SrcTy->isIntegerTy())
1670 Dest.DoubleVal = Src.IntVal.bitsToDouble();
1671 else {
1672 Dest.DoubleVal = Src.DoubleVal;
1673 }
1674 } else {
1675 llvm_unreachable("Invalid Bitcast");
1676 }
1677 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00001678
1679 return Dest;
1680}
1681
1682void Interpreter::visitTruncInst(TruncInst &I) {
Chris Lattnerd7916e92003-05-10 21:22:39 +00001683 ExecutionContext &SF = ECStack.back();
Reid Spencera54b7cb2007-01-12 07:05:14 +00001684 SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1685}
1686
1687void Interpreter::visitSExtInst(SExtInst &I) {
1688 ExecutionContext &SF = ECStack.back();
1689 SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1690}
1691
1692void Interpreter::visitZExtInst(ZExtInst &I) {
1693 ExecutionContext &SF = ECStack.back();
1694 SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1695}
1696
1697void Interpreter::visitFPTruncInst(FPTruncInst &I) {
1698 ExecutionContext &SF = ECStack.back();
1699 SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1700}
1701
1702void Interpreter::visitFPExtInst(FPExtInst &I) {
1703 ExecutionContext &SF = ECStack.back();
1704 SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1705}
1706
1707void Interpreter::visitUIToFPInst(UIToFPInst &I) {
1708 ExecutionContext &SF = ECStack.back();
1709 SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1710}
1711
1712void Interpreter::visitSIToFPInst(SIToFPInst &I) {
1713 ExecutionContext &SF = ECStack.back();
1714 SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1715}
1716
1717void Interpreter::visitFPToUIInst(FPToUIInst &I) {
1718 ExecutionContext &SF = ECStack.back();
1719 SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1720}
1721
1722void Interpreter::visitFPToSIInst(FPToSIInst &I) {
1723 ExecutionContext &SF = ECStack.back();
1724 SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1725}
1726
1727void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
1728 ExecutionContext &SF = ECStack.back();
1729 SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1730}
1731
1732void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
1733 ExecutionContext &SF = ECStack.back();
1734 SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1735}
1736
1737void Interpreter::visitBitCastInst(BitCastInst &I) {
1738 ExecutionContext &SF = ECStack.back();
1739 SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
Chris Lattnera34c5682002-08-27 22:33:45 +00001740}
Chris Lattner92101ac2001-08-23 17:05:04 +00001741
Brian Gaekec1a2be12003-11-07 21:20:47 +00001742#define IMPLEMENT_VAARG(TY) \
1743 case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1744
1745void Interpreter::visitVAArgInst(VAArgInst &I) {
1746 ExecutionContext &SF = ECStack.back();
1747
Brian Gaeke9d20b712004-02-25 23:01:48 +00001748 // Get the incoming valist parameter. LLI treats the valist as a
1749 // (ec-stack-depth var-arg-index) pair.
Brian Gaekec1a2be12003-11-07 21:20:47 +00001750 GenericValue VAList = getOperandValue(I.getOperand(0), SF);
Brian Gaeke9d20b712004-02-25 23:01:48 +00001751 GenericValue Dest;
1752 GenericValue Src = ECStack[VAList.UIntPairVal.first]
Reid Spencerf9536332007-03-06 03:09:31 +00001753 .VarArgs[VAList.UIntPairVal.second];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001754 Type *Ty = I.getType();
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001755 switch (Ty->getTypeID()) {
Jim Grosbachcce07c92013-01-31 19:46:59 +00001756 case Type::IntegerTyID:
1757 Dest.IntVal = Src.IntVal;
Jim Grosbachcce07c92013-01-31 19:46:59 +00001758 break;
Jim Grosbach9f285c62013-02-01 18:57:06 +00001759 IMPLEMENT_VAARG(Pointer);
1760 IMPLEMENT_VAARG(Float);
1761 IMPLEMENT_VAARG(Double);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001762 default:
David Greeneef573a32010-01-05 01:27:23 +00001763 dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001764 llvm_unreachable(0);
Brian Gaekec1a2be12003-11-07 21:20:47 +00001765 }
Misha Brukmand1c881a2005-04-21 22:43:08 +00001766
Brian Gaekec1a2be12003-11-07 21:20:47 +00001767 // Set the Value of this Instruction.
1768 SetValue(&I, Dest, SF);
Andrew Lenharth558bc882005-06-18 18:34:52 +00001769
1770 // Move the pointer to the next vararg.
1771 ++VAList.UIntPairVal.second;
Brian Gaekec1a2be12003-11-07 21:20:47 +00001772}
1773
Nadav Rotem953783e2013-04-01 15:53:30 +00001774void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
1775 ExecutionContext &SF = ECStack.back();
1776 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1777 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1778 GenericValue Dest;
1779
1780 Type *Ty = I.getType();
1781 const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1782
1783 if(Src1.AggregateVal.size() > indx) {
1784 switch (Ty->getTypeID()) {
1785 default:
1786 dbgs() << "Unhandled destination type for extractelement instruction: "
1787 << *Ty << "\n";
1788 llvm_unreachable(0);
1789 break;
1790 case Type::IntegerTyID:
1791 Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1792 break;
1793 case Type::FloatTyID:
1794 Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1795 break;
1796 case Type::DoubleTyID:
1797 Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1798 break;
1799 }
1800 } else {
1801 dbgs() << "Invalid index in extractelement instruction\n";
1802 }
1803
1804 SetValue(&I, Dest, SF);
1805}
1806
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +00001807void Interpreter::visitInsertElementInst(InsertElementInst &I) {
1808 ExecutionContext &SF = ECStack.back();
1809 Type *Ty = I.getType();
1810
1811 if(!(Ty->isVectorTy()) )
1812 llvm_unreachable("Unhandled dest type for insertelement instruction");
1813
1814 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1815 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1816 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1817 GenericValue Dest;
1818
1819 Type *TyContained = Ty->getContainedType(0);
1820
1821 const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
1822 Dest.AggregateVal = Src1.AggregateVal;
1823
1824 if(Src1.AggregateVal.size() <= indx)
1825 llvm_unreachable("Invalid index in insertelement instruction");
1826 switch (TyContained->getTypeID()) {
1827 default:
1828 llvm_unreachable("Unhandled dest type for insertelement instruction");
1829 case Type::IntegerTyID:
1830 Dest.AggregateVal[indx].IntVal = Src2.IntVal;
1831 break;
1832 case Type::FloatTyID:
1833 Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
1834 break;
1835 case Type::DoubleTyID:
1836 Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
1837 break;
1838 }
1839 SetValue(&I, Dest, SF);
1840}
1841
1842void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){
1843 ExecutionContext &SF = ECStack.back();
1844
1845 Type *Ty = I.getType();
1846 if(!(Ty->isVectorTy()))
1847 llvm_unreachable("Unhandled dest type for shufflevector instruction");
1848
1849 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1850 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1851 GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1852 GenericValue Dest;
1853
1854 // There is no need to check types of src1 and src2, because the compiled
1855 // bytecode can't contain different types for src1 and src2 for a
1856 // shufflevector instruction.
1857
1858 Type *TyContained = Ty->getContainedType(0);
1859 unsigned src1Size = (unsigned)Src1.AggregateVal.size();
1860 unsigned src2Size = (unsigned)Src2.AggregateVal.size();
1861 unsigned src3Size = (unsigned)Src3.AggregateVal.size();
1862
1863 Dest.AggregateVal.resize(src3Size);
1864
1865 switch (TyContained->getTypeID()) {
1866 default:
1867 llvm_unreachable("Unhandled dest type for insertelement instruction");
1868 break;
1869 case Type::IntegerTyID:
1870 for( unsigned i=0; i<src3Size; i++) {
1871 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1872 if(j < src1Size)
1873 Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
1874 else if(j < src1Size + src2Size)
1875 Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
1876 else
1877 // The selector may not be greater than sum of lengths of first and
1878 // second operands and llasm should not allow situation like
1879 // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
1880 // <2 x i32> < i32 0, i32 5 >,
1881 // where i32 5 is invalid, but let it be additional check here:
1882 llvm_unreachable("Invalid mask in shufflevector instruction");
1883 }
1884 break;
1885 case Type::FloatTyID:
1886 for( unsigned i=0; i<src3Size; i++) {
1887 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1888 if(j < src1Size)
1889 Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
1890 else if(j < src1Size + src2Size)
1891 Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
1892 else
1893 llvm_unreachable("Invalid mask in shufflevector instruction");
1894 }
1895 break;
1896 case Type::DoubleTyID:
1897 for( unsigned i=0; i<src3Size; i++) {
1898 unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1899 if(j < src1Size)
1900 Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
1901 else if(j < src1Size + src2Size)
1902 Dest.AggregateVal[i].DoubleVal =
1903 Src2.AggregateVal[j-src1Size].DoubleVal;
1904 else
1905 llvm_unreachable("Invalid mask in shufflevector instruction");
1906 }
1907 break;
1908 }
1909 SetValue(&I, Dest, SF);
1910}
1911
Reid Spencere1aa0662007-03-03 06:22:22 +00001912GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1913 ExecutionContext &SF) {
1914 switch (CE->getOpcode()) {
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +00001915 case Instruction::Trunc:
Reid Spencere1aa0662007-03-03 06:22:22 +00001916 return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1917 case Instruction::ZExt:
1918 return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1919 case Instruction::SExt:
1920 return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1921 case Instruction::FPTrunc:
1922 return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1923 case Instruction::FPExt:
1924 return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1925 case Instruction::UIToFP:
1926 return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1927 case Instruction::SIToFP:
1928 return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1929 case Instruction::FPToUI:
1930 return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1931 case Instruction::FPToSI:
1932 return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1933 case Instruction::PtrToInt:
1934 return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1935 case Instruction::IntToPtr:
1936 return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1937 case Instruction::BitCast:
1938 return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1939 case Instruction::GetElementPtr:
1940 return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1941 gep_type_end(CE), SF);
Reid Spencere0929362007-03-03 08:38:04 +00001942 case Instruction::FCmp:
1943 case Instruction::ICmp:
1944 return executeCmpInst(CE->getPredicate(),
1945 getOperandValue(CE->getOperand(0), SF),
1946 getOperandValue(CE->getOperand(1), SF),
1947 CE->getOperand(0)->getType());
1948 case Instruction::Select:
1949 return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
1950 getOperandValue(CE->getOperand(1), SF),
Elena Demikhovsky4ca0ce22013-09-02 06:40:09 +00001951 getOperandValue(CE->getOperand(2), SF),
1952 CE->getOperand(0)->getType());
Reid Spencere1aa0662007-03-03 06:22:22 +00001953 default :
1954 break;
1955 }
Reid Spencere0929362007-03-03 08:38:04 +00001956
1957 // The cases below here require a GenericValue parameter for the result
1958 // so we initialize one, compute it and then return it.
Reid Spencerf9536332007-03-06 03:09:31 +00001959 GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
1960 GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
Reid Spencere1aa0662007-03-03 06:22:22 +00001961 GenericValue Dest;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001962 Type * Ty = CE->getOperand(0)->getType();
Reid Spencere1aa0662007-03-03 06:22:22 +00001963 switch (CE->getOpcode()) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001964 case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
1965 case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
1966 case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
1967 case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
1968 case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
1969 case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
Reid Spencerf9536332007-03-06 03:09:31 +00001970 case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
1971 case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
1972 case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
1973 case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
1974 case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
1975 case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001976 case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
1977 case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
1978 case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
Reid Spencerf9536332007-03-06 03:09:31 +00001979 case Instruction::Shl:
1980 Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
1981 break;
1982 case Instruction::LShr:
1983 Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
1984 break;
1985 case Instruction::AShr:
1986 Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
1987 break;
Reid Spencere1aa0662007-03-03 06:22:22 +00001988 default:
David Greeneef573a32010-01-05 01:27:23 +00001989 dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
Ahmed Charlesb0934ab2012-02-19 11:37:01 +00001990 llvm_unreachable("Unhandled ConstantExpr");
Reid Spencere1aa0662007-03-03 06:22:22 +00001991 }
Reid Spencere0929362007-03-03 08:38:04 +00001992 return Dest;
Reid Spencere1aa0662007-03-03 06:22:22 +00001993}
1994
1995GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
1996 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
1997 return getConstantExprValue(CE, SF);
1998 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
1999 return getConstantValue(CPV);
2000 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2001 return PTOGV(getPointerToGlobal(GV));
2002 } else {
2003 return SF.Values[V];
2004 }
2005}
2006
Chris Lattner92101ac2001-08-23 17:05:04 +00002007//===----------------------------------------------------------------------===//
2008// Dispatch and Execution Code
2009//===----------------------------------------------------------------------===//
2010
Chris Lattner92101ac2001-08-23 17:05:04 +00002011//===----------------------------------------------------------------------===//
Chris Lattnerda82ed52003-05-08 16:18:31 +00002012// callFunction - Execute the specified function...
Chris Lattner92101ac2001-08-23 17:05:04 +00002013//
Chris Lattnerda82ed52003-05-08 16:18:31 +00002014void Interpreter::callFunction(Function *F,
2015 const std::vector<GenericValue> &ArgVals) {
Misha Brukmand1c881a2005-04-21 22:43:08 +00002016 assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
2017 ECStack.back().Caller.arg_size() == ArgVals.size()) &&
2018 "Incorrect number of arguments passed into function call!");
Chris Lattner63bd6132003-09-17 17:26:22 +00002019 // Make a new stack frame... and fill it in.
2020 ECStack.push_back(ExecutionContext());
2021 ExecutionContext &StackFrame = ECStack.back();
Chris Lattnerda82ed52003-05-08 16:18:31 +00002022 StackFrame.CurFunction = F;
Brian Gaekeaf955ba2003-11-07 05:22:49 +00002023
2024 // Special handling for external functions.
Reid Spencer5cbf9852007-01-30 20:08:39 +00002025 if (F->isDeclaration()) {
Brian Gaekeaf955ba2003-11-07 05:22:49 +00002026 GenericValue Result = callExternalFunction (F, ArgVals);
2027 // Simulate a 'ret' instruction of the appropriate type.
2028 popStackAndReturnValueToCaller (F->getReturnType (), Result);
2029 return;
2030 }
2031
2032 // Get pointers to first LLVM BB & Instruction in function.
Chris Lattnercdf51782003-05-08 16:06:52 +00002033 StackFrame.CurBB = F->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00002034 StackFrame.CurInst = StackFrame.CurBB->begin();
Chris Lattner92101ac2001-08-23 17:05:04 +00002035
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00002036 // Run through the function arguments and initialize their values...
Chris Lattnere4d5c442005-03-15 04:54:21 +00002037 assert((ArgVals.size() == F->arg_size() ||
Misha Brukmand1c881a2005-04-21 22:43:08 +00002038 (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00002039 "Invalid number of values passed to function invocation!");
Chris Lattnercdf51782003-05-08 16:06:52 +00002040
2041 // Handle non-varargs arguments...
Chris Lattner365a76e2001-09-10 04:49:44 +00002042 unsigned i = 0;
Reid Spencer4ccf4622007-04-16 21:50:40 +00002043 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
2044 AI != E; ++AI, ++i)
Chris Lattner0b12b5f2002-06-25 16:13:21 +00002045 SetValue(AI, ArgVals[i], StackFrame);
Chris Lattnercdf51782003-05-08 16:06:52 +00002046
2047 // Handle varargs arguments...
2048 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
Chris Lattner92101ac2001-08-23 17:05:04 +00002049}
2050
Reid Spencer951418b2007-05-16 02:05:13 +00002051
Chris Lattner92101ac2001-08-23 17:05:04 +00002052void Interpreter::run() {
Brian Gaeke9ad671d2003-09-04 23:15:40 +00002053 while (!ECStack.empty()) {
Brian Gaeke03e43dc2003-10-24 19:59:01 +00002054 // Interpret a single instruction & increment the "PC".
2055 ExecutionContext &SF = ECStack.back(); // Current stack frame
2056 Instruction &I = *SF.CurInst++; // Increment before execute
Misha Brukmand1c881a2005-04-21 22:43:08 +00002057
Brian Gaeke03e43dc2003-10-24 19:59:01 +00002058 // Track the number of dynamic instructions executed.
2059 ++NumDynamicInsts;
Chris Lattner92101ac2001-08-23 17:05:04 +00002060
David Greeneef573a32010-01-05 01:27:23 +00002061 DEBUG(dbgs() << "About to interpret: " << I);
Brian Gaeke03e43dc2003-10-24 19:59:01 +00002062 visit(I); // Dispatch to one of the visit* methods...
Chris Lattner27725bf2007-09-21 18:30:39 +00002063#if 0
2064 // This is not safe, as visiting the instruction could lower it and free I.
Chris Lattnerbdff5482009-08-23 04:37:46 +00002065DEBUG(
Reid Spencer951418b2007-05-16 02:05:13 +00002066 if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
2067 I.getType() != Type::VoidTy) {
David Greeneef573a32010-01-05 01:27:23 +00002068 dbgs() << " --> ";
Chris Lattner27725bf2007-09-21 18:30:39 +00002069 const GenericValue &Val = SF.Values[&I];
2070 switch (I.getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002071 default: llvm_unreachable("Invalid GenericValue Type");
David Greeneef573a32010-01-05 01:27:23 +00002072 case Type::VoidTyID: dbgs() << "void"; break;
2073 case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
2074 case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
2075 case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
Chris Lattner27725bf2007-09-21 18:30:39 +00002076 break;
2077 case Type::IntegerTyID:
David Greeneef573a32010-01-05 01:27:23 +00002078 dbgs() << "i" << Val.IntVal.getBitWidth() << " "
Chris Lattnerbdff5482009-08-23 04:37:46 +00002079 << Val.IntVal.toStringUnsigned(10)
2080 << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
Chris Lattner27725bf2007-09-21 18:30:39 +00002081 break;
2082 }
Chris Lattnerbdff5482009-08-23 04:37:46 +00002083 });
Chris Lattner27725bf2007-09-21 18:30:39 +00002084#endif
Chris Lattner92101ac2001-08-23 17:05:04 +00002085 }
2086}