LLVM  9.0.0svn
Execution.cpp
Go to the documentation of this file.
1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the actual instruction interpreter.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Interpreter.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Instructions.h"
22 #include "llvm/Support/Debug.h"
26 #include <algorithm>
27 #include <cmath>
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "interpreter"
31 
32 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
33 
34 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
35  cl::desc("make the interpreter print every volatile load and store"));
36 
37 //===----------------------------------------------------------------------===//
38 // Various Helper Functions
39 //===----------------------------------------------------------------------===//
40 
41 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
42  SF.Values[V] = Val;
43 }
44 
45 //===----------------------------------------------------------------------===//
46 // Binary Instruction Implementations
47 //===----------------------------------------------------------------------===//
48 
49 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
50  case Type::TY##TyID: \
51  Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
52  break
53 
54 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
55  GenericValue Src2, Type *Ty) {
56  switch (Ty->getTypeID()) {
57  IMPLEMENT_BINARY_OPERATOR(+, Float);
58  IMPLEMENT_BINARY_OPERATOR(+, Double);
59  default:
60  dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
61  llvm_unreachable(nullptr);
62  }
63 }
64 
65 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
66  GenericValue Src2, Type *Ty) {
67  switch (Ty->getTypeID()) {
68  IMPLEMENT_BINARY_OPERATOR(-, Float);
69  IMPLEMENT_BINARY_OPERATOR(-, Double);
70  default:
71  dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
72  llvm_unreachable(nullptr);
73  }
74 }
75 
76 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
77  GenericValue Src2, Type *Ty) {
78  switch (Ty->getTypeID()) {
79  IMPLEMENT_BINARY_OPERATOR(*, Float);
80  IMPLEMENT_BINARY_OPERATOR(*, Double);
81  default:
82  dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
83  llvm_unreachable(nullptr);
84  }
85 }
86 
87 static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
88  GenericValue Src2, Type *Ty) {
89  switch (Ty->getTypeID()) {
90  IMPLEMENT_BINARY_OPERATOR(/, Float);
91  IMPLEMENT_BINARY_OPERATOR(/, Double);
92  default:
93  dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
94  llvm_unreachable(nullptr);
95  }
96 }
97 
98 static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
99  GenericValue Src2, Type *Ty) {
100  switch (Ty->getTypeID()) {
101  case Type::FloatTyID:
102  Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
103  break;
104  case Type::DoubleTyID:
105  Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
106  break;
107  default:
108  dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
109  llvm_unreachable(nullptr);
110  }
111 }
112 
113 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
114  case Type::IntegerTyID: \
115  Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
116  break;
117 
118 #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \
119  case Type::VectorTyID: { \
120  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
121  Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
122  for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
123  Dest.AggregateVal[_i].IntVal = APInt(1, \
124  Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
125  } break;
126 
127 // Handle pointers specially because they must be compared with only as much
128 // width as the host has. We _do not_ want to be comparing 64 bit values when
129 // running on a 32-bit target, otherwise the upper 32 bits might mess up
130 // comparisons if they contain garbage.
131 #define IMPLEMENT_POINTER_ICMP(OP) \
132  case Type::PointerTyID: \
133  Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
134  (void*)(intptr_t)Src2.PointerVal); \
135  break;
136 
138  Type *Ty) {
139  GenericValue Dest;
140  switch (Ty->getTypeID()) {
141  IMPLEMENT_INTEGER_ICMP(eq,Ty);
144  default:
145  dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
146  llvm_unreachable(nullptr);
147  }
148  return Dest;
149 }
150 
152  Type *Ty) {
153  GenericValue Dest;
154  switch (Ty->getTypeID()) {
155  IMPLEMENT_INTEGER_ICMP(ne,Ty);
158  default:
159  dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
160  llvm_unreachable(nullptr);
161  }
162  return Dest;
163 }
164 
166  Type *Ty) {
167  GenericValue Dest;
168  switch (Ty->getTypeID()) {
169  IMPLEMENT_INTEGER_ICMP(ult,Ty);
172  default:
173  dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
174  llvm_unreachable(nullptr);
175  }
176  return Dest;
177 }
178 
180  Type *Ty) {
181  GenericValue Dest;
182  switch (Ty->getTypeID()) {
183  IMPLEMENT_INTEGER_ICMP(slt,Ty);
186  default:
187  dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
188  llvm_unreachable(nullptr);
189  }
190  return Dest;
191 }
192 
194  Type *Ty) {
195  GenericValue Dest;
196  switch (Ty->getTypeID()) {
197  IMPLEMENT_INTEGER_ICMP(ugt,Ty);
200  default:
201  dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
202  llvm_unreachable(nullptr);
203  }
204  return Dest;
205 }
206 
208  Type *Ty) {
209  GenericValue Dest;
210  switch (Ty->getTypeID()) {
211  IMPLEMENT_INTEGER_ICMP(sgt,Ty);
214  default:
215  dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
216  llvm_unreachable(nullptr);
217  }
218  return Dest;
219 }
220 
222  Type *Ty) {
223  GenericValue Dest;
224  switch (Ty->getTypeID()) {
225  IMPLEMENT_INTEGER_ICMP(ule,Ty);
228  default:
229  dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
230  llvm_unreachable(nullptr);
231  }
232  return Dest;
233 }
234 
236  Type *Ty) {
237  GenericValue Dest;
238  switch (Ty->getTypeID()) {
239  IMPLEMENT_INTEGER_ICMP(sle,Ty);
242  default:
243  dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
244  llvm_unreachable(nullptr);
245  }
246  return Dest;
247 }
248 
250  Type *Ty) {
251  GenericValue Dest;
252  switch (Ty->getTypeID()) {
253  IMPLEMENT_INTEGER_ICMP(uge,Ty);
256  default:
257  dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
258  llvm_unreachable(nullptr);
259  }
260  return Dest;
261 }
262 
264  Type *Ty) {
265  GenericValue Dest;
266  switch (Ty->getTypeID()) {
267  IMPLEMENT_INTEGER_ICMP(sge,Ty);
270  default:
271  dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
272  llvm_unreachable(nullptr);
273  }
274  return Dest;
275 }
276 
278  ExecutionContext &SF = ECStack.back();
279  Type *Ty = I.getOperand(0)->getType();
280  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
281  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
282  GenericValue R; // Result
283 
284  switch (I.getPredicate()) {
285  case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
286  case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
287  case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
288  case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
289  case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
290  case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
291  case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
292  case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
293  case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
294  case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
295  default:
296  dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
297  llvm_unreachable(nullptr);
298  }
299 
300  SetValue(&I, R, SF);
301 }
302 
303 #define IMPLEMENT_FCMP(OP, TY) \
304  case Type::TY##TyID: \
305  Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
306  break
307 
308 #define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \
309  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \
310  Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \
311  for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \
312  Dest.AggregateVal[_i].IntVal = APInt(1, \
313  Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
314  break;
315 
316 #define IMPLEMENT_VECTOR_FCMP(OP) \
317  case Type::VectorTyID: \
318  if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \
319  IMPLEMENT_VECTOR_FCMP_T(OP, Float); \
320  } else { \
321  IMPLEMENT_VECTOR_FCMP_T(OP, Double); \
322  }
323 
325  Type *Ty) {
326  GenericValue Dest;
327  switch (Ty->getTypeID()) {
328  IMPLEMENT_FCMP(==, Float);
329  IMPLEMENT_FCMP(==, Double);
331  default:
332  dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
333  llvm_unreachable(nullptr);
334  }
335  return Dest;
336 }
337 
338 #define IMPLEMENT_SCALAR_NANS(TY, X,Y) \
339  if (TY->isFloatTy()) { \
340  if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
341  Dest.IntVal = APInt(1,false); \
342  return Dest; \
343  } \
344  } else { \
345  if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
346  Dest.IntVal = APInt(1,false); \
347  return Dest; \
348  } \
349  }
350 
351 #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \
352  assert(X.AggregateVal.size() == Y.AggregateVal.size()); \
353  Dest.AggregateVal.resize( X.AggregateVal.size() ); \
354  for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \
355  if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \
356  Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \
357  Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \
358  else { \
359  Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \
360  } \
361  }
362 
363 #define MASK_VECTOR_NANS(TY, X,Y, FLAG) \
364  if (TY->isVectorTy()) { \
365  if (cast<VectorType>(TY)->getElementType()->isFloatTy()) { \
366  MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \
367  } else { \
368  MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \
369  } \
370  } \
371 
372 
373 
375  Type *Ty)
376 {
377  GenericValue Dest;
378  // if input is scalar value and Src1 or Src2 is NaN return false
379  IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
380  // if vector input detect NaNs and fill mask
381  MASK_VECTOR_NANS(Ty, Src1, Src2, false)
382  GenericValue DestMask = Dest;
383  switch (Ty->getTypeID()) {
384  IMPLEMENT_FCMP(!=, Float);
385  IMPLEMENT_FCMP(!=, Double);
387  default:
388  dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
389  llvm_unreachable(nullptr);
390  }
391  // in vector case mask out NaN elements
392  if (Ty->isVectorTy())
393  for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
394  if (DestMask.AggregateVal[_i].IntVal == false)
395  Dest.AggregateVal[_i].IntVal = APInt(1,false);
396 
397  return Dest;
398 }
399 
401  Type *Ty) {
402  GenericValue Dest;
403  switch (Ty->getTypeID()) {
404  IMPLEMENT_FCMP(<=, Float);
405  IMPLEMENT_FCMP(<=, Double);
407  default:
408  dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
409  llvm_unreachable(nullptr);
410  }
411  return Dest;
412 }
413 
415  Type *Ty) {
416  GenericValue Dest;
417  switch (Ty->getTypeID()) {
418  IMPLEMENT_FCMP(>=, Float);
419  IMPLEMENT_FCMP(>=, Double);
421  default:
422  dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
423  llvm_unreachable(nullptr);
424  }
425  return Dest;
426 }
427 
429  Type *Ty) {
430  GenericValue Dest;
431  switch (Ty->getTypeID()) {
432  IMPLEMENT_FCMP(<, Float);
433  IMPLEMENT_FCMP(<, Double);
435  default:
436  dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
437  llvm_unreachable(nullptr);
438  }
439  return Dest;
440 }
441 
443  Type *Ty) {
444  GenericValue Dest;
445  switch (Ty->getTypeID()) {
446  IMPLEMENT_FCMP(>, Float);
447  IMPLEMENT_FCMP(>, Double);
449  default:
450  dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
451  llvm_unreachable(nullptr);
452  }
453  return Dest;
454 }
455 
456 #define IMPLEMENT_UNORDERED(TY, X,Y) \
457  if (TY->isFloatTy()) { \
458  if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
459  Dest.IntVal = APInt(1,true); \
460  return Dest; \
461  } \
462  } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
463  Dest.IntVal = APInt(1,true); \
464  return Dest; \
465  }
466 
467 #define IMPLEMENT_VECTOR_UNORDERED(TY, X, Y, FUNC) \
468  if (TY->isVectorTy()) { \
469  GenericValue DestMask = Dest; \
470  Dest = FUNC(Src1, Src2, Ty); \
471  for (size_t _i = 0; _i < Src1.AggregateVal.size(); _i++) \
472  if (DestMask.AggregateVal[_i].IntVal == true) \
473  Dest.AggregateVal[_i].IntVal = APInt(1, true); \
474  return Dest; \
475  }
476 
478  Type *Ty) {
479  GenericValue Dest;
480  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
481  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
483  return executeFCMP_OEQ(Src1, Src2, Ty);
484 
485 }
486 
488  Type *Ty) {
489  GenericValue Dest;
490  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
491  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
493  return executeFCMP_ONE(Src1, Src2, Ty);
494 }
495 
497  Type *Ty) {
498  GenericValue Dest;
499  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
500  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
502  return executeFCMP_OLE(Src1, Src2, Ty);
503 }
504 
506  Type *Ty) {
507  GenericValue Dest;
508  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
509  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
511  return executeFCMP_OGE(Src1, Src2, Ty);
512 }
513 
515  Type *Ty) {
516  GenericValue Dest;
517  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
518  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
520  return executeFCMP_OLT(Src1, Src2, Ty);
521 }
522 
524  Type *Ty) {
525  GenericValue Dest;
526  IMPLEMENT_UNORDERED(Ty, Src1, Src2)
527  MASK_VECTOR_NANS(Ty, Src1, Src2, true)
529  return executeFCMP_OGT(Src1, Src2, Ty);
530 }
531 
533  Type *Ty) {
534  GenericValue Dest;
535  if(Ty->isVectorTy()) {
536  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
537  Dest.AggregateVal.resize( Src1.AggregateVal.size() );
538  if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
539  for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
540  Dest.AggregateVal[_i].IntVal = APInt(1,
541  ( (Src1.AggregateVal[_i].FloatVal ==
542  Src1.AggregateVal[_i].FloatVal) &&
543  (Src2.AggregateVal[_i].FloatVal ==
544  Src2.AggregateVal[_i].FloatVal)));
545  } else {
546  for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
547  Dest.AggregateVal[_i].IntVal = APInt(1,
548  ( (Src1.AggregateVal[_i].DoubleVal ==
549  Src1.AggregateVal[_i].DoubleVal) &&
550  (Src2.AggregateVal[_i].DoubleVal ==
551  Src2.AggregateVal[_i].DoubleVal)));
552  }
553  } else if (Ty->isFloatTy())
554  Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
555  Src2.FloatVal == Src2.FloatVal));
556  else {
557  Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
558  Src2.DoubleVal == Src2.DoubleVal));
559  }
560  return Dest;
561 }
562 
564  Type *Ty) {
565  GenericValue Dest;
566  if(Ty->isVectorTy()) {
567  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
568  Dest.AggregateVal.resize( Src1.AggregateVal.size() );
569  if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
570  for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
571  Dest.AggregateVal[_i].IntVal = APInt(1,
572  ( (Src1.AggregateVal[_i].FloatVal !=
573  Src1.AggregateVal[_i].FloatVal) ||
574  (Src2.AggregateVal[_i].FloatVal !=
575  Src2.AggregateVal[_i].FloatVal)));
576  } else {
577  for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
578  Dest.AggregateVal[_i].IntVal = APInt(1,
579  ( (Src1.AggregateVal[_i].DoubleVal !=
580  Src1.AggregateVal[_i].DoubleVal) ||
581  (Src2.AggregateVal[_i].DoubleVal !=
582  Src2.AggregateVal[_i].DoubleVal)));
583  }
584  } else if (Ty->isFloatTy())
585  Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
586  Src2.FloatVal != Src2.FloatVal));
587  else {
588  Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
589  Src2.DoubleVal != Src2.DoubleVal));
590  }
591  return Dest;
592 }
593 
595  Type *Ty, const bool val) {
596  GenericValue Dest;
597  if(Ty->isVectorTy()) {
598  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
599  Dest.AggregateVal.resize( Src1.AggregateVal.size() );
600  for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
601  Dest.AggregateVal[_i].IntVal = APInt(1,val);
602  } else {
603  Dest.IntVal = APInt(1, val);
604  }
605 
606  return Dest;
607 }
608 
610  ExecutionContext &SF = ECStack.back();
611  Type *Ty = I.getOperand(0)->getType();
612  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
613  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
614  GenericValue R; // Result
615 
616  switch (I.getPredicate()) {
617  default:
618  dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
619  llvm_unreachable(nullptr);
620  break;
621  case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false);
622  break;
623  case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true);
624  break;
625  case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
626  case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
627  case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
628  case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
629  case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
630  case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
631  case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
632  case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
633  case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
634  case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
635  case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
636  case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
637  case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
638  case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
639  }
640 
641  SetValue(&I, R, SF);
642 }
643 
644 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
645  GenericValue Src2, Type *Ty) {
646  GenericValue Result;
647  switch (predicate) {
648  case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
649  case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
650  case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
651  case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
652  case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
653  case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
654  case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
655  case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
656  case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
657  case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
658  case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
659  case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
660  case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
661  case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
662  case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
663  case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
664  case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
665  case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
666  case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
667  case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
668  case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
669  case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
670  case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
671  case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
672  case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
673  case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true);
674  default:
675  dbgs() << "Unhandled Cmp predicate\n";
676  llvm_unreachable(nullptr);
677  }
678 }
679 
681  ExecutionContext &SF = ECStack.back();
682  Type *Ty = I.getOperand(0)->getType();
683  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
684  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
685  GenericValue R; // Result
686 
687  // First process vector operation
688  if (Ty->isVectorTy()) {
689  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
690  R.AggregateVal.resize(Src1.AggregateVal.size());
691 
692  // Macros to execute binary operation 'OP' over integer vectors
693 #define INTEGER_VECTOR_OPERATION(OP) \
694  for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
695  R.AggregateVal[i].IntVal = \
696  Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
697 
698  // Additional macros to execute binary operations udiv/sdiv/urem/srem since
699  // they have different notation.
700 #define INTEGER_VECTOR_FUNCTION(OP) \
701  for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
702  R.AggregateVal[i].IntVal = \
703  Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
704 
705  // Macros to execute binary operation 'OP' over floating point type TY
706  // (float or double) vectors
707 #define FLOAT_VECTOR_FUNCTION(OP, TY) \
708  for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \
709  R.AggregateVal[i].TY = \
710  Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
711 
712  // Macros to choose appropriate TY: float or double and run operation
713  // execution
714 #define FLOAT_VECTOR_OP(OP) { \
715  if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) \
716  FLOAT_VECTOR_FUNCTION(OP, FloatVal) \
717  else { \
718  if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \
719  FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \
720  else { \
721  dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
722  llvm_unreachable(0); \
723  } \
724  } \
725 }
726 
727  switch(I.getOpcode()){
728  default:
729  dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
730  llvm_unreachable(nullptr);
731  break;
733  case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break;
734  case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break;
735  case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break;
736  case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break;
737  case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break;
738  case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break;
739  case Instruction::And: INTEGER_VECTOR_OPERATION(&) break;
740  case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break;
741  case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break;
742  case Instruction::FAdd: FLOAT_VECTOR_OP(+) break;
743  case Instruction::FSub: FLOAT_VECTOR_OP(-) break;
744  case Instruction::FMul: FLOAT_VECTOR_OP(*) break;
745  case Instruction::FDiv: FLOAT_VECTOR_OP(/) break;
746  case Instruction::FRem:
747  if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
748  for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
749  R.AggregateVal[i].FloatVal =
750  fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
751  else {
752  if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
753  for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
754  R.AggregateVal[i].DoubleVal =
755  fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
756  else {
757  dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
758  llvm_unreachable(nullptr);
759  }
760  }
761  break;
762  }
763  } else {
764  switch (I.getOpcode()) {
765  default:
766  dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
767  llvm_unreachable(nullptr);
768  break;
769  case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
770  case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
771  case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
772  case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
773  case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
774  case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
775  case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
776  case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
777  case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
778  case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
779  case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
780  case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
781  case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
782  case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
783  case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
784  }
785  }
786  SetValue(&I, R, SF);
787 }
788 
790  GenericValue Src3, Type *Ty) {
791  GenericValue Dest;
792  if(Ty->isVectorTy()) {
793  assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
794  assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
795  Dest.AggregateVal.resize( Src1.AggregateVal.size() );
796  for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
797  Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
798  Src3.AggregateVal[i] : Src2.AggregateVal[i];
799  } else {
800  Dest = (Src1.IntVal == 0) ? Src3 : Src2;
801  }
802  return Dest;
803 }
804 
806  ExecutionContext &SF = ECStack.back();
807  Type * Ty = I.getOperand(0)->getType();
808  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
809  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
810  GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
811  GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
812  SetValue(&I, R, SF);
813 }
814 
815 //===----------------------------------------------------------------------===//
816 // Terminator Instruction Implementations
817 //===----------------------------------------------------------------------===//
818 
820  // runAtExitHandlers() assumes there are no stack frames, but
821  // if exit() was called, then it had a stack frame. Blow away
822  // the stack before interpreting atexit handlers.
823  ECStack.clear();
825  exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
826 }
827 
828 /// Pop the last stack frame off of ECStack and then copy the result
829 /// back into the result variable if we are not returning void. The
830 /// result variable may be the ExitValue, or the Value of the calling
831 /// CallInst if there was a previous stack frame. This method may
832 /// invalidate any ECStack iterators you have. This method also takes
833 /// care of switching to the normal destination BB, if we are returning
834 /// from an invoke.
835 ///
836 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
837  GenericValue Result) {
838  // Pop the current stack frame.
839  ECStack.pop_back();
840 
841  if (ECStack.empty()) { // Finished main. Put result into exit code...
842  if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
843  ExitValue = Result; // Capture the exit value of the program
844  } else {
845  memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
846  }
847  } else {
848  // If we have a previous stack frame, and we have a previous call,
849  // fill in the return value...
850  ExecutionContext &CallingSF = ECStack.back();
851  if (Instruction *I = CallingSF.Caller.getInstruction()) {
852  // Save result...
853  if (!CallingSF.Caller.getType()->isVoidTy())
854  SetValue(I, Result, CallingSF);
855  if (InvokeInst *II = dyn_cast<InvokeInst> (I))
856  SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
857  CallingSF.Caller = CallSite(); // We returned from the call...
858  }
859  }
860 }
861 
863  ExecutionContext &SF = ECStack.back();
864  Type *RetTy = Type::getVoidTy(I.getContext());
865  GenericValue Result;
866 
867  // Save away the return value... (if we are not 'ret void')
868  if (I.getNumOperands()) {
869  RetTy = I.getReturnValue()->getType();
870  Result = getOperandValue(I.getReturnValue(), SF);
871  }
872 
873  popStackAndReturnValueToCaller(RetTy, Result);
874 }
875 
877  report_fatal_error("Program executed an 'unreachable' instruction!");
878 }
879 
881  ExecutionContext &SF = ECStack.back();
882  BasicBlock *Dest;
883 
884  Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
885  if (!I.isUnconditional()) {
886  Value *Cond = I.getCondition();
887  if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
888  Dest = I.getSuccessor(1);
889  }
890  SwitchToNewBasicBlock(Dest, SF);
891 }
892 
894  ExecutionContext &SF = ECStack.back();
895  Value* Cond = I.getCondition();
896  Type *ElTy = Cond->getType();
897  GenericValue CondVal = getOperandValue(Cond, SF);
898 
899  // Check to see if any of the cases match...
900  BasicBlock *Dest = nullptr;
901  for (auto Case : I.cases()) {
902  GenericValue CaseVal = getOperandValue(Case.getCaseValue(), SF);
903  if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
904  Dest = cast<BasicBlock>(Case.getCaseSuccessor());
905  break;
906  }
907  }
908  if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
909  SwitchToNewBasicBlock(Dest, SF);
910 }
911 
913  ExecutionContext &SF = ECStack.back();
914  void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
915  SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
916 }
917 
918 
919 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
920 // This function handles the actual updating of block and instruction iterators
921 // as well as execution of all of the PHI nodes in the destination block.
922 //
923 // This method does this because all of the PHI nodes must be executed
924 // atomically, reading their inputs before any of the results are updated. Not
925 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
926 // their inputs. If the input PHI node is updated before it is read, incorrect
927 // results can happen. Thus we use a two phase approach.
928 //
929 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
930  BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
931  SF.CurBB = Dest; // Update CurBB to branch destination
932  SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
933 
934  if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
935 
936  // Loop over all of the PHI nodes in the current block, reading their inputs.
937  std::vector<GenericValue> ResultValues;
938 
939  for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
940  // Search for the value corresponding to this previous bb...
941  int i = PN->getBasicBlockIndex(PrevBB);
942  assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
943  Value *IncomingValue = PN->getIncomingValue(i);
944 
945  // Save the incoming value for this PHI node...
946  ResultValues.push_back(getOperandValue(IncomingValue, SF));
947  }
948 
949  // Now loop over all of the PHI nodes setting their values...
950  SF.CurInst = SF.CurBB->begin();
951  for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
952  PHINode *PN = cast<PHINode>(SF.CurInst);
953  SetValue(PN, ResultValues[i], SF);
954  }
955 }
956 
957 //===----------------------------------------------------------------------===//
958 // Memory Instruction Implementations
959 //===----------------------------------------------------------------------===//
960 
962  ExecutionContext &SF = ECStack.back();
963 
964  Type *Ty = I.getType()->getElementType(); // Type to be allocated
965 
966  // Get the number of elements being allocated by the array...
967  unsigned NumElements =
968  getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
969 
970  unsigned TypeSize = (size_t)getDataLayout().getTypeAllocSize(Ty);
971 
972  // Avoid malloc-ing zero bytes, use max()...
973  unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
974 
975  // Allocate enough memory to hold the type...
976  void *Memory = safe_malloc(MemToAlloc);
977 
978  LLVM_DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize
979  << " bytes) x " << NumElements << " (Total: " << MemToAlloc
980  << ") at " << uintptr_t(Memory) << '\n');
981 
982  GenericValue Result = PTOGV(Memory);
983  assert(Result.PointerVal && "Null pointer returned by malloc!");
984  SetValue(&I, Result, SF);
985 
986  if (I.getOpcode() == Instruction::Alloca)
987  ECStack.back().Allocas.add(Memory);
988 }
989 
990 // getElementOffset - The workhorse for getelementptr.
991 //
992 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
994  ExecutionContext &SF) {
995  assert(Ptr->getType()->isPointerTy() &&
996  "Cannot getElementOffset of a nonpointer type!");
997 
998  uint64_t Total = 0;
999 
1000  for (; I != E; ++I) {
1001  if (StructType *STy = I.getStructTypeOrNull()) {
1002  const StructLayout *SLO = getDataLayout().getStructLayout(STy);
1003 
1004  const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
1005  unsigned Index = unsigned(CPU->getZExtValue());
1006 
1007  Total += SLO->getElementOffset(Index);
1008  } else {
1009  // Get the index number for the array... which must be long type...
1010  GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
1011 
1012  int64_t Idx;
1013  unsigned BitWidth =
1014  cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
1015  if (BitWidth == 32)
1016  Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
1017  else {
1018  assert(BitWidth == 64 && "Invalid index type for getelementptr");
1019  Idx = (int64_t)IdxGV.IntVal.getZExtValue();
1020  }
1021  Total += getDataLayout().getTypeAllocSize(I.getIndexedType()) * Idx;
1022  }
1023  }
1024 
1025  GenericValue Result;
1026  Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
1027  LLVM_DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
1028  return Result;
1029 }
1030 
1032  ExecutionContext &SF = ECStack.back();
1033  SetValue(&I, executeGEPOperation(I.getPointerOperand(),
1034  gep_type_begin(I), gep_type_end(I), SF), SF);
1035 }
1036 
1038  ExecutionContext &SF = ECStack.back();
1039  GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1040  GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
1041  GenericValue Result;
1042  LoadValueFromMemory(Result, Ptr, I.getType());
1043  SetValue(&I, Result, SF);
1044  if (I.isVolatile() && PrintVolatile)
1045  dbgs() << "Volatile load " << I;
1046 }
1047 
1049  ExecutionContext &SF = ECStack.back();
1050  GenericValue Val = getOperandValue(I.getOperand(0), SF);
1051  GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
1052  StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
1053  I.getOperand(0)->getType());
1054  if (I.isVolatile() && PrintVolatile)
1055  dbgs() << "Volatile store: " << I;
1056 }
1057 
1058 //===----------------------------------------------------------------------===//
1059 // Miscellaneous Instruction Implementations
1060 //===----------------------------------------------------------------------===//
1061 
1063  ExecutionContext &SF = ECStack.back();
1064 
1065  // Check to see if this is an intrinsic function call...
1066  Function *F = CS.getCalledFunction();
1067  if (F && F->isDeclaration())
1068  switch (F->getIntrinsicID()) {
1070  break;
1071  case Intrinsic::vastart: { // va_start
1072  GenericValue ArgIndex;
1073  ArgIndex.UIntPairVal.first = ECStack.size() - 1;
1074  ArgIndex.UIntPairVal.second = 0;
1075  SetValue(CS.getInstruction(), ArgIndex, SF);
1076  return;
1077  }
1078  case Intrinsic::vaend: // va_end is a noop for the interpreter
1079  return;
1080  case Intrinsic::vacopy: // va_copy: dest = src
1081  SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
1082  return;
1083  default:
1084  // If it is an unknown intrinsic function, use the intrinsic lowering
1085  // class to transform it into hopefully tasty LLVM code.
1086  //
1088  BasicBlock *Parent = CS.getInstruction()->getParent();
1089  bool atBegin(Parent->begin() == me);
1090  if (!atBegin)
1091  --me;
1092  IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
1093 
1094  // Restore the CurInst pointer to the first instruction newly inserted, if
1095  // any.
1096  if (atBegin) {
1097  SF.CurInst = Parent->begin();
1098  } else {
1099  SF.CurInst = me;
1100  ++SF.CurInst;
1101  }
1102  return;
1103  }
1104 
1105 
1106  SF.Caller = CS;
1107  std::vector<GenericValue> ArgVals;
1108  const unsigned NumArgs = SF.Caller.arg_size();
1109  ArgVals.reserve(NumArgs);
1110  uint16_t pNum = 1;
1111  for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
1112  e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
1113  Value *V = *i;
1114  ArgVals.push_back(getOperandValue(V, SF));
1115  }
1116 
1117  // To handle indirect calls, we must get the pointer value from the argument
1118  // and treat it as a function pointer.
1119  GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
1120  callFunction((Function*)GVTOP(SRC), ArgVals);
1121 }
1122 
1123 // auxiliary function for shift operations
1124 static unsigned getShiftAmount(uint64_t orgShiftAmount,
1125  llvm::APInt valueToShift) {
1126  unsigned valueWidth = valueToShift.getBitWidth();
1127  if (orgShiftAmount < (uint64_t)valueWidth)
1128  return orgShiftAmount;
1129  // according to the llvm documentation, if orgShiftAmount > valueWidth,
1130  // the result is undfeined. but we do shift by this rule:
1131  return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;
1132 }
1133 
1134 
1136  ExecutionContext &SF = ECStack.back();
1137  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1138  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1139  GenericValue Dest;
1140  Type *Ty = I.getType();
1141 
1142  if (Ty->isVectorTy()) {
1143  uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1144  assert(src1Size == Src2.AggregateVal.size());
1145  for (unsigned i = 0; i < src1Size; i++) {
1146  GenericValue Result;
1147  uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1148  llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1149  Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1150  Dest.AggregateVal.push_back(Result);
1151  }
1152  } else {
1153  // scalar
1154  uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1155  llvm::APInt valueToShift = Src1.IntVal;
1156  Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
1157  }
1158 
1159  SetValue(&I, Dest, SF);
1160 }
1161 
1163  ExecutionContext &SF = ECStack.back();
1164  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1165  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1166  GenericValue Dest;
1167  Type *Ty = I.getType();
1168 
1169  if (Ty->isVectorTy()) {
1170  uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
1171  assert(src1Size == Src2.AggregateVal.size());
1172  for (unsigned i = 0; i < src1Size; i++) {
1173  GenericValue Result;
1174  uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1175  llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1176  Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1177  Dest.AggregateVal.push_back(Result);
1178  }
1179  } else {
1180  // scalar
1181  uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1182  llvm::APInt valueToShift = Src1.IntVal;
1183  Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
1184  }
1185 
1186  SetValue(&I, Dest, SF);
1187 }
1188 
1190  ExecutionContext &SF = ECStack.back();
1191  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1192  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1193  GenericValue Dest;
1194  Type *Ty = I.getType();
1195 
1196  if (Ty->isVectorTy()) {
1197  size_t src1Size = Src1.AggregateVal.size();
1198  assert(src1Size == Src2.AggregateVal.size());
1199  for (unsigned i = 0; i < src1Size; i++) {
1200  GenericValue Result;
1201  uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
1202  llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
1203  Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1204  Dest.AggregateVal.push_back(Result);
1205  }
1206  } else {
1207  // scalar
1208  uint64_t shiftAmount = Src2.IntVal.getZExtValue();
1209  llvm::APInt valueToShift = Src1.IntVal;
1210  Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
1211  }
1212 
1213  SetValue(&I, Dest, SF);
1214 }
1215 
1216 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
1217  ExecutionContext &SF) {
1218  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1219  Type *SrcTy = SrcVal->getType();
1220  if (SrcTy->isVectorTy()) {
1221  Type *DstVecTy = DstTy->getScalarType();
1222  unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1223  unsigned NumElts = Src.AggregateVal.size();
1224  // the sizes of src and dst vectors must be equal
1225  Dest.AggregateVal.resize(NumElts);
1226  for (unsigned i = 0; i < NumElts; i++)
1227  Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);
1228  } else {
1229  IntegerType *DITy = cast<IntegerType>(DstTy);
1230  unsigned DBitWidth = DITy->getBitWidth();
1231  Dest.IntVal = Src.IntVal.trunc(DBitWidth);
1232  }
1233  return Dest;
1234 }
1235 
1236 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
1237  ExecutionContext &SF) {
1238  Type *SrcTy = SrcVal->getType();
1239  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1240  if (SrcTy->isVectorTy()) {
1241  Type *DstVecTy = DstTy->getScalarType();
1242  unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1243  unsigned size = Src.AggregateVal.size();
1244  // the sizes of src and dst vectors must be equal.
1245  Dest.AggregateVal.resize(size);
1246  for (unsigned i = 0; i < size; i++)
1247  Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);
1248  } else {
1249  auto *DITy = cast<IntegerType>(DstTy);
1250  unsigned DBitWidth = DITy->getBitWidth();
1251  Dest.IntVal = Src.IntVal.sext(DBitWidth);
1252  }
1253  return Dest;
1254 }
1255 
1256 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
1257  ExecutionContext &SF) {
1258  Type *SrcTy = SrcVal->getType();
1259  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1260  if (SrcTy->isVectorTy()) {
1261  Type *DstVecTy = DstTy->getScalarType();
1262  unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1263 
1264  unsigned size = Src.AggregateVal.size();
1265  // the sizes of src and dst vectors must be equal.
1266  Dest.AggregateVal.resize(size);
1267  for (unsigned i = 0; i < size; i++)
1268  Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);
1269  } else {
1270  auto *DITy = cast<IntegerType>(DstTy);
1271  unsigned DBitWidth = DITy->getBitWidth();
1272  Dest.IntVal = Src.IntVal.zext(DBitWidth);
1273  }
1274  return Dest;
1275 }
1276 
1277 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
1278  ExecutionContext &SF) {
1279  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1280 
1281  if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1282  assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
1283  DstTy->getScalarType()->isFloatTy() &&
1284  "Invalid FPTrunc instruction");
1285 
1286  unsigned size = Src.AggregateVal.size();
1287  // the sizes of src and dst vectors must be equal.
1288  Dest.AggregateVal.resize(size);
1289  for (unsigned i = 0; i < size; i++)
1290  Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
1291  } else {
1292  assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
1293  "Invalid FPTrunc instruction");
1294  Dest.FloatVal = (float)Src.DoubleVal;
1295  }
1296 
1297  return Dest;
1298 }
1299 
1300 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
1301  ExecutionContext &SF) {
1302  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1303 
1304  if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1305  assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
1306  DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
1307 
1308  unsigned size = Src.AggregateVal.size();
1309  // the sizes of src and dst vectors must be equal.
1310  Dest.AggregateVal.resize(size);
1311  for (unsigned i = 0; i < size; i++)
1312  Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
1313  } else {
1314  assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
1315  "Invalid FPExt instruction");
1316  Dest.DoubleVal = (double)Src.FloatVal;
1317  }
1318 
1319  return Dest;
1320 }
1321 
1322 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
1323  ExecutionContext &SF) {
1324  Type *SrcTy = SrcVal->getType();
1325  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1326 
1327  if (SrcTy->getTypeID() == Type::VectorTyID) {
1328  Type *DstVecTy = DstTy->getScalarType();
1329  Type *SrcVecTy = SrcTy->getScalarType();
1330  uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1331  unsigned size = Src.AggregateVal.size();
1332  // the sizes of src and dst vectors must be equal.
1333  Dest.AggregateVal.resize(size);
1334 
1335  if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1336  assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1337  for (unsigned i = 0; i < size; i++)
1338  Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1339  Src.AggregateVal[i].FloatVal, DBitWidth);
1340  } else {
1341  for (unsigned i = 0; i < size; i++)
1342  Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1343  Src.AggregateVal[i].DoubleVal, DBitWidth);
1344  }
1345  } else {
1346  // scalar
1347  uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1348  assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
1349 
1350  if (SrcTy->getTypeID() == Type::FloatTyID)
1351  Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1352  else {
1353  Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1354  }
1355  }
1356 
1357  return Dest;
1358 }
1359 
1360 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
1361  ExecutionContext &SF) {
1362  Type *SrcTy = SrcVal->getType();
1363  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1364 
1365  if (SrcTy->getTypeID() == Type::VectorTyID) {
1366  Type *DstVecTy = DstTy->getScalarType();
1367  Type *SrcVecTy = SrcTy->getScalarType();
1368  uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
1369  unsigned size = Src.AggregateVal.size();
1370  // the sizes of src and dst vectors must be equal
1371  Dest.AggregateVal.resize(size);
1372 
1373  if (SrcVecTy->getTypeID() == Type::FloatTyID) {
1374  assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1375  for (unsigned i = 0; i < size; i++)
1376  Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
1377  Src.AggregateVal[i].FloatVal, DBitWidth);
1378  } else {
1379  for (unsigned i = 0; i < size; i++)
1380  Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
1381  Src.AggregateVal[i].DoubleVal, DBitWidth);
1382  }
1383  } else {
1384  // scalar
1385  unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1386  assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
1387 
1388  if (SrcTy->getTypeID() == Type::FloatTyID)
1389  Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
1390  else {
1391  Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
1392  }
1393  }
1394  return Dest;
1395 }
1396 
1397 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
1398  ExecutionContext &SF) {
1399  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1400 
1401  if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1402  Type *DstVecTy = DstTy->getScalarType();
1403  unsigned size = Src.AggregateVal.size();
1404  // the sizes of src and dst vectors must be equal
1405  Dest.AggregateVal.resize(size);
1406 
1407  if (DstVecTy->getTypeID() == Type::FloatTyID) {
1408  assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1409  for (unsigned i = 0; i < size; i++)
1410  Dest.AggregateVal[i].FloatVal =
1412  } else {
1413  for (unsigned i = 0; i < size; i++)
1414  Dest.AggregateVal[i].DoubleVal =
1416  }
1417  } else {
1418  // scalar
1419  assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
1420  if (DstTy->getTypeID() == Type::FloatTyID)
1422  else {
1424  }
1425  }
1426  return Dest;
1427 }
1428 
1429 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
1430  ExecutionContext &SF) {
1431  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1432 
1433  if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
1434  Type *DstVecTy = DstTy->getScalarType();
1435  unsigned size = Src.AggregateVal.size();
1436  // the sizes of src and dst vectors must be equal
1437  Dest.AggregateVal.resize(size);
1438 
1439  if (DstVecTy->getTypeID() == Type::FloatTyID) {
1440  assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1441  for (unsigned i = 0; i < size; i++)
1442  Dest.AggregateVal[i].FloatVal =
1444  } else {
1445  for (unsigned i = 0; i < size; i++)
1446  Dest.AggregateVal[i].DoubleVal =
1448  }
1449  } else {
1450  // scalar
1451  assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
1452 
1453  if (DstTy->getTypeID() == Type::FloatTyID)
1455  else {
1457  }
1458  }
1459 
1460  return Dest;
1461 }
1462 
1463 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
1464  ExecutionContext &SF) {
1465  uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
1466  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1467  assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
1468 
1469  Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
1470  return Dest;
1471 }
1472 
1473 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
1474  ExecutionContext &SF) {
1475  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1476  assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
1477 
1479  if (PtrSize != Src.IntVal.getBitWidth())
1480  Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
1481 
1483  return Dest;
1484 }
1485 
1486 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
1487  ExecutionContext &SF) {
1488 
1489  // This instruction supports bitwise conversion of vectors to integers and
1490  // to vectors of other types (as long as they have the same size)
1491  Type *SrcTy = SrcVal->getType();
1492  GenericValue Dest, Src = getOperandValue(SrcVal, SF);
1493 
1494  if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1495  (DstTy->getTypeID() == Type::VectorTyID)) {
1496  // vector src bitcast to vector dst or vector src bitcast to scalar dst or
1497  // scalar src bitcast to vector dst
1498  bool isLittleEndian = getDataLayout().isLittleEndian();
1499  GenericValue TempDst, TempSrc, SrcVec;
1500  Type *SrcElemTy;
1501  Type *DstElemTy;
1502  unsigned SrcBitSize;
1503  unsigned DstBitSize;
1504  unsigned SrcNum;
1505  unsigned DstNum;
1506 
1507  if (SrcTy->getTypeID() == Type::VectorTyID) {
1508  SrcElemTy = SrcTy->getScalarType();
1509  SrcBitSize = SrcTy->getScalarSizeInBits();
1510  SrcNum = Src.AggregateVal.size();
1511  SrcVec = Src;
1512  } else {
1513  // if src is scalar value, make it vector <1 x type>
1514  SrcElemTy = SrcTy;
1515  SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1516  SrcNum = 1;
1517  SrcVec.AggregateVal.push_back(Src);
1518  }
1519 
1520  if (DstTy->getTypeID() == Type::VectorTyID) {
1521  DstElemTy = DstTy->getScalarType();
1522  DstBitSize = DstTy->getScalarSizeInBits();
1523  DstNum = (SrcNum * SrcBitSize) / DstBitSize;
1524  } else {
1525  DstElemTy = DstTy;
1526  DstBitSize = DstTy->getPrimitiveSizeInBits();
1527  DstNum = 1;
1528  }
1529 
1530  if (SrcNum * SrcBitSize != DstNum * DstBitSize)
1531  llvm_unreachable("Invalid BitCast");
1532 
1533  // If src is floating point, cast to integer first.
1534  TempSrc.AggregateVal.resize(SrcNum);
1535  if (SrcElemTy->isFloatTy()) {
1536  for (unsigned i = 0; i < SrcNum; i++)
1537  TempSrc.AggregateVal[i].IntVal =
1538  APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);
1539 
1540  } else if (SrcElemTy->isDoubleTy()) {
1541  for (unsigned i = 0; i < SrcNum; i++)
1542  TempSrc.AggregateVal[i].IntVal =
1543  APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);
1544  } else if (SrcElemTy->isIntegerTy()) {
1545  for (unsigned i = 0; i < SrcNum; i++)
1546  TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
1547  } else {
1548  // Pointers are not allowed as the element type of vector.
1549  llvm_unreachable("Invalid Bitcast");
1550  }
1551 
1552  // now TempSrc is integer type vector
1553  if (DstNum < SrcNum) {
1554  // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
1555  unsigned Ratio = SrcNum / DstNum;
1556  unsigned SrcElt = 0;
1557  for (unsigned i = 0; i < DstNum; i++) {
1558  GenericValue Elt;
1559  Elt.IntVal = 0;
1560  Elt.IntVal = Elt.IntVal.zext(DstBitSize);
1561  unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
1562  for (unsigned j = 0; j < Ratio; j++) {
1563  APInt Tmp;
1564  Tmp = Tmp.zext(SrcBitSize);
1565  Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
1566  Tmp = Tmp.zext(DstBitSize);
1567  Tmp <<= ShiftAmt;
1568  ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
1569  Elt.IntVal |= Tmp;
1570  }
1571  TempDst.AggregateVal.push_back(Elt);
1572  }
1573  } else {
1574  // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
1575  unsigned Ratio = DstNum / SrcNum;
1576  for (unsigned i = 0; i < SrcNum; i++) {
1577  unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
1578  for (unsigned j = 0; j < Ratio; j++) {
1579  GenericValue Elt;
1580  Elt.IntVal = Elt.IntVal.zext(SrcBitSize);
1581  Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
1582  Elt.IntVal.lshrInPlace(ShiftAmt);
1583  // it could be DstBitSize == SrcBitSize, so check it
1584  if (DstBitSize < SrcBitSize)
1585  Elt.IntVal = Elt.IntVal.trunc(DstBitSize);
1586  ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
1587  TempDst.AggregateVal.push_back(Elt);
1588  }
1589  }
1590  }
1591 
1592  // convert result from integer to specified type
1593  if (DstTy->getTypeID() == Type::VectorTyID) {
1594  if (DstElemTy->isDoubleTy()) {
1595  Dest.AggregateVal.resize(DstNum);
1596  for (unsigned i = 0; i < DstNum; i++)
1597  Dest.AggregateVal[i].DoubleVal =
1598  TempDst.AggregateVal[i].IntVal.bitsToDouble();
1599  } else if (DstElemTy->isFloatTy()) {
1600  Dest.AggregateVal.resize(DstNum);
1601  for (unsigned i = 0; i < DstNum; i++)
1602  Dest.AggregateVal[i].FloatVal =
1603  TempDst.AggregateVal[i].IntVal.bitsToFloat();
1604  } else {
1605  Dest = TempDst;
1606  }
1607  } else {
1608  if (DstElemTy->isDoubleTy())
1609  Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
1610  else if (DstElemTy->isFloatTy()) {
1611  Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
1612  } else {
1613  Dest.IntVal = TempDst.AggregateVal[0].IntVal;
1614  }
1615  }
1616  } else { // if ((SrcTy->getTypeID() == Type::VectorTyID) ||
1617  // (DstTy->getTypeID() == Type::VectorTyID))
1618 
1619  // scalar src bitcast to scalar dst
1620  if (DstTy->isPointerTy()) {
1621  assert(SrcTy->isPointerTy() && "Invalid BitCast");
1622  Dest.PointerVal = Src.PointerVal;
1623  } else if (DstTy->isIntegerTy()) {
1624  if (SrcTy->isFloatTy())
1625  Dest.IntVal = APInt::floatToBits(Src.FloatVal);
1626  else if (SrcTy->isDoubleTy()) {
1627  Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
1628  } else if (SrcTy->isIntegerTy()) {
1629  Dest.IntVal = Src.IntVal;
1630  } else {
1631  llvm_unreachable("Invalid BitCast");
1632  }
1633  } else if (DstTy->isFloatTy()) {
1634  if (SrcTy->isIntegerTy())
1635  Dest.FloatVal = Src.IntVal.bitsToFloat();
1636  else {
1637  Dest.FloatVal = Src.FloatVal;
1638  }
1639  } else if (DstTy->isDoubleTy()) {
1640  if (SrcTy->isIntegerTy())
1641  Dest.DoubleVal = Src.IntVal.bitsToDouble();
1642  else {
1643  Dest.DoubleVal = Src.DoubleVal;
1644  }
1645  } else {
1646  llvm_unreachable("Invalid Bitcast");
1647  }
1648  }
1649 
1650  return Dest;
1651 }
1652 
1654  ExecutionContext &SF = ECStack.back();
1655  SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
1656 }
1657 
1659  ExecutionContext &SF = ECStack.back();
1660  SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
1661 }
1662 
1664  ExecutionContext &SF = ECStack.back();
1665  SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
1666 }
1667 
1669  ExecutionContext &SF = ECStack.back();
1670  SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
1671 }
1672 
1674  ExecutionContext &SF = ECStack.back();
1675  SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
1676 }
1677 
1679  ExecutionContext &SF = ECStack.back();
1680  SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1681 }
1682 
1684  ExecutionContext &SF = ECStack.back();
1685  SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
1686 }
1687 
1689  ExecutionContext &SF = ECStack.back();
1690  SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
1691 }
1692 
1694  ExecutionContext &SF = ECStack.back();
1695  SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
1696 }
1697 
1699  ExecutionContext &SF = ECStack.back();
1700  SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
1701 }
1702 
1704  ExecutionContext &SF = ECStack.back();
1705  SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
1706 }
1707 
1709  ExecutionContext &SF = ECStack.back();
1710  SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
1711 }
1712 
1713 #define IMPLEMENT_VAARG(TY) \
1714  case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
1715 
1717  ExecutionContext &SF = ECStack.back();
1718 
1719  // Get the incoming valist parameter. LLI treats the valist as a
1720  // (ec-stack-depth var-arg-index) pair.
1721  GenericValue VAList = getOperandValue(I.getOperand(0), SF);
1722  GenericValue Dest;
1723  GenericValue Src = ECStack[VAList.UIntPairVal.first]
1724  .VarArgs[VAList.UIntPairVal.second];
1725  Type *Ty = I.getType();
1726  switch (Ty->getTypeID()) {
1727  case Type::IntegerTyID:
1728  Dest.IntVal = Src.IntVal;
1729  break;
1730  IMPLEMENT_VAARG(Pointer);
1731  IMPLEMENT_VAARG(Float);
1732  IMPLEMENT_VAARG(Double);
1733  default:
1734  dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
1735  llvm_unreachable(nullptr);
1736  }
1737 
1738  // Set the Value of this Instruction.
1739  SetValue(&I, Dest, SF);
1740 
1741  // Move the pointer to the next vararg.
1742  ++VAList.UIntPairVal.second;
1743 }
1744 
1746  ExecutionContext &SF = ECStack.back();
1747  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1748  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1749  GenericValue Dest;
1750 
1751  Type *Ty = I.getType();
1752  const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
1753 
1754  if(Src1.AggregateVal.size() > indx) {
1755  switch (Ty->getTypeID()) {
1756  default:
1757  dbgs() << "Unhandled destination type for extractelement instruction: "
1758  << *Ty << "\n";
1759  llvm_unreachable(nullptr);
1760  break;
1761  case Type::IntegerTyID:
1762  Dest.IntVal = Src1.AggregateVal[indx].IntVal;
1763  break;
1764  case Type::FloatTyID:
1765  Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
1766  break;
1767  case Type::DoubleTyID:
1768  Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
1769  break;
1770  }
1771  } else {
1772  dbgs() << "Invalid index in extractelement instruction\n";
1773  }
1774 
1775  SetValue(&I, Dest, SF);
1776 }
1777 
1779  ExecutionContext &SF = ECStack.back();
1780  VectorType *Ty = cast<VectorType>(I.getType());
1781 
1782  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1783  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1784  GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1785  GenericValue Dest;
1786 
1787  Type *TyContained = Ty->getElementType();
1788 
1789  const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
1790  Dest.AggregateVal = Src1.AggregateVal;
1791 
1792  if(Src1.AggregateVal.size() <= indx)
1793  llvm_unreachable("Invalid index in insertelement instruction");
1794  switch (TyContained->getTypeID()) {
1795  default:
1796  llvm_unreachable("Unhandled dest type for insertelement instruction");
1797  case Type::IntegerTyID:
1798  Dest.AggregateVal[indx].IntVal = Src2.IntVal;
1799  break;
1800  case Type::FloatTyID:
1801  Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
1802  break;
1803  case Type::DoubleTyID:
1804  Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
1805  break;
1806  }
1807  SetValue(&I, Dest, SF);
1808 }
1809 
1811  ExecutionContext &SF = ECStack.back();
1812 
1813  VectorType *Ty = cast<VectorType>(I.getType());
1814 
1815  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
1816  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1817  GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
1818  GenericValue Dest;
1819 
1820  // There is no need to check types of src1 and src2, because the compiled
1821  // bytecode can't contain different types for src1 and src2 for a
1822  // shufflevector instruction.
1823 
1824  Type *TyContained = Ty->getElementType();
1825  unsigned src1Size = (unsigned)Src1.AggregateVal.size();
1826  unsigned src2Size = (unsigned)Src2.AggregateVal.size();
1827  unsigned src3Size = (unsigned)Src3.AggregateVal.size();
1828 
1829  Dest.AggregateVal.resize(src3Size);
1830 
1831  switch (TyContained->getTypeID()) {
1832  default:
1833  llvm_unreachable("Unhandled dest type for insertelement instruction");
1834  break;
1835  case Type::IntegerTyID:
1836  for( unsigned i=0; i<src3Size; i++) {
1837  unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1838  if(j < src1Size)
1839  Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
1840  else if(j < src1Size + src2Size)
1841  Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
1842  else
1843  // The selector may not be greater than sum of lengths of first and
1844  // second operands and llasm should not allow situation like
1845  // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
1846  // <2 x i32> < i32 0, i32 5 >,
1847  // where i32 5 is invalid, but let it be additional check here:
1848  llvm_unreachable("Invalid mask in shufflevector instruction");
1849  }
1850  break;
1851  case Type::FloatTyID:
1852  for( unsigned i=0; i<src3Size; i++) {
1853  unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1854  if(j < src1Size)
1855  Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
1856  else if(j < src1Size + src2Size)
1857  Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
1858  else
1859  llvm_unreachable("Invalid mask in shufflevector instruction");
1860  }
1861  break;
1862  case Type::DoubleTyID:
1863  for( unsigned i=0; i<src3Size; i++) {
1864  unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
1865  if(j < src1Size)
1866  Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
1867  else if(j < src1Size + src2Size)
1868  Dest.AggregateVal[i].DoubleVal =
1869  Src2.AggregateVal[j-src1Size].DoubleVal;
1870  else
1871  llvm_unreachable("Invalid mask in shufflevector instruction");
1872  }
1873  break;
1874  }
1875  SetValue(&I, Dest, SF);
1876 }
1877 
1879  ExecutionContext &SF = ECStack.back();
1880  Value *Agg = I.getAggregateOperand();
1881  GenericValue Dest;
1882  GenericValue Src = getOperandValue(Agg, SF);
1883 
1885  unsigned Num = I.getNumIndices();
1886  GenericValue *pSrc = &Src;
1887 
1888  for (unsigned i = 0 ; i < Num; ++i) {
1889  pSrc = &pSrc->AggregateVal[*IdxBegin];
1890  ++IdxBegin;
1891  }
1892 
1893  Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1894  switch (IndexedType->getTypeID()) {
1895  default:
1896  llvm_unreachable("Unhandled dest type for extractelement instruction");
1897  break;
1898  case Type::IntegerTyID:
1899  Dest.IntVal = pSrc->IntVal;
1900  break;
1901  case Type::FloatTyID:
1902  Dest.FloatVal = pSrc->FloatVal;
1903  break;
1904  case Type::DoubleTyID:
1905  Dest.DoubleVal = pSrc->DoubleVal;
1906  break;
1907  case Type::ArrayTyID:
1908  case Type::StructTyID:
1909  case Type::VectorTyID:
1910  Dest.AggregateVal = pSrc->AggregateVal;
1911  break;
1912  case Type::PointerTyID:
1913  Dest.PointerVal = pSrc->PointerVal;
1914  break;
1915  }
1916 
1917  SetValue(&I, Dest, SF);
1918 }
1919 
1921 
1922  ExecutionContext &SF = ECStack.back();
1923  Value *Agg = I.getAggregateOperand();
1924 
1925  GenericValue Src1 = getOperandValue(Agg, SF);
1926  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
1927  GenericValue Dest = Src1; // Dest is a slightly changed Src1
1928 
1930  unsigned Num = I.getNumIndices();
1931 
1932  GenericValue *pDest = &Dest;
1933  for (unsigned i = 0 ; i < Num; ++i) {
1934  pDest = &pDest->AggregateVal[*IdxBegin];
1935  ++IdxBegin;
1936  }
1937  // pDest points to the target value in the Dest now
1938 
1939  Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
1940 
1941  switch (IndexedType->getTypeID()) {
1942  default:
1943  llvm_unreachable("Unhandled dest type for insertelement instruction");
1944  break;
1945  case Type::IntegerTyID:
1946  pDest->IntVal = Src2.IntVal;
1947  break;
1948  case Type::FloatTyID:
1949  pDest->FloatVal = Src2.FloatVal;
1950  break;
1951  case Type::DoubleTyID:
1952  pDest->DoubleVal = Src2.DoubleVal;
1953  break;
1954  case Type::ArrayTyID:
1955  case Type::StructTyID:
1956  case Type::VectorTyID:
1957  pDest->AggregateVal = Src2.AggregateVal;
1958  break;
1959  case Type::PointerTyID:
1960  pDest->PointerVal = Src2.PointerVal;
1961  break;
1962  }
1963 
1964  SetValue(&I, Dest, SF);
1965 }
1966 
1967 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
1968  ExecutionContext &SF) {
1969  switch (CE->getOpcode()) {
1970  case Instruction::Trunc:
1971  return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
1972  case Instruction::ZExt:
1973  return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
1974  case Instruction::SExt:
1975  return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
1976  case Instruction::FPTrunc:
1977  return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
1978  case Instruction::FPExt:
1979  return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
1980  case Instruction::UIToFP:
1981  return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
1982  case Instruction::SIToFP:
1983  return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
1984  case Instruction::FPToUI:
1985  return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
1986  case Instruction::FPToSI:
1987  return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
1988  case Instruction::PtrToInt:
1989  return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
1990  case Instruction::IntToPtr:
1991  return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
1992  case Instruction::BitCast:
1993  return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
1994  case Instruction::GetElementPtr:
1995  return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
1996  gep_type_end(CE), SF);
1997  case Instruction::FCmp:
1998  case Instruction::ICmp:
1999  return executeCmpInst(CE->getPredicate(),
2000  getOperandValue(CE->getOperand(0), SF),
2001  getOperandValue(CE->getOperand(1), SF),
2002  CE->getOperand(0)->getType());
2003  case Instruction::Select:
2004  return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
2005  getOperandValue(CE->getOperand(1), SF),
2006  getOperandValue(CE->getOperand(2), SF),
2007  CE->getOperand(0)->getType());
2008  default :
2009  break;
2010  }
2011 
2012  // The cases below here require a GenericValue parameter for the result
2013  // so we initialize one, compute it and then return it.
2014  GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
2015  GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
2016  GenericValue Dest;
2017  Type * Ty = CE->getOperand(0)->getType();
2018  switch (CE->getOpcode()) {
2019  case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
2020  case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
2021  case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
2022  case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
2023  case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
2024  case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
2025  case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
2026  case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
2027  case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
2028  case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
2029  case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
2030  case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
2031  case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
2032  case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
2033  case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
2034  case Instruction::Shl:
2035  Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
2036  break;
2037  case Instruction::LShr:
2038  Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
2039  break;
2040  case Instruction::AShr:
2041  Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
2042  break;
2043  default:
2044  dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
2045  llvm_unreachable("Unhandled ConstantExpr");
2046  }
2047  return Dest;
2048 }
2049 
2050 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
2051  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
2052  return getConstantExprValue(CE, SF);
2053  } else if (Constant *CPV = dyn_cast<Constant>(V)) {
2054  return getConstantValue(CPV);
2055  } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2056  return PTOGV(getPointerToGlobal(GV));
2057  } else {
2058  return SF.Values[V];
2059  }
2060 }
2061 
2062 //===----------------------------------------------------------------------===//
2063 // Dispatch and Execution Code
2064 //===----------------------------------------------------------------------===//
2065 
2066 //===----------------------------------------------------------------------===//
2067 // callFunction - Execute the specified function...
2068 //
2070  assert((ECStack.empty() || !ECStack.back().Caller.getInstruction() ||
2071  ECStack.back().Caller.arg_size() == ArgVals.size()) &&
2072  "Incorrect number of arguments passed into function call!");
2073  // Make a new stack frame... and fill it in.
2074  ECStack.emplace_back();
2075  ExecutionContext &StackFrame = ECStack.back();
2076  StackFrame.CurFunction = F;
2077 
2078  // Special handling for external functions.
2079  if (F->isDeclaration()) {
2080  GenericValue Result = callExternalFunction (F, ArgVals);
2081  // Simulate a 'ret' instruction of the appropriate type.
2082  popStackAndReturnValueToCaller (F->getReturnType (), Result);
2083  return;
2084  }
2085 
2086  // Get pointers to first LLVM BB & Instruction in function.
2087  StackFrame.CurBB = &F->front();
2088  StackFrame.CurInst = StackFrame.CurBB->begin();
2089 
2090  // Run through the function arguments and initialize their values...
2091  assert((ArgVals.size() == F->arg_size() ||
2092  (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
2093  "Invalid number of values passed to function invocation!");
2094 
2095  // Handle non-varargs arguments...
2096  unsigned i = 0;
2097  for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
2098  AI != E; ++AI, ++i)
2099  SetValue(&*AI, ArgVals[i], StackFrame);
2100 
2101  // Handle varargs arguments...
2102  StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
2103 }
2104 
2105 
2107  while (!ECStack.empty()) {
2108  // Interpret a single instruction & increment the "PC".
2109  ExecutionContext &SF = ECStack.back(); // Current stack frame
2110  Instruction &I = *SF.CurInst++; // Increment before execute
2111 
2112  // Track the number of dynamic instructions executed.
2113  ++NumDynamicInsts;
2114 
2115  LLVM_DEBUG(dbgs() << "About to interpret: " << I << "\n");
2116  visit(I); // Dispatch to one of the visit* methods...
2117  }
2118 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
IterTy arg_end() const
Definition: CallSite.h:583
Return a value (possibly void), from a function.
void visitVAArgInst(VAArgInst &I)
Definition: Execution.cpp:1716
PointerTy PointerVal
Definition: GenericValue.h:31
std::vector< GenericValue > AggregateVal
Definition: GenericValue.h:37
static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:235
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1209
This instruction extracts a struct member or array element value from an aggregate value...
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:833
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
Value * getAggregateOperand()
double RoundAPIntToDouble(const APInt &APIVal)
Converts the given APInt to a double value.
Definition: APInt.h:2134
static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:514
IterTy arg_begin() const
Definition: CallSite.h:579
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BinaryOps getOpcode() const
Definition: InstrTypes.h:316
void visitAllocaInst(AllocaInst &I)
Definition: Execution.cpp:961
void visitStoreInst(StoreInst &I)
Definition: Execution.cpp:1048
float RoundAPIntToFloat(const APInt &APIVal)
Converts the given APInt to a float vlalue.
Definition: APInt.h:2146
unsigned getNumIndices() const
static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF)
Definition: Execution.cpp:41
iterator begin() const
Definition: ArrayRef.h:136
2: 32-bit floating point type
Definition: Type.h:58
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1590
void visitTruncInst(TruncInst &I)
Definition: Execution.cpp:1653
void visitFPToUIInst(FPToUIInst &I)
Definition: Execution.cpp:1688
unsigned char Untyped[8]
Definition: GenericValue.h:33
static void executeFDivInst(GenericValue &Dest, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:87
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:50
This class represents zero extension of integer types.
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:607
void visitGetElementPtrInst(GetElementPtrInst &I)
Definition: Execution.cpp:1031
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:857
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1519
BasicBlock::iterator CurInst
Definition: Interpreter.h:63
#define MASK_VECTOR_NANS(TY, X, Y, FLAG)
Definition: Execution.cpp:363
Value * getCondition() const
gep_type_iterator gep_type_end(const User *GEP)
unsigned less or equal
Definition: InstrTypes.h:672
unsigned less than
Definition: InstrTypes.h:671
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:652
static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:532
std::map< Value *, GenericValue > Values
Definition: Interpreter.h:66
This instruction constructs a fixed permutation of two input vectors.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:709
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:662
void visitExtractElementInst(ExtractElementInst &I)
Definition: Execution.cpp:1745
BasicBlock * getSuccessor(unsigned i) const
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:810
void visitShl(BinaryOperator &I)
Definition: Execution.cpp:1135
arg_iterator arg_end()
Definition: Function.h:682
13: Structures
Definition: Type.h:72
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:388
STATISTIC(NumFunctions, "Total number of functions")
F(f)
This class represents a sign extension of integer types.
An instruction for reading from memory.
Definition: Instructions.h:167
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:111
const DataLayout & getDataLayout() const
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:875
Value * getCondition() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:229
#define IMPLEMENT_VECTOR_FCMP(OP)
Definition: Execution.cpp:316
15: Pointers
Definition: Type.h:74
static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:428
unsigned getPredicate() const
Return the ICMP or FCMP predicate value.
Definition: Constants.cpp:1197
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1508
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:268
static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:207
static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:477
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:657
void visitFCmpInst(FCmpInst &I)
Definition: Execution.cpp:609
void * PointerTy
Definition: GenericValue.h:21
ArrayRef< unsigned > getIndices() const
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:554
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:656
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:231
static Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This class represents the LLVM &#39;select&#39; instruction.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
static APInt doubleToBits(double V)
Converts a double to APInt bits.
Definition: APInt.h:1730
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:137
PointerType * getType() const
Overload to return most specific pointer type.
Definition: Instructions.h:96
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:161
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:992
void * getPointerToGlobal(const GlobalValue *GV)
getPointerToGlobal - This returns the address of the specified global value.
Class to represent struct types.
Definition: DerivedTypes.h:232
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
ValTy * getCalledValue() const
Return the pointer to function that is being called.
Definition: CallSite.h:104
void visitSelectInst(SelectInst &I)
Definition: Execution.cpp:805
#define IMPLEMENT_BINARY_OPERATOR(OP, TY)
Definition: Execution.cpp:49
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:196
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:653
Type * getType() const
Return the type of the instruction that generated this call site.
Definition: CallSite.h:272
static cl::opt< bool > PrintVolatile("interpreter-print-volatile", cl::Hidden, cl::desc("make the interpreter print every volatile load and store"))
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:977
static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:137
This file implements a class to represent arbitrary precision integral constant values and operations...
This class represents a cast from a pointer to an integer.
#define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY)
Definition: Execution.cpp:118
static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:263
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:89
InstrTy * getInstruction() const
Definition: CallSite.h:96
void visitInsertElementInst(InsertElementInst &I)
Definition: Execution.cpp:1778
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
void visitFPTruncInst(FPTruncInst &I)
Definition: Execution.cpp:1668
#define IMPLEMENT_SCALAR_NANS(TY, X, Y)
Definition: Execution.cpp:338
static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:165
void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, Type *Ty)
FIXME: document.
This instruction compares its operands according to the predicate given to the constructor.
bool isVarArg() const
Definition: DerivedTypes.h:122
This class represents a no-op cast from one type to another.
void callFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Definition: Execution.cpp:2069
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
void visitInsertValueInst(InsertValueInst &I)
Definition: Execution.cpp:1920
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:232
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:125
GenericValue getConstantValue(const Constant *C)
Converts a Constant* into a GenericValue, including handling of ConstantExpr values.
An instruction for storing to memory.
Definition: Instructions.h:320
This class represents a cast from floating point to signed integer.
float RoundSignedAPIntToFloat(const APInt &APIVal)
Converts the given APInt to a float value.
Definition: APInt.h:2153
static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:221
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:65
void visitBitCastInst(BitCastInst &I)
Definition: Execution.cpp:1708
VectorType * getType() const
Overload to return most specific vector type.
void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, Type *Ty)
StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:249
This class represents a truncation of integer types.
Value * getOperand(unsigned i) const
Definition: User.h:169
#define IMPLEMENT_VECTOR_UNORDERED(TY, X, Y, FUNC)
Definition: Execution.cpp:467
#define IMPLEMENT_VAARG(TY)
Definition: Execution.cpp:1713
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:303
11: Arbitrary bit width integers
Definition: Type.h:70
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:140
bool isFloatTy() const
Return true if this is &#39;float&#39;, a 32-bit IEEE fp type.
Definition: Type.h:146
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:873
void visitLShr(BinaryOperator &I)
Definition: Execution.cpp:1162
static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:179
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
void visitICmpInst(ICmpInst &I)
Definition: Execution.cpp:277
This instruction inserts a single (scalar) element into a VectorType value.
static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:523
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:148
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1612
static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:193
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Conditional or Unconditional Branch instruction.
void visitCallSite(CallSite CS)
Definition: Execution.cpp:1062
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
This function has undefined behavior.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:223
Indirect Branch Instruction.
static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:487
void visitIndirectBrInst(IndirectBrInst &I)
Definition: Execution.cpp:912
unsigned getNumIndices() const
BasicBlock * getDefaultDest() const
void visitZExtInst(ZExtInst &I)
Definition: Execution.cpp:1663
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:160
static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:151
static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:374
void exitCalled(GenericValue GV)
Definition: Execution.cpp:819
static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:400
This instruction compares its operands according to the predicate given to the constructor.
size_t arg_size() const
Definition: Function.h:700
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:655
Value * getPointerOperand()
Definition: Instructions.h:284
static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:505
arg_iterator arg_begin()
Definition: Function.h:673
struct IntPair UIntPairVal
Definition: GenericValue.h:32
Class to represent integer types.
Definition: DerivedTypes.h:39
Function * CurFunction
Definition: Interpreter.h:61
BasicBlock * CurBB
Definition: Interpreter.h:62
void visitExtractValueInst(ExtractValueInst &I)
Definition: Execution.cpp:1878
This class represents a cast from an integer to a pointer.
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:663
void visitFPToSIInst(FPToSIInst &I)
Definition: Execution.cpp:1693
void visitIntToPtrInst(IntToPtrInst &I)
Definition: Execution.cpp:1703
void visitFPExtInst(FPExtInst &I)
Definition: Execution.cpp:1673
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:639
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
void visitUnreachableInst(UnreachableInst &I)
Definition: Execution.cpp:876
static void executeFAddInst(GenericValue &Dest, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:54
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:661
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:414
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:970
signed greater than
Definition: InstrTypes.h:673
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
idx_iterator idx_begin() const
static void executeFRemInst(GenericValue &Dest, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:98
14: Arrays
Definition: Type.h:73
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:650
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:946
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
Iterator for intrusive lists based on ilist_node.
void visitShuffleVectorInst(ShuffleVectorInst &I)
Definition: Execution.cpp:1810
unsigned getNumOperands() const
Definition: User.h:191
std::vector< GenericValue > VarArgs
Definition: Interpreter.h:67
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1166
void visitReturnInst(ReturnInst &I)
Definition: Execution.cpp:862
16: SIMD &#39;packed&#39; format, or other vector type
Definition: Type.h:75
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:129
void visitSExtInst(SExtInst &I)
Definition: Execution.cpp:1658
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:660
unsigned arg_size() const
Definition: CallSite.h:226
iterator end() const
Definition: ArrayRef.h:137
signed less than
Definition: InstrTypes.h:675
This class represents a cast from floating point to unsigned integer.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:193
static void executeFSubInst(GenericValue &Dest, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:65
static void executeFMulInst(GenericValue &Dest, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:76
static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:644
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
signed less or equal
Definition: InstrTypes.h:676
Class to represent vector types.
Definition: DerivedTypes.h:424
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
Class for arbitrary precision integers.
Definition: APInt.h:69
void visitPtrToIntInst(PtrToIntInst &I)
Definition: Execution.cpp:1698
void runAtExitHandlers()
runAtExitHandlers - Run any functions registered by the program&#39;s calls to atexit(3), which we intercept and store in AtExitHandlers.
Definition: Interpreter.cpp:70
APInt RoundFloatToAPInt(float Float, unsigned width)
Converts a float value into a APInt.
Definition: APInt.h:2165
void visitBranchInst(BranchInst &I)
Definition: Execution.cpp:880
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:461
#define FLOAT_VECTOR_OP(OP)
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:721
void visitAShr(BinaryOperator &I)
Definition: Execution.cpp:1189
bool isVolatile() const
Return true if this is a store to a volatile memory location.
Definition: Instructions.h:353
static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, GenericValue Src3, Type *Ty)
Definition: Execution.cpp:789
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:576
void LowerIntrinsicCall(CallInst *CI)
Replace a call to the specified intrinsic function.
static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:496
unsigned greater or equal
Definition: InstrTypes.h:670
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1682
#define I(x, y, z)
Definition: MD5.cpp:58
#define IMPLEMENT_INTEGER_ICMP(OP, TY)
Definition: Execution.cpp:113
void visitSIToFPInst(SIToFPInst &I)
Definition: Execution.cpp:1683
This class represents a cast unsigned integer to floating point.
#define INTEGER_VECTOR_OPERATION(OP)
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:654
static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:563
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
idx_iterator idx_begin() const
This instruction extracts a single (scalar) element from a VectorType value.
void visitBinaryOperator(BinaryOperator &I)
Definition: Execution.cpp:680
static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2, Type *Ty, const bool val)
Definition: Execution.cpp:594
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
bool isUnconditional() const
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:658
void visitLoadInst(LoadInst &I)
Definition: Execution.cpp:1037
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:205
float bitsToFloat() const
Converts APInt bits to a double.
Definition: APInt.h:1722
3: 64-bit floating point type
Definition: Type.h:59
Multiway switch.
This class represents a cast from signed integer to floating point.
#define IMPLEMENT_POINTER_ICMP(OP)
Definition: Execution.cpp:131
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BasicBlock & front() const
Definition: Function.h:665
This class represents a truncation of floating point types.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:114
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:649
#define IMPLEMENT_UNORDERED(TY, X, Y)
Definition: Execution.cpp:456
ArrayRef< unsigned > getIndices() const
LLVM Value Representation.
Definition: Value.h:72
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:659
void visitUIToFPInst(UIToFPInst &I)
Definition: Execution.cpp:1678
#define INTEGER_VECTOR_FUNCTION(OP)
Invoke instruction.
GenericValue callExternalFunction(Function *F, ArrayRef< GenericValue > ArgVals)
Type * getElementType() const
Definition: DerivedTypes.h:391
void visitSwitchInst(SwitchInst &I)
Definition: Execution.cpp:893
unsigned greater than
Definition: InstrTypes.h:669
static unsigned getShiftAmount(uint64_t orgShiftAmount, llvm::APInt valueToShift)
Definition: Execution.cpp:1124
static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:324
#define IMPLEMENT_FCMP(OP, TY)
Definition: Execution.cpp:303
APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
Definition: APInt.cpp:714
#define LLVM_DEBUG(X)
Definition: Debug.h:122
This class represents an extension of floating point types.
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:651
static APInt floatToBits(float V)
Converts a float to APInt bits.
Definition: APInt.h:1738
bool isDoubleTy() const
Return true if this is &#39;double&#39;, a 64-bit IEEE fp type.
Definition: Type.h:149
double bitsToDouble() const
Converts APInt bits to a double.
Definition: APInt.h:1713
VectorType * getType() const
Overload to return most specific vector type.
Value * getPointerOperand()
Definition: Instructions.h:412
double RoundSignedAPIntToDouble(const APInt &APIVal)
Converts the given APInt to a double value.
Definition: APInt.h:2141
Type * getElementType() const
Definition: DerivedTypes.h:517
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:648
signed greater or equal
Definition: InstrTypes.h:674
static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2, Type *Ty)
Definition: Execution.cpp:442
const BasicBlock * getParent() const
Definition: Instruction.h:66
an instruction to allocate memory on the stack
Definition: Instructions.h:59
This instruction inserts a struct field of array element value into an aggregate value.
gep_type_iterator gep_type_begin(const User *GEP)